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