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