]> jfr.im git - solanum.git/blob - modules/core/m_join.c
Merge branch 'master' into authd-framework-2
[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 "common.h"
29 #include "hash.h"
30 #include "match.h"
31 #include "ircd.h"
32 #include "numeric.h"
33 #include "send.h"
34 #include "s_serv.h"
35 #include "s_conf.h"
36 #include "s_newconf.h"
37 #include "msg.h"
38 #include "parse.h"
39 #include "modules.h"
40 #include "packet.h"
41 #include "chmode.h"
42 #include "ratelimit.h"
43 #include "s_assert.h"
44
45 static const char join_desc[] = "Provides the JOIN and TS6 SJOIN commands to facilitate joining and creating channels";
46
47 static void m_join(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
48 static void ms_join(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
49 static void ms_sjoin(struct MsgBuf *, 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, 0,
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, 0,
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_AV2(join, NULL, NULL, join_clist, join_hlist, NULL, NULL, NULL, join_desc);
73
74 static void do_join_0(struct Client *client_p, struct Client *source_p);
75 static bool 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 void
142 m_join(struct MsgBuf *msgbuf_p, 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 (!IsExtendChans(source_p) ||
282 (rb_dlink_list_length(&source_p->user->channel) >=
283 (unsigned long) ConfigChannel.max_chans_per_user_large)))
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 |= ConfigChannel.autochanmodes;
345 modes = channel_modes(chptr, &me);
346
347 sendto_channel_local(ONLY_CHANOPS, chptr, ":%s MODE %s %s",
348 me.name, chptr->chname, modes);
349
350 sendto_server(client_p, chptr, CAP_TS6, NOCAPS,
351 ":%s SJOIN %ld %s %s :@%s",
352 me.id, (long) chptr->channelts,
353 chptr->chname, modes, source_p->id);
354 }
355 else
356 {
357 sendto_server(client_p, chptr, CAP_TS6, NOCAPS,
358 ":%s JOIN %ld %s +",
359 use_id(source_p), (long) chptr->channelts,
360 chptr->chname);
361 }
362
363 del_invite(chptr, source_p);
364
365 if(chptr->topic != NULL)
366 {
367 sendto_one(source_p, form_str(RPL_TOPIC), me.name,
368 source_p->name, chptr->chname, chptr->topic);
369
370 sendto_one(source_p, form_str(RPL_TOPICWHOTIME),
371 me.name, source_p->name, chptr->chname,
372 chptr->topic_info,
373 (unsigned long)chptr->topic_time);
374 }
375
376 channel_member_names(chptr, source_p, 1);
377
378 hook_info.client = source_p;
379 hook_info.chptr = chptr;
380 hook_info.key = key;
381 call_hook(h_channel_join, &hook_info);
382 }
383 }
384
385 /*
386 * ms_join
387 * parv[1] = channel TS
388 * parv[2] = channel
389 * parv[3] = "+", formerly channel modes but now unused
390 * alternatively, a single "0" parameter parts all channels
391 */
392 static void
393 ms_join(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
394 {
395 struct Channel *chptr;
396 static struct Mode mode;
397 time_t oldts;
398 time_t newts;
399 int isnew;
400 bool keep_our_modes = true;
401 rb_dlink_node *ptr, *next_ptr;
402
403 /* special case for join 0 */
404 if((parv[1][0] == '0') && (parv[1][1] == '\0') && parc == 2)
405 {
406 do_join_0(client_p, source_p);
407 return;
408 }
409
410 if(parc < 4)
411 return;
412
413 if(!IsChannelName(parv[2]) || !check_channel_name(parv[2]))
414 return;
415
416 /* joins for local channels cant happen. */
417 if(parv[2][0] == '&')
418 return;
419
420 mbuf = modebuf;
421 mode.key[0] = mode.forward[0] = '\0';
422 mode.mode = mode.limit = mode.join_num = mode.join_time = 0;
423
424 if((chptr = get_or_create_channel(source_p, parv[2], &isnew)) == NULL)
425 return;
426
427 newts = atol(parv[1]);
428 oldts = chptr->channelts;
429
430 /* making a channel TS0 */
431 if(!isnew && !newts && oldts)
432 {
433 sendto_channel_local(ALL_MEMBERS, chptr,
434 ":%s NOTICE %s :*** Notice -- TS for %s changed from %ld to 0",
435 me.name, chptr->chname, chptr->chname, (long) oldts);
436 sendto_realops_snomask(SNO_GENERAL, L_ALL,
437 "Server %s changing TS on %s from %ld to 0",
438 source_p->name, chptr->chname, (long) oldts);
439 }
440
441 if(isnew)
442 chptr->channelts = newts;
443 else if(newts == 0 || oldts == 0)
444 chptr->channelts = 0;
445 else if(newts == oldts)
446 ;
447 else if(newts < oldts)
448 {
449 keep_our_modes = false;
450 chptr->channelts = newts;
451 }
452
453 /* Lost the TS, other side wins, so remove modes on this side */
454 if(!keep_our_modes)
455 {
456 set_final_mode(&mode, &chptr->mode);
457 chptr->mode = mode;
458 remove_our_modes(chptr, source_p);
459 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, chptr->invites.head)
460 {
461 del_invite(chptr, ptr->data);
462 }
463 /* If setting -j, clear join throttle state -- jilles */
464 chptr->join_count = chptr->join_delta = 0;
465 sendto_channel_local(ALL_MEMBERS, chptr,
466 ":%s NOTICE %s :*** Notice -- TS for %s changed from %ld to %ld",
467 me.name, chptr->chname, chptr->chname,
468 (long) oldts, (long) newts);
469 /* Update capitalization in channel name, this makes the
470 * capitalization timestamped like modes are -- jilles */
471 strcpy(chptr->chname, parv[2]);
472 if(*modebuf != '\0')
473 sendto_channel_local(ALL_MEMBERS, chptr,
474 ":%s MODE %s %s %s",
475 source_p->servptr->name,
476 chptr->chname, modebuf, parabuf);
477 *modebuf = *parabuf = '\0';
478
479 /* since we're dropping our modes, we want to clear the mlock as well. --nenolod */
480 set_channel_mlock(client_p, source_p, chptr, NULL, false);
481 }
482
483 if(!IsMember(source_p, chptr))
484 {
485 add_user_to_channel(chptr, source_p, CHFL_PEON);
486 if (chptr->mode.join_num &&
487 rb_current_time() - chptr->join_delta >= chptr->mode.join_time)
488 {
489 chptr->join_count = 0;
490 chptr->join_delta = rb_current_time();
491 }
492 chptr->join_count++;
493 send_channel_join(chptr, source_p);
494 }
495
496 sendto_server(client_p, chptr, CAP_TS6, NOCAPS,
497 ":%s JOIN %ld %s +",
498 source_p->id, (long) chptr->channelts, chptr->chname);
499 }
500
501 static void
502 ms_sjoin(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
503 {
504 static char buf_uid[BUFSIZE];
505 static const char empty_modes[] = "0";
506 struct Channel *chptr;
507 struct Client *target_p, *fakesource_p;
508 time_t newts;
509 time_t oldts;
510 static struct Mode mode, *oldmode;
511 const char *modes;
512 int args = 0;
513 bool keep_our_modes = true;
514 bool keep_new_modes = true;
515 int fl;
516 int isnew;
517 int mlen_uid;
518 int len_uid;
519 int len;
520 int joins = 0;
521 const char *s;
522 char *ptr_uid;
523 char *p;
524 int i, joinc = 0, timeslice = 0;
525 static char empty[] = "";
526 rb_dlink_node *ptr, *next_ptr;
527
528 if(parc < 5)
529 return;
530
531 if(!IsChannelName(parv[2]) || !check_channel_name(parv[2]))
532 return;
533
534 /* SJOIN's for local channels can't happen. */
535 if(*parv[2] == '&')
536 return;
537
538 modebuf[0] = parabuf[0] = mode.key[0] = mode.forward[0] = '\0';
539 pargs = mode.mode = mode.limit = mode.join_num = mode.join_time = 0;
540
541 /* Hide connecting server on netburst -- jilles */
542 if (ConfigServerHide.flatten_links && !HasSentEob(source_p))
543 fakesource_p = &me;
544 else
545 fakesource_p = source_p;
546
547 mbuf = modebuf;
548 newts = atol(parv[1]);
549
550 s = parv[3];
551 while (*s)
552 {
553 switch (*(s++))
554 {
555 case 'f':
556 rb_strlcpy(mode.forward, parv[4 + args], sizeof(mode.forward));
557 args++;
558 if(parc < 5 + args)
559 return;
560 break;
561 case 'j':
562 sscanf(parv[4 + args], "%d:%d", &joinc, &timeslice);
563 args++;
564 mode.join_num = joinc;
565 mode.join_time = timeslice;
566 if(parc < 5 + args)
567 return;
568 break;
569 case 'k':
570 rb_strlcpy(mode.key, parv[4 + args], sizeof(mode.key));
571 args++;
572 if(parc < 5 + args)
573 return;
574 break;
575 case 'l':
576 mode.limit = atoi(parv[4 + args]);
577 args++;
578 if(parc < 5 + args)
579 return;
580 break;
581 default:
582 if(chmode_flags[(int) *s] != 0)
583 {
584 mode.mode |= chmode_flags[(int) *s];
585 }
586 }
587 }
588
589 if(parv[args + 4])
590 {
591 s = parv[args + 4];
592
593 /* remove any leading spaces */
594 while (*s == ' ')
595 s++;
596 }
597 else
598 s = "";
599
600 if((chptr = get_or_create_channel(source_p, parv[2], &isnew)) == NULL)
601 return; /* channel name too long? */
602
603
604 oldts = chptr->channelts;
605 oldmode = &chptr->mode;
606
607 if(!isnew && !newts && oldts)
608 {
609 sendto_channel_local(ALL_MEMBERS, chptr,
610 ":%s NOTICE %s :*** Notice -- TS for %s "
611 "changed from %ld to 0",
612 me.name, chptr->chname, chptr->chname, (long) oldts);
613 sendto_realops_snomask(SNO_GENERAL, L_ALL,
614 "Server %s changing TS on %s from %ld to 0",
615 source_p->name, chptr->chname, (long) oldts);
616 }
617
618 if(isnew)
619 chptr->channelts = newts;
620
621 else if(newts == 0 || oldts == 0)
622 chptr->channelts = 0;
623 else if(newts == oldts)
624 ;
625 else if(newts < oldts)
626 {
627 /* If configured, kick people trying to join +i/+k
628 * channels by recreating them on split servers.
629 * If the source has sent EOB, assume this is some
630 * sort of hack by services. If cmode +i is set,
631 * services can send kicks if needed; if the key
632 * differs, services cannot kick in a race-free
633 * manner so do so here.
634 * -- jilles */
635 if (ConfigChannel.kick_on_split_riding &&
636 ((!HasSentEob(source_p) &&
637 mode.mode & MODE_INVITEONLY) ||
638 (mode.key[0] != 0 && irccmp(mode.key, oldmode->key) != 0)))
639 {
640 struct membership *msptr;
641 struct Client *who;
642 int l = rb_dlink_list_length(&chptr->members);
643
644 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, chptr->locmembers.head)
645 {
646 msptr = ptr->data;
647 who = msptr->client_p;
648 sendto_one(who, ":%s KICK %s %s :Net Rider",
649 me.name, chptr->chname, who->name);
650
651 sendto_server(NULL, chptr, CAP_TS6, NOCAPS,
652 ":%s KICK %s %s :Net Rider",
653 me.id, chptr->chname,
654 who->id);
655 remove_user_from_channel(msptr);
656 if (--l == 0)
657 break;
658 }
659 if (l == 0)
660 {
661 /* Channel was emptied, create a new one */
662 if((chptr = get_or_create_channel(source_p, parv[2], &isnew)) == NULL)
663 return; /* oops! */
664
665 oldmode = &chptr->mode;
666 }
667 }
668 keep_our_modes = false;
669 chptr->channelts = newts;
670 }
671 else
672 keep_new_modes = false;
673
674 if(!keep_new_modes)
675 mode = *oldmode;
676 else if(keep_our_modes)
677 {
678 mode.mode |= oldmode->mode;
679 if(oldmode->limit > mode.limit)
680 mode.limit = oldmode->limit;
681 if(strcmp(mode.key, oldmode->key) < 0)
682 strcpy(mode.key, oldmode->key);
683 if(oldmode->join_num > mode.join_num ||
684 (oldmode->join_num == mode.join_num &&
685 oldmode->join_time > mode.join_time))
686 {
687 mode.join_num = oldmode->join_num;
688 mode.join_time = oldmode->join_time;
689 }
690 if(irccmp(mode.forward, oldmode->forward) < 0)
691 strcpy(mode.forward, oldmode->forward);
692 }
693 else
694 {
695 /* If setting -j, clear join throttle state -- jilles */
696 if (!mode.join_num)
697 chptr->join_count = chptr->join_delta = 0;
698 }
699
700 set_final_mode(&mode, oldmode);
701 chptr->mode = mode;
702
703 /* Lost the TS, other side wins, so remove modes on this side */
704 if(!keep_our_modes)
705 {
706 remove_our_modes(chptr, fakesource_p);
707 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, chptr->invites.head)
708 {
709 del_invite(chptr, ptr->data);
710 }
711
712 if(rb_dlink_list_length(&chptr->banlist) > 0)
713 remove_ban_list(chptr, fakesource_p, &chptr->banlist, 'b', ALL_MEMBERS);
714 if(rb_dlink_list_length(&chptr->exceptlist) > 0)
715 remove_ban_list(chptr, fakesource_p, &chptr->exceptlist,
716 'e', ONLY_CHANOPS);
717 if(rb_dlink_list_length(&chptr->invexlist) > 0)
718 remove_ban_list(chptr, fakesource_p, &chptr->invexlist,
719 'I', ONLY_CHANOPS);
720 if(rb_dlink_list_length(&chptr->quietlist) > 0)
721 remove_ban_list(chptr, fakesource_p, &chptr->quietlist,
722 'q', ALL_MEMBERS);
723 chptr->bants++;
724
725 sendto_channel_local(ALL_MEMBERS, chptr,
726 ":%s NOTICE %s :*** Notice -- TS for %s changed from %ld to %ld",
727 me.name, chptr->chname, chptr->chname,
728 (long) oldts, (long) newts);
729 /* Update capitalization in channel name, this makes the
730 * capitalization timestamped like modes are -- jilles */
731 strcpy(chptr->chname, parv[2]);
732
733 /* since we're dropping our modes, we want to clear the mlock as well. --nenolod */
734 set_channel_mlock(client_p, source_p, chptr, NULL, false);
735 }
736
737 if(*modebuf != '\0')
738 sendto_channel_local(ALL_MEMBERS, chptr, ":%s MODE %s %s %s",
739 fakesource_p->name, chptr->chname, modebuf, parabuf);
740
741 *modebuf = *parabuf = '\0';
742
743 if(parv[3][0] != '0' && keep_new_modes)
744 modes = channel_modes(chptr, source_p);
745 else
746 modes = empty_modes;
747
748 mlen_uid = sprintf(buf_uid, ":%s SJOIN %ld %s %s :",
749 use_id(source_p), (long) chptr->channelts, parv[2], modes);
750 ptr_uid = buf_uid + mlen_uid;
751
752 mbuf = modebuf;
753 para[0] = para[1] = para[2] = para[3] = empty;
754 pargs = 0;
755 len_uid = 0;
756
757 /* if theres a space, theres going to be more than one nick, change the
758 * first space to \0, so s is just the first nick, and point p to the
759 * second nick
760 */
761 if((p = strchr(s, ' ')) != NULL)
762 {
763 *p++ = '\0';
764 }
765
766 *mbuf++ = '+';
767
768 while (s)
769 {
770 fl = 0;
771
772 for (i = 0; i < 2; i++)
773 {
774 if(*s == '@')
775 {
776 fl |= CHFL_CHANOP;
777 s++;
778 }
779 else if(*s == '+')
780 {
781 fl |= CHFL_VOICE;
782 s++;
783 }
784 }
785
786 /* if the client doesnt exist or is fake direction, skip. */
787 if(!(target_p = find_client(s)) ||
788 (target_p->from != client_p) || !IsPerson(target_p))
789 goto nextnick;
790
791 /* we assume for these we can fit at least one nick/uid in.. */
792
793 /* check we can fit another status+nick+space into a buffer */
794 if((mlen_uid + len_uid + IDLEN + 3) > (BUFSIZE - 3))
795 {
796 *(ptr_uid - 1) = '\0';
797 sendto_server(client_p->from, NULL, CAP_TS6, NOCAPS, "%s", buf_uid);
798 ptr_uid = buf_uid + mlen_uid;
799 len_uid = 0;
800 }
801
802 if(keep_new_modes)
803 {
804 if(fl & CHFL_CHANOP)
805 {
806 *ptr_uid++ = '@';
807 len_uid++;
808 }
809 if(fl & CHFL_VOICE)
810 {
811 *ptr_uid++ = '+';
812 len_uid++;
813 }
814 }
815
816 /* copy the nick to the two buffers */
817 len = sprintf(ptr_uid, "%s ", use_id(target_p));
818 ptr_uid += len;
819 len_uid += len;
820
821 if(!keep_new_modes)
822 fl = 0;
823
824 if(!IsMember(target_p, chptr))
825 {
826 add_user_to_channel(chptr, target_p, fl);
827 send_channel_join(chptr, target_p);
828 joins++;
829 }
830
831 if(fl & CHFL_CHANOP)
832 {
833 *mbuf++ = 'o';
834 para[pargs++] = target_p->name;
835
836 /* a +ov user.. bleh */
837 if(fl & CHFL_VOICE)
838 {
839 /* its possible the +o has filled up MAXMODEPARAMS, if so, start
840 * a new buffer
841 */
842 if(pargs >= MAXMODEPARAMS)
843 {
844 *mbuf = '\0';
845 sendto_channel_local(ALL_MEMBERS, chptr,
846 ":%s MODE %s %s %s %s %s %s",
847 fakesource_p->name, chptr->chname,
848 modebuf,
849 para[0], para[1], para[2], para[3]);
850 mbuf = modebuf;
851 *mbuf++ = '+';
852 para[0] = para[1] = para[2] = para[3] = NULL;
853 pargs = 0;
854 }
855
856 *mbuf++ = 'v';
857 para[pargs++] = target_p->name;
858 }
859 }
860 else if(fl & CHFL_VOICE)
861 {
862 *mbuf++ = 'v';
863 para[pargs++] = target_p->name;
864 }
865
866 if(pargs >= MAXMODEPARAMS)
867 {
868 *mbuf = '\0';
869 sendto_channel_local(ALL_MEMBERS, chptr,
870 ":%s MODE %s %s %s %s %s %s",
871 fakesource_p->name,
872 chptr->chname,
873 modebuf, para[0], para[1], para[2], para[3]);
874 mbuf = modebuf;
875 *mbuf++ = '+';
876 para[0] = para[1] = para[2] = para[3] = NULL;
877 pargs = 0;
878 }
879
880 nextnick:
881 /* p points to the next nick */
882 s = p;
883
884 /* if there was a trailing space and p was pointing to it, then we
885 * need to exit.. this has the side effect of breaking double spaces
886 * in an sjoin.. but that shouldnt happen anyway
887 */
888 if(s && (*s == '\0'))
889 s = p = NULL;
890
891 /* if p was NULL due to no spaces, s wont exist due to the above, so
892 * we cant check it for spaces.. if there are no spaces, then when
893 * we next get here, s will be NULL
894 */
895 if(s && ((p = strchr(s, ' ')) != NULL))
896 {
897 *p++ = '\0';
898 }
899 }
900
901 *mbuf = '\0';
902 if(pargs)
903 {
904 sendto_channel_local(ALL_MEMBERS, chptr,
905 ":%s MODE %s %s %s %s %s %s",
906 fakesource_p->name, chptr->chname, modebuf,
907 para[0], CheckEmpty(para[1]),
908 CheckEmpty(para[2]), CheckEmpty(para[3]));
909 }
910
911 if(!joins && !(chptr->mode.mode & MODE_PERMANENT) && isnew)
912 {
913 destroy_channel(chptr);
914
915 return;
916 }
917
918 /* Keep the colon if we're sending an SJOIN without nicks -- jilles */
919 if (joins)
920 {
921 *(ptr_uid - 1) = '\0';
922 }
923
924 sendto_server(client_p->from, NULL, CAP_TS6, NOCAPS, "%s", buf_uid);
925 }
926
927 /*
928 * do_join_0
929 *
930 * inputs - pointer to client doing join 0
931 * output - NONE
932 * side effects - Use has decided to join 0. This is legacy
933 * from the days when channels were numbers not names. *sigh*
934 */
935 static void
936 do_join_0(struct Client *client_p, struct Client *source_p)
937 {
938 struct membership *msptr;
939 struct Channel *chptr = NULL;
940 rb_dlink_node *ptr;
941
942 /* Finish the flood grace period... */
943 if(MyClient(source_p) && !IsFloodDone(source_p))
944 flood_endgrace(source_p);
945
946 sendto_server(client_p, NULL, CAP_TS6, NOCAPS, ":%s JOIN 0", use_id(source_p));
947
948 while((ptr = source_p->user->channel.head))
949 {
950 if(MyConnect(source_p) &&
951 !IsOper(source_p) && !IsExemptSpambot(source_p))
952 check_spambot_warning(source_p, NULL);
953
954 msptr = ptr->data;
955 chptr = msptr->chptr;
956 sendto_channel_local(ALL_MEMBERS, chptr, ":%s!%s@%s PART %s",
957 source_p->name,
958 source_p->username, source_p->host, chptr->chname);
959 remove_user_from_channel(msptr);
960 }
961 }
962
963 static bool
964 check_channel_name_loc(struct Client *source_p, const char *name)
965 {
966 const char *p;
967
968 s_assert(name != NULL);
969 if(EmptyString(name))
970 return false;
971
972 if(ConfigFileEntry.disable_fake_channels && !IsOper(source_p))
973 {
974 for(p = name; *p; ++p)
975 {
976 if(!IsChanChar(*p) || IsFakeChanChar(*p))
977 return false;
978 }
979 }
980 else
981 {
982 for(p = name; *p; ++p)
983 {
984 if(!IsChanChar(*p))
985 return false;
986 }
987 }
988
989 if(ConfigChannel.only_ascii_channels)
990 {
991 for(p = name; *p; ++p)
992 if(*p < 33 || *p > 126)
993 return false;
994 }
995
996 return true;
997 }
998
999 /* send_join_error()
1000 *
1001 * input - client to send to, reason, channel name
1002 * output - none
1003 * side effects - error message sent to client
1004 */
1005 static void
1006 send_join_error(struct Client *source_p, int numeric, const char *name)
1007 {
1008 /* This stuff is necessary because the form_str macro only
1009 * accepts constants.
1010 */
1011 switch (numeric)
1012 {
1013 #define NORMAL_NUMERIC(i) \
1014 case i: \
1015 sendto_one(source_p, form_str(i), \
1016 me.name, source_p->name, name); \
1017 break
1018
1019 NORMAL_NUMERIC(ERR_BANNEDFROMCHAN);
1020 NORMAL_NUMERIC(ERR_INVITEONLYCHAN);
1021 NORMAL_NUMERIC(ERR_BADCHANNELKEY);
1022 NORMAL_NUMERIC(ERR_CHANNELISFULL);
1023 NORMAL_NUMERIC(ERR_NEEDREGGEDNICK);
1024 NORMAL_NUMERIC(ERR_THROTTLE);
1025
1026 default:
1027 sendto_one_numeric(source_p, numeric,
1028 "%s :Cannot join channel", name);
1029 break;
1030 }
1031 }
1032
1033 static void
1034 set_final_mode(struct Mode *mode, struct Mode *oldmode)
1035 {
1036 int dir = MODE_QUERY;
1037 char *pbuf = parabuf;
1038 int len;
1039 int i;
1040
1041 /* ok, first get a list of modes we need to add */
1042 for (i = 0; i < 256; i++)
1043 {
1044 if((mode->mode & chmode_flags[i]) && !(oldmode->mode & chmode_flags[i]))
1045 {
1046 if(dir != MODE_ADD)
1047 {
1048 *mbuf++ = '+';
1049 dir = MODE_ADD;
1050 }
1051 *mbuf++ = i;
1052 }
1053 }
1054
1055 /* now the ones we need to remove. */
1056 for (i = 0; i < 256; i++)
1057 {
1058 if((oldmode->mode & chmode_flags[i]) && !(mode->mode & chmode_flags[i]))
1059 {
1060 if(dir != MODE_DEL)
1061 {
1062 *mbuf++ = '-';
1063 dir = MODE_DEL;
1064 }
1065 *mbuf++ = i;
1066 }
1067 }
1068
1069 if(oldmode->limit && !mode->limit)
1070 {
1071 if(dir != MODE_DEL)
1072 {
1073 *mbuf++ = '-';
1074 dir = MODE_DEL;
1075 }
1076 *mbuf++ = 'l';
1077 }
1078 if(oldmode->key[0] && !mode->key[0])
1079 {
1080 if(dir != MODE_DEL)
1081 {
1082 *mbuf++ = '-';
1083 dir = MODE_DEL;
1084 }
1085 *mbuf++ = 'k';
1086 len = sprintf(pbuf, "%s ", oldmode->key);
1087 pbuf += len;
1088 }
1089 if(oldmode->join_num && !mode->join_num)
1090 {
1091 if(dir != MODE_DEL)
1092 {
1093 *mbuf++ = '-';
1094 dir = MODE_DEL;
1095 }
1096 *mbuf++ = 'j';
1097 }
1098 if(oldmode->forward[0] && !mode->forward[0])
1099 {
1100 if(dir != MODE_DEL)
1101 {
1102 *mbuf++ = '-';
1103 dir = MODE_DEL;
1104 }
1105 *mbuf++ = 'f';
1106 }
1107 if(mode->limit && oldmode->limit != mode->limit)
1108 {
1109 if(dir != MODE_ADD)
1110 {
1111 *mbuf++ = '+';
1112 dir = MODE_ADD;
1113 }
1114 *mbuf++ = 'l';
1115 len = sprintf(pbuf, "%d ", mode->limit);
1116 pbuf += len;
1117 }
1118 if(mode->key[0] && strcmp(oldmode->key, mode->key))
1119 {
1120 if(dir != MODE_ADD)
1121 {
1122 *mbuf++ = '+';
1123 dir = MODE_ADD;
1124 }
1125 *mbuf++ = 'k';
1126 len = sprintf(pbuf, "%s ", mode->key);
1127 pbuf += len;
1128 }
1129 if(mode->join_num && (oldmode->join_num != mode->join_num || oldmode->join_time != mode->join_time))
1130 {
1131 if(dir != MODE_ADD)
1132 {
1133 *mbuf++ = '+';
1134 dir = MODE_ADD;
1135 }
1136 *mbuf++ = 'j';
1137 len = sprintf(pbuf, "%d:%d ", mode->join_num, mode->join_time);
1138 pbuf += len;
1139 }
1140 if(mode->forward[0] && strcmp(oldmode->forward, mode->forward) &&
1141 ConfigChannel.use_forward)
1142 {
1143 if(dir != MODE_ADD)
1144 {
1145 *mbuf++ = '+';
1146 dir = MODE_ADD;
1147 }
1148 *mbuf++ = 'f';
1149 len = sprintf(pbuf, "%s ", mode->forward);
1150 pbuf += len;
1151 }
1152 *mbuf = '\0';
1153 }
1154
1155 /*
1156 * remove_our_modes
1157 *
1158 * inputs -
1159 * output -
1160 * side effects -
1161 */
1162 static void
1163 remove_our_modes(struct Channel *chptr, struct Client *source_p)
1164 {
1165 struct membership *msptr;
1166 rb_dlink_node *ptr;
1167 char lmodebuf[MODEBUFLEN];
1168 char *lpara[MAXMODEPARAMS];
1169 int count = 0;
1170 int i;
1171
1172 mbuf = lmodebuf;
1173 *mbuf++ = '-';
1174
1175 for(i = 0; i < MAXMODEPARAMS; i++)
1176 lpara[i] = NULL;
1177
1178 RB_DLINK_FOREACH(ptr, chptr->members.head)
1179 {
1180 msptr = ptr->data;
1181
1182 if(is_chanop(msptr))
1183 {
1184 msptr->flags &= ~CHFL_CHANOP;
1185 lpara[count++] = msptr->client_p->name;
1186 *mbuf++ = 'o';
1187
1188 /* +ov, might not fit so check. */
1189 if(is_voiced(msptr))
1190 {
1191 if(count >= MAXMODEPARAMS)
1192 {
1193 *mbuf = '\0';
1194 sendto_channel_local(ALL_MEMBERS, chptr,
1195 ":%s MODE %s %s %s %s %s %s",
1196 source_p->name, chptr->chname,
1197 lmodebuf, lpara[0], lpara[1],
1198 lpara[2], lpara[3]);
1199
1200 /* preserve the initial '-' */
1201 mbuf = lmodebuf;
1202 *mbuf++ = '-';
1203 count = 0;
1204
1205 for(i = 0; i < MAXMODEPARAMS; i++)
1206 lpara[i] = NULL;
1207 }
1208
1209 msptr->flags &= ~CHFL_VOICE;
1210 lpara[count++] = msptr->client_p->name;
1211 *mbuf++ = 'v';
1212 }
1213 }
1214 else if(is_voiced(msptr))
1215 {
1216 msptr->flags &= ~CHFL_VOICE;
1217 lpara[count++] = msptr->client_p->name;
1218 *mbuf++ = 'v';
1219 }
1220 else
1221 continue;
1222
1223 if(count >= MAXMODEPARAMS)
1224 {
1225 *mbuf = '\0';
1226 sendto_channel_local(ALL_MEMBERS, chptr,
1227 ":%s MODE %s %s %s %s %s %s",
1228 source_p->name, chptr->chname, lmodebuf,
1229 lpara[0], lpara[1], lpara[2], lpara[3]);
1230 mbuf = lmodebuf;
1231 *mbuf++ = '-';
1232 count = 0;
1233
1234 for(i = 0; i < MAXMODEPARAMS; i++)
1235 lpara[i] = NULL;
1236 }
1237 }
1238
1239 if(count != 0)
1240 {
1241 *mbuf = '\0';
1242 sendto_channel_local(ALL_MEMBERS, chptr,
1243 ":%s MODE %s %s %s %s %s %s",
1244 source_p->name, chptr->chname, lmodebuf,
1245 EmptyString(lpara[0]) ? "" : lpara[0],
1246 EmptyString(lpara[1]) ? "" : lpara[1],
1247 EmptyString(lpara[2]) ? "" : lpara[2],
1248 EmptyString(lpara[3]) ? "" : lpara[3]);
1249
1250 }
1251 }
1252
1253 /* remove_ban_list()
1254 *
1255 * inputs - channel, source, list to remove, char of mode, caps needed
1256 * outputs -
1257 * side effects - given list is removed, with modes issued to local clients
1258 */
1259 static void
1260 remove_ban_list(struct Channel *chptr, struct Client *source_p,
1261 rb_dlink_list * list, char c, int mems)
1262 {
1263 static char lmodebuf[BUFSIZE];
1264 static char lparabuf[BUFSIZE];
1265 struct Ban *banptr;
1266 rb_dlink_node *ptr;
1267 rb_dlink_node *next_ptr;
1268 char *pbuf;
1269 int count = 0;
1270 int cur_len, mlen, plen;
1271
1272 pbuf = lparabuf;
1273
1274 cur_len = mlen = sprintf(lmodebuf, ":%s MODE %s -", source_p->name, chptr->chname);
1275 mbuf = lmodebuf + mlen;
1276
1277 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, list->head)
1278 {
1279 banptr = ptr->data;
1280
1281 /* trailing space, and the mode letter itself */
1282 plen = strlen(banptr->banstr) +
1283 (banptr->forward ? strlen(banptr->forward) + 1 : 0) + 2;
1284
1285 if(count >= MAXMODEPARAMS || (cur_len + plen) > BUFSIZE - 4)
1286 {
1287 /* remove trailing space */
1288 *mbuf = '\0';
1289 *(pbuf - 1) = '\0';
1290
1291 sendto_channel_local(mems, chptr, "%s %s", lmodebuf, lparabuf);
1292
1293 cur_len = mlen;
1294 mbuf = lmodebuf + mlen;
1295 pbuf = lparabuf;
1296 count = 0;
1297 }
1298
1299 *mbuf++ = c;
1300 cur_len += plen;
1301 if (banptr->forward)
1302 pbuf += sprintf(pbuf, "%s$%s ", banptr->banstr, banptr->forward);
1303 else
1304 pbuf += sprintf(pbuf, "%s ", banptr->banstr);
1305 count++;
1306
1307 free_ban(banptr);
1308 }
1309
1310 *mbuf = '\0';
1311 *(pbuf - 1) = '\0';
1312 sendto_channel_local(mems, chptr, "%s %s", lmodebuf, lparabuf);
1313
1314 list->head = list->tail = NULL;
1315 list->length = 0;
1316 }