2 * charybdis: An advanced ircd.
3 * m_list_safelist.c: Version of /list that uses the safelist code.
5 * Copyright (c) 2006 William Pitcock <nenolod@nenolod.net>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
11 * 1. Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
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.
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.
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.
33 * $Id: m_list.c 3372 2007-04-03 10:18:07Z nenolod $
44 #include "s_newconf.h"
46 #include "supported.h"
52 static rb_dlink_list safelisting_clients
= { NULL
, NULL
, 0 };
54 static int _modinit(void);
55 static void _moddeinit(void);
57 static int m_list(struct Client
*, struct Client
*, int, const char **);
58 static int mo_list(struct Client
*, struct Client
*, int, const char **);
60 static void safelist_check_cliexit(hook_data_client_exit
* hdata
);
61 static void safelist_client_instantiate(struct Client
*, struct ListClient
*);
62 static void safelist_client_release(struct Client
*);
63 static void safelist_one_channel(struct Client
*source_p
, struct Channel
*chptr
);
64 static void safelist_iterate_client(struct Client
*source_p
);
65 static void safelist_iterate_clients(void *unused
);
66 static void safelist_channel_named(struct Client
*source_p
, const char *name
, int operspy
);
68 struct Message list_msgtab
= {
69 "LIST", 0, 0, 0, MFLG_SLOW
,
70 {mg_unreg
, {m_list
, 0}, mg_ignore
, mg_ignore
, mg_ignore
, {mo_list
, 0}}
73 mapi_clist_av1 list_clist
[] = { &list_msgtab
, NULL
};
75 mapi_hfn_list_av1 list_hfnlist
[] = {
76 {"client_exit", (hookfn
) safelist_check_cliexit
},
80 DECLARE_MODULE_AV1(list
, _modinit
, _moddeinit
, list_clist
, NULL
, list_hfnlist
, "$Revision: 3372 $");
82 static struct ev_entry
*iterate_clients_ev
= NULL
;
84 static int _modinit(void)
86 iterate_clients_ev
= rb_event_add("safelist_iterate_clients", safelist_iterate_clients
, NULL
, 3);
92 * U = user count search (< >)
93 * C = creation time search (C> C<)
94 * T = topic search (T> T<)
96 add_isupport("SAFELIST", isupport_string
, "");
97 add_isupport("ELIST", isupport_string
, "CTU");
102 static void _moddeinit(void)
104 rb_event_delete(iterate_clients_ev
);
106 delete_isupport("SAFELIST");
107 delete_isupport("ELIST");
110 static void safelist_check_cliexit(hook_data_client_exit
* hdata
)
112 /* Cancel the safelist request if we are disconnecting
113 * from the server. That way it doesn't core. :P --nenolod
115 if (MyClient(hdata
->target
) && hdata
->target
->localClient
->safelist_data
!= NULL
)
117 safelist_client_release(hdata
->target
);
124 * XXX - With SAFELIST, do we really need to continue pacing?
125 * In theory, the server cannot be lagged by this. --nenolod
127 static int m_list(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
129 static time_t last_used
= 0L;
131 if (source_p
->localClient
->safelist_data
!= NULL
)
133 sendto_one_notice(source_p
, ":/LIST aborted");
134 safelist_client_release(source_p
);
138 if (parc
< 2 || !IsChannelName(parv
[1]))
140 /* pace this due to the sheer traffic involved */
141 if (((last_used
+ ConfigFileEntry
.pace_wait
) > rb_current_time()))
143 sendto_one(source_p
, form_str(RPL_LOAD2HI
), me
.name
, source_p
->name
, "LIST");
144 sendto_one(source_p
, form_str(RPL_LISTEND
), me
.name
, source_p
->name
);
148 last_used
= rb_current_time();
151 return mo_list(client_p
, source_p
, parc
, parv
);
157 static int mo_list(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
159 struct ListClient
*params
;
165 if (source_p
->localClient
->safelist_data
!= NULL
)
167 sendto_one_notice(source_p
, ":/LIST aborted");
168 safelist_client_release(source_p
);
174 args
= LOCAL_COPY(parv
[1]);
177 if (args
&& *args
== '!' && IsOperSpy(source_p
))
180 report_operspy(source_p
, "LIST", args
);
184 /* Single channel. */
185 if (args
&& IsChannelName(args
))
187 safelist_channel_named(source_p
, args
, operspy
);
191 /* Multiple channels, possibly with parameters. */
192 params
= rb_malloc(sizeof(struct ListClient
));
194 /* XXX rather arbitrary -- jilles */
195 params
->users_min
= 3;
196 params
->users_max
= INT_MAX
;
197 params
->operspy
= operspy
;
198 params
->created_min
= params
->topic_min
=
199 params
->created_max
= params
->topic_max
= 0;
201 if (args
&& !EmptyString(args
))
203 /* Cancel out default minimum. */
204 params
->users_min
= 0;
206 for (i
= 0; i
< 7; i
++)
208 if ((p
= strchr(args
, ',')) != NULL
)
216 params
->users_max
= atoi(args
);
217 if (params
->users_max
== 0)
218 params
->users_max
= INT_MAX
;
223 else if (*args
== '>')
227 params
->users_min
= atoi(args
) + 1;
229 params
->users_min
= 0;
231 else if (*args
== 'C' || *args
== 'c')
236 /* Creation time earlier than last x minutes. */
240 params
->created_max
= rb_current_time() - (60 * atoi(args
));
243 else if (*args
== '<')
245 /* Creation time within last x minutes. */
249 params
->created_min
= rb_current_time() - (60 * atoi(args
));
253 else if (*args
== 'T' || *args
== 't')
258 /* Topic change time earlier than last x minutes. */
262 params
->topic_max
= rb_current_time() - (60 * atoi(args
));
265 else if (*args
== '<')
267 /* Topic change time within last x minutes. */
271 params
->topic_min
= rb_current_time() - (60 * atoi(args
));
283 safelist_client_instantiate(source_p
, params
);
289 * safelist_sendq_exceeded()
291 * inputs - pointer to client that needs checking
292 * outputs - 1 if a client has exceeded the reserved
293 * sendq limit, 0 if not
294 * side effects - none
296 * When safelisting, we only use half of the SendQ at any
299 static int safelist_sendq_exceeded(struct Client
*client_p
)
301 if (rb_linebuf_len(&client_p
->localClient
->buf_sendq
) > (get_sendq(client_p
) / 2))
308 * safelist_client_instantiate()
310 * inputs - pointer to Client to be listed,
311 * pointer to ListClient for params
313 * side effects - the safelist process begins for a
316 * Please do not ever call this on a non-local client.
317 * If you do, you will get SIGSEGV.
319 static void safelist_client_instantiate(struct Client
*client_p
, struct ListClient
*params
)
321 s_assert(MyClient(client_p
));
322 s_assert(params
!= NULL
);
324 client_p
->localClient
->safelist_data
= params
;
326 sendto_one(client_p
, form_str(RPL_LISTSTART
), me
.name
, client_p
->name
);
328 /* pop the client onto the queue for processing */
329 rb_dlinkAddAlloc(client_p
, &safelisting_clients
);
331 /* give the user some initial data to work with */
332 safelist_iterate_client(client_p
);
336 * safelist_client_release()
338 * inputs - pointer to Client being listed on
340 * side effects - the client is no longer being
343 * Please do not ever call this on a non-local client.
344 * If you do, you will get SIGSEGV.
346 static void safelist_client_release(struct Client
*client_p
)
348 s_assert(MyClient(client_p
));
350 rb_dlinkFindDestroy(client_p
, &safelisting_clients
);
352 rb_free(client_p
->localClient
->safelist_data
);
354 client_p
->localClient
->safelist_data
= NULL
;
356 sendto_one(client_p
, form_str(RPL_LISTEND
), me
.name
, client_p
->name
);
360 * safelist_channel_named()
362 * inputs - client pointer, channel name, operspy
364 * side effects - a named channel is listed
366 static void safelist_channel_named(struct Client
*source_p
, const char *name
, int operspy
)
368 struct Channel
*chptr
;
372 sendto_one(source_p
, form_str(RPL_LISTSTART
), me
.name
, source_p
->name
);
374 if ((p
= strchr(name
, ',')))
379 sendto_one_numeric(source_p
, ERR_NOSUCHNICK
, form_str(ERR_NOSUCHNICK
), name
);
380 sendto_one(source_p
, form_str(RPL_LISTEND
), me
.name
, source_p
->name
);
384 chptr
= find_channel(name
);
388 sendto_one_numeric(source_p
, ERR_NOSUCHNICK
, form_str(ERR_NOSUCHNICK
), name
);
389 sendto_one(source_p
, form_str(RPL_LISTEND
), me
.name
, source_p
->name
);
393 visible
= !SecretChannel(chptr
) || IsMember(source_p
, chptr
);
394 if (visible
|| operspy
)
395 sendto_one(source_p
, form_str(RPL_LIST
), me
.name
, source_p
->name
,
397 chptr
->chname
, rb_dlink_list_length(&chptr
->members
),
398 chptr
->topic
== NULL
? "" : chptr
->topic
);
400 sendto_one(source_p
, form_str(RPL_LISTEND
), me
.name
, source_p
->name
);
405 * safelist_one_channel()
407 * inputs - client pointer and channel pointer
409 * side effects - a channel is listed if it meets the
412 static void safelist_one_channel(struct Client
*source_p
, struct Channel
*chptr
)
414 struct ListClient
*safelist_data
= source_p
->localClient
->safelist_data
;
417 visible
= !SecretChannel(chptr
) || IsMember(source_p
, chptr
);
418 if (!visible
&& !safelist_data
->operspy
)
421 if ((unsigned int)chptr
->members
.length
< safelist_data
->users_min
422 || (unsigned int)chptr
->members
.length
> safelist_data
->users_max
)
425 if (safelist_data
->topic_min
&& chptr
->topic_time
< safelist_data
->topic_min
)
428 /* If a topic TS is provided, don't show channels without a topic set. */
429 if (safelist_data
->topic_max
&& (chptr
->topic_time
> safelist_data
->topic_max
430 || chptr
->topic_time
== 0))
433 if (safelist_data
->created_min
&& chptr
->channelts
< safelist_data
->created_min
)
436 if (safelist_data
->created_max
&& chptr
->channelts
> safelist_data
->created_max
)
439 sendto_one(source_p
, form_str(RPL_LIST
), me
.name
, source_p
->name
,
441 chptr
->chname
, rb_dlink_list_length(&chptr
->members
),
442 chptr
->topic
== NULL
? "" : chptr
->topic
);
446 * safelist_iterate_client()
448 * inputs - client pointer
450 * side effects - the client's sendq is filled up again
452 static void safelist_iterate_client(struct Client
*source_p
)
457 for (iter
= source_p
->localClient
->safelist_data
->hash_indice
; iter
< CH_MAX
; iter
++)
459 if (safelist_sendq_exceeded(source_p
->from
) == YES
)
461 source_p
->localClient
->safelist_data
->hash_indice
= iter
;
465 RB_DLINK_FOREACH(ptr
, channelTable
[iter
].head
)
466 safelist_one_channel(source_p
, (struct Channel
*) ptr
->data
);
469 safelist_client_release(source_p
);
472 static void safelist_iterate_clients(void *unused
)
474 rb_dlink_node
*n
, *n2
;
476 RB_DLINK_FOREACH_SAFE(n
, n2
, safelisting_clients
.head
)
477 safelist_iterate_client((struct Client
*)n
->data
);