]> jfr.im git - solanum.git/blame - src/chmode.c
Make sure to check the length of a ban mask before removing a forward channel.
[solanum.git] / src / chmode.c
CommitLineData
25ea5d2f
EM
1/*
2 * charybdis: A slightly useful ircd.
3 * chmode.c: channel mode management
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 * Copyright (C) 2005-2006 charybdis development team
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 * USA
24 *
25 * $Id: chmode.c 3580 2007-11-07 23:45:14Z jilles $
26 */
27
28#include "stdinc.h"
29#include "channel.h"
30#include "client.h"
31#include "common.h"
32#include "hash.h"
33#include "hook.h"
34#include "match.h"
35#include "ircd.h"
36#include "numeric.h"
37#include "s_serv.h" /* captab */
38#include "s_user.h"
39#include "send.h"
40#include "whowas.h"
41#include "s_conf.h" /* ConfigFileEntry, ConfigChannel */
42#include "s_newconf.h"
43#include "logger.h"
44#include "chmode.h"
45
46/* bitmasks for error returns, so we send once per call */
47#define SM_ERR_NOTS 0x00000001 /* No TS on channel */
48#define SM_ERR_NOOPS 0x00000002 /* No chan ops */
49#define SM_ERR_UNKNOWN 0x00000004
50#define SM_ERR_RPL_C 0x00000008
51#define SM_ERR_RPL_B 0x00000010
52#define SM_ERR_RPL_E 0x00000020
53#define SM_ERR_NOTONCHANNEL 0x00000040 /* Not on channel */
54#define SM_ERR_RPL_I 0x00000100
55#define SM_ERR_RPL_D 0x00000200
56#define SM_ERR_NOPRIVS 0x00000400
57#define SM_ERR_RPL_Q 0x00000800
58#define SM_ERR_RPL_F 0x00001000
59#define SM_ERR_MLOCK 0x00002000
60
61#define MAXMODES_SIMPLE 46 /* a-zA-Z except bqeIov */
62
63static struct ChModeChange mode_changes[BUFSIZE];
64static int mode_count;
65static int mode_limit;
66static int mode_limit_simple;
67static int mask_pos;
68static int removed_mask_pos;
69
70char cflagsbuf[256];
71char cflagsmyinfo[256];
72
73int chmode_flags[256];
acdf71d9 74
749d8c11
AC
75extern int h_get_channel_access;
76
9bec26cc 77/* OPTIMIZE ME! -- dwr */
efccc22c 78void
19716b9f 79construct_cflags_strings(void)
efccc22c
VY
80{
81 int i;
c18cb68b
VY
82 char *ptr = cflagsbuf;
83 char *ptr2 = cflagsmyinfo;
c18cb68b
VY
84
85 *ptr = '\0';
86 *ptr2 = '\0';
efccc22c
VY
87
88 for(i = 0; i < 256; i++)
89 {
f588b956
VY
90 if( !(chmode_table[i].set_func == chm_ban) &&
91 !(chmode_table[i].set_func == chm_forward) &&
92 !(chmode_table[i].set_func == chm_throttle) &&
93 !(chmode_table[i].set_func == chm_key) &&
94 !(chmode_table[i].set_func == chm_limit) &&
95 !(chmode_table[i].set_func == chm_op) &&
96 !(chmode_table[i].set_func == chm_voice))
efccc22c
VY
97 {
98 chmode_flags[i] = chmode_table[i].mode_type;
99 }
100 else
101 {
102 chmode_flags[i] = 0;
103 }
9bec26cc 104
c18cb68b
VY
105 switch (chmode_flags[i])
106 {
73d0f900
EM
107 case MODE_EXLIMIT:
108 case MODE_DISFORWARD:
2da6f6eb
JT
109 if(ConfigChannel.use_forward)
110 *ptr++ = (char) i;
111 break;
c18cb68b
VY
112 case MODE_REGONLY:
113 if(rb_dlink_list_length(&service_list))
114 {
115 *ptr++ = (char) i;
116 }
117
118 break;
119 default:
120 if(chmode_flags[i] != 0)
121 {
122 *ptr++ = (char) i;
123 }
124 }
125
126 /* Should we leave orphaned check here? -- dwr */
127 if(!(chmode_table[i].set_func == chm_nosuch) && !(chmode_table[i].set_func == chm_orphaned))
128 {
129 *ptr2++ = (char) i;
130 }
efccc22c 131 }
c18cb68b
VY
132
133 *ptr++ = '\0';
134 *ptr2++ = '\0';
46d59e11
VY
135}
136
137/*
138 * find_umode_slot
139 *
140 * inputs - NONE
141 * outputs - an available cflag bitmask or
142 * 0 if no cflags are available
143 * side effects - NONE
144 */
19716b9f 145static unsigned int
46d59e11
VY
146find_cflag_slot(void)
147{
148 unsigned int all_cflags = 0, my_cflag = 0, i;
149
150 for (i = 0; i < 256; i++)
151 all_cflags |= chmode_flags[i];
152
153 for (my_cflag = 1; my_cflag && (all_cflags & my_cflag);
154 my_cflag <<= 1);
155
156 return my_cflag;
efccc22c
VY
157}
158
19716b9f
JT
159unsigned int
160cflag_add(char c_, ChannelModeFunc function)
161{
162 int c = (unsigned char)c_;
163
164 if (chmode_table[c].set_func != chm_nosuch &&
165 chmode_table[c].set_func != chm_orphaned)
166 return 0;
167
168 if (chmode_table[c].set_func == chm_nosuch)
169 chmode_table[c].mode_type = find_cflag_slot();
170 if (chmode_table[c].mode_type == 0)
171 return 0;
172 chmode_table[c].set_func = function;
173 construct_cflags_strings();
174 return chmode_table[c].mode_type;
175}
176
177void
178cflag_orphan(char c_)
179{
180 int c = (unsigned char)c_;
181
182 s_assert(chmode_flags[c] != 0);
183 chmode_table[c].set_func = chm_orphaned;
184 construct_cflags_strings();
185}
186
3c52f289 187int
7eec45bc 188get_channel_access(struct Client *source_p, struct membership *msptr)
212380e3 189{
749d8c11
AC
190 hook_data_channel_approval moduledata;
191
192 if(!MyClient(source_p))
212380e3
AC
193 return CHFL_CHANOP;
194
96d26127
AC
195 if (msptr == NULL)
196 return CHFL_PEON;
197
749d8c11
AC
198 moduledata.client = source_p;
199 moduledata.chptr = msptr->chptr;
200 moduledata.msptr = msptr;
201 moduledata.target = NULL;
7eec45bc 202 moduledata.approved = is_chanop(msptr) ? CHFL_CHANOP : CHFL_PEON;
8aabb973 203
749d8c11
AC
204 call_hook(h_get_channel_access, &moduledata);
205
206 return moduledata.approved;
212380e3
AC
207}
208
209/* add_id()
210 *
765d839d 211 * inputs - client, channel, id to add, type, forward
212380e3
AC
212 * outputs - 0 on failure, 1 on success
213 * side effects - given id is added to the appropriate list
214 */
215int
765d839d 216add_id(struct Client *source_p, struct Channel *chptr, const char *banid, const char *forward,
330fc5c1 217 rb_dlink_list * list, long mode_type)
212380e3
AC
218{
219 struct Ban *actualBan;
83294285 220 static char who[USERHOST_REPLYLEN];
212380e3 221 char *realban = LOCAL_COPY(banid);
330fc5c1 222 rb_dlink_node *ptr;
212380e3
AC
223
224 /* dont let local clients overflow the banlist, or set redundant
225 * bans
226 */
227 if(MyClient(source_p))
228 {
330fc5c1 229 if((rb_dlink_list_length(&chptr->banlist) + rb_dlink_list_length(&chptr->exceptlist) + rb_dlink_list_length(&chptr->invexlist) + rb_dlink_list_length(&chptr->quietlist)) >= (chptr->mode.mode & MODE_EXLIMIT ? ConfigChannel.max_bans_large : ConfigChannel.max_bans))
212380e3
AC
230 {
231 sendto_one(source_p, form_str(ERR_BANLISTFULL),
232 me.name, source_p->name, chptr->chname, realban);
233 return 0;
234 }
235
5cefa1d6 236 RB_DLINK_FOREACH(ptr, list->head)
212380e3
AC
237 {
238 actualBan = ptr->data;
fd488ac1 239 if(mask_match(actualBan->banstr, realban))
212380e3
AC
240 return 0;
241 }
242 }
243 /* dont let remotes set duplicates */
244 else
245 {
5cefa1d6 246 RB_DLINK_FOREACH(ptr, list->head)
212380e3
AC
247 {
248 actualBan = ptr->data;
249 if(!irccmp(actualBan->banstr, realban))
250 return 0;
251 }
252 }
253
254
255 if(IsPerson(source_p))
b2f0da88 256 rb_sprintf(who, "%s!%s@%s", source_p->name, source_p->username, source_p->host);
212380e3 257 else
f427c8b0 258 rb_strlcpy(who, source_p->name, sizeof(who));
212380e3 259
765d839d 260 actualBan = allocate_ban(realban, who, forward);
e3354945 261 actualBan->when = rb_current_time();
212380e3 262
330fc5c1 263 rb_dlinkAdd(actualBan, &actualBan->node, list);
212380e3
AC
264
265 /* invalidate the can_send() cache */
266 if(mode_type == CHFL_BAN || mode_type == CHFL_QUIET || mode_type == CHFL_EXCEPTION)
267 chptr->bants++;
268
269 return 1;
270}
271
272/* del_id()
273 *
274 * inputs - channel, id to remove, type
765d839d
EM
275 * outputs - pointer to ban that was removed, if any
276 * side effects - given id is removed from the appropriate list and returned
212380e3 277 */
765d839d 278struct Ban *
330fc5c1 279del_id(struct Channel *chptr, const char *banid, rb_dlink_list * list, long mode_type)
212380e3 280{
330fc5c1 281 rb_dlink_node *ptr;
212380e3
AC
282 struct Ban *banptr;
283
284 if(EmptyString(banid))
765d839d 285 return NULL;
212380e3 286
5cefa1d6 287 RB_DLINK_FOREACH(ptr, list->head)
212380e3
AC
288 {
289 banptr = ptr->data;
290
291 if(irccmp(banid, banptr->banstr) == 0)
292 {
330fc5c1 293 rb_dlinkDelete(&banptr->node, list);
212380e3
AC
294
295 /* invalidate the can_send() cache */
296 if(mode_type == CHFL_BAN || mode_type == CHFL_QUIET || mode_type == CHFL_EXCEPTION)
297 chptr->bants++;
298
765d839d 299 return banptr;
212380e3
AC
300 }
301 }
302
765d839d 303 return NULL;
212380e3
AC
304}
305
306/* check_string()
307 *
308 * input - string to check
309 * output - pointer to 'fixed' string, or "*" if empty
310 * side effects - any white space found becomes \0
311 */
312static char *
313check_string(char *s)
314{
315 char *str = s;
316 static char splat[] = "*";
317 if(!(s && *s))
318 return splat;
319
320 for(; *s; ++s)
321 {
322 if(IsSpace(*s))
323 {
324 *s = '\0';
325 break;
326 }
327 }
328 return str;
329}
330
331/* pretty_mask()
332 *
333 * inputs - mask to pretty
334 * outputs - better version of the mask
335 * side effects - mask is chopped to limits, and transformed:
336 * x!y@z => x!y@z
337 * y@z => *!y@z
338 * x!y => x!y@*
339 * x => x!*@*
340 * z.d => *!*@z.d
341 */
342static char *
343pretty_mask(const char *idmask)
344{
345 static char mask_buf[BUFSIZE];
346 int old_mask_pos;
765d839d 347 char *nick, *user, *host, *forward = NULL;
212380e3 348 char splat[] = "*";
765d839d
EM
349 char *t, *at, *ex, *ex2;
350 char ne = 0, ue = 0, he = 0, fe = 0; /* save values at nick[NICKLEN], et all */
351 char e2 = 0; /* save value that delimits forward channel */
212380e3
AC
352 char *mask;
353
354 mask = LOCAL_COPY(idmask);
355 mask = check_string(mask);
356 collapse(mask);
357
358 nick = user = host = splat;
359
360 if((size_t) BUFSIZE - mask_pos < strlen(mask) + 5)
361 return NULL;
362
363 old_mask_pos = mask_pos;
364
365 if (*mask == '$')
366 {
b2f0da88 367 mask_pos += rb_sprintf(mask_buf + mask_pos, "%s", mask) + 1;
212380e3
AC
368 t = mask_buf + old_mask_pos + 1;
369 if (*t == '!')
370 *t = '~';
371 if (*t == '~')
372 t++;
373 *t = ToLower(*t);
374 return mask_buf + old_mask_pos;
375 }
376
765d839d 377 at = ex = ex2 = NULL;
212380e3
AC
378 if((t = strchr(mask, '@')) != NULL)
379 {
380 at = t;
381 *t++ = '\0';
382 if(*t != '\0')
383 host = t;
384
385 if((t = strchr(mask, '!')) != NULL)
386 {
387 ex = t;
388 *t++ = '\0';
389 if(*t != '\0')
390 user = t;
391 if(*mask != '\0')
392 nick = mask;
393 }
394 else
395 {
396 if(*mask != '\0')
397 user = mask;
398 }
765d839d
EM
399
400 if((t = strchr(host, '!')) != NULL || (t = strchr(host, '$')) != NULL)
401 {
402 ex2 = t;
403 e2 = *t;
404 *t++= '\0';
405 if (*t != '\0')
406 forward = t;
407 }
212380e3
AC
408 }
409 else if((t = strchr(mask, '!')) != NULL)
410 {
411 ex = t;
412 *t++ = '\0';
413 if(*mask != '\0')
414 nick = mask;
415 if(*t != '\0')
416 user = t;
417 }
765d839d 418 else if(strchr(mask, '.') != NULL || strchr(mask, ':') != NULL)
212380e3
AC
419 {
420 if(*mask != '\0')
421 host = mask;
422 }
423 else
424 {
425 if(*mask != '\0')
426 nick = mask;
427 }
428
429 /* truncate values to max lengths */
430 if(strlen(nick) > NICKLEN - 1)
431 {
432 ne = nick[NICKLEN - 1];
433 nick[NICKLEN - 1] = '\0';
434 }
435 if(strlen(user) > USERLEN)
436 {
437 ue = user[USERLEN];
438 user[USERLEN] = '\0';
439 }
440 if(strlen(host) > HOSTLEN)
441 {
442 he = host[HOSTLEN];
443 host[HOSTLEN] = '\0';
444 }
765d839d
EM
445 if(forward && strlen(forward) > CHANNELLEN)
446 {
447 fe = forward[CHANNELLEN];
448 forward[CHANNELLEN] = '\0';
449 }
212380e3 450
765d839d
EM
451 if (forward)
452 mask_pos += rb_sprintf(mask_buf + mask_pos, "%s!%s@%s$%s", nick, user, host, forward) + 1;
453 else
454 mask_pos += rb_sprintf(mask_buf + mask_pos, "%s!%s@%s", nick, user, host) + 1;
212380e3
AC
455
456 /* restore mask, since we may need to use it again later */
457 if(at)
458 *at = '@';
459 if(ex)
460 *ex = '!';
765d839d
EM
461 if(ex2)
462 *ex2 = e2;
212380e3
AC
463 if(ne)
464 nick[NICKLEN - 1] = ne;
465 if(ue)
466 user[USERLEN] = ue;
467 if(he)
468 host[HOSTLEN] = he;
765d839d
EM
469 if(fe)
470 forward[CHANNELLEN] = fe;
212380e3
AC
471
472 return mask_buf + old_mask_pos;
473}
474
475/* fix_key()
476 *
477 * input - key to fix
478 * output - the same key, fixed
479 * side effects - anything below ascii 13 is discarded, ':' discarded,
480 * high ascii is dropped to lower half of ascii table
481 */
482static char *
483fix_key(char *arg)
484{
485 u_char *s, *t, c;
486
487 for(s = t = (u_char *) arg; (c = *s); s++)
488 {
489 c &= 0x7f;
490 if(c != ':' && c != ',' && c > ' ')
491 *t++ = c;
492 }
493
494 *t = '\0';
495 return arg;
496}
497
498/* fix_key_remote()
499 *
500 * input - key to fix
501 * ouput - the same key, fixed
502 * side effects - high ascii dropped to lower half of table,
503 * CR/LF/':' are dropped
504 */
505static char *
506fix_key_remote(char *arg)
507{
508 u_char *s, *t, c;
509
510 for(s = t = (u_char *) arg; (c = *s); s++)
511 {
512 c &= 0x7f;
513 if((c != 0x0a) && (c != ':') && (c != ',') && (c != 0x0d) && (c != ' '))
514 *t++ = c;
515 }
516
517 *t = '\0';
518 return arg;
519}
520
521/* chm_*()
522 *
523 * The handlers for each specific mode.
524 */
525void
526chm_nosuch(struct Client *source_p, struct Channel *chptr,
527 int alevel, int parc, int *parn,
528 const char **parv, int *errors, int dir, char c, long mode_type)
529{
530 if(*errors & SM_ERR_UNKNOWN)
531 return;
532 *errors |= SM_ERR_UNKNOWN;
533 sendto_one(source_p, form_str(ERR_UNKNOWNMODE), me.name, source_p->name, c);
534}
535
536void
537chm_simple(struct Client *source_p, struct Channel *chptr,
538 int alevel, int parc, int *parn,
539 const char **parv, int *errors, int dir, char c, long mode_type)
540{
541 if(alevel != CHFL_CHANOP)
542 {
543 if(!(*errors & SM_ERR_NOOPS))
544 sendto_one(source_p, form_str(ERR_CHANOPRIVSNEEDED),
545 me.name, source_p->name, chptr->chname);
546 *errors |= SM_ERR_NOOPS;
547 return;
548 }
549
84c9a8c7 550 if(MyClient(source_p) && (++mode_limit_simple > MAXMODES_SIMPLE))
212380e3
AC
551 return;
552
553 /* setting + */
78e6b731 554 if((dir == MODE_ADD) && !(chptr->mode.mode & mode_type))
212380e3 555 {
2da6f6eb
JT
556 /* if +f is disabled, ignore an attempt to set +QF locally */
557 if(!ConfigChannel.use_forward && MyClient(source_p) &&
558 (c == 'Q' || c == 'F'))
559 return;
560
212380e3
AC
561 chptr->mode.mode |= mode_type;
562
563 mode_changes[mode_count].letter = c;
564 mode_changes[mode_count].dir = MODE_ADD;
565 mode_changes[mode_count].caps = 0;
566 mode_changes[mode_count].nocaps = 0;
567 mode_changes[mode_count].id = NULL;
568 mode_changes[mode_count].mems = ALL_MEMBERS;
cbed45a2
VY
569 mode_changes[mode_count++].arg = NULL;
570 }
78e6b731 571 else if((dir == MODE_DEL) && (chptr->mode.mode & mode_type))
cbed45a2
VY
572 {
573 chptr->mode.mode &= ~mode_type;
574
575 mode_changes[mode_count].letter = c;
576 mode_changes[mode_count].dir = MODE_DEL;
577 mode_changes[mode_count].caps = 0;
578 mode_changes[mode_count].nocaps = 0;
579 mode_changes[mode_count].mems = ALL_MEMBERS;
580 mode_changes[mode_count].id = NULL;
581 mode_changes[mode_count++].arg = NULL;
582 }
583}
584
585void
586chm_orphaned(struct Client *source_p, struct Channel *chptr,
587 int alevel, int parc, int *parn,
588 const char **parv, int *errors, int dir, char c, long mode_type)
589{
590 if(MyClient(source_p))
591 return;
592
593 if((dir == MODE_ADD) && !(chptr->mode.mode & mode_type))
594 {
595 chptr->mode.mode |= mode_type;
596
597 mode_changes[mode_count].letter = c;
598 mode_changes[mode_count].dir = MODE_ADD;
599 mode_changes[mode_count].caps = 0;
600 mode_changes[mode_count].nocaps = 0;
601 mode_changes[mode_count].id = NULL;
602 mode_changes[mode_count].mems = ALL_MEMBERS;
212380e3
AC
603 mode_changes[mode_count++].arg = NULL;
604 }
605 else if((dir == MODE_DEL) && (chptr->mode.mode & mode_type))
606 {
607 chptr->mode.mode &= ~mode_type;
608
609 mode_changes[mode_count].letter = c;
610 mode_changes[mode_count].dir = MODE_DEL;
611 mode_changes[mode_count].caps = 0;
612 mode_changes[mode_count].nocaps = 0;
613 mode_changes[mode_count].mems = ALL_MEMBERS;
614 mode_changes[mode_count].id = NULL;
615 mode_changes[mode_count++].arg = NULL;
616 }
617}
618
619void
620chm_staff(struct Client *source_p, struct Channel *chptr,
621 int alevel, int parc, int *parn,
622 const char **parv, int *errors, int dir, char c, long mode_type)
623{
624 if(!IsOper(source_p) && !IsServer(source_p))
625 {
626 if(!(*errors & SM_ERR_NOPRIVS))
627 sendto_one_numeric(source_p, ERR_NOPRIVILEGES, form_str(ERR_NOPRIVILEGES));
628 *errors |= SM_ERR_NOPRIVS;
629 return;
630 }
1ef5b430
JT
631 if(MyClient(source_p) && !IsOperResv(source_p))
632 {
633 if(!(*errors & SM_ERR_NOPRIVS))
634 sendto_one(source_p, form_str(ERR_NOPRIVS), me.name,
635 source_p->name, "resv");
636 *errors |= SM_ERR_NOPRIVS;
637 return;
638 }
212380e3 639
84c9a8c7
JT
640 if(MyClient(source_p) && (++mode_limit_simple > MAXMODES_SIMPLE))
641 return;
642
212380e3
AC
643 /* setting + */
644 if((dir == MODE_ADD) && !(chptr->mode.mode & mode_type))
645 {
646 chptr->mode.mode |= mode_type;
647
648 mode_changes[mode_count].letter = c;
649 mode_changes[mode_count].dir = MODE_ADD;
650 mode_changes[mode_count].caps = 0;
651 mode_changes[mode_count].nocaps = 0;
652 mode_changes[mode_count].id = NULL;
653 mode_changes[mode_count].mems = ALL_MEMBERS;
654 mode_changes[mode_count++].arg = NULL;
655 }
656 else if((dir == MODE_DEL) && (chptr->mode.mode & mode_type))
657 {
658 chptr->mode.mode &= ~mode_type;
659
660 mode_changes[mode_count].letter = c;
661 mode_changes[mode_count].dir = MODE_DEL;
662 mode_changes[mode_count].caps = 0;
663 mode_changes[mode_count].nocaps = 0;
664 mode_changes[mode_count].mems = ALL_MEMBERS;
665 mode_changes[mode_count].id = NULL;
666 mode_changes[mode_count++].arg = NULL;
667 }
668}
669
670void
671chm_ban(struct Client *source_p, struct Channel *chptr,
672 int alevel, int parc, int *parn,
673 const char **parv, int *errors, int dir, char c, long mode_type)
674{
765d839d 675 char *mask, *raw_mask, *forward;
330fc5c1
AC
676 rb_dlink_list *list;
677 rb_dlink_node *ptr;
212380e3
AC
678 struct Ban *banptr;
679 int errorval;
680 int rpl_list;
681 int rpl_endlist;
682 int caps;
683 int mems;
684
685 switch (mode_type)
686 {
687 case CHFL_BAN:
688 list = &chptr->banlist;
689 errorval = SM_ERR_RPL_B;
690 rpl_list = RPL_BANLIST;
691 rpl_endlist = RPL_ENDOFBANLIST;
692 mems = ALL_MEMBERS;
693 caps = 0;
694 break;
695
696 case CHFL_EXCEPTION:
697 /* if +e is disabled, allow all but +e locally */
698 if(!ConfigChannel.use_except && MyClient(source_p) &&
699 ((dir == MODE_ADD) && (parc > *parn)))
700 return;
701
702 list = &chptr->exceptlist;
703 errorval = SM_ERR_RPL_E;
704 rpl_list = RPL_EXCEPTLIST;
705 rpl_endlist = RPL_ENDOFEXCEPTLIST;
706 caps = CAP_EX;
707
708 if(ConfigChannel.use_except || (dir == MODE_DEL))
709 mems = ONLY_CHANOPS;
710 else
711 mems = ONLY_SERVERS;
712 break;
713
714 case CHFL_INVEX:
715 /* if +I is disabled, allow all but +I locally */
716 if(!ConfigChannel.use_invex && MyClient(source_p) &&
717 (dir == MODE_ADD) && (parc > *parn))
718 return;
719
720 list = &chptr->invexlist;
721 errorval = SM_ERR_RPL_I;
722 rpl_list = RPL_INVITELIST;
723 rpl_endlist = RPL_ENDOFINVITELIST;
724 caps = CAP_IE;
725
726 if(ConfigChannel.use_invex || (dir == MODE_DEL))
727 mems = ONLY_CHANOPS;
728 else
729 mems = ONLY_SERVERS;
730 break;
731
732 case CHFL_QUIET:
733 list = &chptr->quietlist;
734 errorval = SM_ERR_RPL_Q;
51027be8
AF
735 rpl_list = RPL_QUIETLIST;
736 rpl_endlist = RPL_ENDOFQUIETLIST;
212380e3
AC
737 mems = ALL_MEMBERS;
738 caps = 0;
739 break;
740
741 default:
742 sendto_realops_snomask(SNO_GENERAL, L_ALL, "chm_ban() called with unknown type!");
743 return;
744 break;
745 }
746
747 if(dir == 0 || parc <= *parn)
748 {
749 if((*errors & errorval) != 0)
750 return;
751 *errors |= errorval;
752
753 /* non-ops cant see +eI lists.. */
754 if(alevel != CHFL_CHANOP && mode_type != CHFL_BAN &&
755 mode_type != CHFL_QUIET)
756 {
757 if(!(*errors & SM_ERR_NOOPS))
758 sendto_one(source_p, form_str(ERR_CHANOPRIVSNEEDED),
759 me.name, source_p->name, chptr->chname);
760 *errors |= SM_ERR_NOOPS;
761 return;
762 }
763
5cefa1d6 764 RB_DLINK_FOREACH(ptr, list->head)
212380e3 765 {
765d839d 766 char buf[BANLEN];
212380e3 767 banptr = ptr->data;
765d839d
EM
768 if(banptr->forward)
769 rb_snprintf(buf, sizeof(buf), "%s$%s", banptr->banstr, banptr->forward);
770 else
771 rb_strlcpy(buf, banptr->banstr, sizeof(buf));
772
212380e3
AC
773 sendto_one(source_p, form_str(rpl_list),
774 me.name, source_p->name, chptr->chname,
765d839d 775 buf, banptr->who, banptr->when);
212380e3 776 }
51027be8 777 sendto_one(source_p, form_str(rpl_endlist), me.name, source_p->name, chptr->chname);
212380e3
AC
778 return;
779 }
780
781 if(alevel != CHFL_CHANOP)
782 {
783 if(!(*errors & SM_ERR_NOOPS))
784 sendto_one(source_p, form_str(ERR_CHANOPRIVSNEEDED),
785 me.name, source_p->name, chptr->chname);
786 *errors |= SM_ERR_NOOPS;
787 return;
788 }
789
790 if(MyClient(source_p) && (++mode_limit > MAXMODEPARAMS))
791 return;
792
793 raw_mask = parv[(*parn)];
794 (*parn)++;
795
796 /* empty ban, or starts with ':' which messes up s2s, ignore it */
797 if(EmptyString(raw_mask) || *raw_mask == ':')
798 return;
799
800 if(!MyClient(source_p))
801 {
802 if(strchr(raw_mask, ' '))
803 return;
804
805 mask = raw_mask;
806 }
807 else
808 mask = pretty_mask(raw_mask);
809
1aa35c8a
JT
810 /* we'd have problems parsing this, hyb6 does it too
811 * also make sure it will always fit on a line with channel
812 * name etc.
813 */
814 if(strlen(mask) > IRCD_MIN(BANLEN, MODEBUFLEN - 5))
815 return;
816
765d839d
EM
817 /* Look for a $ after the first character.
818 * As the first character, it marks an extban; afterwards
819 * it delimits a forward channel.
820 */
821 if ((forward = strchr(mask+1, '$')) != NULL)
822 {
823 *forward++ = '\0';
824 if (*forward == '\0')
825 forward = NULL;
826 }
827
212380e3
AC
828 /* if we're adding a NEW id */
829 if(dir == MODE_ADD)
830 {
831 if (*mask == '$' && MyClient(source_p))
832 {
833 if (!valid_extban(mask, source_p, chptr, mode_type))
834 /* XXX perhaps return an error message here */
835 return;
836 }
837
2da6f6eb
JT
838 if(forward != NULL && !ConfigChannel.use_forward &&
839 MyClient(source_p))
840 forward = NULL;
841
212380e3
AC
842 /* dont allow local clients to overflow the banlist, dont
843 * let remote servers set duplicate bans
844 */
765d839d 845 if(!add_id(source_p, chptr, mask, forward, list, mode_type))
212380e3
AC
846 return;
847
765d839d
EM
848 if(forward)
849 forward[-1]= '$';
850
212380e3
AC
851 mode_changes[mode_count].letter = c;
852 mode_changes[mode_count].dir = MODE_ADD;
853 mode_changes[mode_count].caps = caps;
854 mode_changes[mode_count].nocaps = 0;
855 mode_changes[mode_count].mems = mems;
856 mode_changes[mode_count].id = NULL;
857 mode_changes[mode_count++].arg = mask;
858 }
859 else if(dir == MODE_DEL)
860 {
765d839d
EM
861 struct Ban *removed;
862 static char buf[BANLEN * MAXMODEPARAMS];
863 int old_removed_mask_pos = removed_mask_pos;
864 if((removed = del_id(chptr, mask, list, mode_type)) == NULL)
212380e3
AC
865 {
866 /* mask isn't a valid ban, check raw_mask */
765d839d 867 if((removed = del_id(chptr, raw_mask, list, mode_type)) != NULL)
212380e3
AC
868 mask = raw_mask;
869 }
870
765d839d
EM
871 if(removed && removed->forward)
872 removed_mask_pos += rb_snprintf(buf, sizeof(buf), "%s$%s", removed->banstr, removed->forward);
873 else
874 removed_mask_pos += rb_strlcpy(buf, mask, sizeof(buf));
875 if(removed)
876 {
877 free_ban(removed);
878 removed = NULL;
879 }
880
212380e3
AC
881 mode_changes[mode_count].letter = c;
882 mode_changes[mode_count].dir = MODE_DEL;
883 mode_changes[mode_count].caps = caps;
884 mode_changes[mode_count].nocaps = 0;
885 mode_changes[mode_count].mems = mems;
886 mode_changes[mode_count].id = NULL;
765d839d 887 mode_changes[mode_count++].arg = buf + old_removed_mask_pos;
212380e3
AC
888 }
889}
890
891void
892chm_op(struct Client *source_p, struct Channel *chptr,
893 int alevel, int parc, int *parn,
894 const char **parv, int *errors, int dir, char c, long mode_type)
895{
896 struct membership *mstptr;
897 const char *opnick;
898 struct Client *targ_p;
899
900 if(alevel != CHFL_CHANOP)
901 {
902 if(!(*errors & SM_ERR_NOOPS))
903 sendto_one(source_p, form_str(ERR_CHANOPRIVSNEEDED),
904 me.name, source_p->name, chptr->chname);
905 *errors |= SM_ERR_NOOPS;
906 return;
907 }
908
909 if((dir == MODE_QUERY) || (parc <= *parn))
910 return;
911
912 opnick = parv[(*parn)];
913 (*parn)++;
914
915 /* empty nick */
916 if(EmptyString(opnick))
917 {
918 sendto_one_numeric(source_p, ERR_NOSUCHNICK, form_str(ERR_NOSUCHNICK), "*");
919 return;
920 }
921
922 if((targ_p = find_chasing(source_p, opnick, NULL)) == NULL)
923 {
924 return;
925 }
926
927 mstptr = find_channel_membership(chptr, targ_p);
928
929 if(mstptr == NULL)
930 {
931 if(!(*errors & SM_ERR_NOTONCHANNEL) && MyClient(source_p))
932 sendto_one_numeric(source_p, ERR_USERNOTINCHANNEL,
933 form_str(ERR_USERNOTINCHANNEL), opnick, chptr->chname);
934 *errors |= SM_ERR_NOTONCHANNEL;
935 return;
936 }
937
938 if(MyClient(source_p) && (++mode_limit > MAXMODEPARAMS))
939 return;
940
941 if(dir == MODE_ADD)
942 {
fa0e2152
JT
943 if(targ_p == source_p && mstptr->flags & CHFL_CHANOP)
944 return;
945
212380e3
AC
946 mode_changes[mode_count].letter = c;
947 mode_changes[mode_count].dir = MODE_ADD;
948 mode_changes[mode_count].caps = 0;
949 mode_changes[mode_count].nocaps = 0;
950 mode_changes[mode_count].mems = ALL_MEMBERS;
951 mode_changes[mode_count].id = targ_p->id;
952 mode_changes[mode_count].arg = targ_p->name;
953 mode_changes[mode_count++].client = targ_p;
954
955 mstptr->flags |= CHFL_CHANOP;
212380e3
AC
956 }
957 else
958 {
959 if(MyClient(source_p) && IsService(targ_p))
960 {
961 sendto_one(source_p, form_str(ERR_ISCHANSERVICE),
962 me.name, source_p->name, targ_p->name, chptr->chname);
963 return;
964 }
965
966 mode_changes[mode_count].letter = c;
967 mode_changes[mode_count].dir = MODE_DEL;
968 mode_changes[mode_count].caps = 0;
969 mode_changes[mode_count].nocaps = 0;
970 mode_changes[mode_count].mems = ALL_MEMBERS;
971 mode_changes[mode_count].id = targ_p->id;
972 mode_changes[mode_count].arg = targ_p->name;
973 mode_changes[mode_count++].client = targ_p;
974
975 mstptr->flags &= ~CHFL_CHANOP;
976 }
977}
978
979void
980chm_voice(struct Client *source_p, struct Channel *chptr,
981 int alevel, int parc, int *parn,
982 const char **parv, int *errors, int dir, char c, long mode_type)
983{
984 struct membership *mstptr;
985 const char *opnick;
986 struct Client *targ_p;
987
988 if(alevel != CHFL_CHANOP)
989 {
990 if(!(*errors & SM_ERR_NOOPS))
991 sendto_one(source_p, form_str(ERR_CHANOPRIVSNEEDED),
992 me.name, source_p->name, chptr->chname);
993 *errors |= SM_ERR_NOOPS;
994 return;
995 }
996
997 if((dir == MODE_QUERY) || parc <= *parn)
998 return;
999
1000 opnick = parv[(*parn)];
1001 (*parn)++;
1002
1003 /* empty nick */
1004 if(EmptyString(opnick))
1005 {
1006 sendto_one_numeric(source_p, ERR_NOSUCHNICK, form_str(ERR_NOSUCHNICK), "*");
1007 return;
1008 }
1009
1010 if((targ_p = find_chasing(source_p, opnick, NULL)) == NULL)
1011 {
1012 return;
1013 }
1014
1015 mstptr = find_channel_membership(chptr, targ_p);
1016
1017 if(mstptr == NULL)
1018 {
1019 if(!(*errors & SM_ERR_NOTONCHANNEL) && MyClient(source_p))
1020 sendto_one_numeric(source_p, ERR_USERNOTINCHANNEL,
1021 form_str(ERR_USERNOTINCHANNEL), opnick, chptr->chname);
1022 *errors |= SM_ERR_NOTONCHANNEL;
1023 return;
1024 }
1025
1026 if(MyClient(source_p) && (++mode_limit > MAXMODEPARAMS))
1027 return;
1028
1029 if(dir == MODE_ADD)
1030 {
1031 mode_changes[mode_count].letter = c;
1032 mode_changes[mode_count].dir = MODE_ADD;
1033 mode_changes[mode_count].caps = 0;
1034 mode_changes[mode_count].nocaps = 0;
1035 mode_changes[mode_count].mems = ALL_MEMBERS;
1036 mode_changes[mode_count].id = targ_p->id;
1037 mode_changes[mode_count].arg = targ_p->name;
1038 mode_changes[mode_count++].client = targ_p;
1039
1040 mstptr->flags |= CHFL_VOICE;
1041 }
1042 else
1043 {
1044 mode_changes[mode_count].letter = 'v';
1045 mode_changes[mode_count].dir = MODE_DEL;
1046 mode_changes[mode_count].caps = 0;
1047 mode_changes[mode_count].nocaps = 0;
1048 mode_changes[mode_count].mems = ALL_MEMBERS;
1049 mode_changes[mode_count].id = targ_p->id;
1050 mode_changes[mode_count].arg = targ_p->name;
1051 mode_changes[mode_count++].client = targ_p;
1052
1053 mstptr->flags &= ~CHFL_VOICE;
1054 }
1055}
1056
1057void
1058chm_limit(struct Client *source_p, struct Channel *chptr,
1059 int alevel, int parc, int *parn,
1060 const char **parv, int *errors, int dir, char c, long mode_type)
1061{
1062 const char *lstr;
1063 static char limitstr[30];
1064 int limit;
1065
1066 if(alevel != CHFL_CHANOP)
1067 {
1068 if(!(*errors & SM_ERR_NOOPS))
1069 sendto_one(source_p, form_str(ERR_CHANOPRIVSNEEDED),
1070 me.name, source_p->name, chptr->chname);
1071 *errors |= SM_ERR_NOOPS;
1072 return;
1073 }
1074
1075 if(dir == MODE_QUERY)
1076 return;
1077
84c9a8c7
JT
1078 if(MyClient(source_p) && (++mode_limit_simple > MAXMODES_SIMPLE))
1079 return;
1080
212380e3
AC
1081 if((dir == MODE_ADD) && parc > *parn)
1082 {
1083 lstr = parv[(*parn)];
1084 (*parn)++;
1085
1086 if(EmptyString(lstr) || (limit = atoi(lstr)) <= 0)
1087 return;
1088
b2f0da88 1089 rb_sprintf(limitstr, "%d", limit);
212380e3
AC
1090
1091 mode_changes[mode_count].letter = c;
1092 mode_changes[mode_count].dir = MODE_ADD;
1093 mode_changes[mode_count].caps = 0;
1094 mode_changes[mode_count].nocaps = 0;
1095 mode_changes[mode_count].mems = ALL_MEMBERS;
1096 mode_changes[mode_count].id = NULL;
1097 mode_changes[mode_count++].arg = limitstr;
1098
1099 chptr->mode.limit = limit;
1100 }
1101 else if(dir == MODE_DEL)
1102 {
1103 if(!chptr->mode.limit)
1104 return;
1105
1106 chptr->mode.limit = 0;
1107
1108 mode_changes[mode_count].letter = c;
1109 mode_changes[mode_count].dir = MODE_DEL;
1110 mode_changes[mode_count].caps = 0;
1111 mode_changes[mode_count].nocaps = 0;
1112 mode_changes[mode_count].mems = ALL_MEMBERS;
1113 mode_changes[mode_count].id = NULL;
1114 mode_changes[mode_count++].arg = NULL;
1115 }
1116}
1117
1118void
1119chm_throttle(struct Client *source_p, struct Channel *chptr,
1120 int alevel, int parc, int *parn,
1121 const char **parv, int *errors, int dir, char c, long mode_type)
1122{
1123 int joins = 0, timeslice = 0;
1124
1125 if(alevel != CHFL_CHANOP)
1126 {
1127 if(!(*errors & SM_ERR_NOOPS))
1128 sendto_one(source_p, form_str(ERR_CHANOPRIVSNEEDED),
1129 me.name, source_p->name, chptr->chname);
1130 *errors |= SM_ERR_NOOPS;
1131 return;
1132 }
1133
1134 if(dir == MODE_QUERY)
1135 return;
1136
84c9a8c7
JT
1137 if(MyClient(source_p) && (++mode_limit_simple > MAXMODES_SIMPLE))
1138 return;
1139
212380e3
AC
1140 if((dir == MODE_ADD) && parc > *parn)
1141 {
1142 sscanf(parv[(*parn)], "%d:%d", &joins, &timeslice);
1143
1144 if(!joins || !timeslice)
1145 return;
1146
1147 mode_changes[mode_count].letter = c;
1148 mode_changes[mode_count].dir = MODE_ADD;
1149 mode_changes[mode_count].caps = 0;
1150 mode_changes[mode_count].nocaps = 0;
1151 mode_changes[mode_count].mems = ALL_MEMBERS;
1152 mode_changes[mode_count].id = NULL;
1153 mode_changes[mode_count++].arg = parv[(*parn)];
1154
1155 (*parn)++;
1156
1157 chptr->mode.join_num = joins;
1158 chptr->mode.join_time = timeslice;
1159 }
1160 else if(dir == MODE_DEL)
1161 {
1162 if(!chptr->mode.join_num)
1163 return;
1164
1165 chptr->mode.join_num = 0;
1166 chptr->mode.join_time = 0;
1167 chptr->join_count = 0;
1168 chptr->join_delta = 0;
1169
1170 mode_changes[mode_count].letter = c;
1171 mode_changes[mode_count].dir = MODE_DEL;
1172 mode_changes[mode_count].caps = 0;
1173 mode_changes[mode_count].nocaps = 0;
1174 mode_changes[mode_count].mems = ALL_MEMBERS;
1175 mode_changes[mode_count].id = NULL;
1176 mode_changes[mode_count++].arg = NULL;
1177 }
1178}
1179
1180void
1181chm_forward(struct Client *source_p, struct Channel *chptr,
1182 int alevel, int parc, int *parn,
1183 const char **parv, int *errors, int dir, char c, long mode_type)
1184{
1185 struct Channel *targptr = NULL;
1186 struct membership *msptr;
1187 const char *forward;
1188
2da6f6eb
JT
1189 /* if +f is disabled, ignore local attempts to set it */
1190 if(!ConfigChannel.use_forward && MyClient(source_p) &&
1191 (dir == MODE_ADD) && (parc > *parn))
1192 return;
1193
212380e3
AC
1194 if(dir == MODE_QUERY || (dir == MODE_ADD && parc <= *parn))
1195 {
1196 if (!(*errors & SM_ERR_RPL_F))
1197 {
1198 if (*chptr->mode.forward == '\0')
5366977b 1199 sendto_one_notice(source_p, ":%s has no forward channel", chptr->chname);
212380e3 1200 else
5366977b 1201 sendto_one_notice(source_p, ":%s forward channel is %s", chptr->chname, chptr->mode.forward);
212380e3
AC
1202 *errors |= SM_ERR_RPL_F;
1203 }
1204 return;
1205 }
1206
1207#ifndef FORWARD_OPERONLY
1208 if(alevel != CHFL_CHANOP)
1209 {
1210 if(!(*errors & SM_ERR_NOOPS))
1211 sendto_one(source_p, form_str(ERR_CHANOPRIVSNEEDED),
1212 me.name, source_p->name, chptr->chname);
1213 *errors |= SM_ERR_NOOPS;
1214 return;
1215 }
1216#else
1217 if(!IsOper(source_p) && !IsServer(source_p))
1218 {
1219 if(!(*errors & SM_ERR_NOPRIVS))
1220 sendto_one_numeric(source_p, ERR_NOPRIVILEGES, form_str(ERR_NOPRIVILEGES));
1221 *errors |= SM_ERR_NOPRIVS;
1222 return;
1223 }
1224#endif
1225
84c9a8c7
JT
1226 if(MyClient(source_p) && (++mode_limit_simple > MAXMODES_SIMPLE))
1227 return;
1228
212380e3
AC
1229 if(dir == MODE_ADD && parc > *parn)
1230 {
1231 forward = parv[(*parn)];
1232 (*parn)++;
1233
1234 if(EmptyString(forward))
1235 return;
1236 if(!check_channel_name(forward) ||
1237 (MyClient(source_p) && (strlen(forward) > LOC_CHANNELLEN || hash_find_resv(forward))))
1238 {
1239 sendto_one_numeric(source_p, ERR_BADCHANNAME, form_str(ERR_BADCHANNAME), forward);
1240 return;
1241 }
1242 /* don't forward to inconsistent target -- jilles */
1243 if(chptr->chname[0] == '#' && forward[0] == '&')
1244 {
1245 sendto_one_numeric(source_p, ERR_BADCHANNAME,
1246 form_str(ERR_BADCHANNAME), forward);
1247 return;
1248 }
1249 if(MyClient(source_p) && (targptr = find_channel(forward)) == NULL)
1250 {
1251 sendto_one_numeric(source_p, ERR_NOSUCHCHANNEL,
1252 form_str(ERR_NOSUCHCHANNEL), forward);
1253 return;
1254 }
1255 if(MyClient(source_p) && !(targptr->mode.mode & MODE_FREETARGET))
1256 {
1257 if((msptr = find_channel_membership(targptr, source_p)) == NULL ||
7eec45bc 1258 get_channel_access(source_p, msptr) != CHFL_CHANOP)
212380e3
AC
1259 {
1260 sendto_one(source_p, form_str(ERR_CHANOPRIVSNEEDED),
1261 me.name, source_p->name, targptr->chname);
1262 return;
1263 }
1264 }
1265
f427c8b0 1266 rb_strlcpy(chptr->mode.forward, forward, sizeof(chptr->mode.forward));
212380e3
AC
1267
1268 mode_changes[mode_count].letter = c;
1269 mode_changes[mode_count].dir = MODE_ADD;
1270 mode_changes[mode_count].caps = 0;
1271 mode_changes[mode_count].nocaps = 0;
2da6f6eb
JT
1272 mode_changes[mode_count].mems =
1273 ConfigChannel.use_forward ? ALL_MEMBERS : ONLY_SERVERS;
212380e3
AC
1274 mode_changes[mode_count].id = NULL;
1275 mode_changes[mode_count++].arg = forward;
1276 }
1277 else if(dir == MODE_DEL)
1278 {
1279 if(!(*chptr->mode.forward))
1280 return;
1281
1282 *chptr->mode.forward = '\0';
1283
1284 mode_changes[mode_count].letter = c;
1285 mode_changes[mode_count].dir = MODE_DEL;
1286 mode_changes[mode_count].caps = 0;
1287 mode_changes[mode_count].nocaps = 0;
1288 mode_changes[mode_count].mems = ALL_MEMBERS;
1289 mode_changes[mode_count].id = NULL;
1290 mode_changes[mode_count++].arg = NULL;
1291 }
1292}
1293
1294void
1295chm_key(struct Client *source_p, struct Channel *chptr,
1296 int alevel, int parc, int *parn,
1297 const char **parv, int *errors, int dir, char c, long mode_type)
1298{
1299 char *key;
1300
1301 if(alevel != CHFL_CHANOP)
1302 {
1303 if(!(*errors & SM_ERR_NOOPS))
1304 sendto_one(source_p, form_str(ERR_CHANOPRIVSNEEDED),
1305 me.name, source_p->name, chptr->chname);
1306 *errors |= SM_ERR_NOOPS;
1307 return;
1308 }
1309
1310 if(dir == MODE_QUERY)
1311 return;
1312
84c9a8c7
JT
1313 if(MyClient(source_p) && (++mode_limit_simple > MAXMODES_SIMPLE))
1314 return;
1315
212380e3
AC
1316 if((dir == MODE_ADD) && parc > *parn)
1317 {
1318 key = LOCAL_COPY(parv[(*parn)]);
1319 (*parn)++;
1320
1321 if(MyClient(source_p))
1322 fix_key(key);
1323 else
1324 fix_key_remote(key);
1325
1326 if(EmptyString(key))
1327 return;
1328
1329 s_assert(key[0] != ' ');
f427c8b0 1330 rb_strlcpy(chptr->mode.key, key, sizeof(chptr->mode.key));
212380e3
AC
1331
1332 mode_changes[mode_count].letter = c;
1333 mode_changes[mode_count].dir = MODE_ADD;
1334 mode_changes[mode_count].caps = 0;
1335 mode_changes[mode_count].nocaps = 0;
1336 mode_changes[mode_count].mems = ALL_MEMBERS;
1337 mode_changes[mode_count].id = NULL;
1338 mode_changes[mode_count++].arg = chptr->mode.key;
1339 }
1340 else if(dir == MODE_DEL)
1341 {
1342 static char splat[] = "*";
1343 int i;
1344
1345 if(parc > *parn)
1346 (*parn)++;
1347
1348 if(!(*chptr->mode.key))
1349 return;
1350
1351 /* hack time. when we get a +k-k mode, the +k arg is
1352 * chptr->mode.key, which the -k sets to \0, so hunt for a
1353 * +k when we get a -k, and set the arg to splat. --anfl
1354 */
1355 for(i = 0; i < mode_count; i++)
1356 {
1357 if(mode_changes[i].letter == 'k' && mode_changes[i].dir == MODE_ADD)
1358 mode_changes[i].arg = splat;
1359 }
1360
1361 *chptr->mode.key = 0;
1362
1363 mode_changes[mode_count].letter = c;
1364 mode_changes[mode_count].dir = MODE_DEL;
1365 mode_changes[mode_count].caps = 0;
1366 mode_changes[mode_count].nocaps = 0;
1367 mode_changes[mode_count].mems = ALL_MEMBERS;
1368 mode_changes[mode_count].id = NULL;
1369 mode_changes[mode_count++].arg = "*";
1370 }
1371}
1372
212380e3
AC
1373/* *INDENT-OFF* */
1374struct ChannelMode chmode_table[256] =
1375{
1376 {chm_nosuch, 0 }, /* 0x00 */
1377 {chm_nosuch, 0 }, /* 0x01 */
1378 {chm_nosuch, 0 }, /* 0x02 */
1379 {chm_nosuch, 0 }, /* 0x03 */
1380 {chm_nosuch, 0 }, /* 0x04 */
1381 {chm_nosuch, 0 }, /* 0x05 */
1382 {chm_nosuch, 0 }, /* 0x06 */
1383 {chm_nosuch, 0 }, /* 0x07 */
1384 {chm_nosuch, 0 }, /* 0x08 */
1385 {chm_nosuch, 0 }, /* 0x09 */
1386 {chm_nosuch, 0 }, /* 0x0a */
1387 {chm_nosuch, 0 }, /* 0x0b */
1388 {chm_nosuch, 0 }, /* 0x0c */
1389 {chm_nosuch, 0 }, /* 0x0d */
1390 {chm_nosuch, 0 }, /* 0x0e */
1391 {chm_nosuch, 0 }, /* 0x0f */
1392 {chm_nosuch, 0 }, /* 0x10 */
1393 {chm_nosuch, 0 }, /* 0x11 */
1394 {chm_nosuch, 0 }, /* 0x12 */
1395 {chm_nosuch, 0 }, /* 0x13 */
1396 {chm_nosuch, 0 }, /* 0x14 */
1397 {chm_nosuch, 0 }, /* 0x15 */
1398 {chm_nosuch, 0 }, /* 0x16 */
1399 {chm_nosuch, 0 }, /* 0x17 */
1400 {chm_nosuch, 0 }, /* 0x18 */
1401 {chm_nosuch, 0 }, /* 0x19 */
1402 {chm_nosuch, 0 }, /* 0x1a */
1403 {chm_nosuch, 0 }, /* 0x1b */
1404 {chm_nosuch, 0 }, /* 0x1c */
1405 {chm_nosuch, 0 }, /* 0x1d */
1406 {chm_nosuch, 0 }, /* 0x1e */
1407 {chm_nosuch, 0 }, /* 0x1f */
1408 {chm_nosuch, 0 }, /* 0x20 */
1409 {chm_nosuch, 0 }, /* 0x21 */
1410 {chm_nosuch, 0 }, /* 0x22 */
1411 {chm_nosuch, 0 }, /* 0x23 */
1412 {chm_nosuch, 0 }, /* 0x24 */
1413 {chm_nosuch, 0 }, /* 0x25 */
1414 {chm_nosuch, 0 }, /* 0x26 */
1415 {chm_nosuch, 0 }, /* 0x27 */
1416 {chm_nosuch, 0 }, /* 0x28 */
1417 {chm_nosuch, 0 }, /* 0x29 */
1418 {chm_nosuch, 0 }, /* 0x2a */
1419 {chm_nosuch, 0 }, /* 0x2b */
1420 {chm_nosuch, 0 }, /* 0x2c */
1421 {chm_nosuch, 0 }, /* 0x2d */
1422 {chm_nosuch, 0 }, /* 0x2e */
1423 {chm_nosuch, 0 }, /* 0x2f */
1424 {chm_nosuch, 0 }, /* 0x30 */
1425 {chm_nosuch, 0 }, /* 0x31 */
1426 {chm_nosuch, 0 }, /* 0x32 */
1427 {chm_nosuch, 0 }, /* 0x33 */
1428 {chm_nosuch, 0 }, /* 0x34 */
1429 {chm_nosuch, 0 }, /* 0x35 */
1430 {chm_nosuch, 0 }, /* 0x36 */
1431 {chm_nosuch, 0 }, /* 0x37 */
1432 {chm_nosuch, 0 }, /* 0x38 */
1433 {chm_nosuch, 0 }, /* 0x39 */
1434 {chm_nosuch, 0 }, /* 0x3a */
1435 {chm_nosuch, 0 }, /* 0x3b */
1436 {chm_nosuch, 0 }, /* 0x3c */
1437 {chm_nosuch, 0 }, /* 0x3d */
1438 {chm_nosuch, 0 }, /* 0x3e */
1439 {chm_nosuch, 0 }, /* 0x3f */
1440
1441 {chm_nosuch, 0 }, /* @ */
1442 {chm_nosuch, 0 }, /* A */
1443 {chm_nosuch, 0 }, /* B */
2e918bf5 1444 {chm_simple, MODE_NOCTCP }, /* C */
212380e3
AC
1445 {chm_nosuch, 0 }, /* D */
1446 {chm_nosuch, 0 }, /* E */
1447 {chm_simple, MODE_FREETARGET }, /* F */
1448 {chm_nosuch, 0 }, /* G */
1449 {chm_nosuch, 0 }, /* H */
1450 {chm_ban, CHFL_INVEX }, /* I */
1451 {chm_nosuch, 0 }, /* J */
1452 {chm_nosuch, 0 }, /* K */
1453 {chm_staff, MODE_EXLIMIT }, /* L */
1454 {chm_nosuch, 0 }, /* M */
1455 {chm_nosuch, 0 }, /* N */
1456 {chm_nosuch, 0 }, /* O */
1457 {chm_staff, MODE_PERMANENT }, /* P */
1458 {chm_simple, MODE_DISFORWARD }, /* Q */
1459 {chm_nosuch, 0 }, /* R */
1460 {chm_nosuch, 0 }, /* S */
1461 {chm_nosuch, 0 }, /* T */
1462 {chm_nosuch, 0 }, /* U */
1463 {chm_nosuch, 0 }, /* V */
1464 {chm_nosuch, 0 }, /* W */
1465 {chm_nosuch, 0 }, /* X */
1466 {chm_nosuch, 0 }, /* Y */
1467 {chm_nosuch, 0 }, /* Z */
1468 {chm_nosuch, 0 },
1469 {chm_nosuch, 0 },
1470 {chm_nosuch, 0 },
1471 {chm_nosuch, 0 },
1472 {chm_nosuch, 0 },
1473 {chm_nosuch, 0 },
1474 {chm_nosuch, 0 }, /* a */
1475 {chm_ban, CHFL_BAN }, /* b */
1476 {chm_simple, MODE_NOCOLOR }, /* c */
1477 {chm_nosuch, 0 }, /* d */
1478 {chm_ban, CHFL_EXCEPTION }, /* e */
1479 {chm_forward, 0 }, /* f */
1480 {chm_simple, MODE_FREEINVITE }, /* g */
1481 {chm_nosuch, 0 }, /* h */
1482 {chm_simple, MODE_INVITEONLY }, /* i */
1483 {chm_throttle, 0 }, /* j */
1484 {chm_key, 0 }, /* k */
1485 {chm_limit, 0 }, /* l */
1486 {chm_simple, MODE_MODERATED }, /* m */
1487 {chm_simple, MODE_NOPRIVMSGS }, /* n */
1488 {chm_op, 0 }, /* o */
1489 {chm_simple, MODE_PRIVATE }, /* p */
1490 {chm_ban, CHFL_QUIET }, /* q */
83b72f91 1491 {chm_simple, MODE_REGONLY }, /* r */
212380e3
AC
1492 {chm_simple, MODE_SECRET }, /* s */
1493 {chm_simple, MODE_TOPICLIMIT }, /* t */
1494 {chm_nosuch, 0 }, /* u */
1495 {chm_voice, 0 }, /* v */
1496 {chm_nosuch, 0 }, /* w */
1497 {chm_nosuch, 0 }, /* x */
1498 {chm_nosuch, 0 }, /* y */
1499 {chm_simple, MODE_OPMODERATE }, /* z */
1500
1501 {chm_nosuch, 0 }, /* 0x7b */
1502 {chm_nosuch, 0 }, /* 0x7c */
1503 {chm_nosuch, 0 }, /* 0x7d */
1504 {chm_nosuch, 0 }, /* 0x7e */
1505 {chm_nosuch, 0 }, /* 0x7f */
1506
1507 {chm_nosuch, 0 }, /* 0x80 */
1508 {chm_nosuch, 0 }, /* 0x81 */
1509 {chm_nosuch, 0 }, /* 0x82 */
1510 {chm_nosuch, 0 }, /* 0x83 */
1511 {chm_nosuch, 0 }, /* 0x84 */
1512 {chm_nosuch, 0 }, /* 0x85 */
1513 {chm_nosuch, 0 }, /* 0x86 */
1514 {chm_nosuch, 0 }, /* 0x87 */
1515 {chm_nosuch, 0 }, /* 0x88 */
1516 {chm_nosuch, 0 }, /* 0x89 */
1517 {chm_nosuch, 0 }, /* 0x8a */
1518 {chm_nosuch, 0 }, /* 0x8b */
1519 {chm_nosuch, 0 }, /* 0x8c */
1520 {chm_nosuch, 0 }, /* 0x8d */
1521 {chm_nosuch, 0 }, /* 0x8e */
1522 {chm_nosuch, 0 }, /* 0x8f */
1523
1524 {chm_nosuch, 0 }, /* 0x90 */
1525 {chm_nosuch, 0 }, /* 0x91 */
1526 {chm_nosuch, 0 }, /* 0x92 */
1527 {chm_nosuch, 0 }, /* 0x93 */
1528 {chm_nosuch, 0 }, /* 0x94 */
1529 {chm_nosuch, 0 }, /* 0x95 */
1530 {chm_nosuch, 0 }, /* 0x96 */
1531 {chm_nosuch, 0 }, /* 0x97 */
1532 {chm_nosuch, 0 }, /* 0x98 */
1533 {chm_nosuch, 0 }, /* 0x99 */
1534 {chm_nosuch, 0 }, /* 0x9a */
1535 {chm_nosuch, 0 }, /* 0x9b */
1536 {chm_nosuch, 0 }, /* 0x9c */
1537 {chm_nosuch, 0 }, /* 0x9d */
1538 {chm_nosuch, 0 }, /* 0x9e */
1539 {chm_nosuch, 0 }, /* 0x9f */
1540
1541 {chm_nosuch, 0 }, /* 0xa0 */
1542 {chm_nosuch, 0 }, /* 0xa1 */
1543 {chm_nosuch, 0 }, /* 0xa2 */
1544 {chm_nosuch, 0 }, /* 0xa3 */
1545 {chm_nosuch, 0 }, /* 0xa4 */
1546 {chm_nosuch, 0 }, /* 0xa5 */
1547 {chm_nosuch, 0 }, /* 0xa6 */
1548 {chm_nosuch, 0 }, /* 0xa7 */
1549 {chm_nosuch, 0 }, /* 0xa8 */
1550 {chm_nosuch, 0 }, /* 0xa9 */
1551 {chm_nosuch, 0 }, /* 0xaa */
1552 {chm_nosuch, 0 }, /* 0xab */
1553 {chm_nosuch, 0 }, /* 0xac */
1554 {chm_nosuch, 0 }, /* 0xad */
1555 {chm_nosuch, 0 }, /* 0xae */
1556 {chm_nosuch, 0 }, /* 0xaf */
1557
1558 {chm_nosuch, 0 }, /* 0xb0 */
1559 {chm_nosuch, 0 }, /* 0xb1 */
1560 {chm_nosuch, 0 }, /* 0xb2 */
1561 {chm_nosuch, 0 }, /* 0xb3 */
1562 {chm_nosuch, 0 }, /* 0xb4 */
1563 {chm_nosuch, 0 }, /* 0xb5 */
1564 {chm_nosuch, 0 }, /* 0xb6 */
1565 {chm_nosuch, 0 }, /* 0xb7 */
1566 {chm_nosuch, 0 }, /* 0xb8 */
1567 {chm_nosuch, 0 }, /* 0xb9 */
1568 {chm_nosuch, 0 }, /* 0xba */
1569 {chm_nosuch, 0 }, /* 0xbb */
1570 {chm_nosuch, 0 }, /* 0xbc */
1571 {chm_nosuch, 0 }, /* 0xbd */
1572 {chm_nosuch, 0 }, /* 0xbe */
1573 {chm_nosuch, 0 }, /* 0xbf */
1574
1575 {chm_nosuch, 0 }, /* 0xc0 */
1576 {chm_nosuch, 0 }, /* 0xc1 */
1577 {chm_nosuch, 0 }, /* 0xc2 */
1578 {chm_nosuch, 0 }, /* 0xc3 */
1579 {chm_nosuch, 0 }, /* 0xc4 */
1580 {chm_nosuch, 0 }, /* 0xc5 */
1581 {chm_nosuch, 0 }, /* 0xc6 */
1582 {chm_nosuch, 0 }, /* 0xc7 */
1583 {chm_nosuch, 0 }, /* 0xc8 */
1584 {chm_nosuch, 0 }, /* 0xc9 */
1585 {chm_nosuch, 0 }, /* 0xca */
1586 {chm_nosuch, 0 }, /* 0xcb */
1587 {chm_nosuch, 0 }, /* 0xcc */
1588 {chm_nosuch, 0 }, /* 0xcd */
1589 {chm_nosuch, 0 }, /* 0xce */
1590 {chm_nosuch, 0 }, /* 0xcf */
1591
1592 {chm_nosuch, 0 }, /* 0xd0 */
1593 {chm_nosuch, 0 }, /* 0xd1 */
1594 {chm_nosuch, 0 }, /* 0xd2 */
1595 {chm_nosuch, 0 }, /* 0xd3 */
1596 {chm_nosuch, 0 }, /* 0xd4 */
1597 {chm_nosuch, 0 }, /* 0xd5 */
1598 {chm_nosuch, 0 }, /* 0xd6 */
1599 {chm_nosuch, 0 }, /* 0xd7 */
1600 {chm_nosuch, 0 }, /* 0xd8 */
1601 {chm_nosuch, 0 }, /* 0xd9 */
1602 {chm_nosuch, 0 }, /* 0xda */
1603 {chm_nosuch, 0 }, /* 0xdb */
1604 {chm_nosuch, 0 }, /* 0xdc */
1605 {chm_nosuch, 0 }, /* 0xdd */
1606 {chm_nosuch, 0 }, /* 0xde */
1607 {chm_nosuch, 0 }, /* 0xdf */
1608
1609 {chm_nosuch, 0 }, /* 0xe0 */
1610 {chm_nosuch, 0 }, /* 0xe1 */
1611 {chm_nosuch, 0 }, /* 0xe2 */
1612 {chm_nosuch, 0 }, /* 0xe3 */
1613 {chm_nosuch, 0 }, /* 0xe4 */
1614 {chm_nosuch, 0 }, /* 0xe5 */
1615 {chm_nosuch, 0 }, /* 0xe6 */
1616 {chm_nosuch, 0 }, /* 0xe7 */
1617 {chm_nosuch, 0 }, /* 0xe8 */
1618 {chm_nosuch, 0 }, /* 0xe9 */
1619 {chm_nosuch, 0 }, /* 0xea */
1620 {chm_nosuch, 0 }, /* 0xeb */
1621 {chm_nosuch, 0 }, /* 0xec */
1622 {chm_nosuch, 0 }, /* 0xed */
1623 {chm_nosuch, 0 }, /* 0xee */
1624 {chm_nosuch, 0 }, /* 0xef */
1625
1626 {chm_nosuch, 0 }, /* 0xf0 */
1627 {chm_nosuch, 0 }, /* 0xf1 */
1628 {chm_nosuch, 0 }, /* 0xf2 */
1629 {chm_nosuch, 0 }, /* 0xf3 */
1630 {chm_nosuch, 0 }, /* 0xf4 */
1631 {chm_nosuch, 0 }, /* 0xf5 */
1632 {chm_nosuch, 0 }, /* 0xf6 */
1633 {chm_nosuch, 0 }, /* 0xf7 */
1634 {chm_nosuch, 0 }, /* 0xf8 */
1635 {chm_nosuch, 0 }, /* 0xf9 */
1636 {chm_nosuch, 0 }, /* 0xfa */
1637 {chm_nosuch, 0 }, /* 0xfb */
1638 {chm_nosuch, 0 }, /* 0xfc */
1639 {chm_nosuch, 0 }, /* 0xfd */
1640 {chm_nosuch, 0 }, /* 0xfe */
1641 {chm_nosuch, 0 }, /* 0xff */
1642};
1643
1644/* *INDENT-ON* */
1645
1646/* set_channel_mode()
1647 *
1648 * inputs - client, source, channel, membership pointer, params
1649 * output -
1650 * side effects - channel modes/memberships are changed, MODE is issued
1651 *
1652 * Extensively modified to be hotpluggable, 03/09/06 -- nenolod
1653 */
1654void
1655set_channel_mode(struct Client *client_p, struct Client *source_p,
1656 struct Channel *chptr, struct membership *msptr, int parc, const char *parv[])
1657{
1658 static char modebuf[BUFSIZE];
1659 static char parabuf[BUFSIZE];
1660 char *mbuf;
1661 char *pbuf;
1662 int cur_len, mlen, paralen, paracount, arglen, len;
1663 int i, j, flags;
1664 int dir = MODE_ADD;
1665 int parn = 1;
1666 int errors = 0;
1667 int alevel;
1668 const char *ml = parv[0];
1669 char c;
1670 struct Client *fakesource_p;
1671
1672 mask_pos = 0;
765d839d 1673 removed_mask_pos = 0;
212380e3
AC
1674 mode_count = 0;
1675 mode_limit = 0;
84c9a8c7 1676 mode_limit_simple = 0;
212380e3 1677
7eec45bc 1678 alevel = get_channel_access(source_p, msptr);
212380e3
AC
1679
1680 /* Hide connecting server on netburst -- jilles */
1681 if (ConfigServerHide.flatten_links && IsServer(source_p) && !has_id(source_p) && !HasSentEob(source_p))
1682 fakesource_p = &me;
1683 else
1684 fakesource_p = source_p;
1685
1686 for(; (c = *ml) != 0; ml++)
1687 {
1688 switch (c)
1689 {
1690 case '+':
1691 dir = MODE_ADD;
1692 break;
1693 case '-':
1694 dir = MODE_DEL;
1695 break;
1696 case '=':
1697 dir = MODE_QUERY;
1698 break;
1699 default:
78e6b731
SB
1700 /* If this mode char is locked, don't allow local users to change it. */
1701 if (MyClient(source_p) && chptr->mode_lock && strchr(chptr->mode_lock, c))
01ed04ab 1702 {
6917ed0e
JT
1703 if (!(errors & SM_ERR_MLOCK))
1704 sendto_one_numeric(source_p,
1705 ERR_MLOCKRESTRICTED,
1706 form_str(ERR_MLOCKRESTRICTED),
1707 chptr->chname,
1708 c,
1709 chptr->mode_lock);
1710 errors |= SM_ERR_MLOCK;
78e6b731 1711 continue;
01ed04ab 1712 }
212380e3
AC
1713 chmode_table[(unsigned char) c].set_func(fakesource_p, chptr, alevel,
1714 parc, &parn, parv,
1715 &errors, dir, c,
1716 chmode_table[(unsigned char) c].mode_type);
1717 break;
1718 }
1719 }
1720
1721 /* bail out if we have nothing to do... */
1722 if(!mode_count)
1723 return;
1724
1725 if(IsServer(source_p))
b2f0da88 1726 mlen = rb_sprintf(modebuf, ":%s MODE %s ", fakesource_p->name, chptr->chname);
212380e3 1727 else
b2f0da88 1728 mlen = rb_sprintf(modebuf, ":%s!%s@%s MODE %s ",
212380e3
AC
1729 source_p->name, source_p->username,
1730 source_p->host, chptr->chname);
1731
1732 for(j = 0, flags = ALL_MEMBERS; j < 2; j++, flags = ONLY_CHANOPS)
1733 {
1734 cur_len = mlen;
1735 mbuf = modebuf + mlen;
1736 pbuf = parabuf;
1737 parabuf[0] = '\0';
1738 paracount = paralen = 0;
1739 dir = MODE_QUERY;
1740
1741 for(i = 0; i < mode_count; i++)
1742 {
1743 if(mode_changes[i].letter == 0 || mode_changes[i].mems != flags)
1744 continue;
1745
1746 if(mode_changes[i].arg != NULL)
1747 {
1748 arglen = strlen(mode_changes[i].arg);
1749
1750 if(arglen > MODEBUFLEN - 5)
1751 continue;
1752 }
1753 else
1754 arglen = 0;
1755
1756 /* if we're creeping over MAXMODEPARAMSSERV, or over
1757 * bufsize (4 == +/-,modechar,two spaces) send now.
1758 */
1759 if(mode_changes[i].arg != NULL &&
1760 ((paracount == MAXMODEPARAMSSERV) ||
1761 ((cur_len + paralen + arglen + 4) > (BUFSIZE - 3))))
1762 {
1763 *mbuf = '\0';
1764
1765 if(cur_len > mlen)
1766 sendto_channel_local(flags, chptr, "%s %s", modebuf,
1767 parabuf);
1768 else
1769 continue;
1770
1771 paracount = paralen = 0;
1772 cur_len = mlen;
1773 mbuf = modebuf + mlen;
1774 pbuf = parabuf;
1775 parabuf[0] = '\0';
1776 dir = MODE_QUERY;
1777 }
1778
1779 if(dir != mode_changes[i].dir)
1780 {
1781 *mbuf++ = (mode_changes[i].dir == MODE_ADD) ? '+' : '-';
1782 cur_len++;
1783 dir = mode_changes[i].dir;
1784 }
1785
1786 *mbuf++ = mode_changes[i].letter;
1787 cur_len++;
1788
1789 if(mode_changes[i].arg != NULL)
1790 {
1791 paracount++;
b2f0da88 1792 len = rb_sprintf(pbuf, "%s ", mode_changes[i].arg);
212380e3
AC
1793 pbuf += len;
1794 paralen += len;
1795 }
1796 }
1797
1798 if(paralen && parabuf[paralen - 1] == ' ')
1799 parabuf[paralen - 1] = '\0';
1800
1801 *mbuf = '\0';
1802 if(cur_len > mlen)
1803 sendto_channel_local(flags, chptr, "%s %s", modebuf, parabuf);
1804 }
1805
1806 /* only propagate modes originating locally, or if we're hubbing */
330fc5c1 1807 if(MyClient(source_p) || rb_dlink_list_length(&serv_list) > 1)
212380e3
AC
1808 send_cap_mode_changes(client_p, source_p, chptr, mode_changes, mode_count);
1809}
8727cbe8
AC
1810
1811/* set_channel_mlock()
1812 *
1813 * inputs - client, source, channel, params
1814 * output -
1815 * side effects - channel mlock is changed / MLOCK is propagated
1816 */
1817void
1818set_channel_mlock(struct Client *client_p, struct Client *source_p,
6fb6bd15 1819 struct Channel *chptr, const char *newmlock, int propagate)
8727cbe8 1820{
78e6b731 1821 rb_free(chptr->mode_lock);
6b8db2da 1822 chptr->mode_lock = newmlock ? rb_strdup(newmlock) : NULL;
8727cbe8 1823
6fb6bd15
AC
1824 if (propagate)
1825 {
1826 sendto_server(client_p, NULL, CAP_TS6 | CAP_MLOCK, NOCAPS, ":%s MLOCK %ld %s :%s",
1827 source_p->id, (long) chptr->channelts, chptr->chname,
1828 chptr->mode_lock ? chptr->mode_lock : "");
1829 }
8727cbe8 1830}