]> jfr.im git - solanum.git/blobdiff - extensions/helpops.c
Remove Windows support
[solanum.git] / extensions / helpops.c
index e3a77a02f470483ff9fc1a18e2d1bc48e32a0309..72d812902d0f8e9657b0ebec3d186d7261d567f3 100644 (file)
 #include "s_newconf.h"
 #include "numeric.h"
 
+static const char helpops_desc[] = "The helpops system as used by freenode";
+
 static rb_dlink_list helper_list = { NULL, NULL, 0 };
 static void h_hdl_stats_request(hook_data_int *hdata);
 static void h_hdl_new_remote_user(struct Client *client_p);
 static void h_hdl_client_exit(hook_data_client_exit *hdata);
 static void h_hdl_umode_changed(hook_data_umode_changed *hdata);
 static void h_hdl_whois(hook_data_client *hdata);
-static int mo_dehelper(struct Client *, struct Client *, int, const char **);
-static int me_dehelper(struct Client *, struct Client *, int, const char **);
-static int do_dehelper(struct Client *source_p, struct Client *target_p);
+static void recurse_client_exit(struct Client *client_p);
+static void helper_add(struct Client *client_p);
+static void helper_delete(struct Client *client_p);
+static void mo_dehelper(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
+static void me_dehelper(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
+static void do_dehelper(struct Client *source_p, struct Client *target_p);
 
 mapi_hfn_list_av1 helpops_hfnlist[] = {
        { "doing_stats", (hookfn) h_hdl_stats_request },
@@ -34,29 +39,30 @@ mapi_hfn_list_av1 helpops_hfnlist[] = {
        { NULL, NULL }
 };
 
-static int UMODE_HELPOPS = 0;
+#define UMODECHAR_HELPOPS 'h'
 
 struct Message dehelper_msgtab = {
-       "DEHELPER", 0, 0, 0, MFLG_SLOW,
+       "DEHELPER", 0, 0, 0, 0,
        {mg_unreg, mg_not_oper, mg_not_oper, mg_ignore, {me_dehelper, 2}, {mo_dehelper, 2}}
 };
 
 mapi_clist_av1 helpops_clist[] = { &dehelper_msgtab, NULL };
 
-static int mo_dehelper(struct Client *client_p, struct Client *source_p, int parc, const char **parv)
+static void
+mo_dehelper(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char **parv)
 {
        struct Client *target_p;
 
-       if (!IsOperAdmin(source_p))
+       if (!HasPrivilege(source_p, "oper:dehelper"))
        {
-               sendto_one(source_p, form_str(ERR_NOPRIVS), me.name, source_p->name, "admin");
-               return 0;
+               sendto_one(source_p, form_str(ERR_NOPRIVS), me.name, source_p->name, "dehelper");
+               return;
        }
 
        if(!(target_p = find_named_person(parv[1])))
        {
                sendto_one_numeric(source_p, ERR_NOSUCHNICK, form_str(ERR_NOSUCHNICK), parv[1]);
-               return 0;
+               return;
        }
 
        if(MyClient(target_p))
@@ -64,59 +70,70 @@ static int mo_dehelper(struct Client *client_p, struct Client *source_p, int par
        else
                sendto_one(target_p, ":%s ENCAP %s DEHELPER %s",
                                use_id(source_p), target_p->servptr->name, use_id(target_p));
-
-       return 0;
 }
 
-static int me_dehelper(struct Client *client_p, struct Client *source_p, int parc, const char **parv)
+static void
+me_dehelper(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char **parv)
 {
        struct Client *target_p = find_person(parv[1]);
        if(!target_p)
        {
                sendto_one_numeric(source_p, ERR_NOSUCHNICK, form_str(ERR_NOSUCHNICK), parv[1]);
-               return 0;
+               return;
        }
        if(!MyClient(target_p))
-               return 0;
+               return;
 
        do_dehelper(source_p, target_p);
-       return 0;
 }
 
-static int do_dehelper(struct Client *source_p, struct Client *target_p)
+static void
+do_dehelper(struct Client *source_p, struct Client *target_p)
 {
        const char *fakeparv[4];
+       static const char minus_helpops[3] = {'-', UMODECHAR_HELPOPS, '\0'};
 
-       if(!(target_p->umodes & UMODE_HELPOPS))
-               return 0;
+       if(!(target_p->umodes & user_modes[UMODECHAR_HELPOPS]))
+               return;
 
        sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, "%s is using DEHELPER on %s",
                        source_p->name, target_p->name);
        sendto_one_notice(target_p, ":*** %s is using DEHELPER on you", source_p->name);
 
        fakeparv[0] = fakeparv[1] = target_p->name;
-       fakeparv[2] = "-H";
+       fakeparv[2] = minus_helpops;
        fakeparv[3] = NULL;
        user_mode(target_p, target_p, 3, fakeparv);
-       return 0;
 }
 
 static int
 _modinit(void)
 {
-       /* add the usermode to the available slot */
-       user_modes['H'] = UMODE_HELPOPS = find_umode_slot();
+       rb_dlink_node *ptr;
+
+       user_modes[UMODECHAR_HELPOPS] = find_umode_slot();
        construct_umodebuf();
 
+       RB_DLINK_FOREACH (ptr, global_client_list.head)
+       {
+               struct Client *client_p = ptr->data;
+               if (IsPerson(client_p) && (client_p->umodes & user_modes[UMODECHAR_HELPOPS]))
+                       helper_add(client_p);
+       }
+
        return 0;
 }
 
 static void
 _moddeinit(void)
 {
-       /* disable the umode and remove it from the available list */
-       user_modes['H'] = UMODE_HELPOPS = 0;
+       rb_dlink_node *n, *tn;
+
+       user_modes[UMODECHAR_HELPOPS] = 0;
        construct_umodebuf();
+
+       RB_DLINK_FOREACH_SAFE(n, tn, helper_list.head)
+               rb_dlinkDestroy(n, &helper_list);
 }
 
 static void
@@ -133,9 +150,6 @@ h_hdl_stats_request(hook_data_int *hdata)
        {
                target_p = helper_ptr->data;
 
-               if(IsOperInvis(target_p) && !IsOper(hdata->client))
-                       continue;
-
                if(target_p->user->away)
                        continue;
 
@@ -153,18 +167,52 @@ h_hdl_stats_request(hook_data_int *hdata)
        hdata->result = 1;
 }
 
+static void
+helper_add(struct Client *client_p)
+{
+       if (rb_dlinkFind(client_p, &helper_list) != NULL)
+               return;
+
+       rb_dlinkAddAlloc(client_p, &helper_list);
+}
+
+static void
+helper_delete(struct Client *client_p)
+{
+       rb_dlinkFindDestroy(client_p, &helper_list);
+}
+
 static void
 h_hdl_new_remote_user(struct Client *client_p)
 {
-       if (client_p->umodes & UMODE_HELPOPS)
-               rb_dlinkAddAlloc(client_p, &helper_list);
+       if (client_p->umodes & user_modes[UMODECHAR_HELPOPS])
+               helper_add(client_p);
+}
+
+static void
+recurse_client_exit(struct Client *client_p)
+{
+       if (IsPerson(client_p))
+       {
+               if (client_p->umodes & user_modes[UMODECHAR_HELPOPS])
+                       helper_delete(client_p);
+       }
+       else if (IsServer(client_p))
+       {
+               rb_dlink_node *nptr;
+
+               RB_DLINK_FOREACH(nptr, client_p->serv->users.head)
+                       recurse_client_exit(nptr->data);
+
+               RB_DLINK_FOREACH(nptr, client_p->serv->servers.head)
+                       recurse_client_exit(nptr->data);
+       }
 }
 
 static void
 h_hdl_client_exit(hook_data_client_exit *hdata)
 {
-       if (hdata->target->umodes & UMODE_HELPOPS)
-               rb_dlinkFindDestroy(hdata->target, &helper_list);
+       recurse_client_exit(hdata->target);
 }
 
 static void
@@ -172,23 +220,28 @@ h_hdl_umode_changed(hook_data_umode_changed *hdata)
 {
        struct Client *source_p = hdata->client;
 
-       /* didn't change +H umode, we don't need to do anything */
-       if (!((hdata->oldumodes ^ source_p->umodes) & UMODE_HELPOPS))
-               return;
+       /* didn't change +h umode, we don't need to do anything */
+       bool changed = (hdata->oldumodes ^ source_p->umodes) & user_modes[UMODECHAR_HELPOPS];
 
-       if (source_p->umodes & UMODE_HELPOPS)
+       if (source_p->umodes & user_modes[UMODECHAR_HELPOPS])
        {
                if (MyClient(source_p) && !HasPrivilege(source_p, "usermode:helpops"))
                {
-                       source_p->umodes &= ~UMODE_HELPOPS;
+                       source_p->umodes &= ~user_modes[UMODECHAR_HELPOPS];
                        sendto_one(source_p, form_str(ERR_NOPRIVS), me.name, source_p->name, "usermode:helpops");
+                       /* they didn't ask for +h so we must be removing it */
+                       if (!changed)
+                               helper_delete(source_p);
                        return;
                }
 
-               rb_dlinkAddAlloc(source_p, &helper_list);
+               if (changed)
+                       helper_add(source_p);
+       }
+       else if (changed)
+       {
+               helper_delete(source_p);
        }
-       else if (!(source_p->umodes & UMODE_HELPOPS))
-               rb_dlinkFindDestroy(source_p, &helper_list);
 }
 
 static void
@@ -197,10 +250,10 @@ h_hdl_whois(hook_data_client *hdata)
        struct Client *source_p = hdata->client;
        struct Client *target_p = hdata->target;
 
-       if ((target_p->umodes & UMODE_HELPOPS) && EmptyString(target_p->user->away))
+       if ((target_p->umodes & user_modes[UMODECHAR_HELPOPS]) && EmptyString(target_p->user->away))
        {
                sendto_one_numeric(source_p, RPL_WHOISHELPOP, form_str(RPL_WHOISHELPOP), target_p->name);
        }
 }
 
-DECLARE_MODULE_AV1(helpops, _modinit, _moddeinit, helpops_clist, NULL, helpops_hfnlist, "");
+DECLARE_MODULE_AV2(helpops, _modinit, _moddeinit, helpops_clist, NULL, helpops_hfnlist, NULL, NULL, helpops_desc);