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