]> jfr.im git - solanum.git/blame - modules/m_list.c
Rerun autoconf.
[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 45#include "s_serv.h"
4c3f066a 46#include "supported.h"
212380e3
AC
47#include "send.h"
48#include "msg.h"
49#include "parse.h"
50#include "modules.h"
212380e3 51
5b96d9a6 52static rb_dlink_list safelisting_clients = { NULL, NULL, 0 };
212380e3
AC
53
54static int _modinit(void);
55static void _moddeinit(void);
56
57static int m_list(struct Client *, struct Client *, int, const char **);
58static int mo_list(struct Client *, struct Client *, int, const char **);
59
60static void safelist_check_cliexit(hook_data_client_exit * hdata);
61static void safelist_client_instantiate(struct Client *, struct ListClient *);
62static void safelist_client_release(struct Client *);
63static void safelist_one_channel(struct Client *source_p, struct Channel *chptr);
64static void safelist_iterate_client(struct Client *source_p);
65static void safelist_iterate_clients(void *unused);
bf0a4592 66static void safelist_channel_named(struct Client *source_p, const char *name, int operspy);
212380e3
AC
67
68struct Message list_msgtab = {
69 "LIST", 0, 0, 0, MFLG_SLOW,
70 {mg_unreg, {m_list, 0}, mg_ignore, mg_ignore, mg_ignore, {mo_list, 0}}
71};
72
73mapi_clist_av1 list_clist[] = { &list_msgtab, NULL };
74
75mapi_hfn_list_av1 list_hfnlist[] = {
76 {"client_exit", (hookfn) safelist_check_cliexit},
77 {NULL, NULL}
78};
79
607cf49f 80DECLARE_MODULE_AV1(list, _modinit, _moddeinit, list_clist, NULL, list_hfnlist, "$Revision: 3372 $");
212380e3 81
75d60088
AC
82static struct ev_entry *iterate_clients_ev = NULL;
83
212380e3
AC
84static int _modinit(void)
85{
75d60088 86 iterate_clients_ev = rb_event_add("safelist_iterate_clients", safelist_iterate_clients, NULL, 3);
212380e3 87
4c3f066a
KB
88 /* ELIST=[tokens]:
89 *
90 * M = mask search
91 * N = !mask search
92 * U = user count search (< >)
93 * C = creation time search (C> C<)
94 * T = topic search (T> T<)
95 */
96 add_isupport("SAFELIST", isupport_string, "");
97 add_isupport("ELIST", isupport_string, "CTU");
98
212380e3
AC
99 return 0;
100}
101
102static void _moddeinit(void)
103{
75d60088 104 rb_event_delete(iterate_clients_ev);
4c3f066a
KB
105
106 delete_isupport("SAFELIST");
107 delete_isupport("ELIST");
212380e3
AC
108}
109
110static void safelist_check_cliexit(hook_data_client_exit * hdata)
111{
112 /* Cancel the safelist request if we are disconnecting
113 * from the server. That way it doesn't core. :P --nenolod
114 */
115 if (MyClient(hdata->target) && hdata->target->localClient->safelist_data != NULL)
116 {
117 safelist_client_release(hdata->target);
118 }
119}
120
121/* m_list()
212380e3
AC
122 * parv[1] = channel
123 *
124 * XXX - With SAFELIST, do we really need to continue pacing?
125 * In theory, the server cannot be lagged by this. --nenolod
126 */
127static int m_list(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
128{
129 static time_t last_used = 0L;
130
131 if (source_p->localClient->safelist_data != NULL)
132 {
133 sendto_one_notice(source_p, ":/LIST aborted");
212380e3
AC
134 safelist_client_release(source_p);
135 return 0;
136 }
137
138 if (parc < 2 || !IsChannelName(parv[1]))
139 {
140 /* pace this due to the sheer traffic involved */
e3354945 141 if (((last_used + ConfigFileEntry.pace_wait) > rb_current_time()))
212380e3
AC
142 {
143 sendto_one(source_p, form_str(RPL_LOAD2HI), me.name, source_p->name, "LIST");
144 sendto_one(source_p, form_str(RPL_LISTEND), me.name, source_p->name);
145 return 0;
146 }
147 else
e3354945 148 last_used = rb_current_time();
212380e3
AC
149 }
150
151 return mo_list(client_p, source_p, parc, parv);
152}
153
154/* mo_list()
212380e3
AC
155 * parv[1] = channel
156 */
157static int mo_list(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
158{
096570b9 159 struct ListClient *params;
bf0a4592
KB
160 char *p;
161 char *args = NULL;
212380e3 162 int i;
bf0a4592 163 int operspy = 0;
212380e3
AC
164
165 if (source_p->localClient->safelist_data != NULL)
166 {
167 sendto_one_notice(source_p, ":/LIST aborted");
212380e3
AC
168 safelist_client_release(source_p);
169 return 0;
170 }
171
bf0a4592
KB
172 if (parc > 1)
173 {
174 args = LOCAL_COPY(parv[1]);
175 }
176
177 if (args && *args == '!' && IsOperSpy(source_p))
178 {
179 args++;
180 report_operspy(source_p, "LIST", args);
181 operspy = 1;
182 }
183
096570b9 184 /* Single channel. */
bf0a4592 185 if (args && IsChannelName(args))
096570b9 186 {
bf0a4592 187 safelist_channel_named(source_p, args, operspy);
096570b9
KB
188 return 0;
189 }
190
191 /* Multiple channels, possibly with parameters. */
192 params = rb_malloc(sizeof(struct ListClient));
193
212380e3 194 /* XXX rather arbitrary -- jilles */
096570b9
KB
195 params->users_min = 3;
196 params->users_max = INT_MAX;
bf0a4592 197 params->operspy = operspy;
096570b9
KB
198 params->created_min = params->topic_min =
199 params->created_max = params->topic_max = 0;
212380e3 200
bf0a4592 201 if (args && !EmptyString(args))
212380e3 202 {
bb55ebeb 203 /* Cancel out default minimum. */
096570b9 204 params->users_min = 0;
212380e3 205
096570b9 206 for (i = 0; i < 7; i++)
212380e3
AC
207 {
208 if ((p = strchr(args, ',')) != NULL)
209 *p++ = '\0';
210
211 if (*args == '<')
212 {
213 args++;
214 if (IsDigit(*args))
215 {
096570b9
KB
216 params->users_max = atoi(args);
217 if (params->users_max == 0)
218 params->users_max = INT_MAX;
212380e3 219 else
096570b9 220 params->users_max--;
212380e3 221 }
212380e3
AC
222 }
223 else if (*args == '>')
224 {
225 args++;
226 if (IsDigit(*args))
096570b9 227 params->users_min = atoi(args) + 1;
212380e3 228 else
096570b9
KB
229 params->users_min = 0;
230 }
231 else if (*args == 'C' || *args == 'c')
232 {
233 args++;
234 if (*args == '>')
235 {
236 /* Creation time earlier than last x minutes. */
237 args++;
238 if (IsDigit(*args))
239 {
240 params->created_max = rb_current_time() - (60 * atoi(args));
241 }
242 }
243 else if (*args == '<')
244 {
245 /* Creation time within last x minutes. */
246 args++;
247 if (IsDigit(*args))
248 {
249 params->created_min = rb_current_time() - (60 * atoi(args));
250 }
251 }
252 }
253 else if (*args == 'T' || *args == 't')
254 {
255 args++;
256 if (*args == '>')
257 {
258 /* Topic change time earlier than last x minutes. */
259 args++;
260 if (IsDigit(*args))
261 {
262 params->topic_max = rb_current_time() - (60 * atoi(args));
263 }
264 }
265 else if (*args == '<')
266 {
267 /* Topic change time within last x minutes. */
268 args++;
269 if (IsDigit(*args))
270 {
271 params->topic_min = rb_current_time() - (60 * atoi(args));
272 }
273 }
212380e3
AC
274 }
275
276 if (EmptyString(p))
277 break;
278 else
279 args = p;
280 }
281 }
212380e3 282
096570b9 283 safelist_client_instantiate(source_p, params);
212380e3
AC
284
285 return 0;
286}
287
288/*
289 * safelist_sendq_exceeded()
290 *
291 * inputs - pointer to client that needs checking
292 * outputs - 1 if a client has exceeded the reserved
293 * sendq limit, 0 if not
294 * side effects - none
295 *
296 * When safelisting, we only use half of the SendQ at any
297 * given time.
298 */
299static int safelist_sendq_exceeded(struct Client *client_p)
300{
75d60088 301 if (rb_linebuf_len(&client_p->localClient->buf_sendq) > (get_sendq(client_p) / 2))
212380e3
AC
302 return YES;
303 else
304 return NO;
305}
306
307/*
308 * safelist_client_instantiate()
309 *
310 * inputs - pointer to Client to be listed,
096570b9 311 * pointer to ListClient for params
212380e3
AC
312 * outputs - none
313 * side effects - the safelist process begins for a
314 * client.
315 *
316 * Please do not ever call this on a non-local client.
317 * If you do, you will get SIGSEGV.
318 */
319static void safelist_client_instantiate(struct Client *client_p, struct ListClient *params)
320{
212380e3
AC
321 s_assert(MyClient(client_p));
322 s_assert(params != NULL);
323
096570b9 324 client_p->localClient->safelist_data = params;
212380e3
AC
325
326 sendto_one(client_p, form_str(RPL_LISTSTART), me.name, client_p->name);
327
328 /* pop the client onto the queue for processing */
75d60088 329 rb_dlinkAddAlloc(client_p, &safelisting_clients);
212380e3
AC
330
331 /* give the user some initial data to work with */
332 safelist_iterate_client(client_p);
333}
334
335/*
336 * safelist_client_release()
337 *
338 * inputs - pointer to Client being listed on
339 * outputs - none
340 * side effects - the client is no longer being
341 * listed
342 *
343 * Please do not ever call this on a non-local client.
344 * If you do, you will get SIGSEGV.
345 */
346static void safelist_client_release(struct Client *client_p)
347{
348 s_assert(MyClient(client_p));
349
555ac41f 350 rb_dlinkFindDestroy(client_p, &safelisting_clients);
212380e3 351
637c4932 352 rb_free(client_p->localClient->safelist_data);
212380e3
AC
353
354 client_p->localClient->safelist_data = NULL;
355
356 sendto_one(client_p, form_str(RPL_LISTEND), me.name, client_p->name);
357}
358
359/*
360 * safelist_channel_named()
361 *
bf0a4592 362 * inputs - client pointer, channel name, operspy
212380e3
AC
363 * outputs - none
364 * side effects - a named channel is listed
365 */
bf0a4592 366static void safelist_channel_named(struct Client *source_p, const char *name, int operspy)
212380e3
AC
367{
368 struct Channel *chptr;
369 char *p;
a4eeda89 370 int visible;
212380e3
AC
371
372 sendto_one(source_p, form_str(RPL_LISTSTART), me.name, source_p->name);
373
bf0a4592 374 if ((p = strchr(name, ',')))
212380e3
AC
375 *p = '\0';
376
bf0a4592 377 if (*name == '\0')
212380e3
AC
378 {
379 sendto_one_numeric(source_p, ERR_NOSUCHNICK, form_str(ERR_NOSUCHNICK), name);
380 sendto_one(source_p, form_str(RPL_LISTEND), me.name, source_p->name);
381 return;
382 }
383
bf0a4592 384 chptr = find_channel(name);
212380e3
AC
385
386 if (chptr == NULL)
387 {
bf0a4592 388 sendto_one_numeric(source_p, ERR_NOSUCHNICK, form_str(ERR_NOSUCHNICK), name);
212380e3
AC
389 sendto_one(source_p, form_str(RPL_LISTEND), me.name, source_p->name);
390 return;
391 }
392
a4eeda89
JT
393 visible = !SecretChannel(chptr) || IsMember(source_p, chptr);
394 if (visible || operspy)
bf0a4592 395 sendto_one(source_p, form_str(RPL_LIST), me.name, source_p->name,
a4eeda89 396 visible ? "" : "!",
bb55ebeb 397 chptr->chname, rb_dlink_list_length(&chptr->members),
212380e3
AC
398 chptr->topic == NULL ? "" : chptr->topic);
399
400 sendto_one(source_p, form_str(RPL_LISTEND), me.name, source_p->name);
401 return;
402}
403
404/*
405 * safelist_one_channel()
406 *
407 * inputs - client pointer and channel pointer
408 * outputs - none
409 * side effects - a channel is listed if it meets the
410 * requirements
411 */
412static void safelist_one_channel(struct Client *source_p, struct Channel *chptr)
413{
414 struct ListClient *safelist_data = source_p->localClient->safelist_data;
a4eeda89 415 int visible;
212380e3 416
a4eeda89
JT
417 visible = !SecretChannel(chptr) || IsMember(source_p, chptr);
418 if (!visible && !safelist_data->operspy)
212380e3
AC
419 return;
420
421 if ((unsigned int)chptr->members.length < safelist_data->users_min
422 || (unsigned int)chptr->members.length > safelist_data->users_max)
423 return;
424
096570b9
KB
425 if (safelist_data->topic_min && chptr->topic_time < safelist_data->topic_min)
426 return;
427
428 /* If a topic TS is provided, don't show channels without a topic set. */
429 if (safelist_data->topic_max && (chptr->topic_time > safelist_data->topic_max
430 || chptr->topic_time == 0))
431 return;
432
433 if (safelist_data->created_min && chptr->channelts < safelist_data->created_min)
434 return;
435
436 if (safelist_data->created_max && chptr->channelts > safelist_data->created_max)
437 return;
438
bb55ebeb 439 sendto_one(source_p, form_str(RPL_LIST), me.name, source_p->name,
a4eeda89 440 visible ? "" : "!",
bb55ebeb
KB
441 chptr->chname, rb_dlink_list_length(&chptr->members),
442 chptr->topic == NULL ? "" : chptr->topic);
212380e3
AC
443}
444
445/*
446 * safelist_iterate_client()
447 *
448 * inputs - client pointer
449 * outputs - none
450 * side effects - the client's sendq is filled up again
451 */
452static void safelist_iterate_client(struct Client *source_p)
453{
5b96d9a6 454 rb_dlink_node *ptr;
212380e3
AC
455 int iter;
456
457 for (iter = source_p->localClient->safelist_data->hash_indice; iter < CH_MAX; iter++)
458 {
459 if (safelist_sendq_exceeded(source_p->from) == YES)
460 {
461 source_p->localClient->safelist_data->hash_indice = iter;
462 return;
463 }
464
5b96d9a6 465 RB_DLINK_FOREACH(ptr, channelTable[iter].head)
212380e3
AC
466 safelist_one_channel(source_p, (struct Channel *) ptr->data);
467 }
468
469 safelist_client_release(source_p);
470}
471
472static void safelist_iterate_clients(void *unused)
473{
5b96d9a6 474 rb_dlink_node *n, *n2;
212380e3 475
5b96d9a6 476 RB_DLINK_FOREACH_SAFE(n, n2, safelisting_clients.head)
212380e3
AC
477 safelist_iterate_client((struct Client *)n->data);
478}