]>
Commit | Line | Data |
---|---|---|
c86edd1d Q |
1 | /* |
2 | * chanservuser.c: | |
3 | * This file maintains the functions associated with the | |
4 | * user on the network relating to the channel service | |
5 | */ | |
6 | ||
7 | #include "chanserv.h" | |
8 | ||
32562540 | 9 | #include "../core/hooks.h" |
c86edd1d Q |
10 | #include "../core/schedule.h" |
11 | #include "../core/config.h" | |
12 | #include "../localuser/localuser.h" | |
13 | #include "../localuser/localuserchannel.h" | |
14 | #include "../irc/irc_config.h" | |
15 | #include "../lib/sstring.h" | |
16 | #include "../nick/nick.h" | |
17 | #include "../parser/parser.h" | |
18 | #include "../lib/splitline.h" | |
b7a95f03 | 19 | #include "../lib/irc_string.h" |
c86edd1d Q |
20 | |
21 | #include <string.h> | |
22 | #include <stdio.h> | |
23 | #include <stdarg.h> | |
24 | #include <stdlib.h> | |
25 | ||
26 | nick *chanservnick; | |
27 | CommandTree *cscommands; | |
28 | CommandTree *csctcpcommands; | |
29 | ||
30 | /* Local prototypes */ | |
31 | void chanservuserhandler(nick *target, int message, void **params); | |
32 | ||
33 | void chanservreguser(void *arg) { | |
6c4eca7b | 34 | sstring *csnick,*csuser,*cshost,*csrealname,*csaccount; |
c86edd1d Q |
35 | chanindex *cip; |
36 | regchan *rcp; | |
37 | int i; | |
38 | ||
39 | csnick=getcopyconfigitem("chanserv","nick","Q",NICKLEN); | |
40 | csuser=getcopyconfigitem("chanserv","user","TheQBot",USERLEN); | |
41 | cshost=getcopyconfigitem("chanserv","host","some.host",HOSTLEN); | |
42 | csrealname=getcopyconfigitem("chanserv","realname","ChannelService",REALLEN); | |
6c4eca7b | 43 | csaccount=getcopyconfigitem("chanserv","account",csnick&&csnick->content&&csnick->content[0]?csnick->content:"Q",ACCOUNTLEN); |
c86edd1d | 44 | |
316959c1 | 45 | Error("chanserv",ERR_INFO,"Connecting %s...",csnick->content); |
46 | ||
c86edd1d | 47 | chanservnick=registerlocaluser(csnick->content,csuser->content,cshost->content, |
6c4eca7b CP |
48 | csrealname->content,csaccount->content, |
49 | UMODE_INV|UMODE_SERVICE|UMODE_DEAF|UMODE_OPER|UMODE_ACCOUNT, | |
c86edd1d Q |
50 | &chanservuserhandler); |
51 | ||
52 | freesstring(csnick); | |
53 | freesstring(csuser); | |
54 | freesstring(cshost); | |
55 | freesstring(csrealname); | |
6c4eca7b CP |
56 | freesstring(csaccount); |
57 | ||
c86edd1d Q |
58 | /* Now join channels */ |
59 | for (i=0;i<CHANNELHASHSIZE;i++) { | |
60 | for (cip=chantable[i];cip;cip=cip->next) { | |
61 | if (cip->channel && (rcp=cip->exts[chanservext]) && !CIsSuspended(rcp)) { | |
316959c1 | 62 | /* This will do timestamp faffing even if it won't actually join */ |
63 | chanservjoinchan(cip->channel); | |
c86edd1d Q |
64 | /* Do a check at some future time.. */ |
65 | cs_schedupdate(cip, 1, 5); | |
66 | rcp->status |= (QCSTAT_OPCHECK | QCSTAT_MODECHECK | QCSTAT_BANCHECK); | |
67 | if (CIsForceTopic(rcp) || CIsTopicSave(rcp)) | |
68 | localsettopic(chanservnick, cip->channel, (rcp->topic) ? rcp->topic->content : ""); | |
69 | } | |
70 | } | |
71 | } | |
72 | ||
316959c1 | 73 | Error("chanserv",ERR_INFO,"Loaded and joined channels."); |
74 | ||
c86edd1d Q |
75 | if (chanserv_init_status == CS_INIT_NOUSER) { |
76 | /* If this is the first time, perform last init tasks */ | |
77 | chanserv_finalinit(); | |
78 | } | |
79 | } | |
80 | ||
81 | void chanservuserhandler(nick *target, int message, void **params) { | |
82 | nick *sender; | |
83 | char *msg; | |
84 | Command *cmd; | |
85 | char *cargv[30]; | |
86 | int cargc; | |
87 | reguser *rup; | |
88 | char *chp; | |
89 | ||
90 | switch(message) { | |
91 | case LU_KILLED: | |
92 | scheduleoneshot(time(NULL),&chanservreguser,NULL); | |
93 | chanservnick=NULL; | |
94 | break; | |
95 | ||
96 | case LU_PRIVMSG: | |
97 | case LU_SECUREMSG: | |
98 | sender=(nick *)params[0]; | |
99 | msg=(char *)params[1]; | |
100 | ||
101 | if (!(cargc=splitline(msg,cargv,30,0))) | |
102 | return; /* Ignore blank lines */ | |
103 | ||
104 | if (cargv[0][0]==1) { | |
105 | /* CTCP */ | |
106 | for (chp=cargv[0]+1;*chp;chp++) { | |
107 | if (*chp=='\001') { | |
108 | *chp='\0'; | |
109 | break; | |
110 | } | |
111 | } | |
112 | cmd=findcommandintree(csctcpcommands, cargv[0]+1, 1); | |
113 | if (cmd) { | |
114 | rejoinline(cargv[1],cargc-1); | |
115 | cmd->handler((void *)sender, cargc-1, &(cargv[1])); | |
116 | } | |
117 | } else { | |
118 | cmd=findcommandintree(cscommands, cargv[0], 1); | |
119 | ||
120 | if (!cmd) { | |
121 | chanservstdmessage(sender, QM_UNKNOWNCMD, cargv[0]); | |
122 | break; | |
123 | } | |
124 | ||
125 | if ((cmd->level & QCMD_SECURE) && (message != LU_SECUREMSG)) { | |
126 | chanservstdmessage(sender, QM_SECUREONLY, cargv[0], chanservnick->nick, myserver->content); | |
127 | break; | |
128 | } | |
129 | ||
130 | if ((cmd->level & QCMD_AUTHED)) { | |
131 | if (!(rup=getreguserfromnick(sender))) { | |
132 | chanservstdmessage(sender, QM_AUTHEDONLY, cargv[0]); | |
133 | break; | |
134 | } | |
135 | if (UIsSuspended(rup) || (rup->status & QUSTAT_DEAD)) { | |
136 | chanservstdmessage(sender, QM_BADAUTH, cargv[0]); | |
137 | break; | |
138 | } | |
139 | } | |
140 | ||
141 | if ((cmd->level & QCMD_NOTAUTHED) && (getreguserfromnick(sender))) { | |
142 | chanservstdmessage(sender, QM_UNAUTHEDONLY, cargv[0]); | |
143 | break; | |
144 | } | |
145 | ||
146 | if ((cmd->level & QCMD_HELPER) && | |
147 | (!(rup=getreguserfromnick(sender)) || !UHasHelperPriv(rup))) { | |
148 | chanservstdmessage(sender, QM_NOACCESS, cargv[0]); | |
149 | break; | |
150 | } | |
151 | ||
152 | if ((cmd->level & QCMD_OPER) && | |
153 | (!IsOper(sender) || !(rup=getreguserfromnick(sender)) || !UHasOperPriv(rup))) { | |
154 | chanservstdmessage(sender, QM_NOACCESS, cargv[0]); | |
155 | break; | |
156 | } | |
157 | ||
158 | if ((cmd->level & QCMD_ADMIN) && | |
159 | (!IsOper(sender) || !(rup=getreguserfromnick(sender)) || !UHasAdminPriv(rup))) { | |
160 | chanservstdmessage(sender, QM_NOACCESS, cargv[0]); | |
161 | break; | |
162 | } | |
163 | ||
164 | if ((cmd->level & QCMD_DEV) && | |
165 | (!IsOper(sender) || !(rup=getreguserfromnick(sender)) || !UIsDev(rup))) { | |
166 | chanservstdmessage(sender, QM_NOACCESS, cargv[0]); | |
167 | break; | |
168 | } | |
169 | ||
170 | if (cmd->maxparams < (cargc-1)) { | |
171 | rejoinline(cargv[cmd->maxparams],cargc-(cmd->maxparams)); | |
172 | cargc=(cmd->maxparams)+1; | |
173 | } | |
174 | ||
175 | cmd->handler((void *)sender, cargc-1, &(cargv[1])); | |
176 | } | |
177 | break; | |
178 | ||
179 | default: | |
180 | break; | |
181 | } | |
182 | } | |
183 | ||
184 | void chanservcommandinit() { | |
185 | cscommands=newcommandtree(); | |
186 | csctcpcommands=newcommandtree(); | |
187 | } | |
188 | ||
189 | void chanservcommandclose() { | |
190 | destroycommandtree(cscommands); | |
191 | destroycommandtree(csctcpcommands); | |
192 | } | |
193 | ||
1f0d7c8b | 194 | void chanservaddcommand(char *command, int flags, int maxparams, CommandHandler handler, char *description, const char *help) { |
c86edd1d Q |
195 | Command *newcmd; |
196 | cmdsummary *summary; | |
197 | ||
198 | newcmd=addcommandtotree(cscommands, command, flags, maxparams, handler); | |
199 | /* Allocate a summary record to put the description in */ | |
200 | summary=(cmdsummary *)malloc(sizeof(cmdsummary)); | |
201 | memset((void *)summary,0,sizeof(cmdsummary)); | |
202 | ||
203 | summary->def=getsstring(description, 250); | |
9c27e757 | 204 | summary->defhelp=(char *)help; /* Assume that help is a constant */ |
1f0d7c8b | 205 | |
c86edd1d Q |
206 | newcmd->ext=(void *)summary; |
207 | loadcommandsummary(newcmd); | |
208 | } | |
209 | ||
210 | void chanservaddctcpcommand(char *command, CommandHandler handler) { | |
211 | addcommandtotree(csctcpcommands, command, 0, 1, handler); | |
212 | } | |
213 | ||
214 | void chanservremovectcpcommand(char *command, CommandHandler handler) { | |
215 | deletecommandfromtree(csctcpcommands, command, handler); | |
216 | } | |
217 | ||
218 | void chanservremovecommand(char *command, CommandHandler handler) { | |
219 | Command *cmd; | |
220 | cmdsummary *summary; | |
221 | int i; | |
222 | ||
223 | if (!(cmd=findcommandintree(cscommands, command, 1))) { | |
224 | Error("chanserv",ERR_WARNING,"Tried to unregister unknown command %s",command); | |
225 | return; | |
226 | } | |
227 | ||
228 | summary=(cmdsummary *)cmd->ext; | |
229 | freesstring(summary->def); | |
230 | for (i=0;i<MAXLANG;i++) { | |
231 | if (summary->bylang[i]) | |
232 | freesstring(summary->bylang[i]); | |
233 | } | |
234 | ||
235 | free(summary); | |
236 | ||
237 | deletecommandfromtree(cscommands, command, handler); | |
238 | } | |
239 | ||
240 | void chanservjoinchan(channel *cp) { | |
241 | regchan *rcp; | |
2df6316a | 242 | unsigned int i; |
243 | nick *np; | |
244 | reguser *rup; | |
245 | regchanuser *rcup; | |
09cb349b | 246 | flag_t themodes = 0; |
af87643b | 247 | |
a54e438d | 248 | /* Skip unregistered channels */ |
249 | if (!(rcp=cp->index->exts[chanservext])) | |
250 | return; | |
251 | ||
0a53499c | 252 | /* Check for a new timestamp. But don't do anything stupid if the incoming timestamp is 0 */ |
253 | if (cp->timestamp && ((!rcp->ltimestamp) || (cp->timestamp < rcp->ltimestamp))) { | |
a54e438d | 254 | rcp->ltimestamp=cp->timestamp; |
255 | csdb_updatechanneltimestamp(rcp); | |
256 | } | |
c86edd1d | 257 | |
a54e438d | 258 | /* We won't be doing any joining or parting if we're not online */ |
259 | if (!chanservnick) | |
c86edd1d Q |
260 | return; |
261 | ||
262 | if ((CIsSuspended(rcp) || !CIsJoined(rcp)) && getnumerichandlefromchanhash(cp->users, chanservnick->numeric)) { | |
334b567e P |
263 | if(rcp->suspendreason) { |
264 | localpartchannel(chanservnick, cp, rcp->suspendreason->content); | |
265 | } else { | |
266 | localpartchannel(chanservnick, cp, NULL); | |
267 | } | |
c86edd1d | 268 | } |
2df6316a | 269 | |
270 | /* Right, we are definately going to either join the channel or at least | |
271 | * burst some modes onto it. | |
272 | * | |
a54e438d | 273 | * We will try and burst our view of the world; if the timestamps are |
274 | * actually equal this will be mostly ignored and we will have to fix it | |
af87643b | 275 | * up later. For modes we use the forced modes, plus the default channel |
276 | * modes (unless any of those are explicitly denied) */ | |
af87643b | 277 | |
43583524 | 278 | /* By default, we set the forcemodes and the default modes, but never denymodes.. |
279 | * OK, actually we should only set the default modes if our timestamp is older.*/ | |
280 | if (cp->timestamp > rcp->ltimestamp) | |
281 | themodes |= CHANMODE_DEFAULT; | |
282 | else | |
283 | themodes=0; | |
284 | ||
285 | themodes = (themodes | rcp->forcemodes) & ~rcp->denymodes; | |
2df6316a | 286 | |
287 | /* Now, if someone has just created a channel and we are going to set +i | |
288 | * or +k on it, this will kick them off. This could be construed as a | |
289 | * bit rude if it's their channel, so if there is only one person on the | |
290 | * channel and they have a right to be there, burst with default modes | |
291 | * only to avoid them being netrider kicked. | |
292 | */ | |
293 | if (cp->users->totalusers==1) { | |
294 | for (i=0;i<cp->users->hashsize;i++) { | |
295 | if (cp->users->content[i] != nouser) { | |
296 | if ((np=getnickbynumeric(cp->users->content[i]&CU_NUMERICMASK)) && | |
297 | (rup=getreguserfromnick(np)) && | |
298 | (rcup=findreguseronchannel(rcp,rup)) && | |
299 | CUKnown(rcup)) { | |
300 | /* OK, there was one user, and they are known on this channel. | |
301 | * Don't burst with +i or +k */ | |
302 | themodes &= ~(CHANMODE_INVITEONLY | CHANMODE_KEY); | |
af87643b | 303 | } |
304 | } | |
305 | } | |
2df6316a | 306 | } |
307 | ||
308 | /* We should be on the channel - join with our nick */ | |
309 | if (!CIsSuspended(rcp) && CIsJoined(rcp) && !getnumerichandlefromchanhash(cp->users, chanservnick->numeric)) { | |
af87643b | 310 | /* If we pass the key parameter here but are not setting +k (see above) |
311 | * then localburstontochannel() will ignore the key */ | |
312 | localburstontochannel(cp, chanservnick, rcp->ltimestamp, themodes, | |
313 | rcp->limit, (rcp->key)?rcp->key->content:NULL); | |
c86edd1d | 314 | } |
2df6316a | 315 | |
316 | /* We're not joining the channel - send the burst with no nick */ | |
b263aa79 | 317 | if (!CIsSuspended(rcp) && !CIsJoined(rcp) && (cp->timestamp > rcp->ltimestamp)) { |
2df6316a | 318 | localburstontochannel(cp, NULL, rcp->ltimestamp, themodes, |
319 | rcp->limit, (rcp->key)?rcp->key->content:NULL); | |
b263aa79 | 320 | } |
c86edd1d Q |
321 | } |
322 | ||
323 | void chanservstdmessage(nick *np, int messageid, ... ) { | |
324 | char buf[5010]; | |
325 | char buf2[512]; | |
326 | int notice; | |
327 | reguser *rup; | |
328 | int language; | |
dd258305 | 329 | va_list va, va2; |
b7ed259b | 330 | char *message, *messageargs; |
c86edd1d Q |
331 | char *bp2,*bp; |
332 | int len; | |
333 | ||
334 | if (getreguserfromnick(np) == NULL) { | |
335 | notice=1; | |
336 | language=0; | |
337 | } else { | |
338 | rup=getreguserfromnick(np); | |
339 | if(UIsNotice(rup)) { | |
340 | notice=1; | |
341 | } else { | |
342 | notice=0; | |
343 | } | |
344 | language=rup->languageid; | |
345 | } | |
346 | ||
347 | if (csmessages[language][messageid]) { | |
348 | message=csmessages[language][messageid]->content; | |
349 | } else if (csmessages[0][messageid]) { | |
350 | message=csmessages[0][messageid]->content; | |
351 | } else { | |
b7ed259b | 352 | message=defaultmessages[messageid*2]; |
c86edd1d Q |
353 | } |
354 | ||
b7ed259b CP |
355 | messageargs=defaultmessages[messageid*2+1]; |
356 | ||
c86edd1d | 357 | va_start(va,messageid); |
dd258305 | 358 | va_copy(va2, va); |
b7ed259b | 359 | q9vsnprintf(buf,5000,message,messageargs,va); |
c86edd1d Q |
360 | va_end(va); |
361 | ||
362 | len=0; | |
363 | bp2=buf2; | |
364 | for (bp=buf; ;bp++) { | |
365 | /* We send something if we hit a \n, fill the buffer or run out of source */ | |
366 | if (*bp=='\n' || len>490 || !*bp) { | |
367 | if (*bp && *bp!='\n') { | |
368 | bp--; | |
369 | } | |
370 | ||
371 | *bp2='\0'; | |
372 | ||
373 | if (chanservnick && *buf2) { | |
374 | if (notice) { | |
375 | sendnoticetouser(chanservnick,np,"%s",buf2); | |
376 | } else { | |
377 | sendmessagetouser(chanservnick,np,"%s",buf2); | |
378 | } | |
379 | } | |
380 | ||
381 | /* If we ran out of buffer, get out here */ | |
382 | if (!*bp) | |
383 | break; | |
384 | ||
385 | bp2=buf2; | |
386 | len=0; | |
387 | } else { | |
388 | len++; | |
389 | *bp2++=*bp; | |
390 | } | |
391 | } | |
dd258305 | 392 | |
393 | /* Special case: If it's a "not enough parameters" message, show the first line of help */ | |
394 | if (messageid==QM_NOTENOUGHPARAMS) { | |
395 | char *command=va_arg(va2, char *); | |
396 | cs_sendhelp(np, command, 1); | |
397 | chanservstdmessage(np, QM_TYPEHELPFORHELP, command); | |
398 | } | |
399 | ||
400 | va_end(va2); | |
c86edd1d Q |
401 | } |
402 | ||
dd258305 | 403 | void chanservsendmessage_real(nick *np, int oneline, char *message, ... ) { |
c86edd1d Q |
404 | char buf[5010]; /* Very large buffer.. */ |
405 | char buf2[512], *bp, *bp2; | |
406 | int notice; | |
407 | int len; | |
408 | reguser *rup; | |
409 | ||
410 | va_list va; | |
411 | ||
412 | va_start(va,message); | |
413 | vsnprintf(buf,5000,message,va); | |
414 | va_end(va); | |
415 | ||
416 | if (getreguserfromnick(np) == NULL) { | |
417 | notice=1; | |
418 | } else { | |
419 | rup=getreguserfromnick(np); | |
420 | if(UIsNotice(rup)) { | |
421 | notice=1; | |
422 | } else { | |
423 | notice=0; | |
424 | } | |
425 | } | |
426 | ||
427 | len=0; | |
428 | bp2=buf2; | |
429 | for (bp=buf; ;bp++) { | |
430 | /* We send something if we hit a \n, fill the buffer or run out of source */ | |
431 | if (*bp=='\n' || len>490 || !*bp) { | |
432 | if (*bp && *bp!='\n') { | |
433 | bp--; | |
434 | } | |
435 | ||
436 | *bp2='\0'; | |
437 | ||
438 | if (chanservnick && *buf2) { | |
439 | if (notice) { | |
440 | sendnoticetouser(chanservnick,np,"%s",buf2); | |
441 | } else { | |
442 | sendmessagetouser(chanservnick,np,"%s",buf2); | |
443 | } | |
444 | } | |
445 | ||
446 | /* If we ran out of buffer, get out here */ | |
dd258305 | 447 | if (!*bp || (*bp=='\n' && oneline)) |
c86edd1d Q |
448 | break; |
449 | ||
450 | bp2=buf2; | |
451 | len=0; | |
452 | } else { | |
453 | len++; | |
454 | *bp2++=*bp; | |
455 | } | |
456 | } | |
457 | } | |
458 | ||
459 | void chanservwallmessage(char *message, ... ) { | |
460 | char buf[5010]; /* Very large buffer.. */ | |
461 | va_list va; | |
462 | nick *np; | |
463 | unsigned int i=0; | |
464 | ||
465 | va_start(va,message); | |
466 | vsnprintf(buf,5000,message,va); | |
467 | va_end(va); | |
468 | ||
469 | /* Scan for users */ | |
470 | for (i=0;i<NICKHASHSIZE;i++) | |
471 | for (np=nicktable[i];np;np=np->next) | |
472 | if (IsOper(np)) | |
473 | chanservsendmessage(np, "%s", buf); | |
474 | } | |
475 | ||
476 | void chanservkillstdmessage(nick *target, int messageid, ... ) { | |
477 | char buf[512]; | |
478 | int language; | |
b7ed259b | 479 | char *message, *messageargs; |
c86edd1d Q |
480 | va_list va; |
481 | reguser *rup; | |
482 | ||
483 | if (!(rup=getreguserfromnick(target))) | |
484 | language=0; | |
485 | else | |
486 | language=rup->languageid; | |
487 | ||
488 | if (csmessages[language][messageid]) | |
489 | message=csmessages[language][messageid]->content; | |
490 | else if (csmessages[0][messageid]) | |
491 | message=csmessages[0][messageid]->content; | |
492 | else | |
b7ed259b | 493 | message=defaultmessages[messageid*2]; |
c86edd1d | 494 | |
b7ed259b | 495 | messageargs=defaultmessages[messageid*2+1]; |
c86edd1d | 496 | va_start(va, messageid); |
b7ed259b | 497 | q9vsnprintf(buf, 511, message, messageargs, va); |
c86edd1d Q |
498 | va_end(va); |
499 | killuser(chanservnick, target, buf); | |
500 | } | |
501 | ||
c86edd1d Q |
502 | int checkpassword(reguser *rup, const char *pass) { |
503 | if (!strncmp(rup->password, pass, PASSLEN)) | |
504 | return 1; | |
505 | return 0; | |
506 | } | |
507 | ||
b7a95f03 CP |
508 | int checkresponse(reguser *rup, const unsigned char *entropy, const char *response, CRAlgorithm algorithm) { |
509 | char usernamel[NICKLEN+1], *dp, *up; | |
510 | ||
511 | for(up=rup->username,dp=usernamel;*up;) | |
512 | *dp++ = ToLower(*up++); | |
513 | *dp = '\0'; | |
514 | ||
515 | return algorithm(usernamel, rup->password, cs_calcchallenge(entropy), response); | |
516 | } | |
517 | ||
721cc8ce CP |
518 | int checkhashpass(reguser *rup, const char *junk, const char *hash) { |
519 | char usernamel[NICKLEN+1], *dp, *up; | |
520 | ||
521 | for(up=rup->username,dp=usernamel;*up;) | |
522 | *dp++ = ToLower(*up++); | |
523 | *dp = '\0'; | |
524 | ||
525 | return cs_checkhashpass(usernamel, rup->password, junk, hash); | |
526 | } | |
527 | ||
c86edd1d Q |
528 | int setpassword(reguser *rup, const char *pass) { |
529 | strncpy(rup->password, pass, PASSLEN); | |
530 | rup->password[PASSLEN]='\0'; | |
531 | return 1; | |
532 | } | |
533 | ||
534 | void cs_checknick(nick *np) { | |
535 | activeuser* aup; | |
536 | reguser *rup; | |
c86edd1d Q |
537 | char userhost[USERLEN+HOSTLEN+3]; |
538 | ||
539 | if (!(aup=getactiveuserfromnick(np))) { | |
540 | aup=getactiveuser(); | |
541 | np->exts[chanservnext]=aup; | |
542 | } | |
543 | ||
544 | assert(getactiveuserfromnick(np)); | |
545 | ||
23b85e10 | 546 | if (IsAccount(np) && np->auth) { |
6e3ceea8 | 547 | if (np->auth->exts[chanservaext]) { |
4b1a43ea | 548 | rup=getreguserfromnick(np); |
c0587316 | 549 | |
4b1a43ea | 550 | /* safe? */ |
c0587316 CP |
551 | if(rup) { |
552 | if (UIsDelayedGline(rup)) { | |
553 | /* delayed-gline - schedule the user's squelching */ | |
554 | deleteschedule(NULL, &chanservdgline, (void*)rup); /* icky, but necessary unless we stick more stuff in reguser structure */ | |
555 | scheduleoneshot(time(NULL)+rand()%900, &chanservdgline, (void*)rup); | |
556 | } else if (UIsGline(rup)) { | |
557 | /* instant-gline - lets be lazy and set a schedule expiring now :) */ | |
558 | deleteschedule(NULL, &chanservdgline, (void*)rup); /* icky, but necessary unless we stick more stuff in reguser structure */ | |
559 | scheduleoneshot(time(NULL), &chanservdgline, (void*)rup); | |
560 | } else if(UHasSuspension(rup)) { | |
561 | chanservkillstdmessage(np, QM_SUSPENDKILL); | |
562 | return; | |
563 | } | |
4b1a43ea | 564 | } |
c86edd1d Q |
565 | cs_doallautomodes(np); |
566 | } else { | |
c86edd1d Q |
567 | /* Auto create user.. */ |
568 | rup=getreguser(); | |
569 | rup->status=0; | |
23b85e10 | 570 | rup->ID=np->auth->userid; |
571 | if (rup->ID > lastuserID) | |
572 | lastuserID=rup->ID; | |
c86edd1d Q |
573 | strncpy(rup->username,np->authname,NICKLEN); rup->username[NICKLEN]='\0'; |
574 | rup->created=time(NULL); | |
7d27d0cd | 575 | rup->lastauth=0; |
c86edd1d Q |
576 | rup->lastemailchange=0; |
577 | rup->flags=QUFLAG_NOTICE; | |
578 | rup->languageid=0; | |
579 | rup->suspendby=0; | |
fa350b40 | 580 | rup->suspendexp=0; |
88d191e3 | 581 | rup->suspendtime=0; |
e086bfee | 582 | rup->lockuntil=0; |
c86edd1d | 583 | rup->password[0]='\0'; |
c86edd1d | 584 | rup->email=NULL; |
9708f78f | 585 | rup->lastemail=NULL; |
e3805f60 P |
586 | rup->localpart=NULL; |
587 | rup->domain=NULL; | |
c86edd1d Q |
588 | rup->info=NULL; |
589 | sprintf(userhost,"%s@%s",np->ident,np->host->name->content); | |
590 | rup->lastuserhost=getsstring(userhost,USERLEN+HOSTLEN+1); | |
591 | rup->suspendreason=NULL; | |
592 | rup->comment=NULL; | |
593 | rup->knownon=NULL; | |
594 | rup->checkshd=NULL; | |
595 | rup->stealcount=0; | |
596 | rup->fakeuser=NULL; | |
c86edd1d Q |
597 | addregusertohash(rup); |
598 | ||
599 | csdb_createuser(rup); | |
c86edd1d | 600 | } |
c86edd1d Q |
601 | } |
602 | ||
603 | cs_checknickbans(np); | |
604 | } | |
605 | ||
606 | void cs_checkchanmodes(channel *cp) { | |
607 | modechanges changes; | |
608 | ||
609 | localsetmodeinit (&changes, cp, chanservnick); | |
610 | cs_docheckchanmodes(cp, &changes); | |
611 | localsetmodeflush (&changes, 1); | |
612 | } | |
613 | ||
614 | void cs_docheckchanmodes(channel *cp, modechanges *changes) { | |
615 | regchan *rcp; | |
616 | ||
617 | if (!cp) | |
618 | return; | |
619 | ||
620 | if ((rcp=cp->index->exts[chanservext])==NULL || CIsSuspended(rcp)) | |
621 | return; | |
622 | ||
623 | /* Take care of the simple modes */ | |
624 | localdosetmode_simple(changes, rcp->forcemodes & ~(cp->flags), rcp->denymodes & cp->flags); | |
625 | ||
626 | /* Check for forced key. Note that we ALWAYS call localdosetmode_key | |
627 | * in case the wrong key is set atm */ | |
628 | if (rcp->forcemodes & CHANMODE_KEY) { | |
629 | if (!rcp->key) { | |
630 | Error("chanserv",ERR_WARNING,"Key forced but no key specified for %s: cleared forcemode.",rcp->index->name->content); | |
631 | rcp->forcemodes &= ~CHANMODE_KEY; | |
a54e438d | 632 | csdb_updatechannel(rcp); |
c86edd1d Q |
633 | } else { |
634 | localdosetmode_key(changes, rcp->key->content, MCB_ADD); | |
635 | } | |
636 | } | |
637 | ||
638 | /* Check for denied key */ | |
639 | if ((rcp->denymodes & CHANMODE_KEY) && IsKey(cp)) { | |
640 | localdosetmode_key(changes, NULL, MCB_DEL); | |
641 | } | |
642 | ||
643 | /* Check for forced limit. Always call in case of wrong limit */ | |
644 | if (rcp->forcemodes & CHANMODE_LIMIT) { | |
645 | localdosetmode_limit(changes, rcp->limit, MCB_ADD); | |
646 | } | |
647 | } | |
648 | ||
5cde8af0 | 649 | /* Slightly misnamed function that checks each USER on the channel against channel |
650 | * settings. Possible actions are opping/deopping, voicing/devoicing, and banning | |
651 | * for chanflag +k or chanlev flag +b */ | |
c86edd1d Q |
652 | void cs_docheckopvoice(channel *cp, modechanges *changes) { |
653 | regchan *rcp; | |
654 | reguser *rup; | |
655 | regchanuser *rcup; | |
656 | nick *np; | |
657 | int i; | |
658 | ||
659 | if (!cp) | |
660 | return; | |
661 | ||
662 | if ((rcp=cp->index->exts[chanservext])==NULL || CIsSuspended(rcp)) | |
663 | return; | |
664 | ||
665 | for (i=0;i<cp->users->hashsize;i++) { | |
666 | if (cp->users->content[i]==nouser) | |
667 | continue; | |
668 | ||
669 | if ((np=getnickbynumeric(cp->users->content[i]))==NULL) { | |
670 | Error("chanserv",ERR_ERROR,"Found non-existent numeric %d on channel %s",cp->users->content[i], | |
671 | cp->index->name->content); | |
672 | continue; | |
673 | } | |
674 | ||
675 | if ((rup=getreguserfromnick(np))) | |
676 | rcup=findreguseronchannel(rcp,rup); | |
677 | else | |
678 | rcup=NULL; | |
679 | ||
680 | if (rcup && CUIsBanned(rcup) && !IsService(np)) { | |
681 | cs_banuser(changes, cp->index, np, NULL); | |
682 | continue; | |
683 | } | |
684 | ||
5cde8af0 | 685 | /* chanflag +k checks; kick them if they "obviously" can't rejoin without a ban */ |
c86edd1d | 686 | if (!IsService(np) && CIsKnownOnly(rcp) && !(rcup && CUKnown(rcup))) { |
0556d281 | 687 | if (IsInviteOnly(cp) || (IsRegOnly(cp) && !IsAccount(np))) { |
5cde8af0 | 688 | localkickuser(chanservnick,cp,np,"Authorised users only."); |
689 | } else { | |
690 | cs_banuser(NULL, cp->index, np, "Authorised users only."); | |
691 | } | |
c86edd1d Q |
692 | continue; |
693 | } | |
5cde8af0 | 694 | |
c86edd1d Q |
695 | if ((cp->users->content[i] & CUMODE_OP) && !IsService(np)) { |
696 | if ((CIsBitch(rcp) && (!rcup || !CUHasOpPriv(rcup))) || | |
697 | (rcup && CUIsDeny(rcup))) | |
698 | localdosetmode_nick(changes, np, MC_DEOP); | |
699 | } else { | |
1482fb78 | 700 | if (rcup && (CUIsProtect(rcup) || CIsProtect(rcp)) && CUIsOp(rcup) && !CUIsDeny(rcup)) { |
c86edd1d | 701 | localdosetmode_nick(changes, np, MC_OP); |
1482fb78 | 702 | cs_logchanop(rcp, np->nick, rcup->user); |
703 | } | |
c86edd1d Q |
704 | } |
705 | ||
706 | if (cp->users->content[i] & CUMODE_VOICE) { | |
707 | if (rcup && CUIsQuiet(rcup)) | |
708 | localdosetmode_nick(changes, np, MC_DEVOICE); | |
709 | } else { | |
710 | if (rcup && (CUIsProtect(rcup) || CIsProtect(rcp)) && CUIsVoice(rcup) && !CUIsQuiet(rcup) && !(cp->users->content[i] & CUMODE_OP)) | |
711 | localdosetmode_nick(changes, np, MC_VOICE); | |
712 | } | |
713 | } | |
714 | } | |
715 | ||
716 | void cs_doallautomodes(nick *np) { | |
717 | reguser *rup; | |
718 | regchanuser *rcup; | |
719 | unsigned long *lp; | |
720 | modechanges changes; | |
721 | ||
722 | if (!(rup=getreguserfromnick(np))) | |
723 | return; | |
724 | ||
725 | for (rcup=rup->knownon;rcup;rcup=rcup->nextbyuser) { | |
726 | if (rcup->chan->index->channel) { | |
727 | /* Channel exists */ | |
728 | if ((lp=getnumerichandlefromchanhash(rcup->chan->index->channel->users, np->numeric))) { | |
db804373 P |
729 | /* User is on channel.. */ |
730 | ||
731 | /* Update last use time */ | |
732 | rcup->usetime=getnettime(); | |
733 | ||
c86edd1d Q |
734 | localsetmodeinit(&changes, rcup->chan->index->channel, chanservnick); |
735 | if (*lp & CUMODE_OP) { | |
736 | if (!IsService(np) && (CUIsDeny(rcup) || (CIsBitch(rcup->chan) && !CUHasOpPriv(rcup)))) | |
737 | localdosetmode_nick(&changes, np, MC_DEOP); | |
738 | } else { | |
739 | if (!CUIsDeny(rcup) && CUIsOp(rcup) && | |
1482fb78 | 740 | (CUIsProtect(rcup) || CIsProtect(rcup->chan) || CUIsAutoOp(rcup))) { |
c86edd1d | 741 | localdosetmode_nick(&changes, np, MC_OP); |
1482fb78 | 742 | cs_logchanop(rcup->chan, np->nick, rup); |
743 | } | |
c86edd1d Q |
744 | } |
745 | ||
746 | if (*lp & CUMODE_VOICE) { | |
747 | if (CUIsQuiet(rcup)) | |
748 | localdosetmode_nick(&changes, np, MC_DEVOICE); | |
749 | } else { | |
750 | if (!CUIsQuiet(rcup) && CUIsVoice(rcup) && !(*lp & CUMODE_OP) && | |
751 | (CUIsProtect(rcup) || CIsProtect(rcup->chan) || CUIsAutoVoice(rcup))) | |
752 | localdosetmode_nick(&changes, np, MC_VOICE); | |
753 | } | |
754 | localsetmodeflush(&changes, 1); | |
755 | } else { | |
756 | /* Channel exists but user is not joined: invite if they are +j-b */ | |
757 | if (CUIsAutoInvite(rcup) && CUKnown(rcup) && !CUIsBanned(rcup)) { | |
758 | localinvite(chanservnick, rcup->chan->index->channel, np); | |
759 | } | |
760 | } | |
761 | } | |
762 | } | |
763 | } | |
764 | ||
765 | void cs_checknickbans(nick *np) { | |
766 | channel **ca; | |
767 | regchan *rcp; | |
768 | int i,j; | |
769 | ||
770 | if (IsService(np)) | |
771 | return; | |
772 | ||
773 | /* Avoid races: memcpy the channel array */ | |
774 | i=np->channels->cursi; | |
775 | ca=(channel **)malloc(i*sizeof(channel *)); | |
776 | memcpy(ca, np->channels->content,i*sizeof(channel *)); | |
777 | ||
778 | for (j=0;j<i;j++) { | |
779 | if ((rcp=ca[j]->index->exts[chanservext]) && !CIsSuspended(rcp) && | |
5cde8af0 | 780 | CIsEnforce(rcp) && nickbanned_visible(np, ca[j])) |
c86edd1d Q |
781 | localkickuser(chanservnick, ca[j], np, "Banned."); |
782 | } | |
783 | ||
784 | free(ca); | |
785 | } | |
786 | ||
787 | void cs_checkbans(channel *cp) { | |
788 | regchan *rcp; | |
789 | int i; | |
790 | nick *np; | |
791 | chanban *cbp; | |
792 | regban *rbp; | |
793 | time_t now; | |
794 | modechanges changes; | |
795 | ||
796 | if (!cp) | |
797 | return; | |
798 | ||
799 | if ((rcp=cp->index->exts[chanservext])==NULL || CIsSuspended(rcp)) | |
800 | return; | |
801 | ||
802 | now=time(NULL); | |
803 | ||
804 | localsetmodeinit(&changes, cp, chanservnick); | |
805 | ||
806 | for (i=0;i<cp->users->hashsize;i++) { | |
807 | if (cp->users->content[i]==nouser) | |
808 | continue; | |
809 | ||
810 | if ((np=getnickbynumeric(cp->users->content[i]))==NULL) { | |
811 | Error("chanserv",ERR_ERROR,"Found numeric %d on channel %s who doesn't exist.", | |
812 | cp->users->content[i], cp->index->name->content); | |
813 | continue; | |
814 | } | |
815 | ||
816 | if (IsService(np)) | |
817 | continue; | |
818 | ||
819 | for (rbp=rcp->bans;rbp;rbp=rbp->next) { | |
820 | if (((!rbp->expiry) || (rbp->expiry <= now)) && | |
10771120 | 821 | nickmatchban_visible(np, rbp->cbp)) { |
822 | if (!nickbanned_visible(np, cp)) { | |
c86edd1d Q |
823 | localdosetmode_ban(&changes, bantostring(rbp->cbp), MCB_ADD); |
824 | } | |
825 | localkickuser(chanservnick,cp,np,rbp->reason?rbp->reason->content:"Banned."); | |
826 | break; | |
827 | } | |
828 | } | |
829 | ||
830 | if (rbp) /* If we broke out of above loop (due to kick) rbp will be set.. */ | |
831 | continue; | |
832 | ||
833 | if (CIsEnforce(rcp)) { | |
834 | for (cbp=cp->bans;cbp;cbp=cbp->next) { | |
10771120 | 835 | if ((cbp->timeset>=rcp->lastbancheck) && nickmatchban_visible(np, cbp)) |
c86edd1d Q |
836 | localkickuser(chanservnick,cp,np,"Banned."); |
837 | } | |
838 | rcp->lastbancheck=time(NULL); | |
839 | } | |
840 | } | |
841 | ||
842 | localsetmodeflush(&changes,1); | |
843 | } | |
844 | ||
845 | /* | |
846 | * cs_schedupdate: | |
847 | * This function schedules an update check on a channel | |
848 | */ | |
849 | void cs_schedupdate(chanindex *cip, int mintime, int maxtime) { | |
850 | int delay=mintime+(rand()%(maxtime-mintime)); | |
851 | regchan *rcp; | |
852 | ||
853 | if (!(rcp=cip->exts[chanservext]) || CIsSuspended(rcp)) | |
854 | return; | |
855 | ||
856 | if (rcp->checksched) { | |
857 | deleteschedule(rcp->checksched, cs_timerfunc, cip); | |
858 | } | |
859 | ||
860 | rcp->checksched=scheduleoneshot(time(NULL)+delay, cs_timerfunc, cip); | |
861 | } | |
862 | ||
863 | /* | |
864 | * cs_timerfunc: | |
865 | * This function does several things: | |
866 | * * Updates auto-limit and/or removes bans as necessary | |
867 | * * Schedules the next timed call | |
868 | * * Does any pending op/ban/mode checks | |
869 | * It is safe to use either as a target for a schedule() call, or to call | |
870 | * directly when parameters change (e.g. autolimit settings or ban expire | |
871 | * timers). | |
872 | * To force a limit update, set rcp->limit to 0.. | |
873 | */ | |
874 | ||
875 | void cs_timerfunc(void *arg) { | |
876 | chanindex *cip=arg; | |
877 | channel *cp=cip->channel; | |
878 | regchan *rcp; | |
879 | chanban *cbp, *ncbp; | |
880 | regban **rbh, *rbp; | |
881 | time_t nextsched=0; | |
882 | time_t now=time(NULL); | |
883 | modechanges changes; | |
884 | ||
885 | if (!(rcp=cip->exts[chanservext])) | |
886 | return; | |
887 | ||
888 | verifyregchan(rcp); | |
889 | ||
890 | /* Always nullify the checksched even if the channel is suspended.. */ | |
891 | if (rcp->checksched) { | |
892 | deleteschedule(rcp->checksched, cs_timerfunc, arg); | |
893 | rcp->checksched=NULL; | |
894 | } | |
895 | ||
896 | if (!cp || CIsSuspended(rcp)) | |
897 | return; | |
898 | ||
899 | /* Check if we need to leave the channel first */ | |
900 | if (chanservnick && CIsJoined(rcp) && cip->channel && | |
901 | cip->channel->users->totalusers==1 && | |
902 | getnumerichandlefromchanhash(cip->channel->users, chanservnick->numeric)) { | |
903 | ||
904 | /* Only chanserv left in this channel */ | |
905 | if (now >= (rcp->lastpart + LINGERTIME)) { | |
906 | /* Time to go */ | |
334b567e | 907 | localpartchannel(chanservnick, cip->channel, "Empty Channel"); |
c86edd1d Q |
908 | return; |
909 | } else { | |
910 | if (!nextsched || nextsched > (rcp->lastpart + LINGERTIME)) | |
911 | nextsched=rcp->lastpart+LINGERTIME; | |
912 | } | |
913 | } | |
914 | ||
915 | localsetmodeinit(&changes, cp, chanservnick); | |
916 | ||
917 | if (CIsAutoLimit(rcp)) { | |
918 | if (!rcp->limit || rcp->autoupdate <= now) { | |
cbdccc66 | 919 | /* Only update the limit if there are <= (autolimit/2) or |
920 | * >= (autolimit * 1.5) slots free */ | |
921 | ||
922 | if ((cp->users->totalusers >= (rcp->limit - rcp->autolimit/2)) || | |
923 | (cp->users->totalusers <= (rcp->limit - (3 * rcp->autolimit)/2))) { | |
924 | rcp->limit=cp->users->totalusers+rcp->autolimit; | |
925 | rcp->status |= QCSTAT_MODECHECK; | |
926 | } | |
c86edd1d Q |
927 | |
928 | /* And set the schedule for the next update */ | |
929 | rcp->autoupdate = now + AUTOLIMIT_INTERVAL; | |
930 | } | |
931 | ||
932 | if (!nextsched || rcp->autoupdate <= nextsched) | |
933 | nextsched=rcp->autoupdate; | |
934 | } | |
935 | ||
936 | if (rcp->status & QCSTAT_OPCHECK) { | |
937 | rcp->status &= ~QCSTAT_OPCHECK; | |
938 | cs_docheckopvoice(cp, &changes); | |
939 | } | |
940 | ||
941 | if (rcp->status & QCSTAT_MODECHECK) { | |
942 | rcp->status &= ~QCSTAT_MODECHECK; | |
943 | cs_docheckchanmodes(cp, &changes); | |
944 | } | |
945 | ||
946 | if (rcp->status & QCSTAT_BANCHECK) { | |
947 | rcp->status &= ~QCSTAT_BANCHECK; | |
948 | cs_checkbans(cp); | |
949 | } | |
950 | ||
951 | /* This ban check is AFTER docheckopvoice in case checkopvoice set a | |
952 | * ban which we need to automatically remove later.. */ | |
953 | ||
954 | if (rcp->banduration) { | |
955 | for (cbp=cp->bans;cbp;cbp=ncbp) { | |
956 | ncbp=cbp->next; | |
957 | if (cbp->timeset+rcp->banduration<=now) { | |
958 | localdosetmode_ban(&changes, bantostring(cbp), MCB_DEL); | |
959 | } else { | |
960 | if (!nextsched || cbp->timeset+rcp->banduration <= nextsched) { | |
961 | nextsched=rcp->banduration+cbp->timeset; | |
962 | } | |
963 | } | |
964 | } | |
965 | } | |
966 | ||
967 | /* Check for expiry of registered bans */ | |
968 | if (rcp->bans) { | |
969 | for (rbh=&(rcp->bans);*rbh;) { | |
970 | rbp=*rbh; | |
971 | verifyregchanban(rbp); | |
972 | if (rbp->expiry && (rbp->expiry < now)) { | |
973 | /* Expired ban */ | |
974 | /* Remove ban if it's on the channel (localdosetmode_ban will silently | |
975 | * skip bans that don't exist) */ | |
976 | localdosetmode_ban(&changes, bantostring(rbp->cbp), MCB_DEL); | |
977 | /* Remove from database */ | |
978 | csdb_deleteban(rbp); | |
979 | /* Remove from list */ | |
980 | (*rbh)=rbp->next; | |
981 | /* Free ban/string and update setby refcount, and free actual regban */ | |
982 | freesstring(rbp->reason); | |
983 | freechanban(rbp->cbp); | |
984 | freeregban(rbp); | |
985 | } else { | |
986 | if (rbp->expiry && (!nextsched || rbp->expiry <= nextsched)) { | |
987 | nextsched=rbp->expiry; | |
988 | } | |
989 | rbh=&(rbp->next); | |
990 | } | |
991 | } | |
992 | } | |
993 | ||
994 | if (nextsched) | |
995 | rcp->checksched=scheduleoneshot(nextsched, cs_timerfunc, arg); | |
996 | ||
997 | localsetmodeflush(&changes, 1); | |
998 | } | |
999 | ||
1000 | void cs_removechannel(regchan *rcp) { | |
1001 | int i; | |
1002 | chanindex *cip; | |
1003 | regchanuser *rcup, *nrcup; | |
1004 | regban *rbp, *nrbp; | |
1005 | ||
1006 | cip=rcp->index; | |
1007 | ||
1008 | for (i=0;i<REGCHANUSERHASHSIZE;i++) { | |
1009 | for (rcup=rcp->regusers[i];rcup;rcup=nrcup) { | |
1010 | nrcup=rcup->nextbychan; | |
1011 | delreguserfromchannel(rcp, rcup->user); | |
1012 | freeregchanuser(rcup); | |
1013 | } | |
1014 | } | |
1015 | ||
1016 | for (rbp=rcp->bans;rbp;rbp=nrbp) { | |
1017 | nrbp=rbp->next; | |
1018 | freesstring(rbp->reason); | |
1019 | freechanban(rbp->cbp); | |
1020 | freeregban(rbp); | |
1021 | } | |
1022 | ||
1023 | rcp->bans=NULL; | |
1024 | ||
1025 | if (rcp->checksched) | |
1026 | deleteschedule(rcp->checksched, cs_timerfunc, rcp->index); | |
1027 | ||
1028 | if (cip->channel) { | |
1029 | rcp->flags=QCFLAG_SUSPENDED; | |
1030 | chanservjoinchan(cip->channel); /* Force off the channel */ | |
1031 | } | |
1032 | ||
1033 | csdb_deletechannel(rcp); | |
1034 | ||
1035 | freesstring(rcp->welcome); | |
1036 | freesstring(rcp->topic); | |
1037 | freesstring(rcp->key); | |
1038 | freesstring(rcp->suspendreason); | |
1039 | freesstring(rcp->comment); | |
1040 | ||
1041 | freeregchan(rcp); | |
1042 | ||
1043 | cip->exts[chanservext]=NULL; | |
1044 | releasechanindex(cip); | |
1045 | } | |
1046 | ||
5f7881b2 | 1047 | /* Sender is who the DELCHAN is attributed to.. */ |
1048 | int cs_removechannelifempty(nick *sender, regchan *rcp) { | |
1049 | unsigned int i; | |
1050 | regchanuser *rcup; | |
1051 | ||
1052 | for (i=0;i<REGCHANUSERHASHSIZE;i++) { | |
1053 | for (rcup=rcp->regusers[i];rcup;rcup=rcup->nextbychan) { | |
50ec21f3 | 1054 | if (rcup->flags & ~(QCUFLAGS_PUNISH)) |
5f7881b2 | 1055 | return 0; |
1056 | } | |
1057 | } | |
1058 | ||
8ba555be | 1059 | cs_log(sender,"DELCHAN %s (Empty)",rcp->index->name->content); |
1060 | cs_removechannel(rcp); | |
5f7881b2 | 1061 | |
1062 | return 1; | |
1063 | } | |
1064 | ||
c86edd1d | 1065 | void cs_removeuser(reguser *rup) { |
c86edd1d Q |
1066 | regchanuser *rcup, *nrcup; |
1067 | regchan *rcp; | |
23b85e10 | 1068 | struct authname *anp; |
c86edd1d Q |
1069 | |
1070 | /* Remove the user from all its channels */ | |
1071 | for (rcup=rup->knownon;rcup;rcup=nrcup) { | |
1072 | nrcup=rcup->nextbyuser; | |
c86edd1d Q |
1073 | rcp=rcup->chan; |
1074 | ||
1075 | delreguserfromchannel(rcp, rup); | |
5f7881b2 | 1076 | cs_removechannelifempty(NULL, rcp); |
c86edd1d Q |
1077 | } |
1078 | ||
e3805f60 P |
1079 | if(rup->domain) |
1080 | delreguserfrommaildomain(rup,rup->domain); | |
1081 | freesstring(rup->localpart); | |
c86edd1d | 1082 | freesstring(rup->email); |
9708f78f | 1083 | freesstring(rup->lastemail); |
c86edd1d Q |
1084 | freesstring(rup->lastuserhost); |
1085 | freesstring(rup->suspendreason); | |
1086 | freesstring(rup->comment); | |
1087 | freesstring(rup->info); | |
1088 | ||
1089 | csdb_deleteuser(rup); | |
1090 | ||
1091 | removereguserfromhash(rup); | |
1092 | ||
23b85e10 | 1093 | if ((anp=findauthname(rup->ID)) && anp->nicks) { |
c86edd1d Q |
1094 | rup->status |= QUSTAT_DEAD; |
1095 | } else { | |
1096 | freereguser(rup); | |
1097 | } | |
1098 | } | |
1099 | ||
1100 | int cs_bancheck(nick *np, channel *cp) { | |
1101 | time_t now=time(NULL); | |
1102 | regban **rbh, *rbp; | |
1103 | modechanges changes; | |
1104 | regchan *rcp; | |
1105 | ||
1106 | if (!(rcp=cp->index->exts[chanservext])) | |
1107 | return 0; | |
1108 | ||
1109 | for (rbh=&(rcp->bans);*rbh;) { | |
1110 | if ((*rbh)->expiry && ((*rbh)->expiry < now)) { | |
1111 | /* Expired ban */ | |
1112 | csdb_deleteban(*rbh); | |
1113 | rbp=*rbh; | |
1114 | (*rbh)=rbp->next; | |
1115 | ||
1116 | freesstring(rbp->reason); | |
1117 | freechanban(rbp->cbp); | |
1118 | freeregban(rbp); | |
10771120 | 1119 | } else if (nickmatchban_visible(np,(*rbh)->cbp)) { |
c86edd1d | 1120 | /* This user matches this ban.. */ |
5cde8af0 | 1121 | if (!nickbanned_visible(np,cp)) { |
c86edd1d Q |
1122 | /* Only bother putting the ban on the channel if they're not banned already */ |
1123 | /* (might be covered by this ban or a different one.. doesn't really matter */ | |
1124 | localsetmodeinit(&changes, cp, chanservnick); | |
1125 | localdosetmode_ban(&changes, bantostring((*rbh)->cbp), MCB_ADD); | |
1126 | localsetmodeflush(&changes, 1); | |
1127 | cs_timerfunc(cp->index); | |
1128 | } | |
1129 | localkickuser(chanservnick, cp, np, (*rbh)->reason ? (*rbh)->reason->content : "Banned."); | |
1130 | return 1; | |
1131 | } else { | |
1132 | rbh=&((*rbh)->next); | |
1133 | } | |
1134 | } | |
1135 | ||
1136 | return 0; | |
1137 | } | |
1138 | ||
1139 | void cs_setregban(chanindex *cip, regban *rbp) { | |
1140 | modechanges changes; | |
1141 | int i; | |
1142 | nick *np; | |
1143 | ||
1144 | if (!cip->channel) | |
1145 | return; | |
b263aa79 | 1146 | |
c86edd1d Q |
1147 | localsetmodeinit(&changes, cip->channel, chanservnick); |
1148 | localdosetmode_ban(&changes, bantostring(rbp->cbp), MCB_ADD); | |
1149 | localsetmodeflush(&changes, 1); | |
1150 | ||
1151 | for (i=0;(cip->channel) && i<cip->channel->users->hashsize;i++) { | |
1152 | if (cip->channel->users->content[i]!=nouser && | |
1153 | (np=getnickbynumeric(cip->channel->users->content[i])) && | |
1154 | !IsService(np) && | |
10771120 | 1155 | nickmatchban_visible(np, rbp->cbp)) |
c86edd1d Q |
1156 | localkickuser(chanservnick, cip->channel, np, rbp->reason ? rbp->reason->content : "Banned."); |
1157 | } | |
1158 | ||
1159 | cs_timerfunc(cip); | |
1160 | } | |
1161 | ||
1162 | void cs_banuser(modechanges *changes, chanindex *cip, nick *np, const char *reason) { | |
1163 | modechanges lchanges; | |
1164 | char banmask[NICKLEN+USERLEN+HOSTLEN+3]; | |
1165 | ||
1166 | if (!cip->channel) | |
1167 | return; | |
1168 | ||
5cde8af0 | 1169 | if (nickbanned_visible(np, cip->channel)) { |
c86edd1d Q |
1170 | localkickuser(chanservnick, cip->channel, np, reason?reason:"Banned."); |
1171 | return; | |
1172 | } | |
1173 | ||
1174 | if (IsAccount(np)) { | |
1175 | /* Ban their account.. */ | |
1176 | sprintf(banmask,"*!*@%s.%s",np->authname,HIS_HIDDENHOST); | |
1177 | } else if (IsSetHost(np)) { | |
1178 | /* sethosted: ban ident@host */ | |
1179 | sprintf(banmask,"*!%s@%s",np->shident->content,np->sethost->content); | |
1180 | } else if (np->host->clonecount>3) { | |
1181 | /* >3 clones, ban user@host */ | |
1182 | sprintf(banmask,"*!%s@%s",np->ident,np->host->name->content); | |
1183 | } else { | |
1184 | sprintf(banmask,"*!*@%s",np->host->name->content); | |
1185 | } | |
1186 | ||
1187 | if (!changes) { | |
1188 | localsetmodeinit(&lchanges, cip->channel, chanservnick); | |
1189 | localdosetmode_ban(&lchanges, banmask, MCB_ADD); | |
1190 | localsetmodeflush(&lchanges, 1); | |
1191 | } else { | |
1192 | localdosetmode_ban(changes, banmask, MCB_ADD); | |
1193 | } | |
1194 | ||
1195 | localkickuser(chanservnick, cip->channel, np, reason?reason:"Banned."); | |
1196 | } | |
95332d7b | 1197 | |
1198 | /* | |
1199 | * cs_sanitisechanlev: Removes impossible combinations from chanlev flags. | |
1200 | * chanservuser.c is probably not the right file for this, but nowhere better | |
1201 | * presented itself... | |
1202 | */ | |
1203 | flag_t cs_sanitisechanlev(flag_t flags) { | |
1204 | /* +m or +n cannot have any "punishment" flags */ | |
1205 | if (flags & (QCUFLAG_MASTER | QCUFLAG_OWNER)) | |
50ec21f3 | 1206 | flags &= ~(QCUFLAGS_PUNISH); |
95332d7b | 1207 | |
1208 | /* +d can't be +o */ | |
1209 | if (flags & QCUFLAG_DENY) | |
1210 | flags &= ~QCUFLAG_OP; | |
1211 | ||
1212 | /* +q can't be +v */ | |
1213 | if (flags & QCUFLAG_QUIET) | |
1214 | flags &= ~QCUFLAG_VOICE; | |
1215 | ||
1216 | /* +p trumps +a and +g */ | |
1217 | if (flags & QCUFLAG_PROTECT) | |
1218 | flags &= ~(QCUFLAG_AUTOOP | QCUFLAG_AUTOVOICE); | |
1219 | ||
1220 | /* -o can't be +a */ | |
1221 | if (!(flags & QCUFLAG_OP)) | |
1222 | flags &= ~QCUFLAG_AUTOOP; | |
1223 | ||
1224 | /* +a or -v can't be +g. +a implies +o at this stage (see above) */ | |
1225 | if (!(flags & QCUFLAG_VOICE) || (flags & QCUFLAG_AUTOOP)) | |
1226 | flags &= ~QCUFLAG_AUTOVOICE; | |
1227 | ||
1228 | /* +p requires +o or +v */ | |
1229 | if (!(flags & (QCUFLAG_VOICE | QCUFLAG_OP))) | |
1230 | flags &= ~QCUFLAG_PROTECT; | |
1231 | ||
e8e09b86 | 1232 | /* The personal flags require one of +mnovk, as does +t */ |
95332d7b | 1233 | if (!(flags & (QCUFLAG_OWNER | QCUFLAG_MASTER | QCUFLAG_OP | QCUFLAG_VOICE | QCUFLAG_KNOWN))) |
e8e09b86 | 1234 | flags &= ~(QCUFLAGS_PERSONAL | QCUFLAG_TOPIC); |
95332d7b | 1235 | |
1236 | return flags; | |
1237 | } | |
c86edd1d | 1238 | |
b263aa79 | 1239 | /* |
1240 | * findreguser: | |
1241 | * This function does the standard "nick or #user" lookup. | |
1242 | * If "sender" is not NULL, a suitable error message will | |
1243 | * be sent if the lookup fails. | |
4630ab32 CP |
1244 | * "sender" MUST be sent when a user is requesting a lookup |
1245 | * as there is some policy code here. | |
b263aa79 | 1246 | */ |
1247 | ||
1248 | reguser *findreguser(nick *sender, const char *str) { | |
4630ab32 | 1249 | reguser *rup, *vrup = getreguserfromnick(sender);; |
b263aa79 | 1250 | nick *np; |
1251 | ||
1252 | if (!str || !*str) | |
1253 | return NULL; | |
1254 | ||
1255 | if (*str=='#') { | |
1256 | if (str[1]=='\0') { | |
1257 | if (sender) | |
1258 | chanservstdmessage(sender, QM_UNKNOWNUSER, str); | |
1259 | return NULL; | |
1260 | } | |
1261 | if (!(rup=findreguserbynick(str+1)) && sender) | |
1262 | chanservstdmessage(sender, QM_UNKNOWNUSER, str); | |
4630ab32 CP |
1263 | } else if (*str=='&' && vrup && UHasHelperPriv(vrup)) { |
1264 | if (str[1]=='\0') { | |
1265 | if (sender) | |
1266 | chanservstdmessage(sender, QM_UNKNOWNUSER, str); | |
1267 | return NULL; | |
1268 | } | |
1269 | if (!(rup=findreguserbyID(atoi(str+1))) && sender) | |
1270 | chanservstdmessage(sender, QM_UNKNOWNUSER, str); | |
b263aa79 | 1271 | } else { |
1272 | if (!(np=getnickbynick(str))) { | |
1273 | if (sender) | |
1274 | chanservstdmessage(sender, QM_UNKNOWNUSER, str); | |
1275 | return NULL; | |
1276 | } | |
1277 | if (!(rup=getreguserfromnick(np)) && sender) | |
1278 | chanservstdmessage(sender, QM_USERNOTAUTHED, str); | |
1279 | } | |
1280 | ||
4002da60 CP |
1281 | /* removed the suspended check from here, I don't see the point... */ |
1282 | if (rup && (rup->status & QUSTAT_DEAD)) { | |
b263aa79 | 1283 | chanservstdmessage(sender, QM_USERHASBADAUTH, rup->username); |
1284 | return NULL; | |
1285 | } | |
1286 | ||
1287 | return rup; | |
1288 | } | |
336765cd CP |
1289 | |
1290 | /* | |
1291 | * Unbans a mask from a channel, including permbans if user has correct privs. | |
0a53499c | 1292 | * |
1293 | * Return 0 if it works, 1 if it don't. | |
336765cd | 1294 | */ |
0a53499c | 1295 | int cs_unbanfn(nick *sender, chanindex *cip, UnbanFN fn, void *arg, int removepermbans, int abortonfailure) { |
336765cd CP |
1296 | regban **rbh, *rbp; |
1297 | chanban **cbh, *cbp; | |
1298 | regchan *rcp; | |
1299 | modechanges changes; | |
1300 | char *banstr; | |
1301 | ||
1302 | rcp=cip->exts[chanservext]; | |
1303 | ||
1304 | if (cip->channel) | |
1305 | localsetmodeinit(&changes, cip->channel, chanservnick); | |
1306 | ||
1307 | for (rbh=&(rcp->bans); *rbh; ) { | |
1308 | rbp=*rbh; | |
1309 | if (fn(arg, rbp->cbp)) { | |
1310 | banstr=bantostring(rbp->cbp); | |
1311 | /* Check perms and remove */ | |
1312 | if(!removepermbans) { | |
1313 | chanservstdmessage(sender, QM_WARNNOTREMOVEDPERMBAN, banstr, cip->name->content); | |
1314 | rbh=&(rbp->next); | |
1315 | } else if (!cs_checkaccess(sender, NULL, CA_MASTERPRIV, cip, NULL, 0, 1)) { | |
1316 | chanservstdmessage(sender, QM_NOTREMOVEDPERMBAN, banstr, cip->name->content); | |
0a53499c | 1317 | if (abortonfailure) return 1; /* Just give up... */ |
336765cd CP |
1318 | rbh=&(rbp->next); |
1319 | } else { | |
1320 | chanservstdmessage(sender, QM_REMOVEDPERMBAN, banstr, cip->name->content); | |
1321 | if (cip->channel) | |
1322 | localdosetmode_ban(&changes, banstr, MCB_DEL); | |
1323 | /* Remove from database */ | |
1324 | csdb_deleteban(rbp); | |
1325 | /* Remove from list */ | |
1326 | (*rbh)=rbp->next; | |
1327 | /* Free ban/string and update setby refcount, and free actual regban */ | |
1328 | freesstring(rbp->reason); | |
1329 | freechanban(rbp->cbp); | |
1330 | freeregban(rbp); | |
1331 | } | |
1332 | } else { | |
1333 | rbh=&(rbp->next); | |
1334 | } | |
1335 | } | |
1336 | ||
1337 | if (cip->channel) { | |
1338 | for (cbh=&(cip->channel->bans); *cbh; ) { | |
1339 | cbp=*cbh; | |
1340 | if (fn(arg, cbp)) { | |
1341 | /* Remove */ | |
1342 | banstr=bantostring(cbp); | |
1343 | chanservstdmessage(sender, QM_REMOVEDCHANBAN, banstr, cip->name->content); | |
1344 | localdosetmode_ban(&changes, banstr, MCB_DEL); | |
1345 | } else { | |
1346 | cbh=&(cbp->next); | |
1347 | } | |
1348 | } | |
1349 | localsetmodeflush(&changes,1); | |
1350 | } | |
0a53499c | 1351 | |
1352 | return 0; | |
336765cd | 1353 | } |
1482fb78 | 1354 | |
1355 | /* Add entry to channel op history when someone gets ops. */ | |
1356 | void cs_logchanop(regchan *rcp, char *nick, reguser *rup) { | |
1357 | strncpy(rcp->chanopnicks[rcp->chanoppos], nick, NICKLEN); | |
1358 | rcp->chanopnicks[rcp->chanoppos][NICKLEN]='\0'; | |
1359 | rcp->chanopaccts[rcp->chanoppos]=rup->ID; | |
1360 | rcp->chanoppos=(rcp->chanoppos+1)%CHANOPHISTORY; | |
1361 | } |