ezmlmx 0.68
ezmlmx
Loading...
Searching...
No Matches
putsubs.c
Go to the documentation of this file.
1#include <unistd.h>
2#include "getln.h"
3#include "error.h"
4#include "logmsg.h"
5#include "readwrite.h"
6#include "str.h"
7#include "fmt.h"
8#include "stralloc.h"
9#include "open.h"
10#include "buffer.h"
11#include "case.h"
12#include "errtxt.h"
13#include "subscribe.h"
14#include "qmail.h"
15#include "idx.h"
16#include <mysql.h>
17
23
24extern MYSQL *mysql;
25
26static buffer bi;
27static char inbuf[512];
28static char strnum[FMT_ULONG];
29static stralloc line = {0};
30static stralloc fn = {0};
31
32static void die_write(void) { logmsg(WHO,111,FATAL,B("ERR_WRITE :","stdout"); }
33
50
51unsigned long putsubs(const char *dbname,unsigned long hash_lo,unsigned long hash_hi,int subwrite(),int flagsql)
52{
53 MYSQL_RES *result;
54 MYSQL_ROW row;
55 const char *table = (char *) 0;
56 unsigned long *lengths;
57
58 unsigned int i;
59 int fd;
60 unsigned long no = 0L;
61 int match;
62 unsigned int hashpos;
63 const char *ret = (char *) 0;
64
65 if (!flagsql || (ret = opensql(dbname,&table))) {
66 if (flagsql && *ret) logmsg(WHO,111,FATAL,ret); /* fallback to local db */
67 if (!stralloc_copys(&fn,dbname)) die_nomem();
68 if (!stralloc_catb(&fn,"/subscribers/?",15))
69 die_nomem(); /* NOTE: Also copies terminal '\0' */
70 hashpos = fn.len - 2;
71 if (hash_lo > 52) hash_lo = 52;
72 if (hash_hi > 52) hash_hi = 52;
74
75 for (i = hash_lo;i <= hash_hi;++i) {
76 fn.s[hashpos] = 64 + i; /* hash range 0-52 */
77 fd = open_read(fn.s);
78 if (fd == -1) {
79 if (errno != error_noent)
80 logmsg(WHO,111,FATAL,B("ERR_READ :",fn.s));
81 } else {
82 buffer_init(&bi,buffer_unixread,fd,inbuf,sizeof(inbuf));
83 for (;;) {
84 if (getln(&bi,&line,&match,'\0') == -1)
85 logmsg(WHO,111,FATAL,B("ERR_READ :",fn.s));
86 if (!match)
87 break;
88 if (subwrite(line.s + 1,line.len - 2) == -1) die_write();
89 no++;
90 }
91 close(fd);
92 }
93 }
94 return no;
95 } else { /* SQL Version: main query */
96 if (!stralloc_copys(&line,"SELECT address FROM ")) die_nomem();
97 if (!stralloc_cats(&line,table)) die_nomem();
98 if (!stralloc_cats(&line," WHERE hash BETWEEN ")) die_nomem();
99 if (!stralloc_catb(&line,strnum,fmt_ulong(strnum,hash_lo))) die_nomem();
100 if (!stralloc_cats(&line," AND ")) die_nomem();
101 if (!stralloc_catb(&line,strnum,fmt_ulong(strnum,hash_hi))) die_nomem();
102
103 if (mysql_real_query(mysql,line.s,line.len)) /* query */
104 logmsg(WHO,111,FATAL,mysql_error(mysql));
105
106 if (!(result = mysql_use_result(mysql)))
107 logmsg(WHO,111,FATAL,mysql_error(mysql));
108 no = 0;
109
110 /* Next statements are safe; even if someone messes with the address field def */
111
112 while ((row = mysql_fetch_row(result))) {
113 if (!(lengths = mysql_fetch_lengths(result)))
114 logmsg(WHO,111,FATAL,mysql_error(mysql));
115 if (subwrite(row[0],lengths[0]) == -1) die_write();
116 no++; /* count for list-list fxn */
117 }
118 if (!mysql_eof(result))
119 logmsg(WHO,111,FATAL,mysql_error(mysql));
120
121 mysql_free_result(result);
122
123 return no;
124 }
125}
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 fn
char inbuf[1024]
buffer bi
unsigned long putsubs(const char *dbname, unsigned long hash_lo, unsigned long hash_hi, int subwrite(), int flagsql)
Definition putsubs.c:49
unsigned long hash_lo
Definition ezmlm-send.c:87
unsigned long hash_hi
Definition ezmlm-send.c:88
int fd
Definition ezmlm-cgi.c:141
int match
Definition ezmlm-cgi.c:140
int subwrite(char *s, unsigned int l)
Definition ezmlm-list.c:29
MYSQL * mysql
Definition opensql.c:23
const char * logmsg(const char *dir, unsigned long num, unsigned long listno, unsigned long subs, int done)
Definition loginfo.c:32
void die_write(void)
Definition subscribe.c:41