]>
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 "../dbapi/dbapi.h"
8 #include "../core/config.h"
9 #include "../lib/sstring.h"
10 #include "../parser/parser.h"
11 #include "../core/events.h"
18 void csdb_updateauthinfo ( reguser
* rup
) {
19 char eschost
[ 2 * HOSTLEN
+ 1 ];
21 dbescapestring ( eschost
, rup
-> lastuserhost
-> content
, rup
-> lastuserhost
-> length
);
22 dbquery ( "UPDATE chanserv.users SET lastauth= %l u,lastuserhost=' %s ' WHERE ID= %u " ,
23 rup
-> lastauth
, eschost
, rup
-> ID
);
26 void csdb_updatelastjoin ( regchanuser
* rcup
) {
27 dbquery ( "UPDATE chanserv.chanusers SET usetime= %l u WHERE userID= %u and channelID= %u " ,
28 rcup
-> usetime
, rcup
-> user
-> ID
, rcup
-> chan
-> ID
);
31 void csdb_updatetopic ( regchan
* rcp
) {
32 char esctopic
[ TOPICLEN
* 2 + 5 ];
35 dbescapestring ( esctopic
, rcp
-> topic
-> content
, rcp
-> topic
-> length
);
39 dbquery ( "UPDATE chanserv.channels SET topic=' %s ' WHERE ID= %u " , esctopic
, rcp
-> ID
);
42 void csdb_updatechannel ( regchan
* rcp
) {
43 char escwelcome
[ WELCOMELEN
* 2 + 1 ];
44 char esctopic
[ TOPICLEN
* 2 + 1 ];
50 dbescapestring ( escname
, rcp
-> index
-> name
-> content
, rcp
-> index
-> name
-> length
);
53 dbescapestring ( escwelcome
, rcp
-> welcome
-> content
,
54 rcp
-> welcome
-> length
);
59 dbescapestring ( esctopic
, rcp
-> topic
-> content
, rcp
-> topic
-> length
);
64 dbescapestring ( esckey
, rcp
-> key
-> content
, rcp
-> key
-> length
);
68 if ( rcp
-> suspendreason
)
69 dbescapestring ( escreason
, rcp
-> suspendreason
-> content
,
70 rcp
-> suspendreason
-> length
);
75 dbescapestring ( esccomment
, rcp
-> comment
-> content
,
76 rcp
-> comment
-> length
);
80 dbquery ( "UPDATE chanserv.channels SET name=' %s ', flags= %d , forcemodes= %d ,"
81 "denymodes= %d , chanlimit= %d , autolimit= %d , banstyle= %d ,"
82 "lastactive= %l u,statsreset= %l u, banduration= %l u, founder= %u ,"
83 "addedby= %u , suspendby= %u , suspendtime= %l u, chantype= %d , totaljoins= %u ,"
84 "tripjoins= %u , maxusers= %u , tripusers= %u ,"
85 "welcome=' %s ', topic=' %s ', chankey=' %s ', suspendreason=' %s ',"
86 "comment=' %s ', lasttimestamp= %j d WHERE ID= %u " , escname
, rcp
-> flags
, rcp
-> forcemodes
,
87 rcp
-> denymodes
, rcp
-> limit
, rcp
-> autolimit
, rcp
-> banstyle
,
88 rcp
-> lastactive
, rcp
-> statsreset
, rcp
-> banduration
,
89 rcp
-> founder
, rcp
-> addedby
, rcp
-> suspendby
, rcp
-> suspendtime
,
90 rcp
-> chantype
, rcp
-> totaljoins
, rcp
-> tripjoins
,
91 rcp
-> maxusers
, rcp
-> tripusers
,
92 escwelcome
, esctopic
, esckey
, escreason
, esccomment
,( intmax_t ) rcp
-> ltimestamp
, rcp
-> ID
);
95 void csdb_updatechannelcounters ( regchan
* rcp
) {
96 dbquery ( "UPDATE chanserv.channels SET "
97 "lastactive= %l u, totaljoins= %u ,"
98 "tripjoins= %u , maxusers= %u , tripusers= %u "
101 rcp
-> totaljoins
, rcp
-> tripjoins
,
102 rcp
-> maxusers
, rcp
-> tripusers
,
106 void csdb_updatechanneltimestamp ( regchan
* rcp
) {
107 dbquery ( "UPDATE chanserv.channels SET "
108 "lasttimestamp= %j d WHERE ID= %u " ,
109 ( intmax_t ) rcp
-> ltimestamp
, rcp
-> ID
);
112 void csdb_createchannel ( regchan
* rcp
) {
113 char escwelcome
[ WELCOMELEN
* 2 + 1 ];
114 char esctopic
[ TOPICLEN
* 2 + 1 ];
117 char esccomment
[ 510 ];
120 dbescapestring ( escname
, rcp
-> index
-> name
-> content
, rcp
-> index
-> name
-> length
);
123 dbescapestring ( escwelcome
, rcp
-> welcome
-> content
,
124 rcp
-> welcome
-> length
);
129 dbescapestring ( esctopic
, rcp
-> topic
-> content
, rcp
-> topic
-> length
);
134 dbescapestring ( esckey
, rcp
-> key
-> content
, rcp
-> key
-> length
);
138 if ( rcp
-> suspendreason
)
139 dbescapestring ( escreason
, rcp
-> suspendreason
-> content
,
140 rcp
-> suspendreason
-> length
);
145 dbescapestring ( esccomment
, rcp
-> comment
-> content
,
146 rcp
-> comment
-> length
);
150 dbquery ( "INSERT INTO chanserv.channels (ID, name, flags, forcemodes, denymodes,"
151 "chanlimit, autolimit, banstyle, created, lastactive, statsreset, "
152 "banduration, founder, addedby, suspendby, suspendtime, chantype, totaljoins, tripjoins,"
153 "maxusers, tripusers, welcome, topic, chankey, suspendreason, "
154 "comment, lasttimestamp) VALUES ( %u ,' %s ', %d , %d , %d , %d , %d , %d , %l u, %l u, %l u, %l u, %u ,"
155 " %u , %u , %l u, %d , %u , %u , %u , %u ,' %s ',' %s ',' %s ',' %s ',' %s ', %j d)" ,
156 rcp
-> ID
, escname
, rcp
-> flags
, rcp
-> forcemodes
,
157 rcp
-> denymodes
, rcp
-> limit
, rcp
-> autolimit
, rcp
-> banstyle
, rcp
-> created
,
158 rcp
-> lastactive
, rcp
-> statsreset
, rcp
-> banduration
,
159 rcp
-> founder
, rcp
-> addedby
, rcp
-> suspendby
, rcp
-> suspendtime
,
160 rcp
-> chantype
, rcp
-> totaljoins
, rcp
-> tripjoins
,
161 rcp
-> maxusers
, rcp
-> tripusers
,
162 escwelcome
, esctopic
, esckey
, escreason
, esccomment
,( intmax_t ) rcp
-> ltimestamp
);
165 void csdb_deletechannel ( regchan
* rcp
) {
166 dbquery ( "DELETE FROM chanserv.channels WHERE ID= %u " , rcp
-> ID
);
167 dbquery ( "DELETE FROM chanserv.chanusers WHERE channelID= %u " , rcp
-> ID
);
168 dbquery ( "DELETE FROM chanserv.bans WHERE channelID= %u " , rcp
-> ID
);
171 void csdb_deleteuser ( reguser
* rup
) {
172 dbquery ( "DELETE FROM chanserv.users WHERE ID= %u " , rup
-> ID
);
173 dbquery ( "DELETE FROM chanserv.chanusers WHERE userID= %u " , rup
-> ID
);
176 void csdb_updateuser ( reguser
* rup
) {
177 char escpassword
[ 25 ];
179 char esclastuserhost
[ 160 ];
181 char esccomment
[ 510 ];
183 char esclastemail
[ 210 ];
185 dbescapestring ( escpassword
, rup
-> password
, strlen ( rup
-> password
));
188 dbescapestring ( escemail
, rup
-> email
-> content
, rup
-> email
-> length
);
193 dbescapestring ( esclastemail
, rup
-> lastemail
-> content
, rup
-> lastemail
-> length
);
195 esclastemail
[ 0 ]= '\0' ;
197 if ( rup
-> lastuserhost
)
198 dbescapestring ( esclastuserhost
, rup
-> lastuserhost
-> content
, rup
-> lastuserhost
-> length
);
200 esclastuserhost
[ 0 ]= '\0' ;
202 if ( rup
-> suspendreason
)
203 dbescapestring ( escreason
, rup
-> suspendreason
-> content
, rup
-> suspendreason
-> length
);
208 dbescapestring ( esccomment
, rup
-> comment
-> content
, rup
-> comment
-> length
);
213 dbescapestring ( escinfo
, rup
-> info
-> content
, rup
-> info
-> length
);
217 dbquery ( "UPDATE chanserv.users SET lastauth= %l u, lastemailchng= %l u, flags= %u ,"
218 "language= %u , suspendby= %u , suspendexp= %l u, suspendtime= %l u, lockuntil= %l u, password=' %s ', email=' %s ',"
219 "lastuserhost=' %s ', suspendreason=' %s ', comment=' %s ', info=' %s ', lastemail=' %s ', lastpasschng= %l u "
221 rup
-> lastauth
, rup
-> lastemailchange
, rup
-> flags
, rup
-> languageid
, rup
-> suspendby
, rup
-> suspendexp
,
222 rup
-> suspendtime
, rup
-> lockuntil
, escpassword
, escemail
, esclastuserhost
, escreason
, esccomment
, escinfo
, esclastemail
,
227 void csdb_createuser ( reguser
* rup
) {
228 char escpassword
[ 25 ];
230 char esclastuserhost
[ 160 ];
232 char esccomment
[ 510 ];
233 char escusername
[ 35 ];
235 char esclastemail
[ 210 ];
237 dbescapestring ( escusername
, rup
-> username
, strlen ( rup
-> username
));
238 dbescapestring ( escpassword
, rup
-> password
, strlen ( rup
-> password
));
241 dbescapestring ( escemail
, rup
-> email
-> content
, rup
-> email
-> length
);
246 dbescapestring ( esclastemail
, rup
-> lastemail
-> content
, rup
-> lastemail
-> length
);
248 esclastemail
[ 0 ]= '\0' ;
250 if ( rup
-> lastuserhost
)
251 dbescapestring ( esclastuserhost
, rup
-> lastuserhost
-> content
, rup
-> lastuserhost
-> length
);
253 esclastuserhost
[ 0 ]= '\0' ;
255 if ( rup
-> suspendreason
)
256 dbescapestring ( escreason
, rup
-> suspendreason
-> content
, rup
-> suspendreason
-> length
);
261 dbescapestring ( esccomment
, rup
-> comment
-> content
, rup
-> comment
-> length
);
266 dbescapestring ( escinfo
, rup
-> info
-> content
, rup
-> info
-> length
);
270 dbquery ( "INSERT INTO chanserv.users (ID, username, created, lastauth, lastemailchng, "
271 "flags, language, suspendby, suspendexp, suspendtime, lockuntil, password, email, lastuserhost, "
272 "suspendreason, comment, info, lastemail, lastpasschng)"
273 "VALUES ( %u ,' %s ', %l u, %l u, %l u, %u , %u , %u , %l u, %l u, %l u,' %s ',' %s ',' %s ',' %s ',' %s ',' %s ',' %s ', %l u)" ,
274 rup
-> ID
, escusername
, rup
-> created
, rup
-> lastauth
, rup
-> lastemailchange
, rup
-> flags
,
275 rup
-> languageid
, rup
-> suspendby
, rup
-> suspendexp
, rup
-> suspendtime
, rup
-> lockuntil
,
276 escpassword
, escemail
, esclastuserhost
, escreason
, esccomment
, escinfo
, esclastemail
,
277 rup
-> lastpasschange
);
281 void csdb_updatechanuser ( regchanuser
* rcup
) {
285 dbescapestring ( escinfo
, rcup
-> info
-> content
, rcup
-> info
-> length
);
289 dbquery ( "UPDATE chanserv.chanusers SET flags= %u , changetime= %l u, "
290 "usetime= %l u, info=' %s ' WHERE channelID= %u and userID= %u " ,
291 rcup
-> flags
, rcup
-> changetime
, rcup
-> usetime
, escinfo
, rcup
-> chan
-> ID
, rcup
-> user
-> ID
);
294 void csdb_createchanuser ( regchanuser
* rcup
) {
298 dbescapestring ( escinfo
, rcup
-> info
-> content
, rcup
-> info
-> length
);
302 dbquery ( "INSERT INTO chanserv.chanusers VALUES( %u , %u , %u , %l u, %l u, ' %s ')" ,
303 rcup
-> user
-> ID
, rcup
-> chan
-> ID
, rcup
-> flags
, rcup
-> changetime
,
304 rcup
-> usetime
, escinfo
);
307 void csdb_deletechanuser ( regchanuser
* rcup
) {
308 dbquery ( "DELETE FROM chanserv.chanusers WHERE channelid= %u AND userID= %u " ,
309 rcup
-> chan
-> ID
, rcup
-> user
-> ID
);
312 void csdb_createban ( regchan
* rcp
, regban
* rbp
) {
317 strcpy ( banstr
, bantostring ( rbp
-> cbp
));
318 dbescapestring ( escban
, banstr
, strlen ( banstr
));
321 dbescapestring ( escreason
, rbp
-> reason
-> content
, rbp
-> reason
-> length
);
325 dbquery ( "INSERT INTO chanserv.bans (banID, channelID, userID, hostmask, "
326 "expiry, reason) VALUES ( %u , %u , %u ,' %s ', %l u,' %s ')" , rbp
-> ID
, rcp
-> ID
,
327 rbp
-> setby
, escban
, rbp
-> expiry
, escreason
);
330 void csdb_updateban ( regchan
* rcp
, regban
* rbp
) {
335 strcpy ( banstr
, bantostring ( rbp
-> cbp
));
336 dbescapestring ( escban
, banstr
, strlen ( banstr
));
339 dbescapestring ( escreason
, rbp
-> reason
-> content
, rbp
-> reason
-> length
);
343 dbquery ( "UPDATE chanserv.bans set channelID= %u , userID= %u , hostmask=' %s ', expiry= %l u, reason=' %s ' "
344 "WHERE banID= %u " , rcp
-> ID
, rbp
-> setby
, escban
, rbp
-> expiry
, escreason
, rbp
-> ID
);
347 void csdb_deleteban ( regban
* rbp
) {
348 dbquery ( "DELETE FROM chanserv.bans WHERE banID= %u " , rbp
-> ID
);
351 void csdb_createmail ( reguser
* rup
, int type
) {
355 if ( type
== QMAIL_NEWEMAIL
) {
357 dbescapestring ( escemail
, rup
-> email
-> content
, rup
-> email
-> length
);
358 sprintf ( sqlquery
, "INSERT INTO chanserv.email (userID, emailType, prevEmail) "
359 "VALUES ( %u , %u ,' %s ')" , rup
-> ID
, type
, escemail
);
362 sprintf ( sqlquery
, "INSERT INTO chanserv.email (userID, emailType) VALUES ( %u , %u )" , rup
-> ID
, type
);
365 dbquery ( " %s " , sqlquery
);
368 void csdb_deletemaildomain ( maildomain
* mdp
) {
369 dbquery ( "DELETE FROM chanserv.maildomain WHERE ID= %u " , mdp
-> ID
);
372 void csdb_createmaildomain ( maildomain
* mdp
) {
374 dbescapestring ( escdomain
, mdp
-> name
-> content
, mdp
-> name
-> length
);
376 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
);
379 void csdb_updatemaildomain ( maildomain
* mdp
) {
381 dbescapestring ( escdomain
, mdp
-> name
-> content
, mdp
-> name
-> length
);
383 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
);
386 void csdb_chanlevhistory_insert ( regchan
* rcp
, nick
* np
, reguser
* trup
, flag_t oldflags
, flag_t newflags
) {
387 reguser
* rup
= getreguserfromnick ( np
);
389 dbquery ( "INSERT INTO chanserv.chanlevhistory (userID, channelID, targetID, changetime, authtime, "
390 "oldflags, newflags) VALUES ( %u , %u , %u , %l u, %l u, %u , %u )" , rup
-> ID
, rcp
-> ID
, trup
-> ID
, getnettime (), np
-> accountts
,
394 void csdb_accounthistory_insert ( nick
* np
, char * oldpass
, char * newpass
, char * oldemail
, char * newemail
) {
395 reguser
* rup
= getreguserfromnick ( np
);
396 char escoldpass
[ PASSLEN
* 2 + 5 ];
397 char escnewpass
[ PASSLEN
* 2 + 5 ];
398 char escoldemail
[ EMAILLEN
* 2 + 5 ];
399 char escnewemail
[ EMAILLEN
* 2 + 5 ];
401 if (! rup
|| UHasOperPriv ( rup
))
405 dbescapestring ( escoldpass
, oldpass
, CSMIN ( strlen ( oldpass
), PASSLEN
));
410 dbescapestring ( escnewpass
, newpass
, CSMIN ( strlen ( newpass
), PASSLEN
));
415 dbescapestring ( escoldemail
, oldemail
, CSMIN ( strlen ( oldemail
), EMAILLEN
));
419 dbescapestring ( escnewemail
, newemail
, CSMIN ( strlen ( newemail
), EMAILLEN
));
423 dbquery ( "INSERT INTO chanserv.accounthistory (userID, changetime, authtime, oldpassword, newpassword, oldemail, "
424 "newemail) VALUES ( %u , %l u, %l u, ' %s ', ' %s ', ' %s ', ' %s ')" , rup
-> ID
, getnettime (), np
-> accountts
, escoldpass
, escnewpass
,
425 escoldemail
, escnewemail
);
428 void csdb_cleanuphistories () {
429 time_t expire_time
= getnettime ()- 604800 ;
431 Error ( "chanserv" , ERR_INFO
, "Cleaning histories." );
432 dbquery ( "DELETE FROM chanserv.authhistory WHERE authtime < %l u" , expire_time
);
433 dbquery ( "DELETE FROM chanserv.chanlevhistory WHERE changetime < %l u" , expire_time
);
434 dbquery ( "DELETE FROM chanserv.accounthistory WHERE changetime < %l u" , expire_time
);
437 void csdb_deletemaillock ( maillock
* mlp
) {
438 dbquery ( "DELETE FROM chanserv.maillocks WHERE ID= %u " , mlp
-> id
);
441 void csdb_createmaillock ( maillock
* mlp
) {
442 char escpattern
[ 1024 ], escreason
[ 1024 ];
444 dbescapestring ( escpattern
, mlp
-> pattern
-> content
, mlp
-> pattern
-> length
);
447 dbescapestring ( escreason
, mlp
-> reason
-> content
, mlp
-> reason
-> length
);
451 dbquery ( "INSERT INTO chanserv.maillocks (id, pattern, reason, createdby, created) VALUES( %u , ' %s ', ' %s ', %u , %j d)" ,
452 mlp
-> id
, escpattern
, escreason
, mlp
-> createdby
,( intmax_t ) mlp
-> created
);
455 void csdb_updatemaillock ( maillock
* mlp
) {
456 char escpattern
[ 1024 ], escreason
[ 1024 ];
458 dbescapestring ( escpattern
, mlp
-> pattern
-> content
, mlp
-> pattern
-> length
);
461 dbescapestring ( escreason
, mlp
-> reason
-> content
, mlp
-> reason
-> length
);
465 dbquery ( "UPDATE chanserv.maillocks SET pattern=' %s ', reason=' %s ', createdby= %u , created= %j d WHERE ID= %u " , escpattern
, escreason
, mlp
-> createdby
, ( intmax_t ) mlp
-> created
, mlp
-> id
);