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