]>
Commit | Line | Data |
---|---|---|
212380e3 AC |
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 | * | |
607cf49f | 33 | * $Id: m_list.c 3372 2007-04-03 10:18:07Z nenolod $ |
212380e3 AC |
34 | */ |
35 | ||
36 | #include "stdinc.h" | |
212380e3 AC |
37 | #include "channel.h" |
38 | #include "client.h" | |
39 | #include "hash.h" | |
4562c604 | 40 | #include "match.h" |
212380e3 AC |
41 | #include "ircd.h" |
42 | #include "numeric.h" | |
43 | #include "s_conf.h" | |
bb55ebeb | 44 | #include "s_newconf.h" |
212380e3 | 45 | #include "s_serv.h" |
4c3f066a | 46 | #include "supported.h" |
212380e3 AC |
47 | #include "send.h" |
48 | #include "msg.h" | |
49 | #include "parse.h" | |
50 | #include "modules.h" | |
69e7a2cd | 51 | #include "inline/stringops.h" |
77d3d2db KB |
52 | #include "s_assert.h" |
53 | #include "logger.h" | |
999fab77 AC |
54 | #include "irc_radixtree.h" |
55 | ||
56 | static rb_dlink_list safelisting_clients = { NULL, NULL, 0 }; | |
212380e3 | 57 | |
212380e3 AC |
58 | static int _modinit(void); |
59 | static void _moddeinit(void); | |
60 | ||
428ca87b AC |
61 | static int m_list(struct MsgBuf *, struct Client *, struct Client *, int, const char **); |
62 | static int mo_list(struct MsgBuf *, struct Client *, struct Client *, int, const char **); | |
212380e3 | 63 | |
69e7a2cd JT |
64 | static void list_one_channel(struct Client *source_p, struct Channel *chptr, int visible); |
65 | ||
999fab77 AC |
66 | static void safelist_one_channel(struct Client *source_p, struct Channel *chptr, struct ListClient *params); |
67 | static void safelist_check_cliexit(hook_data_client_exit * hdata); | |
68 | static void safelist_client_instantiate(struct Client *, struct ListClient *); | |
69 | static void safelist_client_release(struct Client *); | |
70 | static void safelist_iterate_client(struct Client *source_p); | |
71 | static void safelist_iterate_clients(void *unused); | |
bf0a4592 | 72 | static void safelist_channel_named(struct Client *source_p, const char *name, int operspy); |
212380e3 AC |
73 | |
74 | struct Message list_msgtab = { | |
7baa37a9 | 75 | "LIST", 0, 0, 0, 0, |
212380e3 AC |
76 | {mg_unreg, {m_list, 0}, mg_ignore, mg_ignore, mg_ignore, {mo_list, 0}} |
77 | }; | |
78 | ||
79 | mapi_clist_av1 list_clist[] = { &list_msgtab, NULL }; | |
80 | ||
999fab77 AC |
81 | mapi_hfn_list_av1 list_hfnlist[] = { |
82 | {"client_exit", (hookfn) safelist_check_cliexit}, | |
83 | {NULL, NULL} | |
84 | }; | |
85 | ||
86 | DECLARE_MODULE_AV1(list, _modinit, _moddeinit, list_clist, NULL, list_hfnlist, "$Revision: 3372 $"); | |
87 | ||
88 | static struct ev_entry *iterate_clients_ev = NULL; | |
75d60088 | 89 | |
212380e3 AC |
90 | static 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 | ||
108 | static 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 |
116 | static 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 | */ | |
428ca87b | 133 | static int m_list(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[]) |
212380e3 AC |
134 | { |
135 | static time_t last_used = 0L; | |
136 | ||
999fab77 AC |
137 | if (source_p->localClient->safelist_data != NULL) |
138 | { | |
139 | sendto_one_notice(source_p, ":/LIST aborted"); | |
140 | safelist_client_release(source_p); | |
141 | return 0; | |
142 | } | |
143 | ||
212380e3 AC |
144 | if (parc < 2 || !IsChannelName(parv[1])) |
145 | { | |
146 | /* pace this due to the sheer traffic involved */ | |
e3354945 | 147 | if (((last_used + ConfigFileEntry.pace_wait) > rb_current_time())) |
212380e3 AC |
148 | { |
149 | sendto_one(source_p, form_str(RPL_LOAD2HI), me.name, source_p->name, "LIST"); | |
150 | sendto_one(source_p, form_str(RPL_LISTEND), me.name, source_p->name); | |
151 | return 0; | |
152 | } | |
153 | else | |
e3354945 | 154 | last_used = rb_current_time(); |
212380e3 AC |
155 | } |
156 | ||
428ca87b | 157 | return mo_list(msgbuf_p, client_p, source_p, parc, parv); |
212380e3 AC |
158 | } |
159 | ||
160 | /* mo_list() | |
212380e3 AC |
161 | * parv[1] = channel |
162 | */ | |
428ca87b | 163 | static int mo_list(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[]) |
212380e3 | 164 | { |
999fab77 | 165 | struct ListClient *params; |
bf0a4592 KB |
166 | char *p; |
167 | char *args = NULL; | |
212380e3 | 168 | int i; |
bf0a4592 | 169 | int operspy = 0; |
212380e3 | 170 | |
999fab77 AC |
171 | if (source_p->localClient->safelist_data != NULL) |
172 | { | |
173 | sendto_one_notice(source_p, ":/LIST aborted"); | |
174 | safelist_client_release(source_p); | |
175 | return 0; | |
176 | } | |
212380e3 | 177 | |
bf0a4592 KB |
178 | if (parc > 1) |
179 | { | |
180 | args = LOCAL_COPY(parv[1]); | |
181 | } | |
182 | ||
183 | if (args && *args == '!' && IsOperSpy(source_p)) | |
184 | { | |
185 | args++; | |
186 | report_operspy(source_p, "LIST", args); | |
187 | operspy = 1; | |
188 | } | |
189 | ||
096570b9 | 190 | /* Single channel. */ |
bf0a4592 | 191 | if (args && IsChannelName(args)) |
096570b9 | 192 | { |
bf0a4592 | 193 | safelist_channel_named(source_p, args, operspy); |
096570b9 KB |
194 | return 0; |
195 | } | |
196 | ||
197 | /* Multiple channels, possibly with parameters. */ | |
999fab77 | 198 | params = rb_malloc(sizeof(struct ListClient)); |
096570b9 | 199 | |
d513218a | 200 | params->users_min = ConfigChannel.displayed_usercount; |
096570b9 | 201 | params->users_max = INT_MAX; |
bf0a4592 | 202 | params->operspy = operspy; |
55abcbb2 | 203 | params->created_min = params->topic_min = |
096570b9 | 204 | params->created_max = params->topic_max = 0; |
212380e3 | 205 | |
bf0a4592 | 206 | if (args && !EmptyString(args)) |
212380e3 | 207 | { |
bb55ebeb | 208 | /* Cancel out default minimum. */ |
096570b9 | 209 | params->users_min = 0; |
212380e3 | 210 | |
096570b9 | 211 | for (i = 0; i < 7; i++) |
212380e3 AC |
212 | { |
213 | if ((p = strchr(args, ',')) != NULL) | |
214 | *p++ = '\0'; | |
215 | ||
216 | if (*args == '<') | |
217 | { | |
218 | args++; | |
219 | if (IsDigit(*args)) | |
220 | { | |
096570b9 KB |
221 | params->users_max = atoi(args); |
222 | if (params->users_max == 0) | |
223 | params->users_max = INT_MAX; | |
212380e3 | 224 | else |
096570b9 | 225 | params->users_max--; |
212380e3 | 226 | } |
212380e3 AC |
227 | } |
228 | else if (*args == '>') | |
229 | { | |
230 | args++; | |
231 | if (IsDigit(*args)) | |
096570b9 | 232 | params->users_min = atoi(args) + 1; |
212380e3 | 233 | else |
096570b9 KB |
234 | params->users_min = 0; |
235 | } | |
236 | else if (*args == 'C' || *args == 'c') | |
237 | { | |
238 | args++; | |
239 | if (*args == '>') | |
240 | { | |
241 | /* Creation time earlier than last x minutes. */ | |
242 | args++; | |
243 | if (IsDigit(*args)) | |
244 | { | |
245 | params->created_max = rb_current_time() - (60 * atoi(args)); | |
246 | } | |
247 | } | |
248 | else if (*args == '<') | |
249 | { | |
250 | /* Creation time within last x minutes. */ | |
251 | args++; | |
252 | if (IsDigit(*args)) | |
253 | { | |
254 | params->created_min = rb_current_time() - (60 * atoi(args)); | |
255 | } | |
256 | } | |
257 | } | |
258 | else if (*args == 'T' || *args == 't') | |
259 | { | |
260 | args++; | |
261 | if (*args == '>') | |
262 | { | |
263 | /* Topic change time earlier than last x minutes. */ | |
264 | args++; | |
265 | if (IsDigit(*args)) | |
266 | { | |
267 | params->topic_max = rb_current_time() - (60 * atoi(args)); | |
268 | } | |
269 | } | |
270 | else if (*args == '<') | |
271 | { | |
272 | /* Topic change time within last x minutes. */ | |
273 | args++; | |
274 | if (IsDigit(*args)) | |
275 | { | |
276 | params->topic_min = rb_current_time() - (60 * atoi(args)); | |
277 | } | |
278 | } | |
212380e3 AC |
279 | } |
280 | ||
281 | if (EmptyString(p)) | |
282 | break; | |
283 | else | |
284 | args = p; | |
285 | } | |
286 | } | |
212380e3 | 287 | |
999fab77 | 288 | safelist_client_instantiate(source_p, params); |
212380e3 AC |
289 | |
290 | return 0; | |
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 | */ | |
300 | static 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 | |
320 | * outputs - 1 if a client has exceeded the reserved | |
321 | * sendq limit, 0 if not | |
322 | * side effects - none | |
323 | * | |
324 | * When safelisting, we only use half of the SendQ at any | |
325 | * given time. | |
326 | */ | |
327 | static int safelist_sendq_exceeded(struct Client *client_p) | |
328 | { | |
75d60088 | 329 | if (rb_linebuf_len(&client_p->localClient->buf_sendq) > (get_sendq(client_p) / 2)) |
212380e3 AC |
330 | return YES; |
331 | else | |
332 | return NO; | |
333 | } | |
334 | ||
999fab77 AC |
335 | /* |
336 | * safelist_client_instantiate() | |
337 | * | |
338 | * inputs - pointer to Client to be listed, | |
339 | * pointer to ListClient for params | |
340 | * outputs - none | |
341 | * side effects - the safelist process begins for a | |
342 | * client. | |
343 | * | |
344 | * Please do not ever call this on a non-local client. | |
345 | * If you do, you will get SIGSEGV. | |
346 | */ | |
347 | static void safelist_client_instantiate(struct Client *client_p, struct ListClient *params) | |
348 | { | |
349 | s_assert(MyClient(client_p)); | |
350 | s_assert(params != NULL); | |
351 | ||
352 | client_p->localClient->safelist_data = params; | |
353 | ||
354 | sendto_one(client_p, form_str(RPL_LISTSTART), me.name, client_p->name); | |
355 | ||
356 | /* pop the client onto the queue for processing */ | |
357 | rb_dlinkAddAlloc(client_p, &safelisting_clients); | |
358 | ||
359 | /* give the user some initial data to work with */ | |
360 | safelist_iterate_client(client_p); | |
361 | } | |
362 | ||
363 | /* | |
364 | * safelist_client_release() | |
365 | * | |
366 | * inputs - pointer to Client being listed on | |
367 | * outputs - none | |
368 | * side effects - the client is no longer being | |
369 | * listed | |
370 | * | |
371 | * Please do not ever call this on a non-local client. | |
372 | * If you do, you will get SIGSEGV. | |
373 | */ | |
374 | static void safelist_client_release(struct Client *client_p) | |
375 | { | |
376 | s_assert(MyClient(client_p)); | |
377 | ||
378 | rb_dlinkFindDestroy(client_p, &safelisting_clients); | |
379 | ||
380 | rb_free(client_p->localClient->safelist_data->chname); | |
381 | rb_free(client_p->localClient->safelist_data); | |
382 | ||
383 | client_p->localClient->safelist_data = NULL; | |
384 | ||
385 | sendto_one(client_p, form_str(RPL_LISTEND), me.name, client_p->name); | |
386 | } | |
387 | ||
212380e3 AC |
388 | /* |
389 | * safelist_channel_named() | |
390 | * | |
bf0a4592 | 391 | * inputs - client pointer, channel name, operspy |
212380e3 AC |
392 | * outputs - none |
393 | * side effects - a named channel is listed | |
394 | */ | |
bf0a4592 | 395 | static void safelist_channel_named(struct Client *source_p, const char *name, int operspy) |
212380e3 AC |
396 | { |
397 | struct Channel *chptr; | |
398 | char *p; | |
a4eeda89 | 399 | int visible; |
212380e3 AC |
400 | |
401 | sendto_one(source_p, form_str(RPL_LISTSTART), me.name, source_p->name); | |
402 | ||
bf0a4592 | 403 | if ((p = strchr(name, ','))) |
212380e3 AC |
404 | *p = '\0'; |
405 | ||
bf0a4592 | 406 | if (*name == '\0') |
212380e3 AC |
407 | { |
408 | sendto_one_numeric(source_p, ERR_NOSUCHNICK, form_str(ERR_NOSUCHNICK), name); | |
409 | sendto_one(source_p, form_str(RPL_LISTEND), me.name, source_p->name); | |
410 | return; | |
411 | } | |
412 | ||
bf0a4592 | 413 | chptr = find_channel(name); |
212380e3 AC |
414 | |
415 | if (chptr == NULL) | |
416 | { | |
bf0a4592 | 417 | sendto_one_numeric(source_p, ERR_NOSUCHNICK, form_str(ERR_NOSUCHNICK), name); |
212380e3 AC |
418 | sendto_one(source_p, form_str(RPL_LISTEND), me.name, source_p->name); |
419 | return; | |
420 | } | |
421 | ||
a4eeda89 JT |
422 | visible = !SecretChannel(chptr) || IsMember(source_p, chptr); |
423 | if (visible || operspy) | |
69e7a2cd | 424 | list_one_channel(source_p, chptr, visible); |
212380e3 AC |
425 | |
426 | sendto_one(source_p, form_str(RPL_LISTEND), me.name, source_p->name); | |
427 | return; | |
428 | } | |
429 | ||
430 | /* | |
431 | * safelist_one_channel() | |
432 | * | |
433 | * inputs - client pointer and channel pointer | |
434 | * outputs - none | |
435 | * side effects - a channel is listed if it meets the | |
436 | * requirements | |
437 | */ | |
999fab77 | 438 | static void safelist_one_channel(struct Client *source_p, struct Channel *chptr, struct ListClient *params) |
212380e3 | 439 | { |
a4eeda89 | 440 | int visible; |
212380e3 | 441 | |
a4eeda89 | 442 | visible = !SecretChannel(chptr) || IsMember(source_p, chptr); |
730b914c | 443 | if (!visible && !params->operspy) |
212380e3 AC |
444 | return; |
445 | ||
730b914c AC |
446 | if ((unsigned int)chptr->members.length < params->users_min |
447 | || (unsigned int)chptr->members.length > params->users_max) | |
212380e3 AC |
448 | return; |
449 | ||
730b914c | 450 | if (params->topic_min && chptr->topic_time < params->topic_min) |
096570b9 KB |
451 | return; |
452 | ||
453 | /* If a topic TS is provided, don't show channels without a topic set. */ | |
730b914c | 454 | if (params->topic_max && (chptr->topic_time > params->topic_max |
096570b9 KB |
455 | || chptr->topic_time == 0)) |
456 | return; | |
457 | ||
730b914c | 458 | if (params->created_min && chptr->channelts < params->created_min) |
096570b9 KB |
459 | return; |
460 | ||
730b914c | 461 | if (params->created_max && chptr->channelts > params->created_max) |
096570b9 KB |
462 | return; |
463 | ||
69e7a2cd | 464 | list_one_channel(source_p, chptr, visible); |
212380e3 | 465 | } |
999fab77 AC |
466 | |
467 | /* | |
468 | * safelist_iterate_client() | |
469 | * | |
470 | * inputs - client pointer | |
471 | * outputs - none | |
472 | * side effects - the client's sendq is filled up again | |
473 | */ | |
474 | static void safelist_iterate_client(struct Client *source_p) | |
475 | { | |
476 | struct Channel *chptr; | |
477 | struct irc_radixtree_iteration_state iter; | |
478 | ||
479 | IRC_RADIXTREE_FOREACH_FROM(chptr, &iter, channel_tree, source_p->localClient->safelist_data->chname) | |
480 | { | |
481 | if (safelist_sendq_exceeded(source_p->from) == YES) | |
482 | { | |
b9a32bd2 | 483 | rb_free(source_p->localClient->safelist_data->chname); |
999fab77 | 484 | source_p->localClient->safelist_data->chname = rb_strdup(chptr->chname); |
b9a32bd2 | 485 | |
999fab77 AC |
486 | return; |
487 | } | |
488 | ||
489 | safelist_one_channel(source_p, chptr, source_p->localClient->safelist_data); | |
490 | } | |
491 | ||
492 | safelist_client_release(source_p); | |
493 | } | |
494 | ||
495 | static void safelist_iterate_clients(void *unused) | |
496 | { | |
497 | rb_dlink_node *n, *n2; | |
498 | ||
499 | RB_DLINK_FOREACH_SAFE(n, n2, safelisting_clients.head) | |
500 | safelist_iterate_client((struct Client *)n->data); | |
501 | } |