]> jfr.im git - solanum.git/blame - modules/m_list.c
Make new_local_user hooks handle dead clients
[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
AC
68static void safelist_one_channel(struct Client *source_p, struct Channel *chptr, struct ListClient *params);
69static void safelist_check_cliexit(hook_data_client_exit * hdata);
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
AC
83mapi_hfn_list_av1 list_hfnlist[] = {
84 {"client_exit", (hookfn) safelist_check_cliexit},
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, "");
103 add_isupport("ELIST", isupport_string, "CTU");
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
999fab77
AC
116static void safelist_check_cliexit(hook_data_client_exit * hdata)
117{
118 /* Cancel the safelist request if we are disconnecting
119 * from the server. That way it doesn't core. :P --nenolod
120 */
121 if (MyClient(hdata->target) && hdata->target->localClient->safelist_data != NULL)
122 {
123 safelist_client_release(hdata->target);
124 }
125}
126
212380e3 127/* m_list()
212380e3
AC
128 * parv[1] = channel
129 *
130 * XXX - With SAFELIST, do we really need to continue pacing?
131 * In theory, the server cannot be lagged by this. --nenolod
132 */
3c7d6fcc
EM
133static void
134m_list(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
212380e3
AC
135{
136 static time_t last_used = 0L;
137
999fab77
AC
138 if (source_p->localClient->safelist_data != NULL)
139 {
140 sendto_one_notice(source_p, ":/LIST aborted");
141 safelist_client_release(source_p);
3c7d6fcc 142 return;
999fab77
AC
143 }
144
212380e3
AC
145 if (parc < 2 || !IsChannelName(parv[1]))
146 {
147 /* pace this due to the sheer traffic involved */
e3354945 148 if (((last_used + ConfigFileEntry.pace_wait) > rb_current_time()))
212380e3
AC
149 {
150 sendto_one(source_p, form_str(RPL_LOAD2HI), me.name, source_p->name, "LIST");
151 sendto_one(source_p, form_str(RPL_LISTEND), me.name, source_p->name);
3c7d6fcc 152 return;
212380e3
AC
153 }
154 else
e3354945 155 last_used = rb_current_time();
212380e3
AC
156 }
157
3c7d6fcc 158 mo_list(msgbuf_p, client_p, source_p, parc, parv);
212380e3
AC
159}
160
161/* mo_list()
212380e3
AC
162 * parv[1] = channel
163 */
3c7d6fcc
EM
164static void
165mo_list(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
212380e3 166{
999fab77 167 struct ListClient *params;
bf0a4592
KB
168 char *p;
169 char *args = NULL;
212380e3 170 int i;
bf0a4592 171 int operspy = 0;
212380e3 172
999fab77
AC
173 if (source_p->localClient->safelist_data != NULL)
174 {
175 sendto_one_notice(source_p, ":/LIST aborted");
176 safelist_client_release(source_p);
3c7d6fcc 177 return;
999fab77 178 }
212380e3 179
bf0a4592
KB
180 if (parc > 1)
181 {
182 args = LOCAL_COPY(parv[1]);
183 }
184
185 if (args && *args == '!' && IsOperSpy(source_p))
186 {
187 args++;
188 report_operspy(source_p, "LIST", args);
189 operspy = 1;
190 }
191
096570b9 192 /* Single channel. */
bf0a4592 193 if (args && IsChannelName(args))
096570b9 194 {
bf0a4592 195 safelist_channel_named(source_p, args, operspy);
3c7d6fcc 196 return;
096570b9
KB
197 }
198
199 /* Multiple channels, possibly with parameters. */
999fab77 200 params = rb_malloc(sizeof(struct ListClient));
096570b9 201
d513218a 202 params->users_min = ConfigChannel.displayed_usercount;
096570b9 203 params->users_max = INT_MAX;
bf0a4592 204 params->operspy = operspy;
55abcbb2 205 params->created_min = params->topic_min =
096570b9 206 params->created_max = params->topic_max = 0;
212380e3 207
bf0a4592 208 if (args && !EmptyString(args))
212380e3 209 {
bb55ebeb 210 /* Cancel out default minimum. */
096570b9 211 params->users_min = 0;
212380e3 212
096570b9 213 for (i = 0; i < 7; i++)
212380e3
AC
214 {
215 if ((p = strchr(args, ',')) != NULL)
216 *p++ = '\0';
217
218 if (*args == '<')
219 {
220 args++;
221 if (IsDigit(*args))
222 {
096570b9
KB
223 params->users_max = atoi(args);
224 if (params->users_max == 0)
225 params->users_max = INT_MAX;
212380e3 226 else
096570b9 227 params->users_max--;
212380e3 228 }
212380e3
AC
229 }
230 else if (*args == '>')
231 {
232 args++;
233 if (IsDigit(*args))
096570b9 234 params->users_min = atoi(args) + 1;
212380e3 235 else
096570b9
KB
236 params->users_min = 0;
237 }
238 else if (*args == 'C' || *args == 'c')
239 {
240 args++;
241 if (*args == '>')
242 {
243 /* Creation time earlier than last x minutes. */
244 args++;
245 if (IsDigit(*args))
246 {
247 params->created_max = rb_current_time() - (60 * atoi(args));
248 }
249 }
250 else if (*args == '<')
251 {
252 /* Creation time within last x minutes. */
253 args++;
254 if (IsDigit(*args))
255 {
256 params->created_min = rb_current_time() - (60 * atoi(args));
257 }
258 }
259 }
260 else if (*args == 'T' || *args == 't')
261 {
262 args++;
263 if (*args == '>')
264 {
265 /* Topic change time earlier than last x minutes. */
266 args++;
267 if (IsDigit(*args))
268 {
269 params->topic_max = rb_current_time() - (60 * atoi(args));
270 }
271 }
272 else if (*args == '<')
273 {
274 /* Topic change time within last x minutes. */
275 args++;
276 if (IsDigit(*args))
277 {
278 params->topic_min = rb_current_time() - (60 * atoi(args));
279 }
280 }
212380e3
AC
281 }
282
283 if (EmptyString(p))
284 break;
285 else
286 args = p;
287 }
288 }
212380e3 289
999fab77 290 safelist_client_instantiate(source_p, params);
212380e3
AC
291}
292
69e7a2cd
JT
293/*
294 * list_one_channel()
295 *
296 * inputs - client pointer, channel pointer, whether normally visible
297 * outputs - none
298 * side effects - a channel is listed
299 */
300static void list_one_channel(struct Client *source_p, struct Channel *chptr,
301 int visible)
302{
e4af89f4
AC
303 char topic[TOPICLEN + 1];
304
305 if (chptr->topic != NULL)
306 rb_strlcpy(topic, chptr->topic, sizeof topic);
307 else
308 topic[0] = '\0';
309 strip_colour(topic);
69e7a2cd
JT
310 sendto_one(source_p, form_str(RPL_LIST), me.name, source_p->name,
311 visible ? "" : "!",
312 chptr->chname, rb_dlink_list_length(&chptr->members),
e4af89f4 313 topic);
69e7a2cd
JT
314}
315
212380e3
AC
316/*
317 * safelist_sendq_exceeded()
318 *
319 * inputs - pointer to client that needs checking
3c7d6fcc
EM
320 * outputs - true if a client has exceeded the reserved
321 * sendq limit, false if not
212380e3
AC
322 * side effects - none
323 *
324 * When safelisting, we only use half of the SendQ at any
325 * given time.
326 */
3c7d6fcc 327static bool safelist_sendq_exceeded(struct Client *client_p)
212380e3 328{
3c7d6fcc 329 return rb_linebuf_len(&client_p->localClient->buf_sendq) > (get_sendq(client_p) / 2);
212380e3
AC
330}
331
999fab77
AC
332/*
333 * safelist_client_instantiate()
334 *
335 * inputs - pointer to Client to be listed,
336 * pointer to ListClient for params
337 * outputs - none
338 * side effects - the safelist process begins for a
339 * client.
340 *
341 * Please do not ever call this on a non-local client.
342 * If you do, you will get SIGSEGV.
343 */
344static void safelist_client_instantiate(struct Client *client_p, struct ListClient *params)
345{
346 s_assert(MyClient(client_p));
347 s_assert(params != NULL);
348
349 client_p->localClient->safelist_data = params;
350
351 sendto_one(client_p, form_str(RPL_LISTSTART), me.name, client_p->name);
352
353 /* pop the client onto the queue for processing */
354 rb_dlinkAddAlloc(client_p, &safelisting_clients);
355
356 /* give the user some initial data to work with */
357 safelist_iterate_client(client_p);
358}
359
360/*
361 * safelist_client_release()
362 *
363 * inputs - pointer to Client being listed on
364 * outputs - none
365 * side effects - the client is no longer being
366 * listed
999fab77
AC
367 */
368static void safelist_client_release(struct Client *client_p)
369{
3c7d6fcc
EM
370 if(!MyClient(client_p))
371 return;
372
999fab77
AC
373 s_assert(MyClient(client_p));
374
375 rb_dlinkFindDestroy(client_p, &safelisting_clients);
376
377 rb_free(client_p->localClient->safelist_data->chname);
378 rb_free(client_p->localClient->safelist_data);
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
212380e3
AC
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 */
999fab77 435static void safelist_one_channel(struct Client *source_p, struct Channel *chptr, struct ListClient *params)
212380e3 436{
a4eeda89 437 int visible;
212380e3 438
a4eeda89 439 visible = !SecretChannel(chptr) || IsMember(source_p, chptr);
730b914c 440 if (!visible && !params->operspy)
212380e3
AC
441 return;
442
730b914c
AC
443 if ((unsigned int)chptr->members.length < params->users_min
444 || (unsigned int)chptr->members.length > params->users_max)
212380e3
AC
445 return;
446
730b914c 447 if (params->topic_min && chptr->topic_time < params->topic_min)
096570b9
KB
448 return;
449
450 /* If a topic TS is provided, don't show channels without a topic set. */
730b914c 451 if (params->topic_max && (chptr->topic_time > params->topic_max
096570b9
KB
452 || chptr->topic_time == 0))
453 return;
454
730b914c 455 if (params->created_min && chptr->channelts < params->created_min)
096570b9
KB
456 return;
457
730b914c 458 if (params->created_max && chptr->channelts > params->created_max)
096570b9
KB
459 return;
460
69e7a2cd 461 list_one_channel(source_p, chptr, visible);
212380e3 462}
999fab77
AC
463
464/*
465 * safelist_iterate_client()
466 *
467 * inputs - client pointer
468 * outputs - none
469 * side effects - the client's sendq is filled up again
470 */
471static void safelist_iterate_client(struct Client *source_p)
472{
473 struct Channel *chptr;
2fc6772e 474 rb_radixtree_iteration_state iter;
999fab77 475
a4bf26dd 476 RB_RADIXTREE_FOREACH_FROM(chptr, &iter, channel_tree, source_p->localClient->safelist_data->chname)
999fab77 477 {
3c7d6fcc 478 if (safelist_sendq_exceeded(source_p->from))
999fab77 479 {
b9a32bd2 480 rb_free(source_p->localClient->safelist_data->chname);
999fab77 481 source_p->localClient->safelist_data->chname = rb_strdup(chptr->chname);
b9a32bd2 482
999fab77
AC
483 return;
484 }
485
486 safelist_one_channel(source_p, chptr, source_p->localClient->safelist_data);
487 }
488
489 safelist_client_release(source_p);
490}
491
492static void safelist_iterate_clients(void *unused)
493{
494 rb_dlink_node *n, *n2;
495
496 RB_DLINK_FOREACH_SAFE(n, n2, safelisting_clients.head)
497 safelist_iterate_client((struct Client *)n->data);
498}