1 /* Functions for manipulating local users on channels */
4 #include "localuserchannel.h"
5 #include "../nick/nick.h"
6 #include "../channel/channel.h"
7 #include "../irc/irc.h"
8 #include "../lib/version.h"
9 #include "../lib/sstring.h"
18 typedef struct pendingkick
{
19 nick
*source
, *target
;
22 struct pendingkick
*next
;
25 pendingkick
*pendingkicklist
;
27 int handlechannelmsgcmd(void *source
, int cargc
, char **cargv
);
28 int handlechannelnoticecmd(void *source
, int cargc
, char **cargv
);
29 int handleinvitecmd(void *source
, int cargc
, char **cargv
);
30 void clearpendingkicks(int hooknum
, void *arg
);
31 void checkpendingkicknicks(int hooknum
, void *arg
);
32 void checkpendingkickchannels(int hooknum
, void *arg
);
33 void _localkickuser(nick
*np
, channel
*cp
, nick
*target
, const char *message
);
34 void luc_handlekick(int hooknum
, void *arg
);
38 registerserverhandler("P",&handlechannelmsgcmd
,2);
39 registerserverhandler("O",&handlechannelnoticecmd
,2);
40 registerserverhandler("I",&handleinvitecmd
,2);
41 registerhook(HOOK_CHANNEL_KICK
, luc_handlekick
);
42 registerhook(HOOK_NICK_LOSTNICK
, checkpendingkicknicks
);
43 registerhook(HOOK_CHANNEL_LOSTCHANNEL
, checkpendingkickchannels
);
44 registerhook(HOOK_CORE_ENDOFHOOKSQUEUE
,&clearpendingkicks
);
50 deregisterserverhandler("P",&handlechannelmsgcmd
);
51 deregisterserverhandler("O",&handlechannelnoticecmd
);
52 deregisterserverhandler("I",&handleinvitecmd
);
53 deregisterhook(HOOK_CHANNEL_KICK
, luc_handlekick
);
54 deregisterhook(HOOK_NICK_LOSTNICK
, checkpendingkicknicks
);
55 deregisterhook(HOOK_CHANNEL_LOSTCHANNEL
, checkpendingkickchannels
);
56 deregisterhook(HOOK_CORE_ENDOFHOOKSQUEUE
,&clearpendingkicks
);
58 for (pk
=pendingkicklist
;pk
;pk
=pendingkicklist
) {
59 pendingkicklist
= pk
->next
;
60 freesstring(pk
->reason
);
65 void luc_handlekick(int hooknum
, void *arg
) {
70 if (homeserver(target
->numeric
)!=mylongnum
)
73 if (umhandlers
[target
->numeric
& MAXLOCALUSER
]) {
78 (umhandlers
[target
->numeric
& MAXLOCALUSER
])(target
, LU_KICKED
, myargs
);
82 /* invites look something like:
83 * XXyyy I TargetNick :#channel
86 int handleinvitecmd(void *source
, int cargc
, char **cargv
) {
96 if (!(sender
=getnickbynumericstr(source
))) {
97 Error("localuserchannel",ERR_WARNING
,"Got invite from unknown numeric %s.",source
);
101 if (!(target
=getnickbynick(cargv
[0]))) {
102 Error("localuserchannel",ERR_WARNING
,"Got invite for unknown local user %s.",cargv
[0]);
106 if (!(cp
=findchannel(cargv
[1]))) {
107 Error("localuserchannel",ERR_WARNING
,"Got invite for non-existent channel %s.",cargv
[1]);
111 if (homeserver(target
->numeric
) != mylongnum
) {
112 Error("localuserchannel",ERR_WARNING
,"Got invite for non-local user %s.",target
->nick
);
116 /* This is a valid race condition.. */
117 if (getnumerichandlefromchanhash(cp
->users
, target
->numeric
)) {
118 Error("localuserchannel",ERR_DEBUG
,"Got invite for user %s already on %s.",target
->nick
, cp
->index
->name
->content
);
122 nargs
[0]=(void *)sender
;
125 if (umhandlers
[target
->numeric
&MAXLOCALUSER
]) {
126 (umhandlers
[target
->numeric
&MAXLOCALUSER
])(target
, LU_INVITE
, nargs
);
132 /* PRIVMSG/NOTICE to channel handling is identical up to the point where the hook is called. */
133 static int handlechannelmsgornotice(void *source
, int cargc
, char **cargv
, int isnotice
) {
138 unsigned long numeric
;
146 if (cargv
[0][0]!='#' && cargv
[0][0]!='+') {
147 /* Not a channel message */
151 if ((sender
=getnickbynumericstr((char *)source
))==NULL
) {
152 Error("localuserchannel",ERR_DEBUG
,"PRIVMSG/NOTICE from non existant user %s",(char *)source
);
156 if ((target
=findchannel(cargv
[0]))==NULL
) {
157 Error("localuserchannel",ERR_DEBUG
,"PRIVMSG/NOTICE to non existant channel %s",cargv
[0]);
161 /* OK, we have a valid channel the message was sent to. Let's look to see
162 * if we have any local users on there. Set up the arguments first as they are
163 * always going to be the same. */
165 nargs
[0]=(void *)sender
;
166 nargs
[1]=(void *)target
;
167 nargs
[2]=(void *)cargv
[1];
169 for (found
=0,i
=0;i
<target
->users
->hashsize
;i
++) {
170 numeric
=target
->users
->content
[i
];
171 if (numeric
!=nouser
&& homeserver(numeric
&CU_NUMERICMASK
)==mylongnum
) {
172 /* OK, it's one of our users.. we need to deal with it */
173 if (found
&& ((getnickbynumericstr((char *)source
)==NULL
) || ((findchannel(cargv
[0]))==NULL
) || !(getnumerichandlefromchanhash(target
->users
, sender
->numeric
)))) {
174 Error("localuserchannel", ERR_INFO
, "Nick or channel lost, or user no longer on channel in LU_CHANMSG");
178 if (umhandlers
[numeric
&MAXLOCALUSER
]) {
179 if ((np
=getnickbynumeric(numeric
))==NULL
) {
180 Error("localuserchannel",ERR_ERROR
,"PRIVMSG/NOTICE to channel user who doesn't exist (?) on %s",cargv
[0]);
184 (umhandlers
[numeric
&MAXLOCALUSER
])(np
,(isnotice
?LU_CHANNOTICE
:LU_CHANMSG
),nargs
);
193 Error("localuserchannel",ERR_DEBUG
,"Couldn't find any local targets for PRIVMSG/NOTICE to %s",cargv
[0]);
199 /* Wrapper functions to call the above code */
200 int handlechannelmsgcmd(void *source
, int cargc
, char **cargv
) {
201 return handlechannelmsgornotice(source
, cargc
, cargv
, 0);
204 int handlechannelnoticecmd(void *source
, int cargc
, char **cargv
) {
205 return handlechannelmsgornotice(source
, cargc
, cargv
, 1);
208 /* Burst onto channel. This replaces the timestamp and modes
209 * with the provided ones. Keys and limits use the provided ones
210 * if needed. nick * is optional, but joins the channel opped if
213 * Due to the way ircu works, this only works if the provided timestamp is
214 * older than the one currently on the channel. If the timestamps are
215 * equal, the modes are ignored, but the user (if any) is still allowed to
216 * join with op. If the provided timestamp is newer than the exsting one we
217 * just do a join instead - if you try to replace an old timestamp with a
218 * newer one ircu will just laugh at you (and you will be desynced).
220 int localburstontochannel(channel
*cp
, nick
*np
, time_t timestamp
, flag_t modes
, unsigned int limit
, char *key
) {
222 char extramodebuf
[512];
228 if (timestamp
> cp
->timestamp
) {
229 return localjoinchannel(np
, cp
);
232 if (timestamp
< cp
->timestamp
) {
233 cp
->timestamp
=timestamp
;
236 /* deal with key - if we need one use the provided one if set, otherwise
237 * the existing one, but if there is no existing one clear +k */
239 /* Sanitise the provided key - this might invalidate it.. */
244 /* Free old key, if any */
246 freesstring(cp
->key
);
248 cp
->key
=getsstring(key
,KEYLEN
);
254 /* Not +k - free the existing key, if any */
255 freesstring(cp
->key
);
271 /* We also need to blow away all other op/voice and bans on the
272 * channel. This is the same code we use when someone else does
275 for (i
=0;i
<cp
->users
->hashsize
;i
++) {
276 if (cp
->users
->content
[i
]!=nouser
) {
277 cp
->users
->content
[i
]&=CU_NUMERICMASK
;
282 /* Actually add the nick to the channel. Make sure it's a local nick and actually exists first. */
283 if (np
&& (homeserver(np
->numeric
) == mylongnum
) &&
284 !(getnumerichandlefromchanhash(cp
->users
,np
->numeric
))) {
285 addnicktochannel(cp
,(np
->numeric
)|CUMODE_OP
);
287 np
=NULL
; /* If we're not adding it here, don't send it later in the burst msg either */
291 /* actual burst message */
293 sprintf(nickbuf
," %s:o", longtonumeric(np
->numeric
,5));
299 sprintf(extramodebuf
," %d",cp
->limit
);
301 extramodebuf
[0]='\0';
304 /* XX B #channel <timestamp> +modes <limit> <key> <user> */
305 irc_send("%s B %s %lu %s%s%s%s%s",
306 mynumeric
->content
,cp
->index
->name
->content
,cp
->timestamp
,
307 printflags(cp
->flags
,cmodeflags
),extramodebuf
,
308 IsKey(cp
)?" ":"",IsKey(cp
)?cp
->key
->content
:"", nickbuf
);
311 /* Tell the world something happened... */
312 triggerhook(HOOK_CHANNEL_BURST
,cp
);
317 int localjoinchannel(nick
*np
, channel
*cp
) {
320 if (cp
==NULL
|| np
==NULL
) {
324 /* Check that the user _is_ a local one.. */
325 if (homeserver(np
->numeric
)!=mylongnum
) {
329 /* Check that the user isn't on the channel already */
330 if ((getnumerichandlefromchanhash(cp
->users
,np
->numeric
))!=NULL
) {
334 /* OK, join the channel.. */
335 addnicktochannel(cp
,np
->numeric
);
337 /* Trigger the event */
342 irc_send("%s J %s %lu",longtonumeric(np
->numeric
,5),cp
->index
->name
->content
,cp
->timestamp
);
345 triggerhook(HOOK_CHANNEL_JOIN
, harg
);
350 int localpartchannel(nick
*np
, channel
*cp
, char *reason
) {
353 /* Check pointers are valid.. */
354 if (cp
==NULL
|| np
==NULL
) {
355 Error("localuserchannel",ERR_WARNING
,"Trying to part NULL channel or NULL nick (cp=%x,np=%x)",cp
,np
);
359 /* And that user is local.. */
360 if (homeserver(np
->numeric
)!=mylongnum
) {
361 Error("localuserchannel",ERR_WARNING
,"Trying to part remote user %s",np
->nick
);
365 /* Check that user is on channel */
366 if (getnumerichandlefromchanhash(cp
->users
,np
->numeric
)==NULL
) {
367 Error("localuserchannel",ERR_WARNING
,"Trying to part user %s from channel %s it is not on",np
->nick
,cp
->index
->name
->content
);
373 irc_send("%s L %s :%s",longtonumeric(np
->numeric
,5),cp
->index
->name
->content
, reason
);
375 irc_send("%s L %s",longtonumeric(np
->numeric
,5),cp
->index
->name
->content
);
381 triggerhook(HOOK_CHANNEL_PART
,harg
);
383 /* Now leave the channel */
384 delnickfromchannel(cp
,np
->numeric
,1);
389 int localcreatechannel(nick
*np
, char *channame
) {
392 /* Check that the user _is_ a local one.. */
393 if (homeserver(np
->numeric
)!=mylongnum
) {
397 if ((cp
=findchannel(channame
))!=NULL
) {
402 cp
=createchannel(channame
);
403 cp
->timestamp
=getnettime();
405 /* Add the local user to the channel, preopped */
406 addnicktochannel(cp
,(np
->numeric
)|CUMODE_OP
);
409 irc_send("%s C %s %ld",longtonumeric(np
->numeric
,5),cp
->index
->name
->content
,cp
->timestamp
);
412 triggerhook(HOOK_CHANNEL_NEWCHANNEL
,(void *)cp
);
416 int localgetops(nick
*np
, channel
*cp
) {
419 /* Check that the user _is_ a local one.. */
420 if (homeserver(np
->numeric
)!=mylongnum
) {
424 if ((lp
=getnumerichandlefromchanhash(cp
->users
,np
->numeric
))==NULL
) {
428 if (*lp
& CUMODE_OP
) {
437 irc_send("%s M %s +o %s",mynumeric
->content
,cp
->index
->name
->content
,longtonumeric(np
->numeric
,5));
443 int localgetvoice(nick
*np
, channel
*cp
) {
446 /* Check that the user _is_ a local one.. */
447 if (homeserver(np
->numeric
)!=mylongnum
) {
451 if ((lp
=getnumerichandlefromchanhash(cp
->users
,np
->numeric
))==NULL
) {
455 if (*lp
& CUMODE_VOICE
) {
464 irc_send("%s M %s +v %s",mynumeric
->content
,cp
->index
->name
->content
,longtonumeric(np
->numeric
,5));
472 * Initialises the modechanges structure.
475 void localsetmodeinit (modechanges
*changes
, channel
*cp
, nick
*np
) {
478 changes
->changecount
=0;
484 * localdosetmode_ban:
485 * Set or clear a ban on the channel
488 void localdosetmode_ban (modechanges
*changes
, const char *ban
, short dir
) {
489 sstring
*bansstr
=getsstring(ban
,HOSTLEN
+NICKLEN
+USERLEN
+5);
491 /* If we're told to clear a ban that isn't here, do nothing. */
492 if (dir
==MCB_DEL
&& !clearban(changes
->cp
, bansstr
->content
, 1))
495 /* Similarly if someone is trying to add a completely overlapped ban, do
497 if (dir
==MCB_ADD
&& !setban(changes
->cp
, bansstr
->content
))
500 if (changes
->changecount
>= MAXMODEARGS
)
501 localsetmodeflush(changes
, 0);
503 changes
->changes
[changes
->changecount
].str
=bansstr
;
504 changes
->changes
[changes
->changecount
].flag
='b';
505 changes
->changes
[changes
->changecount
++].dir
=dir
;
508 setban(changes
->cp
, bansstr
->content
);
513 * localdosetmode_key:
514 * Set or clear a key on the channel
517 void localdosetmode_key (modechanges
*changes
, char *key
, short dir
) {
521 /* Check we have space in the buffer */
522 if (changes
->changecount
>= MAXMODEARGS
)
523 localsetmodeflush(changes
,0);
526 /* Sanitise the key. If this nullifies it then we give up */
531 /* Get a copy of the key for use later */
532 keysstr
=getsstring(key
, KEYLEN
);
534 /* Check there isn't a key set/clear in the pipeline already.. */
535 for (i
=0;i
<changes
->changecount
;i
++) {
536 if (changes
->changes
[i
].flag
=='k') {
537 /* There's a change already.. */
538 if (changes
->changes
[i
].dir
==MCB_ADD
) {
539 /* Already an add key change. Here, we just replace the key
540 * we were going to add with this one. Note that we need to
541 * free the current cp->key, and the changes.str */
542 freesstring(changes
->changes
[i
].str
);
543 changes
->changes
[i
].str
=keysstr
;
544 freesstring(changes
->cp
->key
);
545 changes
->cp
->key
=getsstring(key
, KEYLEN
);
546 /* That's it, we're done */
547 freesstring(keysstr
);
550 /* There was a command to delete key.. we need to flush
551 * this out then add the new key (-k+k isn't valid).
552 * Make sure this gets flushed, then drop through to common code */
553 localsetmodeflush(changes
, 1);
558 /* We got here, so there's no change pending already .. check for key on chan */
559 if (IsKey(changes
->cp
)) {
560 if (sstringcompare(changes
->cp
->key
, keysstr
)) {
561 /* Key is set and different. Need to put -k in and flush changes now */
562 changes
->changes
[changes
->changecount
].str
=changes
->cp
->key
; /* implicit free */
563 changes
->changes
[changes
->changecount
].dir
=MCB_DEL
;
564 changes
->changes
[changes
->changecount
++].flag
='k';
565 localsetmodeflush(changes
, 1); /* Note that this will free the sstring on the channel */
567 /* Key is set and the same: do nothing. */
568 freesstring(keysstr
); /* Don't need this after all */
573 /* If we get here, there's no key on the channel right now and nothing in the buffer to
574 * add or remove one */
576 changes
->cp
->key
=getsstring(key
, KEYLEN
);
578 changes
->changes
[changes
->changecount
].str
=keysstr
;
579 changes
->changes
[changes
->changecount
].dir
=MCB_ADD
;
580 changes
->changes
[changes
->changecount
++].flag
='k';
582 /* We're removing a key.. */
583 /* Only bother if key is set atm */
584 if (IsKey(changes
->cp
)) {
585 ClearKey(changes
->cp
);
586 for(i
=0;i
<changes
->changecount
;i
++) {
587 if (changes
->changes
[i
].flag
=='k') {
588 /* We were already doing something with a key..
589 * it MUST be adding one */
590 assert(changes
->changes
[i
].dir
==MCB_ADD
);
591 /* Just forget the earlier change */
592 freesstring(changes
->changes
[i
].str
);
593 changes
->changecount
--;
594 for (j
=i
;j
<changes
->changecount
;j
++) {
595 changes
->changes
[j
]=changes
->changes
[j
+1];
597 /* Explicitly free key on chan */
598 freesstring(changes
->cp
->key
);
599 changes
->cp
->key
=NULL
;
604 /* We didn't hit a key change, so put a remove command in */
605 changes
->changes
[changes
->changecount
].str
=changes
->cp
->key
; /* implicit free */
606 changes
->cp
->key
=NULL
;
607 changes
->changes
[changes
->changecount
].dir
=MCB_DEL
;
608 changes
->changes
[changes
->changecount
++].flag
='k';
613 void localdosetmode_limit (modechanges
*changes
, unsigned int limit
, short dir
) {
618 localdosetmode_simple(changes
, 0, CHANMODE_LIMIT
);
622 /* Kill redundant changes */
623 if ((IsLimit(changes
->cp
) && changes
->cp
->limit
==limit
) || !limit
)
626 SetLimit(changes
->cp
);
627 changes
->cp
->limit
=limit
;
628 changes
->delflags
&= ~CHANMODE_LIMIT
;
630 /* Check for existing limit add */
631 for (i
=0;i
<changes
->changecount
;i
++) {
632 if (changes
->changes
[i
].flag
=='l') {
634 freesstring(changes
->changes
[i
].str
);
635 sprintf(buf
,"%u",limit
);
636 changes
->changes
[i
].str
=getsstring(buf
,20);
641 /* None, add new one. Note that we can do +l even if a limit is already set */
642 if (changes
->changecount
>= MAXMODEARGS
)
643 localsetmodeflush(changes
,0);
645 changes
->changes
[changes
->changecount
].flag
='l';
646 sprintf(buf
,"%u",limit
);
647 changes
->changes
[changes
->changecount
].str
=getsstring(buf
,20);
648 changes
->changes
[changes
->changecount
++].dir
=MCB_ADD
;
651 void localdosetmode_simple (modechanges
*changes
, flag_t addmodes
, flag_t delmodes
) {
654 /* We can't add a mode we're deleting, a key, limit, or mode that's already set */
655 addmodes
&= ~(delmodes
| CHANMODE_KEY
| CHANMODE_LIMIT
| changes
->cp
->flags
);
657 /* We can't delete a key or a mode that's not set */
658 delmodes
&= (~(CHANMODE_KEY
) & changes
->cp
->flags
);
660 /* If we're doing +p, do -s as well, and vice versa */
661 /* Also disallow +ps */
662 if (addmodes
& CHANMODE_SECRET
) {
663 addmodes
&= ~(CHANMODE_PRIVATE
);
664 delmodes
|= (CHANMODE_PRIVATE
& changes
->cp
->flags
);
667 if (addmodes
& CHANMODE_PRIVATE
) {
668 delmodes
|= (CHANMODE_SECRET
& changes
->cp
->flags
);
671 /* Fold changes into channel */
672 changes
->cp
->flags
|= addmodes
;
673 changes
->cp
->flags
&= ~delmodes
;
675 if (delmodes
& CHANMODE_LIMIT
) {
676 /* Check for +l in the parametered changes */
677 for (i
=0;i
<changes
->changecount
;i
++) {
678 if (changes
->changes
[i
].flag
=='l') {
679 freesstring(changes
->changes
[i
].str
);
680 changes
->changecount
--;
681 for (j
=0;j
<changes
->changecount
;j
++) {
682 changes
->changes
[j
]=changes
->changes
[j
+1];
687 changes
->cp
->limit
=0;
690 /* And into the changes buffer */
691 changes
->addflags
&= ~delmodes
;
692 changes
->addflags
|= addmodes
;
694 changes
->delflags
&= ~addmodes
;
695 changes
->delflags
|= delmodes
;
700 * Applies a mode change.
703 void localdosetmode_nick (modechanges
*changes
, nick
*target
, short modes
) {
706 if ((lp
=getnumerichandlefromchanhash(changes
->cp
->users
,target
->numeric
))==NULL
) {
707 /* Target isn't on channel, abort */
711 if ((modes
& MC_DEOP
) && (*lp
& CUMODE_OP
)) {
713 if (changes
->changecount
>= MAXMODEARGS
)
714 localsetmodeflush(changes
, 0);
715 changes
->changes
[changes
->changecount
].str
=getsstring(longtonumeric(target
->numeric
,5),5);
716 changes
->changes
[changes
->changecount
].dir
=MCB_DEL
;
717 changes
->changes
[changes
->changecount
++].flag
='o';
720 if ((modes
& MC_DEVOICE
) && (*lp
& CUMODE_VOICE
)) {
721 (*lp
) &= ~CUMODE_VOICE
;
722 if (changes
->changecount
>= MAXMODEARGS
)
723 localsetmodeflush(changes
, 0);
724 changes
->changes
[changes
->changecount
].str
=getsstring(longtonumeric(target
->numeric
,5),5);
725 changes
->changes
[changes
->changecount
].dir
=MCB_DEL
;
726 changes
->changes
[changes
->changecount
++].flag
='v';
729 if ((modes
& MC_OP
) && !(modes
& MC_DEOP
) && !(*lp
& CUMODE_OP
)) {
731 if (changes
->changecount
>= MAXMODEARGS
)
732 localsetmodeflush(changes
, 0);
733 changes
->changes
[changes
->changecount
].str
=getsstring(longtonumeric(target
->numeric
,5),5);
734 changes
->changes
[changes
->changecount
].dir
=MCB_ADD
;
735 changes
->changes
[changes
->changecount
++].flag
='o';
738 if ((modes
& MC_VOICE
) && !(modes
& MC_DEVOICE
) && !(*lp
& CUMODE_VOICE
)) {
739 (*lp
) |= CUMODE_VOICE
;
740 if (changes
->changecount
>= MAXMODEARGS
)
741 localsetmodeflush(changes
, 0);
742 changes
->changes
[changes
->changecount
].str
=getsstring(longtonumeric(target
->numeric
,5),5);
743 changes
->changes
[changes
->changecount
].dir
=MCB_ADD
;
744 changes
->changes
[changes
->changecount
++].flag
='v';
751 * Sends out mode changes to the network.
754 void localsetmodeflush (modechanges
*changes
, int flushall
) {
769 strcpy(addmodes
, printflags_noprefix(changes
->addflags
, cmodeflags
));
770 ampos
=strlen(addmodes
);
772 strcpy(remmodes
, printflags_noprefix(changes
->delflags
, cmodeflags
));
773 rmpos
=strlen(remmodes
);
775 changes
->addflags
=changes
->delflags
=0;
777 for (i
=0;i
<changes
->changecount
;i
++) {
778 /* Don't overflow the string, kinda nasty to work out.. format is: */
779 /* AAAA M #chan +add-rem (addstr) (remstr) */
780 if ((changes
->cp
->index
->name
->length
+ aapos
+ rapos
+
781 ampos
+ rmpos
+ changes
->changes
[i
].str
->length
+ 20) > BUFSIZE
)
784 switch (changes
->changes
[i
].dir
) {
786 addmodes
[ampos
++]=changes
->changes
[i
].flag
;
787 aapos
+=sprintf(addargs
+aapos
, "%s ", changes
->changes
[i
].str
->content
);
791 remmodes
[rmpos
++]=changes
->changes
[i
].flag
;
792 rapos
+=sprintf(remargs
+rapos
, "%s ", changes
->changes
[i
].str
->content
);
795 freesstring(changes
->changes
[i
].str
);
798 if (i
<changes
->changecount
) {
799 for (j
=i
;j
<changes
->changecount
;j
++)
800 changes
->changes
[j
-i
]=changes
->changes
[j
];
803 changes
->changecount
-= i
;
805 if ((ampos
+rmpos
)==0) {
810 addmodes
[ampos
]='\0';
811 remmodes
[rmpos
]='\0';
815 if (changes
->source
==NULL
||
816 (lp
=getnumerichandlefromchanhash(changes
->cp
->users
,changes
->source
->numeric
))==NULL
) {
817 /* User isn't on channel, hack mode */
818 strcpy(source
,mynumeric
->content
);
820 /* Check the user is local */
821 if (homeserver(changes
->source
->numeric
)!=mylongnum
) {
824 if ((*lp
&CUMODE_OP
)==0) {
825 localgetops(changes
->source
,changes
->cp
);
827 strcpy(source
,longtonumeric(changes
->source
->numeric
,5));
831 irc_send("%s M %s %s%s%s%s %s%s",source
,changes
->cp
->index
->name
->content
,
832 rmpos
? "-" : "", remmodes
,
833 ampos
? "+" : "", addmodes
, remargs
, addargs
);
836 /* If we have to flush everything out but didn't finish, go round again */
837 if (changes
->changecount
&& flushall
)
838 localsetmodeflush(changes
, 1);
843 * Sets modes for the user on the channel. This is now a stub routine that
844 * uses the new functions.
847 int localsetmodes(nick
*np
, channel
*cp
, nick
*target
, short modes
) {
850 localsetmodeinit (&changes
, cp
, np
);
851 localdosetmode_nick (&changes
, target
, modes
);
852 localsetmodeflush (&changes
, 1);
857 /* This function just sends the actual mode change,
858 * it assumes that the actual channel modes have been changed appropriately.
859 * This is unfortunately inconsistent with the rest of the localuser API..
862 void localusermodechange(nick
*np
, channel
*cp
, char *modes
) {
866 if (np
==NULL
|| (lp
=getnumerichandlefromchanhash(cp
->users
,np
->numeric
))==NULL
) {
867 /* User isn't on channel, hack mode */
868 strcpy(source
,mynumeric
->content
);
870 /* Check the user is local */
871 if (homeserver(np
->numeric
)!=mylongnum
) {
874 if ((*lp
&CUMODE_OP
)==0) {
877 strcpy(source
,longtonumeric(np
->numeric
,5));
881 irc_send("%s M %s %s",source
,cp
->index
->name
->content
,modes
);
885 /* This function actually sets the topic itself though.. a bit inconsistent :/ */
887 void localsettopic(nick
*np
, channel
*cp
, char *topic
) {
891 if (np
==NULL
|| (lp
=getnumerichandlefromchanhash(cp
->users
,np
->numeric
))==NULL
) {
892 /* User isn't on channel, hack mode */
893 strcpy(source
,mynumeric
->content
);
895 /* Check the user is local */
896 if (homeserver(np
->numeric
)!=mylongnum
) {
899 if ((*lp
&CUMODE_OP
)==0 && IsTopicLimit(cp
)) {
902 strcpy(source
,longtonumeric(np
->numeric
,5));
906 freesstring(cp
->topic
);
909 cp
->topic
=getsstring(topic
,TOPICLEN
);
910 cp
->topictime
=getnettime();
913 irc_send("%s T %s %u %u :%s",source
,cp
->index
->name
->content
,cp
->timestamp
,cp
->topictime
,(cp
->topic
)?cp
->topic
->content
:"");
917 void localkickuser(nick
*np
, channel
*cp
, nick
*target
, const char *message
) {
920 if (hookqueuelength
) {
921 for (pk
= pendingkicklist
; pk
; pk
= pk
->next
)
922 if (pk
->target
== target
&& pk
->chan
== cp
)
925 Error("localuserchannel", ERR_DEBUG
, "Adding pending kick for %s on %s", target
->nick
, cp
->index
->name
->content
);
926 pk
= (pendingkick
*)malloc(sizeof(pendingkick
));
930 pk
->reason
= getsstring(message
, BUFSIZE
);
931 pk
->next
= pendingkicklist
;
932 pendingkicklist
= pk
;
934 _localkickuser(np
, cp
, target
, message
);
938 void _localkickuser(nick
*np
, channel
*cp
, nick
*target
, const char *message
) {
942 if (np
==NULL
|| (lp
=getnumerichandlefromchanhash(cp
->users
,np
->numeric
))==NULL
) {
943 /* User isn't on channel, hack mode */
944 strcpy(source
,mynumeric
->content
);
946 /* Check the user is local */
947 if (homeserver(np
->numeric
)!=mylongnum
) {
950 if ((*lp
&CUMODE_OP
)==0) {
953 strcpy(source
,longtonumeric(np
->numeric
,5));
956 if ((lp
=getnumerichandlefromchanhash(cp
->users
,target
->numeric
))==NULL
)
959 /* Send the message to the network first in case delnickfromchannel()
960 * destroys the channel.. */
962 irc_send("%s K %s %s :%s",source
,cp
->index
->name
->content
,
963 longtonumeric(target
->numeric
,5), message
);
966 delnickfromchannel(cp
, target
->numeric
, 1);
969 void clearpendingkicks(int hooknum
, void *arg
) {
972 pk
= pendingkicklist
;
974 pendingkicklist
= pk
->next
;
976 if (pk
->target
&& pk
->chan
) {
977 Error("localuserchannel", ERR_DEBUG
, "Processing pending kick for %s on %s", pk
->target
->nick
, pk
->chan
->index
->name
->content
);
978 _localkickuser(pk
->source
, pk
->chan
, pk
->target
, pk
->reason
->content
);
981 freesstring(pk
->reason
);
983 pk
= pendingkicklist
;
987 void checkpendingkicknicks(int hooknum
, void *arg
) {
988 nick
*np
= (nick
*)arg
;
991 for (pk
=pendingkicklist
; pk
; pk
= pk
->next
) {
992 if (pk
->source
== np
) {
993 Error("localuserchannel", ERR_INFO
, "Pending kick source %s got deleted, NULL'ing source for pending kick", np
->nick
);
996 if (pk
->target
== np
) {
997 Error("localuserchannel", ERR_INFO
, "Pending kick target %s got deleted, NULL'ing target for pending kick", np
->nick
);
1003 void checkpendingkickchannels(int hooknum
, void *arg
) {
1004 channel
*cp
= (channel
*)arg
;
1007 for (pk
=pendingkicklist
; pk
; pk
= pk
->next
) {
1008 if (pk
->chan
== cp
) {
1009 Error("localuserchannel", ERR_INFO
, "Pending kick channel %s got deleted, NULL'ing channel for pending kick", cp
->index
->name
->content
);
1015 void sendmessagetochannel(nick
*source
, channel
*cp
, char *format
, ... ) {
1023 longtonumeric2(source
->numeric
,5,senderstr
);
1025 va_start(va
,format
);
1026 /* 10 bytes of numeric, 5 bytes of fixed format + terminator = 17 bytes */
1027 /* So max sendable message is 495 bytes. Of course, a client won't be able
1028 * to receive this.. */
1030 vsnprintf(buf
,BUFSIZE
-17,format
,va
);
1034 irc_send("%s P %s :%s",senderstr
,cp
->index
->name
->content
,buf
);
1038 void sendopnoticetochannel(nick
*source
, channel
*cp
, char *format
, ... ) {
1046 longtonumeric2(source
->numeric
,5,senderstr
);
1048 va_start(va
,format
);
1049 /* 10 bytes of numeric, 5 bytes of fixed format + terminator = 17 bytes */
1050 /* So max sendable message is 495 bytes. Of course, a client won't be able
1051 * to receive this.. */
1053 vsnprintf(buf
,BUFSIZE
-17,format
,va
);
1057 irc_send("%s WC %s :%s",senderstr
,cp
->index
->name
->content
,buf
);
1061 void localinvite(nick
*source
, channel
*cp
, nick
*target
) {
1063 /* Servers can't send invites */
1067 /* CHECK: Does the sender have to be on the relevant channel? */
1069 /* For some reason invites are sent with the target nick as
1072 irc_send("%s I %s :%s",longtonumeric(source
->numeric
,5),
1073 target
->nick
, cp
->index
->name
->content
);