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.
49 static rb_dlink_list safelisting_clients
= { NULL
, NULL
, 0 };
51 static int _modinit(void);
52 static void _moddeinit(void);
54 static int m_list(struct Client
*, struct Client
*, int, const char **);
55 static int mo_list(struct Client
*, struct Client
*, int, const char **);
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
);
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}}
70 mapi_clist_av1 list_clist
[] = { &list_msgtab
, NULL
};
72 mapi_hfn_list_av1 list_hfnlist
[] = {
73 {"client_exit", (hookfn
) safelist_check_cliexit
},
77 DECLARE_MODULE_AV1(list
, _modinit
, _moddeinit
, list_clist
, NULL
, list_hfnlist
, "$Revision: 3372 $");
79 static struct ev_entry
*iterate_clients_ev
= NULL
;
81 static int _modinit(void)
83 iterate_clients_ev
= rb_event_add("safelist_iterate_clients", safelist_iterate_clients
, NULL
, 3);
88 static void _moddeinit(void)
90 rb_event_delete(iterate_clients_ev
);
93 static void safelist_check_cliexit(hook_data_client_exit
* hdata
)
95 /* Cancel the safelist request if we are disconnecting
96 * from the server. That way it doesn't core. :P --nenolod
98 if (MyClient(hdata
->target
) && hdata
->target
->localClient
->safelist_data
!= NULL
)
100 safelist_client_release(hdata
->target
);
107 * XXX - With SAFELIST, do we really need to continue pacing?
108 * In theory, the server cannot be lagged by this. --nenolod
110 static int m_list(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
112 static time_t last_used
= 0L;
114 if (source_p
->localClient
->safelist_data
!= NULL
)
116 sendto_one_notice(source_p
, ":/LIST aborted");
117 sendto_one(source_p
, form_str(RPL_LISTEND
), me
.name
, source_p
->name
);
118 safelist_client_release(source_p
);
122 if (parc
< 2 || !IsChannelName(parv
[1]))
124 /* pace this due to the sheer traffic involved */
125 if (((last_used
+ ConfigFileEntry
.pace_wait
) > rb_current_time()))
127 sendto_one(source_p
, form_str(RPL_LOAD2HI
), me
.name
, source_p
->name
, "LIST");
128 sendto_one(source_p
, form_str(RPL_LISTEND
), me
.name
, source_p
->name
);
132 last_used
= rb_current_time();
135 return mo_list(client_p
, source_p
, parc
, parv
);
141 static int mo_list(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
143 struct ListClient params
;
147 if (source_p
->localClient
->safelist_data
!= NULL
)
149 sendto_one_notice(source_p
, ":/LIST aborted");
150 sendto_one(source_p
, form_str(RPL_LISTEND
), me
.name
, source_p
->name
);
151 safelist_client_release(source_p
);
155 /* Let the user set it */
156 params
.users_min
= ConfigFileEntry
.hide_channel_below_users
;
157 params
.users_max
= INT_MAX
;
159 if (parc
> 1 && parv
[1] != NULL
&& !IsChannelName(parv
[1]))
161 args
= LOCAL_COPY(parv
[1]);
162 /* Make any specification cancel out defaults */
164 params
.users_min
= 0;
166 for (i
= 0; i
< 2; i
++)
168 if ((p
= strchr(args
, ',')) != NULL
)
176 params
.users_max
= atoi(args
);
177 if (params
.users_max
== 0)
178 params
.users_max
= INT_MAX
;
183 params
.users_max
= INT_MAX
;
185 else if (*args
== '>')
189 params
.users_min
= atoi(args
) + 1;
191 params
.users_min
= 0;
200 else if (parc
> 1 && IsChannelName(parv
[1]))
202 safelist_channel_named(source_p
, parv
[1]);
206 safelist_client_instantiate(source_p
, ¶ms
);
212 * safelist_sendq_exceeded()
214 * inputs - pointer to client that needs checking
215 * outputs - 1 if a client has exceeded the reserved
216 * sendq limit, 0 if not
217 * side effects - none
219 * When safelisting, we only use half of the SendQ at any
222 static int safelist_sendq_exceeded(struct Client
*client_p
)
224 if (rb_linebuf_len(&client_p
->localClient
->buf_sendq
) > (get_sendq(client_p
) / 2))
231 * safelist_client_instantiate()
233 * inputs - pointer to Client to be listed,
234 * struct ListClient to copy for params
236 * side effects - the safelist process begins for a
239 * Please do not ever call this on a non-local client.
240 * If you do, you will get SIGSEGV.
242 static void safelist_client_instantiate(struct Client
*client_p
, struct ListClient
*params
)
244 struct ListClient
*self
;
246 s_assert(MyClient(client_p
));
247 s_assert(params
!= NULL
);
249 self
= rb_malloc(sizeof(struct ListClient
));
251 self
->hash_indice
= 0;
252 self
->users_min
= params
->users_min
;
253 self
->users_max
= params
->users_max
;
255 client_p
->localClient
->safelist_data
= self
;
257 sendto_one(client_p
, form_str(RPL_LISTSTART
), me
.name
, client_p
->name
);
259 /* pop the client onto the queue for processing */
260 rb_dlinkAddAlloc(client_p
, &safelisting_clients
);
262 /* give the user some initial data to work with */
263 safelist_iterate_client(client_p
);
267 * safelist_client_release()
269 * inputs - pointer to Client being listed on
271 * side effects - the client is no longer being
274 * Please do not ever call this on a non-local client.
275 * If you do, you will get SIGSEGV.
277 static void safelist_client_release(struct Client
*client_p
)
279 s_assert(MyClient(client_p
));
281 rb_dlinkFindDestroy(client_p
, &safelisting_clients
);
283 rb_free(client_p
->localClient
->safelist_data
);
285 client_p
->localClient
->safelist_data
= NULL
;
287 sendto_one(client_p
, form_str(RPL_LISTEND
), me
.name
, client_p
->name
);
291 * safelist_channel_named()
293 * inputs - client pointer, channel name
295 * side effects - a named channel is listed
297 static void safelist_channel_named(struct Client
*source_p
, const char *name
)
299 struct Channel
*chptr
;
301 char *n
= LOCAL_COPY(name
);
303 sendto_one(source_p
, form_str(RPL_LISTSTART
), me
.name
, source_p
->name
);
305 if ((p
= strchr(n
, ',')))
310 sendto_one_numeric(source_p
, ERR_NOSUCHNICK
, form_str(ERR_NOSUCHNICK
), name
);
311 sendto_one(source_p
, form_str(RPL_LISTEND
), me
.name
, source_p
->name
);
315 chptr
= find_channel(n
);
319 sendto_one_numeric(source_p
, ERR_NOSUCHNICK
, form_str(ERR_NOSUCHNICK
), n
);
320 sendto_one(source_p
, form_str(RPL_LISTEND
), me
.name
, source_p
->name
);
324 if (!SecretChannel(chptr
) || IsMember(source_p
, chptr
))
325 sendto_one(source_p
, form_str(RPL_LIST
), me
.name
, source_p
->name
, chptr
->chname
,
326 rb_dlink_list_length(&chptr
->members
),
327 chptr
->topic
== NULL
? "" : chptr
->topic
);
329 sendto_one(source_p
, form_str(RPL_LISTEND
), me
.name
, source_p
->name
);
334 * safelist_one_channel()
336 * inputs - client pointer and channel pointer
338 * side effects - a channel is listed if it meets the
341 static void safelist_one_channel(struct Client
*source_p
, struct Channel
*chptr
)
343 struct ListClient
*safelist_data
= source_p
->localClient
->safelist_data
;
345 if (SecretChannel(chptr
) && !IsMember(source_p
, chptr
))
348 if ((unsigned int)chptr
->members
.length
< safelist_data
->users_min
349 || (unsigned int)chptr
->members
.length
> safelist_data
->users_max
)
352 sendto_one(source_p
, form_str(RPL_LIST
), me
.name
, source_p
->name
, chptr
->chname
,
353 chptr
->members
.length
, chptr
->topic
== NULL
? "" : chptr
->topic
);
357 * safelist_iterate_client()
359 * inputs - client pointer
361 * side effects - the client's sendq is filled up again
363 static void safelist_iterate_client(struct Client
*source_p
)
368 for (iter
= source_p
->localClient
->safelist_data
->hash_indice
; iter
< CH_MAX
; iter
++)
370 if (safelist_sendq_exceeded(source_p
->from
) == YES
)
372 source_p
->localClient
->safelist_data
->hash_indice
= iter
;
376 RB_DLINK_FOREACH(ptr
, channelTable
[iter
].head
)
377 safelist_one_channel(source_p
, (struct Channel
*) ptr
->data
);
380 safelist_client_release(source_p
);
383 static void safelist_iterate_clients(void *unused
)
385 rb_dlink_node
*n
, *n2
;
387 RB_DLINK_FOREACH_SAFE(n
, n2
, safelisting_clients
.head
)
388 safelist_iterate_client((struct Client
*)n
->data
);