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