Changeset

4164:a82b0745383b

mod_muc_inject_mentions: Add new configuration setting to choose between registered nicknames or online participants
author Seve Ferrer <seve@delape.net>
date Thu, 01 Oct 2020 16:20:09 +0200
parents 4163:320f6d374b5d
children 4165:6b2a1c9ef6e2
files mod_muc_inject_mentions/README.markdown mod_muc_inject_mentions/mod_muc_inject_mentions.lua
diffstat 2 files changed, 43 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/mod_muc_inject_mentions/README.markdown	Wed Sep 30 13:14:46 2020 +0200
+++ b/mod_muc_inject_mentions/README.markdown	Thu Oct 01 16:20:09 2020 +0200
@@ -71,6 +71,20 @@
 If the client sends a mention type reference pointing to a nickname using a prefix (`Hey @someone`), the prefix will not be removed.
 
 
+There are two lists where this module pulls the participants from.
+1. Online participants
+2. Participants with registered nicknames
+
+By default, the module will try to find mentions to online participants.
+Using:
+```
+muc_inject_mentions_reserved_nicks = true
+```
+Will try to find mentions to participants with registered nicknames.
+This is useful for setups where the nickname is reserved for all participants,
+allowing the module to catch mentions to participants that might not be online at the moment of sending the message.
+
+
 It is also possible to modify how this module detects mentions.
 In short, the module will detect if a mention is actually a mention
 if the nickname (with or without affixes) is between spaces, new lines, or at the beginning/end of the message.
--- a/mod_muc_inject_mentions/mod_muc_inject_mentions.lua	Wed Sep 30 13:14:46 2020 +0200
+++ b/mod_muc_inject_mentions/mod_muc_inject_mentions.lua	Thu Oct 01 16:20:09 2020 +0200
@@ -10,10 +10,38 @@
 local mention_delimiters = module:get_option_set("muc_inject_mentions_mention_delimiters",  {" ", "", "\n"})
 local append_mentions = module:get_option("muc_inject_mentions_append_mentions", false)
 local strip_out_prefixes = module:get_option("muc_inject_mentions_strip_out_prefixes", false)
+local reserved_nicks = module:get_option("muc_inject_mentions_reserved_nicks", false)
 
 
 local reference_xmlns = "urn:xmpp:reference:0"
 
+local function get_participants(room)
+    if not reserved_nicks then
+        local occupants = room._occupants
+        local key, occupant = next(occupants)
+        return function ()
+            while occupant do -- luacheck: ignore
+                local nick = jid_resource(occupant.nick);
+                local bare_jid = occupant.bare_jid
+                key, occupant = next(occupants, key)
+                return bare_jid, nick
+            end
+        end
+    else
+        local generator = room:each_affiliation()
+        local jid, _, affiliation_data = generator(nil, nil)
+        return function ()
+           while jid do
+                local bare_jid, nick = jid, (affiliation_data or {})["reserved_nickname"]
+                jid, _, affiliation_data = generator(nil, bare_jid)
+                if nick then
+                    return bare_jid, nick
+                end
+           end
+        end
+    end
+end
+
 local function add_mention(mentions, bare_jid, first, last, prefix_indices, has_prefix)
     if strip_out_prefixes then
         if has_prefix then
@@ -111,8 +139,7 @@
 local function search_mentions(room, body, client_mentions)
     local mentions, prefix_indices = {}, {}
 
-    for _, occupant in pairs(room._occupants) do
-        local nick = jid_resource(occupant.nick);
+    for bare_jid, nick in get_participants(room) do
         -- Check for multiple mentions to the same nickname in a message
         -- Hey @nick remember to... Ah, also @nick please let me know if...
         local matches = {}
@@ -128,7 +155,6 @@
 
         -- Filter out intentional mentions from unintentional ones
         for _, match in ipairs(matches) do
-            local bare_jid = occupant.bare_jid
             local first, last = match.first, match.last
             -- Only append new mentions in case the client already sent some
             if not client_mentions[first] then