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