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