summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpiaip <piaip@63ad8ddf-47c3-0310-b6dd-a9e9d9715204>2009-06-19 18:39:25 +0800
committerpiaip <piaip@63ad8ddf-47c3-0310-b6dd-a9e9d9715204>2009-06-19 18:39:25 +0800
commit5ec0b0c8dc4b9aa479f8010f7a94aa3d99b0aac5 (patch)
tree1fff81f99998e464662b39e7f515ecd2fa2480a8
parent7f2af2544f444e62db177d0848928a485c0cfcf4 (diff)
downloadpttbbs-5ec0b0c8dc4b9aa479f8010f7a94aa3d99b0aac5.tar
pttbbs-5ec0b0c8dc4b9aa479f8010f7a94aa3d99b0aac5.tar.gz
pttbbs-5ec0b0c8dc4b9aa479f8010f7a94aa3d99b0aac5.tar.bz2
pttbbs-5ec0b0c8dc4b9aa479f8010f7a94aa3d99b0aac5.tar.lz
pttbbs-5ec0b0c8dc4b9aa479f8010f7a94aa3d99b0aac5.tar.xz
pttbbs-5ec0b0c8dc4b9aa479f8010f7a94aa3d99b0aac5.tar.zst
pttbbs-5ec0b0c8dc4b9aa479f8010f7a94aa3d99b0aac5.zip
* move emaildb to standalone regmaildb
* integrate the daemon structure to daemons.h git-svn-id: http://opensvn.csie.org/pttbbs/trunk/pttbbs@4671 63ad8ddf-47c3-0310-b6dd-a9e9d9715204
-rw-r--r--daemon/logind/loginc.c2
-rw-r--r--daemon/logind/logind.c8
-rw-r--r--daemon/regmaild/Makefile39
-rw-r--r--daemon/regmaild/regmailc.c59
-rw-r--r--daemon/regmaild/regmaild.c377
-rw-r--r--include/daemons.h56
-rw-r--r--include/logind.h28
-rw-r--r--mbbsd/Makefile7
-rw-r--r--mbbsd/emaildb.c271
-rw-r--r--mbbsd/mbbsd.c2
10 files changed, 583 insertions, 266 deletions
diff --git a/daemon/logind/loginc.c b/daemon/logind/loginc.c
index 1de9b5f8..901ee59c 100644
--- a/daemon/logind/loginc.c
+++ b/daemon/logind/loginc.c
@@ -4,7 +4,7 @@
#include <unistd.h>
#include "bbs.h"
-#include "logind.h"
+#include "daemons.h"
// standalone client to test logind
diff --git a/daemon/logind/logind.c b/daemon/logind/logind.c
index 2f7253bf..04ad99e5 100644
--- a/daemon/logind/logind.c
+++ b/daemon/logind/logind.c
@@ -34,7 +34,7 @@
#include "bbs.h"
#include "banip.h"
-#include "logind.h"
+#include "daemons.h"
#ifndef LOGIND_REGULAR_CHECK_DURATION
#define LOGIND_REGULAR_CHECK_DURATION (15)
@@ -2236,7 +2236,11 @@ main(int argc, char *argv[], char *envp[])
}
// warning: after daemonize, the directory was changed to root (/)...
- fprintf(stderr, LOG_PREFIX "start event dispatch.\r\n");
+ {
+ time4_t tnow = time(0);
+ fprintf(stderr, LOG_PREFIX "%s: start event dispatch.\r\n",
+ Cdate(&tnow));
+ }
event_dispatch();
return 0;
diff --git a/daemon/regmaild/Makefile b/daemon/regmaild/Makefile
new file mode 100644
index 00000000..81891121
--- /dev/null
+++ b/daemon/regmaild/Makefile
@@ -0,0 +1,39 @@
+# $Id$
+SRCROOT= ../..
+.include "$(SRCROOT)/pttbbs.mk"
+
+PROGS= regmaild
+PROGRAMS= regmaild regmailc
+UTILDIR= $(SRCROOT)/util
+UTILOBJ= $(UTILDIR)/util_var.o
+
+LDLIBS+=$(SRCROOT)/common/bbs/libcmbbs.a \
+ $(SRCROOT)/common/sys/libcmsys.a \
+ $(SRCROOT)/common/osdep/libosdep.a \
+ -lsqlite3
+
+all: ${PROGRAMS}
+
+.SUFFIXES: .c .cpp .o
+.c.o:
+ $(CC) $(CFLAGS) -c $*.c
+.cpp.o:
+ $(CXX) $(CXXFLAGS) -c $*.cpp
+
+regmaild: regmaild.o
+ ${CC} ${CFLAGS} ${LDFLAGS} -levent -o $@ $> $(UTILOBJ) $(LDLIBS)
+
+regmailc: regmailc.o
+ ${CC} ${CFLAGS} ${LDFLAGS} -levent -o $@ $> $(LDLIBS)
+
+install: $(PROGS)
+ install -d $(BBSHOME)/bin/
+ install -c -m 755 $(PROGS) $(BBSHOME)/bin/
+
+# TODO FIXME
+initemaildb: regmaild.c
+ $(CC) -DINIT_MAIN $(CFLAGS) $(LDFLAGS) -o initemaildb regmaild.c $(LDLIBS)
+
+
+clean:
+ rm -f *~ ${PROGRAMS} regmaild.o regmailc.o
diff --git a/daemon/regmaild/regmailc.c b/daemon/regmaild/regmailc.c
new file mode 100644
index 00000000..cd5d2be8
--- /dev/null
+++ b/daemon/regmaild/regmailc.c
@@ -0,0 +1,59 @@
+// $Id$
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "bbs.h"
+#include "daemons.h"
+
+// standalone client to test fromd
+
+int main(int argc, char *argv[])
+{
+ int fd, ret = 0;
+ int op = 0;
+ regmaildb_req req = {0};
+
+ if (argc < 4) {
+ fprintf(stderr, "Usage: %s operation userid email\n", argv[0]);
+ return 0;
+ }
+
+ if ( (fd = toconnect(REGMAILD_ADDR)) < 0 ) {
+ perror("toconnect");
+ return 1;
+ }
+
+ if (strcmp(argv[1], "count") == 0)
+ {
+ op = REGMAILDB_REQ_COUNT;
+ strlcpy(req.userid, argv[2], sizeof(req.userid));
+ strlcpy(req.email, argv[3], sizeof(req.email));
+ }
+ else if (strcmp(argv[1], "set") == 0)
+ {
+ op = REGMAILDB_REQ_SET;
+ strlcpy(req.userid, argv[2], sizeof(req.userid));
+ strlcpy(req.email, argv[3], sizeof(req.email));
+ }
+ else
+ return 0;
+
+ req.cb = sizeof(req);
+ req.operation = op;
+
+ if (towrite(fd, &req, sizeof(req)) != sizeof(req)) {
+ perror("towrite");
+ return 1;
+ }
+
+ if (toread(fd, &ret, sizeof(ret)) != sizeof(ret)) {
+ perror("toread");
+ return 1;
+ }
+
+ printf("count: %d\n", ret);
+ return 0;
+}
+
+
diff --git a/daemon/regmaild/regmaild.c b/daemon/regmaild/regmaild.c
new file mode 100644
index 00000000..4d887868
--- /dev/null
+++ b/daemon/regmaild/regmaild.c
@@ -0,0 +1,377 @@
+// Register Mail Daemon
+// $Id$
+//
+// sqlite portion from wens
+// Create: Hung-Te Lin <piaip@csie.ntu.edu.tw>
+// Initial Date: 2009/06/19
+//
+// Copyright (C) 2009, Hung-Te Lin <piaip@csie.ntu.edu.tw>
+// All rights reserved
+
+// TODO:
+
+#include <stdio.h>
+#include <unistd.h>
+#include <signal.h>
+#include <ctype.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#include <sys/resource.h>
+#include <sys/wait.h>
+
+#include <event.h>
+#include <sqlite3.h>
+
+#include "bbs.h"
+#include "daemons.h"
+
+// local definiions
+#ifndef EMAILDB_PATH
+#define EMAILDB_PATH BBSHOME "/emaildb.db"
+#endif
+
+sqlite3 *g_Db = NULL;
+
+static int
+regmaildb_open(sqlite3 **Db, const char *fpath) {
+ int rc;
+
+ assert(fpath);
+ if ((rc = sqlite3_open(fpath, Db)) != SQLITE_OK)
+ return rc;
+
+ // create table if it doesn't exist
+ rc = sqlite3_exec(*Db, "CREATE TABLE IF NOT EXISTS emaildb (userid TEXT, email TEXT, PRIMARY KEY (userid));"
+ "CREATE INDEX IF NOT EXISTS email ON emaildb (email);",
+ NULL, NULL, NULL);
+
+ return rc;
+}
+
+int
+regmaildb_check_email(char * email, int email_len, char *myid)
+{
+ int count = -1;
+ sqlite3 *Db = NULL;
+ sqlite3_stmt *Stmt = NULL;
+
+ if (g_Db)
+ Db =g_Db;
+ else if (regmaildb_open(&Db, EMAILDB_PATH) != SQLITE_OK)
+ goto end;
+
+ if (sqlite3_prepare(Db, "SELECT userid FROM emaildb WHERE email LIKE lower(?);",
+ -1, &Stmt, NULL) != SQLITE_OK)
+ goto end;
+
+ if (sqlite3_bind_text(Stmt, 1, email, email_len, SQLITE_STATIC) != SQLITE_OK)
+ goto end;
+
+ count = 0;
+ while (sqlite3_step(Stmt) == SQLITE_ROW) {
+ char *result;
+ userec_t u;
+
+ if ((result = (char*)sqlite3_column_text(Stmt, 0)) == NULL)
+ break;
+
+ // ignore my self, because I may be the one going to
+ // use mail.
+ if (strcasecmp(result, myid) == 0)
+ continue;
+
+ // force update
+ u.email[0] = 0;
+
+ if (passwd_load_user(result, &u))
+ if (strcasecmp(email, u.email) == 0)
+ count++;
+ }
+
+end:
+ if (Stmt != NULL)
+ if (sqlite3_finalize(Stmt) != SQLITE_OK)
+ count = -1;
+
+ if (Db != NULL && !g_Db)
+ sqlite3_close(Db);
+
+ return count;
+}
+
+int
+regmaildb_update_email(char * userid, int userid_len, char * email, int email_len)
+{
+ int ret = -1;
+
+ sqlite3 *Db = NULL;
+ sqlite3_stmt *Stmt = NULL;
+
+ if (g_Db)
+ Db =g_Db;
+ else if (regmaildb_open(&Db, EMAILDB_PATH) != SQLITE_OK)
+ goto end;
+ if (regmaildb_open(&Db, EMAILDB_PATH) != SQLITE_OK)
+ goto end;
+
+ if (strcmp(email, "x") == 0)
+ {
+ if (sqlite3_prepare(Db, "DELETE FROM emaildb WHERE userid like lower(?);",
+ -1, &Stmt, NULL) != SQLITE_OK)
+ goto end;
+
+ if (sqlite3_bind_text(Stmt, 1, userid, userid_len, SQLITE_STATIC) != SQLITE_OK)
+ goto end;
+
+ } else {
+
+ if (sqlite3_prepare(Db, "REPLACE INTO emaildb (userid, email) VALUES (lower(?),lower(?));",
+ -1, &Stmt, NULL) != SQLITE_OK)
+ goto end;
+
+ if (sqlite3_bind_text(Stmt, 1, userid, userid_len, SQLITE_STATIC) != SQLITE_OK)
+ goto end;
+
+ if (sqlite3_bind_text(Stmt, 2, email, email_len, SQLITE_STATIC) != SQLITE_OK)
+ goto end;
+ }
+
+ if (sqlite3_step(Stmt) == SQLITE_DONE)
+ ret = 0;
+
+end:
+ if (Stmt != NULL)
+ sqlite3_finalize(Stmt);
+ if (Db != NULL && !g_Db)
+ sqlite3_close(Db);
+
+ return ret;
+}
+
+///////////////////////////////////////////////////////////////////////
+// Callbacks
+
+// Command syntax:
+// operation\n
+// userid\n
+// email\n
+//
+// operation: set,count
+
+static void
+client_cb(int fd, short event, void *arg)
+{
+ struct event *ev = (struct event*) arg;
+ regmaildb_req req = {0};
+ int ret = -1;
+
+ assert(ev);
+ if ( (event & EV_TIMEOUT) ||
+ !(event & EV_READ) ||
+ toread(fd, &req, sizeof(req)) != sizeof(req) ||
+ req.cb != sizeof(req))
+ {
+ fprintf(stderr, "error: corrupted request.\r\n");
+ close(fd);
+ free(ev);
+ return;
+ }
+
+ switch(req.operation)
+ {
+ case REGMAILDB_REQ_COUNT:
+ ret = regmaildb_check_email(req.email, strlen(req.email), req.userid);
+ fprintf(stderr, "%s check mail[%s]: ret=%d\r\n", req.userid, req.email, ret);
+ if (towrite(fd, &ret, sizeof(ret)) != sizeof(ret))
+ {
+ fprintf(stderr, " error: cannot write response...\r\n");
+ }
+ break;
+
+ case REGMAILDB_REQ_SET:
+ ret = regmaildb_update_email(req.userid, strlen(req.userid),
+ req.email, strlen(req.email));
+ if (towrite(fd, &ret, sizeof(ret)) != sizeof(ret))
+ {
+ fprintf(stderr, " error: cannot write response...\r\n");
+ }
+ break;
+
+ default:
+ fprintf(stderr, "error: invalid operation: %d.\r\n", req.operation);
+ close(fd);
+ free(ev);
+ return;
+ }
+
+ // close connection anyway
+ close(fd);
+ free(ev);
+}
+
+///////////////////////////////////////////////////////////////////////
+// Main
+
+#ifdef INIT_MAIN
+
+// standalone initialize builder
+
+#define TRANSCATION_PERIOD (4096)
+int main(int argc, char *argv[])
+{
+ int fd = 0;
+ userec_t xuser;
+ off_t sz = 0, i = 0, valids = 0;
+ sqlite3 *Db = NULL;
+ sqlite3_stmt *Stmt = NULL, *tranStart = NULL, *tranEnd = NULL;
+ const char *fpath = EMAILDB_PATH;
+
+ // init passwd
+ sz = dashs(FN_PASSWD);
+ fd = open(FN_PASSWD, O_RDONLY);
+ if (fd < 0 || sz <= 0)
+ {
+ fprintf(stderr, "cannot open ~/.PASSWDS.\n");
+ return 0;
+ }
+ sz /= sizeof(userec_t);
+
+ if (argc > 1)
+ fpath = argv[1];
+
+ // init emaildb
+ if (regmaildb_open(&Db, fpath) != SQLITE_OK)
+ {
+ fprintf(stderr, "cannot initialize emaildb: %s.\n", fpath);
+ return 0;
+ }
+
+ if (sqlite3_prepare(Db, "REPLACE INTO emaildb (userid, email) VALUES (lower(?),lower(?));",
+ -1, &Stmt, NULL) != SQLITE_OK ||
+ sqlite3_prepare(Db, "BEGIN TRANSACTION;", -1, &tranStart, NULL) != SQLITE_OK ||
+ sqlite3_prepare(Db, "COMMIT;", -1, &tranEnd, NULL) != SQLITE_OK)
+ {
+ fprintf(stderr, "SQLite 3 internal error.\n");
+ return 0;
+ }
+
+ sqlite3_step(tranStart);
+ sqlite3_reset(tranStart);
+ while (read(fd, &xuser, sizeof(xuser)) == sizeof(xuser))
+ {
+ i++;
+ // got a record
+ if (strlen(xuser.userid) < 2 || strlen(xuser.userid) > IDLEN)
+ continue;
+ if (strlen(xuser.email) < 5)
+ continue;
+
+ if (sqlite3_bind_text(Stmt, 1, xuser.userid, strlen(xuser.userid),
+ SQLITE_STATIC) != SQLITE_OK)
+ {
+ fprintf(stderr, "\ncannot prepare userid param.\n");
+ break;
+ }
+ if (sqlite3_bind_text(Stmt, 2, xuser.email, strlen(xuser.email),
+ SQLITE_STATIC) != SQLITE_OK)
+ {
+ fprintf(stderr, "\ncannot prepare email param.\n");
+ break;
+ }
+
+ if (sqlite3_step(Stmt) != SQLITE_DONE)
+ {
+ fprintf(stderr, "\ncannot execute statement.\n");
+ break;
+ }
+ sqlite3_reset(Stmt);
+
+ valids ++;
+ if (valids % 10 == 0)
+ fprintf(stderr, "%d/%d (valid: %d)\r",
+ (int)i, (int)sz, (int)valids);
+ if (valids % TRANSCATION_PERIOD == 0)
+ {
+ sqlite3_step(tranEnd);
+ sqlite3_step(tranStart);
+ sqlite3_reset(tranEnd);
+ sqlite3_reset(tranStart);
+ }
+ }
+
+ if (valids % TRANSCATION_PERIOD)
+ sqlite3_step(tranEnd);
+
+ if (Stmt != NULL)
+ sqlite3_finalize(Stmt);
+
+ if (Db != NULL)
+ sqlite3_close(Db);
+
+ close(fd);
+ return 0;
+}
+#endif
+
+///////////////////////////////////////////////////////////////////////
+// Event callbacks
+
+///////////////////////////////////////////////////////////////////////
+// Main
+
+struct timeval tv = {60, 0};
+static struct event ev_listen;
+
+static void listen_cb(int fd, short event, void *arg)
+{
+ int cfd;
+
+ if ((cfd = accept(fd, NULL, NULL)) < 0 )
+ return;
+
+ struct event *ev = malloc(sizeof(struct event));
+
+ event_set(ev, cfd, EV_READ, client_cb, ev);
+ event_add(ev, &tv);
+}
+
+int main(int argc, char *argv[])
+{
+ int ch, sfd;
+ char *iface_ip = REGMAILD_ADDR;
+
+ Signal(SIGPIPE, SIG_IGN);
+
+ chdir(BBSHOME);
+ attach_SHM();
+
+ /* Give up root privileges: no way back from here */
+ setgid(BBSGID);
+ setuid(BBSUID);
+
+ while ( (ch = getopt(argc, argv, "i:h")) != -1 )
+ switch( ch ){
+ case 'i':
+ iface_ip = optarg;
+ break;
+ case 'h':
+ default:
+ fprintf(stderr, "usage: %s [-i [interface_ip]:port]\n", argv[0]);
+ return 1;
+ }
+
+ if ( (sfd = tobind(iface_ip)) < 0 )
+ return 1;
+
+ // daemonize(BBSHOME "/run/regmaild.pid", NULL);
+ regmaildb_open(&g_Db, EMAILDB_PATH);
+
+ event_init();
+ event_set(&ev_listen, sfd, EV_READ | EV_PERSIST, listen_cb, &ev_listen);
+ event_add(&ev_listen, NULL);
+ fprintf(stderr, "start dispatch.\r\n");
+ event_dispatch();
+
+ return 0;
+}
+// vim:et
diff --git a/include/daemons.h b/include/daemons.h
new file mode 100644
index 00000000..7d6d6217
--- /dev/null
+++ b/include/daemons.h
@@ -0,0 +1,56 @@
+#ifndef _BBS_DAEMONS_H
+
+#include "pttstruct.h"
+
+///////////////////////////////////////////////////////////////////////
+// From Host Nick Daemon (fromd)
+
+#ifndef FROMD_ADDR
+#define FROMD_ADDR ":5130"
+#endif
+
+///////////////////////////////////////////////////////////////////////
+// Register E-Mail Database Daemon (regmaild)
+
+#ifndef REGMAILD_ADDR
+#define REGMAILD_ADDR ":5131"
+#endif
+
+enum {
+ REGMAILDB_REQ_COUNT = 1,
+ REGMAILDB_REQ_SET,
+};
+
+typedef struct
+{
+ size_t cb;
+ int operation;
+ char userid [IDLEN+1];
+ char email [50]; // TODO define const in pttstruct.h
+} regmaildb_req;
+
+
+///////////////////////////////////////////////////////////////////////
+// Login Daemon (logind)
+
+typedef struct login_data
+{
+ // size of current structure
+ size_t cb;
+ void *ack;
+
+ // terminal information
+ int t_lines, t_cols;
+ int encoding;
+ Fnv32_t client_code;
+
+ // user authentication
+ char userid[IDLEN+1];
+ char hostip[IPV4LEN+1];
+ char port [IDLEN+1];
+
+} login_data;
+
+#endif // _BBS_DAEMONS_H
+
+// vim:et
diff --git a/include/logind.h b/include/logind.h
deleted file mode 100644
index 37d2c658..00000000
--- a/include/logind.h
+++ /dev/null
@@ -1,28 +0,0 @@
-///////////////////////////////////////////////////////////////////////
-// Login Daemon Data
-
-#ifndef _BBS_LOGIND_H
-
-#include "bbs.h"
-
-typedef struct login_data
-{
- // size of current structure
- size_t cb;
- void *ack;
-
- // terminal information
- int t_lines, t_cols;
- int encoding;
- Fnv32_t client_code;
-
- // user authentication
- char userid[IDLEN+1];
- char hostip[IPV4LEN+1];
- char port [IDLEN+1];
-
-} login_data;
-
-#endif // _BBS_LOGIND_H
-
-// vim:et
diff --git a/mbbsd/Makefile b/mbbsd/Makefile
index 1572bc0a..06fda265 100644
--- a/mbbsd/Makefile
+++ b/mbbsd/Makefile
@@ -9,7 +9,7 @@ SRCROOT= ..
PROG= mbbsd
COREOBJS = bbs.o announce.o read.o board.o cache.o cal.o brc.o mail.o record.o fav.o
-ACCOBJS = user.o register.o passwd.o
+ACCOBJS = user.o register.o passwd.o emaildb.o
NETOBJS = mbbsd.o io.o term.o telnet.o
TALKOBJS = talk.o chat.o friend.o
UTILOBJS = stuff.o kaede.o convert.o name.o syspost.o
@@ -37,9 +37,7 @@ CFLAGS+= -DLOG_CRAWLER
.endif
.if !defined(WITHOUT_EMAILDB) && defined(WITH_EMAILDB)
-OBJS+= emaildb.o
CFLAGS+= -DUSE_EMAILDB
-LDLIBS+= -lsqlite3
.endif
.if !defined(WITHOUT_BBSLUA_USAGE) && defined(WITH_BBSLUA_USAGE)
@@ -112,9 +110,6 @@ $(SRCROOT)/include/banip.h: $(SRCROOT)/util/banip.pl
mbbsd.o: mbbsd.c $(SRCROOT)/include/var.h $(SRCROOT)/include/banip.h
$(DIETCC) $(CC) $(CFLAGS) -c $<
-initemaildb: emaildb.c
- $(CC) -DINIT_MAIN $(CFLAGS) $(LDFLAGS) -o initemaildb emaildb.c $(LDLIBS)
-
ctags:
ctags *.c $(SRCROOT)/include/*.h $(SRCROOT)/common/sys/*.c $(SRCROOT)/common/bbs/*.c
diff --git a/mbbsd/emaildb.c b/mbbsd/emaildb.c
index c9313baf..899b2fda 100644
--- a/mbbsd/emaildb.c
+++ b/mbbsd/emaildb.c
@@ -1,259 +1,74 @@
/* $Id$ */
-#include <sqlite3.h>
-#include <sys/wait.h>
-#include "bbs.h"
-
-#define EMAILDB_PATH BBSHOME "/emaildb.db"
-
-// define FORK model to minimize memory usage.
-#define FORK_MODEL
-
-static int emaildb_open(sqlite3 **Db, const char *fpath) {
- int rc;
- assert(fpath);
- if ((rc = sqlite3_open(fpath, Db)) != SQLITE_OK)
- return rc;
-
- // create table if it doesn't exist
- rc = sqlite3_exec(*Db, "CREATE TABLE IF NOT EXISTS emaildb (userid TEXT, email TEXT, PRIMARY KEY (userid));"
- "CREATE INDEX IF NOT EXISTS email ON emaildb (email);",
- NULL, NULL, NULL);
+#include "bbs.h"
+#include "daemons.h"
- return rc;
-}
+#ifdef USE_EMAILDB
-#ifndef INIT_MAIN
int emaildb_check_email(char * email, int email_len)
{
int count = -1;
- pid_t pid = -1;
- sqlite3 *Db = NULL;
- sqlite3_stmt *Stmt = NULL;
+ int fd = -1;
+ regmaildb_req req = {0};
-#ifdef FORK_MODEL
- switch((pid = fork()))
- {
- case -1: // error
- break;
-
- case 0: // child
- break;
+ // initialize request
+ req.cb = sizeof(req);
+ req.operation = REGMAILDB_REQ_COUNT;
+ strlcpy(req.userid, cuser.userid, sizeof(req.userid));
- default:
- waitpid(pid, &count, 0);
- // 0xFF: the limitation of WEXISTSTATUS
- if (WIFEXITED(count) && WEXITSTATUS(count) < 0xFF)
- count = WEXITSTATUS(count);
- // vmsgf(ANSI_RESET "found %d emails", count);
- return count;
+ if ( (fd = toconnect(REGMAILD_ADDR)) < 0 )
+ {
+ // perror("toconnect");
+ return -1;
}
-#endif
-
- if (emaildb_open(&Db, EMAILDB_PATH) != SQLITE_OK)
- goto end;
-
- if (sqlite3_prepare(Db, "SELECT userid FROM emaildb WHERE email LIKE lower(?);",
- -1, &Stmt, NULL) != SQLITE_OK)
- goto end;
-
- if (sqlite3_bind_text(Stmt, 1, email, email_len, SQLITE_STATIC) != SQLITE_OK)
- goto end;
- count = 0;
- while (sqlite3_step(Stmt) == SQLITE_ROW) {
- char *result;
- userec_t u;
-
- if ((result = (char*)sqlite3_column_text(Stmt, 0)) == NULL)
- break;
-
- // ignore my self, because I may be the one going to
- // use mail.
- if (strcasecmp(result, cuser.userid) == 0)
- continue;
-
- // force update
- u.email[0] = 0;
-
- if (getuser(result, &u))
- if (strcasecmp(email, u.email) == 0)
- count++;
+ if (towrite(fd, &req, sizeof(req)) != sizeof(req)) {
+ // perror("towrite");
+ close(fd);
+ return -1;
}
-end:
- if (Stmt != NULL)
- if (sqlite3_finalize(Stmt) != SQLITE_OK)
- count = -1;
-
- if (Db != NULL)
- sqlite3_close(Db);
-
- // XXX exit() can only hold 0~255 for WEXISTSTATUS
- assert(127 >= EMAILDB_LIMIT);
- if (count > 127)
- count = 127;
-
- if (pid == 0)
- exit(count);
+ if (toread(fd, &count, sizeof(count)) != sizeof(count)) {
+ // perror("toread");
+ close(fd);
+ return -1;
+ }
return count;
}
-#endif
int emaildb_update_email(char * userid, int userid_len, char * email, int email_len)
{
- int ret = -1;
- pid_t pid = -1;
-
- sqlite3 *Db = NULL;
- sqlite3_stmt *Stmt = NULL;
-
-#ifdef FORK_MODEL
- switch((pid = fork()))
- {
- case -1: // error
- break;
-
- case 0: // child
- break;
-
- default:
- waitpid(pid, &ret, 0);
- // 0xFF: the limitation of WEXISTSTATUS
- if (WIFEXITED(ret) && WEXITSTATUS(ret) < 0xFF)
- ret = WEXITSTATUS(ret);
- return ret;
- }
-#endif
-
- if (emaildb_open(&Db, EMAILDB_PATH) != SQLITE_OK)
- goto end;
-
- if (sqlite3_prepare(Db, "REPLACE INTO emaildb (userid, email) VALUES (lower(?),lower(?));",
- -1, &Stmt, NULL) != SQLITE_OK)
- goto end;
-
- if (sqlite3_bind_text(Stmt, 1, userid, userid_len, SQLITE_STATIC) != SQLITE_OK)
- goto end;
-
- if (sqlite3_bind_text(Stmt, 2, email, email_len, SQLITE_STATIC) != SQLITE_OK)
- goto end;
-
- if (sqlite3_step(Stmt) == SQLITE_DONE)
- ret = 0;
-
-end:
- if (Stmt != NULL)
- sqlite3_finalize(Stmt);
- if (Db != NULL)
- sqlite3_close(Db);
-
- if (pid == 0)
- exit(ret);
-
- return ret;
-}
-
-#ifdef INIT_MAIN
-
-// standalone initialize builder
-
-#define TRANSCATION_PERIOD (4096)
-int main(int argc, char *argv[])
-{
- int fd = 0;
- userec_t xuser;
- off_t sz = 0, i = 0, valids = 0;
- sqlite3 *Db = NULL;
- sqlite3_stmt *Stmt = NULL, *tranStart = NULL, *tranEnd = NULL;
- const char *fpath = EMAILDB_PATH;
-
- // init passwd
- sz = dashs(FN_PASSWD);
- fd = open(FN_PASSWD, O_RDONLY);
- if (fd < 0 || sz <= 0)
- {
- fprintf(stderr, "cannot open ~/.PASSWDS.\n");
- return 0;
- }
- sz /= sizeof(userec_t);
+ int result = -1;
+ int fd = -1;
+ regmaildb_req req = {0};
- if (argc > 1)
- fpath = argv[1];
+ // initialize request
+ req.cb = sizeof(req);
+ req.operation = REGMAILDB_REQ_SET;
+ strlcpy(req.userid, cuser.userid, sizeof(req.userid));
- // init emaildb
- if (emaildb_open(&Db, fpath) != SQLITE_OK)
+ if ( (fd = toconnect(REGMAILD_ADDR)) < 0 )
{
- fprintf(stderr, "cannot initialize emaildb: %s.\n", fpath);
- return 0;
+ // perror("toconnect");
+ return -1;
}
- if (sqlite3_prepare(Db, "REPLACE INTO emaildb (userid, email) VALUES (lower(?),lower(?));",
- -1, &Stmt, NULL) != SQLITE_OK ||
- sqlite3_prepare(Db, "BEGIN TRANSACTION;", -1, &tranStart, NULL) != SQLITE_OK ||
- sqlite3_prepare(Db, "COMMIT;", -1, &tranEnd, NULL) != SQLITE_OK)
- {
- fprintf(stderr, "SQLite 3 internal error.\n");
- return 0;
+ if (towrite(fd, &req, sizeof(req)) != sizeof(req)) {
+ // perror("towrite");
+ close(fd);
+ return -1;
}
- sqlite3_step(tranStart);
- sqlite3_reset(tranStart);
- while (read(fd, &xuser, sizeof(xuser)) == sizeof(xuser))
- {
- i++;
- // got a record
- if (strlen(xuser.userid) < 2 || strlen(xuser.userid) > IDLEN)
- continue;
- if (strlen(xuser.email) < 5)
- continue;
-
- if (sqlite3_bind_text(Stmt, 1, xuser.userid, strlen(xuser.userid),
- SQLITE_STATIC) != SQLITE_OK)
- {
- fprintf(stderr, "\ncannot prepare userid param.\n");
- break;
- }
- if (sqlite3_bind_text(Stmt, 2, xuser.email, strlen(xuser.email),
- SQLITE_STATIC) != SQLITE_OK)
- {
- fprintf(stderr, "\ncannot prepare email param.\n");
- break;
- }
-
- if (sqlite3_step(Stmt) != SQLITE_DONE)
- {
- fprintf(stderr, "\ncannot execute statement.\n");
- break;
- }
- sqlite3_reset(Stmt);
-
- valids ++;
- if (valids % 10 == 0)
- fprintf(stderr, "%d/%d (valid: %d)\r",
- (int)i, (int)sz, (int)valids);
- if (valids % TRANSCATION_PERIOD == 0)
- {
- sqlite3_step(tranEnd);
- sqlite3_step(tranStart);
- sqlite3_reset(tranEnd);
- sqlite3_reset(tranStart);
- }
+ if (toread(fd, &result, sizeof(result)) != sizeof(result)) {
+ // perror("toread");
+ close(fd);
+ return -1;
}
- if (valids % TRANSCATION_PERIOD)
- sqlite3_step(tranEnd);
-
- if (Stmt != NULL)
- sqlite3_finalize(Stmt);
-
- if (Db != NULL)
- sqlite3_close(Db);
-
- close(fd);
- return 0;
+ return result;
}
+
#endif
-// vim: sw=4
+// vim:et
diff --git a/mbbsd/mbbsd.c b/mbbsd/mbbsd.c
index 80af1426..93643dbc 100644
--- a/mbbsd/mbbsd.c
+++ b/mbbsd/mbbsd.c
@@ -1,7 +1,7 @@
/* $Id$ */
#include "bbs.h"
#include "banip.h"
-#include "logind.h"
+#include "daemons.h"
#include <sys/wait.h>
#include <netinet/tcp.h>