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 - NULL on failure, allocated Ban 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 */
253 if(MyClient(source_p
))
255 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
))
257 sendto_one(source_p
, form_str(ERR_BANLISTFULL
),
258 me
.name
, source_p
->name
, chptr
->chname
, realban
);
263 /* don't let anyone set duplicate bans */
264 RB_DLINK_FOREACH(ptr
, list
->head
)
266 actualBan
= ptr
->data
;
267 if(!irccmp(actualBan
->banstr
, realban
))
271 if(IsPerson(source_p
))
272 sprintf(who
, "%s!%s@%s", source_p
->name
, source_p
->username
, source_p
->host
);
274 rb_strlcpy(who
, source_p
->name
, sizeof(who
));
276 actualBan
= allocate_ban(realban
, who
, forward
);
277 actualBan
->when
= rb_current_time();
279 rb_dlinkAdd(actualBan
, &actualBan
->node
, list
);
281 /* invalidate the can_send() cache */
282 if(mode_type
== CHFL_BAN
|| mode_type
== CHFL_QUIET
|| mode_type
== CHFL_EXCEPTION
)
283 chptr
->bants
= rb_current_time();
290 * inputs - channel, id to remove, type
291 * outputs - pointer to ban that was removed, if any
292 * side effects - given id is removed from the appropriate list and returned
295 del_id(struct Channel
*chptr
, const char *banid
, rb_dlink_list
* list
, long mode_type
)
300 if(EmptyString(banid
))
303 RB_DLINK_FOREACH(ptr
, list
->head
)
307 if(irccmp(banid
, banptr
->banstr
) == 0)
309 rb_dlinkDelete(&banptr
->node
, list
);
311 /* invalidate the can_send() cache */
312 if(mode_type
== CHFL_BAN
|| mode_type
== CHFL_QUIET
|| mode_type
== CHFL_EXCEPTION
)
313 chptr
->bants
= rb_current_time();
324 * input - string to check
325 * output - pointer to 'fixed' string, or "*" if empty
326 * side effects - any white space found becomes \0
329 check_string(char *s
)
332 static char splat
[] = "*";
349 * inputs - mask to pretty
350 * outputs - better version of the mask
351 * side effects - mask is chopped to limits, and transformed:
359 pretty_mask(const char *idmask
)
361 static char mask_buf
[BUFSIZE
];
363 const char *nick
, *user
, *host
, *forward
= NULL
;
369 mask
= LOCAL_COPY(idmask
);
370 mask
= check_string(mask
);
372 masklen
= strlen(mask
);
374 nick
= user
= host
= "*";
378 if((size_t) BUFSIZE
- mask_pos
< masklen
+ 5)
381 old_mask_pos
= mask_pos
;
385 memcpy(mask_buf
+ mask_pos
, mask
, masklen
+ 1);
386 mask_pos
+= masklen
+ 1;
387 t
= mask_buf
+ old_mask_pos
+ 1;
393 return mask_buf
+ old_mask_pos
;
397 if((t
= memchr(mask
, '@', masklen
)) != NULL
)
402 host
= t
, hl
= strlen(t
);
404 if((t
= memchr(mask
, '!', at
- mask
)) != NULL
)
409 user
= t
, ul
= at
- t
;
411 nick
= mask
, nl
= ex
- mask
;
416 user
= mask
, ul
= at
- mask
;
419 if((t
= memchr(host
, '!', hl
)) != NULL
||
420 (t
= memchr(host
, '$', hl
)) != NULL
)
424 forward
= t
, fl
= host
+ hl
- t
;
428 else if((t
= memchr(mask
, '!', masklen
)) != NULL
)
433 nick
= mask
, nl
= ex
- mask
;
435 user
= t
, ul
= strlen(t
);
437 else if(memchr(mask
, '.', masklen
) != NULL
||
438 memchr(mask
, ':', masklen
) != NULL
)
440 host
= mask
, hl
= masklen
;
445 nick
= mask
, nl
= masklen
;
448 /* truncate values to max lengths */
458 memcpy(mask_buf
+ mask_pos
, nick
, nl
), mask_pos
+= nl
;
459 mask_buf
[mask_pos
++] = '!';
460 memcpy(mask_buf
+ mask_pos
, user
, ul
), mask_pos
+= ul
;
461 mask_buf
[mask_pos
++] = '@';
462 memcpy(mask_buf
+ mask_pos
, host
, hl
), mask_pos
+= hl
;
464 mask_buf
[mask_pos
++] = '$';
465 memcpy(mask_buf
+ mask_pos
, forward
, fl
), mask_pos
+= fl
;
467 mask_buf
[mask_pos
++] = '\0';
469 return mask_buf
+ old_mask_pos
;
474 * input - client, channel to set mode on, target channel name
475 * output - true if forwarding should be allowed
476 * side effects - numeric sent if not allowed
479 check_forward(struct Client
*source_p
, struct Channel
*chptr
,
482 struct Channel
*targptr
= NULL
;
483 struct membership
*msptr
;
485 if(!check_channel_name(forward
) ||
486 (MyClient(source_p
) && (strlen(forward
) > LOC_CHANNELLEN
|| hash_find_resv(forward
))))
488 sendto_one_numeric(source_p
, ERR_BADCHANNAME
, form_str(ERR_BADCHANNAME
), forward
);
491 /* don't forward to inconsistent target -- jilles */
492 if(chptr
->chname
[0] == '#' && forward
[0] == '&')
494 sendto_one_numeric(source_p
, ERR_BADCHANNAME
,
495 form_str(ERR_BADCHANNAME
), forward
);
498 if(MyClient(source_p
) && (targptr
= find_channel(forward
)) == NULL
)
500 sendto_one_numeric(source_p
, ERR_NOSUCHCHANNEL
,
501 form_str(ERR_NOSUCHCHANNEL
), forward
);
504 if(MyClient(source_p
) && !(targptr
->mode
.mode
& MODE_FREETARGET
))
506 if((msptr
= find_channel_membership(targptr
, source_p
)) == NULL
||
507 get_channel_access(source_p
, targptr
, msptr
, MODE_QUERY
, NULL
) < CHFL_CHANOP
)
509 sendto_one(source_p
, form_str(ERR_CHANOPRIVSNEEDED
),
510 me
.name
, source_p
->name
, targptr
->chname
);
520 * output - the same key, fixed
521 * side effects - anything below ascii 13 is discarded, ':' discarded,
522 * high ascii is dropped to lower half of ascii table
527 unsigned char *s
, *t
, c
;
529 for(s
= t
= (unsigned char *) arg
; (c
= *s
); s
++)
532 if(c
!= ':' && c
!= ',' && c
> ' ')
543 * ouput - the same key, fixed
544 * side effects - high ascii dropped to lower half of table,
545 * CR/LF/':' are dropped
548 fix_key_remote(char *arg
)
550 unsigned char *s
, *t
, c
;
552 for(s
= t
= (unsigned char *) arg
; (c
= *s
); s
++)
555 if((c
!= 0x0a) && (c
!= ':') && (c
!= ',') && (c
!= 0x0d) && (c
!= ' '))
564 chm_simple(struct Client
*source_p
, struct Channel
*chptr
,
565 int alevel
, const char *arg
, int *errors
, int dir
, char c
, long mode_type
)
567 if(!allow_mode_change(source_p
, chptr
, alevel
, errors
, c
))
571 if((dir
== MODE_ADD
) && !(chptr
->mode
.mode
& mode_type
))
573 /* if +f is disabled, ignore an attempt to set +QF locally */
574 if(!ConfigChannel
.use_forward
&& MyClient(source_p
) &&
575 (c
== 'Q' || c
== 'F'))
578 chptr
->mode
.mode
|= mode_type
;
580 mode_changes
[mode_count
].letter
= c
;
581 mode_changes
[mode_count
].dir
= MODE_ADD
;
582 mode_changes
[mode_count
].id
= NULL
;
583 mode_changes
[mode_count
].mems
= ALL_MEMBERS
;
584 mode_changes
[mode_count
++].arg
= NULL
;
586 else if((dir
== MODE_DEL
) && (chptr
->mode
.mode
& mode_type
))
588 chptr
->mode
.mode
&= ~mode_type
;
590 mode_changes
[mode_count
].letter
= c
;
591 mode_changes
[mode_count
].dir
= MODE_DEL
;
592 mode_changes
[mode_count
].mems
= ALL_MEMBERS
;
593 mode_changes
[mode_count
].id
= NULL
;
594 mode_changes
[mode_count
++].arg
= NULL
;
599 chm_orphaned(struct Client
*source_p
, struct Channel
*chptr
,
600 int alevel
, const char *arg
, int *errors
, int dir
, char c
, long mode_type
)
602 if(MyClient(source_p
))
605 if((dir
== MODE_ADD
) && !(chptr
->mode
.mode
& mode_type
))
607 chptr
->mode
.mode
|= mode_type
;
609 mode_changes
[mode_count
].letter
= c
;
610 mode_changes
[mode_count
].dir
= MODE_ADD
;
611 mode_changes
[mode_count
].id
= NULL
;
612 mode_changes
[mode_count
].mems
= ALL_MEMBERS
;
613 mode_changes
[mode_count
++].arg
= NULL
;
615 else if((dir
== MODE_DEL
) && (chptr
->mode
.mode
& mode_type
))
617 chptr
->mode
.mode
&= ~mode_type
;
619 mode_changes
[mode_count
].letter
= c
;
620 mode_changes
[mode_count
].dir
= MODE_DEL
;
621 mode_changes
[mode_count
].mems
= ALL_MEMBERS
;
622 mode_changes
[mode_count
].id
= NULL
;
623 mode_changes
[mode_count
++].arg
= NULL
;
628 chm_hidden(struct Client
*source_p
, struct Channel
*chptr
,
629 int alevel
, const char *arg
, int *errors
, int dir
, char c
, long mode_type
)
631 if(MyClient(source_p
) && !IsOperGeneral(source_p
))
633 if(!(*errors
& SM_ERR_NOPRIVS
))
634 sendto_one_numeric(source_p
, ERR_NOPRIVILEGES
, form_str(ERR_NOPRIVILEGES
));
635 *errors
|= SM_ERR_NOPRIVS
;
638 if(MyClient(source_p
) && !IsOperAdmin(source_p
))
640 if(!(*errors
& SM_ERR_NOPRIVS
))
641 sendto_one(source_p
, form_str(ERR_NOPRIVS
), me
.name
,
642 source_p
->name
, "admin");
643 *errors
|= SM_ERR_NOPRIVS
;
648 if((dir
== MODE_ADD
) && !(chptr
->mode
.mode
& mode_type
))
650 chptr
->mode
.mode
|= mode_type
;
652 mode_changes
[mode_count
].letter
= c
;
653 mode_changes
[mode_count
].dir
= MODE_ADD
;
654 mode_changes
[mode_count
].id
= NULL
;
655 mode_changes
[mode_count
].mems
= ONLY_OPERS
;
656 mode_changes
[mode_count
++].arg
= NULL
;
658 else if((dir
== MODE_DEL
) && (chptr
->mode
.mode
& mode_type
))
660 chptr
->mode
.mode
&= ~mode_type
;
662 mode_changes
[mode_count
].letter
= c
;
663 mode_changes
[mode_count
].dir
= MODE_DEL
;
664 mode_changes
[mode_count
].mems
= ONLY_OPERS
;
665 mode_changes
[mode_count
].id
= NULL
;
666 mode_changes
[mode_count
++].arg
= NULL
;
671 chm_staff(struct Client
*source_p
, struct Channel
*chptr
,
672 int alevel
, const char *arg
, int *errors
, int dir
, char c
, long mode_type
)
674 if(MyClient(source_p
) && !IsOper(source_p
))
676 if(!(*errors
& SM_ERR_NOPRIVS
))
677 sendto_one_numeric(source_p
, ERR_NOPRIVILEGES
, form_str(ERR_NOPRIVILEGES
));
678 *errors
|= SM_ERR_NOPRIVS
;
681 if(MyClient(source_p
) && !HasPrivilege(source_p
, "oper:cmodes"))
683 if(!(*errors
& SM_ERR_NOPRIVS
))
684 sendto_one(source_p
, form_str(ERR_NOPRIVS
), me
.name
,
685 source_p
->name
, "cmodes");
686 *errors
|= SM_ERR_NOPRIVS
;
691 if((dir
== MODE_ADD
) && !(chptr
->mode
.mode
& mode_type
))
693 chptr
->mode
.mode
|= mode_type
;
695 mode_changes
[mode_count
].letter
= c
;
696 mode_changes
[mode_count
].dir
= MODE_ADD
;
697 mode_changes
[mode_count
].id
= NULL
;
698 mode_changes
[mode_count
].mems
= ALL_MEMBERS
;
699 mode_changes
[mode_count
++].arg
= NULL
;
701 else if((dir
== MODE_DEL
) && (chptr
->mode
.mode
& mode_type
))
703 chptr
->mode
.mode
&= ~mode_type
;
705 mode_changes
[mode_count
].letter
= c
;
706 mode_changes
[mode_count
].dir
= MODE_DEL
;
707 mode_changes
[mode_count
].mems
= ALL_MEMBERS
;
708 mode_changes
[mode_count
].id
= NULL
;
709 mode_changes
[mode_count
++].arg
= NULL
;
714 chm_ban(struct Client
*source_p
, struct Channel
*chptr
,
715 int alevel
, const char *arg
, int *errors
, int dir
, char c
, long mode_type
)
723 const char *rpl_list_p
;
724 const char *rpl_endlist_p
;
730 list
= &chptr
->banlist
;
731 errorval
= SM_ERR_RPL_B
;
732 rpl_list_p
= form_str(RPL_BANLIST
);
733 rpl_endlist_p
= form_str(RPL_ENDOFBANLIST
);
738 /* if +e is disabled, allow all but +e locally */
739 if (!ConfigChannel
.use_except
&& MyClient(source_p
) && dir
== MODE_ADD
)
742 list
= &chptr
->exceptlist
;
743 errorval
= SM_ERR_RPL_E
;
744 rpl_list_p
= form_str(RPL_EXCEPTLIST
);
745 rpl_endlist_p
= form_str(RPL_ENDOFEXCEPTLIST
);
747 if(ConfigChannel
.use_except
|| (dir
== MODE_DEL
))
754 /* if +I is disabled, allow all but +I locally */
755 if (!ConfigChannel
.use_invex
&& MyClient(source_p
) && dir
== MODE_ADD
)
758 list
= &chptr
->invexlist
;
759 errorval
= SM_ERR_RPL_I
;
760 rpl_list_p
= form_str(RPL_INVITELIST
);
761 rpl_endlist_p
= form_str(RPL_ENDOFINVITELIST
);
763 if(ConfigChannel
.use_invex
|| (dir
== MODE_DEL
))
770 list
= &chptr
->quietlist
;
771 errorval
= SM_ERR_RPL_Q
;
772 rpl_list_p
= form_str(RPL_QUIETLIST
);
773 rpl_endlist_p
= form_str(RPL_ENDOFQUIETLIST
);
778 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
, "chm_ban() called with unknown type!");
782 if (dir
== MODE_QUERY
)
784 if((*errors
& errorval
) != 0)
788 /* non-ops cant see +eI lists.. */
789 /* note that this is still permitted if +e/+I are mlocked. */
790 if(alevel
< CHFL_CHANOP
&& mode_type
!= CHFL_BAN
&&
791 mode_type
!= CHFL_QUIET
)
793 if(!(*errors
& SM_ERR_NOOPS
))
794 sendto_one(source_p
, form_str(ERR_CHANOPRIVSNEEDED
),
795 me
.name
, source_p
->name
, chptr
->chname
);
796 *errors
|= SM_ERR_NOOPS
;
800 RB_DLINK_FOREACH(ptr
, list
->head
)
805 snprintf(buf
, sizeof(buf
), "%s$%s", banptr
->banstr
, banptr
->forward
);
807 rb_strlcpy(buf
, banptr
->banstr
, sizeof(buf
));
809 sendto_one(source_p
, rpl_list_p
,
810 me
.name
, source_p
->name
, chptr
->chname
,
811 buf
, banptr
->who
, banptr
->when
);
813 sendto_one(source_p
, rpl_endlist_p
, me
.name
, source_p
->name
, chptr
->chname
);
817 if (!allow_mode_change(source_p
, chptr
, alevel
, errors
, c
))
820 /* empty ban, or starts with ':' which messes up s2s, ignore it */
821 if (EmptyString(arg
) || *arg
== ':')
824 if (!MyClient(source_p
))
826 if (strchr(arg
, ' '))
832 mask
= pretty_mask(arg
);
834 /* we'd have problems parsing this, hyb6 does it too
835 * also make sure it will always fit on a line with channel
838 if(strlen(mask
) > MIN(BANLEN
, MODEBUFLEN
- 5))
840 sendto_one_numeric(source_p
, ERR_INVALIDBAN
,
841 form_str(ERR_INVALIDBAN
),
842 chptr
->chname
, c
, arg
);
846 /* Look for a $ after the first character.
847 * As the first character, it marks an extban; afterwards
848 * it delimits a forward channel.
850 if ((forward
= strchr(mask
+1, '$')) != NULL
)
853 if (*forward
== '\0')
857 /* if we're adding a NEW id */
860 if (*mask
== '$' && MyClient(source_p
))
862 if (!valid_extban(mask
, source_p
, chptr
, mode_type
))
864 sendto_one_numeric(source_p
, ERR_INVALIDBAN
,
865 form_str(ERR_INVALIDBAN
),
866 chptr
->chname
, c
, arg
);
871 /* For compatibility, only check the forward channel from
872 * local clients. Accept any forward channel from servers.
874 if (forward
!= NULL
&& MyClient(source_p
))
876 /* For simplicity and future flexibility, do not
877 * allow '$' in forwarding targets.
879 if (!ConfigChannel
.use_forward
||
880 strchr(forward
, '$') != NULL
)
882 sendto_one_numeric(source_p
, ERR_INVALIDBAN
,
883 form_str(ERR_INVALIDBAN
),
884 chptr
->chname
, c
, arg
);
887 /* check_forward() sends its own error message */
888 if (!check_forward(source_p
, chptr
, forward
))
890 /* Forwards only make sense for bans. */
891 if (mode_type
!= CHFL_BAN
)
893 sendto_one_numeric(source_p
, ERR_INVALIDBAN
,
894 form_str(ERR_INVALIDBAN
),
895 chptr
->chname
, c
, arg
);
900 /* dont allow local clients to overflow the banlist, dont
901 * let remote servers set duplicate bans
903 if (!add_id(source_p
, chptr
, mask
, forward
, list
, mode_type
))
909 mode_changes
[mode_count
].letter
= c
;
910 mode_changes
[mode_count
].dir
= MODE_ADD
;
911 mode_changes
[mode_count
].mems
= mems
;
912 mode_changes
[mode_count
].id
= NULL
;
913 mode_changes
[mode_count
++].arg
= mask
;
915 else if (dir
== MODE_DEL
)
918 static char buf
[BANLEN
* MAXMODEPARAMS
];
919 int old_removed_mask_pos
= removed_mask_pos
;
920 if ((removed
= del_id(chptr
, mask
, list
, mode_type
)) == NULL
)
922 /* mask isn't a valid ban, check arg */
923 if ((removed
= del_id(chptr
, arg
, list
, mode_type
)) != NULL
)
927 if (removed
&& removed
->forward
)
928 removed_mask_pos
+= snprintf(buf
+ old_removed_mask_pos
, sizeof(buf
) - old_removed_mask_pos
, "%s$%s", removed
->banstr
, removed
->forward
) + 1;
930 removed_mask_pos
+= rb_strlcpy(buf
+ old_removed_mask_pos
, removed
? removed
->banstr
: mask
, sizeof(buf
)) + 1;
937 mode_changes
[mode_count
].letter
= c
;
938 mode_changes
[mode_count
].dir
= MODE_DEL
;
939 mode_changes
[mode_count
].mems
= mems
;
940 mode_changes
[mode_count
].id
= NULL
;
941 mode_changes
[mode_count
++].arg
= buf
+ old_removed_mask_pos
;
946 chm_op(struct Client
*source_p
, struct Channel
*chptr
,
947 int alevel
, const char *arg
, int *errors
, int dir
, char c
, long mode_type
)
949 struct membership
*mstptr
;
950 struct Client
*targ_p
;
952 if(!allow_mode_change(source_p
, chptr
, alevel
, errors
, c
))
958 sendto_one_numeric(source_p
, ERR_NOSUCHNICK
, form_str(ERR_NOSUCHNICK
), "*");
962 if((targ_p
= find_chasing(source_p
, arg
, NULL
)) == NULL
)
967 mstptr
= find_channel_membership(chptr
, targ_p
);
971 if(!(*errors
& SM_ERR_NOTONCHANNEL
) && MyClient(source_p
))
972 sendto_one_numeric(source_p
, ERR_USERNOTINCHANNEL
,
973 form_str(ERR_USERNOTINCHANNEL
), arg
, chptr
->chname
);
974 *errors
|= SM_ERR_NOTONCHANNEL
;
980 if(targ_p
== source_p
&& mstptr
->flags
& CHFL_CHANOP
)
983 mode_changes
[mode_count
].letter
= c
;
984 mode_changes
[mode_count
].dir
= MODE_ADD
;
985 mode_changes
[mode_count
].mems
= ALL_MEMBERS
;
986 mode_changes
[mode_count
].id
= targ_p
->id
;
987 mode_changes
[mode_count
++].arg
= targ_p
->name
;
989 mstptr
->flags
|= CHFL_CHANOP
;
993 if(MyClient(source_p
) && IsService(targ_p
))
995 sendto_one(source_p
, form_str(ERR_ISCHANSERVICE
),
996 me
.name
, source_p
->name
, targ_p
->name
, chptr
->chname
);
1000 mode_changes
[mode_count
].letter
= c
;
1001 mode_changes
[mode_count
].dir
= MODE_DEL
;
1002 mode_changes
[mode_count
].mems
= ALL_MEMBERS
;
1003 mode_changes
[mode_count
].id
= targ_p
->id
;
1004 mode_changes
[mode_count
++].arg
= targ_p
->name
;
1006 mstptr
->flags
&= ~CHFL_CHANOP
;
1011 chm_voice(struct Client
*source_p
, struct Channel
*chptr
,
1012 int alevel
, const char *arg
, int *errors
, int dir
, char c
, long mode_type
)
1014 struct membership
*mstptr
;
1015 struct Client
*targ_p
;
1017 if(!allow_mode_change(source_p
, chptr
, alevel
, errors
, c
))
1021 if(EmptyString(arg
))
1023 sendto_one_numeric(source_p
, ERR_NOSUCHNICK
, form_str(ERR_NOSUCHNICK
), "*");
1027 if((targ_p
= find_chasing(source_p
, arg
, NULL
)) == NULL
)
1032 mstptr
= find_channel_membership(chptr
, targ_p
);
1036 if(!(*errors
& SM_ERR_NOTONCHANNEL
) && MyClient(source_p
))
1037 sendto_one_numeric(source_p
, ERR_USERNOTINCHANNEL
,
1038 form_str(ERR_USERNOTINCHANNEL
), arg
, chptr
->chname
);
1039 *errors
|= SM_ERR_NOTONCHANNEL
;
1045 mode_changes
[mode_count
].letter
= c
;
1046 mode_changes
[mode_count
].dir
= MODE_ADD
;
1047 mode_changes
[mode_count
].mems
= ALL_MEMBERS
;
1048 mode_changes
[mode_count
].id
= targ_p
->id
;
1049 mode_changes
[mode_count
++].arg
= targ_p
->name
;
1051 mstptr
->flags
|= CHFL_VOICE
;
1055 mode_changes
[mode_count
].letter
= 'v';
1056 mode_changes
[mode_count
].dir
= MODE_DEL
;
1057 mode_changes
[mode_count
].mems
= ALL_MEMBERS
;
1058 mode_changes
[mode_count
].id
= targ_p
->id
;
1059 mode_changes
[mode_count
++].arg
= targ_p
->name
;
1061 mstptr
->flags
&= ~CHFL_VOICE
;
1066 chm_limit(struct Client
*source_p
, struct Channel
*chptr
,
1067 int alevel
, const char *arg
, int *errors
, int dir
, char c
, long mode_type
)
1069 static char limitstr
[30];
1072 if (!allow_mode_change(source_p
, chptr
, alevel
, errors
, c
))
1075 if (dir
== MODE_ADD
)
1077 if (EmptyString(arg
) || (limit
= atoi(arg
)) <= 0)
1080 sprintf(limitstr
, "%d", limit
);
1082 mode_changes
[mode_count
].letter
= c
;
1083 mode_changes
[mode_count
].dir
= MODE_ADD
;
1084 mode_changes
[mode_count
].mems
= ALL_MEMBERS
;
1085 mode_changes
[mode_count
].id
= NULL
;
1086 mode_changes
[mode_count
++].arg
= limitstr
;
1088 chptr
->mode
.limit
= limit
;
1090 else if (dir
== MODE_DEL
)
1092 if(!chptr
->mode
.limit
)
1095 chptr
->mode
.limit
= 0;
1097 mode_changes
[mode_count
].letter
= c
;
1098 mode_changes
[mode_count
].dir
= MODE_DEL
;
1099 mode_changes
[mode_count
].mems
= ALL_MEMBERS
;
1100 mode_changes
[mode_count
].id
= NULL
;
1101 mode_changes
[mode_count
++].arg
= NULL
;
1106 chm_throttle(struct Client
*source_p
, struct Channel
*chptr
,
1107 int alevel
, const char *arg
, int *errors
, int dir
, char c
, long mode_type
)
1109 int joins
= 0, timeslice
= 0;
1111 if (!allow_mode_change(source_p
, chptr
, alevel
, errors
, c
))
1114 if (dir
== MODE_ADD
)
1116 if (sscanf(arg
, "%d:%d", &joins
, ×lice
) < 2)
1119 if(joins
<= 0 || timeslice
<= 0)
1122 mode_changes
[mode_count
].letter
= c
;
1123 mode_changes
[mode_count
].dir
= MODE_ADD
;
1124 mode_changes
[mode_count
].mems
= ALL_MEMBERS
;
1125 mode_changes
[mode_count
].id
= NULL
;
1126 mode_changes
[mode_count
++].arg
= arg
;
1128 chptr
->mode
.join_num
= joins
;
1129 chptr
->mode
.join_time
= timeslice
;
1131 else if(dir
== MODE_DEL
)
1133 if(!chptr
->mode
.join_num
)
1136 chptr
->mode
.join_num
= 0;
1137 chptr
->mode
.join_time
= 0;
1138 chptr
->join_count
= 0;
1139 chptr
->join_delta
= 0;
1141 mode_changes
[mode_count
].letter
= c
;
1142 mode_changes
[mode_count
].dir
= MODE_DEL
;
1143 mode_changes
[mode_count
].mems
= ALL_MEMBERS
;
1144 mode_changes
[mode_count
].id
= NULL
;
1145 mode_changes
[mode_count
++].arg
= NULL
;
1150 chm_forward(struct Client
*source_p
, struct Channel
*chptr
,
1151 int alevel
, const char *arg
, int *errors
, int dir
, char c
, long mode_type
)
1153 /* if +f is disabled, ignore local attempts to set it */
1154 if (!ConfigChannel
.use_forward
&& MyClient(source_p
) && dir
== MODE_ADD
)
1157 if (dir
== MODE_QUERY
)
1159 if (!(*errors
& SM_ERR_RPL_F
))
1161 if (*chptr
->mode
.forward
== '\0')
1162 sendto_one_notice(source_p
, ":%s has no forward channel", chptr
->chname
);
1164 sendto_one_notice(source_p
, ":%s forward channel is %s", chptr
->chname
, chptr
->mode
.forward
);
1165 *errors
|= SM_ERR_RPL_F
;
1170 #ifndef FORWARD_OPERONLY
1171 if (!allow_mode_change(source_p
, chptr
, alevel
, errors
, c
))
1174 if (!IsOperGeneral(source_p
) && !IsServer(source_p
))
1176 if(!(*errors
& SM_ERR_NOPRIVS
))
1177 sendto_one_numeric(source_p
, ERR_NOPRIVILEGES
, form_str(ERR_NOPRIVILEGES
));
1178 *errors
|= SM_ERR_NOPRIVS
;
1183 if (dir
== MODE_ADD
)
1185 if(EmptyString(arg
))
1188 if(!check_forward(source_p
, chptr
, arg
))
1191 rb_strlcpy(chptr
->mode
.forward
, arg
, sizeof(chptr
->mode
.forward
));
1193 mode_changes
[mode_count
].letter
= c
;
1194 mode_changes
[mode_count
].dir
= MODE_ADD
;
1195 mode_changes
[mode_count
].mems
=
1196 ConfigChannel
.use_forward
? ALL_MEMBERS
: ONLY_SERVERS
;
1197 mode_changes
[mode_count
].id
= NULL
;
1198 mode_changes
[mode_count
++].arg
= arg
;
1200 else if(dir
== MODE_DEL
)
1202 if(!(*chptr
->mode
.forward
))
1205 *chptr
->mode
.forward
= '\0';
1207 mode_changes
[mode_count
].letter
= c
;
1208 mode_changes
[mode_count
].dir
= MODE_DEL
;
1209 mode_changes
[mode_count
].mems
= ALL_MEMBERS
;
1210 mode_changes
[mode_count
].id
= NULL
;
1211 mode_changes
[mode_count
++].arg
= NULL
;
1216 chm_key(struct Client
*source_p
, struct Channel
*chptr
,
1217 int alevel
, const char *arg
, int *errors
, int dir
, char c
, long mode_type
)
1221 if (!allow_mode_change(source_p
, chptr
, alevel
, errors
, c
))
1224 if (dir
== MODE_ADD
)
1226 key
= LOCAL_COPY(arg
);
1228 if(MyClient(source_p
))
1231 fix_key_remote(key
);
1233 if(EmptyString(key
))
1236 s_assert(key
[0] != ' ');
1237 rb_strlcpy(chptr
->mode
.key
, key
, sizeof(chptr
->mode
.key
));
1239 mode_changes
[mode_count
].letter
= c
;
1240 mode_changes
[mode_count
].dir
= MODE_ADD
;
1241 mode_changes
[mode_count
].mems
= ALL_MEMBERS
;
1242 mode_changes
[mode_count
].id
= NULL
;
1243 mode_changes
[mode_count
++].arg
= chptr
->mode
.key
;
1245 else if(dir
== MODE_DEL
)
1247 static char splat
[] = "*";
1250 if(!(*chptr
->mode
.key
))
1253 /* hack time. when we get a +k-k mode, the +k arg is
1254 * chptr->mode.key, which the -k sets to \0, so hunt for a
1255 * +k when we get a -k, and set the arg to splat. --anfl
1257 for(i
= 0; i
< mode_count
; i
++)
1259 if(mode_changes
[i
].letter
== 'k' && mode_changes
[i
].dir
== MODE_ADD
)
1260 mode_changes
[i
].arg
= splat
;
1263 *chptr
->mode
.key
= 0;
1265 mode_changes
[mode_count
].letter
= c
;
1266 mode_changes
[mode_count
].dir
= MODE_DEL
;
1267 mode_changes
[mode_count
].mems
= ALL_MEMBERS
;
1268 mode_changes
[mode_count
].id
= NULL
;
1269 mode_changes
[mode_count
++].arg
= "*";
1274 struct ChannelMode chmode_table
[256] =
1276 ['F'] = {chm_simple
, MODE_FREETARGET
, 0 },
1277 ['I'] = {chm_ban
, CHFL_INVEX
, CHM_QUERYABLE
| CHM_OPS_QUERY
},
1278 ['L'] = {chm_staff
, MODE_EXLIMIT
, 0 },
1279 ['P'] = {chm_staff
, MODE_PERMANENT
, 0 },
1280 ['Q'] = {chm_simple
, MODE_DISFORWARD
, 0 },
1281 ['b'] = {chm_ban
, CHFL_BAN
, CHM_QUERYABLE
},
1282 ['e'] = {chm_ban
, CHFL_EXCEPTION
, CHM_QUERYABLE
| CHM_OPS_QUERY
},
1283 ['f'] = {chm_forward
, 0, CHM_ARG_SET
| CHM_CAN_QUERY
}, /* weird because it's nonstandard and violates isupport */
1284 ['g'] = {chm_simple
, MODE_FREEINVITE
, 0 },
1285 ['i'] = {chm_simple
, MODE_INVITEONLY
, 0 },
1286 ['j'] = {chm_throttle
, 0, CHM_ARG_SET
},
1287 ['k'] = {chm_key
, 0, CHM_QUERYABLE
},
1288 ['l'] = {chm_limit
, 0, CHM_ARG_SET
},
1289 ['m'] = {chm_simple
, MODE_MODERATED
, 0 },
1290 ['n'] = {chm_simple
, MODE_NOPRIVMSGS
, 0 },
1291 ['o'] = {chm_op
, 0, CHM_ARGS
},
1292 ['p'] = {chm_simple
, MODE_PRIVATE
, 0 },
1293 ['q'] = {chm_ban
, CHFL_QUIET
, CHM_QUERYABLE
},
1294 ['r'] = {chm_simple
, MODE_REGONLY
, 0 },
1295 ['s'] = {chm_simple
, MODE_SECRET
, 0 },
1296 ['t'] = {chm_simple
, MODE_TOPICLIMIT
, 0 },
1297 ['v'] = {chm_voice
, 0, CHM_ARGS
},
1298 ['z'] = {chm_simple
, MODE_OPMODERATE
, 0 },
1303 /* set_channel_mode()
1305 * inputs - client, source, channel, membership pointer, params
1307 * side effects - channel modes/memberships are changed, MODE is issued
1309 * Extensively modified to be hotpluggable, 03/09/06 -- nenolod
1312 set_channel_mode(struct Client
*client_p
, struct Client
*source_p
,
1313 struct Channel
*chptr
, struct membership
*msptr
, int parc
, const char *parv
[])
1315 static char modebuf
[BUFSIZE
* 2]; /* paranoid case: 2 canonical chars per input char */
1316 static char parabuf
[BUFSIZE
];
1319 int cur_len
, mlen
, paralen
, paracount
, arglen
, len
;
1322 bool changes
= false;
1323 bool privileged_query
= false;
1327 const char *ml
= parv
[0];
1329 struct Client
*fakesource_p
;
1330 int flags_list
[3] = { ALL_MEMBERS
, ONLY_CHANOPS
, ONLY_OPERS
};
1332 int mode_limit_simple
= 0;
1335 removed_mask_pos
= 0;
1338 /* Hide connecting server on netburst -- jilles */
1339 if (ConfigServerHide
.flatten_links
&& IsServer(source_p
) && !has_id(source_p
) && !HasSentEob(source_p
))
1342 fakesource_p
= source_p
;
1345 const struct ChannelMode
*cm
;
1351 static struct modeset modesets
[MAXMODEPARAMS
+ MAXMODES_SIMPLE
];
1352 struct modeset
*ms
= modesets
, *mend
;
1353 char canon_op
= '\0';
1357 for (ml
= parv
[0]; *ml
!= 0 && ms
- modesets
< ARRAY_SIZE(modesets
); ml
++)
1373 int effective_dir
= dir
;
1374 const struct ChannelMode
*cm
= &chmode_table
[(unsigned char) c
];
1375 bool use_arg
= dir
== MODE_ADD
? cm
->flags
& CHM_ARG_SET
:
1376 dir
== MODE_DEL
? cm
->flags
& CHM_ARG_DEL
:
1378 if (cm
->set_func
== NULL
)
1380 sendto_one(source_p
, form_str(ERR_UNKNOWNMODE
), me
.name
, source_p
->name
, c
);
1383 if (use_arg
&& parn
>= parc
)
1385 if (!(cm
->flags
& CHM_CAN_QUERY
))
1387 sendto_one(source_p
, form_str(ERR_NEEDMOREPARAMS
), me
.name
, source_p
->name
, "MODE");
1390 effective_dir
= MODE_QUERY
;
1394 if (effective_dir
== MODE_QUERY
&& !(cm
->flags
& CHM_CAN_QUERY
))
1396 /* XXX this currently replicates traditional behaviour and just
1397 * does nothing for a query on a mode with no query. would it be
1398 * good to send an error here?
1403 if (MyClient(source_p
))
1405 if (use_arg
&& ++mode_limit
> MAXMODEPARAMS
)
1407 if (!use_arg
&& ++mode_limit_simple
> MAXMODES_SIMPLE
)
1411 char op
= effective_dir
== MODE_ADD
? '+' :
1412 effective_dir
== MODE_DEL
? '-' :
1416 *mbuf
++ = canon_op
= op
;
1420 if (effective_dir
!= MODE_QUERY
)
1422 if (effective_dir
== MODE_QUERY
&& cm
->flags
& CHM_OPS_QUERY
)
1423 privileged_query
= true;
1426 ms
->dir
= effective_dir
;
1428 ms
->arg
= parv
[parn
++];
1437 /* this will happen on something like MODE +-=++-.
1438 * we'd have caught that with the if !mode_count
1439 * later on, but this saves an override notice
1446 /* XXX we could reject excess params here */
1449 /* Finish the flood grace period if we were asked to do anything */
1450 if (changes
&& MyClient(source_p
) && !IsFloodDone(source_p
))
1452 flood_endgrace(source_p
);
1460 rb_strlcat(modebuf
, reconstruct_parv(parn
- 1, parv
+ 1), sizeof modebuf
);
1466 int access_dir
= privileged_query
? MODE_OP_QUERY
:
1467 changes
? MODE_ADD
:
1469 alevel
= get_channel_access(source_p
, chptr
, msptr
, access_dir
, modebuf
);
1471 for (ms
= modesets
; ms
< mend
; ms
++)
1473 ChannelModeFunc
*set_func
= ms
->cm
->set_func
;
1474 set_func(fakesource_p
, chptr
, alevel
, ms
->arg
, &errors
, ms
->dir
, ms
->mode
, ms
->cm
->mode_type
);
1477 /* bail out if we have nothing to do... */
1481 if (IsServer(source_p
))
1482 mlen
= sprintf(modebuf
, ":%s MODE %s ", fakesource_p
->name
, chptr
->chname
);
1484 mlen
= sprintf(modebuf
, ":%s!%s@%s MODE %s ",
1485 source_p
->name
, source_p
->username
,
1486 source_p
->host
, chptr
->chname
);
1488 for(j
= 0; j
< 3; j
++)
1490 int send_flags
= flags
= flags_list
[j
];
1491 const char *priv
= NULL
;
1492 if (flags
== ONLY_OPERS
)
1494 send_flags
= ALL_MEMBERS
;
1495 priv
= "auspex:cmodes";
1498 mbuf
= modebuf
+ mlen
;
1501 paracount
= paralen
= 0;
1504 for(i
= 0; i
< mode_count
; i
++)
1506 if(mode_changes
[i
].letter
== 0 || mode_changes
[i
].mems
!= flags
)
1509 if(mode_changes
[i
].arg
!= NULL
)
1511 arglen
= strlen(mode_changes
[i
].arg
);
1513 if(arglen
> MODEBUFLEN
- 5)
1519 /* if we're creeping over MAXMODEPARAMSSERV, or over
1520 * bufsize (4 == +/-,modechar,two spaces) send now.
1522 if(mode_changes
[i
].arg
!= NULL
&&
1523 ((paracount
== MAXMODEPARAMSSERV
) ||
1524 ((cur_len
+ paralen
+ arglen
+ 4) > (BUFSIZE
- 3))))
1529 sendto_channel_local_priv(IsServer(source_p
) ? fakesource_p
: source_p
,
1530 send_flags
, priv
, chptr
, "%s %s", modebuf
, parabuf
);
1534 paracount
= paralen
= 0;
1536 mbuf
= modebuf
+ mlen
;
1542 if(dir
!= mode_changes
[i
].dir
)
1544 *mbuf
++ = (mode_changes
[i
].dir
== MODE_ADD
) ? '+' : '-';
1546 dir
= mode_changes
[i
].dir
;
1549 *mbuf
++ = mode_changes
[i
].letter
;
1552 if(mode_changes
[i
].arg
!= NULL
)
1555 len
= sprintf(pbuf
, "%s ", mode_changes
[i
].arg
);
1561 if(paralen
&& parabuf
[paralen
- 1] == ' ')
1562 parabuf
[paralen
- 1] = '\0';
1566 sendto_channel_local_priv(IsServer(source_p
) ? fakesource_p
: source_p
,
1567 send_flags
, priv
, chptr
, "%s %s", modebuf
, parabuf
);
1570 /* only propagate modes originating locally, or if we're hubbing */
1571 if(MyClient(source_p
) || rb_dlink_list_length(&serv_list
) > 1)
1572 send_cap_mode_changes(client_p
, source_p
, chptr
, mode_changes
, mode_count
);
1575 /* set_channel_mlock()
1577 * inputs - client, source, channel, params
1579 * side effects - channel mlock is changed / MLOCK is propagated
1582 set_channel_mlock(struct Client
*client_p
, struct Client
*source_p
,
1583 struct Channel
*chptr
, const char *newmlock
, bool propagate
)
1585 rb_free(chptr
->mode_lock
);
1586 chptr
->mode_lock
= newmlock
? rb_strdup(newmlock
) : NULL
;
1590 sendto_server(client_p
, NULL
, CAP_TS6
| CAP_MLOCK
, NOCAPS
, ":%s MLOCK %ld %s :%s",
1591 source_p
->id
, (long) chptr
->channelts
, chptr
->chname
,
1592 chptr
->mode_lock
? chptr
->mode_lock
: "");