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