2 * Solanum: a slightly advanced ircd
3 * chmode.c: channel mode management
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
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.
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.
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
34 #include "s_serv.h" /* captab */
38 #include "s_conf.h" /* ConfigFileEntry, ConfigChannel */
39 #include "s_newconf.h"
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
61 #define MAXMODES_SIMPLE 46 /* a-zA-Z except bqeIov */
63 static struct ChModeChange mode_changes
[BUFSIZE
];
64 static int mode_count
;
65 static int mode_limit
;
66 static int mode_limit_simple
;
68 static int removed_mask_pos
;
71 char cflagsmyinfo
[256];
73 int chmode_flags
[256];
75 extern int h_get_channel_access
;
77 /* OPTIMIZE ME! -- dwr */
79 construct_cflags_strings(void)
82 char *ptr
= cflagsbuf
;
83 char *ptr2
= cflagsmyinfo
;
88 for(i
= 0; i
< 256; i
++)
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
)
98 chmode_flags
[i
] = chmode_table
[i
].mode_type
;
105 switch (chmode_flags
[i
])
107 case MODE_FREETARGET
:
108 case MODE_DISFORWARD
:
109 if(ConfigChannel
.use_forward
)
113 if(chmode_flags
[i
] != 0)
119 /* Should we leave orphaned check here? -- dwr */
120 if (chmode_table
[i
].set_func
!= NULL
&&
121 chmode_table
[i
].set_func
!= chm_orphaned
)
135 * outputs - an available cflag bitmask or
136 * 0 if no cflags are available
137 * side effects - NONE
140 find_cflag_slot(void)
142 unsigned int all_cflags
= 0, my_cflag
= 0, i
;
144 for (i
= 0; i
< 256; i
++)
145 all_cflags
|= chmode_flags
[i
];
147 for (my_cflag
= 1; my_cflag
&& (all_cflags
& my_cflag
);
154 cflag_add(char c_
, ChannelModeFunc function
)
156 int c
= (unsigned char)c_
;
158 if (chmode_table
[c
].set_func
!= NULL
&&
159 chmode_table
[c
].set_func
!= chm_orphaned
)
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)
166 chmode_table
[c
].set_func
= function
;
167 construct_cflags_strings();
168 return chmode_table
[c
].mode_type
;
172 cflag_orphan(char c_
)
174 int c
= (unsigned char)c_
;
176 s_assert(chmode_flags
[c
] != 0);
177 chmode_table
[c
].set_func
= chm_orphaned
;
178 construct_cflags_strings();
182 get_channel_access(struct Client
*source_p
, struct Channel
*chptr
, struct membership
*msptr
, int dir
, const char *modestr
)
184 hook_data_channel_approval moduledata
;
186 if(!MyClient(source_p
))
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
;
197 call_hook(h_get_channel_access
, &moduledata
);
199 return moduledata
.approved
;
202 /* allow_mode_change()
204 * Checks if mlock and chanops permit a mode change.
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
211 allow_mode_change(struct Client
*source_p
, struct Channel
*chptr
, int alevel
,
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
))
217 if (!(*errors
& SM_ERR_MLOCK
))
218 sendto_one_numeric(source_p
,
220 form_str(ERR_MLOCKRESTRICTED
),
224 *errors
|= SM_ERR_MLOCK
;
227 if(alevel
< CHFL_CHANOP
)
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
;
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
245 add_id(struct Client
*source_p
, struct Channel
*chptr
, const char *banid
, const char *forward
,
246 rb_dlink_list
* list
, long mode_type
)
248 struct Ban
*actualBan
;
249 static char who
[USERHOST_REPLYLEN
];
250 char *realban
= LOCAL_COPY(banid
);
253 /* dont let local clients overflow the banlist, or set redundant
256 if(MyClient(source_p
))
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
))
260 sendto_one(source_p
, form_str(ERR_BANLISTFULL
),
261 me
.name
, source_p
->name
, chptr
->chname
, realban
);
265 RB_DLINK_FOREACH(ptr
, list
->head
)
267 actualBan
= ptr
->data
;
268 if(mask_match(actualBan
->banstr
, realban
))
272 /* dont let remotes set duplicates */
275 RB_DLINK_FOREACH(ptr
, list
->head
)
277 actualBan
= ptr
->data
;
278 if(!irccmp(actualBan
->banstr
, realban
))
284 if(IsPerson(source_p
))
285 sprintf(who
, "%s!%s@%s", source_p
->name
, source_p
->username
, source_p
->host
);
287 rb_strlcpy(who
, source_p
->name
, sizeof(who
));
289 actualBan
= allocate_ban(realban
, who
, forward
);
290 actualBan
->when
= rb_current_time();
292 rb_dlinkAdd(actualBan
, &actualBan
->node
, list
);
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();
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
308 del_id(struct Channel
*chptr
, const char *banid
, rb_dlink_list
* list
, long mode_type
)
313 if(EmptyString(banid
))
316 RB_DLINK_FOREACH(ptr
, list
->head
)
320 if(irccmp(banid
, banptr
->banstr
) == 0)
322 rb_dlinkDelete(&banptr
->node
, list
);
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();
337 * input - string to check
338 * output - pointer to 'fixed' string, or "*" if empty
339 * side effects - any white space found becomes \0
342 check_string(char *s
)
345 static char splat
[] = "*";
362 * inputs - mask to pretty
363 * outputs - better version of the mask
364 * side effects - mask is chopped to limits, and transformed:
372 pretty_mask(const char *idmask
)
374 static char mask_buf
[BUFSIZE
];
376 const char *nick
, *user
, *host
, *forward
= NULL
;
382 mask
= LOCAL_COPY(idmask
);
383 mask
= check_string(mask
);
385 masklen
= strlen(mask
);
387 nick
= user
= host
= "*";
391 if((size_t) BUFSIZE
- mask_pos
< masklen
+ 5)
394 old_mask_pos
= mask_pos
;
398 memcpy(mask_buf
+ mask_pos
, mask
, masklen
+ 1);
399 mask_pos
+= masklen
+ 1;
400 t
= mask_buf
+ old_mask_pos
+ 1;
406 return mask_buf
+ old_mask_pos
;
410 if((t
= memchr(mask
, '@', masklen
)) != NULL
)
415 host
= t
, hl
= strlen(t
);
417 if((t
= memchr(mask
, '!', at
- mask
)) != NULL
)
422 user
= t
, ul
= at
- t
;
424 nick
= mask
, nl
= ex
- mask
;
429 user
= mask
, ul
= at
- mask
;
432 if((t
= memchr(host
, '!', hl
)) != NULL
||
433 (t
= memchr(host
, '$', hl
)) != NULL
)
437 forward
= t
, fl
= host
+ hl
- t
;
441 else if((t
= memchr(mask
, '!', masklen
)) != NULL
)
446 nick
= mask
, nl
= ex
- mask
;
448 user
= t
, ul
= strlen(t
);
450 else if(memchr(mask
, '.', masklen
) != NULL
||
451 memchr(mask
, ':', masklen
) != NULL
)
453 host
= mask
, hl
= masklen
;
458 nick
= mask
, nl
= masklen
;
461 /* truncate values to max lengths */
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
;
477 mask_buf
[mask_pos
++] = '$';
478 memcpy(mask_buf
+ mask_pos
, forward
, fl
), mask_pos
+= fl
;
480 mask_buf
[mask_pos
++] = '\0';
482 return mask_buf
+ old_mask_pos
;
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
492 check_forward(struct Client
*source_p
, struct Channel
*chptr
,
495 struct Channel
*targptr
= NULL
;
496 struct membership
*msptr
;
498 if(!check_channel_name(forward
) ||
499 (MyClient(source_p
) && (strlen(forward
) > LOC_CHANNELLEN
|| hash_find_resv(forward
))))
501 sendto_one_numeric(source_p
, ERR_BADCHANNAME
, form_str(ERR_BADCHANNAME
), forward
);
504 /* don't forward to inconsistent target -- jilles */
505 if(chptr
->chname
[0] == '#' && forward
[0] == '&')
507 sendto_one_numeric(source_p
, ERR_BADCHANNAME
,
508 form_str(ERR_BADCHANNAME
), forward
);
511 if(MyClient(source_p
) && (targptr
= find_channel(forward
)) == NULL
)
513 sendto_one_numeric(source_p
, ERR_NOSUCHCHANNEL
,
514 form_str(ERR_NOSUCHCHANNEL
), forward
);
517 if(MyClient(source_p
) && !(targptr
->mode
.mode
& MODE_FREETARGET
))
519 if((msptr
= find_channel_membership(targptr
, source_p
)) == NULL
||
520 get_channel_access(source_p
, targptr
, msptr
, MODE_QUERY
, NULL
) < CHFL_CHANOP
)
522 sendto_one(source_p
, form_str(ERR_CHANOPRIVSNEEDED
),
523 me
.name
, source_p
->name
, targptr
->chname
);
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
540 unsigned char *s
, *t
, c
;
542 for(s
= t
= (unsigned char *) arg
; (c
= *s
); s
++)
545 if(c
!= ':' && c
!= ',' && c
> ' ')
556 * ouput - the same key, fixed
557 * side effects - high ascii dropped to lower half of table,
558 * CR/LF/':' are dropped
561 fix_key_remote(char *arg
)
563 unsigned char *s
, *t
, c
;
565 for(s
= t
= (unsigned char *) arg
; (c
= *s
); s
++)
568 if((c
!= 0x0a) && (c
!= ':') && (c
!= ',') && (c
!= 0x0d) && (c
!= ' '))
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
)
580 if(!allow_mode_change(source_p
, chptr
, alevel
, errors
, c
))
583 if(MyClient(source_p
) && (++mode_limit_simple
> MAXMODES_SIMPLE
))
587 if((dir
== MODE_ADD
) && !(chptr
->mode
.mode
& mode_type
))
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'))
594 chptr
->mode
.mode
|= mode_type
;
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
;
602 else if((dir
== MODE_DEL
) && (chptr
->mode
.mode
& mode_type
))
604 chptr
->mode
.mode
&= ~mode_type
;
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
;
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
)
618 if(MyClient(source_p
))
621 if((dir
== MODE_ADD
) && !(chptr
->mode
.mode
& mode_type
))
623 chptr
->mode
.mode
|= mode_type
;
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
;
631 else if((dir
== MODE_DEL
) && (chptr
->mode
.mode
& mode_type
))
633 chptr
->mode
.mode
&= ~mode_type
;
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
;
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
)
647 if(MyClient(source_p
) && !IsOperGeneral(source_p
))
649 if(!(*errors
& SM_ERR_NOPRIVS
))
650 sendto_one_numeric(source_p
, ERR_NOPRIVILEGES
, form_str(ERR_NOPRIVILEGES
));
651 *errors
|= SM_ERR_NOPRIVS
;
654 if(MyClient(source_p
) && !IsOperAdmin(source_p
))
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
;
663 if(MyClient(source_p
) && (++mode_limit_simple
> MAXMODES_SIMPLE
))
667 if((dir
== MODE_ADD
) && !(chptr
->mode
.mode
& mode_type
))
669 chptr
->mode
.mode
|= mode_type
;
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
;
677 else if((dir
== MODE_DEL
) && (chptr
->mode
.mode
& mode_type
))
679 chptr
->mode
.mode
&= ~mode_type
;
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
;
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
)
693 if(MyClient(source_p
) && !IsOper(source_p
))
695 if(!(*errors
& SM_ERR_NOPRIVS
))
696 sendto_one_numeric(source_p
, ERR_NOPRIVILEGES
, form_str(ERR_NOPRIVILEGES
));
697 *errors
|= SM_ERR_NOPRIVS
;
700 if(MyClient(source_p
) && !HasPrivilege(source_p
, "oper:cmodes"))
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
;
709 if(MyClient(source_p
) && (++mode_limit_simple
> MAXMODES_SIMPLE
))
713 if((dir
== MODE_ADD
) && !(chptr
->mode
.mode
& mode_type
))
715 chptr
->mode
.mode
|= mode_type
;
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
;
723 else if((dir
== MODE_DEL
) && (chptr
->mode
.mode
& mode_type
))
725 chptr
->mode
.mode
&= ~mode_type
;
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
;
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
)
745 const char *rpl_list_p
;
746 const char *rpl_endlist_p
;
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
);
760 /* if +e is disabled, allow all but +e locally */
761 if (!ConfigChannel
.use_except
&& MyClient(source_p
) && dir
== MODE_ADD
)
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
);
769 if(ConfigChannel
.use_except
|| (dir
== MODE_DEL
))
776 /* if +I is disabled, allow all but +I locally */
777 if (!ConfigChannel
.use_invex
&& MyClient(source_p
) && dir
== MODE_ADD
)
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
);
785 if(ConfigChannel
.use_invex
|| (dir
== MODE_DEL
))
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
);
800 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "chm_ban() called with unknown type!");
804 if (dir
== MODE_QUERY
)
806 if((*errors
& errorval
) != 0)
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
)
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
;
822 RB_DLINK_FOREACH(ptr
, list
->head
)
827 snprintf(buf
, sizeof(buf
), "%s$%s", banptr
->banstr
, banptr
->forward
);
829 rb_strlcpy(buf
, banptr
->banstr
, sizeof(buf
));
831 sendto_one(source_p
, rpl_list_p
,
832 me
.name
, source_p
->name
, chptr
->chname
,
833 buf
, banptr
->who
, banptr
->when
);
835 sendto_one(source_p
, rpl_endlist_p
, me
.name
, source_p
->name
, chptr
->chname
);
839 if (!allow_mode_change(source_p
, chptr
, alevel
, errors
, c
))
843 if (MyClient(source_p
) && (++mode_limit
> MAXMODEPARAMS
))
846 /* empty ban, or starts with ':' which messes up s2s, ignore it */
847 if (EmptyString(arg
) || *arg
== ':')
850 if (!MyClient(source_p
))
852 if (strchr(arg
, ' '))
858 mask
= pretty_mask(arg
);
860 /* we'd have problems parsing this, hyb6 does it too
861 * also make sure it will always fit on a line with channel
864 if(strlen(mask
) > MIN(BANLEN
, MODEBUFLEN
- 5))
866 sendto_one_numeric(source_p
, ERR_INVALIDBAN
,
867 form_str(ERR_INVALIDBAN
),
868 chptr
->chname
, c
, arg
);
872 /* Look for a $ after the first character.
873 * As the first character, it marks an extban; afterwards
874 * it delimits a forward channel.
876 if ((forward
= strchr(mask
+1, '$')) != NULL
)
879 if (*forward
== '\0')
883 /* if we're adding a NEW id */
886 if (*mask
== '$' && MyClient(source_p
))
888 if (!valid_extban(mask
, source_p
, chptr
, mode_type
))
890 sendto_one_numeric(source_p
, ERR_INVALIDBAN
,
891 form_str(ERR_INVALIDBAN
),
892 chptr
->chname
, c
, arg
);
897 /* For compatibility, only check the forward channel from
898 * local clients. Accept any forward channel from servers.
900 if (forward
!= NULL
&& MyClient(source_p
))
902 /* For simplicity and future flexibility, do not
903 * allow '$' in forwarding targets.
905 if (!ConfigChannel
.use_forward
||
906 strchr(forward
, '$') != NULL
)
908 sendto_one_numeric(source_p
, ERR_INVALIDBAN
,
909 form_str(ERR_INVALIDBAN
),
910 chptr
->chname
, c
, arg
);
913 /* check_forward() sends its own error message */
914 if (!check_forward(source_p
, chptr
, forward
))
916 /* Forwards only make sense for bans. */
917 if (mode_type
!= CHFL_BAN
)
919 sendto_one_numeric(source_p
, ERR_INVALIDBAN
,
920 form_str(ERR_INVALIDBAN
),
921 chptr
->chname
, c
, arg
);
926 /* dont allow local clients to overflow the banlist, dont
927 * let remote servers set duplicate bans
929 if (!add_id(source_p
, chptr
, mask
, forward
, list
, mode_type
))
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
;
941 else if (dir
== MODE_DEL
)
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
)
948 /* mask isn't a valid ban, check arg */
949 if ((removed
= del_id(chptr
, arg
, list
, mode_type
)) != NULL
)
953 if (removed
&& removed
->forward
)
954 removed_mask_pos
+= snprintf(buf
+ old_removed_mask_pos
, sizeof(buf
), "%s$%s", removed
->banstr
, removed
->forward
) + 1;
956 removed_mask_pos
+= rb_strlcpy(buf
+ old_removed_mask_pos
, mask
, sizeof(buf
)) + 1;
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
;
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
)
975 struct membership
*mstptr
;
976 struct Client
*targ_p
;
978 if(!allow_mode_change(source_p
, chptr
, alevel
, errors
, c
))
984 sendto_one_numeric(source_p
, ERR_NOSUCHNICK
, form_str(ERR_NOSUCHNICK
), "*");
988 if((targ_p
= find_chasing(source_p
, arg
, NULL
)) == NULL
)
993 mstptr
= find_channel_membership(chptr
, targ_p
);
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
;
1004 if(MyClient(source_p
) && (++mode_limit
> MAXMODEPARAMS
))
1009 if(targ_p
== source_p
&& mstptr
->flags
& CHFL_CHANOP
)
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
;
1018 mstptr
->flags
|= CHFL_CHANOP
;
1022 if(MyClient(source_p
) && IsService(targ_p
))
1024 sendto_one(source_p
, form_str(ERR_ISCHANSERVICE
),
1025 me
.name
, source_p
->name
, targ_p
->name
, chptr
->chname
);
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
;
1035 mstptr
->flags
&= ~CHFL_CHANOP
;
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
)
1043 struct membership
*mstptr
;
1044 struct Client
*targ_p
;
1046 if(!allow_mode_change(source_p
, chptr
, alevel
, errors
, c
))
1050 if(EmptyString(arg
))
1052 sendto_one_numeric(source_p
, ERR_NOSUCHNICK
, form_str(ERR_NOSUCHNICK
), "*");
1056 if((targ_p
= find_chasing(source_p
, arg
, NULL
)) == NULL
)
1061 mstptr
= find_channel_membership(chptr
, targ_p
);
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
;
1072 if(MyClient(source_p
) && (++mode_limit
> MAXMODEPARAMS
))
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
;
1083 mstptr
->flags
|= CHFL_VOICE
;
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
;
1093 mstptr
->flags
&= ~CHFL_VOICE
;
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
)
1101 static char limitstr
[30];
1104 if (!allow_mode_change(source_p
, chptr
, alevel
, errors
, c
))
1107 if (MyClient(source_p
) && (++mode_limit_simple
> MAXMODES_SIMPLE
))
1110 if (dir
== MODE_ADD
)
1112 if (EmptyString(arg
) || (limit
= atoi(arg
)) <= 0)
1115 sprintf(limitstr
, "%d", limit
);
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
;
1123 chptr
->mode
.limit
= limit
;
1125 else if (dir
== MODE_DEL
)
1127 if(!chptr
->mode
.limit
)
1130 chptr
->mode
.limit
= 0;
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
;
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
)
1144 int joins
= 0, timeslice
= 0;
1146 if (!allow_mode_change(source_p
, chptr
, alevel
, errors
, c
))
1149 if (MyClient(source_p
) && (++mode_limit_simple
> MAXMODES_SIMPLE
))
1152 if (dir
== MODE_ADD
)
1154 if (sscanf(arg
, "%d:%d", &joins
, ×lice
) < 2)
1157 if(joins
<= 0 || timeslice
<= 0)
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
;
1166 chptr
->mode
.join_num
= joins
;
1167 chptr
->mode
.join_time
= timeslice
;
1169 else if(dir
== MODE_DEL
)
1171 if(!chptr
->mode
.join_num
)
1174 chptr
->mode
.join_num
= 0;
1175 chptr
->mode
.join_time
= 0;
1176 chptr
->join_count
= 0;
1177 chptr
->join_delta
= 0;
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
;
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
)
1191 /* if +f is disabled, ignore local attempts to set it */
1192 if (!ConfigChannel
.use_forward
&& MyClient(source_p
) && dir
== MODE_ADD
)
1195 if (dir
== MODE_QUERY
)
1197 if (!(*errors
& SM_ERR_RPL_F
))
1199 if (*chptr
->mode
.forward
== '\0')
1200 sendto_one_notice(source_p
, ":%s has no forward channel", chptr
->chname
);
1202 sendto_one_notice(source_p
, ":%s forward channel is %s", chptr
->chname
, chptr
->mode
.forward
);
1203 *errors
|= SM_ERR_RPL_F
;
1208 #ifndef FORWARD_OPERONLY
1209 if (!allow_mode_change(source_p
, chptr
, alevel
, errors
, c
))
1212 if (!IsOperGeneral(source_p
) && !IsServer(source_p
))
1214 if(!(*errors
& SM_ERR_NOPRIVS
))
1215 sendto_one_numeric(source_p
, ERR_NOPRIVILEGES
, form_str(ERR_NOPRIVILEGES
));
1216 *errors
|= SM_ERR_NOPRIVS
;
1221 if (MyClient(source_p
) && (++mode_limit_simple
> MAXMODES_SIMPLE
))
1224 if (dir
== MODE_ADD
)
1226 if(EmptyString(arg
))
1229 if(!check_forward(source_p
, chptr
, arg
))
1232 rb_strlcpy(chptr
->mode
.forward
, arg
, sizeof(chptr
->mode
.forward
));
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
;
1241 else if(dir
== MODE_DEL
)
1243 if(!(*chptr
->mode
.forward
))
1246 *chptr
->mode
.forward
= '\0';
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
;
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
)
1262 if (!allow_mode_change(source_p
, chptr
, alevel
, errors
, c
))
1265 if (MyClient(source_p
) && (++mode_limit_simple
> MAXMODES_SIMPLE
))
1268 if (dir
== MODE_ADD
)
1270 key
= LOCAL_COPY(arg
);
1272 if(MyClient(source_p
))
1275 fix_key_remote(key
);
1277 if(EmptyString(key
))
1280 s_assert(key
[0] != ' ');
1281 rb_strlcpy(chptr
->mode
.key
, key
, sizeof(chptr
->mode
.key
));
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
;
1289 else if(dir
== MODE_DEL
)
1291 static char splat
[] = "*";
1294 if(!(*chptr
->mode
.key
))
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
1301 for(i
= 0; i
< mode_count
; i
++)
1303 if(mode_changes
[i
].letter
== 'k' && mode_changes
[i
].dir
== MODE_ADD
)
1304 mode_changes
[i
].arg
= splat
;
1307 *chptr
->mode
.key
= 0;
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
= "*";
1318 struct ChannelMode chmode_table
[256] =
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 },
1347 /* set_channel_mode()
1349 * inputs - client, source, channel, membership pointer, params
1351 * side effects - channel modes/memberships are changed, MODE is issued
1353 * Extensively modified to be hotpluggable, 03/09/06 -- nenolod
1356 set_channel_mode(struct Client
*client_p
, struct Client
*source_p
,
1357 struct Channel
*chptr
, struct membership
*msptr
, int parc
, const char *parv
[])
1359 static char modebuf
[BUFSIZE
* 2]; /* paranoid case: 2 canonical chars per input char */
1360 static char parabuf
[BUFSIZE
];
1363 int cur_len
, mlen
, paralen
, paracount
, arglen
, len
;
1366 int access_dir
= MODE_QUERY
;
1370 const char *ml
= parv
[0];
1372 struct Client
*fakesource_p
;
1373 int flags_list
[3] = { ALL_MEMBERS
, ONLY_CHANOPS
, ONLY_OPERS
};
1376 removed_mask_pos
= 0;
1379 mode_limit_simple
= 0;
1381 /* Hide connecting server on netburst -- jilles */
1382 if (ConfigServerHide
.flatten_links
&& IsServer(source_p
) && !has_id(source_p
) && !HasSentEob(source_p
))
1385 fakesource_p
= source_p
;
1388 const struct ChannelMode
*cm
;
1394 static struct modeset modesets
[MAXPARA
];
1395 struct modeset
*ms
= modesets
, *mend
;
1396 char canon_op
= '\0';
1400 for (ml
= parv
[0]; *ml
!= 0; ml
++)
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
:
1421 if (cm
->set_func
== NULL
)
1423 sendto_one(source_p
, form_str(ERR_UNKNOWNMODE
), me
.name
, source_p
->name
, c
);
1426 if (use_arg
&& parn
>= parc
)
1428 if (!(cm
->flags
& CHM_CAN_QUERY
))
1430 sendto_one(source_p
, form_str(ERR_NEEDMOREPARAMS
), me
.name
, source_p
->name
, "MODE");
1433 effective_dir
= MODE_QUERY
;
1437 if (effective_dir
== MODE_QUERY
&& !(cm
->flags
& CHM_CAN_QUERY
))
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?
1446 char op
= effective_dir
== MODE_ADD
? '+' :
1447 effective_dir
== MODE_DEL
? '-' :
1451 *mbuf
++ = canon_op
= op
;
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
;
1461 ms
->dir
= effective_dir
;
1463 ms
->arg
= parv
[parn
++];
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
1481 /* XXX we could reject excess params here */
1489 rb_strlcat(modebuf
, reconstruct_parv(parn
- 1, parv
+ 1), sizeof modebuf
);
1495 alevel
= get_channel_access(source_p
, chptr
, msptr
, access_dir
, modebuf
);
1497 for (ms
= modesets
; ms
< mend
; ms
++)
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
);
1503 /* bail out if we have nothing to do... */
1507 if (IsServer(source_p
))
1508 mlen
= sprintf(modebuf
, ":%s MODE %s ", fakesource_p
->name
, chptr
->chname
);
1510 mlen
= sprintf(modebuf
, ":%s!%s@%s MODE %s ",
1511 source_p
->name
, source_p
->username
,
1512 source_p
->host
, chptr
->chname
);
1514 for(j
= 0; j
< 3; j
++)
1516 int send_flags
= flags
= flags_list
[j
];
1517 const char *priv
= NULL
;
1518 if (flags
== ONLY_OPERS
)
1520 send_flags
= ALL_MEMBERS
;
1521 priv
= "auspex:cmodes";
1524 mbuf
= modebuf
+ mlen
;
1527 paracount
= paralen
= 0;
1530 for(i
= 0; i
< mode_count
; i
++)
1532 if(mode_changes
[i
].letter
== 0 || mode_changes
[i
].mems
!= flags
)
1535 if(mode_changes
[i
].arg
!= NULL
)
1537 arglen
= strlen(mode_changes
[i
].arg
);
1539 if(arglen
> MODEBUFLEN
- 5)
1545 /* if we're creeping over MAXMODEPARAMSSERV, or over
1546 * bufsize (4 == +/-,modechar,two spaces) send now.
1548 if(mode_changes
[i
].arg
!= NULL
&&
1549 ((paracount
== MAXMODEPARAMSSERV
) ||
1550 ((cur_len
+ paralen
+ arglen
+ 4) > (BUFSIZE
- 3))))
1555 sendto_channel_local_priv(IsServer(source_p
) ? fakesource_p
: source_p
,
1556 send_flags
, priv
, chptr
, "%s %s", modebuf
, parabuf
);
1560 paracount
= paralen
= 0;
1562 mbuf
= modebuf
+ mlen
;
1568 if(dir
!= mode_changes
[i
].dir
)
1570 *mbuf
++ = (mode_changes
[i
].dir
== MODE_ADD
) ? '+' : '-';
1572 dir
= mode_changes
[i
].dir
;
1575 *mbuf
++ = mode_changes
[i
].letter
;
1578 if(mode_changes
[i
].arg
!= NULL
)
1581 len
= sprintf(pbuf
, "%s ", mode_changes
[i
].arg
);
1587 if(paralen
&& parabuf
[paralen
- 1] == ' ')
1588 parabuf
[paralen
- 1] = '\0';
1592 sendto_channel_local_priv(IsServer(source_p
) ? fakesource_p
: source_p
,
1593 send_flags
, priv
, chptr
, "%s %s", modebuf
, parabuf
);
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
);
1601 /* set_channel_mlock()
1603 * inputs - client, source, channel, params
1605 * side effects - channel mlock is changed / MLOCK is propagated
1608 set_channel_mlock(struct Client
*client_p
, struct Client
*source_p
,
1609 struct Channel
*chptr
, const char *newmlock
, bool propagate
)
1611 rb_free(chptr
->mode_lock
);
1612 chptr
->mode_lock
= newmlock
? rb_strdup(newmlock
) : NULL
;
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
: "");