summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorwens <wens@63ad8ddf-47c3-0310-b6dd-a9e9d9715204>2008-01-02 20:18:33 +0800
committerwens <wens@63ad8ddf-47c3-0310-b6dd-a9e9d9715204>2008-01-02 20:18:33 +0800
commitf7b95c5ed07e5ca5bf330d9646cba88c89abe128 (patch)
tree1feb6ecac27c1602a5a4e0c71ccda071a9d23eb5
parent8dcf7da5ab295eb2c441eb6cc427aaa6bfd76d35 (diff)
downloadpttbbs-f7b95c5ed07e5ca5bf330d9646cba88c89abe128.tar
pttbbs-f7b95c5ed07e5ca5bf330d9646cba88c89abe128.tar.gz
pttbbs-f7b95c5ed07e5ca5bf330d9646cba88c89abe128.tar.bz2
pttbbs-f7b95c5ed07e5ca5bf330d9646cba88c89abe128.tar.lz
pttbbs-f7b95c5ed07e5ca5bf330d9646cba88c89abe128.tar.xz
pttbbs-f7b95c5ed07e5ca5bf330d9646cba88c89abe128.tar.zst
pttbbs-f7b95c5ed07e5ca5bf330d9646cba88c89abe128.zip
New email database interface for restricting how many registered ids per email. (uses sqlite)
git-svn-id: http://opensvn.csie.org/pttbbs/trunk/pttbbs@3772 63ad8ddf-47c3-0310-b6dd-a9e9d9715204
-rw-r--r--mbbsd/emaildb.c92
1 files changed, 92 insertions, 0 deletions
diff --git a/mbbsd/emaildb.c b/mbbsd/emaildb.c
new file mode 100644
index 00000000..2a31ecb4
--- /dev/null
+++ b/mbbsd/emaildb.c
@@ -0,0 +1,92 @@
+/* $Id$ */
+#include <sqlite3.h>
+#include "bbs.h"
+
+#define EMAILDB_PATH BBSHOME "/emaildb.db"
+
+static int emaildb_open(sqlite3 **Db) {
+ int rc;
+
+ if ((rc = sqlite3_open(EMAILDB_PATH, 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 emaildb_check_email(char * email, int email_len)
+{
+ int count = -1;
+ sqlite3 *Db = NULL;
+ sqlite3_stmt *Stmt = NULL;
+
+ if (emaildb_open(&Db) != 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 = sqlite3_column_text(Stmt, 0)) == NULL)
+ break;
+
+ if (getuser(result, &u))
+ if (strcasecmp(email, u.email) == 0)
+ count++;
+ }
+
+end:
+ if (Stmt != NULL)
+ if (sqlite3_finalize(Stmt) != SQLITE_OK)
+ count = -1;
+
+ if (Db != NULL)
+ sqlite3_close(Db);
+
+ return count;
+}
+
+int emaildb_update_email(char * userid, int userid_len, char * email, int email_len)
+{
+ int ret = -1;
+ sqlite3 *Db = NULL;
+ sqlite3_stmt *Stmt = NULL;
+
+ if (emaildb_open(&Db) != 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);
+
+ return ret;
+}
+
+// vim: sw=4