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