]> jfr.im git - solanum.git/blame - modules/m_list.c
Implement operspy for /LIST.
[solanum.git] / modules / m_list.c
CommitLineData
212380e3
AC
1/*
2 * charybdis: An advanced ircd.
3 * m_list_safelist.c: Version of /list that uses the safelist code.
4 *
5 * Copyright (c) 2006 William Pitcock <nenolod@nenolod.net>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * 1. Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
13 *
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * 3. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 *
607cf49f 33 * $Id: m_list.c 3372 2007-04-03 10:18:07Z nenolod $
212380e3
AC
34 */
35
36#include "stdinc.h"
212380e3
AC
37#include "channel.h"
38#include "client.h"
39#include "hash.h"
4562c604 40#include "match.h"
212380e3
AC
41#include "ircd.h"
42#include "numeric.h"
43#include "s_conf.h"
bb55ebeb 44#include "s_newconf.h"
212380e3
AC
45#include "s_serv.h"
46#include "send.h"
47#include "msg.h"
48#include "parse.h"
49#include "modules.h"
212380e3 50
5b96d9a6 51static rb_dlink_list safelisting_clients = { NULL, NULL, 0 };
212380e3
AC
52
53static int _modinit(void);
54static void _moddeinit(void);
55
56static int m_list(struct Client *, struct Client *, int, const char **);
57static int mo_list(struct Client *, struct Client *, int, const char **);
58
59static void safelist_check_cliexit(hook_data_client_exit * hdata);
60static void safelist_client_instantiate(struct Client *, struct ListClient *);
61static void safelist_client_release(struct Client *);
62static void safelist_one_channel(struct Client *source_p, struct Channel *chptr);
63static void safelist_iterate_client(struct Client *source_p);
64static void safelist_iterate_clients(void *unused);
65static void safelist_channel_named(struct Client *source_p, const char *name);
66
67struct Message list_msgtab = {
68 "LIST", 0, 0, 0, MFLG_SLOW,
69 {mg_unreg, {m_list, 0}, mg_ignore, mg_ignore, mg_ignore, {mo_list, 0}}
70};
71
72mapi_clist_av1 list_clist[] = { &list_msgtab, NULL };
73
74mapi_hfn_list_av1 list_hfnlist[] = {
75 {"client_exit", (hookfn) safelist_check_cliexit},
76 {NULL, NULL}
77};
78
607cf49f 79DECLARE_MODULE_AV1(list, _modinit, _moddeinit, list_clist, NULL, list_hfnlist, "$Revision: 3372 $");
212380e3 80
75d60088
AC
81static struct ev_entry *iterate_clients_ev = NULL;
82
212380e3
AC
83static int _modinit(void)
84{
75d60088 85 iterate_clients_ev = rb_event_add("safelist_iterate_clients", safelist_iterate_clients, NULL, 3);
212380e3
AC
86
87 return 0;
88}
89
90static void _moddeinit(void)
91{
75d60088 92 rb_event_delete(iterate_clients_ev);
212380e3
AC
93}
94
95static void safelist_check_cliexit(hook_data_client_exit * hdata)
96{
97 /* Cancel the safelist request if we are disconnecting
98 * from the server. That way it doesn't core. :P --nenolod
99 */
100 if (MyClient(hdata->target) && hdata->target->localClient->safelist_data != NULL)
101 {
102 safelist_client_release(hdata->target);
103 }
104}
105
106/* m_list()
212380e3
AC
107 * parv[1] = channel
108 *
109 * XXX - With SAFELIST, do we really need to continue pacing?
110 * In theory, the server cannot be lagged by this. --nenolod
111 */
112static int m_list(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
113{
114 static time_t last_used = 0L;
115
116 if (source_p->localClient->safelist_data != NULL)
117 {
118 sendto_one_notice(source_p, ":/LIST aborted");
212380e3
AC
119 safelist_client_release(source_p);
120 return 0;
121 }
122
123 if (parc < 2 || !IsChannelName(parv[1]))
124 {
125 /* pace this due to the sheer traffic involved */
e3354945 126 if (((last_used + ConfigFileEntry.pace_wait) > rb_current_time()))
212380e3
AC
127 {
128 sendto_one(source_p, form_str(RPL_LOAD2HI), me.name, source_p->name, "LIST");
129 sendto_one(source_p, form_str(RPL_LISTEND), me.name, source_p->name);
130 return 0;
131 }
132 else
e3354945 133 last_used = rb_current_time();
212380e3
AC
134 }
135
136 return mo_list(client_p, source_p, parc, parv);
137}
138
139/* mo_list()
212380e3
AC
140 * parv[1] = channel
141 */
142static int mo_list(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
143{
144 struct ListClient params;
145 char *p, *args;
146 int i;
147
148 if (source_p->localClient->safelist_data != NULL)
149 {
150 sendto_one_notice(source_p, ":/LIST aborted");
212380e3
AC
151 safelist_client_release(source_p);
152 return 0;
153 }
154
155 /* XXX rather arbitrary -- jilles */
156 params.users_min = 3;
157 params.users_max = INT_MAX;
bb55ebeb 158 params.operspy = 0;
212380e3
AC
159
160 if (parc > 1 && parv[1] != NULL && !IsChannelName(parv[1]))
161 {
162 args = LOCAL_COPY(parv[1]);
bb55ebeb
KB
163
164 /* Cancel out default minimum. */
165 params.users_min = 0;
212380e3
AC
166
167 for (i = 0; i < 2; i++)
168 {
169 if ((p = strchr(args, ',')) != NULL)
170 *p++ = '\0';
171
172 if (*args == '<')
173 {
174 args++;
175 if (IsDigit(*args))
176 {
177 params.users_max = atoi(args);
178 if (params.users_max == 0)
179 params.users_max = INT_MAX;
180 else
181 params.users_max--;
182 }
183 else
184 params.users_max = INT_MAX;
185 }
186 else if (*args == '>')
187 {
188 args++;
189 if (IsDigit(*args))
190 params.users_min = atoi(args) + 1;
191 else
192 params.users_min = 0;
193 }
bb55ebeb
KB
194 /* Only accept operspy as the first option. */
195 else if (*args == '!' && IsOperSpy(source_p) && i == 0)
196 {
197 params.operspy = 1;
198 report_operspy(source_p, "LIST", p);
199 }
212380e3
AC
200
201 if (EmptyString(p))
202 break;
203 else
204 args = p;
205 }
206 }
207 else if (parc > 1 && IsChannelName(parv[1]))
208 {
209 safelist_channel_named(source_p, parv[1]);
210 return 0;
211 }
212
213 safelist_client_instantiate(source_p, &params);
214
215 return 0;
216}
217
218/*
219 * safelist_sendq_exceeded()
220 *
221 * inputs - pointer to client that needs checking
222 * outputs - 1 if a client has exceeded the reserved
223 * sendq limit, 0 if not
224 * side effects - none
225 *
226 * When safelisting, we only use half of the SendQ at any
227 * given time.
228 */
229static int safelist_sendq_exceeded(struct Client *client_p)
230{
75d60088 231 if (rb_linebuf_len(&client_p->localClient->buf_sendq) > (get_sendq(client_p) / 2))
212380e3
AC
232 return YES;
233 else
234 return NO;
235}
236
237/*
238 * safelist_client_instantiate()
239 *
240 * inputs - pointer to Client to be listed,
241 * struct ListClient to copy for params
242 * outputs - none
243 * side effects - the safelist process begins for a
244 * client.
245 *
246 * Please do not ever call this on a non-local client.
247 * If you do, you will get SIGSEGV.
248 */
249static void safelist_client_instantiate(struct Client *client_p, struct ListClient *params)
250{
251 struct ListClient *self;
252
253 s_assert(MyClient(client_p));
254 s_assert(params != NULL);
255
eddc2ab6 256 self = rb_malloc(sizeof(struct ListClient));
212380e3
AC
257
258 self->hash_indice = 0;
259 self->users_min = params->users_min;
260 self->users_max = params->users_max;
bb55ebeb 261 self->operspy = params->operspy;
212380e3
AC
262
263 client_p->localClient->safelist_data = self;
264
265 sendto_one(client_p, form_str(RPL_LISTSTART), me.name, client_p->name);
266
267 /* pop the client onto the queue for processing */
75d60088 268 rb_dlinkAddAlloc(client_p, &safelisting_clients);
212380e3
AC
269
270 /* give the user some initial data to work with */
271 safelist_iterate_client(client_p);
272}
273
274/*
275 * safelist_client_release()
276 *
277 * inputs - pointer to Client being listed on
278 * outputs - none
279 * side effects - the client is no longer being
280 * listed
281 *
282 * Please do not ever call this on a non-local client.
283 * If you do, you will get SIGSEGV.
284 */
285static void safelist_client_release(struct Client *client_p)
286{
287 s_assert(MyClient(client_p));
288
555ac41f 289 rb_dlinkFindDestroy(client_p, &safelisting_clients);
212380e3 290
637c4932 291 rb_free(client_p->localClient->safelist_data);
212380e3
AC
292
293 client_p->localClient->safelist_data = NULL;
294
295 sendto_one(client_p, form_str(RPL_LISTEND), me.name, client_p->name);
296}
297
298/*
299 * safelist_channel_named()
300 *
301 * inputs - client pointer, channel name
302 * outputs - none
303 * side effects - a named channel is listed
304 */
305static void safelist_channel_named(struct Client *source_p, const char *name)
306{
307 struct Channel *chptr;
308 char *p;
309 char *n = LOCAL_COPY(name);
310
311 sendto_one(source_p, form_str(RPL_LISTSTART), me.name, source_p->name);
312
313 if ((p = strchr(n, ',')))
314 *p = '\0';
315
316 if (*n == '\0')
317 {
318 sendto_one_numeric(source_p, ERR_NOSUCHNICK, form_str(ERR_NOSUCHNICK), name);
319 sendto_one(source_p, form_str(RPL_LISTEND), me.name, source_p->name);
320 return;
321 }
322
323 chptr = find_channel(n);
324
325 if (chptr == NULL)
326 {
327 sendto_one_numeric(source_p, ERR_NOSUCHNICK, form_str(ERR_NOSUCHNICK), n);
328 sendto_one(source_p, form_str(RPL_LISTEND), me.name, source_p->name);
329 return;
330 }
331
2bad5789 332 if (!SecretChannel(chptr) || IsMember(source_p, chptr))
bb55ebeb
KB
333 sendto_one(source_p, form_str(RPL_LIST), me.name, source_p->name, "",
334 chptr->chname, rb_dlink_list_length(&chptr->members),
212380e3
AC
335 chptr->topic == NULL ? "" : chptr->topic);
336
337 sendto_one(source_p, form_str(RPL_LISTEND), me.name, source_p->name);
338 return;
339}
340
341/*
342 * safelist_one_channel()
343 *
344 * inputs - client pointer and channel pointer
345 * outputs - none
346 * side effects - a channel is listed if it meets the
347 * requirements
348 */
349static void safelist_one_channel(struct Client *source_p, struct Channel *chptr)
350{
351 struct ListClient *safelist_data = source_p->localClient->safelist_data;
352
bb55ebeb 353 if (SecretChannel(chptr) && !IsMember(source_p, chptr) && !safelist_data->operspy)
212380e3
AC
354 return;
355
356 if ((unsigned int)chptr->members.length < safelist_data->users_min
357 || (unsigned int)chptr->members.length > safelist_data->users_max)
358 return;
359
bb55ebeb
KB
360 sendto_one(source_p, form_str(RPL_LIST), me.name, source_p->name,
361 (safelist_data->operspy && SecretChannel(chptr)) ? "!" : "",
362 chptr->chname, rb_dlink_list_length(&chptr->members),
363 chptr->topic == NULL ? "" : chptr->topic);
212380e3
AC
364}
365
366/*
367 * safelist_iterate_client()
368 *
369 * inputs - client pointer
370 * outputs - none
371 * side effects - the client's sendq is filled up again
372 */
373static void safelist_iterate_client(struct Client *source_p)
374{
5b96d9a6 375 rb_dlink_node *ptr;
212380e3
AC
376 int iter;
377
378 for (iter = source_p->localClient->safelist_data->hash_indice; iter < CH_MAX; iter++)
379 {
380 if (safelist_sendq_exceeded(source_p->from) == YES)
381 {
382 source_p->localClient->safelist_data->hash_indice = iter;
383 return;
384 }
385
5b96d9a6 386 RB_DLINK_FOREACH(ptr, channelTable[iter].head)
212380e3
AC
387 safelist_one_channel(source_p, (struct Channel *) ptr->data);
388 }
389
390 safelist_client_release(source_p);
391}
392
393static void safelist_iterate_clients(void *unused)
394{
5b96d9a6 395 rb_dlink_node *n, *n2;
212380e3 396
5b96d9a6 397 RB_DLINK_FOREACH_SAFE(n, n2, safelisting_clients.head)
212380e3
AC
398 safelist_iterate_client((struct Client *)n->data);
399}