]> jfr.im git - solanum.git/blobdiff - modules/m_list.c
extensions/umode_hide_idle_time: mask times for hidden sources (#373)
[solanum.git] / modules / m_list.c
index 9c16161b2dde9ec95e55ac98a9f4543c2be3d784..c50d508a3c5453486ee7691db822a63da5028aeb 100644 (file)
@@ -1,8 +1,8 @@
 /*
- * charybdis: An advanced ircd.
+ * Solanum: a slightly advanced ircd
  * m_list_safelist.c: Version of /list that uses the safelist code.
  *
- * Copyright (c) 2006 William Pitcock <nenolod@nenolod.net>
+ * Copyright (c) 2006 Ariadne Conill <ariadne@dereferenced.org>
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are
  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  * POSSIBILITY OF SUCH DAMAGE.
- *
- * $Id: m_list.c 3372 2007-04-03 10:18:07Z nenolod $
  */
 
 #include "stdinc.h"
-#include "tools.h"
 #include "channel.h"
 #include "client.h"
 #include "hash.h"
-#include "irc_string.h"
+#include "match.h"
 #include "ircd.h"
 #include "numeric.h"
 #include "s_conf.h"
+#include "s_newconf.h"
 #include "s_serv.h"
+#include "supported.h"
 #include "send.h"
 #include "msg.h"
 #include "parse.h"
 #include "modules.h"
-#include "event.h"
+#include "inline/stringops.h"
+#include "s_assert.h"
+#include "logger.h"
+#include "rb_radixtree.h"
+
+static const char list_desc[] = "Provides the LIST command to clients to view non-hidden channels";
 
 static rb_dlink_list safelisting_clients = { NULL, NULL, 0 };
 
+static struct ev_entry *iterate_clients_ev = NULL;
+
 static int _modinit(void);
 static void _moddeinit(void);
 
-static int m_list(struct Client *, struct Client *, int, const char **);
-static int mo_list(struct Client *, struct Client *, int, const char **);
+static void m_list(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
+static void mo_list(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
 
-static void safelist_check_cliexit(hook_data_client_exit * hdata);
+static void list_one_channel(struct Client *source_p, struct Channel *chptr, int visible);
+
+static void safelist_one_channel(struct Client *source_p, struct Channel *chptr, struct ListClient *params);
+static void safelist_check_cliexit(void *);
 static void safelist_client_instantiate(struct Client *, struct ListClient *);
 static void safelist_client_release(struct Client *);
-static void safelist_one_channel(struct Client *source_p, struct Channel *chptr);
 static void safelist_iterate_client(struct Client *source_p);
 static void safelist_iterate_clients(void *unused);
-static void safelist_channel_named(struct Client *source_p, const char *name);
+static void safelist_channel_named(struct Client *source_p, const char *name, int operspy);
 
 struct Message list_msgtab = {
-       "LIST", 0, 0, 0, MFLG_SLOW,
+       "LIST", 0, 0, 0, 0,
        {mg_unreg, {m_list, 0}, mg_ignore, mg_ignore, mg_ignore, {mo_list, 0}}
 };
 
 mapi_clist_av1 list_clist[] = { &list_msgtab, NULL };
 
 mapi_hfn_list_av1 list_hfnlist[] = {
-       {"client_exit", (hookfn) safelist_check_cliexit},
+       {"client_exit", safelist_check_cliexit},
        {NULL, NULL}
 };
 
-DECLARE_MODULE_AV1(list, _modinit, _moddeinit, list_clist, NULL, list_hfnlist, "$Revision: 3372 $");
+DECLARE_MODULE_AV2(list, _modinit, _moddeinit, list_clist, NULL, list_hfnlist, NULL, NULL, list_desc);
 
 static int _modinit(void)
 {
-       eventAdd("safelist_iterate_clients", safelist_iterate_clients, NULL, 3);
+       iterate_clients_ev = rb_event_add("safelist_iterate_clients", safelist_iterate_clients, NULL, 3);
+
+       /* ELIST=[tokens]:
+        *
+        * M = mask search
+        * N = !mask search
+        * U = user count search (< >)
+        * C = creation time search (C> C<)
+        * T = topic search (T> T<)
+        */
+       add_isupport("SAFELIST", isupport_string, "");
+       add_isupport("ELIST", isupport_string, "CMNTU");
 
        return 0;
 }
 
 static void _moddeinit(void)
 {
-       eventDelete(safelist_iterate_clients, NULL);
+       rb_event_delete(iterate_clients_ev);
+
+       delete_isupport("SAFELIST");
+       delete_isupport("ELIST");
 }
 
-static void safelist_check_cliexit(hook_data_client_exit * hdata)
+static void safelist_check_cliexit(void *data)
 {
+       hook_data_client_exit * hdata = data;
        /* Cancel the safelist request if we are disconnecting
         * from the server. That way it doesn't core. :P --nenolod
         */
@@ -103,95 +126,177 @@ static void safelist_check_cliexit(hook_data_client_exit * hdata)
 }
 
 /* m_list()
- *      parv[0] = sender prefix
  *      parv[1] = channel
  *
  * XXX - With SAFELIST, do we really need to continue pacing?
  *       In theory, the server cannot be lagged by this. --nenolod
  */
-static int m_list(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
+static void
+m_list(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
 {
        static time_t last_used = 0L;
 
        if (source_p->localClient->safelist_data != NULL)
        {
                sendto_one_notice(source_p, ":/LIST aborted");
-               sendto_one(source_p, form_str(RPL_LISTEND), me.name, source_p->name);
                safelist_client_release(source_p);
-               return 0;
+               return;
        }
 
        if (parc < 2 || !IsChannelName(parv[1]))
        {
                /* pace this due to the sheer traffic involved */
-               if (((last_used + ConfigFileEntry.pace_wait) > CurrentTime))
+               if (((last_used + ConfigFileEntry.pace_wait) > rb_current_time()))
                {
                        sendto_one(source_p, form_str(RPL_LOAD2HI), me.name, source_p->name, "LIST");
                        sendto_one(source_p, form_str(RPL_LISTEND), me.name, source_p->name);
-                       return 0;
+                       return;
                }
                else
-                       last_used = CurrentTime;
+                       last_used = rb_current_time();
        }
 
-       return mo_list(client_p, source_p, parc, parv);
+       mo_list(msgbuf_p, client_p, source_p, parc, parv);
 }
 
 /* mo_list()
- *      parv[0] = sender prefix
  *      parv[1] = channel
  */
-static int mo_list(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
+static void
+mo_list(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
 {
-       struct ListClient params;
-       char *p, *args;
+       struct ListClient *params;
+       char *p;
+       char *args = NULL;
        int i;
+       int operspy = 0;
 
        if (source_p->localClient->safelist_data != NULL)
        {
                sendto_one_notice(source_p, ":/LIST aborted");
-               sendto_one(source_p, form_str(RPL_LISTEND), me.name, source_p->name);
                safelist_client_release(source_p);
-               return 0;
+               return;
        }
 
-       /* XXX rather arbitrary -- jilles */
-       params.users_min = 3;
-       params.users_max = INT_MAX;
-
-       if (parc > 1 && parv[1] != NULL && !IsChannelName(parv[1]))
+       if (parc > 1)
        {
                args = LOCAL_COPY(parv[1]);
-               /* Make any specification cancel out defaults */
-               if (*args == '<')
-                       params.users_min = 0;
+       }
+
+       if (args && *args == '!' && IsOperSpy(source_p))
+       {
+               args++;
+               report_operspy(source_p, "LIST", args);
+               operspy = 1;
+       }
+
+       /* Single channel. */
+       if (args && IsChannelName(args) && !strpbrk(args, "*?, "))
+       {
+               safelist_channel_named(source_p, args, operspy);
+               return;
+       }
+
+       /* Multiple channels, possibly with parameters. */
+       params = rb_malloc(sizeof(struct ListClient));
+
+       params->users_min = ConfigChannel.displayed_usercount;
+       params->users_max = INT_MAX;
+       params->operspy = operspy;
+       params->created_min = params->topic_min =
+               params->created_max = params->topic_max = 0;
+       params->mask = NULL;
+       params->nomask = NULL;
+
+       if (args && !EmptyString(args))
+       {
+               /* Cancel out default minimum. */
+               params->users_min = 0;
 
-               for (i = 0; i < 2; i++)
+               for (i = 0; i < 7; i++)
                {
                        if ((p = strchr(args, ',')) != NULL)
                                *p++ = '\0';
 
                        if (*args == '<')
+                       {
+                               args++;
+                               if (!IsDigit(*args)) goto fail;
+
+                               params->users_max = atoi(args);
+                               if (params->users_max == 0)
+                                       params->users_max = INT_MAX;
+                               else
+                                       params->users_max--;
+                       }
+                       else if (*args == '>')
                        {
                                args++;
                                if (IsDigit(*args))
+                                       params->users_min = atoi(args) + 1;
+                               else if (args[0] == '-' && IsDigit(args[1]))
+                                       params->users_min = 0;
+                               else
+                                       goto fail;
+                       }
+                       else if (*args == 'C' || *args == 'c')
+                       {
+                               args++;
+                               if (*args == '>')
+                               {
+                                       /* Creation time earlier than last x minutes. */
+                                       args++;
+                                       if (!IsDigit(*args)) goto fail;
+                                       params->created_max = rb_current_time() - (60 * atoi(args));
+                               }
+                               else if (*args == '<')
                                {
-                                       params.users_max = atoi(args);
-                                       if (params.users_max == 0)
-                                               params.users_max = INT_MAX;
-                                       else
-                                               params.users_max--;
+                                       /* Creation time within last x minutes. */
+                                       args++;
+                                       if (!IsDigit(*args)) goto fail;
+                                       params->created_min = rb_current_time() - (60 * atoi(args));
                                }
                                else
-                                       params.users_max = INT_MAX;
+                               {
+                                       goto fail;
+                               }
                        }
-                       else if (*args == '>')
+                       else if (*args == 'T' || *args == 't')
                        {
                                args++;
-                               if (IsDigit(*args))
-                                       params.users_min = atoi(args) + 1;
+                               if (*args == '>')
+                               {
+                                       /* Topic change time earlier than last x minutes. */
+                                       args++;
+                                       if (!IsDigit(*args)) goto fail;
+                                       params->topic_max = rb_current_time() - (60 * atoi(args));
+                               }
+                               else if (*args == '<')
+                               {
+                                       /* Topic change time within last x minutes. */
+                                       args++;
+                                       if (!IsDigit(*args)) goto fail;
+                                       params->topic_min = rb_current_time() - (60 * atoi(args));
+                               }
                                else
-                                       params.users_min = 0;
+                               {
+                                       goto fail;
+                               }
+                       }
+                       else if (*args == '!')
+                       {
+                               args++;
+                               rb_free(params->nomask);
+                               params->nomask = rb_strdup(args);
+                       }
+                       else if (*args == '?' || *args == '*' || IsChanPrefix(*args))
+                       {
+                               rb_free(params->mask);
+                               params->mask = rb_strdup(args);
+                       }
+                       else
+                       {
+                               goto fail;
                        }
 
                        if (EmptyString(p))
@@ -200,41 +305,61 @@ static int mo_list(struct Client *client_p, struct Client *source_p, int parc, c
                                args = p;
                }
        }
-       else if (parc > 1 && IsChannelName(parv[1]))
-       {
-               safelist_channel_named(source_p, parv[1]);
-               return 0;
-       }
 
-       safelist_client_instantiate(source_p, &params);
+       safelist_client_instantiate(source_p, params);
+       return;
 
-       return 0;
+fail:
+       rb_free(params);
+       sendto_one(source_p, form_str(RPL_LISTSTART), me.name, source_p->name);
+       sendto_one_notice(source_p, ":Invalid parameters for /LIST");
+       sendto_one(source_p, form_str(RPL_LISTEND), me.name, source_p->name);
+}
+
+/*
+ * list_one_channel()
+ *
+ * inputs       - client pointer, channel pointer, whether normally visible
+ * outputs      - none
+ * side effects - a channel is listed
+ */
+static void list_one_channel(struct Client *source_p, struct Channel *chptr,
+               int visible)
+{
+       char topic[TOPICLEN + 1];
+
+       if (chptr->topic != NULL)
+               rb_strlcpy(topic, chptr->topic, sizeof topic);
+       else
+               topic[0] = '\0';
+       strip_colour(topic);
+       sendto_one(source_p, form_str(RPL_LIST), me.name, source_p->name,
+                  visible ? "" : "!",
+                  chptr->chname, rb_dlink_list_length(&chptr->members),
+                  topic);
 }
 
 /*
  * safelist_sendq_exceeded()
  *
  * inputs       - pointer to client that needs checking
- * outputs      - 1 if a client has exceeded the reserved
- *                sendq limit, 0 if not
+ * outputs      - true if a client has exceeded the reserved
+ *                sendq limit, false if not
  * side effects - none
  *
  * When safelisting, we only use half of the SendQ at any
  * given time.
  */
-static int safelist_sendq_exceeded(struct Client *client_p)
+static bool safelist_sendq_exceeded(struct Client *client_p)
 {
-       if (linebuf_len(&client_p->localClient->buf_sendq) > (get_sendq(client_p) / 2))
-               return YES;
-       else
-               return NO;
+       return rb_linebuf_len(&client_p->localClient->buf_sendq) > (get_sendq(client_p) / 2);
 }
 
 /*
  * safelist_client_instantiate()
  *
  * inputs       - pointer to Client to be listed,
- *                struct ListClient to copy for params
+ *                pointer to ListClient for params
  * outputs      - none
  * side effects - the safelist process begins for a
  *                client.
@@ -244,25 +369,25 @@ static int safelist_sendq_exceeded(struct Client *client_p)
  */
 static void safelist_client_instantiate(struct Client *client_p, struct ListClient *params)
 {
-       struct ListClient *self;
+       struct Channel *chptr;
 
        s_assert(MyClient(client_p));
        s_assert(params != NULL);
 
-       self = MyMalloc(sizeof(struct ListClient));
-
-       self->hash_indice = 0;
-       self->users_min = params->users_min;
-       self->users_max = params->users_max;
-
-       client_p->localClient->safelist_data = self;
+       client_p->localClient->safelist_data = params;
 
        sendto_one(client_p, form_str(RPL_LISTSTART), me.name, client_p->name);
 
        /* pop the client onto the queue for processing */
-       dlinkAddAlloc(client_p, &safelisting_clients);
+       rb_dlinkAddAlloc(client_p, &safelisting_clients);
 
        /* give the user some initial data to work with */
+       if (params->mask && (chptr = find_channel(params->mask)))
+       {
+               bool visible = !SecretChannel(chptr) || IsMember(client_p, chptr);
+               if (visible || params->operspy)
+                       list_one_channel(client_p, chptr, visible);
+       }
        safelist_iterate_client(client_p);
 }
 
@@ -273,17 +398,20 @@ static void safelist_client_instantiate(struct Client *client_p, struct ListClie
  * outputs      - none
  * side effects - the client is no longer being
  *                listed
- *
- * Please do not ever call this on a non-local client.
- * If you do, you will get SIGSEGV.
  */
 static void safelist_client_release(struct Client *client_p)
 {
+       if(!MyClient(client_p))
+               return;
+
        s_assert(MyClient(client_p));
 
-       dlinkFindDestroy(client_p, &safelisting_clients);
+       rb_dlinkFindDestroy(client_p, &safelisting_clients);
 
-       MyFree(client_p->localClient->safelist_data);
+       rb_free(client_p->localClient->safelist_data->chname);
+       rb_free(client_p->localClient->safelist_data->mask);
+       rb_free(client_p->localClient->safelist_data->nomask);
+       rb_free(client_p->localClient->safelist_data);
 
        client_p->localClient->safelist_data = NULL;
 
@@ -293,41 +421,40 @@ static void safelist_client_release(struct Client *client_p)
 /*
  * safelist_channel_named()
  *
- * inputs       - client pointer, channel name
+ * inputs       - client pointer, channel name, operspy
  * outputs      - none
  * side effects - a named channel is listed
  */
-static void safelist_channel_named(struct Client *source_p, const char *name)
+static void safelist_channel_named(struct Client *source_p, const char *name, int operspy)
 {
        struct Channel *chptr;
        char *p;
-       char *n = LOCAL_COPY(name);
+       int visible;
 
        sendto_one(source_p, form_str(RPL_LISTSTART), me.name, source_p->name);
 
-       if ((p = strchr(n, ',')))
+       if ((p = strchr(name, ',')))
                *p = '\0';
 
-       if (*n == '\0')
+       if (*name == '\0')
        {
                sendto_one_numeric(source_p, ERR_NOSUCHNICK, form_str(ERR_NOSUCHNICK), name);
                sendto_one(source_p, form_str(RPL_LISTEND), me.name, source_p->name);
                return;
        }
 
-       chptr = find_channel(n);
+       chptr = find_channel(name);
 
        if (chptr == NULL)
        {
-               sendto_one_numeric(source_p, ERR_NOSUCHNICK, form_str(ERR_NOSUCHNICK), n);
+               sendto_one_numeric(source_p, ERR_NOSUCHNICK, form_str(ERR_NOSUCHNICK), name);
                sendto_one(source_p, form_str(RPL_LISTEND), me.name, source_p->name);
                return;
        }
 
-       if (ShowChannel(source_p, chptr))
-               sendto_one(source_p, form_str(RPL_LIST), me.name, source_p->name, chptr->chname,
-                          rb_dlink_list_length(&chptr->members),
-                          chptr->topic == NULL ? "" : chptr->topic);
+       visible = !SecretChannel(chptr) || IsMember(source_p, chptr);
+       if (visible || operspy)
+               list_one_channel(source_p, chptr, visible);
 
        sendto_one(source_p, form_str(RPL_LISTEND), me.name, source_p->name);
        return;
@@ -341,19 +468,39 @@ static void safelist_channel_named(struct Client *source_p, const char *name)
  * side effects - a channel is listed if it meets the
  *                requirements
  */
-static void safelist_one_channel(struct Client *source_p, struct Channel *chptr)
+static void safelist_one_channel(struct Client *source_p, struct Channel *chptr, struct ListClient *params)
 {
-       struct ListClient *safelist_data = source_p->localClient->safelist_data;
+       int visible;
+
+       visible = !SecretChannel(chptr) || IsMember(source_p, chptr);
+       if (!visible && !params->operspy)
+               return;
 
-       if (SecretChannel(chptr) && !IsMember(source_p, chptr))
+       if ((unsigned int)chptr->members.length < params->users_min
+           || (unsigned int)chptr->members.length > params->users_max)
                return;
 
-       if ((unsigned int)chptr->members.length < safelist_data->users_min
-           || (unsigned int)chptr->members.length > safelist_data->users_max)
+       if (params->topic_min && chptr->topic_time < params->topic_min)
                return;
 
-       sendto_one(source_p, form_str(RPL_LIST), me.name, source_p->name, chptr->chname,
-                  chptr->members.length, chptr->topic == NULL ? "" : chptr->topic);
+       /* If a topic TS is provided, don't show channels without a topic set. */
+       if (params->topic_max && (chptr->topic_time > params->topic_max
+               || chptr->topic_time == 0))
+               return;
+
+       if (params->created_min && chptr->channelts < params->created_min)
+               return;
+
+       if (params->created_max && chptr->channelts > params->created_max)
+               return;
+
+       if (params->mask && (!irccmp(params->mask, chptr->chname) || !match(params->mask, chptr->chname)))
+               return;
+
+       if (params->nomask && match(params->nomask, chptr->chname))
+               return;
+
+       list_one_channel(source_p, chptr, visible);
 }
 
 /*
@@ -365,19 +512,20 @@ static void safelist_one_channel(struct Client *source_p, struct Channel *chptr)
  */
 static void safelist_iterate_client(struct Client *source_p)
 {
-       rb_dlink_node *ptr;
-       int iter;
+       struct Channel *chptr;
+       rb_radixtree_iteration_state iter;
 
-       for (iter = source_p->localClient->safelist_data->hash_indice; iter < CH_MAX; iter++)
+       RB_RADIXTREE_FOREACH_FROM(chptr, &iter, channel_tree, source_p->localClient->safelist_data->chname)
        {
-               if (safelist_sendq_exceeded(source_p->from) == YES)
+               if (safelist_sendq_exceeded(source_p->from))
                {
-                       source_p->localClient->safelist_data->hash_indice = iter;
+                       rb_free(source_p->localClient->safelist_data->chname);
+                       source_p->localClient->safelist_data->chname = rb_strdup(chptr->chname);
+
                        return;
                }
 
-               RB_DLINK_FOREACH(ptr, channelTable[iter].head) 
-                       safelist_one_channel(source_p, (struct Channel *) ptr->data);
+               safelist_one_channel(source_p, chptr, source_p->localClient->safelist_data);
        }
 
        safelist_client_release(source_p);
@@ -387,6 +535,6 @@ static void safelist_iterate_clients(void *unused)
 {
        rb_dlink_node *n, *n2;
 
-       RB_DLINK_FOREACH_SAFE(n, n2, safelisting_clients.head) 
+       RB_DLINK_FOREACH_SAFE(n, n2, safelisting_clients.head)
                safelist_iterate_client((struct Client *)n->data);
 }