-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathcontrol_db.c
164 lines (123 loc) · 3.7 KB
/
control_db.c
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
/*
* NOperserv v0.01
*
* A replacement for Germania's ageing Operservice2
* DB functions
*
* Copyright (C) 2005 Chris Porter.
*/
#include "../nick/nick.h"
#include "../core/error.h"
#include "../lib/irc_string.h"
#include "../core/schedule.h"
#include "../dbapi2/dbapi2.h"
#include "control.h"
#include <stdlib.h>
int db_loaded = 0;
unsigned long loadedusers = 0;
void noperserv_create_tables(void);
void noperserv_free_user(no_autheduser *au);
void noperserv_load_users(const DBAPIResult *res, void *arg);
static DBAPIConn *nodb;
int noperserv_load_db(void) {
if(!nodb) {
nodb = dbapi2open(DBAPI2_DEFAULT, "noperserv");
if(!nodb) {
Error("noperserv", ERR_STOP, "Could not connect to database.");
return 0;
}
}
db_loaded = 1;
noperserv_create_tables();
nodb->query(nodb, noperserv_load_users, NULL,
"SELECT userid, authname, flags, noticelevel FROM ?", "T", "users");
return 1;
}
void noperserv_load_users(const DBAPIResult *res, void *arg) {
no_autheduser *nu;
if(!res)
Error("control", ERR_STOP, "Failed to load noperserv database. Your database might be corrupted or the schema is incompatible.");
if(!res->success) {
Error("noperserv", ERR_ERROR, "Error loading user list.");
res->clear(res);
return;
}
while(res->next(res)) {
nu = noperserv_new_autheduser(strtoul(res->get(res, 0), NULL, 10), res->get(res, 1));
if(!nu)
continue;
nu->authlevel = strtoul(res->get(res, 2), NULL, 10);
nu->noticelevel = strtoul(res->get(res, 3), NULL, 10);
nu->newuser = 0;
}
Error("noperserv", ERR_INFO, "Loaded %lu users", loadedusers);
res->clear(res);
}
void noperserv_create_tables(void) {
nodb->createtable(nodb, NULL, NULL,
"CREATE TABLE ? ("
"userid INT NOT NULL,"
"authname VARCHAR(?) NOT NULL,"
"flags INT NOT NULL,"
"noticelevel INT NOT NULL,"
"PRIMARY KEY (userid))", "Td", "users", ACCOUNTLEN);
}
void noperserv_cleanup_db(void) {
int i;
authname *anp, *next;
no_autheduser *au;
for (i=0;i<AUTHNAMEHASHSIZE;i++) {
for (anp=authnametable[i];anp;) {
next = anp->next;
au = anp->exts[noperserv_ext];
if(au)
noperserv_free_user(au);
anp = next;
}
}
nodb->close(nodb);
nodb = NULL;
}
no_autheduser *noperserv_new_autheduser(unsigned long userid, char *name) {
authname *anp;
no_autheduser *au;
anp = findorcreateauthname(userid, name);
if(!anp)
return NULL;
au = malloc(sizeof(no_autheduser));
if(!au)
return NULL;
au->authname = anp;
loadedusers++;
au->newuser = 1;
anp->exts[noperserv_ext] = au;
return au;
}
void noperserv_delete_autheduser(no_autheduser *au) {
if(!au->newuser)
nodb->squery(nodb, "DELETE FROM ? WHERE userid = ?", "Tu", "users", au->authname->userid);
noperserv_free_user(au);
}
void noperserv_update_autheduser(no_autheduser *au) {
if(au->newuser) {
nodb->squery(nodb, "INSERT INTO ? (userid, authname, flags, noticelevel) VALUES (?,?,?,?)", "Tusuu", "users", au->authname->userid, au->authname->name, NOGetAuthLevel(au), NOGetNoticeLevel(au));
au->newuser = 0;
} else {
nodb->squery(nodb, "UPDATE ? SET flags = ?, noticelevel = ? WHERE userid = ?", "Tuuu", "users", NOGetAuthLevel(au), NOGetNoticeLevel(au), au->authname->userid);
}
}
void noperserv_free_user(no_autheduser *au) {
authname *anp = au->authname;
anp->exts[noperserv_ext] = NULL;
releaseauthname(anp);
free(au);
loadedusers--;
}
no_autheduser *noperserv_get_autheduser(authname *anp) {
if (!anp)
return NULL;
return anp->exts[noperserv_ext];
}
unsigned long noperserv_get_autheduser_count(void) {
return loadedusers;
}