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