4530d6bcb7d84c96e983c7768fe0f29fb745b71b
[ircu2.10.12-pk.git] / ircd / m_silence.c
1 /*
2  * IRC - Internet Relay Chat, ircd/m_silence.c
3  * Copyright (C) 1990 Jarkko Oikarinen and
4  *                    University of Oulu, Computing Center
5  *
6  * See file AUTHORS in IRC package for additional names of
7  * the programmers.
8  *
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)
12  * any later version.
13  *
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.
18  *
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.
22  */
23 /** @file
24  * @brief Handlers for SILENCE command.
25  * @version $Id$
26  */
27
28 #include "config.h"
29
30 #include "channel.h"
31 #include "client.h"
32 #include "hash.h"
33 #include "ircd.h"
34 #include "ircd_features.h"
35 #include "ircd_log.h"
36 #include "ircd_reply.h"
37 #include "ircd_snprintf.h"
38 #include "ircd_string.h"
39 #include "list.h"
40 #include "msg.h"
41 #include "numeric.h"
42 #include "numnicks.h"
43 #include "s_user.h"
44 #include "send.h"
45 #include "struct.h"
46
47 /* #include <assert.h> -- Now using assert in ircd_log.h */
48 #include <stdlib.h>
49 #include <string.h>
50
51 /** Attempt to apply a SILENCE update to a user.
52  *
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.
57  *
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.
61  */
62 static struct Ban *
63 apply_silence(struct Client *sptr, char *mask)
64 {
65   struct Ban *sile;
66   int flags;
67   char orig_mask[NICKLEN+USERLEN+HOSTLEN+3];
68
69   assert(mask && mask[0]);
70
71   /* Check for add or remove. */
72   if (mask[0] == '-') {
73     flags = BAN_DEL;
74     mask++;
75   } else if (mask[0] == '+') {
76     flags = BAN_ADD;
77     mask++;
78   } else
79     flags = BAN_ADD;
80
81   /* Check for being an exception. */
82   if (mask[0] == '~') {
83     flags |= BAN_EXCEPTION;
84     mask++;
85   }
86
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));
90   sile->flags |= flags;
91
92   /* If they're a local user trying to ban too broad a mask, forbid it. */
93   if (MyUser(sptr)
94       && (sile->flags & BAN_IPMASK)
95       && sile->addrbits > 0
96       && sile->addrbits < (irc_in_addr_is_ipv4(&sile->address) ? 112 : 32)) {
97     send_reply(sptr, ERR_MASKTOOWIDE, orig_mask);
98     free_ban(sile);
99     return NULL;
100   }
101
102   /* Apply it to the silence list. */
103   return apply_ban(&cli_user(sptr)->silence, sile, 1) ? NULL : sile;
104 }
105
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).
110  */
111 static void
112 forward_silences(struct Client *sptr, char *silences, struct Client *dest)
113 {
114   struct Ban *accepted[MAXPARA], *sile, **plast;
115   char *cp, *p, buf[BUFSIZE];
116   size_t ac_count, buf_used, slen, ii;
117
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;
124   }
125
126
127   if (MyUser(sptr)) {
128     size_t siles, maxsiles, totlength, maxlength, jj;
129
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))
138         continue;
139       siles++;
140       totlength += strlen(sile->banstr);
141       plast = &sile->next;
142     }
143     for (ii = jj = 0; ii < ac_count; ++ii) {
144       sile = accepted[ii];
145       /* If the update is being added, and we would exceed the maximum
146        * count or length, drop the update.
147        */
148       if (!(sile->flags & (BAN_OVERLAPPED | BAN_DEL))) {
149         slen = strlen(sile->banstr);
150         if ((siles >= maxsiles) || (totlength + slen >= maxlength)) {
151           *plast = NULL;
152           if (MyUser(sptr))
153             send_reply(sptr, ERR_SILELISTFULL, accepted[ii]->banstr);
154           free_ban(accepted[ii]);
155           continue;
156         }
157         /* Update counts. */
158         siles++;
159         totlength += slen;
160         plast = &sile->next;
161       }
162       /* Store the update. */
163       accepted[jj++] = sile;
164     }
165     /* Write back the number of accepted updates. */
166     ac_count = jj;
167
168     /* Send the silence update list, including overlapped silences (to
169      * make it easier on clients).
170      */
171     buf_used = 0;
172     for (sile = cli_user(sptr)->silence; sile; sile = sile->next) {
173       char ch;
174       if (sile->flags & (BAN_OVERLAPPED | BAN_DEL))
175         ch = '-';
176       else if (sile->flags & BAN_ADD)
177         ch = '+';
178       else
179         continue;
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);
184         buf_used = 0;
185       }
186       if (buf_used)
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);
192       buf_used += slen;
193     }
194     if (buf_used > 0) {
195         buf[buf_used] = '\0';
196         sendcmdto_one(sptr, CMD_SILENCE, sptr, "%s", buf);
197         buf_used = 0;
198     }
199   }
200
201   /* Forward any silence removals or exceptions updates to other
202    * servers if the user has positive silences.
203    */
204   if (!dest || !MyUser(dest)) {
205     for (ii = buf_used = 0; ii < ac_count; ++ii) {
206       char ch;
207       sile = accepted[ii];
208       if (sile->flags & BAN_OVERLAPPED)
209         continue;
210       else if (sile->flags & BAN_DEL)
211         ch = '-';
212       else if (sile->flags & BAN_ADD) {
213         if (!(sile->flags & BAN_EXCEPTION))
214           continue;
215         ch = '+';
216       } else
217         continue;
218       slen = strlen(sile->banstr);
219       if (buf_used + slen + 4 > 400) {
220         buf[buf_used] = '\0';
221         if (dest)
222           sendcmdto_one(sptr, CMD_SILENCE, dest, "%C %s", dest, buf);
223         else
224           sendcmdto_serv_butone(sptr, CMD_SILENCE, sptr, "* %s", buf);
225         buf_used = 0;
226       }
227       if (buf_used)
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);
233       buf_used += slen;
234     }
235     if (buf_used > 0) {
236         buf[buf_used] = '\0';
237         if (dest)
238           sendcmdto_one(sptr, CMD_SILENCE, dest, "%C %s", dest, buf);
239         else
240           sendcmdto_serv_butone(sptr, CMD_SILENCE, sptr, "* %s", buf);
241         buf_used = 0;
242     }
243   }
244
245   /* Remove overlapped and deleted silences from the user's silence
246    * list.  Clear BAN_ADD since we're walking the list anyway.
247    */
248   for (plast = &cli_user(sptr)->silence; (sile = *plast) != NULL; ) {
249     if (sile->flags & (BAN_OVERLAPPED | BAN_DEL)) {
250       *plast = sile->next;
251       free_ban(sile);
252     } else {
253       sile->flags &= ~BAN_ADD;
254       *plast = sile;
255       plast = &sile->next;
256     }
257   }
258
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]);
263   }
264 }
265
266 /** Handle a SILENCE command from a local user.
267  * See @ref m_functions for general discussion of parameters.
268  *
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
273  *
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.
278  */
279 int m_silence(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
280 {
281   struct Client *acptr;
282   struct Ban *sile;
283
284   assert(0 != cptr);
285   assert(cptr == sptr);
286
287   /* See if the user is requesting a silence list. */
288   acptr = sptr;
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);
294       }
295     }
296     send_reply(sptr, RPL_ENDOFSILELIST, cli_name(acptr));
297     return 0;
298   }
299
300   /* The user must be attempting to update their list. */
301   forward_silences(sptr, parv[1], NULL);
302   return 0;
303 }
304
305 /** Handle a SILENCE command from a server.
306  * See @ref m_functions for general discussion of parameters.
307  *
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.
311  *
312  * \a parv[2] is a comma-separated list of silence updates.
313  *
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.
318  */
319 int ms_silence(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
320 {
321   if (IsServer(sptr))
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");
325
326   /* Figure out which silences can be forwarded. */
327   forward_silences(sptr, parv[2], findNUser(parv[1]));
328   return 0;
329   (void)cptr;
330 }