]> jfr.im git - solanum.git/blobdiff - modules/um_callerid.c
Make opers talking through +g controllable by user mode +M (#275)
[solanum.git] / modules / um_callerid.c
index afbd624b4e2f4de85a91ab529846d8c6d7c8b8ef..74d4ce3b038b2201b8261d77e1e8c6c4f91130c3 100644 (file)
 #include "supported.h"
 #include "logger.h"
 
-static int
-um_callerid_modinit(void)
-{
-       user_modes['g'] = find_umode_slot();
-       if (!user_modes['g'])
-       {
-               ierror("um_callerid: unable to allocate usermode slot for +g; unloading module.");
-               return -1;
-       }
+#define IsSetStrictCallerID(c) ((c->umodes & user_modes['g']) == user_modes['g'])
+#define IsSetRelaxedCallerID(c)        ((c->umodes & user_modes['G']) == user_modes['G'])
+#define IsSetAnyCallerID(c)    (IsSetStrictCallerID(c) || IsSetRelaxedCallerID(c))
+#define IsSetTalkThroughCallerID(c)    ((c->umodes & user_modes['M']) == user_modes['M'])
 
-       user_modes['G'] = find_umode_slot();
-       if (!user_modes['G'])
-       {
-               user_modes['g'] = 0;
+static const char um_callerid_desc[] =
+       "Provides usermodes +g and +G which restrict messages from unauthorized users.";
 
-               ierror("um_callerid: unable to allocate usermode slot for +G; unloading module.");
-               return -1;
-       }
 
-       construct_umodebuf();
+struct CallerIDOverrideSession {
+       rb_dlink_node node;
 
-       add_isupport("CALLERID", isupport_umode, "g");
+       struct Client *client;
+       time_t deadline;
+};
 
-       return 0;
-}
+static rb_dlink_list callerid_overriding_opers = { NULL, NULL, 0 };
+struct ev_entry *expire_callerid_override_deadlines_ev = NULL;
 
 static void
-um_callerid_moddeinit(void)
+update_session_deadline(struct Client *source_p)
 {
-       user_modes['g'] = 0;
-       user_modes['G'] = 0;
-       construct_umodebuf();
+       struct CallerIDOverrideSession *session_p = NULL;
+       rb_dlink_node *n;
 
-       delete_isupport("CALLERID");
-}
+       RB_DLINK_FOREACH(n, callerid_overriding_opers.head)
+       {
+               struct CallerIDOverrideSession *s = n->data;
 
-#define IsSetStrictCallerID(c) ((c->umodes & user_modes['g']) == user_modes['g'])
-#define IsSetRelaxedCallerID(c)        ((c->umodes & user_modes['G']) == user_modes['G'])
-#define IsSetAnyCallerID(c)    (IsSetStrictCallerID(c) || IsSetRelaxedCallerID(c))
+               if (s->client == source_p)
+               {
+                       session_p = s;
+                       break;
+               }
+       }
 
-static const char um_callerid_desc[] =
-       "Provides usermodes +g and +G which restrict messages from unauthorized users.";
+       if (session_p != NULL)
+       {
+               rb_dlinkDelete(&session_p->node, &callerid_overriding_opers);
+       }
+       else
+       {
+               session_p = rb_malloc(sizeof(struct CallerIDOverrideSession));
+               session_p->client = source_p;
+       }
 
-static bool
-has_common_channel(struct Client *source_p, struct Client *target_p)
+       session_p->deadline = rb_current_time() + 1800;
+
+       rb_dlinkAddTail(session_p, &session_p->node, &callerid_overriding_opers);
+}
+
+static void
+expire_callerid_override_deadlines(void *unused)
 {
-       rb_dlink_node *ptr;
+       rb_dlink_node *n, *tn;
 
-       RB_DLINK_FOREACH(ptr, source_p->user->channel.head)
+       RB_DLINK_FOREACH_SAFE(n, tn, callerid_overriding_opers.head)
        {
-               struct membership *msptr = ptr->data;
-               if (IsMember(target_p, msptr->chptr))
-                       return true;
-       }
+               struct CallerIDOverrideSession *session_p = n->data;
 
-       return false;
+               if (session_p->deadline >= rb_current_time())
+               {
+                       break;
+               }
+               else
+               {
+                       const char *parv[4] = {session_p->client->name, session_p->client->name, "-M", NULL};
+                       user_mode(session_p->client, session_p->client, 3, parv);
+               }
+       }
 }
 
 static bool
@@ -111,14 +125,16 @@ allow_message(struct Client *source_p, struct Client *target_p)
        if (!IsSetAnyCallerID(target_p))
                return true;
 
-       if (IsSetRelaxedCallerID(target_p) && has_common_channel(source_p, target_p) && !IsSetStrictCallerID(target_p))
+       if (!IsPerson(source_p))
                return true;
 
-       if (IsServer(source_p))
+       if (IsSetRelaxedCallerID(target_p) &&
+                       !IsSetStrictCallerID(target_p) &&
+                       has_common_channel(source_p, target_p))
                return true;
 
        /* XXX: controversial?  allow opers to send through +g */
-       if (IsOper(source_p))
+       if (IsSetTalkThroughCallerID(source_p) || MayHavePrivilege(source_p, "oper:always_message"))
                return true;
 
        if (accept_message(source_p, target_p))
@@ -168,7 +184,7 @@ add_callerid_accept_for_source(enum message_type msgtype, struct Client *source_
        if(msgtype != MESSAGE_TYPE_NOTICE &&
                IsSetAnyCallerID(source_p) &&
                !accept_message(target_p, source_p) &&
-               !IsOper(target_p))
+               !IsOperGeneral(target_p))
        {
                if(rb_dlink_list_length(&source_p->localClient->allow_list) <
                                (unsigned long)ConfigFileEntry.max_accept)
@@ -180,7 +196,7 @@ add_callerid_accept_for_source(enum message_type msgtype, struct Client *source_
                {
                        sendto_one_numeric(source_p, ERR_OWNMODE,
                                        form_str(ERR_OWNMODE),
-                                       target_p->name, "+g");
+                                       target_p->name, IsSetStrictCallerID(target_p) ? "+g" : "+G");
                        return false;
                }
        }
@@ -240,11 +256,156 @@ h_hdl_privmsg_user(void *vdata)
        data->approved = ERR_TARGUMODEG;
 }
 
+static void
+check_umode_change(void *vdata)
+{
+       hook_data_umode_changed *data = (hook_data_umode_changed *)vdata;
+       bool changed = false;
+       struct Client *source_p = data->client;
+
+       if (!MyClient(source_p))
+               return;
+
+       if (data->oldumodes & UMODE_OPER && !IsOper(source_p))
+               source_p->umodes &= ~user_modes['M'];
+
+       changed = ((data->oldumodes ^ source_p->umodes) & user_modes['M']);
+
+       if (changed && source_p->umodes & user_modes['M'])
+       {
+               if (!HasPrivilege(source_p, "oper:message"))
+               {
+                       sendto_one_notice(source_p, ":*** You need oper:message privilege for +M");
+                       source_p->umodes &= ~user_modes['M'];
+                       return;
+               }
+
+               update_session_deadline(source_p);
+       }
+       else if (changed)
+       {
+               // Unsetting +M; remove the timeout session
+               rb_dlink_node *n, *tn;
+
+               RB_DLINK_FOREACH_SAFE(n, tn, callerid_overriding_opers.head)
+               {
+                       struct CallerIDOverrideSession *session_p = n->data;
+
+                       if (session_p->client != source_p)
+                               continue;
+
+                       rb_dlinkDelete(n, &callerid_overriding_opers);
+                       rb_free(session_p);
+               }
+       }
+}
+
+static void check_priv_change(void *vdata)
+{
+       hook_data_priv_change *data = (hook_data_priv_change*)vdata;
+       struct Client *source_p = data->client;
+       const char *fakeparv[4];
+
+       if (!MyClient(source_p))
+               return;
+
+       if (source_p->umodes & user_modes['M'] && !HasPrivilege(source_p, "oper:message"))
+       {
+               sendto_one_notice(source_p, ":*** You need oper:message privilege for +M");
+               fakeparv[0] = fakeparv[1] = source_p->name;
+               fakeparv[2] = "-M";
+               fakeparv[3] = NULL;
+               user_mode(source_p, source_p, 3, fakeparv);
+       }
+}
+
+static void
+handle_client_exit(void *vdata)
+{
+       hook_data_client_exit *data = (hook_data_client_exit *) vdata;
+       rb_dlink_node *n, *tn;
+       struct Client *source_p = data->target;
+
+       RB_DLINK_FOREACH_SAFE(n, tn, callerid_overriding_opers.head)
+       {
+               struct CallerIDOverrideSession *session_p = n->data;
+
+               if (session_p->client != source_p)
+                       continue;
+
+               rb_dlinkDelete(n, &callerid_overriding_opers);
+               rb_free(session_p);
+       }
+}
+
 static mapi_hfn_list_av1 um_callerid_hfnlist[] = {
+       { "umode_changed", check_umode_change },
+       { "priv_change", check_priv_change },
        { "invite", h_hdl_invite },
        { "privmsg_user", h_hdl_privmsg_user },
+       { "client_exit", handle_client_exit },
        { NULL, NULL }
 };
 
+static int
+um_callerid_modinit(void)
+{
+       rb_dlink_node *ptr;
+
+       user_modes['g'] = find_umode_slot();
+       if (!user_modes['g'])
+       {
+               ierror("um_callerid: unable to allocate usermode slot for +g; unloading module.");
+               return -1;
+       }
+
+       user_modes['G'] = find_umode_slot();
+       if (!user_modes['G'])
+       {
+               user_modes['g'] = 0;
+
+               ierror("um_callerid: unable to allocate usermode slot for +G; unloading module.");
+               return -1;
+       }
+
+       user_modes['M'] = find_umode_slot();
+       if (!user_modes['M'])
+       {
+               user_modes['g'] = 0;
+               user_modes['G'] = 0;
+
+               ierror("um_callerid: unable to allocate usermode slot for +M; unloading module.");
+               return -1;
+       }
+
+       construct_umodebuf();
+
+       add_isupport("CALLERID", isupport_umode, "g");
+
+       RB_DLINK_FOREACH(ptr, lclient_list.head)
+       {
+               struct Client *client_p = ptr->data;
+               if (IsPerson(client_p) && (client_p->umodes & user_modes['M']))
+                       update_session_deadline(client_p);
+       }
+
+       expire_callerid_override_deadlines_ev = rb_event_add("expire_callerid_override_deadlines", expire_callerid_override_deadlines, NULL, 60);
+
+       return 0;
+}
+
+static void
+um_callerid_moddeinit(void)
+{
+       user_modes['g'] = 0;
+       user_modes['G'] = 0;
+       user_modes['M'] = 0;
+       construct_umodebuf();
+
+       delete_isupport("CALLERID");
+
+       rb_event_delete(expire_callerid_override_deadlines_ev);
+}
+
 DECLARE_MODULE_AV2(um_callerid, um_callerid_modinit, um_callerid_moddeinit,
                   NULL, NULL, um_callerid_hfnlist, NULL, NULL, um_callerid_desc);