diff options
author | piaip <piaip@63ad8ddf-47c3-0310-b6dd-a9e9d9715204> | 2009-06-20 01:15:42 +0800 |
---|---|---|
committer | piaip <piaip@63ad8ddf-47c3-0310-b6dd-a9e9d9715204> | 2009-06-20 01:15:42 +0800 |
commit | 43681fc130abb9cce56ad5435fb6159ea7a8527f (patch) | |
tree | 3b13de62edbe16af0438a49a2cdcba91f5cdf0d1 /daemon | |
parent | 5a1828b34592469b5bf7fe9f46c7fe69bbd30cba (diff) | |
download | pttbbs-43681fc130abb9cce56ad5435fb6159ea7a8527f.tar pttbbs-43681fc130abb9cce56ad5435fb6159ea7a8527f.tar.gz pttbbs-43681fc130abb9cce56ad5435fb6159ea7a8527f.tar.bz2 pttbbs-43681fc130abb9cce56ad5435fb6159ea7a8527f.tar.lz pttbbs-43681fc130abb9cce56ad5435fb6159ea7a8527f.tar.xz pttbbs-43681fc130abb9cce56ad5435fb6159ea7a8527f.tar.zst pttbbs-43681fc130abb9cce56ad5435fb6159ea7a8527f.zip |
* recover initemaildb
git-svn-id: http://opensvn.csie.org/pttbbs/trunk/pttbbs@4680 63ad8ddf-47c3-0310-b6dd-a9e9d9715204
Diffstat (limited to 'daemon')
-rw-r--r-- | daemon/regmaild/regmaild.c | 239 |
1 files changed, 127 insertions, 112 deletions
diff --git a/daemon/regmaild/regmaild.c b/daemon/regmaild/regmaild.c index bd980b77..eabc45c8 100644 --- a/daemon/regmaild/regmaild.c +++ b/daemon/regmaild/regmaild.c @@ -12,18 +12,23 @@ #include <stdio.h> #include <unistd.h> +#include <fcntl.h> #include <signal.h> #include <ctype.h> +#include <assert.h> +#include <string.h> #include <sys/time.h> #include <sys/types.h> #include <sys/resource.h> #include <sys/wait.h> -#include <event.h> -#include <sqlite3.h> +#include "common.h" +#include "pttstruct.h" -#include "bbs.h" -#include "daemons.h" +/////////////////////////////////////////////////////////////////////// +// Common Section + +#include <sqlite3.h> // local definiions #ifndef EMAILDB_PATH @@ -48,6 +53,118 @@ regmaildb_open(sqlite3 **Db, const char *fpath) { return rc; } +/////////////////////////////////////////////////////////////////////// +// InitEmaildb Tool 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; +} + +#else // !INIT_MAIN + +/////////////////////////////////////////////////////////////////////// +// As Daemon + +#include <event.h> +#include "bbs.h" +#include "daemons.h" + int regmaildb_check_email(const char * email, int email_len, const char *myid) { @@ -222,114 +339,6 @@ client_cb(int fd, short event, void *arg) } /////////////////////////////////////////////////////////////////////// -// 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; @@ -347,6 +356,9 @@ static void listen_cb(int fd, short event, void *arg) event_add(ev, &tv); } +/////////////////////////////////////////////////////////////////////// +// Daemon Main + int main(int argc, char *argv[]) { int ch, sfd; @@ -386,4 +398,7 @@ int main(int argc, char *argv[]) return 0; } + +#endif // !INIT_MAIN + // vim:et |