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