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