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