2 * IRC - Internet Relay Chat, ircd/m_silence.c
3 * Copyright (C) 1990 Jarkko Oikarinen and
4 * University of Oulu, Computing Center
6 * See file AUTHORS in IRC package for additional names of
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 1, or (at your option)
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 * @brief Handlers for SILENCE command.
34 #include "ircd_features.h"
36 #include "ircd_reply.h"
37 #include "ircd_snprintf.h"
38 #include "ircd_string.h"
47 /* #include <assert.h> -- Now using assert in ircd_log.h */
51 /** Attempt to apply a SILENCE update to a user.
53 * Silences are propagated lazily between servers to save on bandwidth
54 * and remote memory. Any removal and any silence exception must be
55 * propagated until a server has not seen the mask being removed or
56 * has no positive silences for the user.
58 * @param[in] sptr Client to update.
59 * @param[in] mask Single silence mask to apply, optionally preceded by '+' or '-' and maybe '~'.
60 * @return The new ban entry on success, NULL on failure.
63 apply_silence(struct Client *sptr, char *mask)
67 char orig_mask[NICKLEN+USERLEN+HOSTLEN+3];
69 assert(mask && mask[0]);
71 /* Check for add or remove. */
75 } else if (mask[0] == '+') {
81 /* Check for being an exception. */
83 flags |= BAN_EXCEPTION;
87 /* Make the silence and set additional flags. */
88 ircd_strncpy(orig_mask, mask, sizeof(orig_mask) - 1);
89 sile = make_ban(pretty_mask(mask));
92 /* If they're a local user trying to ban too broad a mask, forbid it. */
94 && (sile->flags & BAN_IPMASK)
96 && sile->addrbits < (irc_in_addr_is_ipv4(&sile->address) ? 112 : 32)) {
97 send_reply(sptr, ERR_MASKTOOWIDE, orig_mask);
102 /* Apply it to the silence list. */
103 return apply_ban(&cli_user(sptr)->silence, sile, 1) ? NULL : sile;
106 /** Apply and send silence updates for a user.
107 * @param[in] sptr Client whose silence list has been updated.
108 * @param[in] silences Comma-separated list of silence updates.
109 * @param[in] dest Direction to send updates in (NULL for broadcast).
112 forward_silences(struct Client *sptr, char *silences, struct Client *dest)
114 struct Ban *accepted[MAXPARA], *sile, **plast;
115 char *cp, *p, buf[BUFSIZE];
116 size_t ac_count, buf_used, slen, ii;
118 /* Split the list of silences and try to apply each one in turn. */
119 for (cp = ircd_strtok(&p, silences, ","), ac_count = 0;
120 cp && (ac_count < MAXPARA);
121 cp = ircd_strtok(&p, 0, ",")) {
122 if ((sile = apply_silence(sptr, cp)))
123 accepted[ac_count++] = sile;
128 size_t siles, maxsiles, totlength, maxlength, jj;
130 /* Check that silence count and total length are permitted. */
131 maxsiles = feature_int(FEAT_MAXSILES);
132 maxlength = maxsiles * feature_int(FEAT_AVBANLEN);
133 siles = totlength = 0;
134 /* Count number of current silences and their total length. */
135 plast = &cli_user(sptr)->silence;
136 for (sile = cli_user(sptr)->silence; sile; sile = sile->next) {
137 if (sile->flags & (BAN_OVERLAPPED | BAN_ADD | BAN_DEL))
140 totlength += strlen(sile->banstr);
143 for (ii = jj = 0; ii < ac_count; ++ii) {
145 /* If the update is being added, and we would exceed the maximum
146 * count or length, drop the update.
148 if (!(sile->flags & (BAN_OVERLAPPED | BAN_DEL))) {
149 slen = strlen(sile->banstr);
150 if ((siles >= maxsiles) || (totlength + slen >= maxlength)) {
153 send_reply(sptr, ERR_SILELISTFULL, accepted[ii]->banstr);
154 free_ban(accepted[ii]);
162 /* Store the update. */
163 accepted[jj++] = sile;
165 /* Write back the number of accepted updates. */
168 /* Send the silence update list, including overlapped silences (to
169 * make it easier on clients).
172 for (sile = cli_user(sptr)->silence; sile; sile = sile->next) {
174 if (sile->flags & (BAN_OVERLAPPED | BAN_DEL))
176 else if (sile->flags & BAN_ADD)
180 slen = strlen(sile->banstr);
181 if (buf_used + slen + 4 > 400) {
182 buf[buf_used] = '\0';
183 sendcmdto_one(sptr, CMD_SILENCE, sptr, "%s", buf);
187 buf[buf_used++] = ',';
188 buf[buf_used++] = ch;
189 if (sile->flags & BAN_EXCEPTION)
190 buf[buf_used++] = '~';
191 memcpy(buf + buf_used, sile->banstr, slen);
195 buf[buf_used] = '\0';
196 sendcmdto_one(sptr, CMD_SILENCE, sptr, "%s", buf);
201 /* Forward any silence removals or exceptions updates to other
202 * servers if the user has positive silences.
204 if (!dest || !MyUser(dest)) {
205 for (ii = buf_used = 0; ii < ac_count; ++ii) {
208 if (sile->flags & BAN_OVERLAPPED)
210 else if (sile->flags & BAN_DEL)
212 else if (sile->flags & BAN_ADD) {
213 if (!(sile->flags & BAN_EXCEPTION))
218 slen = strlen(sile->banstr);
219 if (buf_used + slen + 4 > 400) {
220 buf[buf_used] = '\0';
222 sendcmdto_one(sptr, CMD_SILENCE, dest, "%C %s", dest, buf);
224 sendcmdto_serv_butone(sptr, CMD_SILENCE, sptr, "* %s", buf);
228 buf[buf_used++] = ',';
229 buf[buf_used++] = ch;
230 if (sile->flags & BAN_EXCEPTION)
231 buf[buf_used++] = '~';
232 memcpy(buf + buf_used, sile->banstr, slen);
236 buf[buf_used] = '\0';
238 sendcmdto_one(sptr, CMD_SILENCE, dest, "%C %s", dest, buf);
240 sendcmdto_serv_butone(sptr, CMD_SILENCE, sptr, "* %s", buf);
245 /* Remove overlapped and deleted silences from the user's silence
246 * list. Clear BAN_ADD since we're walking the list anyway.
248 for (plast = &cli_user(sptr)->silence; (sile = *plast) != NULL; ) {
249 if (sile->flags & (BAN_OVERLAPPED | BAN_DEL)) {
253 sile->flags &= ~BAN_ADD;
259 /* Free any silence-deleting updates. */
260 for (ii = 0; ii < ac_count; ++ii) {
261 if (accepted[ii]->flags & BAN_DEL)
262 free_ban(accepted[ii]);
266 /** Handle a SILENCE command from a local user.
267 * See @ref m_functions for general discussion of parameters.
269 * \a parv[1] may be any of the following:
270 * \li Omitted or empty, to view your own silence list.
271 * \li Nickname of a user, to view that user's silence list.
272 * \li A comma-separated list of silence updates
274 * @param[in] cptr Client that sent us the message.
275 * @param[in] sptr Original source of message.
276 * @param[in] parc Number of arguments.
277 * @param[in] parv Argument vector.
279 int m_silence(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
281 struct Client *acptr;
285 assert(cptr == sptr);
287 /* See if the user is requesting a silence list. */
289 if (parc < 2 || EmptyString(parv[1]) || (acptr = FindUser(parv[1]))) {
290 if (cli_user(acptr)) {
291 for (sile = cli_user(acptr)->silence; sile; sile = sile->next) {
292 send_reply(sptr, RPL_SILELIST, cli_name(acptr),
293 (sile->flags & BAN_EXCEPTION ? "~" : ""), sile->banstr);
296 send_reply(sptr, RPL_ENDOFSILELIST, cli_name(acptr));
300 /* The user must be attempting to update their list. */
301 forward_silences(sptr, parv[1], NULL);
305 /** Handle a SILENCE command from a server.
306 * See @ref m_functions for general discussion of parameters.
308 * \a parv[1] may be one of the following:
309 * \li "*" to indicate a broadcast update (removing a SILENCE)
310 * \li A client numnick that should be specifically SILENCEd.
312 * \a parv[2] is a comma-separated list of silence updates.
314 * @param[in] cptr Client that sent us the message.
315 * @param[in] sptr Original source of message.
316 * @param[in] parc Number of arguments.
317 * @param[in] parv Argument vector.
319 int ms_silence(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
322 return protocol_violation(sptr, "Server trying to silence a user");
323 if (parc < 3 || EmptyString(parv[2]))
324 return need_more_params(sptr, "SILENCE");
326 /* Figure out which silences can be forwarded. */
327 forward_silences(sptr, parv[2], findNUser(parv[1]));