]> jfr.im git - irc/rqf/shadowircd.git/blob - modules/core/m_join.c
b72f8303e75be2c892cdc473121276685f0bac6b
[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 /* working on the presumption eventually itll be more efficient to
770 * build a TS6 buffer without checking its needed..
771 */
772 mlen_uid = rb_sprintf(buf_uid, ":%s SJOIN %ld %s %s :",
773 use_id(source_p), (long) chptr->channelts, parv[2], modes);
774 ptr_uid = buf_uid + mlen_uid;
775
776 mbuf = modebuf;
777 para[0] = para[1] = para[2] = para[3] = empty;
778 pargs = 0;
779 len_nick = len_uid = 0;
780
781 /* if theres a space, theres going to be more than one nick, change the
782 * first space to \0, so s is just the first nick, and point p to the
783 * second nick
784 */
785 if((p = strchr(s, ' ')) != NULL)
786 {
787 *p++ = '\0';
788 }
789
790 *mbuf++ = '+';
791
792 while (s)
793 {
794 fl = 0;
795
796 for (i = 0; i < 2; i++)
797 {
798 if(*s == '@')
799 {
800 fl |= CHFL_CHANOP;
801 s++;
802 }
803 else if(*s == '+')
804 {
805 fl |= CHFL_VOICE;
806 s++;
807 }
808 }
809
810 /* if the client doesnt exist or is fake direction, skip. */
811 if(!(target_p = find_client(s)) ||
812 (target_p->from != client_p) || !IsPerson(target_p))
813 goto nextnick;
814
815 /* we assume for these we can fit at least one nick/uid in.. */
816
817 /* check we can fit another status+nick+space into a buffer */
818 if((mlen_uid + len_uid + IDLEN + 3) > (BUFSIZE - 3))
819 {
820 *(ptr_uid - 1) = '\0';
821 sendto_server(client_p->from, NULL, CAP_TS6, NOCAPS, "%s", buf_uid);
822 ptr_uid = buf_uid + mlen_uid;
823 len_uid = 0;
824 }
825
826 if(keep_new_modes)
827 {
828 if(fl & CHFL_CHANOP)
829 {
830 *ptr_uid++ = '@';
831 len_nick++;
832 len_uid++;
833 }
834 if(fl & CHFL_VOICE)
835 {
836 *ptr_uid++ = '+';
837 len_nick++;
838 len_uid++;
839 }
840 }
841
842 /* copy the nick to the two buffers */
843 len = rb_sprintf(ptr_uid, "%s ", use_id(target_p));
844 ptr_uid += len;
845 len_uid += len;
846
847 if(!keep_new_modes)
848 fl = 0;
849
850 if(!IsMember(target_p, chptr))
851 {
852 add_user_to_channel(chptr, target_p, fl);
853 sendto_channel_local(ALL_MEMBERS, chptr, ":%s!%s@%s JOIN :%s",
854 target_p->name,
855 target_p->username, target_p->host, parv[2]);
856 joins++;
857 }
858
859 if(fl & CHFL_CHANOP)
860 {
861 *mbuf++ = 'o';
862 para[pargs++] = target_p->name;
863
864 /* a +ov user.. bleh */
865 if(fl & CHFL_VOICE)
866 {
867 /* its possible the +o has filled up MAXMODEPARAMS, if so, start
868 * a new buffer
869 */
870 if(pargs >= MAXMODEPARAMS)
871 {
872 *mbuf = '\0';
873 sendto_channel_local(ALL_MEMBERS, chptr,
874 ":%s MODE %s %s %s %s %s %s",
875 fakesource_p->name, chptr->chname,
876 modebuf,
877 para[0], para[1], para[2], para[3]);
878 mbuf = modebuf;
879 *mbuf++ = '+';
880 para[0] = para[1] = para[2] = para[3] = NULL;
881 pargs = 0;
882 }
883
884 *mbuf++ = 'v';
885 para[pargs++] = target_p->name;
886 }
887 }
888 else if(fl & CHFL_VOICE)
889 {
890 *mbuf++ = 'v';
891 para[pargs++] = target_p->name;
892 }
893
894 if(pargs >= MAXMODEPARAMS)
895 {
896 *mbuf = '\0';
897 sendto_channel_local(ALL_MEMBERS, chptr,
898 ":%s MODE %s %s %s %s %s %s",
899 fakesource_p->name,
900 chptr->chname,
901 modebuf, para[0], para[1], para[2], para[3]);
902 mbuf = modebuf;
903 *mbuf++ = '+';
904 para[0] = para[1] = para[2] = para[3] = NULL;
905 pargs = 0;
906 }
907
908 nextnick:
909 /* p points to the next nick */
910 s = p;
911
912 /* if there was a trailing space and p was pointing to it, then we
913 * need to exit.. this has the side effect of breaking double spaces
914 * in an sjoin.. but that shouldnt happen anyway
915 */
916 if(s && (*s == '\0'))
917 s = p = NULL;
918
919 /* if p was NULL due to no spaces, s wont exist due to the above, so
920 * we cant check it for spaces.. if there are no spaces, then when
921 * we next get here, s will be NULL
922 */
923 if(s && ((p = strchr(s, ' ')) != NULL))
924 {
925 *p++ = '\0';
926 }
927 }
928
929 *mbuf = '\0';
930 if(pargs)
931 {
932 sendto_channel_local(ALL_MEMBERS, chptr,
933 ":%s MODE %s %s %s %s %s %s",
934 fakesource_p->name, chptr->chname, modebuf,
935 para[0], CheckEmpty(para[1]),
936 CheckEmpty(para[2]), CheckEmpty(para[3]));
937 }
938
939 if(!joins && !(chptr->mode.mode & MODE_PERMANENT) && isnew)
940 {
941 destroy_channel(chptr);
942
943 return 0;
944 }
945
946 /* Keep the colon if we're sending an SJOIN without nicks -- jilles */
947 if (joins)
948 {
949 *(ptr_uid - 1) = '\0';
950 }
951
952 sendto_server(client_p->from, NULL, CAP_TS6, NOCAPS, "%s", buf_uid);
953
954 return 0;
955 }
956
957 /*
958 * do_join_0
959 *
960 * inputs - pointer to client doing join 0
961 * output - NONE
962 * side effects - Use has decided to join 0. This is legacy
963 * from the days when channels were numbers not names. *sigh*
964 * There is a bunch of evilness necessary here due to
965 * anti spambot code.
966 */
967 static void
968 do_join_0(struct Client *client_p, struct Client *source_p)
969 {
970 struct membership *msptr;
971 struct Channel *chptr = NULL;
972 rb_dlink_node *ptr;
973
974 /* Finish the flood grace period... */
975 if(MyClient(source_p) && !IsFloodDone(source_p))
976 flood_endgrace(source_p);
977
978 sendto_server(client_p, NULL, CAP_TS6, NOCAPS, ":%s JOIN 0", use_id(source_p));
979
980 if(source_p->user->channel.head && MyConnect(source_p) &&
981 !IsOper(source_p) && !IsExemptSpambot(source_p))
982 check_spambot_warning(source_p, NULL);
983
984 while((ptr = source_p->user->channel.head))
985 {
986 msptr = ptr->data;
987 chptr = msptr->chptr;
988 sendto_channel_local(ALL_MEMBERS, chptr, ":%s!%s@%s PART %s",
989 source_p->name,
990 source_p->username, source_p->host, chptr->chname);
991 remove_user_from_channel(msptr);
992 }
993 }
994
995 static int
996 check_channel_name_loc(struct Client *source_p, const char *name)
997 {
998 s_assert(name != NULL);
999 if(EmptyString(name))
1000 return 0;
1001
1002 if(ConfigFileEntry.disable_fake_channels && !IsOper(source_p))
1003 {
1004 for(; *name; ++name)
1005 {
1006 if(!IsChanChar(*name) || IsFakeChanChar(*name))
1007 return 0;
1008 }
1009 }
1010 else
1011 {
1012 for(; *name; ++name)
1013 {
1014 if(!IsChanChar(*name))
1015 return 0;
1016 }
1017 }
1018
1019 return 1;
1020 }
1021
1022 static void
1023 set_final_mode(struct Mode *mode, struct Mode *oldmode)
1024 {
1025 int dir = MODE_QUERY;
1026 char *pbuf = parabuf;
1027 int len;
1028 int i;
1029
1030 /* ok, first get a list of modes we need to add */
1031 for (i = 0; i < 256; i++)
1032 {
1033 if((mode->mode & chmode_flags[i]) && !(oldmode->mode & chmode_flags[i]))
1034 {
1035 if(dir != MODE_ADD)
1036 {
1037 *mbuf++ = '+';
1038 dir = MODE_ADD;
1039 }
1040 *mbuf++ = i;
1041 }
1042 }
1043
1044 /* now the ones we need to remove. */
1045 for (i = 0; i < 256; i++)
1046 {
1047 if((oldmode->mode & chmode_flags[i]) && !(mode->mode & chmode_flags[i]))
1048 {
1049 if(dir != MODE_DEL)
1050 {
1051 *mbuf++ = '-';
1052 dir = MODE_DEL;
1053 }
1054 *mbuf++ = i;
1055 }
1056 }
1057
1058 if(oldmode->limit && !mode->limit)
1059 {
1060 if(dir != MODE_DEL)
1061 {
1062 *mbuf++ = '-';
1063 dir = MODE_DEL;
1064 }
1065 *mbuf++ = 'l';
1066 }
1067 if(oldmode->key[0] && !mode->key[0])
1068 {
1069 if(dir != MODE_DEL)
1070 {
1071 *mbuf++ = '-';
1072 dir = MODE_DEL;
1073 }
1074 *mbuf++ = 'k';
1075 len = rb_sprintf(pbuf, "%s ", oldmode->key);
1076 pbuf += len;
1077 }
1078 if(oldmode->join_num && !mode->join_num)
1079 {
1080 if(dir != MODE_DEL)
1081 {
1082 *mbuf++ = '-';
1083 dir = MODE_DEL;
1084 }
1085 *mbuf++ = 'j';
1086 }
1087 if(oldmode->forward[0] && !mode->forward[0])
1088 {
1089 if(dir != MODE_DEL)
1090 {
1091 *mbuf++ = '-';
1092 dir = MODE_DEL;
1093 }
1094 *mbuf++ = 'f';
1095 }
1096 if(mode->limit && oldmode->limit != mode->limit)
1097 {
1098 if(dir != MODE_ADD)
1099 {
1100 *mbuf++ = '+';
1101 dir = MODE_ADD;
1102 }
1103 *mbuf++ = 'l';
1104 len = rb_sprintf(pbuf, "%d ", mode->limit);
1105 pbuf += len;
1106 }
1107 if(mode->key[0] && strcmp(oldmode->key, mode->key))
1108 {
1109 if(dir != MODE_ADD)
1110 {
1111 *mbuf++ = '+';
1112 dir = MODE_ADD;
1113 }
1114 *mbuf++ = 'k';
1115 len = rb_sprintf(pbuf, "%s ", mode->key);
1116 pbuf += len;
1117 }
1118 if(mode->join_num && (oldmode->join_num != mode->join_num || oldmode->join_time != mode->join_time))
1119 {
1120 if(dir != MODE_ADD)
1121 {
1122 *mbuf++ = '+';
1123 dir = MODE_ADD;
1124 }
1125 *mbuf++ = 'j';
1126 len = rb_sprintf(pbuf, "%d:%d ", mode->join_num, mode->join_time);
1127 pbuf += len;
1128 }
1129 if(mode->forward[0] && strcmp(oldmode->forward, mode->forward) && ConfigChannel.use_forward)
1130 {
1131 if(dir != MODE_ADD)
1132 {
1133 *mbuf++ = '+';
1134 dir = MODE_ADD;
1135 }
1136 *mbuf++ = 'f';
1137 len = rb_sprintf(pbuf, "%s ", mode->forward);
1138 pbuf += len;
1139 }
1140 *mbuf = '\0';
1141 }
1142
1143 /*
1144 * remove_our_modes
1145 *
1146 * inputs -
1147 * output -
1148 * side effects -
1149 */
1150 static void
1151 remove_our_modes(struct Channel *chptr, struct Client *source_p)
1152 {
1153 struct membership *msptr;
1154 rb_dlink_node *ptr;
1155 char lmodebuf[MODEBUFLEN];
1156 char *lpara[MAXMODEPARAMS];
1157 int count = 0;
1158 int i;
1159
1160 mbuf = lmodebuf;
1161 *mbuf++ = '-';
1162
1163 for(i = 0; i < MAXMODEPARAMS; i++)
1164 lpara[i] = NULL;
1165
1166 RB_DLINK_FOREACH(ptr, chptr->members.head)
1167 {
1168 msptr = ptr->data;
1169
1170 if(is_chanop(msptr))
1171 {
1172 msptr->flags &= ~CHFL_CHANOP;
1173 lpara[count++] = msptr->client_p->name;
1174 *mbuf++ = 'o';
1175
1176 /* +ov, might not fit so check. */
1177 if(is_voiced(msptr))
1178 {
1179 if(count >= MAXMODEPARAMS)
1180 {
1181 *mbuf = '\0';
1182 sendto_channel_local(ALL_MEMBERS, chptr,
1183 ":%s MODE %s %s %s %s %s %s",
1184 source_p->name, chptr->chname,
1185 lmodebuf, lpara[0], lpara[1],
1186 lpara[2], lpara[3]);
1187
1188 /* preserve the initial '-' */
1189 mbuf = lmodebuf;
1190 *mbuf++ = '-';
1191 count = 0;
1192
1193 for(i = 0; i < MAXMODEPARAMS; i++)
1194 lpara[i] = NULL;
1195 }
1196
1197 msptr->flags &= ~CHFL_VOICE;
1198 lpara[count++] = msptr->client_p->name;
1199 *mbuf++ = 'v';
1200 }
1201 }
1202 else if(is_voiced(msptr))
1203 {
1204 msptr->flags &= ~CHFL_VOICE;
1205 lpara[count++] = msptr->client_p->name;
1206 *mbuf++ = 'v';
1207 }
1208 else
1209 continue;
1210
1211 if(count >= MAXMODEPARAMS)
1212 {
1213 *mbuf = '\0';
1214 sendto_channel_local(ALL_MEMBERS, chptr,
1215 ":%s MODE %s %s %s %s %s %s",
1216 source_p->name, chptr->chname, lmodebuf,
1217 lpara[0], lpara[1], lpara[2], lpara[3]);
1218 mbuf = lmodebuf;
1219 *mbuf++ = '-';
1220 count = 0;
1221
1222 for(i = 0; i < MAXMODEPARAMS; i++)
1223 lpara[i] = NULL;
1224 }
1225 }
1226
1227 if(count != 0)
1228 {
1229 *mbuf = '\0';
1230 sendto_channel_local(ALL_MEMBERS, chptr,
1231 ":%s MODE %s %s %s %s %s %s",
1232 source_p->name, chptr->chname, lmodebuf,
1233 EmptyString(lpara[0]) ? "" : lpara[0],
1234 EmptyString(lpara[1]) ? "" : lpara[1],
1235 EmptyString(lpara[2]) ? "" : lpara[2],
1236 EmptyString(lpara[3]) ? "" : lpara[3]);
1237
1238 }
1239 }
1240
1241 /* remove_ban_list()
1242 *
1243 * inputs - channel, source, list to remove, char of mode, caps needed
1244 * outputs -
1245 * side effects - given list is removed, with modes issued to local clients
1246 */
1247 static void
1248 remove_ban_list(struct Channel *chptr, struct Client *source_p,
1249 rb_dlink_list * list, char c, int mems)
1250 {
1251 static char lmodebuf[BUFSIZE];
1252 static char lparabuf[BUFSIZE];
1253 struct Ban *banptr;
1254 rb_dlink_node *ptr;
1255 rb_dlink_node *next_ptr;
1256 char *pbuf;
1257 int count = 0;
1258 int cur_len, mlen, plen;
1259
1260 pbuf = lparabuf;
1261
1262 cur_len = mlen = rb_sprintf(lmodebuf, ":%s MODE %s -", source_p->name, chptr->chname);
1263 mbuf = lmodebuf + mlen;
1264
1265 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, list->head)
1266 {
1267 banptr = ptr->data;
1268
1269 /* trailing space, and the mode letter itself */
1270 plen = strlen(banptr->banstr) + 2;
1271
1272 if(count >= MAXMODEPARAMS || (cur_len + plen) > BUFSIZE - 4)
1273 {
1274 /* remove trailing space */
1275 *mbuf = '\0';
1276 *(pbuf - 1) = '\0';
1277
1278 sendto_channel_local(mems, chptr, "%s %s", lmodebuf, lparabuf);
1279
1280 cur_len = mlen;
1281 mbuf = lmodebuf + mlen;
1282 pbuf = lparabuf;
1283 count = 0;
1284 }
1285
1286 *mbuf++ = c;
1287 cur_len += plen;
1288 pbuf += rb_sprintf(pbuf, "%s ", banptr->banstr);
1289 count++;
1290
1291 free_ban(banptr);
1292 }
1293
1294 *mbuf = '\0';
1295 *(pbuf - 1) = '\0';
1296 sendto_channel_local(mems, chptr, "%s %s", lmodebuf, lparabuf);
1297
1298 list->head = list->tail = NULL;
1299 list->length = 0;
1300 }