78 const char *event,
int flagsql,
int forcehash,
const char *tab)
85 char szhash[3] =
"00";
86 const char *r = (
char *) 0;
87 const char *table = (
char *) 0;
88 const char **ptable = &table;
92 unsigned char ch, lcch;
99 if (tab) ptable = &tab;
101 if (!flagsql || (r =
opensql(dbname,ptable))) {
115 case_lowerb(
addr.s + j + 1,
addr.len - j - 1);
117 case_lowerb(lcaddr.s + 1,j - 1);
119 if (forcehash >= 0 && forcehash <= 52) {
120 ch = lcch = (
unsigned char) forcehash;
124 for (j = 0;j <
addr.len;++j) {
125 h = (h + (h << 5)) ^ (uint32) (
unsigned char)
addr.s[j];
126 lch = (lch + (lch << 5)) ^ (uint32) (
unsigned char) lcaddr.s[j];
128 lcch = 64 + (lch % 53);
135 if (!stralloc_copys(&fnlock,dbname))
die_nomem();
137 if (!stralloc_cats(&
fn,
"/subscribers/"))
die_nomem();
144 if (!stralloc_cats(&fnlock,
"/lock"))
die_nomem();
153 fdnew = open_trunc(
fnnew.s);
159 fd = open_read(
fn.s);
161 if (errno != ENOENT) { close(fdnew);
die_read(); }
167 if (getln(&
bi,&line,&
match,
'\0') == -1) {
171 if (line.len ==
addr.len)
172 if (!case_diffb(line.s,line.len,
addr.s)) {
177 if (buffer_bput(&
bo,line.s,line.len) == -1) {
185 if (flagadd && !flagwasthere)
190 if (buffer_flush(&
bo) == -1) { close(fdnew);
die_write(); }
191 if (fsync(fdnew) == -1) { close(fdnew);
die_write(); }
194 if (rename(
fnnew.s,
fn.s) == -1)
197 if ((ch == lcch) || flagwasthere) {
199 if (flagadd ^ flagwasthere) {
211 fn.s[
fn.len - 2] = ch;
213 fdnew = open_trunc(
fnnew.s);
217 fd = open_read(
fn.s);
219 if (errno != ENOENT) { close(fdnew);
die_read(); }
224 if (getln(&
bi,&line,&
match,
'\0') == -1)
227 if (line.len ==
addr.len)
228 if (!case_diffb(line.s,line.len,
addr.s)) {
232 if (buffer_bput(&
bo,line.s,line.len) == -1)
239 if (buffer_flush(&
bo) == -1) { close(fdnew);
die_write(); }
240 if (fsync(fdnew) == -1) { close(fdnew);
die_write(); }
243 if (rename(
fnnew.s,
fn.s) == -1)
247 if (flagadd ^ flagwasthere) {
264 case_lowerb(cpat + 1,
addr.len - j - 1);
268 case_lowerb(lcaddr.s,j);
270 for (j = 0;j < lcaddr.len;++j) {
271 h = (h + (h << 5)) ^ (uint32) (
unsigned char) lcaddr.s[j];
275 ch = (forcehash % 100);
277 szhash[0] =
'0' + ch / 10;
278 szhash[1] =
'0' + (ch % 10);
281 if (!stralloc_copys(&line,
"SELECT address FROM "))
die_nomem();
282 if (!stralloc_cats(&line,table))
die_nomem();
283 if (!stralloc_cats(&line,
" WHERE address ~* '^"))
die_nomem();
285 if (!stralloc_cats(&line,
"$'"))
die_nomem();
288 result = PQexec(
pgsql,line.s);
292 if (PQresultStatus(result) != PGRES_TUPLES_OK)
293 logmsg(
WHO,111,FATAL,PQresultErrorMessage(result));
295 if (PQntuples(result)>0) {
300 if (!stralloc_copys(&line,
"INSERT INTO "))
die_nomem();
301 if (!stralloc_cats(&line,table))
die_nomem();
302 if (!stralloc_cats(&line,
" (address,hash) VALUES ('"))
die_nomem();
304 if (!stralloc_cats(&line,
"',"))
die_nomem();
305 if (!stralloc_cats(&line,szhash))
die_nomem();
306 if (!stralloc_cats(&line,
")"))
die_nomem();
309 result = PQexec(
pgsql,line.s);
313 if (PQresultStatus(result) != PGRES_COMMAND_OK)
314 logmsg(
WHO,111,FATAL,PQresultErrorMessage(result));
317 if (!stralloc_copys(&line,
"DELETE FROM "))
die_nomem();
318 if (!stralloc_cats(&line,table))
die_nomem();
319 if (!stralloc_cats(&line,
" WHERE address ~* '^"))
die_nomem();
322 if (forcehash >= 0) {
323 if (!stralloc_cats(&line,
"$' AND hash="))
die_nomem();
324 if (!stralloc_cats(&line,szhash))
die_nomem();
326 if (!stralloc_cats(&line,
"$' AND hash BETWEEN 0 AND 52"))
331 result = PQexec(
pgsql,line.s);
335 if (PQresultStatus(result) != PGRES_COMMAND_OK)
336 logmsg(
WHO,111,FATAL,PQresultErrorMessage(result));
337 if (atoi(PQcmdTuples(result)) < 1)
346 if (!stralloc_copys(&logline,
"INSERT INTO "))
die_nomem();
347 if (!stralloc_cats(&logline,table))
die_nomem();
348 if (!stralloc_cats(&logline,
"_slog (address,edir,etype,fromline) VALUES ('"))
die_nomem();
352 if (!stralloc_cats(&logline,
"','+','"))
die_nomem();
354 if (!stralloc_cats(&logline,
"','-','"))
die_nomem();
357 if (!stralloc_catb(&logline,event+1,1))
die_nomem();
358 if (!stralloc_cats(&logline,
"','"))
die_nomem();
362 if (!stralloc_cats(&logline,
"')"))
die_nomem();
365 result = PQexec(
pgsql,logline.s);
368 if (!stralloc_0(&
addr))
int subscribe(const char *dbname, const char *userhost, int flagadd, const char *comment, const char *event, int flagmysql, int forcehash, const char *tab)