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