]> jfr.im git - irc/rqf/shadowircd.git/blame_incremental - modules/core/m_join.c
Move target change code to src/tgchange.c,
[irc/rqf/shadowircd.git] / modules / core / m_join.c
... / ...
CommitLineData
1/*
2 * ircd-ratbox: A slightly useful ircd.
3 * m_join.c: Joins a channel.
4 *
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
8 *
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.
13 *
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.
18 *
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
22 * USA
23 *
24 * $Id: m_join.c 3494 2007-05-27 13:07:27Z jilles $
25 */
26
27#include "stdinc.h"
28#include "channel.h"
29#include "client.h"
30#include "common.h"
31#include "hash.h"
32#include "match.h"
33#include "ircd.h"
34#include "numeric.h"
35#include "send.h"
36#include "s_serv.h"
37#include "s_conf.h"
38#include "s_newconf.h"
39#include "msg.h"
40#include "parse.h"
41#include "modules.h"
42#include "packet.h"
43#include "chmode.h"
44
45static int m_join(struct Client *, struct Client *, int, const char **);
46static int ms_join(struct Client *, struct Client *, int, const char **);
47static int ms_sjoin(struct Client *, struct Client *, int, const char **);
48
49static int h_can_create_channel;
50static int h_channel_join;
51
52struct Message join_msgtab = {
53 "JOIN", 0, 0, 0, MFLG_SLOW,
54 {mg_unreg, {m_join, 2}, {ms_join, 2}, mg_ignore, mg_ignore, {m_join, 2}}
55};
56
57struct Message sjoin_msgtab = {
58 "SJOIN", 0, 0, 0, MFLG_SLOW,
59 {mg_unreg, mg_ignore, mg_ignore, {ms_sjoin, 4}, mg_ignore, mg_ignore}
60};
61
62mapi_clist_av1 join_clist[] = { &join_msgtab, &sjoin_msgtab, NULL };
63
64mapi_hlist_av1 join_hlist[] = {
65 { "can_create_channel", &h_can_create_channel },
66 { "channel_join", &h_channel_join },
67 { NULL, NULL },
68};
69
70DECLARE_MODULE_AV1(join, NULL, NULL, join_clist, join_hlist, NULL, "$Revision: 3494 $");
71
72static void do_join_0(struct Client *client_p, struct Client *source_p);
73static int check_channel_name_loc(struct Client *source_p, const char *name);
74
75static void set_final_mode(struct Mode *mode, struct Mode *oldmode);
76static void remove_our_modes(struct Channel *chptr, struct Client *source_p);
77
78static void remove_ban_list(struct Channel *chptr, struct Client *source_p,
79 rb_dlink_list * list, char c, int mems);
80
81static char modebuf[MODEBUFLEN];
82static char parabuf[MODEBUFLEN];
83static const char *para[MAXMODEPARAMS];
84static char *mbuf;
85static int pargs;
86
87/* Check what we will forward to, without sending any notices to the user
88 * -- jilles
89 */
90static struct Channel *
91check_forward(struct Client *source_p, struct Channel *chptr,
92 char *key)
93{
94 int depth = 0, i;
95
96 /* User is +Q */
97 if (IsNoForward(source_p))
98 return NULL;
99
100 while (depth < 16)
101 {
102 chptr = find_channel(chptr->mode.forward);
103 /* Can only forward to existing channels */
104 if (chptr == NULL)
105 return NULL;
106 /* Already on there, show original error message */
107 if (IsMember(source_p, chptr))
108 return NULL;
109 /* Juped. Sending a warning notice would be unfair */
110 if (hash_find_resv(chptr->chname))
111 return NULL;
112 /* Don't forward to +Q channel */
113 if (chptr->mode.mode & MODE_DISFORWARD)
114 return NULL;
115 i = can_join(source_p, chptr, key);
116 if (i == 0)
117 return chptr;
118 if (i != ERR_INVITEONLYCHAN && i != ERR_NEEDREGGEDNICK && i != ERR_THROTTLE && i != ERR_CHANNELISFULL)
119 return NULL;
120 depth++;
121 }
122
123 return NULL;
124}
125
126/*
127 * m_join
128 * parv[1] = channel
129 * parv[2] = channel password (key)
130 */
131static int
132m_join(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
133{
134 static char jbuf[BUFSIZE];
135 struct Channel *chptr = NULL;
136 struct ConfItem *aconf;
137 char *name;
138 char *key = NULL;
139 const char *modes;
140 int i, flags = 0;
141 char *p = NULL, *p2 = NULL;
142 char *chanlist;
143 char *mykey;
144 int successful_join_count = 0; /* Number of channels successfully joined */
145
146 jbuf[0] = '\0';
147
148 /* rebuild the list of channels theyre supposed to be joining.
149 * this code has a side effect of losing keys, but..
150 */
151 chanlist = LOCAL_COPY(parv[1]);
152 for(name = rb_strtok_r(chanlist, ",", &p); name; name = rb_strtok_r(NULL, ",", &p))
153 {
154 /* check the length and name of channel is ok */
155 if(!check_channel_name_loc(source_p, name) || (strlen(name) > LOC_CHANNELLEN))
156 {
157 sendto_one_numeric(source_p, ERR_BADCHANNAME,
158 form_str(ERR_BADCHANNAME), (unsigned char *) name);
159 continue;
160 }
161
162 /* join 0 parts all channels */
163 if(*name == '0' && (name[1] == ',' || name[1] == '\0') && name == chanlist)
164 {
165 (void) strcpy(jbuf, "0");
166 continue;
167 }
168
169 /* check it begins with # or &, and local chans are disabled */
170 else if(!IsChannelName(name))
171 {
172 sendto_one_numeric(source_p, ERR_NOSUCHCHANNEL,
173 form_str(ERR_NOSUCHCHANNEL), name);
174 continue;
175 }
176
177 /* see if its resv'd */
178 if(!IsExemptResv(source_p) && (aconf = hash_find_resv(name)))
179 {
180 sendto_one_numeric(source_p, ERR_BADCHANNAME,
181 form_str(ERR_BADCHANNAME), name);
182
183 /* dont warn for opers */
184 if(!IsExemptJupe(source_p) && !IsOper(source_p))
185 sendto_realops_snomask(SNO_SPY, L_NETWIDE,
186 "User %s (%s@%s) is attempting to join locally juped channel %s (%s)",
187 source_p->name, source_p->username,
188 source_p->orighost, name, aconf->passwd);
189 /* dont update tracking for jupe exempt users, these
190 * are likely to be spamtrap leaves
191 */
192 else if(IsExemptJupe(source_p))
193 aconf->port--;
194
195 continue;
196 }
197
198 if(splitmode && !IsOper(source_p) && (*name != '&') &&
199 ConfigChannel.no_join_on_split)
200 {
201 sendto_one(source_p, form_str(ERR_UNAVAILRESOURCE),
202 me.name, source_p->name, name);
203 continue;
204 }
205
206 if(*jbuf)
207 (void) strcat(jbuf, ",");
208 (void) rb_strlcat(jbuf, name, sizeof(jbuf));
209 }
210
211 if(parc > 2)
212 {
213 mykey = LOCAL_COPY(parv[2]);
214 key = rb_strtok_r(mykey, ",", &p2);
215 }
216
217 for(name = rb_strtok_r(jbuf, ",", &p); name;
218 key = (key) ? rb_strtok_r(NULL, ",", &p2) : NULL, name = rb_strtok_r(NULL, ",", &p))
219 {
220 hook_data_channel_activity hook_info;
221
222 /* JOIN 0 simply parts all channels the user is in */
223 if(*name == '0' && !atoi(name))
224 {
225 if(source_p->user->channel.head == NULL)
226 continue;
227
228 do_join_0(&me, source_p);
229 continue;
230 }
231
232 /* look for the channel */
233 if((chptr = find_channel(name)) != NULL)
234 {
235 if(IsMember(source_p, chptr))
236 continue;
237
238 flags = 0;
239 }
240 else
241 {
242 hook_data_client_approval moduledata;
243
244 moduledata.client = source_p;
245 moduledata.approved = 0;
246
247 call_hook(h_can_create_channel, &moduledata);
248
249 if(moduledata.approved != 0)
250 {
251 sendto_one(source_p, form_str(moduledata.approved),
252 me.name, source_p->name, name);
253 continue;
254 }
255
256 if(splitmode && !IsOper(source_p) && (*name != '&') &&
257 ConfigChannel.no_create_on_split)
258 {
259 sendto_one(source_p, form_str(ERR_UNAVAILRESOURCE),
260 me.name, source_p->name, name);
261 continue;
262 }
263
264 flags = CHFL_CHANOP;
265 }
266
267 if((rb_dlink_list_length(&source_p->user->channel) >=
268 (unsigned long) ConfigChannel.max_chans_per_user) &&
269 (!IsOper(source_p) ||
270 (rb_dlink_list_length(&source_p->user->channel) >=
271 (unsigned long) ConfigChannel.max_chans_per_user * 3)))
272 {
273 sendto_one(source_p, form_str(ERR_TOOMANYCHANNELS),
274 me.name, source_p->name, name);
275 if(successful_join_count)
276 source_p->localClient->last_join_time = rb_current_time();
277 return 0;
278 }
279
280 if(flags == 0) /* if channel doesn't exist, don't penalize */
281 successful_join_count++;
282
283 if(chptr == NULL) /* If I already have a chptr, no point doing this */
284 {
285 chptr = get_or_create_channel(source_p, name, NULL);
286
287 if(chptr == NULL)
288 {
289 sendto_one(source_p, form_str(ERR_UNAVAILRESOURCE),
290 me.name, source_p->name, name);
291 if(successful_join_count > 0)
292 successful_join_count--;
293 continue;
294 }
295 }
296
297 if(!IsOper(source_p) && !IsExemptSpambot(source_p))
298 check_spambot_warning(source_p, name);
299
300 /* can_join checks for +i key, bans etc */
301 if((i = can_join(source_p, chptr, key)))
302 {
303 if ((i != ERR_NEEDREGGEDNICK && i != ERR_THROTTLE && i != ERR_INVITEONLYCHAN && i != ERR_CHANNELISFULL) ||
304 (!ConfigChannel.use_forward || (chptr = check_forward(source_p, chptr, key)) == NULL))
305 {
306 /* might be wrong, but is there any other better location for such?
307 * see extensions/chm_operonly.c for other comments on this
308 * -- dwr
309 */
310 if(i != ERR_CUSTOM)
311 sendto_one(source_p, form_str(i), me.name, source_p->name, name);
312
313 if(successful_join_count > 0)
314 successful_join_count--;
315 continue;
316 }
317
318 sendto_one_numeric(source_p, ERR_LINKCHANNEL, form_str(ERR_LINKCHANNEL), name, chptr->chname);
319 }
320
321 /* add the user to the channel */
322 add_user_to_channel(chptr, source_p, flags);
323 if (chptr->mode.join_num &&
324 rb_current_time() - chptr->join_delta >= chptr->mode.join_time)
325 {
326 chptr->join_count = 0;
327 chptr->join_delta = rb_current_time();
328 }
329 chptr->join_count++;
330
331 /* we send the user their join here, because we could have to
332 * send a mode out next.
333 */
334 sendto_channel_local(ALL_MEMBERS, chptr, ":%s!%s@%s JOIN :%s",
335 source_p->name,
336 source_p->username, source_p->host, chptr->chname);
337
338 /* its a new channel, set +nt and burst. */
339 if(flags & CHFL_CHANOP)
340 {
341 chptr->channelts = rb_current_time();
342 chptr->mode.mode |= MODE_TOPICLIMIT;
343 chptr->mode.mode |= MODE_NOPRIVMSGS;
344 modes = channel_modes(chptr, &me);
345
346 sendto_channel_local(ONLY_CHANOPS, chptr, ":%s MODE %s %s",
347 me.name, chptr->chname, modes);
348
349 sendto_server(client_p, chptr, CAP_TS6, NOCAPS,
350 ":%s SJOIN %ld %s %s :@%s",
351 me.id, (long) chptr->channelts,
352 chptr->chname, modes, source_p->id);
353 }
354 else
355 {
356 sendto_server(client_p, chptr, CAP_TS6, NOCAPS,
357 ":%s JOIN %ld %s +",
358 use_id(source_p), (long) chptr->channelts,
359 chptr->chname);
360 }
361
362 del_invite(chptr, source_p);
363
364 if(chptr->topic != NULL)
365 {
366 sendto_one(source_p, form_str(RPL_TOPIC), me.name,
367 source_p->name, chptr->chname, chptr->topic);
368
369 sendto_one(source_p, form_str(RPL_TOPICWHOTIME),
370 me.name, source_p->name, chptr->chname,
371 chptr->topic_info, chptr->topic_time);
372 }
373
374 channel_member_names(chptr, source_p, 1);
375
376 if(successful_join_count)
377 source_p->localClient->last_join_time = rb_current_time();
378
379 hook_info.client = source_p;
380 hook_info.chptr = chptr;
381 hook_info.key = key;
382 call_hook(h_channel_join, &hook_info);
383 }
384
385 return 0;
386}
387
388/*
389 * ms_join
390 * parv[1] = channel TS
391 * parv[2] = channel
392 * parv[3] = "+", formerly channel modes but now unused
393 * alternatively, a single "0" parameter parts all channels
394 */
395static int
396ms_join(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
397{
398 struct Channel *chptr;
399 static struct Mode mode;
400 time_t oldts;
401 time_t newts;
402 int isnew;
403 int keep_our_modes = YES;
404 int keep_new_modes = YES;
405 rb_dlink_node *ptr, *next_ptr;
406
407 /* special case for join 0 */
408 if((parv[1][0] == '0') && (parv[1][1] == '\0') && parc == 2)
409 {
410 do_join_0(client_p, source_p);
411 return 0;
412 }
413
414 if(parc < 4)
415 return 0;
416
417 if(!IsChannelName(parv[2]) || !check_channel_name(parv[2]))
418 return 0;
419
420 /* joins for local channels cant happen. */
421 if(parv[2][0] == '&')
422 return 0;
423
424 mbuf = modebuf;
425 mode.key[0] = mode.forward[0] = '\0';
426 mode.mode = mode.limit = mode.join_num = mode.join_time = 0;
427
428 if((chptr = get_or_create_channel(source_p, parv[2], &isnew)) == NULL)
429 return 0;
430
431 newts = atol(parv[1]);
432 oldts = chptr->channelts;
433
434#ifdef IGNORE_BOGUS_TS
435 if(newts < 800000000)
436 {
437 sendto_realops_snomask(SNO_DEBUG, L_ALL,
438 "*** Bogus TS %ld on %s ignored from %s",
439 (long) newts, chptr->chname, client_p->name);
440 newts = (oldts == 0) ? oldts : 800000000;
441 }
442#else
443 /* making a channel TS0 */
444 if(!isnew && !newts && oldts)
445 {
446 sendto_channel_local(ALL_MEMBERS, chptr,
447 ":%s NOTICE %s :*** Notice -- TS for %s changed from %ld to 0",
448 me.name, chptr->chname, chptr->chname, (long) oldts);
449 sendto_realops_snomask(SNO_GENERAL, L_ALL,
450 "Server %s changing TS on %s from %ld to 0",
451 source_p->name, chptr->chname, (long) oldts);
452 }
453#endif
454
455 if(isnew)
456 chptr->channelts = newts;
457 else if(newts == 0 || oldts == 0)
458 chptr->channelts = 0;
459 else if(newts == oldts)
460 ;
461 else if(newts < oldts)
462 {
463 keep_our_modes = NO;
464 chptr->channelts = newts;
465 }
466 else
467 keep_new_modes = NO;
468
469 /* Lost the TS, other side wins, so remove modes on this side */
470 if(!keep_our_modes)
471 {
472 set_final_mode(&mode, &chptr->mode);
473 chptr->mode = mode;
474 remove_our_modes(chptr, source_p);
475 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, chptr->invites.head)
476 {
477 del_invite(chptr, ptr->data);
478 }
479 /* If setting -j, clear join throttle state -- jilles */
480 chptr->join_count = chptr->join_delta = 0;
481 sendto_channel_local(ALL_MEMBERS, chptr,
482 ":%s NOTICE %s :*** Notice -- TS for %s changed from %ld to %ld",
483 me.name, chptr->chname, chptr->chname,
484 (long) oldts, (long) newts);
485 /* Update capitalization in channel name, this makes the
486 * capitalization timestamped like modes are -- jilles */
487 strcpy(chptr->chname, parv[2]);
488 if(*modebuf != '\0')
489 sendto_channel_local(ALL_MEMBERS, chptr,
490 ":%s MODE %s %s %s",
491 source_p->servptr->name,
492 chptr->chname, modebuf, parabuf);
493 *modebuf = *parabuf = '\0';
494 }
495
496 if(!IsMember(source_p, chptr))
497 {
498 add_user_to_channel(chptr, source_p, CHFL_PEON);
499 if (chptr->mode.join_num &&
500 rb_current_time() - chptr->join_delta >= chptr->mode.join_time)
501 {
502 chptr->join_count = 0;
503 chptr->join_delta = rb_current_time();
504 }
505 chptr->join_count++;
506 sendto_channel_local(ALL_MEMBERS, chptr, ":%s!%s@%s JOIN :%s",
507 source_p->name, source_p->username,
508 source_p->host, chptr->chname);
509 }
510
511 sendto_server(client_p, chptr, CAP_TS6, NOCAPS,
512 ":%s JOIN %ld %s +",
513 source_p->id, (long) chptr->channelts, chptr->chname);
514 return 0;
515}
516
517static int
518ms_sjoin(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
519{
520 static char buf_uid[BUFSIZE];
521 static const char empty_modes[] = "0";
522 struct Channel *chptr;
523 struct Client *target_p, *fakesource_p;
524 time_t newts;
525 time_t oldts;
526 static struct Mode mode, *oldmode;
527 const char *modes;
528 int args = 0;
529 int keep_our_modes = 1;
530 int keep_new_modes = 1;
531 int fl;
532 int isnew;
533 int mlen_uid;
534 int len_nick;
535 int len_uid;
536 int len;
537 int joins = 0;
538 const char *s;
539 char *ptr_uid;
540 char *p;
541 int i, joinc = 0, timeslice = 0;
542 static char empty[] = "";
543 rb_dlink_node *ptr, *next_ptr;
544
545 if(!IsChannelName(parv[2]) || !check_channel_name(parv[2]))
546 return 0;
547
548 /* SJOIN's for local channels can't happen. */
549 if(*parv[2] == '&')
550 return 0;
551
552 modebuf[0] = parabuf[0] = mode.key[0] = mode.forward[0] = '\0';
553 pargs = mode.mode = mode.limit = mode.join_num = mode.join_time = 0;
554
555 /* Hide connecting server on netburst -- jilles */
556 if (ConfigServerHide.flatten_links && !HasSentEob(source_p))
557 fakesource_p = &me;
558 else
559 fakesource_p = source_p;
560
561 mbuf = modebuf;
562 newts = atol(parv[1]);
563
564 s = parv[3];
565 while (*s)
566 {
567 switch (*(s++))
568 {
569 case 'f':
570 rb_strlcpy(mode.forward, parv[4 + args], sizeof(mode.forward));
571 args++;
572 if(parc < 5 + args)
573 return 0;
574 break;
575 case 'j':
576 sscanf(parv[4 + args], "%d:%d", &joinc, &timeslice);
577 args++;
578 mode.join_num = joinc;
579 mode.join_time = timeslice;
580 if(parc < 5 + args)
581 return 0;
582 break;
583 case 'k':
584 rb_strlcpy(mode.key, parv[4 + args], sizeof(mode.key));
585 args++;
586 if(parc < 5 + args)
587 return 0;
588 break;
589 case 'l':
590 mode.limit = atoi(parv[4 + args]);
591 args++;
592 if(parc < 5 + args)
593 return 0;
594 break;
595 default:
596 if(chmode_flags[(int) *s] != 0)
597 {
598 mode.mode |= chmode_flags[(int) *s];
599 }
600 }
601 }
602
603 if(parv[args + 4])
604 {
605 s = parv[args + 4];
606
607 /* remove any leading spaces */
608 while (*s == ' ')
609 s++;
610 }
611 else
612 s = "";
613
614 if((chptr = get_or_create_channel(source_p, parv[2], &isnew)) == NULL)
615 return 0; /* channel name too long? */
616
617
618 oldts = chptr->channelts;
619 oldmode = &chptr->mode;
620
621#ifdef IGNORE_BOGUS_TS
622 if(newts < 800000000)
623 {
624 sendto_realops_snomask(SNO_DEBUG, L_ALL,
625 "*** Bogus TS %ld on %s ignored from %s",
626 (long) newts, chptr->chname, client_p->name);
627
628 newts = (oldts == 0) ? oldts : 800000000;
629 }
630#else
631 if(!isnew && !newts && oldts)
632 {
633 sendto_channel_local(ALL_MEMBERS, chptr,
634 ":%s NOTICE %s :*** Notice -- TS for %s "
635 "changed from %ld to 0",
636 me.name, chptr->chname, chptr->chname, (long) oldts);
637 sendto_realops_snomask(SNO_GENERAL, L_ALL,
638 "Server %s changing TS on %s from %ld to 0",
639 source_p->name, chptr->chname, (long) oldts);
640 }
641#endif
642
643 if(isnew)
644 chptr->channelts = newts;
645
646 else if(newts == 0 || oldts == 0)
647 chptr->channelts = 0;
648 else if(newts == oldts)
649 ;
650 else if(newts < oldts)
651 {
652 /* If configured, kick people trying to join +i/+k
653 * channels by recreating them on split servers.
654 * Don't kick if the source has sent EOB (services
655 * deopping everyone by TS-1 SJOIN).
656 * -- jilles */
657 if (ConfigChannel.kick_on_split_riding &&
658 !HasSentEob(source_p) &&
659 ((mode.mode & MODE_INVITEONLY) ||
660 (mode.key[0] != 0 && irccmp(mode.key, oldmode->key) != 0)))
661 {
662 struct membership *msptr;
663 struct Client *who;
664 int l = rb_dlink_list_length(&chptr->members);
665
666 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, chptr->locmembers.head)
667 {
668 msptr = ptr->data;
669 who = msptr->client_p;
670 sendto_one(who, ":%s KICK %s %s :Net Rider",
671 me.name, chptr->chname, who->name);
672
673 sendto_server(NULL, chptr, CAP_TS6, NOCAPS,
674 ":%s KICK %s %s :Net Rider",
675 me.id, chptr->chname,
676 who->id);
677 remove_user_from_channel(msptr);
678 if (--l == 0)
679 break;
680 }
681 if (l == 0)
682 {
683 /* Channel was emptied, create a new one */
684 if((chptr = get_or_create_channel(source_p, parv[2], &isnew)) == NULL)
685 return 0; /* oops! */
686
687 oldmode = &chptr->mode;
688 }
689 }
690 keep_our_modes = NO;
691 chptr->channelts = newts;
692 }
693 else
694 keep_new_modes = NO;
695
696 if(!keep_new_modes)
697 mode = *oldmode;
698 else if(keep_our_modes)
699 {
700 mode.mode |= oldmode->mode;
701 if(oldmode->limit > mode.limit)
702 mode.limit = oldmode->limit;
703 if(strcmp(mode.key, oldmode->key) < 0)
704 strcpy(mode.key, oldmode->key);
705 if(oldmode->join_num > mode.join_num ||
706 (oldmode->join_num == mode.join_num &&
707 oldmode->join_time > mode.join_time))
708 {
709 mode.join_num = oldmode->join_num;
710 mode.join_time = oldmode->join_time;
711 }
712 if(irccmp(mode.forward, oldmode->forward) < 0)
713 strcpy(mode.forward, oldmode->forward);
714 }
715 else
716 {
717 /* If setting -j, clear join throttle state -- jilles */
718 if (!mode.join_num)
719 chptr->join_count = chptr->join_delta = 0;
720 }
721
722 set_final_mode(&mode, oldmode);
723 chptr->mode = mode;
724
725 /* Lost the TS, other side wins, so remove modes on this side */
726 if(!keep_our_modes)
727 {
728 remove_our_modes(chptr, fakesource_p);
729 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, chptr->invites.head)
730 {
731 del_invite(chptr, ptr->data);
732 }
733
734 if(rb_dlink_list_length(&chptr->banlist) > 0)
735 remove_ban_list(chptr, fakesource_p, &chptr->banlist, 'b', ALL_MEMBERS);
736 if(rb_dlink_list_length(&chptr->exceptlist) > 0)
737 remove_ban_list(chptr, fakesource_p, &chptr->exceptlist,
738 'e', ONLY_CHANOPS);
739 if(rb_dlink_list_length(&chptr->invexlist) > 0)
740 remove_ban_list(chptr, fakesource_p, &chptr->invexlist,
741 'I', ONLY_CHANOPS);
742 if(rb_dlink_list_length(&chptr->quietlist) > 0)
743 remove_ban_list(chptr, fakesource_p, &chptr->quietlist,
744 'q', ALL_MEMBERS);
745 chptr->bants++;
746
747 sendto_channel_local(ALL_MEMBERS, chptr,
748 ":%s NOTICE %s :*** Notice -- TS for %s changed from %ld to %ld",
749 me.name, chptr->chname, chptr->chname,
750 (long) oldts, (long) newts);
751 /* Update capitalization in channel name, this makes the
752 * capitalization timestamped like modes are -- jilles */
753 strcpy(chptr->chname, parv[2]);
754 }
755
756 if(*modebuf != '\0')
757 sendto_channel_local(ALL_MEMBERS, chptr, ":%s MODE %s %s %s",
758 fakesource_p->name, chptr->chname, modebuf, parabuf);
759
760 *modebuf = *parabuf = '\0';
761
762 if(parv[3][0] != '0' && keep_new_modes)
763 modes = channel_modes(chptr, source_p);
764 else
765 modes = empty_modes;
766
767 mlen_uid = rb_sprintf(buf_uid, ":%s SJOIN %ld %s %s :",
768 use_id(source_p), (long) chptr->channelts, parv[2], modes);
769 ptr_uid = buf_uid + mlen_uid;
770
771 mbuf = modebuf;
772 para[0] = para[1] = para[2] = para[3] = empty;
773 pargs = 0;
774 len_nick = len_uid = 0;
775
776 /* if theres a space, theres going to be more than one nick, change the
777 * first space to \0, so s is just the first nick, and point p to the
778 * second nick
779 */
780 if((p = strchr(s, ' ')) != NULL)
781 {
782 *p++ = '\0';
783 }
784
785 *mbuf++ = '+';
786
787 while (s)
788 {
789 fl = 0;
790
791 for (i = 0; i < 2; i++)
792 {
793 if(*s == '@')
794 {
795 fl |= CHFL_CHANOP;
796 s++;
797 }
798 else if(*s == '+')
799 {
800 fl |= CHFL_VOICE;
801 s++;
802 }
803 }
804
805 /* if the client doesnt exist or is fake direction, skip. */
806 if(!(target_p = find_client(s)) ||
807 (target_p->from != client_p) || !IsPerson(target_p))
808 goto nextnick;
809
810 /* we assume for these we can fit at least one nick/uid in.. */
811
812 /* check we can fit another status+nick+space into a buffer */
813 if((mlen_uid + len_uid + IDLEN + 3) > (BUFSIZE - 3))
814 {
815 *(ptr_uid - 1) = '\0';
816 sendto_server(client_p->from, NULL, CAP_TS6, NOCAPS, "%s", buf_uid);
817 ptr_uid = buf_uid + mlen_uid;
818 len_uid = 0;
819 }
820
821 if(keep_new_modes)
822 {
823 if(fl & CHFL_CHANOP)
824 {
825 *ptr_uid++ = '@';
826 len_nick++;
827 len_uid++;
828 }
829 if(fl & CHFL_VOICE)
830 {
831 *ptr_uid++ = '+';
832 len_nick++;
833 len_uid++;
834 }
835 }
836
837 /* copy the nick to the two buffers */
838 len = rb_sprintf(ptr_uid, "%s ", use_id(target_p));
839 ptr_uid += len;
840 len_uid += len;
841
842 if(!keep_new_modes)
843 fl = 0;
844
845 if(!IsMember(target_p, chptr))
846 {
847 add_user_to_channel(chptr, target_p, fl);
848 sendto_channel_local(ALL_MEMBERS, chptr, ":%s!%s@%s JOIN :%s",
849 target_p->name,
850 target_p->username, target_p->host, parv[2]);
851 joins++;
852 }
853
854 if(fl & CHFL_CHANOP)
855 {
856 *mbuf++ = 'o';
857 para[pargs++] = target_p->name;
858
859 /* a +ov user.. bleh */
860 if(fl & CHFL_VOICE)
861 {
862 /* its possible the +o has filled up MAXMODEPARAMS, if so, start
863 * a new buffer
864 */
865 if(pargs >= MAXMODEPARAMS)
866 {
867 *mbuf = '\0';
868 sendto_channel_local(ALL_MEMBERS, chptr,
869 ":%s MODE %s %s %s %s %s %s",
870 fakesource_p->name, chptr->chname,
871 modebuf,
872 para[0], para[1], para[2], para[3]);
873 mbuf = modebuf;
874 *mbuf++ = '+';
875 para[0] = para[1] = para[2] = para[3] = NULL;
876 pargs = 0;
877 }
878
879 *mbuf++ = 'v';
880 para[pargs++] = target_p->name;
881 }
882 }
883 else if(fl & CHFL_VOICE)
884 {
885 *mbuf++ = 'v';
886 para[pargs++] = target_p->name;
887 }
888
889 if(pargs >= MAXMODEPARAMS)
890 {
891 *mbuf = '\0';
892 sendto_channel_local(ALL_MEMBERS, chptr,
893 ":%s MODE %s %s %s %s %s %s",
894 fakesource_p->name,
895 chptr->chname,
896 modebuf, para[0], para[1], para[2], para[3]);
897 mbuf = modebuf;
898 *mbuf++ = '+';
899 para[0] = para[1] = para[2] = para[3] = NULL;
900 pargs = 0;
901 }
902
903 nextnick:
904 /* p points to the next nick */
905 s = p;
906
907 /* if there was a trailing space and p was pointing to it, then we
908 * need to exit.. this has the side effect of breaking double spaces
909 * in an sjoin.. but that shouldnt happen anyway
910 */
911 if(s && (*s == '\0'))
912 s = p = NULL;
913
914 /* if p was NULL due to no spaces, s wont exist due to the above, so
915 * we cant check it for spaces.. if there are no spaces, then when
916 * we next get here, s will be NULL
917 */
918 if(s && ((p = strchr(s, ' ')) != NULL))
919 {
920 *p++ = '\0';
921 }
922 }
923
924 *mbuf = '\0';
925 if(pargs)
926 {
927 sendto_channel_local(ALL_MEMBERS, chptr,
928 ":%s MODE %s %s %s %s %s %s",
929 fakesource_p->name, chptr->chname, modebuf,
930 para[0], CheckEmpty(para[1]),
931 CheckEmpty(para[2]), CheckEmpty(para[3]));
932 }
933
934 if(!joins && !(chptr->mode.mode & MODE_PERMANENT) && isnew)
935 {
936 destroy_channel(chptr);
937
938 return 0;
939 }
940
941 /* Keep the colon if we're sending an SJOIN without nicks -- jilles */
942 if (joins)
943 {
944 *(ptr_uid - 1) = '\0';
945 }
946
947 sendto_server(client_p->from, NULL, CAP_TS6, NOCAPS, "%s", buf_uid);
948
949 return 0;
950}
951
952/*
953 * do_join_0
954 *
955 * inputs - pointer to client doing join 0
956 * output - NONE
957 * side effects - Use has decided to join 0. This is legacy
958 * from the days when channels were numbers not names. *sigh*
959 * There is a bunch of evilness necessary here due to
960 * anti spambot code.
961 */
962static void
963do_join_0(struct Client *client_p, struct Client *source_p)
964{
965 struct membership *msptr;
966 struct Channel *chptr = NULL;
967 rb_dlink_node *ptr;
968
969 /* Finish the flood grace period... */
970 if(MyClient(source_p) && !IsFloodDone(source_p))
971 flood_endgrace(source_p);
972
973 sendto_server(client_p, NULL, CAP_TS6, NOCAPS, ":%s JOIN 0", use_id(source_p));
974
975 if(source_p->user->channel.head && MyConnect(source_p) &&
976 !IsOper(source_p) && !IsExemptSpambot(source_p))
977 check_spambot_warning(source_p, NULL);
978
979 while((ptr = source_p->user->channel.head))
980 {
981 msptr = ptr->data;
982 chptr = msptr->chptr;
983 sendto_channel_local(ALL_MEMBERS, chptr, ":%s!%s@%s PART %s",
984 source_p->name,
985 source_p->username, source_p->host, chptr->chname);
986 remove_user_from_channel(msptr);
987 }
988}
989
990static int
991check_channel_name_loc(struct Client *source_p, const char *name)
992{
993 const char *p;
994
995 s_assert(name != NULL);
996 if(EmptyString(name))
997 return 0;
998
999 if(ConfigFileEntry.disable_fake_channels && !IsOper(source_p))
1000 {
1001 for(p = name; *p; ++p)
1002 {
1003 if(!IsChanChar(*p) || IsFakeChanChar(*p))
1004 return 0;
1005 }
1006 }
1007 else
1008 {
1009 for(p = name; *p; ++p)
1010 {
1011 if(!IsChanChar(*p))
1012 return 0;
1013 }
1014 }
1015
1016 if(ConfigChannel.only_ascii_channels)
1017 {
1018 for(p = name; *p; ++p)
1019 if(*p < 33 || *p > 126)
1020 return 0;
1021 }
1022
1023 return 1;
1024}
1025
1026static void
1027set_final_mode(struct Mode *mode, struct Mode *oldmode)
1028{
1029 int dir = MODE_QUERY;
1030 char *pbuf = parabuf;
1031 int len;
1032 int i;
1033
1034 /* ok, first get a list of modes we need to add */
1035 for (i = 0; i < 256; i++)
1036 {
1037 if((mode->mode & chmode_flags[i]) && !(oldmode->mode & chmode_flags[i]))
1038 {
1039 if(dir != MODE_ADD)
1040 {
1041 *mbuf++ = '+';
1042 dir = MODE_ADD;
1043 }
1044 *mbuf++ = i;
1045 }
1046 }
1047
1048 /* now the ones we need to remove. */
1049 for (i = 0; i < 256; i++)
1050 {
1051 if((oldmode->mode & chmode_flags[i]) && !(mode->mode & chmode_flags[i]))
1052 {
1053 if(dir != MODE_DEL)
1054 {
1055 *mbuf++ = '-';
1056 dir = MODE_DEL;
1057 }
1058 *mbuf++ = i;
1059 }
1060 }
1061
1062 if(oldmode->limit && !mode->limit)
1063 {
1064 if(dir != MODE_DEL)
1065 {
1066 *mbuf++ = '-';
1067 dir = MODE_DEL;
1068 }
1069 *mbuf++ = 'l';
1070 }
1071 if(oldmode->key[0] && !mode->key[0])
1072 {
1073 if(dir != MODE_DEL)
1074 {
1075 *mbuf++ = '-';
1076 dir = MODE_DEL;
1077 }
1078 *mbuf++ = 'k';
1079 len = rb_sprintf(pbuf, "%s ", oldmode->key);
1080 pbuf += len;
1081 }
1082 if(oldmode->join_num && !mode->join_num)
1083 {
1084 if(dir != MODE_DEL)
1085 {
1086 *mbuf++ = '-';
1087 dir = MODE_DEL;
1088 }
1089 *mbuf++ = 'j';
1090 }
1091 if(oldmode->forward[0] && !mode->forward[0])
1092 {
1093 if(dir != MODE_DEL)
1094 {
1095 *mbuf++ = '-';
1096 dir = MODE_DEL;
1097 }
1098 *mbuf++ = 'f';
1099 }
1100 if(mode->limit && oldmode->limit != mode->limit)
1101 {
1102 if(dir != MODE_ADD)
1103 {
1104 *mbuf++ = '+';
1105 dir = MODE_ADD;
1106 }
1107 *mbuf++ = 'l';
1108 len = rb_sprintf(pbuf, "%d ", mode->limit);
1109 pbuf += len;
1110 }
1111 if(mode->key[0] && strcmp(oldmode->key, mode->key))
1112 {
1113 if(dir != MODE_ADD)
1114 {
1115 *mbuf++ = '+';
1116 dir = MODE_ADD;
1117 }
1118 *mbuf++ = 'k';
1119 len = rb_sprintf(pbuf, "%s ", mode->key);
1120 pbuf += len;
1121 }
1122 if(mode->join_num && (oldmode->join_num != mode->join_num || oldmode->join_time != mode->join_time))
1123 {
1124 if(dir != MODE_ADD)
1125 {
1126 *mbuf++ = '+';
1127 dir = MODE_ADD;
1128 }
1129 *mbuf++ = 'j';
1130 len = rb_sprintf(pbuf, "%d:%d ", mode->join_num, mode->join_time);
1131 pbuf += len;
1132 }
1133 if(mode->forward[0] && strcmp(oldmode->forward, mode->forward) && ConfigChannel.use_forward)
1134 {
1135 if(dir != MODE_ADD)
1136 {
1137 *mbuf++ = '+';
1138 dir = MODE_ADD;
1139 }
1140 *mbuf++ = 'f';
1141 len = rb_sprintf(pbuf, "%s ", mode->forward);
1142 pbuf += len;
1143 }
1144 *mbuf = '\0';
1145}
1146
1147/*
1148 * remove_our_modes
1149 *
1150 * inputs -
1151 * output -
1152 * side effects -
1153 */
1154static void
1155remove_our_modes(struct Channel *chptr, struct Client *source_p)
1156{
1157 struct membership *msptr;
1158 rb_dlink_node *ptr;
1159 char lmodebuf[MODEBUFLEN];
1160 char *lpara[MAXMODEPARAMS];
1161 int count = 0;
1162 int i;
1163
1164 mbuf = lmodebuf;
1165 *mbuf++ = '-';
1166
1167 for(i = 0; i < MAXMODEPARAMS; i++)
1168 lpara[i] = NULL;
1169
1170 RB_DLINK_FOREACH(ptr, chptr->members.head)
1171 {
1172 msptr = ptr->data;
1173
1174 if(is_chanop(msptr))
1175 {
1176 msptr->flags &= ~CHFL_CHANOP;
1177 lpara[count++] = msptr->client_p->name;
1178 *mbuf++ = 'o';
1179
1180 /* +ov, might not fit so check. */
1181 if(is_voiced(msptr))
1182 {
1183 if(count >= MAXMODEPARAMS)
1184 {
1185 *mbuf = '\0';
1186 sendto_channel_local(ALL_MEMBERS, chptr,
1187 ":%s MODE %s %s %s %s %s %s",
1188 source_p->name, chptr->chname,
1189 lmodebuf, lpara[0], lpara[1],
1190 lpara[2], lpara[3]);
1191
1192 /* preserve the initial '-' */
1193 mbuf = lmodebuf;
1194 *mbuf++ = '-';
1195 count = 0;
1196
1197 for(i = 0; i < MAXMODEPARAMS; i++)
1198 lpara[i] = NULL;
1199 }
1200
1201 msptr->flags &= ~CHFL_VOICE;
1202 lpara[count++] = msptr->client_p->name;
1203 *mbuf++ = 'v';
1204 }
1205 }
1206 else if(is_voiced(msptr))
1207 {
1208 msptr->flags &= ~CHFL_VOICE;
1209 lpara[count++] = msptr->client_p->name;
1210 *mbuf++ = 'v';
1211 }
1212 else
1213 continue;
1214
1215 if(count >= MAXMODEPARAMS)
1216 {
1217 *mbuf = '\0';
1218 sendto_channel_local(ALL_MEMBERS, chptr,
1219 ":%s MODE %s %s %s %s %s %s",
1220 source_p->name, chptr->chname, lmodebuf,
1221 lpara[0], lpara[1], lpara[2], lpara[3]);
1222 mbuf = lmodebuf;
1223 *mbuf++ = '-';
1224 count = 0;
1225
1226 for(i = 0; i < MAXMODEPARAMS; i++)
1227 lpara[i] = NULL;
1228 }
1229 }
1230
1231 if(count != 0)
1232 {
1233 *mbuf = '\0';
1234 sendto_channel_local(ALL_MEMBERS, chptr,
1235 ":%s MODE %s %s %s %s %s %s",
1236 source_p->name, chptr->chname, lmodebuf,
1237 EmptyString(lpara[0]) ? "" : lpara[0],
1238 EmptyString(lpara[1]) ? "" : lpara[1],
1239 EmptyString(lpara[2]) ? "" : lpara[2],
1240 EmptyString(lpara[3]) ? "" : lpara[3]);
1241
1242 }
1243}
1244
1245/* remove_ban_list()
1246 *
1247 * inputs - channel, source, list to remove, char of mode, caps needed
1248 * outputs -
1249 * side effects - given list is removed, with modes issued to local clients
1250 */
1251static void
1252remove_ban_list(struct Channel *chptr, struct Client *source_p,
1253 rb_dlink_list * list, char c, int mems)
1254{
1255 static char lmodebuf[BUFSIZE];
1256 static char lparabuf[BUFSIZE];
1257 struct Ban *banptr;
1258 rb_dlink_node *ptr;
1259 rb_dlink_node *next_ptr;
1260 char *pbuf;
1261 int count = 0;
1262 int cur_len, mlen, plen;
1263
1264 pbuf = lparabuf;
1265
1266 cur_len = mlen = rb_sprintf(lmodebuf, ":%s MODE %s -", source_p->name, chptr->chname);
1267 mbuf = lmodebuf + mlen;
1268
1269 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, list->head)
1270 {
1271 banptr = ptr->data;
1272
1273 /* trailing space, and the mode letter itself */
1274 plen = strlen(banptr->banstr) + 2;
1275
1276 if(count >= MAXMODEPARAMS || (cur_len + plen) > BUFSIZE - 4)
1277 {
1278 /* remove trailing space */
1279 *mbuf = '\0';
1280 *(pbuf - 1) = '\0';
1281
1282 sendto_channel_local(mems, chptr, "%s %s", lmodebuf, lparabuf);
1283
1284 cur_len = mlen;
1285 mbuf = lmodebuf + mlen;
1286 pbuf = lparabuf;
1287 count = 0;
1288 }
1289
1290 *mbuf++ = c;
1291 cur_len += plen;
1292 pbuf += rb_sprintf(pbuf, "%s ", banptr->banstr);
1293 count++;
1294
1295 free_ban(banptr);
1296 }
1297
1298 *mbuf = '\0';
1299 *(pbuf - 1) = '\0';
1300 sendto_channel_local(mems, chptr, "%s %s", lmodebuf, lparabuf);
1301
1302 list->head = list->tail = NULL;
1303 list->length = 0;
1304}