]>
jfr.im git - irc/quakenet/newserv.git/blob - chanserv/chanservdb_updates.c
2 * chanservdb_updates.c:
3 * Handle all the update requests for the database.
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"
19 void csdb_updateauthinfo(reguser
*rup
) {
20 char eschost
[2*HOSTLEN
+1];
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
);
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
);
32 void csdb_updatetopic(regchan
*rcp
) {
33 char esctopic
[TOPICLEN
*2+5];
36 PQescapeString(esctopic
,rcp
->topic
->content
,rcp
->topic
->length
);
40 pqquery("UPDATE channels SET topic='%s' WHERE ID=%u",esctopic
,rcp
->ID
);
43 void csdb_updatechannel(regchan
*rcp
) {
44 char escwelcome
[WELCOMELEN
*2+1];
45 char esctopic
[TOPICLEN
*2+1];
51 PQescapeString(escname
, rcp
->index
->name
->content
, rcp
->index
->name
->length
);
54 PQescapeString(escwelcome
, rcp
->welcome
->content
,
55 rcp
->welcome
->length
);
60 PQescapeString(esctopic
, rcp
->topic
->content
, rcp
->topic
->length
);
65 PQescapeString(esckey
, rcp
->key
->content
, rcp
->key
->length
);
69 if (rcp
->suspendreason
)
70 PQescapeString(escreason
, rcp
->suspendreason
->content
,
71 rcp
->suspendreason
->length
);
76 PQescapeString(esccomment
, rcp
->comment
->content
,
77 rcp
->comment
->length
);
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,"
84 "addedby=%u, suspendby=%u, chantype=%d, totaljoins=%u,"
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
,
90 rcp
->founder
, rcp
->addedby
, rcp
->suspendby
,
91 rcp
->chantype
,rcp
->totaljoins
,rcp
->tripjoins
,
92 rcp
->maxusers
,rcp
->tripusers
,
93 escwelcome
,esctopic
,esckey
,escreason
,esccomment
,rcp
->ltimestamp
,rcp
->ID
);
96 void csdb_updatechannelcounters(regchan
*rcp
) {
97 pqquery("UPDATE channels SET "
98 "lastactive=%lu, totaljoins=%u,"
99 "tripjoins=%u, maxusers=%u, tripusers=%u "
102 rcp
->totaljoins
,rcp
->tripjoins
,
103 rcp
->maxusers
,rcp
->tripusers
,
107 void csdb_updatechanneltimestamp(regchan
*rcp
) {
108 pqquery("UPDATE channels SET "
109 "lasttimestamp=%u WHERE ID=%u",
110 rcp
->ltimestamp
, rcp
->ID
);
113 void csdb_createchannel(regchan
*rcp
) {
114 char escwelcome
[WELCOMELEN
*2+1];
115 char esctopic
[TOPICLEN
*2+1];
118 char esccomment
[510];
121 PQescapeString(escname
, rcp
->index
->name
->content
, rcp
->index
->name
->length
);
124 PQescapeString(escwelcome
, rcp
->welcome
->content
,
125 rcp
->welcome
->length
);
130 PQescapeString(esctopic
, rcp
->topic
->content
, rcp
->topic
->length
);
135 PQescapeString(esckey
, rcp
->key
->content
, rcp
->key
->length
);
139 if (rcp
->suspendreason
)
140 PQescapeString(escreason
, rcp
->suspendreason
->content
,
141 rcp
->suspendreason
->length
);
146 PQescapeString(esccomment
, rcp
->comment
->content
,
147 rcp
->comment
->length
);
151 pqquery("INSERT INTO channels (ID, name, flags, forcemodes, denymodes,"
152 "chanlimit, autolimit, banstyle, created, lastactive, statsreset, "
153 "banduration, founder, addedby, suspendby, 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,%d,%u,%u,%u,%u,'%s','%s','%s','%s','%s',%d)",
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
,
161 rcp
->chantype
,rcp
->totaljoins
,rcp
->tripjoins
,
162 rcp
->maxusers
,rcp
->tripusers
,
163 escwelcome
,esctopic
,esckey
,escreason
,esccomment
,rcp
->ltimestamp
);
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
);
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
);
177 void csdb_updateuser(reguser
*rup
) {
178 char escpassword
[25];
179 char escmasterpass
[25];
181 char esclastuserhost
[160];
183 char esccomment
[510];
186 PQescapeString(escpassword
, rup
->password
, strlen(rup
->password
));
187 PQescapeString(escmasterpass
, rup
->masterpass
, strlen(rup
->masterpass
));
190 PQescapeString(escemail
, rup
->email
->content
, rup
->email
->length
);
194 if (rup
->lastuserhost
)
195 PQescapeString(esclastuserhost
, rup
->lastuserhost
->content
, rup
->lastuserhost
->length
);
197 esclastuserhost
[0]='\0';
199 if (rup
->suspendreason
)
200 PQescapeString(escreason
, rup
->suspendreason
->content
, rup
->suspendreason
->length
);
205 PQescapeString(esccomment
, rup
->comment
->content
, rup
->comment
->length
);
210 PQescapeString(escinfo
, rup
->info
->content
, rup
->info
->length
);
214 pqquery("UPDATE users SET lastauth=%lu, lastemailchng=%lu, flags=%u,"
215 "language=%u, suspendby=%u, suspendexp=%lu, password='%s', masterpass='%s', email='%s',"
216 "lastuserhost='%s', suspendreason='%s', comment='%s', info='%s' WHERE ID=%u",
217 rup
->lastauth
, rup
->lastemailchange
, rup
->flags
, rup
->languageid
, rup
->suspendby
, rup
->suspendexp
,
218 escpassword
, escmasterpass
, escemail
, esclastuserhost
, escreason
, esccomment
, escinfo
,
222 void csdb_createuser(reguser
*rup
) {
223 char escpassword
[25];
224 char escmasterpass
[25];
226 char esclastuserhost
[160];
228 char esccomment
[510];
229 char escusername
[35];
232 PQescapeString(escusername
, rup
->username
, strlen(rup
->username
));
233 PQescapeString(escpassword
, rup
->password
, strlen(rup
->password
));
234 PQescapeString(escmasterpass
, rup
->masterpass
, strlen(rup
->masterpass
));
237 PQescapeString(escemail
, rup
->email
->content
, rup
->email
->length
);
241 if (rup
->lastuserhost
)
242 PQescapeString(esclastuserhost
, rup
->lastuserhost
->content
, rup
->lastuserhost
->length
);
244 esclastuserhost
[0]='\0';
246 if (rup
->suspendreason
)
247 PQescapeString(escreason
, rup
->suspendreason
->content
, rup
->suspendreason
->length
);
252 PQescapeString(esccomment
, rup
->comment
->content
, rup
->comment
->length
);
257 PQescapeString(escinfo
, rup
->info
->content
, rup
->info
->length
);
261 pqquery("INSERT INTO users (ID, username, created, lastauth, lastemailchng, "
262 "flags, language, suspendby, suspendexp, password, masterpass, email, lastuserhost, "
263 "suspendreason, comment, info) VALUES (%u,'%s',%lu,%lu,%lu,%u,%u,%u,%lu,'%s','%s',"
264 "'%s','%s','%s','%s','%s')",
265 rup
->ID
, escusername
, rup
->created
, rup
->lastauth
, rup
->lastemailchange
, rup
->flags
,
266 rup
->languageid
, rup
->suspendby
, rup
->suspendexp
,
267 escpassword
, escmasterpass
, escemail
, esclastuserhost
, escreason
, esccomment
, escinfo
);
271 void csdb_updatechanuser(regchanuser
*rcup
) {
275 PQescapeString(escinfo
, rcup
->info
->content
, rcup
->info
->length
);
279 pqquery("UPDATE chanusers SET flags=%u, changetime=%lu, "
280 "usetime=%lu, info='%s' WHERE channelID=%u and userID=%u",
281 rcup
->flags
, rcup
->changetime
, rcup
->usetime
, escinfo
, rcup
->chan
->ID
,rcup
->user
->ID
);
284 void csdb_createchanuser(regchanuser
*rcup
) {
288 PQescapeString(escinfo
, rcup
->info
->content
, rcup
->info
->length
);
292 pqquery("INSERT INTO chanusers VALUES(%u, %u, %u, %lu, %lu, '%s')",
293 rcup
->user
->ID
, rcup
->chan
->ID
, rcup
->flags
, rcup
->changetime
,
294 rcup
->usetime
, escinfo
);
297 void csdb_deletechanuser(regchanuser
*rcup
) {
298 pqquery("DELETE FROM chanusers WHERE channelid=%u AND userID=%u",
299 rcup
->chan
->ID
, rcup
->user
->ID
);
302 void csdb_createban(regchan
*rcp
, regban
*rbp
) {
307 strcpy(banstr
,bantostring(rbp
->cbp
));
308 PQescapeString(escban
,banstr
,strlen(banstr
));
311 PQescapeString(escreason
, rbp
->reason
->content
, rbp
->reason
->length
);
315 pqquery("INSERT INTO bans (banID, channelID, userID, hostmask, "
316 "expiry, reason) VALUES (%u,%u,%u,'%s',%lu,'%s')", rbp
->ID
, rcp
->ID
,
317 rbp
->setby
, escban
, rbp
->expiry
, escreason
);
320 void csdb_deleteban(regban
*rbp
) {
321 pqquery("DELETE FROM bans WHERE banID=%u", rbp
->ID
);
324 void csdb_createmail(reguser
*rup
, int type
) {
328 if (type
== QMAIL_NEWEMAIL
) {
330 PQescapeString(escemail
, rup
->email
->content
, rup
->email
->length
);
331 sprintf(sqlquery
, "INSERT INTO email (userID, emailType, prevEmail) "
332 "VALUES (%u,%u,'%s')", rup
->ID
, type
, escemail
);
335 sprintf(sqlquery
, "INSERT INTO email (userID, emailType) VALUES (%u,%u)", rup
->ID
, type
);
338 pqquery("%s", sqlquery
);
341 void csdb_deletemaildomain(maildomain
*mdp
) {
342 pqquery("DELETE FROM maildomain WHERE ID=%u", mdp
->ID
);
345 void csdb_createmaildomain(maildomain
*mdp
) {
347 PQescapeString(escdomain
, mdp
->name
->content
, mdp
->name
->length
);
349 pqquery("INSERT INTO maildomain (id, name, domainlimit) VALUES(%u, '%s', %u)", mdp
->ID
,escdomain
,mdp
->limit
);
352 void csdb_updatemaildomain(maildomain
*mdp
) {
354 PQescapeString(escdomain
, mdp
->name
->content
, mdp
->name
->length
);
356 pqquery("UPDATE maildomain SET domainlimit=%u WHERE ID=%u", mdp
->limit
,mdp
->ID
);
359 void csdb_chanlevhistory_insert(regchan
*rcp
, nick
*np
, reguser
*trup
, flag_t oldflags
, flag_t newflags
) {
360 reguser
*rup
=getreguserfromnick(np
);
362 pqquery("INSERT INTO chanlevhistory (userID, channelID, targetID, changetime, authtime, "
363 "oldflags, newflags) VALUES (%u, %u, %u, %lu, %lu, %u, %u)", rup
->ID
, rcp
->ID
, trup
->ID
, getnettime(), np
->accountts
,
367 void csdb_accounthistory_insert(nick
*np
, char *oldpass
, char *newpass
, sstring
*oldemail
, sstring
*newemail
) {
368 reguser
*rup
=getreguserfromnick(np
);
371 char escoldemail
[130];
372 char escnewemail
[130];
374 if (!rup
|| UHasOperPriv(rup
))
378 PQescapeString(escoldpass
, oldpass
, strlen(oldpass
));
383 PQescapeString(escnewpass
, newpass
, strlen(newpass
));
388 PQescapeString(escoldemail
, oldemail
->content
, oldemail
->length
);
392 PQescapeString(escnewemail
, newemail
->content
, newemail
->length
);
396 pqquery("INSERT INTO accounthistory (userID, changetime, authtime, oldpassword, newpassword, oldemail, "
397 "newemail) VALUES (%u, %lu, %lu, '%s', '%s', '%s', '%s')", rup
->ID
, getnettime(), np
->accountts
, escoldpass
, escnewpass
,
398 escoldemail
, escnewemail
);
401 freesstring(newemail
);
404 void csdb_cleanuphistories() {
405 time_t expire_time
=getnettime()-604800;
407 Error("chanserv", ERR_INFO
, "Cleaning histories.");
408 pqquery("DELETE FROM authhistory WHERE authtime < %lu", expire_time
);
409 pqquery("DELETE FROM chanlevhistory WHERE changetime < %lu", expire_time
);
410 pqquery("DELETE FROM accounthistory WHERE changetime < %lu", expire_time
);