]> jfr.im git - irc/rqf/shadowircd.git/blame - modules/m_list.c
Add some ircd-seven love to CREDITS.
[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 *
607cf49f 33 * $Id: m_list.c 3372 2007-04-03 10:18:07Z nenolod $
212380e3 34 */
35
36#include "stdinc.h"
212380e3 37#include "channel.h"
38#include "client.h"
39#include "hash.h"
13ae2f4b 40#include "match.h"
212380e3 41#include "ircd.h"
42#include "numeric.h"
43#include "s_conf.h"
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");
118 sendto_one(source_p, form_str(RPL_LISTEND), me.name, source_p->name);
119 safelist_client_release(source_p);
120 return 0;
121 }
122
123 if (parc < 2 || !IsChannelName(parv[1]))
124 {
125 /* pace this due to the sheer traffic involved */
9f6bbe3c 126 if (((last_used + ConfigFileEntry.pace_wait) > rb_current_time()))
212380e3 127 {
128 sendto_one(source_p, form_str(RPL_LOAD2HI), me.name, source_p->name, "LIST");
129 sendto_one(source_p, form_str(RPL_LISTEND), me.name, source_p->name);
130 return 0;
131 }
132 else
9f6bbe3c 133 last_used = rb_current_time();
212380e3 134 }
135
136 return mo_list(client_p, source_p, parc, parv);
137}
138
139/* mo_list()
212380e3 140 * parv[1] = channel
141 */
142static int mo_list(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
143{
144 struct ListClient params;
145 char *p, *args;
146 int i;
147
148 if (source_p->localClient->safelist_data != NULL)
149 {
150 sendto_one_notice(source_p, ":/LIST aborted");
151 sendto_one(source_p, form_str(RPL_LISTEND), me.name, source_p->name);
152 safelist_client_release(source_p);
153 return 0;
154 }
155
156 /* XXX rather arbitrary -- jilles */
157 params.users_min = 3;
158 params.users_max = INT_MAX;
159
160 if (parc > 1 && parv[1] != NULL && !IsChannelName(parv[1]))
161 {
162 args = LOCAL_COPY(parv[1]);
163 /* Make any specification cancel out defaults */
164 if (*args == '<')
165 params.users_min = 0;
166
167 for (i = 0; i < 2; i++)
168 {
169 if ((p = strchr(args, ',')) != NULL)
170 *p++ = '\0';
171
172 if (*args == '<')
173 {
174 args++;
175 if (IsDigit(*args))
176 {
177 params.users_max = atoi(args);
178 if (params.users_max == 0)
179 params.users_max = INT_MAX;
180 else
181 params.users_max--;
182 }
183 else
184 params.users_max = INT_MAX;
185 }
186 else if (*args == '>')
187 {
188 args++;
189 if (IsDigit(*args))
190 params.users_min = atoi(args) + 1;
191 else
192 params.users_min = 0;
193 }
194
195 if (EmptyString(p))
196 break;
197 else
198 args = p;
199 }
200 }
201 else if (parc > 1 && IsChannelName(parv[1]))
202 {
203 safelist_channel_named(source_p, parv[1]);
204 return 0;
205 }
206
207 safelist_client_instantiate(source_p, &params);
208
209 return 0;
210}
211
212/*
213 * safelist_sendq_exceeded()
214 *
215 * inputs - pointer to client that needs checking
216 * outputs - 1 if a client has exceeded the reserved
217 * sendq limit, 0 if not
218 * side effects - none
219 *
220 * When safelisting, we only use half of the SendQ at any
221 * given time.
222 */
223static int safelist_sendq_exceeded(struct Client *client_p)
224{
03e24016 225 if (rb_linebuf_len(&client_p->localClient->buf_sendq) > (get_sendq(client_p) / 2))
212380e3 226 return YES;
227 else
228 return NO;
229}
230
231/*
232 * safelist_client_instantiate()
233 *
234 * inputs - pointer to Client to be listed,
235 * struct ListClient to copy for params
236 * outputs - none
237 * side effects - the safelist process begins for a
238 * client.
239 *
240 * Please do not ever call this on a non-local client.
241 * If you do, you will get SIGSEGV.
242 */
243static void safelist_client_instantiate(struct Client *client_p, struct ListClient *params)
244{
245 struct ListClient *self;
246
247 s_assert(MyClient(client_p));
248 s_assert(params != NULL);
249
8e43b0b4 250 self = rb_malloc(sizeof(struct ListClient));
212380e3 251
252 self->hash_indice = 0;
253 self->users_min = params->users_min;
254 self->users_max = params->users_max;
255
256 client_p->localClient->safelist_data = self;
257
258 sendto_one(client_p, form_str(RPL_LISTSTART), me.name, client_p->name);
259
260 /* pop the client onto the queue for processing */
03e24016 261 rb_dlinkAddAlloc(client_p, &safelisting_clients);
212380e3 262
263 /* give the user some initial data to work with */
264 safelist_iterate_client(client_p);
265}
266
267/*
268 * safelist_client_release()
269 *
270 * inputs - pointer to Client being listed on
271 * outputs - none
272 * side effects - the client is no longer being
273 * listed
274 *
275 * Please do not ever call this on a non-local client.
276 * If you do, you will get SIGSEGV.
277 */
278static void safelist_client_release(struct Client *client_p)
279{
280 s_assert(MyClient(client_p));
281
9f6c3353 282 rb_dlinkFindDestroy(client_p, &safelisting_clients);
212380e3 283
90a3c35b 284 rb_free(client_p->localClient->safelist_data);
212380e3 285
286 client_p->localClient->safelist_data = NULL;
287
288 sendto_one(client_p, form_str(RPL_LISTEND), me.name, client_p->name);
289}
290
291/*
292 * safelist_channel_named()
293 *
294 * inputs - client pointer, channel name
295 * outputs - none
296 * side effects - a named channel is listed
297 */
298static void safelist_channel_named(struct Client *source_p, const char *name)
299{
300 struct Channel *chptr;
301 char *p;
302 char *n = LOCAL_COPY(name);
303
304 sendto_one(source_p, form_str(RPL_LISTSTART), me.name, source_p->name);
305
306 if ((p = strchr(n, ',')))
307 *p = '\0';
308
309 if (*n == '\0')
310 {
311 sendto_one_numeric(source_p, ERR_NOSUCHNICK, form_str(ERR_NOSUCHNICK), name);
312 sendto_one(source_p, form_str(RPL_LISTEND), me.name, source_p->name);
313 return;
314 }
315
316 chptr = find_channel(n);
317
318 if (chptr == NULL)
319 {
320 sendto_one_numeric(source_p, ERR_NOSUCHNICK, form_str(ERR_NOSUCHNICK), n);
321 sendto_one(source_p, form_str(RPL_LISTEND), me.name, source_p->name);
322 return;
323 }
324
97a6160b 325 if (!SecretChannel(chptr) || IsMember(source_p, chptr))
212380e3 326 sendto_one(source_p, form_str(RPL_LIST), me.name, source_p->name, chptr->chname,
08d11e34 327 rb_dlink_list_length(&chptr->members),
212380e3 328 chptr->topic == NULL ? "" : chptr->topic);
329
330 sendto_one(source_p, form_str(RPL_LISTEND), me.name, source_p->name);
331 return;
332}
333
334/*
335 * safelist_one_channel()
336 *
337 * inputs - client pointer and channel pointer
338 * outputs - none
339 * side effects - a channel is listed if it meets the
340 * requirements
341 */
342static void safelist_one_channel(struct Client *source_p, struct Channel *chptr)
343{
344 struct ListClient *safelist_data = source_p->localClient->safelist_data;
345
346 if (SecretChannel(chptr) && !IsMember(source_p, chptr))
347 return;
348
349 if ((unsigned int)chptr->members.length < safelist_data->users_min
350 || (unsigned int)chptr->members.length > safelist_data->users_max)
351 return;
352
353 sendto_one(source_p, form_str(RPL_LIST), me.name, source_p->name, chptr->chname,
354 chptr->members.length, chptr->topic == NULL ? "" : chptr->topic);
355}
356
357/*
358 * safelist_iterate_client()
359 *
360 * inputs - client pointer
361 * outputs - none
362 * side effects - the client's sendq is filled up again
363 */
364static void safelist_iterate_client(struct Client *source_p)
365{
08d11e34 366 rb_dlink_node *ptr;
212380e3 367 int iter;
368
369 for (iter = source_p->localClient->safelist_data->hash_indice; iter < CH_MAX; iter++)
370 {
371 if (safelist_sendq_exceeded(source_p->from) == YES)
372 {
373 source_p->localClient->safelist_data->hash_indice = iter;
374 return;
375 }
376
08d11e34 377 RB_DLINK_FOREACH(ptr, channelTable[iter].head)
212380e3 378 safelist_one_channel(source_p, (struct Channel *) ptr->data);
379 }
380
381 safelist_client_release(source_p);
382}
383
384static void safelist_iterate_clients(void *unused)
385{
08d11e34 386 rb_dlink_node *n, *n2;
212380e3 387
08d11e34 388 RB_DLINK_FOREACH_SAFE(n, n2, safelisting_clients.head)
212380e3 389 safelist_iterate_client((struct Client *)n->data);
390}