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.
33 * $Id: m_list.c 3372 2007-04-03 10:18:07Z nenolod $
44 #include "s_newconf.h"
46 #include "supported.h"
51 #include "inline/stringops.h"
55 static int _modinit(void);
56 static void _moddeinit(void);
58 static int m_list(struct Client
*, struct Client
*, int, const char **);
59 static int mo_list(struct Client
*, struct Client
*, int, const char **);
63 unsigned int users_min
, users_max
;
64 time_t created_min
, created_max
, topic_min
, topic_max
;
68 static void list_one_channel(struct Client
*source_p
, struct Channel
*chptr
, int visible
);
70 static void safelist_one_channel(struct Client
*source_p
, struct Channel
*chptr
, struct ListOptions
*params
);
71 static void safelist_channel_named(struct Client
*source_p
, const char *name
, int operspy
);
73 struct Message list_msgtab
= {
74 "LIST", 0, 0, 0, MFLG_SLOW
,
75 {mg_unreg
, {m_list
, 0}, mg_ignore
, mg_ignore
, mg_ignore
, {mo_list
, 0}}
78 mapi_clist_av1 list_clist
[] = { &list_msgtab
, NULL
};
80 DECLARE_MODULE_AV1(list
, _modinit
, _moddeinit
, list_clist
, NULL
, NULL
, "$Revision: 3372 $");
82 static int _modinit(void)
88 * U = user count search (< >)
89 * C = creation time search (C> C<)
90 * T = topic search (T> T<)
92 add_isupport("SAFELIST", isupport_string
, "");
93 add_isupport("ELIST", isupport_string
, "CTU");
98 static void _moddeinit(void)
100 delete_isupport("SAFELIST");
101 delete_isupport("ELIST");
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 (parc
< 2 || !IsChannelName(parv
[1]))
116 /* pace this due to the sheer traffic involved */
117 if (((last_used
+ ConfigFileEntry
.pace_wait
) > rb_current_time()))
119 sendto_one(source_p
, form_str(RPL_LOAD2HI
), me
.name
, source_p
->name
, "LIST");
120 sendto_one(source_p
, form_str(RPL_LISTEND
), me
.name
, source_p
->name
);
124 last_used
= rb_current_time();
127 return mo_list(client_p
, source_p
, parc
, parv
);
133 static int mo_list(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
135 struct ListOptions
*params
;
140 int sendq_limit
= get_sendq_hard(client_p
);
148 args
= LOCAL_COPY(parv
[1]);
151 if (args
&& *args
== '!' && IsOperSpy(source_p
))
154 report_operspy(source_p
, "LIST", args
);
158 /* Single channel. */
159 if (args
&& IsChannelName(args
))
161 safelist_channel_named(source_p
, args
, operspy
);
165 /* Multiple channels, possibly with parameters. */
166 params
= rb_malloc(sizeof(struct ListOptions
));
168 params
->users_min
= ConfigChannel
.displayed_usercount
;
169 params
->users_max
= INT_MAX
;
170 params
->operspy
= operspy
;
171 params
->created_min
= params
->topic_min
=
172 params
->created_max
= params
->topic_max
= 0;
174 if (args
&& !EmptyString(args
))
176 /* Cancel out default minimum. */
177 params
->users_min
= 0;
179 for (i
= 0; i
< 7; i
++)
181 if ((p
= strchr(args
, ',')) != NULL
)
189 params
->users_max
= atoi(args
);
190 if (params
->users_max
== 0)
191 params
->users_max
= INT_MAX
;
196 else if (*args
== '>')
200 params
->users_min
= atoi(args
) + 1;
202 params
->users_min
= 0;
204 else if (*args
== 'C' || *args
== 'c')
209 /* Creation time earlier than last x minutes. */
213 params
->created_max
= rb_current_time() - (60 * atoi(args
));
216 else if (*args
== '<')
218 /* Creation time within last x minutes. */
222 params
->created_min
= rb_current_time() - (60 * atoi(args
));
226 else if (*args
== 'T' || *args
== 't')
231 /* Topic change time earlier than last x minutes. */
235 params
->topic_max
= rb_current_time() - (60 * atoi(args
));
238 else if (*args
== '<')
240 /* Topic change time within last x minutes. */
244 params
->topic_min
= rb_current_time() - (60 * atoi(args
));
256 sendto_one(client_p
, form_str(RPL_LISTSTART
), me
.name
, client_p
->name
);
258 RB_DLINK_FOREACH(ptr
, global_channel_list
.head
)
260 safelist_one_channel(client_p
, ptr
->data
, params
);
262 if (rb_linebuf_len(&client_p
->localClient
->buf_sendq
) > sendq_limit
)
264 sendto_one(source_p
, form_str(ERR_TOOMANYMATCHES
), me
.name
, source_p
->name
, "LIST");
269 sendto_one(source_p
, form_str(RPL_LISTEND
), me
.name
, source_p
->name
);
277 * inputs - client pointer, channel pointer, whether normally visible
279 * side effects - a channel is listed
281 static void list_one_channel(struct Client
*source_p
, struct Channel
*chptr
,
284 char topic
[TOPICLEN
+ 1];
286 if (chptr
->topic
!= NULL
)
287 rb_strlcpy(topic
, chptr
->topic
, sizeof topic
);
291 sendto_one(source_p
, form_str(RPL_LIST
), me
.name
, source_p
->name
,
293 chptr
->chname
, rb_dlink_list_length(&chptr
->members
),
298 * safelist_sendq_exceeded()
300 * inputs - pointer to client that needs checking
301 * outputs - 1 if a client has exceeded the reserved
302 * sendq limit, 0 if not
303 * side effects - none
305 * When safelisting, we only use half of the SendQ at any
308 static int safelist_sendq_exceeded(struct Client
*client_p
)
310 if (rb_linebuf_len(&client_p
->localClient
->buf_sendq
) > (get_sendq(client_p
) / 2))
317 * safelist_channel_named()
319 * inputs - client pointer, channel name, operspy
321 * side effects - a named channel is listed
323 static void safelist_channel_named(struct Client
*source_p
, const char *name
, int operspy
)
325 struct Channel
*chptr
;
329 sendto_one(source_p
, form_str(RPL_LISTSTART
), me
.name
, source_p
->name
);
331 if ((p
= strchr(name
, ',')))
336 sendto_one_numeric(source_p
, ERR_NOSUCHNICK
, form_str(ERR_NOSUCHNICK
), name
);
337 sendto_one(source_p
, form_str(RPL_LISTEND
), me
.name
, source_p
->name
);
341 chptr
= find_channel(name
);
345 sendto_one_numeric(source_p
, ERR_NOSUCHNICK
, form_str(ERR_NOSUCHNICK
), name
);
346 sendto_one(source_p
, form_str(RPL_LISTEND
), me
.name
, source_p
->name
);
350 visible
= !SecretChannel(chptr
) || IsMember(source_p
, chptr
);
351 if (visible
|| operspy
)
352 list_one_channel(source_p
, chptr
, visible
);
354 sendto_one(source_p
, form_str(RPL_LISTEND
), me
.name
, source_p
->name
);
359 * safelist_one_channel()
361 * inputs - client pointer and channel pointer
363 * side effects - a channel is listed if it meets the
366 static void safelist_one_channel(struct Client
*source_p
, struct Channel
*chptr
, struct ListOptions
*params
)
370 visible
= !SecretChannel(chptr
) || IsMember(source_p
, chptr
);
371 if (!visible
&& !params
->operspy
)
374 if ((unsigned int)chptr
->members
.length
< params
->users_min
375 || (unsigned int)chptr
->members
.length
> params
->users_max
)
378 if (params
->topic_min
&& chptr
->topic_time
< params
->topic_min
)
381 /* If a topic TS is provided, don't show channels without a topic set. */
382 if (params
->topic_max
&& (chptr
->topic_time
> params
->topic_max
383 || chptr
->topic_time
== 0))
386 if (params
->created_min
&& chptr
->channelts
< params
->created_min
)
389 if (params
->created_max
&& chptr
->channelts
> params
->created_max
)
392 list_one_channel(source_p
, chptr
, visible
);