]> jfr.im git - solanum.git/blob - modules/m_list.c
Clean these modules up.
[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 "tools.h"
38 #include "channel.h"
39 #include "client.h"
40 #include "hash.h"
41 #include "irc_string.h"
42 #include "ircd.h"
43 #include "numeric.h"
44 #include "s_conf.h"
45 #include "s_serv.h"
46 #include "send.h"
47 #include "msg.h"
48 #include "parse.h"
49 #include "modules.h"
50 #include "event.h"
51
52 static rb_dlink_list safelisting_clients = { NULL, NULL, 0 };
53
54 static int _modinit(void);
55 static void _moddeinit(void);
56
57 static int m_list(struct Client *, struct Client *, int, const char **);
58 static int mo_list(struct Client *, struct Client *, int, const char **);
59
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);
67
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}}
71 };
72
73 mapi_clist_av1 list_clist[] = { &list_msgtab, NULL };
74
75 mapi_hfn_list_av1 list_hfnlist[] = {
76 {"client_exit", (hookfn) safelist_check_cliexit},
77 {NULL, NULL}
78 };
79
80 DECLARE_MODULE_AV1(list, _modinit, _moddeinit, list_clist, NULL, list_hfnlist, "$Revision: 3372 $");
81
82 static struct ev_entry *iterate_clients_ev = NULL;
83
84 static int _modinit(void)
85 {
86 iterate_clients_ev = rb_event_add("safelist_iterate_clients", safelist_iterate_clients, NULL, 3);
87
88 return 0;
89 }
90
91 static void _moddeinit(void)
92 {
93 rb_event_delete(iterate_clients_ev);
94 }
95
96 static void safelist_check_cliexit(hook_data_client_exit * hdata)
97 {
98 /* Cancel the safelist request if we are disconnecting
99 * from the server. That way it doesn't core. :P --nenolod
100 */
101 if (MyClient(hdata->target) && hdata->target->localClient->safelist_data != NULL)
102 {
103 safelist_client_release(hdata->target);
104 }
105 }
106
107 /* m_list()
108 * parv[0] = sender prefix
109 * parv[1] = channel
110 *
111 * XXX - With SAFELIST, do we really need to continue pacing?
112 * In theory, the server cannot be lagged by this. --nenolod
113 */
114 static int m_list(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
115 {
116 static time_t last_used = 0L;
117
118 if (source_p->localClient->safelist_data != NULL)
119 {
120 sendto_one_notice(source_p, ":/LIST aborted");
121 sendto_one(source_p, form_str(RPL_LISTEND), me.name, source_p->name);
122 safelist_client_release(source_p);
123 return 0;
124 }
125
126 if (parc < 2 || !IsChannelName(parv[1]))
127 {
128 /* pace this due to the sheer traffic involved */
129 if (((last_used + ConfigFileEntry.pace_wait) > CurrentTime))
130 {
131 sendto_one(source_p, form_str(RPL_LOAD2HI), me.name, source_p->name, "LIST");
132 sendto_one(source_p, form_str(RPL_LISTEND), me.name, source_p->name);
133 return 0;
134 }
135 else
136 last_used = CurrentTime;
137 }
138
139 return mo_list(client_p, source_p, parc, parv);
140 }
141
142 /* mo_list()
143 * parv[0] = sender prefix
144 * parv[1] = channel
145 */
146 static int mo_list(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
147 {
148 struct ListClient params;
149 char *p, *args;
150 int i;
151
152 if (source_p->localClient->safelist_data != NULL)
153 {
154 sendto_one_notice(source_p, ":/LIST aborted");
155 sendto_one(source_p, form_str(RPL_LISTEND), me.name, source_p->name);
156 safelist_client_release(source_p);
157 return 0;
158 }
159
160 /* XXX rather arbitrary -- jilles */
161 params.users_min = 3;
162 params.users_max = INT_MAX;
163
164 if (parc > 1 && parv[1] != NULL && !IsChannelName(parv[1]))
165 {
166 args = LOCAL_COPY(parv[1]);
167 /* Make any specification cancel out defaults */
168 if (*args == '<')
169 params.users_min = 0;
170
171 for (i = 0; i < 2; i++)
172 {
173 if ((p = strchr(args, ',')) != NULL)
174 *p++ = '\0';
175
176 if (*args == '<')
177 {
178 args++;
179 if (IsDigit(*args))
180 {
181 params.users_max = atoi(args);
182 if (params.users_max == 0)
183 params.users_max = INT_MAX;
184 else
185 params.users_max--;
186 }
187 else
188 params.users_max = INT_MAX;
189 }
190 else if (*args == '>')
191 {
192 args++;
193 if (IsDigit(*args))
194 params.users_min = atoi(args) + 1;
195 else
196 params.users_min = 0;
197 }
198
199 if (EmptyString(p))
200 break;
201 else
202 args = p;
203 }
204 }
205 else if (parc > 1 && IsChannelName(parv[1]))
206 {
207 safelist_channel_named(source_p, parv[1]);
208 return 0;
209 }
210
211 safelist_client_instantiate(source_p, &params);
212
213 return 0;
214 }
215
216 /*
217 * safelist_sendq_exceeded()
218 *
219 * inputs - pointer to client that needs checking
220 * outputs - 1 if a client has exceeded the reserved
221 * sendq limit, 0 if not
222 * side effects - none
223 *
224 * When safelisting, we only use half of the SendQ at any
225 * given time.
226 */
227 static int safelist_sendq_exceeded(struct Client *client_p)
228 {
229 if (rb_linebuf_len(&client_p->localClient->buf_sendq) > (get_sendq(client_p) / 2))
230 return YES;
231 else
232 return NO;
233 }
234
235 /*
236 * safelist_client_instantiate()
237 *
238 * inputs - pointer to Client to be listed,
239 * struct ListClient to copy for params
240 * outputs - none
241 * side effects - the safelist process begins for a
242 * client.
243 *
244 * Please do not ever call this on a non-local client.
245 * If you do, you will get SIGSEGV.
246 */
247 static void safelist_client_instantiate(struct Client *client_p, struct ListClient *params)
248 {
249 struct ListClient *self;
250
251 s_assert(MyClient(client_p));
252 s_assert(params != NULL);
253
254 self = MyMalloc(sizeof(struct ListClient));
255
256 self->hash_indice = 0;
257 self->users_min = params->users_min;
258 self->users_max = params->users_max;
259
260 client_p->localClient->safelist_data = self;
261
262 sendto_one(client_p, form_str(RPL_LISTSTART), me.name, client_p->name);
263
264 /* pop the client onto the queue for processing */
265 rb_dlinkAddAlloc(client_p, &safelisting_clients);
266
267 /* give the user some initial data to work with */
268 safelist_iterate_client(client_p);
269 }
270
271 /*
272 * safelist_client_release()
273 *
274 * inputs - pointer to Client being listed on
275 * outputs - none
276 * side effects - the client is no longer being
277 * listed
278 *
279 * Please do not ever call this on a non-local client.
280 * If you do, you will get SIGSEGV.
281 */
282 static void safelist_client_release(struct Client *client_p)
283 {
284 s_assert(MyClient(client_p));
285
286 rb_dlinkFindDestroy(client_p, &safelisting_clients);
287
288 MyFree(client_p->localClient->safelist_data);
289
290 client_p->localClient->safelist_data = NULL;
291
292 sendto_one(client_p, form_str(RPL_LISTEND), me.name, client_p->name);
293 }
294
295 /*
296 * safelist_channel_named()
297 *
298 * inputs - client pointer, channel name
299 * outputs - none
300 * side effects - a named channel is listed
301 */
302 static void safelist_channel_named(struct Client *source_p, const char *name)
303 {
304 struct Channel *chptr;
305 char *p;
306 char *n = LOCAL_COPY(name);
307
308 sendto_one(source_p, form_str(RPL_LISTSTART), me.name, source_p->name);
309
310 if ((p = strchr(n, ',')))
311 *p = '\0';
312
313 if (*n == '\0')
314 {
315 sendto_one_numeric(source_p, ERR_NOSUCHNICK, form_str(ERR_NOSUCHNICK), name);
316 sendto_one(source_p, form_str(RPL_LISTEND), me.name, source_p->name);
317 return;
318 }
319
320 chptr = find_channel(n);
321
322 if (chptr == NULL)
323 {
324 sendto_one_numeric(source_p, ERR_NOSUCHNICK, form_str(ERR_NOSUCHNICK), n);
325 sendto_one(source_p, form_str(RPL_LISTEND), me.name, source_p->name);
326 return;
327 }
328
329 if (ShowChannel(source_p, chptr))
330 sendto_one(source_p, form_str(RPL_LIST), me.name, source_p->name, chptr->chname,
331 rb_dlink_list_length(&chptr->members),
332 chptr->topic == NULL ? "" : chptr->topic);
333
334 sendto_one(source_p, form_str(RPL_LISTEND), me.name, source_p->name);
335 return;
336 }
337
338 /*
339 * safelist_one_channel()
340 *
341 * inputs - client pointer and channel pointer
342 * outputs - none
343 * side effects - a channel is listed if it meets the
344 * requirements
345 */
346 static void safelist_one_channel(struct Client *source_p, struct Channel *chptr)
347 {
348 struct ListClient *safelist_data = source_p->localClient->safelist_data;
349
350 if (SecretChannel(chptr) && !IsMember(source_p, chptr))
351 return;
352
353 if ((unsigned int)chptr->members.length < safelist_data->users_min
354 || (unsigned int)chptr->members.length > safelist_data->users_max)
355 return;
356
357 sendto_one(source_p, form_str(RPL_LIST), me.name, source_p->name, chptr->chname,
358 chptr->members.length, chptr->topic == NULL ? "" : chptr->topic);
359 }
360
361 /*
362 * safelist_iterate_client()
363 *
364 * inputs - client pointer
365 * outputs - none
366 * side effects - the client's sendq is filled up again
367 */
368 static void safelist_iterate_client(struct Client *source_p)
369 {
370 rb_dlink_node *ptr;
371 int iter;
372
373 for (iter = source_p->localClient->safelist_data->hash_indice; iter < CH_MAX; iter++)
374 {
375 if (safelist_sendq_exceeded(source_p->from) == YES)
376 {
377 source_p->localClient->safelist_data->hash_indice = iter;
378 return;
379 }
380
381 RB_DLINK_FOREACH(ptr, channelTable[iter].head)
382 safelist_one_channel(source_p, (struct Channel *) ptr->data);
383 }
384
385 safelist_client_release(source_p);
386 }
387
388 static void safelist_iterate_clients(void *unused)
389 {
390 rb_dlink_node *n, *n2;
391
392 RB_DLINK_FOREACH_SAFE(n, n2, safelisting_clients.head)
393 safelist_iterate_client((struct Client *)n->data);
394 }