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