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