]> jfr.im git - solanum.git/blobdiff - modules/m_list.c
ircd/packet.c: make function definition consistent with declaration (#301)
[solanum.git] / modules / m_list.c
index 2c197552fc61910b4ab374b6268bb615c10dbcc9..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
@@ -55,16 +55,18 @@ static const char list_desc[] = "Provides the LIST command to clients to view no
 
 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 MsgBuf *, struct Client *, struct Client *, int, const char **);
-static int mo_list(struct MsgBuf *, 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 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(hook_data_client_exit * hdata);
+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_iterate_client(struct Client *source_p);
@@ -79,14 +81,12 @@ struct Message list_msgtab = {
 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_AV2(list, _modinit, _moddeinit, list_clist, NULL, list_hfnlist, NULL, NULL, list_desc);
 
-static struct ev_entry *iterate_clients_ev = NULL;
-
 static int _modinit(void)
 {
        iterate_clients_ev = rb_event_add("safelist_iterate_clients", safelist_iterate_clients, NULL, 3);
@@ -100,7 +100,7 @@ static int _modinit(void)
         * T = topic search (T> T<)
         */
        add_isupport("SAFELIST", isupport_string, "");
-       add_isupport("ELIST", isupport_string, "CTU");
+       add_isupport("ELIST", isupport_string, "CMNTU");
 
        return 0;
 }
@@ -113,8 +113,9 @@ static void _moddeinit(void)
        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
         */
@@ -130,7 +131,8 @@ static void safelist_check_cliexit(hook_data_client_exit * hdata)
  * 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 MsgBuf *msgbuf_p, 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;
 
@@ -138,7 +140,7 @@ static int m_list(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Clien
        {
                sendto_one_notice(source_p, ":/LIST aborted");
                safelist_client_release(source_p);
-               return 0;
+               return;
        }
 
        if (parc < 2 || !IsChannelName(parv[1]))
@@ -148,19 +150,20 @@ static int m_list(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Clien
                {
                        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 = rb_current_time();
        }
 
-       return mo_list(msgbuf_p, client_p, source_p, parc, parv);
+       mo_list(msgbuf_p, client_p, source_p, parc, parv);
 }
 
 /* mo_list()
  *      parv[1] = channel
  */
-static int mo_list(struct MsgBuf *msgbuf_p, 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;
@@ -172,7 +175,7 @@ static int mo_list(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Clie
        {
                sendto_one_notice(source_p, ":/LIST aborted");
                safelist_client_release(source_p);
-               return 0;
+               return;
        }
 
        if (parc > 1)
@@ -188,10 +191,10 @@ static int mo_list(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Clie
        }
 
        /* Single channel. */
-       if (args && IsChannelName(args))
+       if (args && IsChannelName(args) && !strpbrk(args, "*?, "))
        {
                safelist_channel_named(source_p, args, operspy);
-               return 0;
+               return;
        }
 
        /* Multiple channels, possibly with parameters. */
@@ -202,6 +205,8 @@ static int mo_list(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Clie
        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))
        {
@@ -216,22 +221,23 @@ static int mo_list(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Clie
                        if (*args == '<')
                        {
                                args++;
-                               if (IsDigit(*args))
-                               {
-                                       params->users_max = atoi(args);
-                                       if (params->users_max == 0)
-                                               params->users_max = INT_MAX;
-                                       else
-                                               params->users_max--;
-                               }
+                               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
+                               else if (args[0] == '-' && IsDigit(args[1]))
                                        params->users_min = 0;
+                               else
+                                       goto fail;
                        }
                        else if (*args == 'C' || *args == 'c')
                        {
@@ -240,19 +246,19 @@ static int mo_list(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Clie
                                {
                                        /* Creation time earlier than last x minutes. */
                                        args++;
-                                       if (IsDigit(*args))
-                                       {
-                                               params->created_max = rb_current_time() - (60 * atoi(args));
-                                       }
+                                       if (!IsDigit(*args)) goto fail;
+                                       params->created_max = rb_current_time() - (60 * atoi(args));
                                }
                                else if (*args == '<')
                                {
                                        /* Creation time within last x minutes. */
                                        args++;
-                                       if (IsDigit(*args))
-                                       {
-                                               params->created_min = rb_current_time() - (60 * atoi(args));
-                                       }
+                                       if (!IsDigit(*args)) goto fail;
+                                       params->created_min = rb_current_time() - (60 * atoi(args));
+                               }
+                               else
+                               {
+                                       goto fail;
                                }
                        }
                        else if (*args == 'T' || *args == 't')
@@ -262,20 +268,35 @@ static int mo_list(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Clie
                                {
                                        /* Topic change time earlier than last x minutes. */
                                        args++;
-                                       if (IsDigit(*args))
-                                       {
-                                               params->topic_max = rb_current_time() - (60 * atoi(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))
-                                       {
-                                               params->topic_min = rb_current_time() - (60 * atoi(args));
-                                       }
+                                       if (!IsDigit(*args)) goto fail;
+                                       params->topic_min = rb_current_time() - (60 * atoi(args));
                                }
+                               else
+                               {
+                                       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))
@@ -286,8 +307,13 @@ static int mo_list(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Clie
        }
 
        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);
 }
 
 /*
@@ -317,19 +343,16 @@ static void list_one_channel(struct Client *source_p, struct Channel *chptr,
  * 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 (rb_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);
 }
 
 /*
@@ -346,6 +369,8 @@ static int safelist_sendq_exceeded(struct Client *client_p)
  */
 static void safelist_client_instantiate(struct Client *client_p, struct ListClient *params)
 {
+       struct Channel *chptr;
+
        s_assert(MyClient(client_p));
        s_assert(params != NULL);
 
@@ -357,6 +382,12 @@ static void safelist_client_instantiate(struct Client *client_p, struct ListClie
        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);
 }
 
@@ -367,17 +398,19 @@ 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));
 
        rb_dlinkFindDestroy(client_p, &safelisting_clients);
 
        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;
@@ -461,6 +494,12 @@ static void safelist_one_channel(struct Client *source_p, struct Channel *chptr,
        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);
 }
 
@@ -474,11 +513,11 @@ static void safelist_one_channel(struct Client *source_p, struct Channel *chptr,
 static void safelist_iterate_client(struct Client *source_p)
 {
        struct Channel *chptr;
-       struct rb_radixtree_iteration_state iter;
+       rb_radixtree_iteration_state 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))
                {
                        rb_free(source_p->localClient->safelist_data->chname);
                        source_p->localClient->safelist_data->chname = rb_strdup(chptr->chname);