2 * ircd-ratbox: A slightly useful ircd.
3 * m_sjoin.c: Joins a user to a channel.
5 * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
6 * Copyright (C) 1996-2002 Hybrid Development Team
7 * Copyright (C) 2002-2005 ircd-ratbox development team
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
24 * $Id: m_sjoin.c 3434 2007-04-28 23:47:25Z jilles $
31 #include "irc_string.h"
32 #include "sprintf_irc.h"
43 static int ms_sjoin(struct Client
*, struct Client
*, int, const char **);
45 struct Message sjoin_msgtab
= {
46 "SJOIN", 0, 0, 0, MFLG_SLOW
,
47 {mg_unreg
, mg_ignore
, mg_ignore
, {ms_sjoin
, 0}, mg_ignore
, mg_ignore
}
50 mapi_clist_av1 sjoin_clist
[] = { &sjoin_msgtab
, NULL
};
52 DECLARE_MODULE_AV1(sjoin
, NULL
, NULL
, sjoin_clist
, NULL
, NULL
, "$Revision: 3434 $");
59 * parv[3] - modes + n arguments (key and/or limit)
60 * parv[4+n] - flags+nick list (all in one parameter)
62 * process a SJOIN, taking the TS's into account to either ignore the
63 * incoming modes or undo the existing ones or merge them, and JOIN
64 * all the specified users while sending JOIN/MODEs to local clients
68 static char modebuf
[MODEBUFLEN
];
69 static char parabuf
[MODEBUFLEN
];
70 static const char *para
[MAXMODEPARAMS
];
74 static void set_final_mode(struct Mode
*mode
, struct Mode
*oldmode
);
75 static void remove_our_modes(struct Channel
*chptr
, struct Client
*source_p
);
76 static void remove_ban_list(struct Channel
*chptr
, struct Client
*source_p
,
77 rb_dlink_list
* list
, char c
, int cap
, int mems
);
80 ms_sjoin(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
82 static char buf_nick
[BUFSIZE
];
83 static char buf_uid
[BUFSIZE
];
84 static const char empty_modes
[] = "0";
85 struct Channel
*chptr
;
86 struct Client
*target_p
, *fakesource_p
;
89 static struct Mode mode
, *oldmode
;
92 int keep_our_modes
= 1;
93 int keep_new_modes
= 1;
96 int mlen_nick
, mlen_uid
;
105 int i
, joinc
= 0, timeslice
= 0;
106 static char empty
[] = "";
107 rb_dlink_node
*ptr
, *next_ptr
;
109 if(!IsChannelName(parv
[2]) || !check_channel_name(parv
[2]))
112 /* SJOIN's for local channels can't happen. */
116 modebuf
[0] = parabuf
[0] = mode
.key
[0] = mode
.forward
[0] = '\0';
117 pargs
= mode
.mode
= mode
.limit
= mode
.join_num
= mode
.join_time
= 0;
119 /* Hide connecting server on netburst -- jilles */
120 if (ConfigServerHide
.flatten_links
&& !HasSentEob(source_p
))
123 fakesource_p
= source_p
;
126 newts
= atol(parv
[1]);
134 mode
.mode
|= MODE_INVITEONLY
;
137 mode
.mode
|= MODE_NOPRIVMSGS
;
140 mode
.mode
|= MODE_PRIVATE
;
143 mode
.mode
|= MODE_SECRET
;
146 mode
.mode
|= MODE_MODERATED
;
149 mode
.mode
|= MODE_TOPICLIMIT
;
152 mode
.mode
|= MODE_REGONLY
;
155 mode
.mode
|= MODE_EXLIMIT
;
158 mode
.mode
|= MODE_PERMANENT
;
161 mode
.mode
|= MODE_NOCOLOR
;
164 mode
.mode
|= MODE_FREEINVITE
;
167 mode
.mode
|= MODE_OPMODERATE
;
170 mode
.mode
|= MODE_FREETARGET
;
173 mode
.mode
|= MODE_DISFORWARD
;
176 strlcpy(mode
.forward
, parv
[4 + args
], sizeof(mode
.forward
));
182 sscanf(parv
[4 + args
], "%d:%d", &joinc
, ×lice
);
184 mode
.join_num
= joinc
;
185 mode
.join_time
= timeslice
;
190 strlcpy(mode
.key
, parv
[4 + args
], sizeof(mode
.key
));
196 mode
.limit
= atoi(parv
[4 + args
]);
208 /* remove any leading spaces */
215 if((chptr
= get_or_create_channel(source_p
, parv
[2], &isnew
)) == NULL
)
216 return 0; /* channel name too long? */
219 oldts
= chptr
->channelts
;
220 oldmode
= &chptr
->mode
;
222 #ifdef IGNORE_BOGUS_TS
223 if(newts
< 800000000)
225 sendto_realops_snomask(SNO_DEBUG
, L_ALL
,
226 "*** Bogus TS %ld on %s ignored from %s",
227 (long) newts
, chptr
->chname
, client_p
->name
);
229 newts
= (oldts
== 0) ? oldts
: 800000000;
232 if(!isnew
&& !newts
&& oldts
)
234 sendto_channel_local(ALL_MEMBERS
, chptr
,
235 ":%s NOTICE %s :*** Notice -- TS for %s "
236 "changed from %ld to 0",
237 me
.name
, chptr
->chname
, chptr
->chname
, (long) oldts
);
238 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
239 "Server %s changing TS on %s from %ld to 0",
240 source_p
->name
, chptr
->chname
, (long) oldts
);
245 chptr
->channelts
= newts
;
247 else if(newts
== 0 || oldts
== 0)
248 chptr
->channelts
= 0;
249 else if(newts
== oldts
)
251 else if(newts
< oldts
)
253 /* If configured, kick people trying to join +i/+k
254 * channels by recreating them on split servers.
255 * Don't kick if the source has sent EOB (services
256 * deopping everyone by TS-1 SJOIN).
258 if (ConfigChannel
.kick_on_split_riding
&&
259 !HasSentEob(source_p
) &&
260 ((mode
.mode
& MODE_INVITEONLY
) ||
261 (mode
.key
[0] != 0 && irccmp(mode
.key
, oldmode
->key
) != 0)))
263 struct membership
*msptr
;
265 int l
= rb_dlink_list_length(&chptr
->members
);
266 int b
= rb_dlink_list_length(&chptr
->banlist
) +
267 rb_dlink_list_length(&chptr
->exceptlist
) +
268 rb_dlink_list_length(&chptr
->invexlist
) +
269 rb_dlink_list_length(&chptr
->quietlist
);
271 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, chptr
->locmembers
.head
)
274 who
= msptr
->client_p
;
275 sendto_one(who
, ":%s KICK %s %s :Net Rider",
276 me
.name
, chptr
->chname
, who
->name
);
278 sendto_server(NULL
, chptr
, CAP_TS6
, NOCAPS
,
279 ":%s KICK %s %s :Net Rider",
280 me
.id
, chptr
->chname
,
282 sendto_server(NULL
, chptr
, NOCAPS
, CAP_TS6
,
283 ":%s KICK %s %s :Net Rider",
284 me
.name
, chptr
->chname
, who
->name
);
285 remove_user_from_channel(msptr
);
291 /* Channel was emptied, create a new one */
292 if((chptr
= get_or_create_channel(source_p
, parv
[2], &isnew
)) == NULL
)
293 return 0; /* oops! */
295 /* If the source does not do TS6,
296 * nontimestamped bans have been sent to it,
297 * but we have just lost those here. Let's
298 * warn the channel about this. Because
299 * of the kicks, any users on the channel
300 * will be at client_p. -- jilles */
301 if (!has_id(source_p
) && b
> 0)
302 sendto_one(client_p
, ":%s NOTICE %s :*** Notice -- possible ban desync on %s, please remove any bans just added by servers", get_id(&me
, client_p
), parv
[2], parv
[2]);
303 oldmode
= &chptr
->mode
;
307 chptr
->channelts
= newts
;
314 else if(keep_our_modes
)
316 mode
.mode
|= oldmode
->mode
;
317 if(oldmode
->limit
> mode
.limit
)
318 mode
.limit
= oldmode
->limit
;
319 if(strcmp(mode
.key
, oldmode
->key
) < 0)
320 strcpy(mode
.key
, oldmode
->key
);
321 if(oldmode
->join_num
> mode
.join_num
||
322 (oldmode
->join_num
== mode
.join_num
&&
323 oldmode
->join_time
> mode
.join_time
))
325 mode
.join_num
= oldmode
->join_num
;
326 mode
.join_time
= oldmode
->join_time
;
328 if(irccmp(mode
.forward
, oldmode
->forward
) < 0)
329 strcpy(mode
.forward
, oldmode
->forward
);
333 /* If setting -j, clear join throttle state -- jilles */
335 chptr
->join_count
= chptr
->join_delta
= 0;
338 set_final_mode(&mode
, oldmode
);
341 /* Lost the TS, other side wins, so remove modes on this side */
344 remove_our_modes(chptr
, fakesource_p
);
345 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, chptr
->invites
.head
)
347 del_invite(chptr
, ptr
->data
);
349 sendto_channel_local(ALL_MEMBERS
, chptr
,
350 ":%s NOTICE %s :*** Notice -- TS for %s changed from %ld to %ld",
351 me
.name
, chptr
->chname
, chptr
->chname
,
352 (long) oldts
, (long) newts
);
353 /* Update capitalization in channel name, this makes the
354 * capitalization timestamped like modes are -- jilles */
355 strcpy(chptr
->chname
, parv
[2]);
359 sendto_channel_local(ALL_MEMBERS
, chptr
, ":%s MODE %s %s %s",
360 fakesource_p
->name
, chptr
->chname
, modebuf
, parabuf
);
362 *modebuf
= *parabuf
= '\0';
364 if(parv
[3][0] != '0' && keep_new_modes
)
365 modes
= channel_modes(chptr
, source_p
);
369 mlen_nick
= rb_sprintf(buf_nick
, ":%s SJOIN %ld %s %s :",
370 source_p
->name
, (long) chptr
->channelts
, parv
[2], modes
);
371 ptr_nick
= buf_nick
+ mlen_nick
;
373 /* working on the presumption eventually itll be more efficient to
374 * build a TS6 buffer without checking its needed..
376 mlen_uid
= rb_sprintf(buf_uid
, ":%s SJOIN %ld %s %s :",
377 use_id(source_p
), (long) chptr
->channelts
, parv
[2], modes
);
378 ptr_uid
= buf_uid
+ mlen_uid
;
381 para
[0] = para
[1] = para
[2] = para
[3] = empty
;
383 len_nick
= len_uid
= 0;
385 /* if theres a space, theres going to be more than one nick, change the
386 * first space to \0, so s is just the first nick, and point p to the
389 if((p
= strchr(s
, ' ')) != NULL
)
400 for (i
= 0; i
< 2; i
++)
414 /* if the client doesnt exist or is fake direction, skip. */
415 if(!(target_p
= find_client(s
)) ||
416 (target_p
->from
!= client_p
) || !IsPerson(target_p
))
419 /* we assume for these we can fit at least one nick/uid in.. */
421 /* check we can fit another status+nick+space into a buffer */
422 if((mlen_nick
+ len_nick
+ NICKLEN
+ 3) > (BUFSIZE
- 3))
424 *(ptr_nick
- 1) = '\0';
425 sendto_server(client_p
->from
, NULL
, NOCAPS
, CAP_TS6
, "%s", buf_nick
);
426 ptr_nick
= buf_nick
+ mlen_nick
;
430 if((mlen_uid
+ len_uid
+ IDLEN
+ 3) > (BUFSIZE
- 3))
432 *(ptr_uid
- 1) = '\0';
433 sendto_server(client_p
->from
, NULL
, CAP_TS6
, NOCAPS
, "%s", buf_uid
);
434 ptr_uid
= buf_uid
+ mlen_uid
;
456 /* copy the nick to the two buffers */
457 len
= rb_sprintf(ptr_nick
, "%s ", target_p
->name
);
460 len
= rb_sprintf(ptr_uid
, "%s ", use_id(target_p
));
467 if(!IsMember(target_p
, chptr
))
469 add_user_to_channel(chptr
, target_p
, fl
);
470 sendto_channel_local(ALL_MEMBERS
, chptr
, ":%s!%s@%s JOIN :%s",
472 target_p
->username
, target_p
->host
, parv
[2]);
479 para
[pargs
++] = target_p
->name
;
481 /* a +ov user.. bleh */
484 /* its possible the +o has filled up MAXMODEPARAMS, if so, start
487 if(pargs
>= MAXMODEPARAMS
)
490 sendto_channel_local(ALL_MEMBERS
, chptr
,
491 ":%s MODE %s %s %s %s %s %s",
492 fakesource_p
->name
, chptr
->chname
,
494 para
[0], para
[1], para
[2], para
[3]);
497 para
[0] = para
[1] = para
[2] = para
[3] = NULL
;
502 para
[pargs
++] = target_p
->name
;
505 else if(fl
& CHFL_VOICE
)
508 para
[pargs
++] = target_p
->name
;
511 if(pargs
>= MAXMODEPARAMS
)
514 sendto_channel_local(ALL_MEMBERS
, chptr
,
515 ":%s MODE %s %s %s %s %s %s",
518 modebuf
, para
[0], para
[1], para
[2], para
[3]);
521 para
[0] = para
[1] = para
[2] = para
[3] = NULL
;
526 /* p points to the next nick */
529 /* if there was a trailing space and p was pointing to it, then we
530 * need to exit.. this has the side effect of breaking double spaces
531 * in an sjoin.. but that shouldnt happen anyway
533 if(s
&& (*s
== '\0'))
536 /* if p was NULL due to no spaces, s wont exist due to the above, so
537 * we cant check it for spaces.. if there are no spaces, then when
538 * we next get here, s will be NULL
540 if(s
&& ((p
= strchr(s
, ' ')) != NULL
))
549 sendto_channel_local(ALL_MEMBERS
, chptr
,
550 ":%s MODE %s %s %s %s %s %s",
551 fakesource_p
->name
, chptr
->chname
, modebuf
,
552 para
[0], CheckEmpty(para
[1]),
553 CheckEmpty(para
[2]), CheckEmpty(para
[3]));
556 if(!joins
&& !(chptr
->mode
.mode
& MODE_PERMANENT
) && isnew
)
558 destroy_channel(chptr
);
563 /* Keep the colon if we're sending an SJOIN without nicks -- jilles */
566 *(ptr_nick
- 1) = '\0';
567 *(ptr_uid
- 1) = '\0';
570 sendto_server(client_p
->from
, NULL
, CAP_TS6
, NOCAPS
, "%s", buf_uid
);
571 sendto_server(client_p
->from
, NULL
, NOCAPS
, CAP_TS6
, "%s", buf_nick
);
573 /* if the source does TS6 we have to remove our bans. Its now safe
574 * to issue -b's to the non-ts6 servers, as the sjoin we've just
575 * sent will kill any ops they have.
577 if(!keep_our_modes
&& source_p
->id
[0] != '\0')
579 if(rb_dlink_list_length(&chptr
->banlist
) > 0)
580 remove_ban_list(chptr
, fakesource_p
, &chptr
->banlist
, 'b', NOCAPS
, ALL_MEMBERS
);
582 if(rb_dlink_list_length(&chptr
->exceptlist
) > 0)
583 remove_ban_list(chptr
, fakesource_p
, &chptr
->exceptlist
,
584 'e', CAP_EX
, ONLY_CHANOPS
);
586 if(rb_dlink_list_length(&chptr
->invexlist
) > 0)
587 remove_ban_list(chptr
, fakesource_p
, &chptr
->invexlist
,
588 'I', CAP_IE
, ONLY_CHANOPS
);
590 if(rb_dlink_list_length(&chptr
->quietlist
) > 0)
591 remove_ban_list(chptr
, fakesource_p
, &chptr
->quietlist
,
592 'q', NOCAPS
, ALL_MEMBERS
);
601 set_final_mode(struct Mode
*mode
, struct Mode
*oldmode
)
603 int dir
= MODE_QUERY
;
604 char *pbuf
= parabuf
;
608 /* ok, first get a list of modes we need to add */
609 for (i
= 0; chmode_flags
[i
].letter
; i
++)
611 if((mode
->mode
& chmode_flags
[i
].mode
) && !(oldmode
->mode
& chmode_flags
[i
].mode
))
618 *mbuf
++ = chmode_flags
[i
].letter
;
622 /* now the ones we need to remove. */
623 for (i
= 0; chmode_flags
[i
].letter
; i
++)
625 if((oldmode
->mode
& chmode_flags
[i
].mode
) && !(mode
->mode
& chmode_flags
[i
].mode
))
632 *mbuf
++ = chmode_flags
[i
].letter
;
636 if(oldmode
->limit
&& !mode
->limit
)
645 if(oldmode
->key
[0] && !mode
->key
[0])
653 len
= rb_sprintf(pbuf
, "%s ", oldmode
->key
);
657 if(oldmode
->join_num
&& !mode
->join_num
)
666 if(oldmode
->forward
[0] && !mode
->forward
[0])
675 if(mode
->limit
&& oldmode
->limit
!= mode
->limit
)
683 len
= rb_sprintf(pbuf
, "%d ", mode
->limit
);
687 if(mode
->key
[0] && strcmp(oldmode
->key
, mode
->key
))
695 len
= rb_sprintf(pbuf
, "%s ", mode
->key
);
699 if(mode
->join_num
&& (oldmode
->join_num
!= mode
->join_num
|| oldmode
->join_time
!= mode
->join_time
))
707 len
= rb_sprintf(pbuf
, "%d:%d ", mode
->join_num
, mode
->join_time
);
711 if(mode
->forward
[0] && strcmp(oldmode
->forward
, mode
->forward
) && ConfigChannel
.use_forward
)
719 len
= rb_sprintf(pbuf
, "%s ", mode
->forward
);
734 remove_our_modes(struct Channel
*chptr
, struct Client
*source_p
)
736 struct membership
*msptr
;
738 char lmodebuf
[MODEBUFLEN
];
739 char *lpara
[MAXMODEPARAMS
];
746 for (i
= 0; i
< MAXMODEPARAMS
; i
++)
749 RB_DLINK_FOREACH(ptr
, chptr
->members
.head
)
755 msptr
->flags
&= ~CHFL_CHANOP
;
756 lpara
[count
++] = msptr
->client_p
->name
;
759 /* +ov, might not fit so check. */
762 if(count
>= MAXMODEPARAMS
)
765 sendto_channel_local(ALL_MEMBERS
, chptr
,
766 ":%s MODE %s %s %s %s %s %s",
767 me
.name
, chptr
->chname
,
768 lmodebuf
, lpara
[0], lpara
[1],
771 /* preserve the initial '-' */
776 for (i
= 0; i
< MAXMODEPARAMS
; i
++)
780 msptr
->flags
&= ~CHFL_VOICE
;
781 lpara
[count
++] = msptr
->client_p
->name
;
785 else if(is_voiced(msptr
))
787 msptr
->flags
&= ~CHFL_VOICE
;
788 lpara
[count
++] = msptr
->client_p
->name
;
794 if(count
>= MAXMODEPARAMS
)
797 sendto_channel_local(ALL_MEMBERS
, chptr
,
798 ":%s MODE %s %s %s %s %s %s",
799 me
.name
, chptr
->chname
, lmodebuf
,
800 lpara
[0], lpara
[1], lpara
[2], lpara
[3]);
805 for (i
= 0; i
< MAXMODEPARAMS
; i
++)
813 sendto_channel_local(ALL_MEMBERS
, chptr
,
814 ":%s MODE %s %s %s %s %s %s",
815 me
.name
, chptr
->chname
, lmodebuf
,
816 EmptyString(lpara
[0]) ? "" : lpara
[0],
817 EmptyString(lpara
[1]) ? "" : lpara
[1],
818 EmptyString(lpara
[2]) ? "" : lpara
[2],
819 EmptyString(lpara
[3]) ? "" : lpara
[3]);
826 * inputs - channel, source, list to remove, char of mode, caps needed
828 * side effects - given list is removed, with modes issued to local clients
829 * and non-TS6 servers.
832 remove_ban_list(struct Channel
*chptr
, struct Client
*source_p
,
833 rb_dlink_list
* list
, char c
, int cap
, int mems
)
835 static char lmodebuf
[BUFSIZE
];
836 static char lparabuf
[BUFSIZE
];
839 rb_dlink_node
*next_ptr
;
842 int cur_len
, mlen
, plen
;
846 cur_len
= mlen
= rb_sprintf(lmodebuf
, ":%s MODE %s -", source_p
->name
, chptr
->chname
);
847 mbuf
= lmodebuf
+ mlen
;
849 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, list
->head
)
853 /* trailing space, and the mode letter itself */
854 plen
= strlen(banptr
->banstr
) + 2;
856 if(count
>= MAXMODEPARAMS
|| (cur_len
+ plen
) > BUFSIZE
- 4)
858 /* remove trailing space */
862 sendto_channel_local(mems
, chptr
, "%s %s", lmodebuf
, lparabuf
);
863 /* Tricky tricky. If we changed source_p to &me
864 * in ms_sjoin(), this still won't send stuff
865 * where it should not be sent, because the
866 * real source_p does TS6 -- jilles */
867 sendto_server(source_p
, chptr
, cap
, CAP_TS6
, "%s %s", lmodebuf
, lparabuf
);
870 mbuf
= lmodebuf
+ mlen
;
877 pbuf
+= rb_sprintf(pbuf
, "%s ", banptr
->banstr
);
885 sendto_channel_local(mems
, chptr
, "%s %s", lmodebuf
, lparabuf
);
886 sendto_server(source_p
, chptr
, cap
, CAP_TS6
, "%s %s", lmodebuf
, lparabuf
);
888 list
->head
= list
->tail
= NULL
;