Implement silence exceptions.
[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_reply.h"
36 #include "ircd_snprintf.h"
37 #include "ircd_string.h"
38 #include "list.h"
39 #include "msg.h"
40 #include "numeric.h"
41 #include "numnicks.h"
42 #include "s_user.h"
43 #include "send.h"
44 #include "struct.h"
45
46 #include <assert.h>
47 #include <stdlib.h>
48 #include <string.h>
49
50 /** Attempt to apply a SILENCE update to a user.
51  *
52  * Silences are propagated lazily between servers to save on bandwidth
53  * and remote memory.  Any removal and any silence exception must be
54  * propagated until a server has not seen the mask being removed or
55  * has no positive silences for the user.
56  *
57  * @param[in] sptr Client to update.
58  * @param[in] mask Single silence mask to apply, optionally preceded by '+' or '-' and maybe '~'.
59  * @return The new ban entry on success, NULL on failure.
60  */
61 static struct Ban *
62 apply_silence(struct Client *sptr, const char *mask)
63 {
64   struct Ban *sile;
65   int flags;
66
67   assert(mask && mask[0]);
68
69   /* Check for add or remove. */
70   if (mask[0] == '-') {
71     flags = BAN_DEL;
72     mask++;
73   } else if (mask[0] == '+') {
74     flags = BAN_ADD;
75     mask++;
76   } else
77     flags = BAN_ADD;
78
79   /* Check for being an exception. */
80   if (mask[0] == '~') {
81     flags |= BAN_EXCEPTION;
82     mask++;
83   }
84
85   /* Make the silence, set flags, and apply it. */
86   sile = make_ban(mask);
87   sile->flags |= flags;
88   return apply_ban(&cli_user(sptr)->silence, sile) ? NULL : sile;
89 }
90
91 /** Apply and send silence updates for a user.
92  * @param[in] sptr Client whose silence list has been updated.
93  * @param[in] silences Comma-separated list of silence updates.
94  * @param[in] dest Direction to send updates in (NULL for broadcast).
95  */
96 static void
97 forward_silences(struct Client *sptr, char *silences, struct Client *dest)
98 {
99   struct Ban *accepted[MAXPARA], *sile, **plast;
100   char *cp, *p, buf[BUFSIZE];
101   size_t ac_count, buf_used, slen, ii;
102
103   /* Split the list of silences and try to apply each one in turn. */
104   for (cp = ircd_strtok(&p, silences, ","), ac_count = 0;
105        cp && (ac_count < MAXPARA);
106        cp = ircd_strtok(&p, 0, ",")) {
107     if ((sile = apply_silence(sptr, cp)))
108       accepted[ac_count++] = sile;
109   }
110
111
112   if (MyUser(sptr)) {
113     size_t siles, maxsiles, totlength, maxlength, jj;
114
115     /* Check that silence count and total length are permitted. */
116     maxsiles = feature_int(FEAT_MAXSILES);
117     maxlength = maxsiles * feature_int(FEAT_AVBANLEN);
118     siles = totlength = 0;
119     /* Count number of current silences and their total length. */
120     for (sile = cli_user(sptr)->silence; sile; sile = sile->next) {
121       if (sile->flags & (BAN_OVERLAPPED | BAN_ADD | BAN_DEL))
122         continue;
123       siles++;
124       totlength += strlen(sile->banstr);
125     }
126     for (ii = jj = 0; ii < ac_count; ++ii) {
127       sile = accepted[ii];
128       /* If the update is being added, and we would exceed the maximum
129        * count or length, drop the update.
130        */
131       if (!(sile->flags & (BAN_OVERLAPPED | BAN_DEL))) {
132         slen = strlen(sile->banstr);
133         if ((siles >= maxsiles) || (totlength + slen >= maxlength)) {
134           free_ban(accepted[ii]);
135           continue;
136         }
137         /* Update counts. */
138         siles++;
139         totlength += slen;
140       }
141       /* Store the update. */
142       accepted[jj++] = sile;
143     }
144     /* Write back the number of accepted updates. */
145     ac_count = jj;
146
147     /* Send the silence update list, including overlapped silences (to
148      * make it easier on clients).
149      */
150     buf_used = 0;
151     for (sile = cli_user(sptr)->silence; sile; sile = sile->next) {
152       char ch;
153       if (sile->flags & (BAN_OVERLAPPED | BAN_DEL))
154         ch = '-';
155       else if (sile->flags & BAN_ADD)
156         ch = '+';
157       else
158         continue;
159       slen = strlen(sile->banstr);
160       if (buf_used + slen + 4 > 400) {
161         buf[buf_used] = '\0';
162         sendcmdto_one(sptr, CMD_SILENCE, sptr, "%s", buf);
163         buf_used = 0;
164       }
165       if (buf_used)
166         buf[buf_used++] = ',';
167       buf[buf_used++] = ch;
168       if (sile->flags & BAN_EXCEPTION)
169         buf[buf_used++] = '~';
170       memcpy(buf + buf_used, sile->banstr, slen);
171       buf_used += slen;
172     }
173     if (buf_used > 0) {
174         buf[buf_used] = '\0';
175         sendcmdto_one(sptr, CMD_SILENCE, sptr, "%s", buf);
176         buf_used = 0;
177     }
178   }
179
180   /* Forward any silence removals or exceptions updates to other
181    * servers if the user has positive silences.
182    */
183   if (!dest || !MyUser(dest)) {
184     for (ii = buf_used = 0; ii < ac_count; ++ii) {
185       char ch;
186       sile = accepted[ii];
187       if (sile->flags & BAN_OVERLAPPED)
188         continue;
189       else if (sile->flags & BAN_DEL)
190         ch = '-';
191       else if (sile->flags & BAN_ADD) {
192         if (!(sile->flags & BAN_EXCEPTION))
193           continue;
194         ch = '+';
195       } else
196         continue;
197       slen = strlen(sile->banstr);
198       if (buf_used + slen + 4 > 400) {
199         buf[buf_used] = '\0';
200         if (dest)
201           sendcmdto_one(sptr, CMD_SILENCE, dest, "%C %s", dest, buf);
202         else
203           sendcmdto_serv_butone(sptr, CMD_SILENCE, sptr, "* %s", buf);
204         buf_used = 0;
205       }
206       if (buf_used)
207         buf[buf_used++] = ',';
208       buf[buf_used++] = ch;
209       if (sile->flags & BAN_EXCEPTION)
210         buf[buf_used++] = '~';
211       memcpy(buf + buf_used, sile->banstr, slen);
212       buf_used += slen;
213     }
214     if (buf_used > 0) {
215         buf[buf_used] = '\0';
216         if (dest)
217           sendcmdto_one(sptr, CMD_SILENCE, dest, "%C %s", dest, buf);
218         else
219           sendcmdto_serv_butone(sptr, CMD_SILENCE, sptr, "* %s", buf);
220         buf_used = 0;
221     }
222   }
223
224   /* Remove overlapped and deleted silences from the user's silence
225    * list.  Clear BAN_ADD since we're walking the list anyway.
226    */
227   for (plast = &cli_user(sptr)->silence; (sile = *plast) != NULL; ) {
228     if (sile->flags & (BAN_OVERLAPPED | BAN_DEL)) {
229       *plast = sile->next;
230       free_ban(sile);
231     } else {
232       sile->flags &= ~BAN_ADD;
233       *plast = sile;
234       plast = &sile->next;
235     }
236   }
237
238   /* Free any silence-deleting updates. */
239   for (ii = 0; ii < ac_count; ++ii) {
240     if (accepted[ii]->flags & BAN_DEL)
241       free_ban(accepted[ii]);
242   }
243 }
244
245 /** Handle a SILENCE command from a local user.
246  * See @ref m_functions for general discussion of parameters.
247  *
248  * \a parv[1] may be any of the following:
249  * \li Omitted or empty, to view your own silence list.
250  * \li Nickname of a user, to view that user's silence list.
251  * \li A comma-separated list of silence updates
252  *
253  * @param[in] cptr Client that sent us the message.
254  * @param[in] sptr Original source of message.
255  * @param[in] parc Number of arguments.
256  * @param[in] parv Argument vector.
257  */
258 int m_silence(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
259 {
260   struct Client *acptr;
261   struct Ban *sile;
262
263   assert(0 != cptr);
264   assert(cptr == sptr);
265
266   /* See if the user is requesting a silence list. */
267   acptr = sptr;
268   if (parc < 2 || EmptyString(parv[1]) || (acptr = FindUser(parv[1]))) {
269     if (cli_user(acptr)) {
270       for (sile = cli_user(acptr)->silence; sile; sile = sile->next) {
271         send_reply(sptr, RPL_SILELIST, cli_name(acptr),
272                    (sile->flags & BAN_EXCEPTION ? "~" : ""),  sile->banstr);
273       }
274     }
275     send_reply(sptr, RPL_ENDOFSILELIST, cli_name(acptr));
276     return 0;
277   }
278
279   /* The user must be attempting to update their list. */
280   forward_silences(sptr, parv[1], NULL);
281   return 0;
282 }
283
284 /** Handle a SILENCE command from a server.
285  * See @ref m_functions for general discussion of parameters.
286  *
287  * \a parv[1] may be one of the following:
288  * \li "*" to indicate a broadcast update (removing a SILENCE)
289  * \li A client numnick that should be specifically SILENCEd.
290  *
291  * \a parv[2] is a comma-separated list of silence updates.
292  *
293  * @param[in] cptr Client that sent us the message.
294  * @param[in] sptr Original source of message.
295  * @param[in] parc Number of arguments.
296  * @param[in] parv Argument vector.
297  */
298 int ms_silence(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
299 {
300   if (IsServer(sptr))
301     return protocol_violation(sptr, "Server trying to silence a user");
302   if (parc < 3 || EmptyString(parv[2]))
303     return need_more_params(sptr, "SILENCE");
304
305   /* Figure out which silences can be forwarded. */
306   forward_silences(sptr, parv[2], findNUser(parv[1]));
307   return 0;
308   (void)cptr;
309 }