2 * Solanum: a slightly advanced ircd
3 * m_list_safelist.c: Version of /list that uses the safelist code.
5 * Copyright (c) 2006 Ariadne Conill <ariadne@dereferenced.org>
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.
42 #include "s_newconf.h"
44 #include "supported.h"
49 #include "inline/stringops.h"
52 #include "rb_radixtree.h"
54 static const char list_desc
[] = "Provides the LIST command to clients to view non-hidden channels";
56 static rb_dlink_list safelisting_clients
= { NULL
, NULL
, 0 };
58 static struct ev_entry
*iterate_clients_ev
= NULL
;
60 static int _modinit(void);
61 static void _moddeinit(void);
63 static void m_list(struct MsgBuf
*, struct Client
*, struct Client
*, int, const char **);
64 static void mo_list(struct MsgBuf
*, struct Client
*, struct Client
*, int, const char **);
66 static void list_one_channel(struct Client
*source_p
, struct Channel
*chptr
, int visible
);
68 static void safelist_one_channel(struct Client
*source_p
, struct Channel
*chptr
, struct ListClient
*params
);
69 static void safelist_check_cliexit(void *);
70 static void safelist_client_instantiate(struct Client
*, struct ListClient
*);
71 static void safelist_client_release(struct Client
*);
72 static void safelist_iterate_client(struct Client
*source_p
);
73 static void safelist_iterate_clients(void *unused
);
74 static void safelist_channel_named(struct Client
*source_p
, const char *name
, int operspy
);
76 struct Message list_msgtab
= {
78 {mg_unreg
, {m_list
, 0}, mg_ignore
, mg_ignore
, mg_ignore
, {mo_list
, 0}}
81 mapi_clist_av1 list_clist
[] = { &list_msgtab
, NULL
};
83 mapi_hfn_list_av1 list_hfnlist
[] = {
84 {"client_exit", safelist_check_cliexit
},
88 DECLARE_MODULE_AV2(list
, _modinit
, _moddeinit
, list_clist
, NULL
, list_hfnlist
, NULL
, NULL
, list_desc
);
90 static int _modinit(void)
92 iterate_clients_ev
= rb_event_add("safelist_iterate_clients", safelist_iterate_clients
, NULL
, 3);
98 * U = user count search (< >)
99 * C = creation time search (C> C<)
100 * T = topic search (T> T<)
102 add_isupport("SAFELIST", isupport_string
, "");
103 add_isupport("ELIST", isupport_string
, "CMNTU");
108 static void _moddeinit(void)
110 rb_event_delete(iterate_clients_ev
);
112 delete_isupport("SAFELIST");
113 delete_isupport("ELIST");
116 static void safelist_check_cliexit(void *data
)
118 hook_data_client_exit
* hdata
= data
;
119 /* Cancel the safelist request if we are disconnecting
120 * from the server. That way it doesn't core. :P --nenolod
122 if (MyClient(hdata
->target
) && hdata
->target
->localClient
->safelist_data
!= NULL
)
124 safelist_client_release(hdata
->target
);
131 * XXX - With SAFELIST, do we really need to continue pacing?
132 * In theory, the server cannot be lagged by this. --nenolod
135 m_list(struct MsgBuf
*msgbuf_p
, struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
137 static time_t last_used
= 0L;
139 if (source_p
->localClient
->safelist_data
!= NULL
)
141 sendto_one_notice(source_p
, ":/LIST aborted");
142 safelist_client_release(source_p
);
146 if (parc
< 2 || !IsChannelName(parv
[1]))
148 /* pace this due to the sheer traffic involved */
149 if (((last_used
+ ConfigFileEntry
.pace_wait
) > rb_current_time()))
151 sendto_one(source_p
, form_str(RPL_LOAD2HI
), me
.name
, source_p
->name
, "LIST");
152 sendto_one(source_p
, form_str(RPL_LISTEND
), me
.name
, source_p
->name
);
156 last_used
= rb_current_time();
159 mo_list(msgbuf_p
, client_p
, source_p
, parc
, parv
);
166 mo_list(struct MsgBuf
*msgbuf_p
, struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
168 struct ListClient
*params
;
174 if (source_p
->localClient
->safelist_data
!= NULL
)
176 sendto_one_notice(source_p
, ":/LIST aborted");
177 safelist_client_release(source_p
);
183 args
= LOCAL_COPY(parv
[1]);
186 if (args
&& *args
== '!' && IsOperSpy(source_p
))
189 report_operspy(source_p
, "LIST", args
);
193 /* Single channel. */
194 if (args
&& IsChannelName(args
) && !strpbrk(args
, "*?, "))
196 safelist_channel_named(source_p
, args
, operspy
);
200 /* Multiple channels, possibly with parameters. */
201 params
= rb_malloc(sizeof(struct ListClient
));
203 params
->users_min
= ConfigChannel
.displayed_usercount
;
204 params
->users_max
= INT_MAX
;
205 params
->operspy
= operspy
;
206 params
->created_min
= params
->topic_min
=
207 params
->created_max
= params
->topic_max
= 0;
209 params
->nomask
= NULL
;
211 if (args
&& !EmptyString(args
))
213 /* Cancel out default minimum. */
214 params
->users_min
= 0;
216 for (i
= 0; i
< 7; i
++)
218 if ((p
= strchr(args
, ',')) != NULL
)
224 if (!IsDigit(*args
)) goto fail
;
226 params
->users_max
= atoi(args
);
227 if (params
->users_max
== 0)
228 params
->users_max
= INT_MAX
;
232 else if (*args
== '>')
236 params
->users_min
= atoi(args
) + 1;
237 else if (args
[0] == '-' && IsDigit(args
[1]))
238 params
->users_min
= 0;
242 else if (*args
== 'C' || *args
== 'c')
247 /* Creation time earlier than last x minutes. */
249 if (!IsDigit(*args
)) goto fail
;
250 params
->created_max
= rb_current_time() - (60 * atoi(args
));
252 else if (*args
== '<')
254 /* Creation time within last x minutes. */
256 if (!IsDigit(*args
)) goto fail
;
257 params
->created_min
= rb_current_time() - (60 * atoi(args
));
264 else if (*args
== 'T' || *args
== 't')
269 /* Topic change time earlier than last x minutes. */
271 if (!IsDigit(*args
)) goto fail
;
272 params
->topic_max
= rb_current_time() - (60 * atoi(args
));
274 else if (*args
== '<')
276 /* Topic change time within last x minutes. */
278 if (!IsDigit(*args
)) goto fail
;
279 params
->topic_min
= rb_current_time() - (60 * atoi(args
));
286 else if (*args
== '!')
289 rb_free(params
->nomask
);
290 params
->nomask
= rb_strdup(args
);
292 else if (*args
== '?' || *args
== '*' || IsChanPrefix(*args
))
294 rb_free(params
->mask
);
295 params
->mask
= rb_strdup(args
);
309 safelist_client_instantiate(source_p
, params
);
314 sendto_one(source_p
, form_str(RPL_LISTSTART
), me
.name
, source_p
->name
);
315 sendto_one_notice(source_p
, ":Invalid parameters for /LIST");
316 sendto_one(source_p
, form_str(RPL_LISTEND
), me
.name
, source_p
->name
);
322 * inputs - client pointer, channel pointer, whether normally visible
324 * side effects - a channel is listed
326 static void list_one_channel(struct Client
*source_p
, struct Channel
*chptr
,
329 char topic
[TOPICLEN
+ 1];
331 if (chptr
->topic
!= NULL
)
332 rb_strlcpy(topic
, chptr
->topic
, sizeof topic
);
336 sendto_one(source_p
, form_str(RPL_LIST
), me
.name
, source_p
->name
,
338 chptr
->chname
, rb_dlink_list_length(&chptr
->members
),
343 * safelist_sendq_exceeded()
345 * inputs - pointer to client that needs checking
346 * outputs - true if a client has exceeded the reserved
347 * sendq limit, false if not
348 * side effects - none
350 * When safelisting, we only use half of the SendQ at any
353 static bool safelist_sendq_exceeded(struct Client
*client_p
)
355 return rb_linebuf_len(&client_p
->localClient
->buf_sendq
) > (get_sendq(client_p
) / 2);
359 * safelist_client_instantiate()
361 * inputs - pointer to Client to be listed,
362 * pointer to ListClient for params
364 * side effects - the safelist process begins for a
367 * Please do not ever call this on a non-local client.
368 * If you do, you will get SIGSEGV.
370 static void safelist_client_instantiate(struct Client
*client_p
, struct ListClient
*params
)
372 struct Channel
*chptr
;
374 s_assert(MyClient(client_p
));
375 s_assert(params
!= NULL
);
377 client_p
->localClient
->safelist_data
= params
;
379 sendto_one(client_p
, form_str(RPL_LISTSTART
), me
.name
, client_p
->name
);
381 /* pop the client onto the queue for processing */
382 rb_dlinkAddAlloc(client_p
, &safelisting_clients
);
384 /* give the user some initial data to work with */
385 if (params
->mask
&& (chptr
= find_channel(params
->mask
)))
387 bool visible
= !SecretChannel(chptr
) || IsMember(client_p
, chptr
);
388 if (visible
|| params
->operspy
)
389 list_one_channel(client_p
, chptr
, visible
);
391 safelist_iterate_client(client_p
);
395 * safelist_client_release()
397 * inputs - pointer to Client being listed on
399 * side effects - the client is no longer being
402 static void safelist_client_release(struct Client
*client_p
)
404 if(!MyClient(client_p
))
407 s_assert(MyClient(client_p
));
409 rb_dlinkFindDestroy(client_p
, &safelisting_clients
);
411 rb_free(client_p
->localClient
->safelist_data
->chname
);
412 rb_free(client_p
->localClient
->safelist_data
->mask
);
413 rb_free(client_p
->localClient
->safelist_data
->nomask
);
414 rb_free(client_p
->localClient
->safelist_data
);
416 client_p
->localClient
->safelist_data
= NULL
;
418 sendto_one(client_p
, form_str(RPL_LISTEND
), me
.name
, client_p
->name
);
422 * safelist_channel_named()
424 * inputs - client pointer, channel name, operspy
426 * side effects - a named channel is listed
428 static void safelist_channel_named(struct Client
*source_p
, const char *name
, int operspy
)
430 struct Channel
*chptr
;
434 sendto_one(source_p
, form_str(RPL_LISTSTART
), me
.name
, source_p
->name
);
436 if ((p
= strchr(name
, ',')))
441 sendto_one_numeric(source_p
, ERR_NOSUCHNICK
, form_str(ERR_NOSUCHNICK
), name
);
442 sendto_one(source_p
, form_str(RPL_LISTEND
), me
.name
, source_p
->name
);
446 chptr
= find_channel(name
);
450 sendto_one_numeric(source_p
, ERR_NOSUCHNICK
, form_str(ERR_NOSUCHNICK
), name
);
451 sendto_one(source_p
, form_str(RPL_LISTEND
), me
.name
, source_p
->name
);
455 visible
= !SecretChannel(chptr
) || IsMember(source_p
, chptr
);
456 if (visible
|| operspy
)
457 list_one_channel(source_p
, chptr
, visible
);
459 sendto_one(source_p
, form_str(RPL_LISTEND
), me
.name
, source_p
->name
);
464 * safelist_one_channel()
466 * inputs - client pointer and channel pointer
468 * side effects - a channel is listed if it meets the
471 static void safelist_one_channel(struct Client
*source_p
, struct Channel
*chptr
, struct ListClient
*params
)
475 visible
= !SecretChannel(chptr
) || IsMember(source_p
, chptr
);
476 if (!visible
&& !params
->operspy
)
479 if ((unsigned int)chptr
->members
.length
< params
->users_min
480 || (unsigned int)chptr
->members
.length
> params
->users_max
)
483 if (params
->topic_min
&& chptr
->topic_time
< params
->topic_min
)
486 /* If a topic TS is provided, don't show channels without a topic set. */
487 if (params
->topic_max
&& (chptr
->topic_time
> params
->topic_max
488 || chptr
->topic_time
== 0))
491 if (params
->created_min
&& chptr
->channelts
< params
->created_min
)
494 if (params
->created_max
&& chptr
->channelts
> params
->created_max
)
497 if (params
->mask
&& (!irccmp(params
->mask
, chptr
->chname
) || !match(params
->mask
, chptr
->chname
)))
500 if (params
->nomask
&& match(params
->nomask
, chptr
->chname
))
503 list_one_channel(source_p
, chptr
, visible
);
507 * safelist_iterate_client()
509 * inputs - client pointer
511 * side effects - the client's sendq is filled up again
513 static void safelist_iterate_client(struct Client
*source_p
)
515 struct Channel
*chptr
;
516 rb_radixtree_iteration_state iter
;
518 RB_RADIXTREE_FOREACH_FROM(chptr
, &iter
, channel_tree
, source_p
->localClient
->safelist_data
->chname
)
520 if (safelist_sendq_exceeded(source_p
->from
))
522 rb_free(source_p
->localClient
->safelist_data
->chname
);
523 source_p
->localClient
->safelist_data
->chname
= rb_strdup(chptr
->chname
);
528 safelist_one_channel(source_p
, chptr
, source_p
->localClient
->safelist_data
);
531 safelist_client_release(source_p
);
534 static void safelist_iterate_clients(void *unused
)
536 rb_dlink_node
*n
, *n2
;
538 RB_DLINK_FOREACH_SAFE(n
, n2
, safelisting_clients
.head
)
539 safelist_iterate_client((struct Client
*)n
->data
);