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