summaryrefslogtreecommitdiffstats
path: root/mbbsd/emaildb.c
blob: 45b634cee15322ef4319c6292770073468bfc732 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/* $Id$ */
#include <sqlite3.h>
#include "bbs.h"

#define EMAILDB_PATH BBSHOME "/emaildb.db"

#if defined(__GLIBC__)
void __libc_freeres();
#endif 

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;
}

#ifndef INIT_MAIN
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 = (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++;
    }

end:
    if (Stmt != NULL)
    if (sqlite3_finalize(Stmt) != SQLITE_OK)
        count = -1;

    if (Db != NULL)
    sqlite3_close(Db);

    return count;
}
#endif

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);

#if defined(__GLIBC__)
    __libc_freeres();   // discovered by wens, to reduce internal cache caused by sqlite.
#endif 

    return ret;
}

#ifdef INIT_MAIN

// standalone initialize builder

#define TRANSCATION_PERIOD (4096)
int main()
{
    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;

    // 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);

    // init emaildb
    if (emaildb_open(&Db) != SQLITE_OK)
    {
    fprintf(stderr, "cannot initialize emaildb.\n");
    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);

#if defined(__GLIBC__)
    __libc_freeres();   // discovered by wens, to reduce internal cache caused by sqlite.
#endif 

    close(fd);
    return 0;
}
#endif

// vim: sw=4