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"
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
62 #define MAXMODES_SIMPLE 46 /* a-zA-Z except bqeIov */
64 static struct ChModeChange mode_changes
[BUFSIZE
];
65 static int mode_count
;
67 static int removed_mask_pos
;
70 char cflagsmyinfo
[256];
72 int chmode_flags
[256];
74 extern int h_get_channel_access
;
76 /* OPTIMIZE ME! -- dwr */
78 construct_cflags_strings(void)
81 char *ptr
= cflagsbuf
;
82 char *ptr2
= cflagsmyinfo
;
87 for(i
= 0; i
< 256; i
++)
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
)
97 chmode_flags
[i
] = chmode_table
[i
].mode_type
;
104 switch (chmode_flags
[i
])
106 case MODE_FREETARGET
:
107 case MODE_DISFORWARD
:
108 if(ConfigChannel
.use_forward
)
112 if(chmode_flags
[i
] != 0)
118 /* Should we leave orphaned check here? -- dwr */
119 if (chmode_table
[i
].set_func
!= NULL
&&
120 chmode_table
[i
].set_func
!= chm_orphaned
)
134 * outputs - an available cflag bitmask or
135 * 0 if no cflags are available
136 * side effects - NONE
139 find_cflag_slot(void)
141 unsigned int all_cflags
= 0, my_cflag
= 0, i
;
143 for (i
= 0; i
< 256; i
++)
144 all_cflags
|= chmode_flags
[i
];
146 for (my_cflag
= 1; my_cflag
&& (all_cflags
& my_cflag
);
153 cflag_add(char c_
, ChannelModeFunc function
)
155 int c
= (unsigned char)c_
;
157 if (chmode_table
[c
].set_func
!= NULL
&&
158 chmode_table
[c
].set_func
!= chm_orphaned
)
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)
165 chmode_table
[c
].set_func
= function
;
166 construct_cflags_strings();
167 return chmode_table
[c
].mode_type
;
171 cflag_orphan(char c_
)
173 int c
= (unsigned char)c_
;
175 s_assert(chmode_flags
[c
] != 0);
176 chmode_table
[c
].set_func
= chm_orphaned
;
177 construct_cflags_strings();
181 get_channel_access(struct Client
*source_p
, struct Channel
*chptr
, struct membership
*msptr
, int dir
, const char *modestr
)
183 hook_data_channel_approval moduledata
;
185 if(!MyClient(source_p
))
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
;
196 call_hook(h_get_channel_access
, &moduledata
);
198 return moduledata
.approved
;
201 /* allow_mode_change()
203 * Checks if mlock and chanops permit a mode change.
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
210 allow_mode_change(struct Client
*source_p
, struct Channel
*chptr
, int alevel
,
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
))
216 if (!(*errors
& SM_ERR_MLOCK
))
217 sendto_one_numeric(source_p
,
219 form_str(ERR_MLOCKRESTRICTED
),
223 *errors
|= SM_ERR_MLOCK
;
226 if(alevel
< CHFL_CHANOP
)
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
;
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
244 add_id(struct Client
*source_p
, struct Channel
*chptr
, const char *banid
, const char *forward
,
245 rb_dlink_list
* list
, long mode_type
)
247 struct Ban
*actualBan
;
248 static char who
[USERHOST_REPLYLEN
];
249 char *realban
= LOCAL_COPY(banid
);
252 /* dont let local clients overflow the banlist, or set redundant
255 if(MyClient(source_p
))
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
))
259 sendto_one(source_p
, form_str(ERR_BANLISTFULL
),
260 me
.name
, source_p
->name
, chptr
->chname
, realban
);
264 RB_DLINK_FOREACH(ptr
, list
->head
)
266 actualBan
= ptr
->data
;
267 if(mask_match(actualBan
->banstr
, realban
))
271 /* dont let remotes set duplicates */
274 RB_DLINK_FOREACH(ptr
, list
->head
)
276 actualBan
= ptr
->data
;
277 if(!irccmp(actualBan
->banstr
, realban
))
283 if(IsPerson(source_p
))
284 sprintf(who
, "%s!%s@%s", source_p
->name
, source_p
->username
, source_p
->host
);
286 rb_strlcpy(who
, source_p
->name
, sizeof(who
));
288 actualBan
= allocate_ban(realban
, who
, forward
);
289 actualBan
->when
= rb_current_time();
291 rb_dlinkAdd(actualBan
, &actualBan
->node
, list
);
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();
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
307 del_id(struct Channel
*chptr
, const char *banid
, rb_dlink_list
* list
, long mode_type
)
312 if(EmptyString(banid
))
315 RB_DLINK_FOREACH(ptr
, list
->head
)
319 if(irccmp(banid
, banptr
->banstr
) == 0)
321 rb_dlinkDelete(&banptr
->node
, list
);
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();
336 * input - string to check
337 * output - pointer to 'fixed' string, or "*" if empty
338 * side effects - any white space found becomes \0
341 check_string(char *s
)
344 static char splat
[] = "*";
361 * inputs - mask to pretty
362 * outputs - better version of the mask
363 * side effects - mask is chopped to limits, and transformed:
371 pretty_mask(const char *idmask
)
373 static char mask_buf
[BUFSIZE
];
375 const char *nick
, *user
, *host
, *forward
= NULL
;
381 mask
= LOCAL_COPY(idmask
);
382 mask
= check_string(mask
);
384 masklen
= strlen(mask
);
386 nick
= user
= host
= "*";
390 if((size_t) BUFSIZE
- mask_pos
< masklen
+ 5)
393 old_mask_pos
= mask_pos
;
397 memcpy(mask_buf
+ mask_pos
, mask
, masklen
+ 1);
398 mask_pos
+= masklen
+ 1;
399 t
= mask_buf
+ old_mask_pos
+ 1;
405 return mask_buf
+ old_mask_pos
;
409 if((t
= memchr(mask
, '@', masklen
)) != NULL
)
414 host
= t
, hl
= strlen(t
);
416 if((t
= memchr(mask
, '!', at
- mask
)) != NULL
)
421 user
= t
, ul
= at
- t
;
423 nick
= mask
, nl
= ex
- mask
;
428 user
= mask
, ul
= at
- mask
;
431 if((t
= memchr(host
, '!', hl
)) != NULL
||
432 (t
= memchr(host
, '$', hl
)) != NULL
)
436 forward
= t
, fl
= host
+ hl
- t
;
440 else if((t
= memchr(mask
, '!', masklen
)) != NULL
)
445 nick
= mask
, nl
= ex
- mask
;
447 user
= t
, ul
= strlen(t
);
449 else if(memchr(mask
, '.', masklen
) != NULL
||
450 memchr(mask
, ':', masklen
) != NULL
)
452 host
= mask
, hl
= masklen
;
457 nick
= mask
, nl
= masklen
;
460 /* truncate values to max lengths */
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
;
476 mask_buf
[mask_pos
++] = '$';
477 memcpy(mask_buf
+ mask_pos
, forward
, fl
), mask_pos
+= fl
;
479 mask_buf
[mask_pos
++] = '\0';
481 return mask_buf
+ old_mask_pos
;
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
491 check_forward(struct Client
*source_p
, struct Channel
*chptr
,
494 struct Channel
*targptr
= NULL
;
495 struct membership
*msptr
;
497 if(!check_channel_name(forward
) ||
498 (MyClient(source_p
) && (strlen(forward
) > LOC_CHANNELLEN
|| hash_find_resv(forward
))))
500 sendto_one_numeric(source_p
, ERR_BADCHANNAME
, form_str(ERR_BADCHANNAME
), forward
);
503 /* don't forward to inconsistent target -- jilles */
504 if(chptr
->chname
[0] == '#' && forward
[0] == '&')
506 sendto_one_numeric(source_p
, ERR_BADCHANNAME
,
507 form_str(ERR_BADCHANNAME
), forward
);
510 if(MyClient(source_p
) && (targptr
= find_channel(forward
)) == NULL
)
512 sendto_one_numeric(source_p
, ERR_NOSUCHCHANNEL
,
513 form_str(ERR_NOSUCHCHANNEL
), forward
);
516 if(MyClient(source_p
) && !(targptr
->mode
.mode
& MODE_FREETARGET
))
518 if((msptr
= find_channel_membership(targptr
, source_p
)) == NULL
||
519 get_channel_access(source_p
, targptr
, msptr
, MODE_QUERY
, NULL
) < CHFL_CHANOP
)
521 sendto_one(source_p
, form_str(ERR_CHANOPRIVSNEEDED
),
522 me
.name
, source_p
->name
, targptr
->chname
);
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
539 unsigned char *s
, *t
, c
;
541 for(s
= t
= (unsigned char *) arg
; (c
= *s
); s
++)
544 if(c
!= ':' && c
!= ',' && c
> ' ')
555 * ouput - the same key, fixed
556 * side effects - high ascii dropped to lower half of table,
557 * CR/LF/':' are dropped
560 fix_key_remote(char *arg
)
562 unsigned char *s
, *t
, c
;
564 for(s
= t
= (unsigned char *) arg
; (c
= *s
); s
++)
567 if((c
!= 0x0a) && (c
!= ':') && (c
!= ',') && (c
!= 0x0d) && (c
!= ' '))
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
)
579 if(!allow_mode_change(source_p
, chptr
, alevel
, errors
, c
))
583 if((dir
== MODE_ADD
) && !(chptr
->mode
.mode
& mode_type
))
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'))
590 chptr
->mode
.mode
|= mode_type
;
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
;
598 else if((dir
== MODE_DEL
) && (chptr
->mode
.mode
& mode_type
))
600 chptr
->mode
.mode
&= ~mode_type
;
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
;
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
)
614 if(MyClient(source_p
))
617 if((dir
== MODE_ADD
) && !(chptr
->mode
.mode
& mode_type
))
619 chptr
->mode
.mode
|= mode_type
;
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
;
627 else if((dir
== MODE_DEL
) && (chptr
->mode
.mode
& mode_type
))
629 chptr
->mode
.mode
&= ~mode_type
;
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
;
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
)
643 if(MyClient(source_p
) && !IsOperGeneral(source_p
))
645 if(!(*errors
& SM_ERR_NOPRIVS
))
646 sendto_one_numeric(source_p
, ERR_NOPRIVILEGES
, form_str(ERR_NOPRIVILEGES
));
647 *errors
|= SM_ERR_NOPRIVS
;
650 if(MyClient(source_p
) && !IsOperAdmin(source_p
))
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
;
660 if((dir
== MODE_ADD
) && !(chptr
->mode
.mode
& mode_type
))
662 chptr
->mode
.mode
|= mode_type
;
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
;
670 else if((dir
== MODE_DEL
) && (chptr
->mode
.mode
& mode_type
))
672 chptr
->mode
.mode
&= ~mode_type
;
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
;
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
)
686 if(MyClient(source_p
) && !IsOper(source_p
))
688 if(!(*errors
& SM_ERR_NOPRIVS
))
689 sendto_one_numeric(source_p
, ERR_NOPRIVILEGES
, form_str(ERR_NOPRIVILEGES
));
690 *errors
|= SM_ERR_NOPRIVS
;
693 if(MyClient(source_p
) && !HasPrivilege(source_p
, "oper:cmodes"))
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
;
703 if((dir
== MODE_ADD
) && !(chptr
->mode
.mode
& mode_type
))
705 chptr
->mode
.mode
|= mode_type
;
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
;
713 else if((dir
== MODE_DEL
) && (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_DEL
;
719 mode_changes
[mode_count
].mems
= ALL_MEMBERS
;
720 mode_changes
[mode_count
].id
= NULL
;
721 mode_changes
[mode_count
++].arg
= NULL
;
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
)
735 const char *rpl_list_p
;
736 const char *rpl_endlist_p
;
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
);
750 /* if +e is disabled, allow all but +e locally */
751 if (!ConfigChannel
.use_except
&& MyClient(source_p
) && dir
== MODE_ADD
)
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
);
759 if(ConfigChannel
.use_except
|| (dir
== MODE_DEL
))
766 /* if +I is disabled, allow all but +I locally */
767 if (!ConfigChannel
.use_invex
&& MyClient(source_p
) && dir
== MODE_ADD
)
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
);
775 if(ConfigChannel
.use_invex
|| (dir
== MODE_DEL
))
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
);
790 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
, "chm_ban() called with unknown type!");
794 if (dir
== MODE_QUERY
)
796 if((*errors
& errorval
) != 0)
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
)
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
;
812 RB_DLINK_FOREACH(ptr
, list
->head
)
817 snprintf(buf
, sizeof(buf
), "%s$%s", banptr
->banstr
, banptr
->forward
);
819 rb_strlcpy(buf
, banptr
->banstr
, sizeof(buf
));
821 sendto_one(source_p
, rpl_list_p
,
822 me
.name
, source_p
->name
, chptr
->chname
,
823 buf
, banptr
->who
, banptr
->when
);
825 sendto_one(source_p
, rpl_endlist_p
, me
.name
, source_p
->name
, chptr
->chname
);
829 if (!allow_mode_change(source_p
, chptr
, alevel
, errors
, c
))
832 /* empty ban, or starts with ':' which messes up s2s, ignore it */
833 if (EmptyString(arg
) || *arg
== ':')
836 if (!MyClient(source_p
))
838 if (strchr(arg
, ' '))
844 mask
= pretty_mask(arg
);
846 /* we'd have problems parsing this, hyb6 does it too
847 * also make sure it will always fit on a line with channel
850 if(strlen(mask
) > MIN(BANLEN
, MODEBUFLEN
- 5))
852 sendto_one_numeric(source_p
, ERR_INVALIDBAN
,
853 form_str(ERR_INVALIDBAN
),
854 chptr
->chname
, c
, arg
);
858 /* Look for a $ after the first character.
859 * As the first character, it marks an extban; afterwards
860 * it delimits a forward channel.
862 if ((forward
= strchr(mask
+1, '$')) != NULL
)
865 if (*forward
== '\0')
869 /* if we're adding a NEW id */
872 if (*mask
== '$' && MyClient(source_p
))
874 if (!valid_extban(mask
, source_p
, chptr
, mode_type
))
876 sendto_one_numeric(source_p
, ERR_INVALIDBAN
,
877 form_str(ERR_INVALIDBAN
),
878 chptr
->chname
, c
, arg
);
883 /* For compatibility, only check the forward channel from
884 * local clients. Accept any forward channel from servers.
886 if (forward
!= NULL
&& MyClient(source_p
))
888 /* For simplicity and future flexibility, do not
889 * allow '$' in forwarding targets.
891 if (!ConfigChannel
.use_forward
||
892 strchr(forward
, '$') != NULL
)
894 sendto_one_numeric(source_p
, ERR_INVALIDBAN
,
895 form_str(ERR_INVALIDBAN
),
896 chptr
->chname
, c
, arg
);
899 /* check_forward() sends its own error message */
900 if (!check_forward(source_p
, chptr
, forward
))
902 /* Forwards only make sense for bans. */
903 if (mode_type
!= CHFL_BAN
)
905 sendto_one_numeric(source_p
, ERR_INVALIDBAN
,
906 form_str(ERR_INVALIDBAN
),
907 chptr
->chname
, c
, arg
);
912 /* dont allow local clients to overflow the banlist, dont
913 * let remote servers set duplicate bans
915 if (!add_id(source_p
, chptr
, mask
, forward
, list
, mode_type
))
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
;
927 else if (dir
== MODE_DEL
)
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
)
934 /* mask isn't a valid ban, check arg */
935 if ((removed
= del_id(chptr
, arg
, list
, mode_type
)) != NULL
)
939 if (removed
&& removed
->forward
)
940 removed_mask_pos
+= snprintf(buf
+ old_removed_mask_pos
, sizeof(buf
), "%s$%s", removed
->banstr
, removed
->forward
) + 1;
942 removed_mask_pos
+= rb_strlcpy(buf
+ old_removed_mask_pos
, mask
, sizeof(buf
)) + 1;
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
;
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
)
961 struct membership
*mstptr
;
962 struct Client
*targ_p
;
964 if(!allow_mode_change(source_p
, chptr
, alevel
, errors
, c
))
970 sendto_one_numeric(source_p
, ERR_NOSUCHNICK
, form_str(ERR_NOSUCHNICK
), "*");
974 if((targ_p
= find_chasing(source_p
, arg
, NULL
)) == NULL
)
979 mstptr
= find_channel_membership(chptr
, targ_p
);
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
;
992 if(targ_p
== source_p
&& mstptr
->flags
& CHFL_CHANOP
)
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
;
1001 mstptr
->flags
|= CHFL_CHANOP
;
1005 if(MyClient(source_p
) && IsService(targ_p
))
1007 sendto_one(source_p
, form_str(ERR_ISCHANSERVICE
),
1008 me
.name
, source_p
->name
, targ_p
->name
, chptr
->chname
);
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
;
1018 mstptr
->flags
&= ~CHFL_CHANOP
;
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
)
1026 struct membership
*mstptr
;
1027 struct Client
*targ_p
;
1029 if(!allow_mode_change(source_p
, chptr
, alevel
, errors
, c
))
1033 if(EmptyString(arg
))
1035 sendto_one_numeric(source_p
, ERR_NOSUCHNICK
, form_str(ERR_NOSUCHNICK
), "*");
1039 if((targ_p
= find_chasing(source_p
, arg
, NULL
)) == NULL
)
1044 mstptr
= find_channel_membership(chptr
, targ_p
);
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
;
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
;
1063 mstptr
->flags
|= CHFL_VOICE
;
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
;
1073 mstptr
->flags
&= ~CHFL_VOICE
;
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
)
1081 static char limitstr
[30];
1084 if (!allow_mode_change(source_p
, chptr
, alevel
, errors
, c
))
1087 if (dir
== MODE_ADD
)
1089 if (EmptyString(arg
) || (limit
= atoi(arg
)) <= 0)
1092 sprintf(limitstr
, "%d", limit
);
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
;
1100 chptr
->mode
.limit
= limit
;
1102 else if (dir
== MODE_DEL
)
1104 if(!chptr
->mode
.limit
)
1107 chptr
->mode
.limit
= 0;
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
;
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
)
1121 int joins
= 0, timeslice
= 0;
1123 if (!allow_mode_change(source_p
, chptr
, alevel
, errors
, c
))
1126 if (dir
== MODE_ADD
)
1128 if (sscanf(arg
, "%d:%d", &joins
, ×lice
) < 2)
1131 if(joins
<= 0 || timeslice
<= 0)
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
;
1140 chptr
->mode
.join_num
= joins
;
1141 chptr
->mode
.join_time
= timeslice
;
1143 else if(dir
== MODE_DEL
)
1145 if(!chptr
->mode
.join_num
)
1148 chptr
->mode
.join_num
= 0;
1149 chptr
->mode
.join_time
= 0;
1150 chptr
->join_count
= 0;
1151 chptr
->join_delta
= 0;
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
;
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
)
1165 /* if +f is disabled, ignore local attempts to set it */
1166 if (!ConfigChannel
.use_forward
&& MyClient(source_p
) && dir
== MODE_ADD
)
1169 if (dir
== MODE_QUERY
)
1171 if (!(*errors
& SM_ERR_RPL_F
))
1173 if (*chptr
->mode
.forward
== '\0')
1174 sendto_one_notice(source_p
, ":%s has no forward channel", chptr
->chname
);
1176 sendto_one_notice(source_p
, ":%s forward channel is %s", chptr
->chname
, chptr
->mode
.forward
);
1177 *errors
|= SM_ERR_RPL_F
;
1182 #ifndef FORWARD_OPERONLY
1183 if (!allow_mode_change(source_p
, chptr
, alevel
, errors
, c
))
1186 if (!IsOperGeneral(source_p
) && !IsServer(source_p
))
1188 if(!(*errors
& SM_ERR_NOPRIVS
))
1189 sendto_one_numeric(source_p
, ERR_NOPRIVILEGES
, form_str(ERR_NOPRIVILEGES
));
1190 *errors
|= SM_ERR_NOPRIVS
;
1195 if (dir
== MODE_ADD
)
1197 if(EmptyString(arg
))
1200 if(!check_forward(source_p
, chptr
, arg
))
1203 rb_strlcpy(chptr
->mode
.forward
, arg
, sizeof(chptr
->mode
.forward
));
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
;
1212 else if(dir
== MODE_DEL
)
1214 if(!(*chptr
->mode
.forward
))
1217 *chptr
->mode
.forward
= '\0';
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
;
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
)
1233 if (!allow_mode_change(source_p
, chptr
, alevel
, errors
, c
))
1236 if (dir
== MODE_ADD
)
1238 key
= LOCAL_COPY(arg
);
1240 if(MyClient(source_p
))
1243 fix_key_remote(key
);
1245 if(EmptyString(key
))
1248 s_assert(key
[0] != ' ');
1249 rb_strlcpy(chptr
->mode
.key
, key
, sizeof(chptr
->mode
.key
));
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
;
1257 else if(dir
== MODE_DEL
)
1259 static char splat
[] = "*";
1262 if(!(*chptr
->mode
.key
))
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
1269 for(i
= 0; i
< mode_count
; i
++)
1271 if(mode_changes
[i
].letter
== 'k' && mode_changes
[i
].dir
== MODE_ADD
)
1272 mode_changes
[i
].arg
= splat
;
1275 *chptr
->mode
.key
= 0;
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
= "*";
1286 struct ChannelMode chmode_table
[256] =
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 },
1315 /* set_channel_mode()
1317 * inputs - client, source, channel, membership pointer, params
1319 * side effects - channel modes/memberships are changed, MODE is issued
1321 * Extensively modified to be hotpluggable, 03/09/06 -- nenolod
1324 set_channel_mode(struct Client
*client_p
, struct Client
*source_p
,
1325 struct Channel
*chptr
, struct membership
*msptr
, int parc
, const char *parv
[])
1327 static char modebuf
[BUFSIZE
* 2]; /* paranoid case: 2 canonical chars per input char */
1328 static char parabuf
[BUFSIZE
];
1331 int cur_len
, mlen
, paralen
, paracount
, arglen
, len
;
1334 bool changes
= false;
1335 bool privileged_query
= false;
1339 const char *ml
= parv
[0];
1341 struct Client
*fakesource_p
;
1342 int flags_list
[3] = { ALL_MEMBERS
, ONLY_CHANOPS
, ONLY_OPERS
};
1344 int mode_limit_simple
= 0;
1347 removed_mask_pos
= 0;
1350 /* Hide connecting server on netburst -- jilles */
1351 if (ConfigServerHide
.flatten_links
&& IsServer(source_p
) && !has_id(source_p
) && !HasSentEob(source_p
))
1354 fakesource_p
= source_p
;
1357 const struct ChannelMode
*cm
;
1363 static struct modeset modesets
[MAXMODEPARAMS
+ MAXMODES_SIMPLE
];
1364 struct modeset
*ms
= modesets
, *mend
;
1365 char canon_op
= '\0';
1369 for (ml
= parv
[0]; *ml
!= 0 && ms
- modesets
< ARRAY_SIZE(modesets
); ml
++)
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
:
1390 if (cm
->set_func
== NULL
)
1392 sendto_one(source_p
, form_str(ERR_UNKNOWNMODE
), me
.name
, source_p
->name
, c
);
1395 if (use_arg
&& parn
>= parc
)
1397 if (!(cm
->flags
& CHM_CAN_QUERY
))
1399 sendto_one(source_p
, form_str(ERR_NEEDMOREPARAMS
), me
.name
, source_p
->name
, "MODE");
1402 effective_dir
= MODE_QUERY
;
1406 if (effective_dir
== MODE_QUERY
&& !(cm
->flags
& CHM_CAN_QUERY
))
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?
1415 if (MyClient(source_p
))
1417 if (use_arg
&& ++mode_limit
> MAXMODEPARAMS
)
1419 if (!use_arg
&& ++mode_limit_simple
> MAXMODES_SIMPLE
)
1423 char op
= effective_dir
== MODE_ADD
? '+' :
1424 effective_dir
== MODE_DEL
? '-' :
1428 *mbuf
++ = canon_op
= op
;
1432 if (effective_dir
!= MODE_QUERY
)
1434 if (effective_dir
== MODE_QUERY
&& cm
->flags
& CHM_OPS_QUERY
)
1435 privileged_query
= true;
1438 ms
->dir
= effective_dir
;
1440 ms
->arg
= parv
[parn
++];
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
1458 /* XXX we could reject excess params here */
1461 /* Finish the flood grace period if we were asked to do anything */
1462 if (changes
&& MyClient(source_p
) && !IsFloodDone(source_p
))
1464 flood_endgrace(source_p
);
1472 rb_strlcat(modebuf
, reconstruct_parv(parn
- 1, parv
+ 1), sizeof modebuf
);
1478 int access_dir
= privileged_query
? MODE_OP_QUERY
:
1479 changes
? MODE_ADD
:
1481 alevel
= get_channel_access(source_p
, chptr
, msptr
, access_dir
, modebuf
);
1483 for (ms
= modesets
; ms
< mend
; ms
++)
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
);
1489 /* bail out if we have nothing to do... */
1493 if (IsServer(source_p
))
1494 mlen
= sprintf(modebuf
, ":%s MODE %s ", fakesource_p
->name
, chptr
->chname
);
1496 mlen
= sprintf(modebuf
, ":%s!%s@%s MODE %s ",
1497 source_p
->name
, source_p
->username
,
1498 source_p
->host
, chptr
->chname
);
1500 for(j
= 0; j
< 3; j
++)
1502 int send_flags
= flags
= flags_list
[j
];
1503 const char *priv
= NULL
;
1504 if (flags
== ONLY_OPERS
)
1506 send_flags
= ALL_MEMBERS
;
1507 priv
= "auspex:cmodes";
1510 mbuf
= modebuf
+ mlen
;
1513 paracount
= paralen
= 0;
1516 for(i
= 0; i
< mode_count
; i
++)
1518 if(mode_changes
[i
].letter
== 0 || mode_changes
[i
].mems
!= flags
)
1521 if(mode_changes
[i
].arg
!= NULL
)
1523 arglen
= strlen(mode_changes
[i
].arg
);
1525 if(arglen
> MODEBUFLEN
- 5)
1531 /* if we're creeping over MAXMODEPARAMSSERV, or over
1532 * bufsize (4 == +/-,modechar,two spaces) send now.
1534 if(mode_changes
[i
].arg
!= NULL
&&
1535 ((paracount
== MAXMODEPARAMSSERV
) ||
1536 ((cur_len
+ paralen
+ arglen
+ 4) > (BUFSIZE
- 3))))
1541 sendto_channel_local_priv(IsServer(source_p
) ? fakesource_p
: source_p
,
1542 send_flags
, priv
, chptr
, "%s %s", modebuf
, parabuf
);
1546 paracount
= paralen
= 0;
1548 mbuf
= modebuf
+ mlen
;
1554 if(dir
!= mode_changes
[i
].dir
)
1556 *mbuf
++ = (mode_changes
[i
].dir
== MODE_ADD
) ? '+' : '-';
1558 dir
= mode_changes
[i
].dir
;
1561 *mbuf
++ = mode_changes
[i
].letter
;
1564 if(mode_changes
[i
].arg
!= NULL
)
1567 len
= sprintf(pbuf
, "%s ", mode_changes
[i
].arg
);
1573 if(paralen
&& parabuf
[paralen
- 1] == ' ')
1574 parabuf
[paralen
- 1] = '\0';
1578 sendto_channel_local_priv(IsServer(source_p
) ? fakesource_p
: source_p
,
1579 send_flags
, priv
, chptr
, "%s %s", modebuf
, parabuf
);
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
);
1587 /* set_channel_mlock()
1589 * inputs - client, source, channel, params
1591 * side effects - channel mlock is changed / MLOCK is propagated
1594 set_channel_mlock(struct Client
*client_p
, struct Client
*source_p
,
1595 struct Channel
*chptr
, const char *newmlock
, bool propagate
)
1597 rb_free(chptr
->mode_lock
);
1598 chptr
->mode_lock
= newmlock
? rb_strdup(newmlock
) : NULL
;
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
: "");