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