]>
jfr.im git - irc/quakenet/newserv.git/blob - channel/channelhandlers.c
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"
14 #include "../lib/strlfunc.h"
16 int handleburstmsg(void *source
, int cargc
, char **cargv
) {
24 int waslimit
,waskeyed
;
26 unsigned long currentmode
;
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 */
33 Error("channel",ERR_WARNING
,"Burst message with only %d parameters",cargc
);
37 timestamp
=strtol(cargv
[1],NULL
,10);
39 if ((cp
=findchannel(cargv
[0]))==NULL
) {
40 /* We don't have this channel already */
41 cp
=createchannel(cargv
[0]);
42 cp
->timestamp
=timestamp
;
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
);
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
;
63 } else if (timestamp
>cp
->timestamp
) {
64 /* The incoming timestamp is greater. Ignore any incoming modes they may happen to set */
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]=='+') {
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') {
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 */
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 */
96 newlimit
=strtol(cargv
[arg
],NULL
,10);
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
) {
104 /* No limit before -- we just have the new one */
107 } else if (waslimit
) {
108 SetLimit(cp
); /* We had a limit before, but the burst didn't set one. Restore flag. */
111 if (IsKey(cp
)) { /* A key was SET by the burst */
113 /* Ran out of args -- oopsie! */
114 Error("channel",ERR_WARNING
,"Burst +k with no argument");
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
);
125 /* No key before -- just the new one */
126 cp
->key
=getsstring(cargv
[arg
],KEYLEN
);
128 } else if (waskeyed
) {
129 SetKey(cp
); /* We had a key before, but the burst didn't set one. Restore flag. */
132 } else if (cargv
[arg
][0]=='%') {
133 /* We have one or more bans here */
134 nextnum
=cargv
[arg
]+1;
136 /* Split off the next ban */
137 for (charp
=nextnum
;*charp
;charp
++) {
148 /* List of numerics */
149 nextnum
=charp
=cargv
[arg
];
151 while (*nextnum
!='\0') {
152 /* Step over the next numeric */
163 } else if (*charp
==':') {
168 for (;*charp
;charp
++) {
170 currentmode
|=CUMODE_VOICE
;
171 } else if (*charp
=='o') {
172 currentmode
|=CUMODE_OP
;
173 } else if (*charp
==',') {
178 /* If we're ignore incoming modes, zap it to zero again */
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
));
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.. */
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
);
201 /* If this is a new channel, we do the NEWCHANNEL hook also */
203 triggerhook(HOOK_CHANNEL_NEWCHANNEL
,cp
);
205 /* Just one hook to say "something happened to this channel" */
206 triggerhook(HOOK_CHANNEL_BURST
,cp
);
212 int handlejoinmsg(void *source
, int cargc
, char **cargv
) {
226 /* We must have received a timestamp too */
227 timestamp
=strtol(cargv
[1],NULL
,10);
230 /* Find out who we are talking about here */
231 np
=getnickbynumericstr(source
);
233 Error("channel",ERR_WARNING
,"Channel join from non existent user %s",source
);
237 nextchan
=pos
=cargv
[0];
238 while (*nextchan
!='\0') {
239 /* Find the next chan position */
240 for (;*pos
!='\0' && *pos
!=',';pos
++)
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 */
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
++) {
260 triggerhook(HOOK_CHANNEL_PART
,harg
);
261 delnickfromchannel(ch
[i
],np
->numeric
,0);
263 array_free(np
->channels
);
264 array_init(np
->channels
,sizeof(channel
*));
266 /* It's an actual channel join */
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
);
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
;
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 */
287 /* If we just created a channel, flag it */
289 triggerhook(HOOK_CHANNEL_NEWCHANNEL
,cp
);
295 triggerhook(HOOK_CHANNEL_JOIN
,harg
);
303 int handlecreatemsg(void *source
, int cargc
, char **cargv
) {
315 timestamp
=strtol(cargv
[1],NULL
,10);
317 /* Find out who we are talking about here */
318 np
=getnickbynumericstr(source
);
320 Error("channel",ERR_WARNING
,"Channel create from non existent user %s",source
);
324 nextchan
=pos
=cargv
[0];
325 while (*nextchan
!='\0') {
326 /* Find the next chan position */
327 for (;*pos
!='\0' && *pos
!=',';pos
++)
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 */
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
;
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
;
352 /* Add the user to the channel, preopped */
353 if (addnicktochannel(cp
,(np
->numeric
)|CUMODE_OP
)) {
358 /* Flag the channel as new if necessary */
360 triggerhook(HOOK_CHANNEL_NEWCHANNEL
,cp
);
366 triggerhook(HOOK_CHANNEL_CREATE
,harg
);
374 int handlepartmsg(void *source
, int cargc
, char **cargv
) {
385 Error("channel",ERR_WARNING
,"PART with too many parameters (%d)",cargc
);
389 harg
[2]=(void *)cargv
[1];
394 /* Find out who we are talking about here */
395 np
=getnickbynumericstr(source
);
397 Error("channel",ERR_WARNING
,"PART from non existent numeric %s",source
);
403 nextchan
=pos
=cargv
[0];
404 while (*nextchan
!='\0') {
405 /* Find the next chan position */
406 for (;*pos
!='\0' && *pos
!=',';pos
++)
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 */
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
);
421 /* Trigger hook *FIRST* */
423 triggerhook(HOOK_CHANNEL_PART
,harg
);
425 delnickfromchannel(cp
,np
->numeric
,1);
433 int handlekickmsg(void *source
, int cargc
, char **cargv
) {
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]);
448 /* And who did the kicking */
449 if (((char *)source
)[2]=='\0') {
450 /* 'Twas a server.. */
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
);
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]);
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
);
467 /* Trigger hook *FIRST* */
472 triggerhook(HOOK_CHANNEL_KICK
,harg
);
474 /* We let delnickfromchannel() worry about whether this nick is actually on this channel */
475 delnickfromchannel(cp
,np
->numeric
,1);
481 int handletopicmsg(void *source
, int cargc
, char **cargv
) {
485 time_t topictime
=0, timestamp
=0;
492 topictime
=strtol(cargv
[cargc
-2], NULL
, 10);
495 timestamp
=strtol(cargv
[cargc
-3], NULL
, 10);
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
);
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.. */
507 if (timestamp
&& (cp
->timestamp
< timestamp
)) {
508 /* Ignore topic change for younger channel
509 * (note that topic change for OLDER channel should be impossible!) */
512 if (topictime
&& (cp
->topictime
> topictime
)) {
513 /* Ignore topic change with older topic */
516 if (cp
->topic
!=NULL
) {
517 freesstring(cp
->topic
);
519 if (cargv
[cargc
-1][0]=='\0') {
523 cp
->topic
=getsstring(cargv
[cargc
-1],TOPICLEN
);
524 cp
->topictime
=topictime
?topictime
:getnettime();
529 triggerhook(HOOK_CHANNEL_TOPIC
,harg
);
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)
541 int handlemodemsg(void *source
, int cargc
, char **cargv
) {
556 if (cargv
[0][0]!='#' && cargv
[0][0]!='+') {
557 /* Not a channel, ignore */
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]);
567 if (((char *)source
)[2]=='\0') {
568 /* Server mode change, treat as divine intervention */
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
);
575 /* Set up the hook data */
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
++) {
583 /* Set whether we are adding or removing modes */
593 /* Simple modes: just set or clear based on value of dir */
596 if (dir
) { SetNoExtMsg(cp
); } else { ClearNoExtMsg(cp
); }
597 changes
|= MODECHANGE_MODES
;
601 if (dir
) { SetTopicLimit(cp
); } else { ClearTopicLimit(cp
); }
602 changes
|= MODECHANGE_MODES
;
606 if (dir
) { SetSecret(cp
); ClearPrivate(cp
); } else { ClearSecret(cp
); }
607 changes
|= MODECHANGE_MODES
;
611 if (dir
) { SetPrivate(cp
); ClearSecret(cp
); } else { ClearPrivate(cp
); }
612 changes
|= MODECHANGE_MODES
;
616 if (dir
) { SetInviteOnly(cp
); } else { ClearInviteOnly(cp
); }
617 changes
|= MODECHANGE_MODES
;
621 if (dir
) { SetModerated(cp
); } else { ClearModerated(cp
); }
622 changes
|= MODECHANGE_MODES
;
626 if (dir
) { SetNoColour(cp
); } else { ClearNoColour(cp
); }
627 changes
|= MODECHANGE_MODES
;
631 if (dir
) { SetNoCTCP(cp
); } else { ClearNoCTCP(cp
); }
632 changes
|= MODECHANGE_MODES
;
636 if (dir
) { SetRegOnly(cp
); } else { ClearRegOnly(cp
); }
637 changes
|= MODECHANGE_MODES
;
641 if (dir
) { SetDelJoins(cp
); } else { ClearDelJoins(cp
); }
642 changes
|= MODECHANGE_MODES
;
646 if (dir
) { SetNoQuitMsg(cp
); } else { ClearNoQuitMsg(cp
); }
647 changes
|= MODECHANGE_MODES
;
651 if (dir
) { SetNoNotice(cp
); } else { ClearNoNotice(cp
); }
652 changes
|= MODECHANGE_MODES
;
656 if (dir
) { SetModNoAuth(cp
); } else { ClearModNoAuth(cp
); }
657 changes
|= MODECHANGE_MODES
;
661 if (dir
) { SetSingleTarg(cp
); } else { ClearSingleTarg(cp
); }
662 changes
|= MODECHANGE_MODES
;
665 /* Parameter modes: advance parameter and possibly read it in */
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 ;) */
674 cp
->limit
=strtol(cargv
[arg
++],NULL
,10);
681 changes
|= MODECHANGE_MODES
;
686 /* +k uses a parameter in both directions */
688 freesstring(cp
->key
); /* It's probably NULL, but be safe */
689 cp
->key
=getsstring(cargv
[arg
++],KEYLEN
);
693 freesstring(cp
->key
);
696 arg
++; /* Eat the arg without looking at it, even if it's not there */
698 changes
|= MODECHANGE_MODES
;
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
);
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 */
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
);
724 changes
|= MODECHANGE_USERS
;
730 setban(cp
,cargv
[arg
++]);
731 triggerhook(HOOK_CHANNEL_BANSET
,harg
);
733 clearban(cp
,cargv
[arg
++],0);
734 triggerhook(HOOK_CHANNEL_BANCLEAR
,harg
);
737 changes
|= MODECHANGE_BANS
;
741 Error("channel",ERR_DEBUG
,"Unknown mode char '%c' %s on %s",*modestr
,dir
?"set":"cleared",cp
->index
->name
->content
);
746 harg
[2]=(void *)((long)changes
);
747 triggerhook(HOOK_CHANNEL_MODECHANGE
,(void *)harg
);
752 * Deal with 2.10.11ism: CLEARMODE
754 * [hAAA CM #twilightzone ovpsmikbl
757 int handleclearmodemsg(void *source
, int cargc
, char **cargv
) {
762 unsigned long usermask
=0;
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]);
776 if (((char *)source
)[2]=='\0') {
777 /* Server mode change? (I don't think servers are allowed to do CLEARMODE) */
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
);
787 for (mcp
=cargv
[1];*mcp
;mcp
++) {
790 usermask
|= CUMODE_OP
;
791 changes
|= MODECHANGE_USERS
;
795 usermask
|= CUMODE_VOICE
;
796 changes
|= MODECHANGE_USERS
;
801 changes
|= MODECHANGE_MODES
;
806 changes
|= MODECHANGE_MODES
;
811 changes
|= MODECHANGE_MODES
;
816 changes
|= MODECHANGE_MODES
;
821 changes
|= MODECHANGE_MODES
;
826 changes
|= MODECHANGE_MODES
;
831 changes
|= MODECHANGE_MODES
;
836 changes
|= MODECHANGE_MODES
;
841 changes
|= MODECHANGE_MODES
;
846 changes
|= MODECHANGE_MODES
;
851 changes
|= MODECHANGE_MODES
;
856 changes
|= MODECHANGE_MODES
;
861 changes
|= MODECHANGE_MODES
;
866 changes
|= MODECHANGE_MODES
;
871 changes
|= MODECHANGE_BANS
;
875 /* This is all safe even if there is no key atm */
876 freesstring(cp
->key
);
879 changes
|= MODECHANGE_MODES
;
885 changes
|= MODECHANGE_MODES
;
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?");
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
;
912 harg
[2]=(void *)((long)changes
);
913 triggerhook(HOOK_CHANNEL_MODECHANGE
, harg
);
917 void handlewhoischannels(int hooknum
, void *arg
) {
924 void **args
= (void **)arg
;
925 nick
*sender
= (nick
*)args
[0], *target
= (nick
*)args
[1];
927 if(IsService(target
) || IsHideChan(target
))
930 chans
= (channel
**)(target
->channels
->content
);
935 /* Not handling delayed joins. */
936 for(i
=target
->channels
->cursi
-1;i
>=0;i
--) {
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
))
943 name
= chans
[i
]->index
->name
;
944 if (bufpos
+ name
->length
> 508) { /* why 508? - need room for -@#channame\0 + 1 slack */
945 irc_send("%s", buffer
);
950 if(buffer
[0] == '\0')
951 bufpos
=snprintf(buffer
, sizeof(buffer
), ":%s 319 %s %s :", myserver
->content
, sender
->nick
, target
->nick
);
953 num
= getnumerichandlefromchanhash(chans
[i
]->users
, target
->numeric
);
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 */
958 buffer
[bufpos
++]='-';
959 if (*num
& CUMODE_OP
)
960 buffer
[bufpos
++]='@';
961 else if (*num
& CUMODE_VOICE
)
962 buffer
[bufpos
++]='+';
964 bufpos
+= sprintf(buffer
+bufpos
, "%s ",name
->content
);
967 if (buffer
[0] != '\0')
968 irc_send("%s", buffer
);