]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * NOperserv v0.01 | |
3 | * | |
4 | * A replacement for Germania's ageing Operservice2 | |
5 | * DB functions | |
6 | * | |
7 | * Copyright (C) 2005 Chris Porter. | |
8 | */ | |
9 | ||
10 | #include "../nick/nick.h" | |
11 | #include "../core/error.h" | |
12 | #include "../lib/irc_string.h" | |
13 | #include "../core/schedule.h" | |
14 | #include "../dbapi2/dbapi2.h" | |
15 | ||
16 | #include "control.h" | |
17 | ||
18 | #include <stdlib.h> | |
19 | ||
20 | int db_loaded = 0; | |
21 | unsigned long loadedusers = 0; | |
22 | ||
23 | void noperserv_create_tables(void); | |
24 | ||
25 | void noperserv_free_user(no_autheduser *au); | |
26 | void noperserv_load_users(const DBAPIResult *res, void *arg); | |
27 | ||
28 | static DBAPIConn *nodb; | |
29 | ||
30 | int noperserv_load_db(void) { | |
31 | if(!nodb) { | |
32 | nodb = dbapi2open(DBAPI2_DEFAULT, "noperserv"); | |
33 | if(!nodb) { | |
34 | Error("noperserv", ERR_STOP, "Could not connect to database."); | |
35 | return 0; | |
36 | } | |
37 | } | |
38 | ||
39 | db_loaded = 1; | |
40 | ||
41 | noperserv_create_tables(); | |
42 | ||
43 | nodb->query(nodb, noperserv_load_users, NULL, | |
44 | "SELECT userid, authname, flags, noticelevel FROM ?", "T", "users"); | |
45 | ||
46 | return 1; | |
47 | } | |
48 | ||
49 | void noperserv_load_users(const DBAPIResult *res, void *arg) { | |
50 | no_autheduser *nu; | |
51 | ||
52 | if(!res) | |
53 | Error("control", ERR_STOP, "Failed to load noperserv database. Your database might be corrupted or the schema is incompatible."); | |
54 | ||
55 | if(!res->success) { | |
56 | Error("noperserv", ERR_ERROR, "Error loading user list."); | |
57 | res->clear(res); | |
58 | return; | |
59 | } | |
60 | ||
61 | while(res->next(res)) { | |
62 | nu = noperserv_new_autheduser(strtoul(res->get(res, 0), NULL, 10), res->get(res, 1)); | |
63 | if(!nu) | |
64 | continue; | |
65 | ||
66 | nu->authlevel = strtoul(res->get(res, 2), NULL, 10); | |
67 | nu->noticelevel = strtoul(res->get(res, 3), NULL, 10); | |
68 | nu->newuser = 0; | |
69 | } | |
70 | ||
71 | Error("noperserv", ERR_INFO, "Loaded %lu users", loadedusers); | |
72 | ||
73 | res->clear(res); | |
74 | } | |
75 | ||
76 | void noperserv_create_tables(void) { | |
77 | nodb->createtable(nodb, NULL, NULL, | |
78 | "CREATE TABLE ? (" | |
79 | "userid INT NOT NULL," | |
80 | "authname VARCHAR(?) NOT NULL," | |
81 | "flags INT NOT NULL," | |
82 | "noticelevel INT NOT NULL," | |
83 | "PRIMARY KEY (userid))", "Td", "users", ACCOUNTLEN); | |
84 | } | |
85 | ||
86 | void noperserv_cleanup_db(void) { | |
87 | int i; | |
88 | authname *anp, *next; | |
89 | no_autheduser *au; | |
90 | ||
91 | for (i=0;i<AUTHNAMEHASHSIZE;i++) { | |
92 | for (anp=authnametable[i];anp;) { | |
93 | next = anp->next; | |
94 | ||
95 | au = anp->exts[noperserv_ext]; | |
96 | if(au) | |
97 | noperserv_free_user(au); | |
98 | ||
99 | anp = next; | |
100 | } | |
101 | } | |
102 | ||
103 | nodb->close(nodb); | |
104 | nodb = NULL; | |
105 | } | |
106 | ||
107 | no_autheduser *noperserv_new_autheduser(unsigned long userid, char *name) { | |
108 | authname *anp; | |
109 | no_autheduser *au; | |
110 | ||
111 | anp = findorcreateauthname(userid, name); | |
112 | if(!anp) | |
113 | return NULL; | |
114 | ||
115 | au = malloc(sizeof(no_autheduser)); | |
116 | if(!au) | |
117 | return NULL; | |
118 | ||
119 | au->authname = anp; | |
120 | ||
121 | loadedusers++; | |
122 | au->newuser = 1; | |
123 | ||
124 | anp->exts[noperserv_ext] = au; | |
125 | ||
126 | return au; | |
127 | } | |
128 | ||
129 | void noperserv_delete_autheduser(no_autheduser *au) { | |
130 | if(!au->newuser) | |
131 | nodb->squery(nodb, "DELETE FROM ? WHERE userid = ?", "Tu", "users", au->authname->userid); | |
132 | ||
133 | noperserv_free_user(au); | |
134 | } | |
135 | ||
136 | void noperserv_update_autheduser(no_autheduser *au) { | |
137 | if(au->newuser) { | |
138 | nodb->squery(nodb, "INSERT INTO ? (userid, authname, flags, noticelevel) VALUES (?,?,?,?)", "Tusuu", "users", au->authname->userid, au->authname->name, NOGetAuthLevel(au), NOGetNoticeLevel(au)); | |
139 | au->newuser = 0; | |
140 | } else { | |
141 | nodb->squery(nodb, "UPDATE ? SET flags = ?, noticelevel = ? WHERE userid = ?", "Tuuu", "users", NOGetAuthLevel(au), NOGetNoticeLevel(au), au->authname->userid); | |
142 | } | |
143 | } | |
144 | ||
145 | void noperserv_free_user(no_autheduser *au) { | |
146 | authname *anp = au->authname; | |
147 | anp->exts[noperserv_ext] = NULL; | |
148 | releaseauthname(anp); | |
149 | free(au); | |
150 | ||
151 | loadedusers--; | |
152 | } | |
153 | ||
154 | no_autheduser *noperserv_get_autheduser(authname *anp) { | |
155 | if (!anp) | |
156 | return NULL; | |
157 | ||
158 | return anp->exts[noperserv_ext]; | |
159 | } | |
160 | ||
161 | unsigned long noperserv_get_autheduser_count(void) { | |
162 | return loadedusers; | |
163 | } | |
164 |