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