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