]> jfr.im git - irc/quakenet/newserv.git/blame - channel/channelhandlers.c
Compiler warning fixes
[irc/quakenet/newserv.git] / channel / channelhandlers.c
CommitLineData
c86edd1d
Q
1/* channel.c */
2
f2b36aa8
CP
3#include <stdio.h>
4#include <string.h>
5
c86edd1d
Q
6#include "channel.h"
7#include "../server/server.h"
8#include "../nick/nick.h"
9#include "../lib/irc_string.h"
10#include "../irc/irc_config.h"
11#include "../parser/parser.h"
12#include "../irc/irc.h"
13#include "../lib/base64.h"
f2b36aa8 14#include "../lib/strlfunc.h"
c86edd1d
Q
15
16int handleburstmsg(void *source, int cargc, char **cargv) {
17 channel *cp;
18 time_t timestamp;
19 int wipeout=0;
20 int i;
21 int arg=0;
22 char *charp;
23 int newlimit;
24 int waslimit,waskeyed;
25 char *nextnum;
26 unsigned long currentmode;
27 int isnewchan;
28
29 /* (we don't see the first 2 params in cargc) */
30 /* AK B #+lod+ 1017561154 +tnk eits ATJWu:o,AiW1a,Ag3lV,AiWnl,AE6oI :%*!@D577A90D.kabel.telenet.be */
31
32 if (cargc<2) {
33 Error("channel",ERR_WARNING,"Burst message with only %d parameters",cargc);
34 return CMD_OK;
35 }
36
37 timestamp=strtol(cargv[1],NULL,10);
38
39 if ((cp=findchannel(cargv[0]))==NULL) {
40 /* We don't have this channel already */
41 cp=createchannel(cargv[0]);
42 cp->timestamp=timestamp;
43 isnewchan=1;
44 } else {
45 isnewchan=0;
46 if (timestamp<cp->timestamp) {
47 /* The incoming timestamp is older. Erase all our current channel modes, and the topic. */
48 cp->timestamp=timestamp;
49 freesstring(cp->topic);
50 cp->topic=NULL;
51 cp->topictime=0;
52 freesstring(cp->key);
53 cp->key=NULL;
54 cp->limit=0;
55 cp->flags=0;
56 clearallbans(cp);
57 /* Remove all +v, +o we currently have */
58 for(i=0;i<cp->users->hashsize;i++) {
59 if (cp->users->content[i]!=nouser) {
60 cp->users->content[i]&=CU_NUMERICMASK;
61 }
62 }
63 } else if (timestamp>cp->timestamp) {
64 /* The incoming timestamp is greater. Ignore any incoming modes they may happen to set */
65 wipeout=1;
66 }
67 }
68
69 /* OK, dealt with the name and timestamp.
70 * Loop over the remaining args */
71 for (arg=2;arg<cargc;arg++) {
72 if (cargv[arg][0]=='+') {
73 /* Channel modes */
74 if (wipeout) {
75 /* We ignore the modes, but we need to see if they include +l or +k
76 * so that we can ignore their corresponding values */
77 for (charp=cargv[arg];*charp;charp++) {
78 if (*charp=='k' || *charp=='l') {
79 arg++;
80 }
81 }
82 } else {
83 /* Clear off the limit and key flags before calling setflags so we can see if the burst tried to set them */
84 /* If the burst doesn't set them, we restore them afterwards */
85 waslimit=IsLimit(cp); ClearLimit(cp);
86 waskeyed=IsKey(cp); ClearKey(cp);
87 /* We can then use the flag function for these modes */
88 setflags(&(cp->flags),CHANMODE_ALL,cargv[arg],cmodeflags,REJECT_NONE);
89 /* Pick up the limit and key, if they were set. Note that the limit comes first */
90 if (IsLimit(cp)) { /* A limit was SET by the burst */
91 if (++arg>=cargc) {
92 /* Ran out of args -- damn ircd is spewing out crap again */
93 Error("channel",ERR_WARNING,"Burst +l with no argument");
94 break; /* "break" being the operative word */
95 } else {
96 newlimit=strtol(cargv[arg],NULL,10);
97 }
98 if (cp->limit>0 && waslimit) {
99 /* We had a limit before -- we now have the lowest one of the two */
100 if (newlimit<cp->limit) {
101 cp->limit=newlimit;
102 }
103 } else {
104 /* No limit before -- we just have the new one */
105 cp->limit=newlimit;
106 }
107 } else if (waslimit) {
108 SetLimit(cp); /* We had a limit before, but the burst didn't set one. Restore flag. */
109 }
110
111 if (IsKey(cp)) { /* A key was SET by the burst */
112 if (++arg>=cargc) {
113 /* Ran out of args -- oopsie! */
114 Error("channel",ERR_WARNING,"Burst +k with no argument");
115 break;
116 }
117 if (waskeyed) {
118 /* We had a key before -- alphabetically first wins */
119 if (ircd_strcmp(cargv[arg],cp->key->content)<0) {
120 /* Replace our key */
121 freesstring(cp->key);
122 cp->key=getsstring(cargv[arg],KEYLEN);
123 }
124 } else {
125 /* No key before -- just the new one */
126 cp->key=getsstring(cargv[arg],KEYLEN);
127 }
128 } else if (waskeyed) {
129 SetKey(cp); /* We had a key before, but the burst didn't set one. Restore flag. */
130 }
131 }
132 } else if (cargv[arg][0]=='%') {
133 /* We have one or more bans here */
134 nextnum=cargv[arg]+1;
135 while (*nextnum) {
136 /* Split off the next ban */
137 for (charp=nextnum;*charp;charp++) {
138 if (*charp==' ') {
139 *charp='\0';
140 charp++;
141 break;
142 }
143 }
144 setban(cp,nextnum);
145 nextnum=charp;
146 }
147 } else {
148 /* List of numerics */
149 nextnum=charp=cargv[arg];
150 currentmode=0;
151 while (*nextnum!='\0') {
152 /* Step over the next numeric */
153 for (i=0;i<5;i++) {
154 if (*charp++=='\0')
155 break;
156 }
157 if (i<5) {
158 break;
159 }
160 if (*charp==',') {
161 *charp='\0';
162 charp++;
163 } else if (*charp==':') {
164 *charp='\0';
165 charp++;
166 currentmode=0;
167 /* Look for modes */
168 for (;*charp;charp++) {
169 if (*charp=='v') {
170 currentmode|=CUMODE_VOICE;
171 } else if (*charp=='o') {
172 currentmode|=CUMODE_OP;
173 } else if (*charp==',') {
174 charp++;
175 break;
176 }
177 }
178 /* If we're ignore incoming modes, zap it to zero again */
179 if (wipeout) {
180 currentmode=0;
181 }
182 }
183 /* OK. At this point charp points to either '\0' if we're at the end,
184 * or the start of the next numeric otherwise. nextnum points at a valid numeric
185 * we need to add, and currentmode reflects the correct mode */
186 addnicktochannel(cp,(numerictolong(nextnum,5)|currentmode));
187 nextnum=charp;
188 }
189 }
190 }
191 if (cp->users->totalusers==0) {
192 /* Oh dear, the channel is now empty. Perhaps one of those
193 * charming empty burst messages you get sometimes.. */
194 if (!isnewchan) {
195 /* I really don't think this can happen, can it..? */
196 /* Only send the LOSTCHANNEL if the channel existed before */
197 triggerhook(HOOK_CHANNEL_LOSTCHANNEL,cp);
198 }
199 delchannel(cp);
200 } else {
201 /* If this is a new channel, we do the NEWCHANNEL hook also */
202 if (isnewchan) {
203 triggerhook(HOOK_CHANNEL_NEWCHANNEL,cp);
204 }
205 /* Just one hook to say "something happened to this channel" */
206 triggerhook(HOOK_CHANNEL_BURST,cp);
207 }
208
209 return CMD_OK;
210}
211
212int handlejoinmsg(void *source, int cargc, char **cargv) {
213 char *pos,*nextchan;
214 nick *np;
215 void *harg[2];
216 channel *cp,**ch;
217 long timestamp=0;
218 int i;
219 int newchan=0;
220
221 if (cargc<1) {
222 return CMD_OK;
223 }
224
225 if (cargc>1) {
226 /* We must have received a timestamp too */
227 timestamp=strtol(cargv[1],NULL,10);
228 }
229
230 /* Find out who we are talking about here */
231 np=getnickbynumericstr(source);
232 if (np==NULL) {
233 Error("channel",ERR_WARNING,"Channel join from non existent user %s",source);
234 return CMD_OK;
235 }
236
237 nextchan=pos=cargv[0];
238 while (*nextchan!='\0') {
239 /* Find the next chan position */
240 for (;*pos!='\0' && *pos!=',';pos++)
241 ; /* Empty loop */
242
243 if (*pos==',') {
244 *pos='\0';
245 pos++;
246 }
247
248 /* OK, pos now points at either null or the next chan
249 * and nextchan now points at the channel name we want to parse next */
250
251 if(nextchan[0]=='0' && nextchan[1]=='\0') {
252 /* Leave all channels
253 * We do this as if they were leaving the network,
254 * then vape their channels array and start again */
255 ch=(channel **)(np->channels->content);
256 for(i=0;i<np->channels->cursi;i++) {
257 /* Send hook */
258 harg[0]=ch[i];
259 harg[1]=np;
260 triggerhook(HOOK_CHANNEL_PART,harg);
261 delnickfromchannel(ch[i],np->numeric,0);
262 }
263 array_free(np->channels);
264 array_init(np->channels,sizeof(channel *));
265 } else {
266 /* It's an actual channel join */
267 newchan=0;
268 if ((cp=findchannel(nextchan))==NULL) {
269 /* User joined non-existent channel - create it. Note that createchannel automatically
270 * puts the right magic timestamp in for us */
271 Error("channel",ERR_DEBUG,"User %s joined non existent channel %s.",np->nick,nextchan);
272 cp=createchannel(nextchan);
273 newchan=1;
274 }
275 if (cp->timestamp==MAGIC_REMOTE_JOIN_TS && timestamp) {
276 /* No valid timestamp on the chan and we received one -- set */
277 cp->timestamp=timestamp;
278 }
279 /* OK, this is slightly inefficient since we turn the nick * into a numeric,
280 * and addnicktochannel then converts it back again. BUT it's fewer lines of code :) */
281 if (addnicktochannel(cp,np->numeric)) {
282 /* The user wasn't added */
283 if (newchan) {
284 delchannel(cp);
285 }
286 } else {
287 /* If we just created a channel, flag it */
288 if (newchan) {
289 triggerhook(HOOK_CHANNEL_NEWCHANNEL,cp);
290 }
291
292 /* send hook */
293 harg[0]=cp;
294 harg[1]=np;
295 triggerhook(HOOK_CHANNEL_JOIN,harg);
296 }
297 }
298 nextchan=pos;
299 }
300 return CMD_OK;
301}
302
303int handlecreatemsg(void *source, int cargc, char **cargv) {
304 char *pos,*nextchan;
305 nick *np;
306 channel *cp;
307 long timestamp=0;
308 int newchan=1;
309 void *harg[2];
310
311 if (cargc<2) {
312 return CMD_OK;
313 }
314
315 timestamp=strtol(cargv[1],NULL,10);
316
317 /* Find out who we are talking about here */
318 np=getnickbynumericstr(source);
319 if (np==NULL) {
320 Error("channel",ERR_WARNING,"Channel create from non existent user %s",source);
321 return CMD_OK;
322 }
323
324 nextchan=pos=cargv[0];
325 while (*nextchan!='\0') {
326 /* Find the next chan position */
327 for (;*pos!='\0' && *pos!=',';pos++)
328 ; /* Empty loop */
329
330 if (*pos==',') {
331 *pos='\0';
332 pos++;
333 }
334
335 /* OK, pos now points at either null or the next chan
336 * and nextchan now points at the channel name we want to parse next */
337
338 /* It's a channel create */
339 if ((cp=findchannel(nextchan))==NULL) {
340 /* This is the expected case -- the channel didn't exist before */
341 cp=createchannel(nextchan);
342 cp->timestamp=timestamp;
343 newchan=1;
344 } else {
345 Error("channel",ERR_DEBUG,"Received CREATE for already existing channel %s",cp->index->name->content);
346 if (cp->timestamp==MAGIC_REMOTE_JOIN_TS && timestamp) {
347 /* No valid timestamp on the chan and we received one -- set */
348 cp->timestamp=timestamp;
349 }
350 newchan=0;
351 }
352 /* Add the user to the channel, preopped */
353 if (addnicktochannel(cp,(np->numeric)|CUMODE_OP)) {
354 if (newchan) {
355 delchannel(cp);
356 }
357 } else {
358 /* Flag the channel as new if necessary */
359 if (newchan) {
360 triggerhook(HOOK_CHANNEL_NEWCHANNEL,cp);
361 }
362
363 /* Trigger hook */
364 harg[0]=cp;
365 harg[1]=np;
366 triggerhook(HOOK_CHANNEL_CREATE,harg);
367 }
368 nextchan=pos;
369 }
370
371 return CMD_OK;
372}
373
374int handlepartmsg(void *source, int cargc, char **cargv) {
375 char *pos,*nextchan;
376 nick *np;
377 channel *cp;
378 void *harg[3];
379
380 if (cargc<1) {
381 return CMD_OK;
382 }
383
384 if (cargc>2) {
385 Error("channel",ERR_WARNING,"PART with too many parameters (%d)",cargc);
386 }
387
388 if (cargc>1) {
389 harg[2]=(void *)cargv[1];
390 } else {
391 harg[2]=(void *)"";
392 }
393
394 /* Find out who we are talking about here */
395 np=getnickbynumericstr(source);
396 if (np==NULL) {
397 Error("channel",ERR_WARNING,"PART from non existent numeric %s",source);
398 return CMD_OK;
399 }
400
401 harg[1]=np;
402
403 nextchan=pos=cargv[0];
404 while (*nextchan!='\0') {
405 /* Find the next chan position */
406 for (;*pos!='\0' && *pos!=',';pos++)
407 ; /* Empty loop */
408
409 if (*pos==',') {
410 *pos='\0';
411 pos++;
412 }
413
414 /* OK, pos now points at either null or the next chan
415 * and nextchan now points at the channel name we want to parse next */
416
417 if ((cp=findchannel(nextchan))==NULL) {
418 /* Erm, parting a channel that's not there?? */
419 Error("channel",ERR_WARNING,"Nick %s left non-existent channel %s",np->nick,nextchan);
420 } else {
421 /* Trigger hook *FIRST* */
422 harg[0]=cp;
423 triggerhook(HOOK_CHANNEL_PART,harg);
424
425 delnickfromchannel(cp,np->numeric,1);
426 }
427 nextchan=pos;
428 }
429
430 return CMD_OK;
431}
432
433int handlekickmsg(void *source, int cargc, char **cargv) {
434 nick *np,*kicker;
435 channel *cp;
436 void *harg[4];
437
438 if (cargc<3) {
439 return CMD_OK;
440 }
441
442 /* Find out who we are talking about here */
443 if ((np=getnickbynumericstr(cargv[1]))==NULL) {
444 Error("channel",ERR_DEBUG,"Non-existant numeric %s kicked from channel %s",source,cargv[0]);
445 return CMD_OK;
446 }
447
448 /* And who did the kicking */
449 if (((char *)source)[2]=='\0') {
450 /* 'Twas a server.. */
451 kicker=NULL;
452 } else if ((kicker=getnickbynumericstr((char *)source))==NULL) {
453 /* It looks strange, but we let the kick go through anyway */
454 Error("channel",ERR_DEBUG,"Kick from non-existant nick %s",(char *)source);
455 }
456
457 /* And find out which channel */
458 if ((cp=findchannel(cargv[0]))==NULL) {
459 /* OK, not a channel that actually exists then.. */
460 Error("channel",ERR_DEBUG,"Nick %s kicked from non-existent channel %s",np->nick,cargv[0]);
461 } else {
462 /* Before we do anything else, we have to acknowledge the kick to the network */
463 if (homeserver(np->numeric)==mylongnum) {
464 irc_send("%s L %s",longtonumeric(np->numeric,5),cp->index->name->content);
465 }
466
467 /* Trigger hook *FIRST* */
468 harg[0]=cp;
469 harg[1]=np;
470 harg[2]=kicker;
471 harg[3]=cargv[2];
472 triggerhook(HOOK_CHANNEL_KICK,harg);
473
474 /* We let delnickfromchannel() worry about whether this nick is actually on this channel */
475 delnickfromchannel(cp,np->numeric,1);
476 }
477
478 return CMD_OK;
479}
480
481int handletopicmsg(void *source, int cargc, char **cargv) {
482 channel *cp;
483 nick *np;
484 void *harg[2];
485 time_t topictime=0, timestamp=0;
486
487 if (cargc<2) {
488 return CMD_OK;
489 }
490
491 if (cargc>2)
492 topictime=strtol(cargv[cargc-2], NULL, 10);
493
494 if (cargc>3)
495 timestamp=strtol(cargv[cargc-3], NULL, 10);
496
497 if ((np=getnickbynumericstr((char *)source))==NULL) {
498 /* We should check the sender exists, but we still change the topic even if it doesn't */
499 Error("channel",ERR_WARNING,"Topic change by non-existent user %s",(char *)source);
500 }
501
502 /* Grab channel pointer */
503 if ((cp=findchannel(cargv[0]))==NULL) {
504 /* We're not going to create a channel for the sake of a topic.. */
505 return CMD_OK;
506 } else {
507 if (timestamp && (cp->timestamp < timestamp)) {
508 /* Ignore topic change for younger channel
509 * (note that topic change for OLDER channel should be impossible!) */
510 return CMD_OK;
511 }
512 if (topictime && (cp->topictime > topictime)) {
513 /* Ignore topic change with older topic */
514 return CMD_OK;
515 }
516 if (cp->topic!=NULL) {
517 freesstring(cp->topic);
518 }
519 if (cargv[cargc-1][0]=='\0') {
520 cp->topic=NULL;
521 cp->topictime=0;
522 } else {
523 cp->topic=getsstring(cargv[cargc-1],TOPICLEN);
524 cp->topictime=topictime?topictime:getnettime();
525 }
526 /* Trigger hook */
527 harg[0]=cp;
528 harg[1]=np;
529 triggerhook(HOOK_CHANNEL_TOPIC,harg);
530 }
531
532 return CMD_OK;
533}
534
535/*
536 * Note that this function is also used for processing OPMODE messages.
537 * There is no checking on the source etc. anyway, so this should be OK.
538 * (we are trusting ircd not to feed us bogus modes)
539 */
540
541int handlemodemsg(void *source, int cargc, char **cargv) {
542 channel *cp;
543 int dir=1;
544 int arg=2;
545 char *modestr;
546 unsigned long *lp;
547 void *harg[3];
548 nick *np, *target;
549 int hooknum;
550 int changes=0;
551
552 if (cargc<2) {
553 return CMD_OK;
554 }
555
556 if (cargv[0][0]!='#' && cargv[0][0]!='+') {
557 /* Not a channel, ignore */
558 return CMD_OK;
559 }
560
561 if ((cp=findchannel(cargv[0]))==NULL) {
562 /* No channel, abort */
563 Error("channel",ERR_WARNING,"Mode change on non-existent channel %s",cargv[0]);
564 return CMD_OK;
565 }
566
567 if (((char *)source)[2]=='\0') {
568 /* Server mode change, treat as divine intervention */
569 np=NULL;
570 } else if ((np=getnickbynumericstr((char *)source))==NULL) {
571 /* No sender, continue but moan */
572 Error("channel",ERR_WARNING,"Mode change by non-existent user %s on channel %s",(char *)source,cp->index->name->content);
573 }
574
575 /* Set up the hook data */
576 harg[0]=cp;
577 harg[1]=np;
578
579 /* Process the mode string one character at a time */
580 /* Maybe I'll write this more intelligently one day if I can comprehend the ircu code that does this */
581 for (modestr=cargv[1];*modestr;modestr++) {
582 switch(*modestr) {
583 /* Set whether we are adding or removing modes */
584
585 case '+':
586 dir=1;
587 break;
588
589 case '-':
590 dir=0;
591 break;
592
593 /* Simple modes: just set or clear based on value of dir */
594
595 case 'n':
596 if (dir) { SetNoExtMsg(cp); } else { ClearNoExtMsg(cp); }
597 changes |= MODECHANGE_MODES;
598 break;
599
600 case 't':
601 if (dir) { SetTopicLimit(cp); } else { ClearTopicLimit(cp); }
602 changes |= MODECHANGE_MODES;
603 break;
604
605 case 's':
606 if (dir) { SetSecret(cp); ClearPrivate(cp); } else { ClearSecret(cp); }
607 changes |= MODECHANGE_MODES;
608 break;
609
610 case 'p':
611 if (dir) { SetPrivate(cp); ClearSecret(cp); } else { ClearPrivate(cp); }
612 changes |= MODECHANGE_MODES;
613 break;
614
615 case 'i':
616 if (dir) { SetInviteOnly(cp); } else { ClearInviteOnly(cp); }
617 changes |= MODECHANGE_MODES;
618 break;
619
620 case 'm':
621 if (dir) { SetModerated(cp); } else { ClearModerated(cp); }
622 changes |= MODECHANGE_MODES;
623 break;
624
625 case 'c':
626 if (dir) { SetNoColour(cp); } else { ClearNoColour(cp); }
627 changes |= MODECHANGE_MODES;
628 break;
629
630 case 'C':
631 if (dir) { SetNoCTCP(cp); } else { ClearNoCTCP(cp); }
632 changes |= MODECHANGE_MODES;
633 break;
634
635 case 'r':
636 if (dir) { SetRegOnly(cp); } else { ClearRegOnly(cp); }
637 changes |= MODECHANGE_MODES;
638 break;
639
640 case 'D':
641 if (dir) { SetDelJoins(cp); } else { ClearDelJoins(cp); }
642 changes |= MODECHANGE_MODES;
643 break;
644
645 case 'u':
646 if (dir) { SetNoQuitMsg(cp); } else { ClearNoQuitMsg(cp); }
647 changes |= MODECHANGE_MODES;
648 break;
649
650 case 'N':
651 if (dir) { SetNoNotice(cp); } else { ClearNoNotice(cp); }
652 changes |= MODECHANGE_MODES;
653 break;
654
73a2c60a 655 case 'M':
656 if (dir) { SetModNoAuth(cp); } else { ClearModNoAuth(cp); }
657 changes |= MODECHANGE_MODES;
658 break;
659
660 case 'T':
661 if (dir) { SetSingleTarg(cp); } else { ClearSingleTarg(cp); }
662 changes |= MODECHANGE_MODES;
663 break;
664
c86edd1d
Q
665 /* Parameter modes: advance parameter and possibly read it in */
666
667 case 'l':
668 if (dir) {
669 /* +l uses a parameter, but -l does not.
670 * If there is no parameter, don't set the mode.
671 * I guess we should moan too in that case, but
672 * they might be even nastier to us if we do ;) */
673 if (arg<cargc) {
674 cp->limit=strtol(cargv[arg++],NULL,10);
675 SetLimit(cp);
676 }
677 } else {
678 ClearLimit(cp);
679 cp->limit=0;
680 }
681 changes |= MODECHANGE_MODES;
682 break;
683
684 case 'k':
685 if (dir) {
686 /* +k uses a parameter in both directions */
687 if (arg<cargc) {
688 freesstring(cp->key); /* It's probably NULL, but be safe */
689 cp->key=getsstring(cargv[arg++],KEYLEN);
690 SetKey(cp);
691 }
692 } else {
693 freesstring(cp->key);
694 cp->key=NULL;
695 ClearKey(cp);
696 arg++; /* Eat the arg without looking at it, even if it's not there */
697 }
698 changes |= MODECHANGE_MODES;
699 break;
700
701 /* Op/Voice */
702
703 case 'o':
704 case 'v':
705 if (arg<cargc) {
706 if((lp=getnumerichandlefromchanhash(cp->users,numerictolong(cargv[arg++],5)))==NULL) {
707 /* They're not on the channel; MODE crossed with part/kill/kick/blah */
708 Error("channel",ERR_DEBUG,"Mode change for user %s not on channel %s",cargv[arg-1],cp->index->name->content);
709 } else {
710 if ((target=getnickbynumeric(*lp))==NULL) {
711 /* This really is a fuckup, we found them on the channel but there isn't a user with that numeric */
712 /* This means there's a serious bug in the nick/channel tracking code */
713 Error("channel",ERR_ERROR,"Mode change for user %s on channel %s who doesn't exist",cargv[arg-1],cp->index->name->content);
714 } else { /* Do the mode change whilst admiring the beautiful code layout */
715 harg[2]=target;
716 if (*modestr=='o') { if (dir) { *lp |= CUMODE_OP; hooknum=HOOK_CHANNEL_OPPED; } else
717 { *lp &= ~CUMODE_OP; hooknum=HOOK_CHANNEL_DEOPPED; } }
718 else { if (dir) { *lp |= CUMODE_VOICE; hooknum=HOOK_CHANNEL_VOICED; } else
719 { *lp &= ~CUMODE_VOICE; hooknum=HOOK_CHANNEL_DEVOICED; } }
720 triggerhook(hooknum,harg);
721 }
722 }
723 }
724 changes |= MODECHANGE_USERS;
725 break;
726
727 case 'b':
728 if (arg<cargc) {
729 if (dir) {
730 setban(cp,cargv[arg++]);
731 triggerhook(HOOK_CHANNEL_BANSET,harg);
732 } else {
733 clearban(cp,cargv[arg++],0);
734 triggerhook(HOOK_CHANNEL_BANCLEAR,harg);
735 }
736 }
737 changes |= MODECHANGE_BANS;
738 break;
739
740 default:
741 Error("channel",ERR_DEBUG,"Unknown mode char '%c' %s on %s",*modestr,dir?"set":"cleared",cp->index->name->content);
742 break;
743 }
744 }
745
c3db6f7e 746 harg[2]=(void *)((long)changes);
c86edd1d
Q
747 triggerhook(HOOK_CHANNEL_MODECHANGE,(void *)harg);
748 return CMD_OK;
749}
750
751/*
752 * Deal with 2.10.11ism: CLEARMODE
753 *
754 * [hAAA CM #twilightzone ovpsmikbl
755 */
756
757int handleclearmodemsg(void *source, int cargc, char **cargv) {
758 channel *cp;
759 void *harg[3];
760 nick *np, *target;
761 char *mcp;
762 unsigned long usermask=0;
763 int i;
764 int changes=0;
765
766 if (cargc<2) {
767 return CMD_OK;
768 }
769
770 if ((cp=findchannel(cargv[0]))==NULL) {
771 /* No channel, abort */
772 Error("channel",ERR_WARNING,"Mode change on non-existent channel %s",cargv[0]);
773 return CMD_OK;
774 }
775
776 if (((char *)source)[2]=='\0') {
777 /* Server mode change? (I don't think servers are allowed to do CLEARMODE) */
778 np=NULL;
779 } else if ((np=getnickbynumericstr((char *)source))==NULL) {
780 /* No sender, continue but moan */
781 Error("channel",ERR_WARNING,"Mode change by non-existent user %s on channel %s",(char *)source,cp->index->name->content);
782 }
783
784 harg[0]=cp;
785 harg[1]=np;
786
787 for (mcp=cargv[1];*mcp;mcp++) {
788 switch (*mcp) {
789 case 'o':
790 usermask |= CUMODE_OP;
791 changes |= MODECHANGE_USERS;
792 break;
793
794 case 'v':
795 usermask |= CUMODE_VOICE;
796 changes |= MODECHANGE_USERS;
797 break;
798
799 case 'n':
800 ClearNoExtMsg(cp);
801 changes |= MODECHANGE_MODES;
802 break;
803
804 case 't':
805 ClearTopicLimit(cp);
806 changes |= MODECHANGE_MODES;
807 break;
808
809 case 's':
810 ClearSecret(cp);
811 changes |= MODECHANGE_MODES;
812 break;
813
814 case 'p':
815 ClearPrivate(cp);
816 changes |= MODECHANGE_MODES;
817 break;
818
819 case 'i':
820 ClearInviteOnly(cp);
821 changes |= MODECHANGE_MODES;
822 break;
823
824 case 'm':
825 ClearModerated(cp);
826 changes |= MODECHANGE_MODES;
827 break;
828
829 case 'c':
830 ClearNoColour(cp);
831 changes |= MODECHANGE_MODES;
832 break;
833
834 case 'C':
835 ClearNoCTCP(cp);
836 changes |= MODECHANGE_MODES;
837 break;
838
839 case 'r':
840 ClearRegOnly(cp);
841 changes |= MODECHANGE_MODES;
842 break;
843
844 case 'D':
845 ClearDelJoins(cp);
846 changes |= MODECHANGE_MODES;
847 break;
848
849 case 'u':
850 ClearNoQuitMsg(cp);
851 changes |= MODECHANGE_MODES;
852 break;
853
854 case 'N':
855 ClearNoNotice(cp);
856 changes |= MODECHANGE_MODES;
857 break;
73a2c60a 858
859 case 'M':
860 ClearModNoAuth(cp);
861 changes |= MODECHANGE_MODES;
862 break;
863
864 case 'T':
865 ClearSingleTarg(cp);
866 changes |= MODECHANGE_MODES;
867 break;
c86edd1d
Q
868
869 case 'b':
870 clearallbans(cp);
871 changes |= MODECHANGE_BANS;
872 break;
873
874 case 'k':
875 /* This is all safe even if there is no key atm */
876 freesstring(cp->key);
877 cp->key=NULL;
878 ClearKey(cp);
879 changes |= MODECHANGE_MODES;
880 break;
881
882 case 'l':
883 cp->limit=0;
884 ClearLimit(cp);
885 changes |= MODECHANGE_MODES;
886 break;
887 }
888 }
889
890 if (usermask) {
891 /* We have to strip something off each user */
892 for (i=0;i<cp->users->hashsize;i++) {
893 if (cp->users->content[i]!=nouser && (cp->users->content[i] & usermask)) {
894 /* This user exists and has at least one of the modes we're clearing */
895 if ((target=getnickbynumeric(cp->users->content[i]))==NULL) {
896 /* This really is a fuckup, we found them on the channel but there isn't a user with that numeric */
897 /* This means there's a serious bug in the nick/channel tracking code */
898 Error("channel",ERR_ERROR,"CLEARMODE failed: user on channel who doesn't exist?");
899 } else {
900 harg[2]=target;
901 /* Yes, these are deliberate three way bitwise ANDs.. */
902 if (cp->users->content[i] & usermask & CUMODE_OP)
903 triggerhook(HOOK_CHANNEL_DEOPPED, harg);
904 if (cp->users->content[i] & usermask & CUMODE_VOICE)
905 triggerhook(HOOK_CHANNEL_DEVOICED, harg);
906 cp->users->content[i] &= ~usermask;
907 }
908 }
909 }
910 }
911
c3db6f7e 912 harg[2]=(void *)((long)changes);
c86edd1d
Q
913 triggerhook(HOOK_CHANNEL_MODECHANGE, harg);
914 return CMD_OK;
915}
f2b36aa8
CP
916
917void handlewhoischannels(int hooknum, void *arg) {
e40b9754 918 channel **chans;
f2b36aa8 919 char buffer[1024];
e40b9754 920 unsigned int bufpos;
921 sstring *name;
922 unsigned long *num;
f2b36aa8
CP
923 int i;
924 void **args = (void **)arg;
925 nick *sender = (nick *)args[0], *target = (nick *)args[1];
926
927 if(IsService(target) || IsHideChan(target))
928 return;
929
930 chans = (channel **)(target->channels->content);
931
932 buffer[0] = '\0';
e40b9754 933 bufpos=0;
934
f2b36aa8 935 /* Not handling delayed joins. */
bfae7e88 936 for(i=target->channels->cursi-1;i>=0;i--) {
e40b9754 937 /* Secret / Private channels: only show if the sender is on the channel as well */
938 if(IsSecret(chans[i]) || IsPrivate(chans[i])) {
939 if (!getnumerichandlefromchanhash(chans[i]->users, sender->numeric))
f2b36aa8
CP
940 continue;
941 }
e40b9754 942
f2b36aa8 943 name = chans[i]->index->name;
e40b9754 944 if (bufpos + name->length > 508) { /* why 508? - need room for -@#channame\0 + 1 slack */
f2b36aa8
CP
945 irc_send("%s", buffer);
946 buffer[0] = '\0';
e40b9754 947 bufpos=0;
f2b36aa8
CP
948 }
949
950 if(buffer[0] == '\0')
e40b9754 951 bufpos=snprintf(buffer, sizeof(buffer), ":%s 319 %s %s :", myserver->content, sender->nick, target->nick);
f2b36aa8
CP
952
953 num = getnumerichandlefromchanhash(chans[i]->users, target->numeric);
e40b9754 954
955 /* Adding these flags might make the string "unsafe" (without terminating \0). */
956 /* sprintf'ing the channel name afterwards is guaranteed to fix it though */
957 if (IsDeaf(target))
958 buffer[bufpos++]='-';
f2b36aa8 959 if (*num & CUMODE_OP)
e40b9754 960 buffer[bufpos++]='@';
f2b36aa8 961 else if (*num & CUMODE_VOICE)
e40b9754 962 buffer[bufpos++]='+';
f2b36aa8 963
e40b9754 964 bufpos += sprintf(buffer+bufpos, "%s ",name->content);
f2b36aa8
CP
965 }
966
967 if (buffer[0] != '\0')
968 irc_send("%s", buffer);
969}
970