1 /*$Id: tagmsg.c,v 1.2 1999/10/07 23:31:01 lindberg Exp $*/
2 /*$Name: ezmlm-idx-040 $*/
10 #include "subscribe.h"
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 */
20 static void die_nomem(fatal
)
23 strerr_die2x(100,fatal
,ERR_NOMEM
);
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). */
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
;
45 PGresult
*result2
; /* Need for dupicate check */
46 char *table
= (char *) 0;
50 strnum
[fmt_ulong(strnum
,msgnum
)] = '\0'; /* message nr ->string*/
52 switch(slurp("key",&key
,32)) {
54 strerr_die3sys(111,fatal
,ERR_READ
,"key: ");
56 strerr_die3x(100,fatal
,"key",ERR_NOEXIST
);
58 cookie(hash
,key
.s
,key
.len
,strnum
,seed
,action
);
59 for (i
= 0; i
< COOKIE
; i
++)
62 if ((ret
= opensql(dir
,&table
))) {
63 if (*ret
) strerr_die2x(111,fatal
,ret
);
64 return; /* no sql => success */
67 if (chunk
>= 53L) chunk
= 0L; /* sanity */
69 /* INSERT INTO table_cookie (msgnum,cookie) VALUES (num,cookie) */
70 /* (we may have tried message before, but failed to complete, so */
71 /* ER_DUP_ENTRY is ok) */
72 if (!stralloc_copys(&line
,"INSERT INTO ")) die_nomem(fatal
);
73 if (!stralloc_cats(&line
,table
)) die_nomem(fatal
);
74 if (!stralloc_cats(&line
,"_cookie (msgnum,cookie,bodysize,chunk) VALUES ("))
76 if (!stralloc_cats(&line
,strnum
)) die_nomem(fatal
);
77 if (!stralloc_cats(&line
,",'")) die_nomem(fatal
);
78 if (!stralloc_catb(&line
,hash
,COOKIE
)) die_nomem(fatal
);
79 if (!stralloc_cats(&line
,"',")) die_nomem(fatal
);
80 if (!stralloc_catb(&line
,strnum
,fmt_ulong(strnum
,bodysize
)))
82 if (!stralloc_cats(&line
,",")) die_nomem(fatal
);
83 if (!stralloc_catb(&line
,strnum
,fmt_ulong(strnum
,chunk
))) die_nomem(fatal
);
84 if (!stralloc_cats(&line
,")")) die_nomem(fatal
);
86 if (!stralloc_0(&line
)) die_nomem(fatal
);
87 result
= PQexec(psql
,line
.s
);
89 strerr_die2x(111,fatal
,PQerrorMessage(psql
));
90 if (PQresultStatus(result
) != PGRES_COMMAND_OK
) { /* Possible tuplicate */
91 if (!stralloc_copys(&line
,"SELECT msgnum FROM ")) die_nomem(fatal
);
92 if (!stralloc_cats(&line
,table
)) die_nomem(fatal
);
93 if (!stralloc_cats(&line
,"_cookie WHERE msgnum = ")) die_nomem(fatal
);
94 if (!stralloc_catb(&line
,strnum
,fmt_ulong(strnum
,msgnum
)))
97 if (!stralloc_0(&line
)) die_nomem(fatal
);
98 result2
= PQexec(psql
,line
.s
);
100 strerr_die2x(111,fatal
,PQerrorMessage(psql
));
101 if (PQresultStatus(result2
) != PGRES_TUPLES_OK
)
102 strerr_die2x(111,fatal
,PQresultErrorMessage(result2
));
103 /* No duplicate, return ERROR from first query */
104 if (PQntuples(result2
)<1)
105 strerr_die2x(111,fatal
,PQresultErrorMessage(result
));
110 if (! (ret
= logmsg(dir
,msgnum
,0L,0L,1))) return; /* log done=1*/
111 if (*ret
) strerr_die2x(111,fatal
,ret
);