diff mod_muc_inject_mentions/mod_muc_inject_mentions.lua @ 4159:94e3e7753220

mod_muc_inject_mentions: Improve mentions lookup by using a set instead of a list
author Seve Ferrer <seve@delape.net>
date Mon, 28 Sep 2020 18:46:33 +0200
parents 308b92b07da6
children 032e1c79d039
line wrap: on
line diff
--- a/mod_muc_inject_mentions/mod_muc_inject_mentions.lua	Wed Sep 30 13:27:27 2020 +0100
+++ b/mod_muc_inject_mentions/mod_muc_inject_mentions.lua	Mon Sep 28 18:46:33 2020 +0200
@@ -6,21 +6,10 @@
 local suffixes = module:get_option("muc_inject_mentions_suffixes", nil)
 local enabled_rooms = module:get_option("muc_inject_mentions_enabled_rooms", nil)
 local disabled_rooms = module:get_option("muc_inject_mentions_disabled_rooms", nil)
-
-local reference_xmlns = "urn:xmpp:reference:0"
+local mention_delimiters = module:get_option_set("muc_inject_mentions_mention_delimiters",  {" ", "", "\n"})
 
 
-local mention_delimiters = {" ", "", "\n"}
-
-local function in_list(value, list)
-    for _, v in ipairs(list) do
-        if v == value then
-            return true
-        end
-    end
-    return false
-end
-
+local reference_xmlns = "urn:xmpp:reference:0"
 
 local function is_room_eligible(jid)
     if not enabled_rooms and not disabled_rooms then
@@ -55,7 +44,7 @@
     -- Preffix must have a space before it,
     -- be the first character of the body
     -- or be the first character after a new line
-    if not in_list(body:sub(first - 2, first - 2), mention_delimiters) then
+    if not mention_delimiters:contains(body:sub(first - 2, first - 2)) then
         return false
     end
 
@@ -76,7 +65,7 @@
     -- Suffix must have a space after it,
     -- be the last character of the body
     -- or be the last character before a new line
-    if not in_list(body:sub(last + 2, last + 2), mention_delimiters) then
+    if not mention_delimiters:contains(body:sub(last + 2, last + 2)) then
         return false
     end
 
@@ -115,8 +104,8 @@
             local first, last = match.first, match.last
 
             -- Body only contains nickname or is between spaces, new lines or at the end/start of the body
-            if in_list(body:sub(first - 1, first - 1), mention_delimiters) and
-                in_list(body:sub(last + 1, last + 1), mention_delimiters)
+            if mention_delimiters:contains(body:sub(first - 1, first - 1)) and
+                mention_delimiters:contains(body:sub(last + 1, last + 1))
             then
                 table.insert(mentions, {bare_jid=bare_jid, first=first, last=last})
             else
@@ -130,13 +119,13 @@
 
                 -- @nickname ...
                 elseif has_preffix and not has_suffix then
-                    if in_list(body:sub(last + 1, last + 1), mention_delimiters) then
+                    if mention_delimiters:contains(body:sub(last + 1, last + 1)) then
                         table.insert(mentions, {bare_jid=bare_jid, first=first, last=last})
                     end
 
                 -- nickname: ...
                 elseif not has_preffix and has_suffix then
-                    if in_list(body:sub(first - 1, first - 1), mention_delimiters) then
+                    if mention_delimiters:contains(body:sub(first - 1, first - 1)) then
                         table.insert(mentions, {bare_jid=bare_jid, first=first, last=last})
                     end
                 end