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