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