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