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