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