Import ezmlm-idx 0.40
[ezmlm] / sub_mysql / tagmsg.c
1 /*$Id: tagmsg.c,v 1.11 1999/11/10 04:08:27 lindberg Exp $*/
2 /*$Name: ezmlm-idx-040 $*/
3 #include "stralloc.h"
4 #include "scan.h"
5 #include "fmt.h"
6 #include "strerr.h"
7 #include "cookie.h"
8 #include "slurp.h"
9 #include "errtxt.h"
10 #include "subscribe.h"
11 #include "makehash.h"
12 #include <mysql.h>
13 #include <mysqld_error.h>
14
15 static stralloc line = {0};
16 static stralloc key = {0};
17 static char hash[COOKIE];
18 static char strnum[FMT_ULONG]; /* message number as sz */
19
20 static void die_nomem(fatal)
21 char *fatal;
22 {
23 strerr_die2x(100,fatal,ERR_NOMEM);
24 }
25
26 void tagmsg(dir,msgnum,seed,action,hashout,bodysize,chunk,fatal)
27 /* This routine creates a cookie from num,seed and the */
28 /* list key and returns that cookie in hashout. The use of sender/num and */
29 /* first char of action is used to make cookie differ between messages, */
30 /* the key is the secret list key. The cookie will be inserted into */
31 /* table_cookie where table and other data is taken from dir/sql. We log */
32 /* arrival of the message (done=0). */
33
34 char *dir; /* db base dir */
35 unsigned long msgnum; /* number of this message */
36 char *seed; /* seed. NULL ok, but less entropy */
37 char *action; /* to make it certain the cookie differs from*/
38 /* one used for a digest */
39 char *hashout; /* calculated hash goes here */
40 unsigned long bodysize;
41 unsigned long chunk;
42 char *fatal;
43 {
44 char *table = (char *) 0;
45 char *ret;
46 unsigned int i;
47
48 strnum[fmt_ulong(strnum,msgnum)] = '\0'; /* message nr ->string*/
49
50 switch(slurp("key",&key,32)) {
51 case -1:
52 strerr_die3sys(111,fatal,ERR_READ,"key: ");
53 case 0:
54 strerr_die3x(100,fatal,"key",ERR_NOEXIST);
55 }
56 cookie(hash,key.s,key.len,strnum,seed,action);
57 for (i = 0; i < COOKIE; i++)
58 hashout[i] = hash[i];
59
60 if ((ret = opensql(dir,&table))) {
61 if (*ret) strerr_die2x(111,fatal,ret);
62 return; /* no sql => success */
63
64 } else {
65 if (chunk >= 53L) chunk = 0L; /* sanity */
66
67 /* INSERT INTO table_cookie (msgnum,cookie) VALUES (num,cookie) */
68 /* (we may have tried message before, but failed to complete, so */
69 /* ER_DUP_ENTRY is ok) */
70 if (!stralloc_copys(&line,"INSERT INTO ")) die_nomem(fatal);
71 if (!stralloc_cats(&line,table)) die_nomem(fatal);
72 if (!stralloc_cats(&line,"_cookie (msgnum,cookie,bodysize,chunk) VALUES ("))
73 die_nomem(fatal);
74 if (!stralloc_cats(&line,strnum)) die_nomem(fatal);
75 if (!stralloc_cats(&line,",'")) die_nomem(fatal);
76 if (!stralloc_catb(&line,hash,COOKIE)) die_nomem(fatal);
77 if (!stralloc_cats(&line,"',")) die_nomem(fatal);
78 if (!stralloc_catb(&line,strnum,fmt_ulong(strnum,bodysize)))
79 die_nomem(fatal);
80 if (!stralloc_cats(&line,",")) die_nomem(fatal);
81 if (!stralloc_catb(&line,strnum,fmt_ulong(strnum,chunk))) die_nomem(fatal);
82 if (!stralloc_cats(&line,")")) die_nomem(fatal);
83 if (mysql_real_query((MYSQL *) psql,line.s,line.len) != 0)
84 if (mysql_errno((MYSQL *) psql) != ER_DUP_ENTRY) /* ignore dups */
85 strerr_die2x(111,fatal,mysql_error((MYSQL *) psql)); /* cookie query */
86
87 if (! (ret = logmsg(dir,msgnum,0L,0L,1))) return; /* log done=1*/
88 if (*ret) strerr_die2x(111,fatal,ret);
89 }
90
91 return;
92 }