]> jfr.im git - solanum.git/blame - ircd/chmode.c
send: add sendto_one_multiline_* API
[solanum.git] / ircd / chmode.c
CommitLineData
25ea5d2f 1/*
a6f63a82 2 * Solanum: a slightly advanced ircd
25ea5d2f
EM
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
25ea5d2f
EM
24 */
25
26#include "stdinc.h"
27#include "channel.h"
28#include "client.h"
25ea5d2f
EM
29#include "hash.h"
30#include "hook.h"
31#include "match.h"
32#include "ircd.h"
33#include "numeric.h"
34#include "s_serv.h" /* captab */
35#include "s_user.h"
36#include "send.h"
37#include "whowas.h"
38#include "s_conf.h" /* ConfigFileEntry, ConfigChannel */
39#include "s_newconf.h"
40#include "logger.h"
41#include "chmode.h"
77d3d2db 42#include "s_assert.h"
b870a5f8 43#include "parse.h"
04952c32 44#include "msgbuf.h"
25ea5d2f
EM
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;
55abcbb2 84
c18cb68b
VY
85 *ptr = '\0';
86 *ptr2 = '\0';
efccc22c
VY
87
88 for(i = 0; i < 256; i++)
89 {
b5c8d52d
EK
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 }
55abcbb2 104
c18cb68b
VY
105 switch (chmode_flags[i])
106 {
c55b2782 107 case MODE_FREETARGET:
73d0f900 108 case MODE_DISFORWARD:
2da6f6eb
JT
109 if(ConfigChannel.use_forward)
110 *ptr++ = (char) i;
111 break;
c18cb68b
VY
112 default:
113 if(chmode_flags[i] != 0)
114 {
115 *ptr++ = (char) i;
116 }
117 }
55abcbb2 118
c18cb68b 119 /* Should we leave orphaned check here? -- dwr */
b5c8d52d 120 if (chmode_table[i].set_func != NULL &&
b5c8d52d 121 chmode_table[i].set_func != chm_orphaned)
c18cb68b
VY
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
b5c8d52d 158 if (chmode_table[c].set_func != NULL &&
19716b9f
JT
159 chmode_table[c].set_func != chm_orphaned)
160 return 0;
161
d295a398 162 if (chmode_table[c].set_func == NULL)
19716b9f
JT
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
3ee43bcf 182get_channel_access(struct Client *source_p, struct Channel *chptr, struct membership *msptr, int dir, const char *modestr)
212380e3 183{
749d8c11
AC
184 hook_data_channel_approval moduledata;
185
186 if(!MyClient(source_p))
212380e3
AC
187 return CHFL_CHANOP;
188
749d8c11 189 moduledata.client = source_p;
3ee43bcf 190 moduledata.chptr = chptr;
749d8c11
AC
191 moduledata.msptr = msptr;
192 moduledata.target = NULL;
b4e3861b 193 moduledata.approved = (msptr != NULL && is_chanop(msptr)) ? CHFL_CHANOP : CHFL_PEON;
202d4966 194 moduledata.dir = dir;
b870a5f8 195 moduledata.modestr = modestr;
8aabb973 196
749d8c11
AC
197 call_hook(h_get_channel_access, &moduledata);
198
199 return moduledata.approved;
212380e3
AC
200}
201
f3b3ad0b
JT
202/* allow_mode_change()
203 *
204 * Checks if mlock and chanops permit a mode change.
205 *
206 * inputs - client, channel, access level, errors pointer, mode char
9aa639ed 207 * outputs - false on failure, true on success
f3b3ad0b
JT
208 * side effects - error message sent on failure
209 */
9aa639ed 210static bool
f3b3ad0b
JT
211allow_mode_change(struct Client *source_p, struct Channel *chptr, int alevel,
212 int *errors, char c)
213{
214 /* If this mode char is locked, don't allow local users to change it. */
215 if (MyClient(source_p) && chptr->mode_lock && strchr(chptr->mode_lock, c))
216 {
217 if (!(*errors & SM_ERR_MLOCK))
218 sendto_one_numeric(source_p,
219 ERR_MLOCKRESTRICTED,
220 form_str(ERR_MLOCKRESTRICTED),
221 chptr->chname,
222 c,
223 chptr->mode_lock);
224 *errors |= SM_ERR_MLOCK;
9aa639ed 225 return false;
f3b3ad0b 226 }
1046ac77 227 if(alevel < CHFL_CHANOP)
f3b3ad0b
JT
228 {
229 if(!(*errors & SM_ERR_NOOPS))
230 sendto_one(source_p, form_str(ERR_CHANOPRIVSNEEDED),
231 me.name, source_p->name, chptr->chname);
232 *errors |= SM_ERR_NOOPS;
9aa639ed 233 return false;
f3b3ad0b 234 }
9aa639ed 235 return true;
f3b3ad0b
JT
236}
237
212380e3
AC
238/* add_id()
239 *
765d839d 240 * inputs - client, channel, id to add, type, forward
a383180a 241 * outputs - false on failure, true on success
212380e3
AC
242 * side effects - given id is added to the appropriate list
243 */
a383180a 244bool
765d839d 245add_id(struct Client *source_p, struct Channel *chptr, const char *banid, const char *forward,
330fc5c1 246 rb_dlink_list * list, long mode_type)
212380e3
AC
247{
248 struct Ban *actualBan;
83294285 249 static char who[USERHOST_REPLYLEN];
212380e3 250 char *realban = LOCAL_COPY(banid);
330fc5c1 251 rb_dlink_node *ptr;
212380e3
AC
252
253 /* dont let local clients overflow the banlist, or set redundant
254 * bans
255 */
256 if(MyClient(source_p))
257 {
d8f0b5d7 258 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)) >= (unsigned long)((chptr->mode.mode & MODE_EXLIMIT) ? ConfigChannel.max_bans_large : ConfigChannel.max_bans))
212380e3
AC
259 {
260 sendto_one(source_p, form_str(ERR_BANLISTFULL),
261 me.name, source_p->name, chptr->chname, realban);
a383180a 262 return false;
212380e3
AC
263 }
264
5cefa1d6 265 RB_DLINK_FOREACH(ptr, list->head)
212380e3
AC
266 {
267 actualBan = ptr->data;
fd488ac1 268 if(mask_match(actualBan->banstr, realban))
a383180a 269 return false;
212380e3
AC
270 }
271 }
272 /* dont let remotes set duplicates */
273 else
274 {
5cefa1d6 275 RB_DLINK_FOREACH(ptr, list->head)
212380e3
AC
276 {
277 actualBan = ptr->data;
278 if(!irccmp(actualBan->banstr, realban))
a383180a 279 return false;
212380e3
AC
280 }
281 }
282
283
284 if(IsPerson(source_p))
5203cba5 285 sprintf(who, "%s!%s@%s", source_p->name, source_p->username, source_p->host);
212380e3 286 else
f427c8b0 287 rb_strlcpy(who, source_p->name, sizeof(who));
212380e3 288
765d839d 289 actualBan = allocate_ban(realban, who, forward);
e3354945 290 actualBan->when = rb_current_time();
212380e3 291
330fc5c1 292 rb_dlinkAdd(actualBan, &actualBan->node, list);
212380e3
AC
293
294 /* invalidate the can_send() cache */
295 if(mode_type == CHFL_BAN || mode_type == CHFL_QUIET || mode_type == CHFL_EXCEPTION)
b5f3e5e5 296 chptr->bants = rb_current_time();
212380e3 297
a383180a 298 return true;
212380e3
AC
299}
300
301/* del_id()
302 *
303 * inputs - channel, id to remove, type
765d839d
EM
304 * outputs - pointer to ban that was removed, if any
305 * side effects - given id is removed from the appropriate list and returned
212380e3 306 */
765d839d 307struct Ban *
330fc5c1 308del_id(struct Channel *chptr, const char *banid, rb_dlink_list * list, long mode_type)
212380e3 309{
330fc5c1 310 rb_dlink_node *ptr;
212380e3
AC
311 struct Ban *banptr;
312
313 if(EmptyString(banid))
765d839d 314 return NULL;
212380e3 315
5cefa1d6 316 RB_DLINK_FOREACH(ptr, list->head)
212380e3
AC
317 {
318 banptr = ptr->data;
319
320 if(irccmp(banid, banptr->banstr) == 0)
321 {
330fc5c1 322 rb_dlinkDelete(&banptr->node, list);
212380e3
AC
323
324 /* invalidate the can_send() cache */
325 if(mode_type == CHFL_BAN || mode_type == CHFL_QUIET || mode_type == CHFL_EXCEPTION)
b5f3e5e5 326 chptr->bants = rb_current_time();
212380e3 327
765d839d 328 return banptr;
212380e3
AC
329 }
330 }
331
765d839d 332 return NULL;
212380e3
AC
333}
334
335/* check_string()
336 *
337 * input - string to check
338 * output - pointer to 'fixed' string, or "*" if empty
339 * side effects - any white space found becomes \0
340 */
341static char *
342check_string(char *s)
343{
344 char *str = s;
345 static char splat[] = "*";
346 if(!(s && *s))
347 return splat;
348
349 for(; *s; ++s)
350 {
351 if(IsSpace(*s))
352 {
353 *s = '\0';
354 break;
355 }
356 }
357 return str;
358}
359
360/* pretty_mask()
361 *
362 * inputs - mask to pretty
363 * outputs - better version of the mask
364 * side effects - mask is chopped to limits, and transformed:
365 * x!y@z => x!y@z
366 * y@z => *!y@z
367 * x!y => x!y@*
368 * x => x!*@*
369 * z.d => *!*@z.d
370 */
371static char *
372pretty_mask(const char *idmask)
373{
374 static char mask_buf[BUFSIZE];
375 int old_mask_pos;
f4e893b5 376 const char *nick, *user, *host, *forward = NULL;
74b2fb72 377 char *t, *at, *ex;
f4e893b5 378 int nl, ul, hl, fl;
212380e3 379 char *mask;
74b2fb72 380 size_t masklen;
212380e3
AC
381
382 mask = LOCAL_COPY(idmask);
383 mask = check_string(mask);
384 collapse(mask);
74b2fb72 385 masklen = strlen(mask);
212380e3 386
f4e893b5 387 nick = user = host = "*";
74b2fb72
JT
388 nl = ul = hl = 1;
389 fl = 0;
212380e3 390
74b2fb72 391 if((size_t) BUFSIZE - mask_pos < masklen + 5)
212380e3
AC
392 return NULL;
393
394 old_mask_pos = mask_pos;
395
396 if (*mask == '$')
397 {
74b2fb72
JT
398 memcpy(mask_buf + mask_pos, mask, masklen + 1);
399 mask_pos += masklen + 1;
212380e3
AC
400 t = mask_buf + old_mask_pos + 1;
401 if (*t == '!')
402 *t = '~';
403 if (*t == '~')
404 t++;
7e6b5384 405 *t = irctolower(*t);
212380e3
AC
406 return mask_buf + old_mask_pos;
407 }
408
74b2fb72
JT
409 at = ex = NULL;
410 if((t = memchr(mask, '@', masklen)) != NULL)
212380e3
AC
411 {
412 at = t;
74b2fb72 413 t++;
212380e3 414 if(*t != '\0')
74b2fb72 415 host = t, hl = strlen(t);
212380e3 416
74b2fb72 417 if((t = memchr(mask, '!', at - mask)) != NULL)
212380e3
AC
418 {
419 ex = t;
74b2fb72
JT
420 t++;
421 if(at != t)
422 user = t, ul = at - t;
423 if(ex != mask)
424 nick = mask, nl = ex - mask;
212380e3
AC
425 }
426 else
427 {
74b2fb72
JT
428 if(at != mask)
429 user = mask, ul = at - mask;
212380e3 430 }
765d839d 431
74b2fb72
JT
432 if((t = memchr(host, '!', hl)) != NULL ||
433 (t = memchr(host, '$', hl)) != NULL)
765d839d 434 {
74b2fb72
JT
435 t++;
436 if (host + hl != t)
437 forward = t, fl = host + hl - t;
438 hl = t - 1 - host;
765d839d 439 }
212380e3 440 }
74b2fb72 441 else if((t = memchr(mask, '!', masklen)) != NULL)
212380e3
AC
442 {
443 ex = t;
74b2fb72
JT
444 t++;
445 if(ex != mask)
446 nick = mask, nl = ex - mask;
212380e3 447 if(*t != '\0')
74b2fb72 448 user = t, ul = strlen(t);
212380e3 449 }
74b2fb72
JT
450 else if(memchr(mask, '.', masklen) != NULL ||
451 memchr(mask, ':', masklen) != NULL)
212380e3 452 {
74b2fb72 453 host = mask, hl = masklen;
212380e3
AC
454 }
455 else
456 {
74b2fb72
JT
457 if(masklen > 0)
458 nick = mask, nl = masklen;
212380e3
AC
459 }
460
461 /* truncate values to max lengths */
f4e893b5
JT
462 if(nl > NICKLEN - 1)
463 nl = NICKLEN - 1;
f4e893b5
JT
464 if(ul > USERLEN)
465 ul = USERLEN;
f4e893b5
JT
466 if(hl > HOSTLEN)
467 hl = HOSTLEN;
f4e893b5
JT
468 if(fl > CHANNELLEN)
469 fl = CHANNELLEN;
470
471 memcpy(mask_buf + mask_pos, nick, nl), mask_pos += nl;
472 mask_buf[mask_pos++] = '!';
473 memcpy(mask_buf + mask_pos, user, ul), mask_pos += ul;
474 mask_buf[mask_pos++] = '@';
475 memcpy(mask_buf + mask_pos, host, hl), mask_pos += hl;
476 if (forward) {
477 mask_buf[mask_pos++] = '$';
478 memcpy(mask_buf + mask_pos, forward, fl), mask_pos += fl;
212380e3 479 }
f4e893b5 480 mask_buf[mask_pos++] = '\0';
212380e3 481
212380e3
AC
482 return mask_buf + old_mask_pos;
483}
484
0c730321
JT
485/* check_forward()
486 *
487 * input - client, channel to set mode on, target channel name
488 * output - true if forwarding should be allowed
489 * side effects - numeric sent if not allowed
490 */
e0a9b5d3 491static bool
0c730321
JT
492check_forward(struct Client *source_p, struct Channel *chptr,
493 const char *forward)
494{
73d759ae 495 struct Channel *targptr = NULL;
0c730321
JT
496 struct membership *msptr;
497
498 if(!check_channel_name(forward) ||
499 (MyClient(source_p) && (strlen(forward) > LOC_CHANNELLEN || hash_find_resv(forward))))
500 {
501 sendto_one_numeric(source_p, ERR_BADCHANNAME, form_str(ERR_BADCHANNAME), forward);
e0a9b5d3 502 return false;
0c730321
JT
503 }
504 /* don't forward to inconsistent target -- jilles */
505 if(chptr->chname[0] == '#' && forward[0] == '&')
506 {
507 sendto_one_numeric(source_p, ERR_BADCHANNAME,
508 form_str(ERR_BADCHANNAME), forward);
e0a9b5d3 509 return false;
0c730321
JT
510 }
511 if(MyClient(source_p) && (targptr = find_channel(forward)) == NULL)
512 {
513 sendto_one_numeric(source_p, ERR_NOSUCHCHANNEL,
514 form_str(ERR_NOSUCHCHANNEL), forward);
e0a9b5d3 515 return false;
0c730321
JT
516 }
517 if(MyClient(source_p) && !(targptr->mode.mode & MODE_FREETARGET))
518 {
519 if((msptr = find_channel_membership(targptr, source_p)) == NULL ||
1046ac77 520 get_channel_access(source_p, targptr, msptr, MODE_QUERY, NULL) < CHFL_CHANOP)
0c730321
JT
521 {
522 sendto_one(source_p, form_str(ERR_CHANOPRIVSNEEDED),
523 me.name, source_p->name, targptr->chname);
e0a9b5d3 524 return false;
0c730321
JT
525 }
526 }
e0a9b5d3 527 return true;
0c730321
JT
528}
529
212380e3
AC
530/* fix_key()
531 *
532 * input - key to fix
533 * output - the same key, fixed
534 * side effects - anything below ascii 13 is discarded, ':' discarded,
535 * high ascii is dropped to lower half of ascii table
536 */
537static char *
538fix_key(char *arg)
539{
4b11f391 540 unsigned char *s, *t, c;
212380e3 541
4b11f391 542 for(s = t = (unsigned char *) arg; (c = *s); s++)
212380e3
AC
543 {
544 c &= 0x7f;
545 if(c != ':' && c != ',' && c > ' ')
546 *t++ = c;
547 }
548
549 *t = '\0';
550 return arg;
551}
552
553/* fix_key_remote()
554 *
555 * input - key to fix
556 * ouput - the same key, fixed
557 * side effects - high ascii dropped to lower half of table,
558 * CR/LF/':' are dropped
559 */
560static char *
561fix_key_remote(char *arg)
562{
4b11f391 563 unsigned char *s, *t, c;
212380e3 564
4b11f391 565 for(s = t = (unsigned char *) arg; (c = *s); s++)
212380e3
AC
566 {
567 c &= 0x7f;
568 if((c != 0x0a) && (c != ':') && (c != ',') && (c != 0x0d) && (c != ' '))
569 *t++ = c;
570 }
571
572 *t = '\0';
573 return arg;
574}
575
212380e3
AC
576void
577chm_simple(struct Client *source_p, struct Channel *chptr,
04952c32 578 int alevel, const char *arg, int *errors, int dir, char c, long mode_type)
212380e3 579{
f3b3ad0b 580 if(!allow_mode_change(source_p, chptr, alevel, errors, c))
212380e3 581 return;
212380e3 582
84c9a8c7 583 if(MyClient(source_p) && (++mode_limit_simple > MAXMODES_SIMPLE))
212380e3
AC
584 return;
585
586 /* setting + */
78e6b731 587 if((dir == MODE_ADD) && !(chptr->mode.mode & mode_type))
212380e3 588 {
2da6f6eb
JT
589 /* if +f is disabled, ignore an attempt to set +QF locally */
590 if(!ConfigChannel.use_forward && MyClient(source_p) &&
591 (c == 'Q' || c == 'F'))
592 return;
593
212380e3
AC
594 chptr->mode.mode |= mode_type;
595
596 mode_changes[mode_count].letter = c;
597 mode_changes[mode_count].dir = MODE_ADD;
212380e3
AC
598 mode_changes[mode_count].id = NULL;
599 mode_changes[mode_count].mems = ALL_MEMBERS;
cbed45a2
VY
600 mode_changes[mode_count++].arg = NULL;
601 }
78e6b731 602 else if((dir == MODE_DEL) && (chptr->mode.mode & mode_type))
cbed45a2
VY
603 {
604 chptr->mode.mode &= ~mode_type;
605
606 mode_changes[mode_count].letter = c;
607 mode_changes[mode_count].dir = MODE_DEL;
cbed45a2
VY
608 mode_changes[mode_count].mems = ALL_MEMBERS;
609 mode_changes[mode_count].id = NULL;
610 mode_changes[mode_count++].arg = NULL;
611 }
612}
613
614void
615chm_orphaned(struct Client *source_p, struct Channel *chptr,
04952c32 616 int alevel, const char *arg, int *errors, int dir, char c, long mode_type)
cbed45a2
VY
617{
618 if(MyClient(source_p))
619 return;
55abcbb2 620
cbed45a2
VY
621 if((dir == MODE_ADD) && !(chptr->mode.mode & mode_type))
622 {
623 chptr->mode.mode |= mode_type;
624
625 mode_changes[mode_count].letter = c;
626 mode_changes[mode_count].dir = MODE_ADD;
cbed45a2
VY
627 mode_changes[mode_count].id = NULL;
628 mode_changes[mode_count].mems = ALL_MEMBERS;
212380e3
AC
629 mode_changes[mode_count++].arg = NULL;
630 }
631 else if((dir == MODE_DEL) && (chptr->mode.mode & mode_type))
632 {
633 chptr->mode.mode &= ~mode_type;
634
635 mode_changes[mode_count].letter = c;
636 mode_changes[mode_count].dir = MODE_DEL;
212380e3
AC
637 mode_changes[mode_count].mems = ALL_MEMBERS;
638 mode_changes[mode_count].id = NULL;
639 mode_changes[mode_count++].arg = NULL;
640 }
641}
642
be29ec79
AC
643void
644chm_hidden(struct Client *source_p, struct Channel *chptr,
04952c32 645 int alevel, const char *arg, int *errors, int dir, char c, long mode_type)
be29ec79 646{
07697336 647 if(MyClient(source_p) && !IsOperGeneral(source_p))
be29ec79
AC
648 {
649 if(!(*errors & SM_ERR_NOPRIVS))
650 sendto_one_numeric(source_p, ERR_NOPRIVILEGES, form_str(ERR_NOPRIVILEGES));
651 *errors |= SM_ERR_NOPRIVS;
652 return;
653 }
654 if(MyClient(source_p) && !IsOperAdmin(source_p))
655 {
656 if(!(*errors & SM_ERR_NOPRIVS))
657 sendto_one(source_p, form_str(ERR_NOPRIVS), me.name,
658 source_p->name, "admin");
659 *errors |= SM_ERR_NOPRIVS;
660 return;
661 }
662
663 if(MyClient(source_p) && (++mode_limit_simple > MAXMODES_SIMPLE))
664 return;
665
666 /* setting + */
667 if((dir == MODE_ADD) && !(chptr->mode.mode & mode_type))
668 {
669 chptr->mode.mode |= mode_type;
670
671 mode_changes[mode_count].letter = c;
672 mode_changes[mode_count].dir = MODE_ADD;
be29ec79
AC
673 mode_changes[mode_count].id = NULL;
674 mode_changes[mode_count].mems = ONLY_OPERS;
be29ec79
AC
675 mode_changes[mode_count++].arg = NULL;
676 }
677 else if((dir == MODE_DEL) && (chptr->mode.mode & mode_type))
678 {
679 chptr->mode.mode &= ~mode_type;
680
681 mode_changes[mode_count].letter = c;
682 mode_changes[mode_count].dir = MODE_DEL;
be29ec79
AC
683 mode_changes[mode_count].mems = ONLY_OPERS;
684 mode_changes[mode_count].id = NULL;
be29ec79
AC
685 mode_changes[mode_count++].arg = NULL;
686 }
687}
688
212380e3
AC
689void
690chm_staff(struct Client *source_p, struct Channel *chptr,
04952c32 691 int alevel, const char *arg, int *errors, int dir, char c, long mode_type)
212380e3 692{
07697336 693 if(MyClient(source_p) && !IsOper(source_p))
212380e3
AC
694 {
695 if(!(*errors & SM_ERR_NOPRIVS))
696 sendto_one_numeric(source_p, ERR_NOPRIVILEGES, form_str(ERR_NOPRIVILEGES));
697 *errors |= SM_ERR_NOPRIVS;
698 return;
699 }
80303ab7 700 if(MyClient(source_p) && !HasPrivilege(source_p, "oper:cmodes"))
1ef5b430
JT
701 {
702 if(!(*errors & SM_ERR_NOPRIVS))
703 sendto_one(source_p, form_str(ERR_NOPRIVS), me.name,
80303ab7 704 source_p->name, "cmodes");
1ef5b430
JT
705 *errors |= SM_ERR_NOPRIVS;
706 return;
707 }
212380e3 708
84c9a8c7
JT
709 if(MyClient(source_p) && (++mode_limit_simple > MAXMODES_SIMPLE))
710 return;
711
212380e3
AC
712 /* setting + */
713 if((dir == MODE_ADD) && !(chptr->mode.mode & mode_type))
714 {
715 chptr->mode.mode |= mode_type;
716
717 mode_changes[mode_count].letter = c;
718 mode_changes[mode_count].dir = MODE_ADD;
212380e3
AC
719 mode_changes[mode_count].id = NULL;
720 mode_changes[mode_count].mems = ALL_MEMBERS;
721 mode_changes[mode_count++].arg = NULL;
722 }
723 else if((dir == MODE_DEL) && (chptr->mode.mode & mode_type))
724 {
725 chptr->mode.mode &= ~mode_type;
726
727 mode_changes[mode_count].letter = c;
728 mode_changes[mode_count].dir = MODE_DEL;
212380e3
AC
729 mode_changes[mode_count].mems = ALL_MEMBERS;
730 mode_changes[mode_count].id = NULL;
731 mode_changes[mode_count++].arg = NULL;
732 }
733}
734
735void
736chm_ban(struct Client *source_p, struct Channel *chptr,
04952c32 737 int alevel, const char *arg, int *errors, int dir, char c, long mode_type)
212380e3 738{
04952c32 739 const char *mask;
cea0689e 740 char *forward;
330fc5c1
AC
741 rb_dlink_list *list;
742 rb_dlink_node *ptr;
212380e3
AC
743 struct Ban *banptr;
744 int errorval;
6f7b36d5
AC
745 const char *rpl_list_p;
746 const char *rpl_endlist_p;
212380e3
AC
747 int mems;
748
749 switch (mode_type)
750 {
751 case CHFL_BAN:
752 list = &chptr->banlist;
753 errorval = SM_ERR_RPL_B;
6f7b36d5
AC
754 rpl_list_p = form_str(RPL_BANLIST);
755 rpl_endlist_p = form_str(RPL_ENDOFBANLIST);
212380e3 756 mems = ALL_MEMBERS;
212380e3
AC
757 break;
758
759 case CHFL_EXCEPTION:
760 /* if +e is disabled, allow all but +e locally */
04952c32 761 if (!ConfigChannel.use_except && MyClient(source_p) && dir == MODE_ADD)
212380e3
AC
762 return;
763
764 list = &chptr->exceptlist;
765 errorval = SM_ERR_RPL_E;
6f7b36d5
AC
766 rpl_list_p = form_str(RPL_EXCEPTLIST);
767 rpl_endlist_p = form_str(RPL_ENDOFEXCEPTLIST);
212380e3
AC
768
769 if(ConfigChannel.use_except || (dir == MODE_DEL))
770 mems = ONLY_CHANOPS;
771 else
772 mems = ONLY_SERVERS;
773 break;
774
775 case CHFL_INVEX:
776 /* if +I is disabled, allow all but +I locally */
04952c32 777 if (!ConfigChannel.use_invex && MyClient(source_p) && dir == MODE_ADD)
212380e3
AC
778 return;
779
780 list = &chptr->invexlist;
781 errorval = SM_ERR_RPL_I;
6f7b36d5
AC
782 rpl_list_p = form_str(RPL_INVITELIST);
783 rpl_endlist_p = form_str(RPL_ENDOFINVITELIST);
212380e3
AC
784
785 if(ConfigChannel.use_invex || (dir == MODE_DEL))
786 mems = ONLY_CHANOPS;
787 else
788 mems = ONLY_SERVERS;
789 break;
790
791 case CHFL_QUIET:
792 list = &chptr->quietlist;
793 errorval = SM_ERR_RPL_Q;
6f7b36d5
AC
794 rpl_list_p = form_str(RPL_QUIETLIST);
795 rpl_endlist_p = form_str(RPL_ENDOFQUIETLIST);
212380e3 796 mems = ALL_MEMBERS;
212380e3
AC
797 break;
798
799 default:
a9227555 800 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, "chm_ban() called with unknown type!");
212380e3 801 return;
212380e3
AC
802 }
803
04952c32 804 if (dir == MODE_QUERY)
212380e3
AC
805 {
806 if((*errors & errorval) != 0)
807 return;
808 *errors |= errorval;
809
810 /* non-ops cant see +eI lists.. */
f3b3ad0b 811 /* note that this is still permitted if +e/+I are mlocked. */
1046ac77 812 if(alevel < CHFL_CHANOP && mode_type != CHFL_BAN &&
212380e3
AC
813 mode_type != CHFL_QUIET)
814 {
815 if(!(*errors & SM_ERR_NOOPS))
816 sendto_one(source_p, form_str(ERR_CHANOPRIVSNEEDED),
817 me.name, source_p->name, chptr->chname);
818 *errors |= SM_ERR_NOOPS;
819 return;
820 }
821
5cefa1d6 822 RB_DLINK_FOREACH(ptr, list->head)
212380e3 823 {
765d839d 824 char buf[BANLEN];
212380e3 825 banptr = ptr->data;
765d839d 826 if(banptr->forward)
5203cba5 827 snprintf(buf, sizeof(buf), "%s$%s", banptr->banstr, banptr->forward);
765d839d
EM
828 else
829 rb_strlcpy(buf, banptr->banstr, sizeof(buf));
830
6f7b36d5 831 sendto_one(source_p, rpl_list_p,
212380e3 832 me.name, source_p->name, chptr->chname,
765d839d 833 buf, banptr->who, banptr->when);
212380e3 834 }
6f7b36d5 835 sendto_one(source_p, rpl_endlist_p, me.name, source_p->name, chptr->chname);
212380e3
AC
836 return;
837 }
838
04952c32 839 if (!allow_mode_change(source_p, chptr, alevel, errors, c))
212380e3 840 return;
f3b3ad0b 841
212380e3 842
04952c32 843 if (MyClient(source_p) && (++mode_limit > MAXMODEPARAMS))
212380e3
AC
844 return;
845
212380e3 846 /* empty ban, or starts with ':' which messes up s2s, ignore it */
04952c32 847 if (EmptyString(arg) || *arg == ':')
212380e3
AC
848 return;
849
04952c32 850 if (!MyClient(source_p))
212380e3 851 {
04952c32 852 if (strchr(arg, ' '))
212380e3
AC
853 return;
854
04952c32 855 mask = arg;
212380e3
AC
856 }
857 else
04952c32 858 mask = pretty_mask(arg);
212380e3 859
1aa35c8a
JT
860 /* we'd have problems parsing this, hyb6 does it too
861 * also make sure it will always fit on a line with channel
862 * name etc.
863 */
79435744 864 if(strlen(mask) > MIN(BANLEN, MODEBUFLEN - 5))
be0365e1
JT
865 {
866 sendto_one_numeric(source_p, ERR_INVALIDBAN,
867 form_str(ERR_INVALIDBAN),
04952c32 868 chptr->chname, c, arg);
1aa35c8a 869 return;
be0365e1 870 }
1aa35c8a 871
765d839d
EM
872 /* Look for a $ after the first character.
873 * As the first character, it marks an extban; afterwards
874 * it delimits a forward channel.
875 */
876 if ((forward = strchr(mask+1, '$')) != NULL)
877 {
878 *forward++ = '\0';
879 if (*forward == '\0')
880 forward = NULL;
881 }
882
212380e3 883 /* if we're adding a NEW id */
04952c32 884 if (dir == MODE_ADD)
212380e3
AC
885 {
886 if (*mask == '$' && MyClient(source_p))
887 {
888 if (!valid_extban(mask, source_p, chptr, mode_type))
be0365e1
JT
889 {
890 sendto_one_numeric(source_p, ERR_INVALIDBAN,
891 form_str(ERR_INVALIDBAN),
04952c32 892 chptr->chname, c, arg);
212380e3 893 return;
be0365e1 894 }
212380e3
AC
895 }
896
e1dc9e54
JT
897 /* For compatibility, only check the forward channel from
898 * local clients. Accept any forward channel from servers.
899 */
04952c32 900 if (forward != NULL && MyClient(source_p))
e1dc9e54 901 {
f2edb2be
JT
902 /* For simplicity and future flexibility, do not
903 * allow '$' in forwarding targets.
904 */
04952c32 905 if (!ConfigChannel.use_forward ||
be0365e1
JT
906 strchr(forward, '$') != NULL)
907 {
908 sendto_one_numeric(source_p, ERR_INVALIDBAN,
909 form_str(ERR_INVALIDBAN),
04952c32 910 chptr->chname, c, arg);
be0365e1
JT
911 return;
912 }
913 /* check_forward() sends its own error message */
04952c32 914 if (!check_forward(source_p, chptr, forward))
f2edb2be 915 return;
5efa7ef6 916 /* Forwards only make sense for bans. */
04952c32 917 if (mode_type != CHFL_BAN)
be0365e1
JT
918 {
919 sendto_one_numeric(source_p, ERR_INVALIDBAN,
920 form_str(ERR_INVALIDBAN),
04952c32 921 chptr->chname, c, arg);
5efa7ef6 922 return;
be0365e1 923 }
e1dc9e54 924 }
2da6f6eb 925
212380e3
AC
926 /* dont allow local clients to overflow the banlist, dont
927 * let remote servers set duplicate bans
928 */
04952c32 929 if (!add_id(source_p, chptr, mask, forward, list, mode_type))
212380e3
AC
930 return;
931
04952c32 932 if (forward)
765d839d
EM
933 forward[-1]= '$';
934
212380e3
AC
935 mode_changes[mode_count].letter = c;
936 mode_changes[mode_count].dir = MODE_ADD;
212380e3
AC
937 mode_changes[mode_count].mems = mems;
938 mode_changes[mode_count].id = NULL;
939 mode_changes[mode_count++].arg = mask;
940 }
04952c32 941 else if (dir == MODE_DEL)
212380e3 942 {
765d839d
EM
943 struct Ban *removed;
944 static char buf[BANLEN * MAXMODEPARAMS];
945 int old_removed_mask_pos = removed_mask_pos;
04952c32 946 if ((removed = del_id(chptr, mask, list, mode_type)) == NULL)
212380e3 947 {
04952c32
EK
948 /* mask isn't a valid ban, check arg */
949 if ((removed = del_id(chptr, arg, list, mode_type)) != NULL)
950 mask = arg;
212380e3
AC
951 }
952
04952c32 953 if (removed && removed->forward)
5203cba5 954 removed_mask_pos += snprintf(buf + old_removed_mask_pos, sizeof(buf), "%s$%s", removed->banstr, removed->forward) + 1;
765d839d 955 else
3e910a18 956 removed_mask_pos += rb_strlcpy(buf + old_removed_mask_pos, mask, sizeof(buf)) + 1;
04952c32 957 if (removed)
765d839d
EM
958 {
959 free_ban(removed);
960 removed = NULL;
961 }
962
212380e3
AC
963 mode_changes[mode_count].letter = c;
964 mode_changes[mode_count].dir = MODE_DEL;
212380e3
AC
965 mode_changes[mode_count].mems = mems;
966 mode_changes[mode_count].id = NULL;
765d839d 967 mode_changes[mode_count++].arg = buf + old_removed_mask_pos;
212380e3
AC
968 }
969}
970
971void
972chm_op(struct Client *source_p, struct Channel *chptr,
04952c32 973 int alevel, const char *arg, int *errors, int dir, char c, long mode_type)
212380e3
AC
974{
975 struct membership *mstptr;
212380e3
AC
976 struct Client *targ_p;
977
f3b3ad0b 978 if(!allow_mode_change(source_p, chptr, alevel, errors, c))
212380e3 979 return;
212380e3 980
212380e3 981 /* empty nick */
04952c32 982 if(EmptyString(arg))
212380e3
AC
983 {
984 sendto_one_numeric(source_p, ERR_NOSUCHNICK, form_str(ERR_NOSUCHNICK), "*");
985 return;
986 }
987
04952c32 988 if((targ_p = find_chasing(source_p, arg, NULL)) == NULL)
212380e3
AC
989 {
990 return;
991 }
992
993 mstptr = find_channel_membership(chptr, targ_p);
994
995 if(mstptr == NULL)
996 {
997 if(!(*errors & SM_ERR_NOTONCHANNEL) && MyClient(source_p))
998 sendto_one_numeric(source_p, ERR_USERNOTINCHANNEL,
04952c32 999 form_str(ERR_USERNOTINCHANNEL), arg, chptr->chname);
212380e3
AC
1000 *errors |= SM_ERR_NOTONCHANNEL;
1001 return;
1002 }
1003
1004 if(MyClient(source_p) && (++mode_limit > MAXMODEPARAMS))
1005 return;
1006
1007 if(dir == MODE_ADD)
1008 {
fa0e2152
JT
1009 if(targ_p == source_p && mstptr->flags & CHFL_CHANOP)
1010 return;
1011
212380e3
AC
1012 mode_changes[mode_count].letter = c;
1013 mode_changes[mode_count].dir = MODE_ADD;
212380e3
AC
1014 mode_changes[mode_count].mems = ALL_MEMBERS;
1015 mode_changes[mode_count].id = targ_p->id;
0f8db055 1016 mode_changes[mode_count++].arg = targ_p->name;
212380e3
AC
1017
1018 mstptr->flags |= CHFL_CHANOP;
212380e3
AC
1019 }
1020 else
1021 {
1022 if(MyClient(source_p) && IsService(targ_p))
1023 {
1024 sendto_one(source_p, form_str(ERR_ISCHANSERVICE),
1025 me.name, source_p->name, targ_p->name, chptr->chname);
1026 return;
1027 }
1028
1029 mode_changes[mode_count].letter = c;
1030 mode_changes[mode_count].dir = MODE_DEL;
212380e3
AC
1031 mode_changes[mode_count].mems = ALL_MEMBERS;
1032 mode_changes[mode_count].id = targ_p->id;
0f8db055 1033 mode_changes[mode_count++].arg = targ_p->name;
212380e3
AC
1034
1035 mstptr->flags &= ~CHFL_CHANOP;
1036 }
1037}
1038
1039void
1040chm_voice(struct Client *source_p, struct Channel *chptr,
04952c32 1041 int alevel, const char *arg, int *errors, int dir, char c, long mode_type)
212380e3
AC
1042{
1043 struct membership *mstptr;
212380e3
AC
1044 struct Client *targ_p;
1045
f3b3ad0b 1046 if(!allow_mode_change(source_p, chptr, alevel, errors, c))
212380e3 1047 return;
212380e3 1048
212380e3 1049 /* empty nick */
04952c32 1050 if(EmptyString(arg))
212380e3
AC
1051 {
1052 sendto_one_numeric(source_p, ERR_NOSUCHNICK, form_str(ERR_NOSUCHNICK), "*");
1053 return;
1054 }
1055
04952c32 1056 if((targ_p = find_chasing(source_p, arg, NULL)) == NULL)
212380e3
AC
1057 {
1058 return;
1059 }
1060
1061 mstptr = find_channel_membership(chptr, targ_p);
1062
1063 if(mstptr == NULL)
1064 {
1065 if(!(*errors & SM_ERR_NOTONCHANNEL) && MyClient(source_p))
1066 sendto_one_numeric(source_p, ERR_USERNOTINCHANNEL,
04952c32 1067 form_str(ERR_USERNOTINCHANNEL), arg, chptr->chname);
212380e3
AC
1068 *errors |= SM_ERR_NOTONCHANNEL;
1069 return;
1070 }
1071
1072 if(MyClient(source_p) && (++mode_limit > MAXMODEPARAMS))
1073 return;
1074
1075 if(dir == MODE_ADD)
1076 {
1077 mode_changes[mode_count].letter = c;
1078 mode_changes[mode_count].dir = MODE_ADD;
212380e3
AC
1079 mode_changes[mode_count].mems = ALL_MEMBERS;
1080 mode_changes[mode_count].id = targ_p->id;
0f8db055 1081 mode_changes[mode_count++].arg = targ_p->name;
212380e3
AC
1082
1083 mstptr->flags |= CHFL_VOICE;
1084 }
1085 else
1086 {
1087 mode_changes[mode_count].letter = 'v';
1088 mode_changes[mode_count].dir = MODE_DEL;
212380e3
AC
1089 mode_changes[mode_count].mems = ALL_MEMBERS;
1090 mode_changes[mode_count].id = targ_p->id;
0f8db055 1091 mode_changes[mode_count++].arg = targ_p->name;
212380e3
AC
1092
1093 mstptr->flags &= ~CHFL_VOICE;
1094 }
1095}
1096
1097void
1098chm_limit(struct Client *source_p, struct Channel *chptr,
04952c32 1099 int alevel, const char *arg, int *errors, int dir, char c, long mode_type)
212380e3 1100{
212380e3
AC
1101 static char limitstr[30];
1102 int limit;
1103
04952c32 1104 if (!allow_mode_change(source_p, chptr, alevel, errors, c))
212380e3
AC
1105 return;
1106
04952c32 1107 if (MyClient(source_p) && (++mode_limit_simple > MAXMODES_SIMPLE))
84c9a8c7
JT
1108 return;
1109
04952c32 1110 if (dir == MODE_ADD)
212380e3 1111 {
04952c32 1112 if (EmptyString(arg) || (limit = atoi(arg)) <= 0)
212380e3
AC
1113 return;
1114
5203cba5 1115 sprintf(limitstr, "%d", limit);
212380e3
AC
1116
1117 mode_changes[mode_count].letter = c;
1118 mode_changes[mode_count].dir = MODE_ADD;
212380e3
AC
1119 mode_changes[mode_count].mems = ALL_MEMBERS;
1120 mode_changes[mode_count].id = NULL;
1121 mode_changes[mode_count++].arg = limitstr;
1122
1123 chptr->mode.limit = limit;
1124 }
04952c32 1125 else if (dir == MODE_DEL)
212380e3
AC
1126 {
1127 if(!chptr->mode.limit)
1128 return;
1129
1130 chptr->mode.limit = 0;
1131
1132 mode_changes[mode_count].letter = c;
1133 mode_changes[mode_count].dir = MODE_DEL;
212380e3
AC
1134 mode_changes[mode_count].mems = ALL_MEMBERS;
1135 mode_changes[mode_count].id = NULL;
1136 mode_changes[mode_count++].arg = NULL;
1137 }
1138}
1139
1140void
1141chm_throttle(struct Client *source_p, struct Channel *chptr,
04952c32 1142 int alevel, const char *arg, int *errors, int dir, char c, long mode_type)
212380e3
AC
1143{
1144 int joins = 0, timeslice = 0;
1145
04952c32 1146 if (!allow_mode_change(source_p, chptr, alevel, errors, c))
212380e3
AC
1147 return;
1148
04952c32 1149 if (MyClient(source_p) && (++mode_limit_simple > MAXMODES_SIMPLE))
84c9a8c7
JT
1150 return;
1151
04952c32 1152 if (dir == MODE_ADD)
212380e3 1153 {
04952c32 1154 if (sscanf(arg, "%d:%d", &joins, &timeslice) < 2)
2daf1813 1155 return;
212380e3 1156
90552e21 1157 if(joins <= 0 || timeslice <= 0)
212380e3
AC
1158 return;
1159
1160 mode_changes[mode_count].letter = c;
1161 mode_changes[mode_count].dir = MODE_ADD;
212380e3
AC
1162 mode_changes[mode_count].mems = ALL_MEMBERS;
1163 mode_changes[mode_count].id = NULL;
04952c32 1164 mode_changes[mode_count++].arg = arg;
212380e3
AC
1165
1166 chptr->mode.join_num = joins;
1167 chptr->mode.join_time = timeslice;
1168 }
1169 else if(dir == MODE_DEL)
1170 {
1171 if(!chptr->mode.join_num)
1172 return;
1173
1174 chptr->mode.join_num = 0;
1175 chptr->mode.join_time = 0;
1176 chptr->join_count = 0;
1177 chptr->join_delta = 0;
1178
1179 mode_changes[mode_count].letter = c;
1180 mode_changes[mode_count].dir = MODE_DEL;
212380e3
AC
1181 mode_changes[mode_count].mems = ALL_MEMBERS;
1182 mode_changes[mode_count].id = NULL;
1183 mode_changes[mode_count++].arg = NULL;
1184 }
1185}
1186
1187void
1188chm_forward(struct Client *source_p, struct Channel *chptr,
04952c32 1189 int alevel, const char *arg, int *errors, int dir, char c, long mode_type)
212380e3 1190{
2da6f6eb 1191 /* if +f is disabled, ignore local attempts to set it */
04952c32 1192 if (!ConfigChannel.use_forward && MyClient(source_p) && dir == MODE_ADD)
2da6f6eb
JT
1193 return;
1194
04952c32 1195 if (dir == MODE_QUERY)
212380e3
AC
1196 {
1197 if (!(*errors & SM_ERR_RPL_F))
1198 {
1199 if (*chptr->mode.forward == '\0')
5366977b 1200 sendto_one_notice(source_p, ":%s has no forward channel", chptr->chname);
212380e3 1201 else
5366977b 1202 sendto_one_notice(source_p, ":%s forward channel is %s", chptr->chname, chptr->mode.forward);
212380e3
AC
1203 *errors |= SM_ERR_RPL_F;
1204 }
1205 return;
1206 }
1207
1208#ifndef FORWARD_OPERONLY
04952c32 1209 if (!allow_mode_change(source_p, chptr, alevel, errors, c))
212380e3 1210 return;
212380e3 1211#else
04952c32 1212 if (!IsOperGeneral(source_p) && !IsServer(source_p))
212380e3
AC
1213 {
1214 if(!(*errors & SM_ERR_NOPRIVS))
1215 sendto_one_numeric(source_p, ERR_NOPRIVILEGES, form_str(ERR_NOPRIVILEGES));
1216 *errors |= SM_ERR_NOPRIVS;
1217 return;
1218 }
1219#endif
1220
04952c32 1221 if (MyClient(source_p) && (++mode_limit_simple > MAXMODES_SIMPLE))
84c9a8c7
JT
1222 return;
1223
04952c32 1224 if (dir == MODE_ADD)
212380e3 1225 {
04952c32 1226 if(EmptyString(arg))
212380e3 1227 return;
0c730321 1228
04952c32 1229 if(!check_forward(source_p, chptr, arg))
212380e3 1230 return;
212380e3 1231
04952c32 1232 rb_strlcpy(chptr->mode.forward, arg, sizeof(chptr->mode.forward));
212380e3
AC
1233
1234 mode_changes[mode_count].letter = c;
1235 mode_changes[mode_count].dir = MODE_ADD;
2da6f6eb
JT
1236 mode_changes[mode_count].mems =
1237 ConfigChannel.use_forward ? ALL_MEMBERS : ONLY_SERVERS;
212380e3 1238 mode_changes[mode_count].id = NULL;
04952c32 1239 mode_changes[mode_count++].arg = arg;
212380e3
AC
1240 }
1241 else if(dir == MODE_DEL)
1242 {
1243 if(!(*chptr->mode.forward))
1244 return;
1245
1246 *chptr->mode.forward = '\0';
1247
1248 mode_changes[mode_count].letter = c;
1249 mode_changes[mode_count].dir = MODE_DEL;
212380e3
AC
1250 mode_changes[mode_count].mems = ALL_MEMBERS;
1251 mode_changes[mode_count].id = NULL;
1252 mode_changes[mode_count++].arg = NULL;
1253 }
1254}
1255
1256void
1257chm_key(struct Client *source_p, struct Channel *chptr,
04952c32 1258 int alevel, const char *arg, int *errors, int dir, char c, long mode_type)
212380e3
AC
1259{
1260 char *key;
1261
04952c32 1262 if (!allow_mode_change(source_p, chptr, alevel, errors, c))
212380e3 1263 return;
212380e3 1264
04952c32 1265 if (MyClient(source_p) && (++mode_limit_simple > MAXMODES_SIMPLE))
212380e3
AC
1266 return;
1267
04952c32 1268 if (dir == MODE_ADD)
212380e3 1269 {
04952c32 1270 key = LOCAL_COPY(arg);
212380e3
AC
1271
1272 if(MyClient(source_p))
1273 fix_key(key);
1274 else
1275 fix_key_remote(key);
1276
1277 if(EmptyString(key))
1278 return;
1279
1280 s_assert(key[0] != ' ');
f427c8b0 1281 rb_strlcpy(chptr->mode.key, key, sizeof(chptr->mode.key));
212380e3
AC
1282
1283 mode_changes[mode_count].letter = c;
1284 mode_changes[mode_count].dir = MODE_ADD;
212380e3
AC
1285 mode_changes[mode_count].mems = ALL_MEMBERS;
1286 mode_changes[mode_count].id = NULL;
1287 mode_changes[mode_count++].arg = chptr->mode.key;
1288 }
1289 else if(dir == MODE_DEL)
1290 {
1291 static char splat[] = "*";
1292 int i;
1293
212380e3
AC
1294 if(!(*chptr->mode.key))
1295 return;
1296
1297 /* hack time. when we get a +k-k mode, the +k arg is
1298 * chptr->mode.key, which the -k sets to \0, so hunt for a
1299 * +k when we get a -k, and set the arg to splat. --anfl
1300 */
1301 for(i = 0; i < mode_count; i++)
1302 {
1303 if(mode_changes[i].letter == 'k' && mode_changes[i].dir == MODE_ADD)
1304 mode_changes[i].arg = splat;
1305 }
1306
1307 *chptr->mode.key = 0;
1308
1309 mode_changes[mode_count].letter = c;
1310 mode_changes[mode_count].dir = MODE_DEL;
212380e3
AC
1311 mode_changes[mode_count].mems = ALL_MEMBERS;
1312 mode_changes[mode_count].id = NULL;
1313 mode_changes[mode_count++].arg = "*";
1314 }
1315}
1316
212380e3
AC
1317/* *INDENT-OFF* */
1318struct ChannelMode chmode_table[256] =
1319{
04952c32
EK
1320 ['F'] = {chm_simple, MODE_FREETARGET, 0 },
1321 ['I'] = {chm_ban, CHFL_INVEX, CHM_QUERYABLE | CHM_OPS_QUERY },
1322 ['L'] = {chm_staff, MODE_EXLIMIT, 0 },
1323 ['P'] = {chm_staff, MODE_PERMANENT, 0 },
1324 ['Q'] = {chm_simple, MODE_DISFORWARD, 0 },
1325 ['b'] = {chm_ban, CHFL_BAN, CHM_QUERYABLE },
1326 ['e'] = {chm_ban, CHFL_EXCEPTION, CHM_QUERYABLE | CHM_OPS_QUERY },
1327 ['f'] = {chm_forward, 0, CHM_ARG_SET | CHM_CAN_QUERY }, /* weird because it's nonstandard and violates isupport */
1328 ['g'] = {chm_simple, MODE_FREEINVITE, 0 },
1329 ['i'] = {chm_simple, MODE_INVITEONLY, 0 },
1330 ['j'] = {chm_throttle, 0, CHM_ARG_SET },
1331 ['k'] = {chm_key, 0, CHM_QUERYABLE },
1332 ['l'] = {chm_limit, 0, CHM_ARG_SET },
1333 ['m'] = {chm_simple, MODE_MODERATED, 0 },
1334 ['n'] = {chm_simple, MODE_NOPRIVMSGS, 0 },
1335 ['o'] = {chm_op, 0, CHM_ARGS },
1336 ['p'] = {chm_simple, MODE_PRIVATE, 0 },
1337 ['q'] = {chm_ban, CHFL_QUIET, CHM_QUERYABLE },
1338 ['r'] = {chm_simple, MODE_REGONLY, 0 },
1339 ['s'] = {chm_simple, MODE_SECRET, 0 },
1340 ['t'] = {chm_simple, MODE_TOPICLIMIT, 0 },
1341 ['v'] = {chm_voice, 0, CHM_ARGS },
1342 ['z'] = {chm_simple, MODE_OPMODERATE, 0 },
212380e3
AC
1343};
1344
1345/* *INDENT-ON* */
1346
1347/* set_channel_mode()
1348 *
1349 * inputs - client, source, channel, membership pointer, params
55abcbb2 1350 * output -
212380e3
AC
1351 * side effects - channel modes/memberships are changed, MODE is issued
1352 *
1353 * Extensively modified to be hotpluggable, 03/09/06 -- nenolod
1354 */
1355void
1356set_channel_mode(struct Client *client_p, struct Client *source_p,
1357 struct Channel *chptr, struct membership *msptr, int parc, const char *parv[])
1358{
047b56e7 1359 static char modebuf[BUFSIZE * 2]; /* paranoid case: 2 canonical chars per input char */
212380e3
AC
1360 static char parabuf[BUFSIZE];
1361 char *mbuf;
1362 char *pbuf;
1363 int cur_len, mlen, paralen, paracount, arglen, len;
1364 int i, j, flags;
04952c32
EK
1365 int dir = MODE_ADD;
1366 int access_dir = MODE_QUERY;
212380e3
AC
1367 int parn = 1;
1368 int errors = 0;
1369 int alevel;
1370 const char *ml = parv[0];
1371 char c;
1372 struct Client *fakesource_p;
be29ec79 1373 int flags_list[3] = { ALL_MEMBERS, ONLY_CHANOPS, ONLY_OPERS };
212380e3
AC
1374
1375 mask_pos = 0;
765d839d 1376 removed_mask_pos = 0;
212380e3
AC
1377 mode_count = 0;
1378 mode_limit = 0;
84c9a8c7 1379 mode_limit_simple = 0;
212380e3 1380
212380e3
AC
1381 /* Hide connecting server on netburst -- jilles */
1382 if (ConfigServerHide.flatten_links && IsServer(source_p) && !has_id(source_p) && !HasSentEob(source_p))
1383 fakesource_p = &me;
1384 else
1385 fakesource_p = source_p;
1386
04952c32
EK
1387 struct modeset {
1388 const struct ChannelMode *cm;
1389 const char *arg;
1390 int dir;
1391 char mode;
1392 };
202d4966 1393
04952c32
EK
1394 static struct modeset modesets[MAXPARA];
1395 struct modeset *ms = modesets, *mend;
047b56e7
EK
1396 char canon_op = '\0';
1397
1398 mbuf = modebuf;
04952c32
EK
1399
1400 for (ml = parv[0]; *ml != 0; ml++)
212380e3 1401 {
04952c32 1402 c = *ml;
212380e3
AC
1403 switch (c)
1404 {
1405 case '+':
1406 dir = MODE_ADD;
1407 break;
1408 case '-':
1409 dir = MODE_DEL;
1410 break;
1411 case '=':
1412 dir = MODE_QUERY;
1413 break;
1414 default:
04952c32
EK
1415 {
1416 int effective_dir = dir;
1417 const struct ChannelMode *cm = &chmode_table[(unsigned char) c];
1418 bool use_arg = dir == MODE_ADD ? cm->flags & CHM_ARG_SET :
1419 dir == MODE_DEL ? cm->flags & CHM_ARG_DEL :
1420 false;
d295a398 1421 if (cm->set_func == NULL)
04952c32
EK
1422 {
1423 sendto_one(source_p, form_str(ERR_UNKNOWNMODE), me.name, source_p->name, c);
1424 return;
1425 }
1426 if (use_arg && parn >= parc)
1427 {
1428 if (!(cm->flags & CHM_CAN_QUERY))
1429 {
1430 sendto_one(source_p, form_str(ERR_NEEDMOREPARAMS), me.name, source_p->name, "MODE");
1431 return;
1432 }
1433 effective_dir = MODE_QUERY;
1434 use_arg = false;
1435 }
1436
1437 if (effective_dir == MODE_QUERY && !(cm->flags & CHM_CAN_QUERY))
1438 {
1439 /* XXX this currently replicates traditional behaviour and just
1440 * does nothing for a query on a mode with no query. would it be
1441 * good to send an error here?
1442 */
047b56e7 1443 continue;
04952c32
EK
1444 }
1445
047b56e7
EK
1446 char op = effective_dir == MODE_ADD ? '+' :
1447 effective_dir == MODE_DEL ? '-' :
1448 '=';
1449
1450 if (op != canon_op)
1451 *mbuf++ = canon_op = op;
1452
1453 *mbuf++ = c;
1454
04952c32
EK
1455 if (effective_dir != MODE_QUERY && access_dir == MODE_QUERY)
1456 access_dir = effective_dir;
ea41b24f
EK
1457 if (effective_dir == MODE_QUERY && cm->flags & CHM_OPS_QUERY)
1458 access_dir = MODE_OP_QUERY;
04952c32
EK
1459
1460 ms->cm = cm;
1461 ms->dir = effective_dir;
1462 if (use_arg)
1463 ms->arg = parv[parn++];
1464 else
1465 ms->arg = NULL;
1466 ms->mode = c;
1467 ms++;
1468 }
212380e3
AC
1469 }
1470 }
1471
047b56e7
EK
1472 /* this will happen on something like MODE +-=++-.
1473 * we'd have caught that with the if !mode_count
1474 * later on, but this saves an override notice
1475 */
1476 if (ms == modesets)
1477 return;
1478
04952c32
EK
1479 if (parn < parc)
1480 {
1481 /* XXX we could reject excess params here */
1482 }
1483
1484 mend = ms;
1485
047b56e7
EK
1486 if (parn > 1)
1487 {
1488 strcpy(mbuf, " ");
1489 rb_strlcat(modebuf, reconstruct_parv(parn - 1, parv + 1), sizeof modebuf);
1490 }
1491 else
1492 {
1493 *mbuf = '\0';
1494 }
1495 alevel = get_channel_access(source_p, chptr, msptr, access_dir, modebuf);
04952c32
EK
1496
1497 for (ms = modesets; ms < mend; ms++)
1498 {
92c6e47b 1499 ChannelModeFunc *set_func = ms->cm->set_func;
04952c32
EK
1500 set_func(fakesource_p, chptr, alevel, ms->arg, &errors, ms->dir, ms->mode, ms->cm->mode_type);
1501 }
1502
212380e3 1503 /* bail out if we have nothing to do... */
047b56e7 1504 if (!mode_count)
212380e3
AC
1505 return;
1506
047b56e7 1507 if (IsServer(source_p))
5203cba5 1508 mlen = sprintf(modebuf, ":%s MODE %s ", fakesource_p->name, chptr->chname);
212380e3 1509 else
5203cba5 1510 mlen = sprintf(modebuf, ":%s!%s@%s MODE %s ",
212380e3
AC
1511 source_p->name, source_p->username,
1512 source_p->host, chptr->chname);
1513
87c44482 1514 for(j = 0; j < 3; j++)
212380e3 1515 {
d3fd88a4 1516 int send_flags = flags = flags_list[j];
f5d60bb5 1517 const char *priv = NULL;
d3fd88a4
EK
1518 if (flags == ONLY_OPERS)
1519 {
1520 send_flags = ALL_MEMBERS;
1521 priv = "auspex:cmodes";
1522 }
212380e3
AC
1523 cur_len = mlen;
1524 mbuf = modebuf + mlen;
1525 pbuf = parabuf;
1526 parabuf[0] = '\0';
1527 paracount = paralen = 0;
1528 dir = MODE_QUERY;
1529
1530 for(i = 0; i < mode_count; i++)
1531 {
1532 if(mode_changes[i].letter == 0 || mode_changes[i].mems != flags)
1533 continue;
1534
1535 if(mode_changes[i].arg != NULL)
1536 {
1537 arglen = strlen(mode_changes[i].arg);
1538
1539 if(arglen > MODEBUFLEN - 5)
1540 continue;
1541 }
1542 else
1543 arglen = 0;
1544
1545 /* if we're creeping over MAXMODEPARAMSSERV, or over
1546 * bufsize (4 == +/-,modechar,two spaces) send now.
1547 */
1548 if(mode_changes[i].arg != NULL &&
1549 ((paracount == MAXMODEPARAMSSERV) ||
1550 ((cur_len + paralen + arglen + 4) > (BUFSIZE - 3))))
1551 {
1552 *mbuf = '\0';
1553
1554 if(cur_len > mlen)
d3fd88a4
EK
1555 sendto_channel_local_priv(IsServer(source_p) ? fakesource_p : source_p,
1556 send_flags, priv, chptr, "%s %s", modebuf, parabuf);
212380e3
AC
1557 else
1558 continue;
1559
1560 paracount = paralen = 0;
1561 cur_len = mlen;
1562 mbuf = modebuf + mlen;
1563 pbuf = parabuf;
1564 parabuf[0] = '\0';
1565 dir = MODE_QUERY;
1566 }
1567
1568 if(dir != mode_changes[i].dir)
1569 {
1570 *mbuf++ = (mode_changes[i].dir == MODE_ADD) ? '+' : '-';
1571 cur_len++;
1572 dir = mode_changes[i].dir;
1573 }
1574
1575 *mbuf++ = mode_changes[i].letter;
1576 cur_len++;
1577
1578 if(mode_changes[i].arg != NULL)
1579 {
1580 paracount++;
5203cba5 1581 len = sprintf(pbuf, "%s ", mode_changes[i].arg);
212380e3
AC
1582 pbuf += len;
1583 paralen += len;
1584 }
1585 }
1586
1587 if(paralen && parabuf[paralen - 1] == ' ')
1588 parabuf[paralen - 1] = '\0';
1589
1590 *mbuf = '\0';
1591 if(cur_len > mlen)
d3fd88a4
EK
1592 sendto_channel_local_priv(IsServer(source_p) ? fakesource_p : source_p,
1593 send_flags, priv, chptr, "%s %s", modebuf, parabuf);
212380e3
AC
1594 }
1595
1596 /* only propagate modes originating locally, or if we're hubbing */
330fc5c1 1597 if(MyClient(source_p) || rb_dlink_list_length(&serv_list) > 1)
212380e3
AC
1598 send_cap_mode_changes(client_p, source_p, chptr, mode_changes, mode_count);
1599}
8727cbe8
AC
1600
1601/* set_channel_mlock()
1602 *
1603 * inputs - client, source, channel, params
55abcbb2 1604 * output -
8727cbe8
AC
1605 * side effects - channel mlock is changed / MLOCK is propagated
1606 */
1607void
1608set_channel_mlock(struct Client *client_p, struct Client *source_p,
07554369 1609 struct Channel *chptr, const char *newmlock, bool propagate)
8727cbe8 1610{
78e6b731 1611 rb_free(chptr->mode_lock);
6b8db2da 1612 chptr->mode_lock = newmlock ? rb_strdup(newmlock) : NULL;
8727cbe8 1613
6fb6bd15
AC
1614 if (propagate)
1615 {
1616 sendto_server(client_p, NULL, CAP_TS6 | CAP_MLOCK, NOCAPS, ":%s MLOCK %ld %s :%s",
1617 source_p->id, (long) chptr->channelts, chptr->chname,
1618 chptr->mode_lock ? chptr->mode_lock : "");
1619 }
8727cbe8 1620}