]> jfr.im git - solanum.git/blob - ircd/chmode.c
valid_temp_time: style fixes
[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 #include "packet.h"
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
64 static struct ChModeChange mode_changes[BUFSIZE];
65 static int mode_count;
66 static int mask_pos;
67 static int removed_mask_pos;
68
69 char cflagsbuf[256];
70 char cflagsmyinfo[256];
71
72 int chmode_flags[256];
73
74 extern int h_get_channel_access;
75
76 /* OPTIMIZE ME! -- dwr */
77 void
78 construct_cflags_strings(void)
79 {
80 int i;
81 char *ptr = cflagsbuf;
82 char *ptr2 = cflagsmyinfo;
83
84 *ptr = '\0';
85 *ptr2 = '\0';
86
87 for(i = 0; i < 256; i++)
88 {
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)
96 {
97 chmode_flags[i] = chmode_table[i].mode_type;
98 }
99 else
100 {
101 chmode_flags[i] = 0;
102 }
103
104 switch (chmode_flags[i])
105 {
106 case MODE_FREETARGET:
107 case MODE_DISFORWARD:
108 if(ConfigChannel.use_forward)
109 *ptr++ = (char) i;
110 break;
111 default:
112 if(chmode_flags[i] != 0)
113 {
114 *ptr++ = (char) i;
115 }
116 }
117
118 /* Should we leave orphaned check here? -- dwr */
119 if (chmode_table[i].set_func != NULL &&
120 chmode_table[i].set_func != chm_orphaned)
121 {
122 *ptr2++ = (char) i;
123 }
124 }
125
126 *ptr++ = '\0';
127 *ptr2++ = '\0';
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 */
138 static unsigned int
139 find_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;
150 }
151
152 unsigned int
153 cflag_add(char c_, ChannelModeFunc function)
154 {
155 int c = (unsigned char)c_;
156
157 if (chmode_table[c].set_func != NULL &&
158 chmode_table[c].set_func != chm_orphaned)
159 return 0;
160
161 if (chmode_table[c].set_func == NULL)
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
170 void
171 cflag_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
180 int
181 get_channel_access(struct Client *source_p, struct Channel *chptr, struct membership *msptr, int dir, const char *modestr)
182 {
183 hook_data_channel_approval moduledata;
184
185 if(!MyClient(source_p))
186 return CHFL_CHANOP;
187
188 moduledata.client = source_p;
189 moduledata.chptr = chptr;
190 moduledata.msptr = msptr;
191 moduledata.target = NULL;
192 moduledata.approved = (msptr != NULL && is_chanop(msptr)) ? CHFL_CHANOP : CHFL_PEON;
193 moduledata.dir = dir;
194 moduledata.modestr = modestr;
195
196 call_hook(h_get_channel_access, &moduledata);
197
198 return moduledata.approved;
199 }
200
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
206 * outputs - false on failure, true on success
207 * side effects - error message sent on failure
208 */
209 static bool
210 allow_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;
224 return false;
225 }
226 if(alevel < CHFL_CHANOP)
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;
232 return false;
233 }
234 return true;
235 }
236
237 /* add_id()
238 *
239 * inputs - client, channel, id to add, type, forward
240 * outputs - false on failure, true on success
241 * side effects - given id is added to the appropriate list
242 */
243 bool
244 add_id(struct Client *source_p, struct Channel *chptr, const char *banid, const char *forward,
245 rb_dlink_list * list, long mode_type)
246 {
247 struct Ban *actualBan;
248 static char who[USERHOST_REPLYLEN];
249 char *realban = LOCAL_COPY(banid);
250 rb_dlink_node *ptr;
251
252 /* dont let local clients overflow the banlist, or set redundant
253 * bans
254 */
255 if(MyClient(source_p))
256 {
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))
258 {
259 sendto_one(source_p, form_str(ERR_BANLISTFULL),
260 me.name, source_p->name, chptr->chname, realban);
261 return false;
262 }
263
264 RB_DLINK_FOREACH(ptr, list->head)
265 {
266 actualBan = ptr->data;
267 if(mask_match(actualBan->banstr, realban))
268 return false;
269 }
270 }
271 /* dont let remotes set duplicates */
272 else
273 {
274 RB_DLINK_FOREACH(ptr, list->head)
275 {
276 actualBan = ptr->data;
277 if(!irccmp(actualBan->banstr, realban))
278 return false;
279 }
280 }
281
282
283 if(IsPerson(source_p))
284 sprintf(who, "%s!%s@%s", source_p->name, source_p->username, source_p->host);
285 else
286 rb_strlcpy(who, source_p->name, sizeof(who));
287
288 actualBan = allocate_ban(realban, who, forward);
289 actualBan->when = rb_current_time();
290
291 rb_dlinkAdd(actualBan, &actualBan->node, list);
292
293 /* invalidate the can_send() cache */
294 if(mode_type == CHFL_BAN || mode_type == CHFL_QUIET || mode_type == CHFL_EXCEPTION)
295 chptr->bants = rb_current_time();
296
297 return true;
298 }
299
300 /* del_id()
301 *
302 * inputs - channel, id to remove, type
303 * outputs - pointer to ban that was removed, if any
304 * side effects - given id is removed from the appropriate list and returned
305 */
306 struct Ban *
307 del_id(struct Channel *chptr, const char *banid, rb_dlink_list * list, long mode_type)
308 {
309 rb_dlink_node *ptr;
310 struct Ban *banptr;
311
312 if(EmptyString(banid))
313 return NULL;
314
315 RB_DLINK_FOREACH(ptr, list->head)
316 {
317 banptr = ptr->data;
318
319 if(irccmp(banid, banptr->banstr) == 0)
320 {
321 rb_dlinkDelete(&banptr->node, list);
322
323 /* invalidate the can_send() cache */
324 if(mode_type == CHFL_BAN || mode_type == CHFL_QUIET || mode_type == CHFL_EXCEPTION)
325 chptr->bants = rb_current_time();
326
327 return banptr;
328 }
329 }
330
331 return NULL;
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 */
340 static char *
341 check_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 */
370 static char *
371 pretty_mask(const char *idmask)
372 {
373 static char mask_buf[BUFSIZE];
374 int old_mask_pos;
375 const char *nick, *user, *host, *forward = NULL;
376 char *t, *at, *ex;
377 int nl, ul, hl, fl;
378 char *mask;
379 size_t masklen;
380
381 mask = LOCAL_COPY(idmask);
382 mask = check_string(mask);
383 collapse(mask);
384 masklen = strlen(mask);
385
386 nick = user = host = "*";
387 nl = ul = hl = 1;
388 fl = 0;
389
390 if((size_t) BUFSIZE - mask_pos < masklen + 5)
391 return NULL;
392
393 old_mask_pos = mask_pos;
394
395 if (*mask == '$')
396 {
397 memcpy(mask_buf + mask_pos, mask, masklen + 1);
398 mask_pos += masklen + 1;
399 t = mask_buf + old_mask_pos + 1;
400 if (*t == '!')
401 *t = '~';
402 if (*t == '~')
403 t++;
404 *t = irctolower(*t);
405 return mask_buf + old_mask_pos;
406 }
407
408 at = ex = NULL;
409 if((t = memchr(mask, '@', masklen)) != NULL)
410 {
411 at = t;
412 t++;
413 if(*t != '\0')
414 host = t, hl = strlen(t);
415
416 if((t = memchr(mask, '!', at - mask)) != NULL)
417 {
418 ex = t;
419 t++;
420 if(at != t)
421 user = t, ul = at - t;
422 if(ex != mask)
423 nick = mask, nl = ex - mask;
424 }
425 else
426 {
427 if(at != mask)
428 user = mask, ul = at - mask;
429 }
430
431 if((t = memchr(host, '!', hl)) != NULL ||
432 (t = memchr(host, '$', hl)) != NULL)
433 {
434 t++;
435 if (host + hl != t)
436 forward = t, fl = host + hl - t;
437 hl = t - 1 - host;
438 }
439 }
440 else if((t = memchr(mask, '!', masklen)) != NULL)
441 {
442 ex = t;
443 t++;
444 if(ex != mask)
445 nick = mask, nl = ex - mask;
446 if(*t != '\0')
447 user = t, ul = strlen(t);
448 }
449 else if(memchr(mask, '.', masklen) != NULL ||
450 memchr(mask, ':', masklen) != NULL)
451 {
452 host = mask, hl = masklen;
453 }
454 else
455 {
456 if(masklen > 0)
457 nick = mask, nl = masklen;
458 }
459
460 /* truncate values to max lengths */
461 if(nl > NICKLEN - 1)
462 nl = NICKLEN - 1;
463 if(ul > USERLEN)
464 ul = USERLEN;
465 if(hl > HOSTLEN)
466 hl = HOSTLEN;
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;
478 }
479 mask_buf[mask_pos++] = '\0';
480
481 return mask_buf + old_mask_pos;
482 }
483
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 */
490 static bool
491 check_forward(struct Client *source_p, struct Channel *chptr,
492 const char *forward)
493 {
494 struct Channel *targptr = NULL;
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);
501 return false;
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);
508 return false;
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);
514 return false;
515 }
516 if(MyClient(source_p) && !(targptr->mode.mode & MODE_FREETARGET))
517 {
518 if((msptr = find_channel_membership(targptr, source_p)) == NULL ||
519 get_channel_access(source_p, targptr, msptr, MODE_QUERY, NULL) < CHFL_CHANOP)
520 {
521 sendto_one(source_p, form_str(ERR_CHANOPRIVSNEEDED),
522 me.name, source_p->name, targptr->chname);
523 return false;
524 }
525 }
526 return true;
527 }
528
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 */
536 static char *
537 fix_key(char *arg)
538 {
539 unsigned char *s, *t, c;
540
541 for(s = t = (unsigned char *) arg; (c = *s); s++)
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 */
559 static char *
560 fix_key_remote(char *arg)
561 {
562 unsigned char *s, *t, c;
563
564 for(s = t = (unsigned char *) arg; (c = *s); s++)
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
575 void
576 chm_simple(struct Client *source_p, struct Channel *chptr,
577 int alevel, const char *arg, int *errors, int dir, char c, long mode_type)
578 {
579 if(!allow_mode_change(source_p, chptr, alevel, errors, c))
580 return;
581
582 /* setting + */
583 if((dir == MODE_ADD) && !(chptr->mode.mode & mode_type))
584 {
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
590 chptr->mode.mode |= mode_type;
591
592 mode_changes[mode_count].letter = c;
593 mode_changes[mode_count].dir = MODE_ADD;
594 mode_changes[mode_count].id = NULL;
595 mode_changes[mode_count].mems = ALL_MEMBERS;
596 mode_changes[mode_count++].arg = NULL;
597 }
598 else if((dir == MODE_DEL) && (chptr->mode.mode & mode_type))
599 {
600 chptr->mode.mode &= ~mode_type;
601
602 mode_changes[mode_count].letter = c;
603 mode_changes[mode_count].dir = MODE_DEL;
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
610 void
611 chm_orphaned(struct Client *source_p, struct Channel *chptr,
612 int alevel, const char *arg, int *errors, int dir, char c, long mode_type)
613 {
614 if(MyClient(source_p))
615 return;
616
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;
623 mode_changes[mode_count].id = NULL;
624 mode_changes[mode_count].mems = ALL_MEMBERS;
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;
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
639 void
640 chm_hidden(struct Client *source_p, struct Channel *chptr,
641 int alevel, const char *arg, int *errors, int dir, char c, long mode_type)
642 {
643 if(MyClient(source_p) && !IsOperGeneral(source_p))
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
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;
666 mode_changes[mode_count].id = NULL;
667 mode_changes[mode_count].mems = ONLY_OPERS;
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;
676 mode_changes[mode_count].mems = ONLY_OPERS;
677 mode_changes[mode_count].id = NULL;
678 mode_changes[mode_count++].arg = NULL;
679 }
680 }
681
682 void
683 chm_staff(struct Client *source_p, struct Channel *chptr,
684 int alevel, const char *arg, int *errors, int dir, char c, long mode_type)
685 {
686 if(MyClient(source_p) && !IsOper(source_p))
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 }
693 if(MyClient(source_p) && !HasPrivilege(source_p, "oper:cmodes"))
694 {
695 if(!(*errors & SM_ERR_NOPRIVS))
696 sendto_one(source_p, form_str(ERR_NOPRIVS), me.name,
697 source_p->name, "cmodes");
698 *errors |= SM_ERR_NOPRIVS;
699 return;
700 }
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;
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;
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
725 void
726 chm_ban(struct Client *source_p, struct Channel *chptr,
727 int alevel, const char *arg, int *errors, int dir, char c, long mode_type)
728 {
729 const char *mask;
730 char *forward;
731 rb_dlink_list *list;
732 rb_dlink_node *ptr;
733 struct Ban *banptr;
734 int errorval;
735 const char *rpl_list_p;
736 const char *rpl_endlist_p;
737 int mems;
738
739 switch (mode_type)
740 {
741 case CHFL_BAN:
742 list = &chptr->banlist;
743 errorval = SM_ERR_RPL_B;
744 rpl_list_p = form_str(RPL_BANLIST);
745 rpl_endlist_p = form_str(RPL_ENDOFBANLIST);
746 mems = ALL_MEMBERS;
747 break;
748
749 case CHFL_EXCEPTION:
750 /* if +e is disabled, allow all but +e locally */
751 if (!ConfigChannel.use_except && MyClient(source_p) && dir == MODE_ADD)
752 return;
753
754 list = &chptr->exceptlist;
755 errorval = SM_ERR_RPL_E;
756 rpl_list_p = form_str(RPL_EXCEPTLIST);
757 rpl_endlist_p = form_str(RPL_ENDOFEXCEPTLIST);
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 */
767 if (!ConfigChannel.use_invex && MyClient(source_p) && dir == MODE_ADD)
768 return;
769
770 list = &chptr->invexlist;
771 errorval = SM_ERR_RPL_I;
772 rpl_list_p = form_str(RPL_INVITELIST);
773 rpl_endlist_p = form_str(RPL_ENDOFINVITELIST);
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;
784 rpl_list_p = form_str(RPL_QUIETLIST);
785 rpl_endlist_p = form_str(RPL_ENDOFQUIETLIST);
786 mems = ALL_MEMBERS;
787 break;
788
789 default:
790 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, "chm_ban() called with unknown type!");
791 return;
792 }
793
794 if (dir == MODE_QUERY)
795 {
796 if((*errors & errorval) != 0)
797 return;
798 *errors |= errorval;
799
800 /* non-ops cant see +eI lists.. */
801 /* note that this is still permitted if +e/+I are mlocked. */
802 if(alevel < CHFL_CHANOP && mode_type != CHFL_BAN &&
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
812 RB_DLINK_FOREACH(ptr, list->head)
813 {
814 char buf[BANLEN];
815 banptr = ptr->data;
816 if(banptr->forward)
817 snprintf(buf, sizeof(buf), "%s$%s", banptr->banstr, banptr->forward);
818 else
819 rb_strlcpy(buf, banptr->banstr, sizeof(buf));
820
821 sendto_one(source_p, rpl_list_p,
822 me.name, source_p->name, chptr->chname,
823 buf, banptr->who, banptr->when);
824 }
825 sendto_one(source_p, rpl_endlist_p, me.name, source_p->name, chptr->chname);
826 return;
827 }
828
829 if (!allow_mode_change(source_p, chptr, alevel, errors, c))
830 return;
831
832 /* empty ban, or starts with ':' which messes up s2s, ignore it */
833 if (EmptyString(arg) || *arg == ':')
834 return;
835
836 if (!MyClient(source_p))
837 {
838 if (strchr(arg, ' '))
839 return;
840
841 mask = arg;
842 }
843 else
844 mask = pretty_mask(arg);
845
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 */
850 if(strlen(mask) > MIN(BANLEN, MODEBUFLEN - 5))
851 {
852 sendto_one_numeric(source_p, ERR_INVALIDBAN,
853 form_str(ERR_INVALIDBAN),
854 chptr->chname, c, arg);
855 return;
856 }
857
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
869 /* if we're adding a NEW id */
870 if (dir == MODE_ADD)
871 {
872 if (*mask == '$' && MyClient(source_p))
873 {
874 if (!valid_extban(mask, source_p, chptr, mode_type))
875 {
876 sendto_one_numeric(source_p, ERR_INVALIDBAN,
877 form_str(ERR_INVALIDBAN),
878 chptr->chname, c, arg);
879 return;
880 }
881 }
882
883 /* For compatibility, only check the forward channel from
884 * local clients. Accept any forward channel from servers.
885 */
886 if (forward != NULL && MyClient(source_p))
887 {
888 /* For simplicity and future flexibility, do not
889 * allow '$' in forwarding targets.
890 */
891 if (!ConfigChannel.use_forward ||
892 strchr(forward, '$') != NULL)
893 {
894 sendto_one_numeric(source_p, ERR_INVALIDBAN,
895 form_str(ERR_INVALIDBAN),
896 chptr->chname, c, arg);
897 return;
898 }
899 /* check_forward() sends its own error message */
900 if (!check_forward(source_p, chptr, forward))
901 return;
902 /* Forwards only make sense for bans. */
903 if (mode_type != CHFL_BAN)
904 {
905 sendto_one_numeric(source_p, ERR_INVALIDBAN,
906 form_str(ERR_INVALIDBAN),
907 chptr->chname, c, arg);
908 return;
909 }
910 }
911
912 /* dont allow local clients to overflow the banlist, dont
913 * let remote servers set duplicate bans
914 */
915 if (!add_id(source_p, chptr, mask, forward, list, mode_type))
916 return;
917
918 if (forward)
919 forward[-1]= '$';
920
921 mode_changes[mode_count].letter = c;
922 mode_changes[mode_count].dir = MODE_ADD;
923 mode_changes[mode_count].mems = mems;
924 mode_changes[mode_count].id = NULL;
925 mode_changes[mode_count++].arg = mask;
926 }
927 else if (dir == MODE_DEL)
928 {
929 struct Ban *removed;
930 static char buf[BANLEN * MAXMODEPARAMS];
931 int old_removed_mask_pos = removed_mask_pos;
932 if ((removed = del_id(chptr, mask, list, mode_type)) == NULL)
933 {
934 /* mask isn't a valid ban, check arg */
935 if ((removed = del_id(chptr, arg, list, mode_type)) != NULL)
936 mask = arg;
937 }
938
939 if (removed && removed->forward)
940 removed_mask_pos += snprintf(buf + old_removed_mask_pos, sizeof(buf), "%s$%s", removed->banstr, removed->forward) + 1;
941 else
942 removed_mask_pos += rb_strlcpy(buf + old_removed_mask_pos, mask, sizeof(buf)) + 1;
943 if (removed)
944 {
945 free_ban(removed);
946 removed = NULL;
947 }
948
949 mode_changes[mode_count].letter = c;
950 mode_changes[mode_count].dir = MODE_DEL;
951 mode_changes[mode_count].mems = mems;
952 mode_changes[mode_count].id = NULL;
953 mode_changes[mode_count++].arg = buf + old_removed_mask_pos;
954 }
955 }
956
957 void
958 chm_op(struct Client *source_p, struct Channel *chptr,
959 int alevel, const char *arg, int *errors, int dir, char c, long mode_type)
960 {
961 struct membership *mstptr;
962 struct Client *targ_p;
963
964 if(!allow_mode_change(source_p, chptr, alevel, errors, c))
965 return;
966
967 /* empty nick */
968 if(EmptyString(arg))
969 {
970 sendto_one_numeric(source_p, ERR_NOSUCHNICK, form_str(ERR_NOSUCHNICK), "*");
971 return;
972 }
973
974 if((targ_p = find_chasing(source_p, arg, NULL)) == NULL)
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,
985 form_str(ERR_USERNOTINCHANNEL), arg, chptr->chname);
986 *errors |= SM_ERR_NOTONCHANNEL;
987 return;
988 }
989
990 if(dir == MODE_ADD)
991 {
992 if(targ_p == source_p && mstptr->flags & CHFL_CHANOP)
993 return;
994
995 mode_changes[mode_count].letter = c;
996 mode_changes[mode_count].dir = MODE_ADD;
997 mode_changes[mode_count].mems = ALL_MEMBERS;
998 mode_changes[mode_count].id = targ_p->id;
999 mode_changes[mode_count++].arg = targ_p->name;
1000
1001 mstptr->flags |= CHFL_CHANOP;
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;
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 }
1021
1022 void
1023 chm_voice(struct Client *source_p, struct Channel *chptr,
1024 int alevel, const char *arg, int *errors, int dir, char c, long mode_type)
1025 {
1026 struct membership *mstptr;
1027 struct Client *targ_p;
1028
1029 if(!allow_mode_change(source_p, chptr, alevel, errors, c))
1030 return;
1031
1032 /* empty nick */
1033 if(EmptyString(arg))
1034 {
1035 sendto_one_numeric(source_p, ERR_NOSUCHNICK, form_str(ERR_NOSUCHNICK), "*");
1036 return;
1037 }
1038
1039 if((targ_p = find_chasing(source_p, arg, NULL)) == NULL)
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,
1050 form_str(ERR_USERNOTINCHANNEL), arg, chptr->chname);
1051 *errors |= SM_ERR_NOTONCHANNEL;
1052 return;
1053 }
1054
1055 if(dir == MODE_ADD)
1056 {
1057 mode_changes[mode_count].letter = c;
1058 mode_changes[mode_count].dir = MODE_ADD;
1059 mode_changes[mode_count].mems = ALL_MEMBERS;
1060 mode_changes[mode_count].id = targ_p->id;
1061 mode_changes[mode_count++].arg = targ_p->name;
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;
1069 mode_changes[mode_count].mems = ALL_MEMBERS;
1070 mode_changes[mode_count].id = targ_p->id;
1071 mode_changes[mode_count++].arg = targ_p->name;
1072
1073 mstptr->flags &= ~CHFL_VOICE;
1074 }
1075 }
1076
1077 void
1078 chm_limit(struct Client *source_p, struct Channel *chptr,
1079 int alevel, const char *arg, int *errors, int dir, char c, long mode_type)
1080 {
1081 static char limitstr[30];
1082 int limit;
1083
1084 if (!allow_mode_change(source_p, chptr, alevel, errors, c))
1085 return;
1086
1087 if (dir == MODE_ADD)
1088 {
1089 if (EmptyString(arg) || (limit = atoi(arg)) <= 0)
1090 return;
1091
1092 sprintf(limitstr, "%d", limit);
1093
1094 mode_changes[mode_count].letter = c;
1095 mode_changes[mode_count].dir = MODE_ADD;
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 }
1102 else if (dir == MODE_DEL)
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;
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
1117 void
1118 chm_throttle(struct Client *source_p, struct Channel *chptr,
1119 int alevel, const char *arg, int *errors, int dir, char c, long mode_type)
1120 {
1121 int joins = 0, timeslice = 0;
1122
1123 if (!allow_mode_change(source_p, chptr, alevel, errors, c))
1124 return;
1125
1126 if (dir == MODE_ADD)
1127 {
1128 if (sscanf(arg, "%d:%d", &joins, &timeslice) < 2)
1129 return;
1130
1131 if(joins <= 0 || timeslice <= 0)
1132 return;
1133
1134 mode_changes[mode_count].letter = c;
1135 mode_changes[mode_count].dir = MODE_ADD;
1136 mode_changes[mode_count].mems = ALL_MEMBERS;
1137 mode_changes[mode_count].id = NULL;
1138 mode_changes[mode_count++].arg = arg;
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;
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
1161 void
1162 chm_forward(struct Client *source_p, struct Channel *chptr,
1163 int alevel, const char *arg, int *errors, int dir, char c, long mode_type)
1164 {
1165 /* if +f is disabled, ignore local attempts to set it */
1166 if (!ConfigChannel.use_forward && MyClient(source_p) && dir == MODE_ADD)
1167 return;
1168
1169 if (dir == MODE_QUERY)
1170 {
1171 if (!(*errors & SM_ERR_RPL_F))
1172 {
1173 if (*chptr->mode.forward == '\0')
1174 sendto_one_notice(source_p, ":%s has no forward channel", chptr->chname);
1175 else
1176 sendto_one_notice(source_p, ":%s forward channel is %s", chptr->chname, chptr->mode.forward);
1177 *errors |= SM_ERR_RPL_F;
1178 }
1179 return;
1180 }
1181
1182 #ifndef FORWARD_OPERONLY
1183 if (!allow_mode_change(source_p, chptr, alevel, errors, c))
1184 return;
1185 #else
1186 if (!IsOperGeneral(source_p) && !IsServer(source_p))
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
1195 if (dir == MODE_ADD)
1196 {
1197 if(EmptyString(arg))
1198 return;
1199
1200 if(!check_forward(source_p, chptr, arg))
1201 return;
1202
1203 rb_strlcpy(chptr->mode.forward, arg, sizeof(chptr->mode.forward));
1204
1205 mode_changes[mode_count].letter = c;
1206 mode_changes[mode_count].dir = MODE_ADD;
1207 mode_changes[mode_count].mems =
1208 ConfigChannel.use_forward ? ALL_MEMBERS : ONLY_SERVERS;
1209 mode_changes[mode_count].id = NULL;
1210 mode_changes[mode_count++].arg = arg;
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;
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
1227 void
1228 chm_key(struct Client *source_p, struct Channel *chptr,
1229 int alevel, const char *arg, int *errors, int dir, char c, long mode_type)
1230 {
1231 char *key;
1232
1233 if (!allow_mode_change(source_p, chptr, alevel, errors, c))
1234 return;
1235
1236 if (dir == MODE_ADD)
1237 {
1238 key = LOCAL_COPY(arg);
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] != ' ');
1249 rb_strlcpy(chptr->mode.key, key, sizeof(chptr->mode.key));
1250
1251 mode_changes[mode_count].letter = c;
1252 mode_changes[mode_count].dir = MODE_ADD;
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
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;
1279 mode_changes[mode_count].mems = ALL_MEMBERS;
1280 mode_changes[mode_count].id = NULL;
1281 mode_changes[mode_count++].arg = "*";
1282 }
1283 }
1284
1285 /* *INDENT-OFF* */
1286 struct ChannelMode chmode_table[256] =
1287 {
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 },
1311 };
1312
1313 /* *INDENT-ON* */
1314
1315 /* set_channel_mode()
1316 *
1317 * inputs - client, source, channel, membership pointer, params
1318 * output -
1319 * side effects - channel modes/memberships are changed, MODE is issued
1320 *
1321 * Extensively modified to be hotpluggable, 03/09/06 -- nenolod
1322 */
1323 void
1324 set_channel_mode(struct Client *client_p, struct Client *source_p,
1325 struct Channel *chptr, struct membership *msptr, int parc, const char *parv[])
1326 {
1327 static char modebuf[BUFSIZE * 2]; /* paranoid case: 2 canonical chars per input char */
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;
1333 int dir = MODE_ADD;
1334 bool changes = false;
1335 bool privileged_query = false;
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;
1342 int flags_list[3] = { ALL_MEMBERS, ONLY_CHANOPS, ONLY_OPERS };
1343 int mode_limit = 0;
1344 int mode_limit_simple = 0;
1345
1346 mask_pos = 0;
1347 removed_mask_pos = 0;
1348 mode_count = 0;
1349
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
1356 struct modeset {
1357 const struct ChannelMode *cm;
1358 const char *arg;
1359 int dir;
1360 char mode;
1361 };
1362
1363 static struct modeset modesets[MAXMODEPARAMS + MAXMODES_SIMPLE];
1364 struct modeset *ms = modesets, *mend;
1365 char canon_op = '\0';
1366
1367 mbuf = modebuf;
1368
1369 for (ml = parv[0]; *ml != 0 && ms - modesets < ARRAY_SIZE(modesets); ml++)
1370 {
1371 c = *ml;
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:
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;
1390 if (cm->set_func == NULL)
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 */
1412 continue;
1413 }
1414
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
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
1432 if (effective_dir != MODE_QUERY)
1433 changes = true;
1434 if (effective_dir == MODE_QUERY && cm->flags & CHM_OPS_QUERY)
1435 privileged_query = true;
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 }
1446 }
1447 }
1448
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
1456 if (parn < parc)
1457 {
1458 /* XXX we could reject excess params here */
1459 }
1460
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
1467 mend = ms;
1468
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 }
1478 int access_dir = privileged_query ? MODE_OP_QUERY :
1479 changes ? MODE_ADD :
1480 MODE_QUERY;
1481 alevel = get_channel_access(source_p, chptr, msptr, access_dir, modebuf);
1482
1483 for (ms = modesets; ms < mend; ms++)
1484 {
1485 ChannelModeFunc *set_func = ms->cm->set_func;
1486 set_func(fakesource_p, chptr, alevel, ms->arg, &errors, ms->dir, ms->mode, ms->cm->mode_type);
1487 }
1488
1489 /* bail out if we have nothing to do... */
1490 if (!mode_count)
1491 return;
1492
1493 if (IsServer(source_p))
1494 mlen = sprintf(modebuf, ":%s MODE %s ", fakesource_p->name, chptr->chname);
1495 else
1496 mlen = sprintf(modebuf, ":%s!%s@%s MODE %s ",
1497 source_p->name, source_p->username,
1498 source_p->host, chptr->chname);
1499
1500 for(j = 0; j < 3; j++)
1501 {
1502 int send_flags = flags = flags_list[j];
1503 const char *priv = NULL;
1504 if (flags == ONLY_OPERS)
1505 {
1506 send_flags = ALL_MEMBERS;
1507 priv = "auspex:cmodes";
1508 }
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)
1541 sendto_channel_local_priv(IsServer(source_p) ? fakesource_p : source_p,
1542 send_flags, priv, chptr, "%s %s", modebuf, parabuf);
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++;
1567 len = sprintf(pbuf, "%s ", mode_changes[i].arg);
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)
1578 sendto_channel_local_priv(IsServer(source_p) ? fakesource_p : source_p,
1579 send_flags, priv, chptr, "%s %s", modebuf, parabuf);
1580 }
1581
1582 /* only propagate modes originating locally, or if we're hubbing */
1583 if(MyClient(source_p) || rb_dlink_list_length(&serv_list) > 1)
1584 send_cap_mode_changes(client_p, source_p, chptr, mode_changes, mode_count);
1585 }
1586
1587 /* set_channel_mlock()
1588 *
1589 * inputs - client, source, channel, params
1590 * output -
1591 * side effects - channel mlock is changed / MLOCK is propagated
1592 */
1593 void
1594 set_channel_mlock(struct Client *client_p, struct Client *source_p,
1595 struct Channel *chptr, const char *newmlock, bool propagate)
1596 {
1597 rb_free(chptr->mode_lock);
1598 chptr->mode_lock = newmlock ? rb_strdup(newmlock) : NULL;
1599
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 }
1606 }