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