]> jfr.im git - solanum.git/blob - modules/m_list.c
whois: Fix UID leak.
[solanum.git] / modules / m_list.c
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 *
33 * $Id: m_list.c 3372 2007-04-03 10:18:07Z nenolod $
34 */
35
36 #include "stdinc.h"
37 #include "channel.h"
38 #include "client.h"
39 #include "hash.h"
40 #include "match.h"
41 #include "ircd.h"
42 #include "numeric.h"
43 #include "s_conf.h"
44 #include "s_newconf.h"
45 #include "s_serv.h"
46 #include "supported.h"
47 #include "send.h"
48 #include "msg.h"
49 #include "parse.h"
50 #include "modules.h"
51 #include "inline/stringops.h"
52
53 static rb_dlink_list safelisting_clients = { NULL, NULL, 0 };
54
55 static int _modinit(void);
56 static void _moddeinit(void);
57
58 static int m_list(struct Client *, struct Client *, int, const char **);
59 static int mo_list(struct Client *, struct Client *, int, const char **);
60
61 static void list_one_channel(struct Client *source_p, struct Channel *chptr, int visible);
62
63 static void safelist_check_cliexit(hook_data_client_exit * hdata);
64 static void safelist_client_instantiate(struct Client *, struct ListClient *);
65 static void safelist_client_release(struct Client *);
66 static void safelist_one_channel(struct Client *source_p, struct Channel *chptr);
67 static void safelist_iterate_client(struct Client *source_p);
68 static void safelist_iterate_clients(void *unused);
69 static void safelist_channel_named(struct Client *source_p, const char *name, int operspy);
70
71 struct 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
76 mapi_clist_av1 list_clist[] = { &list_msgtab, NULL };
77
78 mapi_hfn_list_av1 list_hfnlist[] = {
79 {"client_exit", (hookfn) safelist_check_cliexit},
80 {NULL, NULL}
81 };
82
83 DECLARE_MODULE_AV1(list, _modinit, _moddeinit, list_clist, NULL, list_hfnlist, "$Revision: 3372 $");
84
85 static struct ev_entry *iterate_clients_ev = NULL;
86
87 static int _modinit(void)
88 {
89 iterate_clients_ev = rb_event_add("safelist_iterate_clients", safelist_iterate_clients, NULL, 3);
90
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
102 return 0;
103 }
104
105 static void _moddeinit(void)
106 {
107 rb_event_delete(iterate_clients_ev);
108
109 delete_isupport("SAFELIST");
110 delete_isupport("ELIST");
111 }
112
113 static 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()
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 */
130 static 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");
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 */
144 if (((last_used + ConfigFileEntry.pace_wait) > rb_current_time()))
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
151 last_used = rb_current_time();
152 }
153
154 return mo_list(client_p, source_p, parc, parv);
155 }
156
157 /* mo_list()
158 * parv[1] = channel
159 */
160 static int mo_list(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
161 {
162 struct ListClient *params;
163 char *p;
164 char *args = NULL;
165 int i;
166 int operspy = 0;
167
168 if (source_p->localClient->safelist_data != NULL)
169 {
170 sendto_one_notice(source_p, ":/LIST aborted");
171 safelist_client_release(source_p);
172 return 0;
173 }
174
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
187 /* Single channel. */
188 if (args && IsChannelName(args))
189 {
190 safelist_channel_named(source_p, args, operspy);
191 return 0;
192 }
193
194 /* Multiple channels, possibly with parameters. */
195 params = rb_malloc(sizeof(struct ListClient));
196
197 /* XXX rather arbitrary -- jilles */
198 params->users_min = 3;
199 params->users_max = INT_MAX;
200 params->operspy = operspy;
201 params->created_min = params->topic_min =
202 params->created_max = params->topic_max = 0;
203
204 if (args && !EmptyString(args))
205 {
206 /* Cancel out default minimum. */
207 params->users_min = 0;
208
209 for (i = 0; i < 7; i++)
210 {
211 if ((p = strchr(args, ',')) != NULL)
212 *p++ = '\0';
213
214 if (*args == '<')
215 {
216 args++;
217 if (IsDigit(*args))
218 {
219 params->users_max = atoi(args);
220 if (params->users_max == 0)
221 params->users_max = INT_MAX;
222 else
223 params->users_max--;
224 }
225 }
226 else if (*args == '>')
227 {
228 args++;
229 if (IsDigit(*args))
230 params->users_min = atoi(args) + 1;
231 else
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 }
277 }
278
279 if (EmptyString(p))
280 break;
281 else
282 args = p;
283 }
284 }
285
286 safelist_client_instantiate(source_p, params);
287
288 return 0;
289 }
290
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 */
298 static 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
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 */
325 static int safelist_sendq_exceeded(struct Client *client_p)
326 {
327 if (rb_linebuf_len(&client_p->localClient->buf_sendq) > (get_sendq(client_p) / 2))
328 return YES;
329 else
330 return NO;
331 }
332
333 /*
334 * safelist_client_instantiate()
335 *
336 * inputs - pointer to Client to be listed,
337 * pointer to ListClient for params
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 */
345 static void safelist_client_instantiate(struct Client *client_p, struct ListClient *params)
346 {
347 s_assert(MyClient(client_p));
348 s_assert(params != NULL);
349
350 client_p->localClient->safelist_data = params;
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 */
355 rb_dlinkAddAlloc(client_p, &safelisting_clients);
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 */
372 static void safelist_client_release(struct Client *client_p)
373 {
374 s_assert(MyClient(client_p));
375
376 rb_dlinkFindDestroy(client_p, &safelisting_clients);
377
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
385 /*
386 * safelist_channel_named()
387 *
388 * inputs - client pointer, channel name, operspy
389 * outputs - none
390 * side effects - a named channel is listed
391 */
392 static void safelist_channel_named(struct Client *source_p, const char *name, int operspy)
393 {
394 struct Channel *chptr;
395 char *p;
396 int visible;
397
398 sendto_one(source_p, form_str(RPL_LISTSTART), me.name, source_p->name);
399
400 if ((p = strchr(name, ',')))
401 *p = '\0';
402
403 if (*name == '\0')
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
410 chptr = find_channel(name);
411
412 if (chptr == NULL)
413 {
414 sendto_one_numeric(source_p, ERR_NOSUCHNICK, form_str(ERR_NOSUCHNICK), name);
415 sendto_one(source_p, form_str(RPL_LISTEND), me.name, source_p->name);
416 return;
417 }
418
419 visible = !SecretChannel(chptr) || IsMember(source_p, chptr);
420 if (visible || operspy)
421 list_one_channel(source_p, chptr, visible);
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 */
435 static void safelist_one_channel(struct Client *source_p, struct Channel *chptr)
436 {
437 struct ListClient *safelist_data = source_p->localClient->safelist_data;
438 int visible;
439
440 visible = !SecretChannel(chptr) || IsMember(source_p, chptr);
441 if (!visible && !safelist_data->operspy)
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
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
462 list_one_channel(source_p, chptr, visible);
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 */
472 static void safelist_iterate_client(struct Client *source_p)
473 {
474 rb_dlink_node *ptr;
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
485 RB_DLINK_FOREACH(ptr, channelTable[iter].head)
486 safelist_one_channel(source_p, (struct Channel *) ptr->data);
487 }
488
489 safelist_client_release(source_p);
490 }
491
492 static 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 }