]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * chanservdb_updates.c: | |
3 | * Handle all the update requests for the database. | |
4 | */ | |
5 | ||
6 | #include "chanserv.h" | |
7 | #include "../dbapi/dbapi.h" | |
8 | #include "../core/config.h" | |
9 | #include "../lib/sstring.h" | |
10 | #include "../parser/parser.h" | |
11 | #include "../core/events.h" | |
12 | ||
13 | #include <string.h> | |
14 | #include <stdio.h> | |
15 | #include <sys/poll.h> | |
16 | #include <stdarg.h> | |
17 | #include <assert.h> | |
18 | ||
19 | void csdb_updateauthinfo(reguser *rup) { | |
20 | char eschost[2*HOSTLEN+1]; | |
21 | ||
22 | dbescapestring(eschost,rup->lastuserhost->content,rup->lastuserhost->length); | |
23 | dbquery("UPDATE chanserv.users SET lastauth=%lu,lastuserhost='%s' WHERE ID=%u", | |
24 | rup->lastauth,eschost,rup->ID); | |
25 | } | |
26 | ||
27 | void csdb_updatelastjoin(regchanuser *rcup) { | |
28 | dbquery("UPDATE chanserv.chanusers SET usetime=%lu WHERE userID=%u and channelID=%u", | |
29 | rcup->usetime, rcup->user->ID, rcup->chan->ID); | |
30 | } | |
31 | ||
32 | void csdb_updatetopic(regchan *rcp) { | |
33 | char esctopic[TOPICLEN*2+5]; | |
34 | ||
35 | if (rcp->topic) { | |
36 | dbescapestring(esctopic,rcp->topic->content,rcp->topic->length); | |
37 | } else { | |
38 | esctopic[0]='\0'; | |
39 | } | |
40 | dbquery("UPDATE chanserv.channels SET topic='%s' WHERE ID=%u",esctopic,rcp->ID); | |
41 | } | |
42 | ||
43 | void csdb_updatechannel(regchan *rcp) { | |
44 | char escwelcome[WELCOMELEN*2+1]; | |
45 | char esctopic[TOPICLEN*2+1]; | |
46 | char esckey[70]; | |
47 | char escreason[510]; | |
48 | char esccomment[510]; | |
49 | char escname[1000]; | |
50 | ||
51 | dbescapestring(escname, rcp->index->name->content, rcp->index->name->length); | |
52 | ||
53 | if (rcp->welcome) | |
54 | dbescapestring(escwelcome, rcp->welcome->content, | |
55 | rcp->welcome->length); | |
56 | else | |
57 | escwelcome[0]='\0'; | |
58 | ||
59 | if (rcp->topic) | |
60 | dbescapestring(esctopic, rcp->topic->content, rcp->topic->length); | |
61 | else | |
62 | esctopic[0]='\0'; | |
63 | ||
64 | if (rcp->key) | |
65 | dbescapestring(esckey, rcp->key->content, rcp->key->length); | |
66 | else | |
67 | esckey[0]='\0'; | |
68 | ||
69 | if (rcp->suspendreason) | |
70 | dbescapestring(escreason, rcp->suspendreason->content, | |
71 | rcp->suspendreason->length); | |
72 | else | |
73 | escreason[0]='\0'; | |
74 | ||
75 | if (rcp->comment) | |
76 | dbescapestring(esccomment, rcp->comment->content, | |
77 | rcp->comment->length); | |
78 | else | |
79 | esccomment[0]='\0'; | |
80 | ||
81 | dbquery("UPDATE chanserv.channels SET name='%s', flags=%d, forcemodes=%d," | |
82 | "denymodes=%d, chanlimit=%d, autolimit=%d, banstyle=%d," | |
83 | "lastactive=%lu,statsreset=%lu, banduration=%lu, founder=%u," | |
84 | "addedby=%u, suspendby=%u, suspendtime=%lu, chantype=%d, totaljoins=%u," | |
85 | "tripjoins=%u, maxusers=%u, tripusers=%u," | |
86 | "welcome='%s', topic='%s', chankey='%s', suspendreason='%s'," | |
87 | "comment='%s', lasttimestamp=%jd WHERE ID=%u",escname,rcp->flags,rcp->forcemodes, | |
88 | rcp->denymodes,rcp->limit,rcp->autolimit, rcp->banstyle, | |
89 | rcp->lastactive,rcp->statsreset,rcp->banduration, | |
90 | rcp->founder, rcp->addedby, rcp->suspendby, rcp->suspendtime, | |
91 | rcp->chantype,rcp->totaljoins,rcp->tripjoins, | |
92 | rcp->maxusers,rcp->tripusers, | |
93 | escwelcome,esctopic,esckey,escreason,esccomment,(intmax_t)rcp->ltimestamp,rcp->ID); | |
94 | } | |
95 | ||
96 | void csdb_updatechannelcounters(regchan *rcp) { | |
97 | dbquery("UPDATE chanserv.channels SET " | |
98 | "lastactive=%lu, totaljoins=%u," | |
99 | "tripjoins=%u, maxusers=%u, tripusers=%u " | |
100 | "WHERE ID=%u", | |
101 | rcp->lastactive, | |
102 | rcp->totaljoins,rcp->tripjoins, | |
103 | rcp->maxusers,rcp->tripusers, | |
104 | rcp->ID); | |
105 | } | |
106 | ||
107 | void csdb_updatechanneltimestamp(regchan *rcp) { | |
108 | dbquery("UPDATE chanserv.channels SET " | |
109 | "lasttimestamp=%jd WHERE ID=%u", | |
110 | (intmax_t)rcp->ltimestamp, rcp->ID); | |
111 | } | |
112 | ||
113 | void csdb_createchannel(regchan *rcp) { | |
114 | char escwelcome[WELCOMELEN*2+1]; | |
115 | char esctopic[TOPICLEN*2+1]; | |
116 | char esckey[70]; | |
117 | char escreason[510]; | |
118 | char esccomment[510]; | |
119 | char escname[510]; | |
120 | ||
121 | dbescapestring(escname, rcp->index->name->content, rcp->index->name->length); | |
122 | ||
123 | if (rcp->welcome) | |
124 | dbescapestring(escwelcome, rcp->welcome->content, | |
125 | rcp->welcome->length); | |
126 | else | |
127 | escwelcome[0]='\0'; | |
128 | ||
129 | if (rcp->topic) | |
130 | dbescapestring(esctopic, rcp->topic->content, rcp->topic->length); | |
131 | else | |
132 | esctopic[0]='\0'; | |
133 | ||
134 | if (rcp->key) | |
135 | dbescapestring(esckey, rcp->key->content, rcp->key->length); | |
136 | else | |
137 | esckey[0]='\0'; | |
138 | ||
139 | if (rcp->suspendreason) | |
140 | dbescapestring(escreason, rcp->suspendreason->content, | |
141 | rcp->suspendreason->length); | |
142 | else | |
143 | escreason[0]='\0'; | |
144 | ||
145 | if (rcp->comment) | |
146 | dbescapestring(esccomment, rcp->comment->content, | |
147 | rcp->comment->length); | |
148 | else | |
149 | esccomment[0]='\0'; | |
150 | ||
151 | dbquery("INSERT INTO chanserv.channels (ID, name, flags, forcemodes, denymodes," | |
152 | "chanlimit, autolimit, banstyle, created, lastactive, statsreset, " | |
153 | "banduration, founder, addedby, suspendby, suspendtime, chantype, totaljoins, tripjoins," | |
154 | "maxusers, tripusers, welcome, topic, chankey, suspendreason, " | |
155 | "comment, lasttimestamp) VALUES (%u,'%s',%d,%d,%d,%d,%d,%d,%lu,%lu,%lu,%lu,%u," | |
156 | "%u,%u,%lu,%d,%u,%u,%u,%u,'%s','%s','%s','%s','%s',%jd)", | |
157 | rcp->ID, escname, rcp->flags,rcp->forcemodes, | |
158 | rcp->denymodes,rcp->limit,rcp->autolimit, rcp->banstyle, rcp->created, | |
159 | rcp->lastactive,rcp->statsreset,rcp->banduration, | |
160 | rcp->founder, rcp->addedby, rcp->suspendby, rcp->suspendtime, | |
161 | rcp->chantype,rcp->totaljoins,rcp->tripjoins, | |
162 | rcp->maxusers,rcp->tripusers, | |
163 | escwelcome,esctopic,esckey,escreason,esccomment,(intmax_t)rcp->ltimestamp); | |
164 | } | |
165 | ||
166 | void csdb_deletechannel(regchan *rcp) { | |
167 | dbquery("DELETE FROM chanserv.channels WHERE ID=%u",rcp->ID); | |
168 | dbquery("DELETE FROM chanserv.chanusers WHERE channelID=%u",rcp->ID); | |
169 | dbquery("DELETE FROM chanserv.bans WHERE channelID=%u",rcp->ID); | |
170 | } | |
171 | ||
172 | void csdb_deleteuser(reguser *rup) { | |
173 | dbquery("DELETE FROM chanserv.users WHERE ID=%u",rup->ID); | |
174 | dbquery("DELETE FROM chanserv.chanusers WHERE userID=%u",rup->ID); | |
175 | } | |
176 | ||
177 | void csdb_updateuser(reguser *rup) { | |
178 | char escpassword[25]; | |
179 | char escemail[210]; | |
180 | char esclastuserhost[160]; | |
181 | char escreason[510]; | |
182 | char esccomment[510]; | |
183 | char escinfo[210]; | |
184 | char esclastemail[210]; | |
185 | ||
186 | dbescapestring(escpassword, rup->password, strlen(rup->password)); | |
187 | ||
188 | if (rup->email) | |
189 | dbescapestring(escemail, rup->email->content, rup->email->length); | |
190 | else | |
191 | escemail[0]='\0'; | |
192 | ||
193 | if (rup->lastemail) | |
194 | dbescapestring(esclastemail, rup->lastemail->content, rup->lastemail->length); | |
195 | else | |
196 | esclastemail[0]='\0'; | |
197 | ||
198 | if (rup->lastuserhost) | |
199 | dbescapestring(esclastuserhost, rup->lastuserhost->content, rup->lastuserhost->length); | |
200 | else | |
201 | esclastuserhost[0]='\0'; | |
202 | ||
203 | if (rup->suspendreason) | |
204 | dbescapestring(escreason, rup->suspendreason->content, rup->suspendreason->length); | |
205 | else | |
206 | escreason[0]='\0'; | |
207 | ||
208 | if (rup->comment) | |
209 | dbescapestring(esccomment, rup->comment->content, rup->comment->length); | |
210 | else | |
211 | esccomment[0]='\0'; | |
212 | ||
213 | if (rup->info) | |
214 | dbescapestring(escinfo, rup->info->content, rup->info->length); | |
215 | else | |
216 | escinfo[0]='\0'; | |
217 | ||
218 | dbquery("UPDATE chanserv.users SET lastauth=%lu, lastemailchng=%lu, flags=%u," | |
219 | "language=%u, suspendby=%u, suspendexp=%lu, suspendtime=%lu, lockuntil=%lu, password='%s', email='%s'," | |
220 | "lastuserhost='%s', suspendreason='%s', comment='%s', info='%s', lastemail='%s', lastpasschng=%lu " | |
221 | " WHERE ID=%u", | |
222 | rup->lastauth, rup->lastemailchange, rup->flags, rup->languageid, rup->suspendby, rup->suspendexp, | |
223 | rup->suspendtime, rup->lockuntil, escpassword, escemail, esclastuserhost, escreason, esccomment, escinfo, esclastemail, | |
224 | rup->lastpasschange, | |
225 | rup->ID); | |
226 | } | |
227 | ||
228 | void csdb_createuser(reguser *rup) { | |
229 | char escpassword[25]; | |
230 | char escemail[210]; | |
231 | char esclastuserhost[160]; | |
232 | char escreason[510]; | |
233 | char esccomment[510]; | |
234 | char escusername[35]; | |
235 | char escinfo[210]; | |
236 | char esclastemail[210]; | |
237 | ||
238 | dbescapestring(escusername, rup->username, strlen(rup->username)); | |
239 | dbescapestring(escpassword, rup->password, strlen(rup->password)); | |
240 | ||
241 | if (rup->email) | |
242 | dbescapestring(escemail, rup->email->content, rup->email->length); | |
243 | else | |
244 | escemail[0]='\0'; | |
245 | ||
246 | if (rup->lastemail) | |
247 | dbescapestring(esclastemail, rup->lastemail->content, rup->lastemail->length); | |
248 | else | |
249 | esclastemail[0]='\0'; | |
250 | ||
251 | if (rup->lastuserhost) | |
252 | dbescapestring(esclastuserhost, rup->lastuserhost->content, rup->lastuserhost->length); | |
253 | else | |
254 | esclastuserhost[0]='\0'; | |
255 | ||
256 | if (rup->suspendreason) | |
257 | dbescapestring(escreason, rup->suspendreason->content, rup->suspendreason->length); | |
258 | else | |
259 | escreason[0]='\0'; | |
260 | ||
261 | if (rup->comment) | |
262 | dbescapestring(esccomment, rup->comment->content, rup->comment->length); | |
263 | else | |
264 | esccomment[0]='\0'; | |
265 | ||
266 | if (rup->info) | |
267 | dbescapestring(escinfo, rup->info->content, rup->info->length); | |
268 | else | |
269 | escinfo[0]='\0'; | |
270 | ||
271 | dbquery("INSERT INTO chanserv.users (ID, username, created, lastauth, lastemailchng, " | |
272 | "flags, language, suspendby, suspendexp, suspendtime, lockuntil, password, email, lastuserhost, " | |
273 | "suspendreason, comment, info, lastemail, lastpasschng)" | |
274 | "VALUES (%u,'%s',%lu,%lu,%lu,%u,%u,%u,%lu,%lu,%lu,'%s','%s','%s','%s','%s','%s','%s',%lu)", | |
275 | rup->ID, escusername, rup->created, rup->lastauth, rup->lastemailchange, rup->flags, | |
276 | rup->languageid, rup->suspendby, rup->suspendexp, rup->suspendtime, rup->lockuntil, | |
277 | escpassword, escemail, esclastuserhost, escreason, esccomment, escinfo, esclastemail, | |
278 | rup->lastpasschange); | |
279 | } | |
280 | ||
281 | ||
282 | void csdb_updatechanuser(regchanuser *rcup) { | |
283 | char escinfo[210]; | |
284 | ||
285 | if (rcup->info) | |
286 | dbescapestring(escinfo, rcup->info->content, rcup->info->length); | |
287 | else | |
288 | escinfo[0]='\0'; | |
289 | ||
290 | dbquery("UPDATE chanserv.chanusers SET flags=%u, changetime=%lu, " | |
291 | "usetime=%lu, info='%s' WHERE channelID=%u and userID=%u", | |
292 | rcup->flags, rcup->changetime, rcup->usetime, escinfo, rcup->chan->ID,rcup->user->ID); | |
293 | } | |
294 | ||
295 | void csdb_createchanuser(regchanuser *rcup) { | |
296 | char escinfo[210]; | |
297 | ||
298 | if (rcup->info) | |
299 | dbescapestring(escinfo, rcup->info->content, rcup->info->length); | |
300 | else | |
301 | escinfo[0]='\0'; | |
302 | ||
303 | dbquery("INSERT INTO chanserv.chanusers VALUES(%u, %u, %u, %lu, %lu, '%s')", | |
304 | rcup->user->ID, rcup->chan->ID, rcup->flags, rcup->changetime, | |
305 | rcup->usetime, escinfo); | |
306 | } | |
307 | ||
308 | void csdb_deletechanuser(regchanuser *rcup) { | |
309 | dbquery("DELETE FROM chanserv.chanusers WHERE channelid=%u AND userID=%u", | |
310 | rcup->chan->ID, rcup->user->ID); | |
311 | } | |
312 | ||
313 | void csdb_createban(regchan *rcp, regban *rbp) { | |
314 | char escreason[500]; | |
315 | char banstr[100]; | |
316 | char escban[200]; | |
317 | ||
318 | strcpy(banstr,bantostring(rbp->cbp)); | |
319 | dbescapestring(escban,banstr,strlen(banstr)); | |
320 | ||
321 | if (rbp->reason) | |
322 | dbescapestring(escreason, rbp->reason->content, rbp->reason->length); | |
323 | else | |
324 | escreason[0]='\0'; | |
325 | ||
326 | dbquery("INSERT INTO chanserv.bans (banID, channelID, userID, hostmask, " | |
327 | "expiry, reason) VALUES (%u,%u,%u,'%s',%lu,'%s')", rbp->ID, rcp->ID, | |
328 | rbp->setby, escban, rbp->expiry, escreason); | |
329 | } | |
330 | ||
331 | void csdb_updateban(regchan *rcp, regban *rbp) { | |
332 | char escreason[500]; | |
333 | char banstr[100]; | |
334 | char escban[200]; | |
335 | ||
336 | strcpy(banstr,bantostring(rbp->cbp)); | |
337 | dbescapestring(escban,banstr,strlen(banstr)); | |
338 | ||
339 | if (rbp->reason) | |
340 | dbescapestring(escreason, rbp->reason->content, rbp->reason->length); | |
341 | else | |
342 | escreason[0]='\0'; | |
343 | ||
344 | dbquery("UPDATE chanserv.bans set channelID=%u, userID=%u, hostmask='%s', expiry=%lu, reason='%s' " | |
345 | "WHERE banID=%u", rcp->ID, rbp->setby, escban, rbp->expiry, escreason, rbp->ID); | |
346 | } | |
347 | ||
348 | void csdb_deleteban(regban *rbp) { | |
349 | dbquery("DELETE FROM chanserv.bans WHERE banID=%u", rbp->ID); | |
350 | } | |
351 | ||
352 | void csdb_createmail(reguser *rup, int type) { | |
353 | char sqlquery[6000]; | |
354 | char escemail[210]; | |
355 | ||
356 | if (type == QMAIL_NEWEMAIL) { | |
357 | if (rup->email) { | |
358 | dbescapestring(escemail, rup->email->content, rup->email->length); | |
359 | sprintf(sqlquery, "INSERT INTO chanserv.email (userID, emailType, prevEmail) " | |
360 | "VALUES (%u,%u,'%s')", rup->ID, type, escemail); | |
361 | } | |
362 | } else { | |
363 | sprintf(sqlquery, "INSERT INTO chanserv.email (userID, emailType) VALUES (%u,%u)", rup->ID, type); | |
364 | } | |
365 | ||
366 | dbquery("%s", sqlquery); | |
367 | } | |
368 | ||
369 | void csdb_deletemaildomain(maildomain *mdp) { | |
370 | dbquery("DELETE FROM chanserv.maildomain WHERE ID=%u", mdp->ID); | |
371 | } | |
372 | ||
373 | void csdb_createmaildomain(maildomain *mdp) { | |
374 | char escdomain[210]; | |
375 | dbescapestring(escdomain, mdp->name->content, mdp->name->length); | |
376 | ||
377 | dbquery("INSERT INTO chanserv.maildomain (id, name, domainlimit, actlimit, flags) VALUES(%u, '%s', %u, %u, %u)", mdp->ID,escdomain,mdp->limit,mdp->actlimit,mdp->flags); | |
378 | } | |
379 | ||
380 | void csdb_updatemaildomain(maildomain *mdp) { | |
381 | char escdomain[210]; | |
382 | dbescapestring(escdomain, mdp->name->content, mdp->name->length); | |
383 | ||
384 | dbquery("UPDATE chanserv.maildomain SET domainlimit=%u, actlimit=%u, flags=%u, name='%s' WHERE ID=%u", mdp->limit,mdp->actlimit,mdp->flags,escdomain,mdp->ID); | |
385 | } | |
386 | ||
387 | void csdb_chanlevhistory_insert(regchan *rcp, nick *np, reguser *trup, flag_t oldflags, flag_t newflags) { | |
388 | reguser *rup=getreguserfromnick(np); | |
389 | assert(rup != NULL); | |
390 | ||
391 | dbquery("INSERT INTO chanserv.chanlevhistory (userID, channelID, targetID, changetime, authtime, " | |
392 | "oldflags, newflags) VALUES (%u, %u, %u, %lu, %lu, %u, %u)", rup->ID, rcp->ID, trup->ID, getnettime(), np->accountts, | |
393 | oldflags, newflags); | |
394 | } | |
395 | ||
396 | void csdb_accounthistory_insert(nick *np, char *oldpass, char *newpass, char *oldemail, char *newemail) { | |
397 | reguser *rup=getreguserfromnick(np); | |
398 | char escoldpass[PASSLEN*2+5]; | |
399 | char escnewpass[PASSLEN*2+5]; | |
400 | char escoldemail[EMAILLEN*2+5]; | |
401 | char escnewemail[EMAILLEN*2+5]; | |
402 | ||
403 | if (!rup || UHasOperPriv(rup)) | |
404 | return; | |
405 | ||
406 | if (oldpass) | |
407 | dbescapestring(escoldpass, oldpass, CSMIN(strlen(oldpass), PASSLEN)); | |
408 | else | |
409 | escoldpass[0]='\0'; | |
410 | ||
411 | if (newpass) | |
412 | dbescapestring(escnewpass, newpass, CSMIN(strlen(newpass), PASSLEN)); | |
413 | else | |
414 | escnewpass[0]='\0'; | |
415 | ||
416 | if (oldemail) | |
417 | dbescapestring(escoldemail, oldemail, CSMIN(strlen(oldemail), EMAILLEN)); | |
418 | else | |
419 | escoldemail[0]='\0'; | |
420 | if (newemail) | |
421 | dbescapestring(escnewemail, newemail, CSMIN(strlen(newemail), EMAILLEN)); | |
422 | else | |
423 | escnewemail[0]='\0'; | |
424 | ||
425 | dbquery("INSERT INTO chanserv.accounthistory (userID, changetime, authtime, oldpassword, newpassword, oldemail, " | |
426 | "newemail) VALUES (%u, %lu, %lu, '%s', '%s', '%s', '%s')", rup->ID, getnettime(), np->accountts, escoldpass, escnewpass, | |
427 | escoldemail, escnewemail); | |
428 | } | |
429 | ||
430 | void csdb_cleanuphistories(time_t expire_time) { | |
431 | Error("chanserv", ERR_INFO, "Cleaning histories."); | |
432 | dbquery("DELETE FROM chanserv.authhistory WHERE disconnecttime < %lu AND disconnecttime <> 0", expire_time); | |
433 | dbquery("DELETE FROM chanserv.chanlevhistory WHERE authtime < %lu", expire_time); | |
434 | dbquery("DELETE FROM chanserv.accounthistory WHERE authtime < %lu", expire_time); | |
435 | } | |
436 | ||
437 | void csdb_deletemaillock(maillock *mlp) { | |
438 | dbquery("DELETE FROM chanserv.maillocks WHERE ID=%u", mlp->id); | |
439 | } | |
440 | ||
441 | void csdb_createmaillock(maillock *mlp) { | |
442 | char escpattern[1024], escreason[1024]; | |
443 | ||
444 | dbescapestring(escpattern, mlp->pattern->content, mlp->pattern->length); | |
445 | ||
446 | if (mlp->reason) | |
447 | dbescapestring(escreason, mlp->reason->content, mlp->reason->length); | |
448 | else | |
449 | escreason[0]='\0'; | |
450 | ||
451 | dbquery("INSERT INTO chanserv.maillocks (id, pattern, reason, createdby, created) VALUES(%u, '%s', '%s', %u, %jd)", | |
452 | mlp->id,escpattern,escreason,mlp->createdby,(intmax_t)mlp->created); | |
453 | } | |
454 | ||
455 | void csdb_updatemaillock(maillock *mlp) { | |
456 | char escpattern[1024], escreason[1024]; | |
457 | ||
458 | dbescapestring(escpattern, mlp->pattern->content, mlp->pattern->length); | |
459 | ||
460 | if (mlp->reason) | |
461 | dbescapestring(escreason, mlp->reason->content, mlp->reason->length); | |
462 | else | |
463 | escreason[0]='\0'; | |
464 | ||
465 | dbquery("UPDATE chanserv.maillocks SET pattern='%s', reason='%s', createdby=%u, created=%jd WHERE ID=%u", escpattern, escreason, mlp->createdby, (intmax_t)mlp->created, mlp->id); | |
466 | } | |
467 |