ezmlmx 0.68
ezmlmx
Loading...
Searching...
No Matches
getlistno.c
Go to the documentation of this file.
1#include "stralloc.h"
2#include "fmt.h"
3#include "str.h"
4#include "logmsg.h"
5#include "slurp.h"
6#include "errtxt.h"
7#include "subscribe.h"
8#include "ezmlm.h"
9#include <mysql/mysql.h>
10
18
19static stralloc line = {0};
20static stralloc quoted = {0};
21static char strnum[FMT_ULONG]; /* message number as sz */
22
38
39char *getlistno(char *dir,unsigned long msgnum,char * listname)
40{
41 MYSQL_RES *result;
42 MYSQL_ROW row;
43 unsigned long *lengths;
44 char *table = "ezmlm";
45 char *sublistname = (char *) 0;
46 char *ret;
47 unsigned int len;
48
49 if (!listname) /* if we spec listname we want a new one */
50 if (listno.len) return (char *) 0; /* otherwise, use the one we've got */
51
52 /* We don't check dir against previous call. We assume that all SQL */
53 /* using dbs of a list (e.g. subscribers, "allow", "digest" have the*/
54 /* same "name"). If the dir/sql files have differnt names, this will*/
55 /* get screwed up. Document! */
56
57 if ((ret = opensql(dir,&table,&sublistname))) return ret;
58 if (listname) sublistname = listname;
59 if (!sublistname) {
60 if (!stralloc_copys(&listno,"0")) return ERR_NOMEM;
61 return "";
62 }
63
64 strnum[fmt_ulong(strnum,msgnum)] = '\0'; /* message nr ->string*/
65
66 /* select listno from list_name where name='sublist' AND notuse = 0 */
67 /* AND (msgnum BETWEEN msgnum_lo AND msgnum_hi) ORDER BY listno */
68 /* LIMIT 1. So, lowest listno matching 'sublist' and active for this*/
69 /* message number. There may be more than one entry for a list, but */
70 /* we always log to the lowest active listno */
71
72 if (!stralloc_copys(&line,"SELECT listno FROM ")) return ERR_NOMEM;
73 if (!stralloc_cats(&line,table)) return ERR_NOMEM;
74 if (!stralloc_cats(&line,"_name WHERE name='")) return ERR_NOMEM;
75 len = str_len(sublistname);
76 if (!stralloc_ready(&quoted,2 * len + 1)) return ERR_NOMEM;
77 quoted.len = mysql_escape_string(quoted.s,sublistname,len);
78 if (!stralloc_cat(&line,&quoted)) return ERR_NOMEM;
79 if (!stralloc_cats(&line,"' AND notuse = 0 AND (")) return ERR_NOMEM;
80 if (!stralloc_cats(&line,strnum)) return ERR_NOMEM; /* msg nr */
81 if (!stralloc_cats(&line," BETWEEN msgnum_lo AND msgnum_hi)"
82 " ORDER BY listno LIMIT 1")) return ERR_NOMEM;
83
84 if (mysql_real_query((MYSQL *) psql,line.s,line.len) != 0) /* lno query */
85 return mysql_error((MYSQL *) psql);
86
87 if (!(result = mysql_use_result((MYSQL *) psql))) /* use result */
88 return mysql_error((MYSQL *) psql);
89
90 if (!(row = mysql_fetch_row(result))) {
91 if (!mysql_eof(result)) /* eof => no entry. Otherwise error! */
92 return mysql_error((MYSQL *) psql);
93 else { /* set to "0" if none found. */
94 if (!stralloc_copys(&listno,"0")) return ERR_NOMEM;
95 return "";
96 }
97 }
98
99 if (!(lengths = mysql_fetch_lengths(result)))
100 return mysql_error((MYSQL *) psql);
101 if (!stralloc_copyb(&listno,row[0],lengths[0])) return ERR_NOMEM;
102 mysql_free_result(result); /* free result */
103
104 return (char *) 0;
105}
Error messages. If you translate these, I would urge you to keep the English version as well....
#define ERR_NOMEM
Definition errtxt.h:14
charset, outhost, outlocal and flagcd are shared
const char * opensql(const char *dir, const char **table)
Definition opensql.c:13
stralloc listno
Definition ezmlm-get.c:78
char * dir
char * getlistno(char *dir, unsigned long msgnum, char *listname)
Definition getlistno.c:13
void ** psql
unsigned int len
Definition ezmlm-cron.c:68
stralloc listname
Definition ezmlm-cgi.c:132
unsigned long msgnum
stralloc quoted
Definition ezmlm-clean.c:90