ezmlmx 0.68
ezmlmx
Loading...
Searching...
No Matches
tagmsg.c
Go to the documentation of this file.
1#include "stralloc.h"
2#include "scan.h"
3#include "fmt.h"
4#include "logmsg.h"
5#include "cookie.h"
6#include "slurp.h"
7#include "errtxt.h"
8#include "subscribe.h"
9#include "makehash.h"
10#include "idx.h"
11#include <unistd.h>
12#include <libpq-fe.h>
13
14#define WHO "tagmsg"
15
21
22extern PGconn *pgsql;
23
24static stralloc line = {0};
25static stralloc key = {0};
26static char hash[COOKIE];
27static char strnum[FMT_ULONG]; /* message number as sz */
28
45
46void tagmsg(const char *dir,unsigned long msgnum,const char *seed,const char *action,
47 char *hashout,unsigned long bodysize,unsigned long chunk)
48{
49 PGresult *result;
50 PGresult *result2; /* Need for dupicate check */
51 const char *table = (char *) 0;
52 const char *ret;
53 unsigned int i;
54
55 strnum[fmt_ulong(strnum,msgnum)] = '\0'; /* message nr ->string*/
56
57 switch (openreadclose("key",&key,32)) {
58 case -1: logmsg(WHO,111,FATAL,B("ERR_READ :","key: "));
59 case 0: logmsg(WHO,100,FATAL,B("key"," ERR_NOEXIST"));
60 }
61
62 cookie(hash,key.s,key.len,strnum,seed,action);
63 for (i = 0; i < COOKIE; i++)
64 hashout[i] = hash[i];
65
66 if ((ret = opensql(dir,&table))) {
67 if (*ret) logmsg(WHO,2x(111,FATAL,ret);
68 return; /* no sql => success */
69 } else {
70 if (chunk >= 53L) chunk = 0L; /* sanity */
71
72 /* INSERT INTO table_cookie (msgnum,cookie) VALUES (num,cookie) */
73 /* (we may have tried message before, but failed to complete, so */
74 /* ER_DUP_ENTRY is ok) */
75
76 if (!stralloc_copys(&line,"INSERT INTO ")) die_nomem();
77 if (!stralloc_cats(&line,table)) die_nomem();
78 if (!stralloc_cats(&line,"_cookie (msgnum,cookie,bodysize,chunk) VALUES (")) die_nomem();
79 if (!stralloc_cats(&line,strnum)) die_nomem();
80 if (!stralloc_cats(&line,",'")) die_nomem();
81 if (!stralloc_catb(&line,hash,COOKIE)) die_nomem();
82 if (!stralloc_cats(&line,"',")) die_nomem();
83 if (!stralloc_catb(&line,strnum,fmt_ulong(strnum,bodysize))) die_nomem();
84 if (!stralloc_cats(&line,",")) die_nomem();
85 if (!stralloc_catb(&line,strnum,fmt_ulong(strnum,chunk))) die_nomem();
86 if (!stralloc_cats(&line,")")) die_nomem();
87 if (!stralloc_0(&line)) die_nomem();
88
89 result = PQexec(pgsql,line.s);
90 if (result == NULL)
91 logmsg(WHO,111,FATAL,PQerrorMessage(pgsql));
92
93 if (PQresultStatus(result) != PGRES_COMMAND_OK) { /* Possible duplicate */
94 if (!stralloc_copys(&line,"SELECT msgnum FROM ")) die_nomem();
95 if (!stralloc_cats(&line,table)) die_nomem();
96 if (!stralloc_cats(&line,"_cookie WHERE msgnum = ")) die_nomem();
97 if (!stralloc_catb(&line,strnum,fmt_ulong(strnum,msgnum))) die_nomem();
98 if (!stralloc_0(&line)) die_nomem();
99
100 /* Query */
101
102 result2 = PQexec(pgsql,line.s);
103 if (result2 == NULL)
104 logmsg(WHO,111,FATAL,PQerrorMessage(pgsql));
105 if (PQresultStatus(result2) != PGRES_TUPLES_OK)
106 logmsg(WHO,111,FATAL,PQresultErrorMessage(result2));
107
108 /* No duplicate, return ERROR from first query */
109
110 if (PQntuples(result2) < 1)
111 logmsg(WHO,111,FATAL,PQresultErrorMessage(result));
112
113 PQclear(result2);
114 }
115 PQclear(result);
116
117 if (! (ret = logmsg(dir,msgnum,0L,0L,1))) return; /* log done=1*/
118 if (*ret) logmsg(WHO,111,FATAL,ret);
119 }
120
121 return;
122}
#define COOKIE
Definition cookie.h:4
Error messages. If you translate these, I would urge you to keep the English version as well....
const char * opensql(const char *dir, const char **table)
Definition opensql.c:13
void die_nomem()
Definition getconf.c:17
#define WHO
Definition author.c:1
stralloc seed
Definition ezmlm-get.c:74
char hashout[COOKIE]
Definition ezmlm-get.c:90
char * dir
void cookie(char *hash, const char *key, unsigned int keylen, const char *date, const char *addr, const char *action)
Definition cookie.c:14
unsigned long msgnum
stralloc action
Definition ezmlm-store.c:84
PGconn * pgsql
Definition opensql.c:22
const char * logmsg(const char *dir, unsigned long num, unsigned long listno, unsigned long subs, int done)
Definition loginfo.c:32
void tagmsg(const char *dir, unsigned long msgnum, const char *seed, const char *action, char *hashout, unsigned long bodysize, unsigned long chunk)
Definition tagmsg.c:45