comparison 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
comparison
equal deleted inserted replaced
4158:df1e0465ff81 4159:94e3e7753220
4 4
5 local prefixes = module:get_option("muc_inject_mentions_prefixes", nil) 5 local prefixes = module:get_option("muc_inject_mentions_prefixes", nil)
6 local suffixes = module:get_option("muc_inject_mentions_suffixes", nil) 6 local suffixes = module:get_option("muc_inject_mentions_suffixes", nil)
7 local enabled_rooms = module:get_option("muc_inject_mentions_enabled_rooms", nil) 7 local enabled_rooms = module:get_option("muc_inject_mentions_enabled_rooms", nil)
8 local disabled_rooms = module:get_option("muc_inject_mentions_disabled_rooms", nil) 8 local disabled_rooms = module:get_option("muc_inject_mentions_disabled_rooms", nil)
9 local mention_delimiters = module:get_option_set("muc_inject_mentions_mention_delimiters", {" ", "", "\n"})
10
9 11
10 local reference_xmlns = "urn:xmpp:reference:0" 12 local reference_xmlns = "urn:xmpp:reference:0"
11
12
13 local mention_delimiters = {" ", "", "\n"}
14
15 local function in_list(value, list)
16 for _, v in ipairs(list) do
17 if v == value then
18 return true
19 end
20 end
21 return false
22 end
23
24 13
25 local function is_room_eligible(jid) 14 local function is_room_eligible(jid)
26 if not enabled_rooms and not disabled_rooms then 15 if not enabled_rooms and not disabled_rooms then
27 return true; 16 return true;
28 end 17 end
53 if not prefixes or #prefixes < 1 then return false end 42 if not prefixes or #prefixes < 1 then return false end
54 43
55 -- Preffix must have a space before it, 44 -- Preffix must have a space before it,
56 -- be the first character of the body 45 -- be the first character of the body
57 -- or be the first character after a new line 46 -- or be the first character after a new line
58 if not in_list(body:sub(first - 2, first - 2), mention_delimiters) then 47 if not mention_delimiters:contains(body:sub(first - 2, first - 2)) then
59 return false 48 return false
60 end 49 end
61 50
62 local preffix = body:sub(first - 1, first - 1) 51 local preffix = body:sub(first - 1, first - 1)
63 for _, _preffix in ipairs(prefixes) do 52 for _, _preffix in ipairs(prefixes) do
74 if not suffixes or #suffixes < 1 then return false end 63 if not suffixes or #suffixes < 1 then return false end
75 64
76 -- Suffix must have a space after it, 65 -- Suffix must have a space after it,
77 -- be the last character of the body 66 -- be the last character of the body
78 -- or be the last character before a new line 67 -- or be the last character before a new line
79 if not in_list(body:sub(last + 2, last + 2), mention_delimiters) then 68 if not mention_delimiters:contains(body:sub(last + 2, last + 2)) then
80 return false 69 return false
81 end 70 end
82 71
83 local suffix = body:sub(last+1, last+1) 72 local suffix = body:sub(last+1, last+1)
84 for _, _suffix in ipairs(suffixes) do 73 for _, _suffix in ipairs(suffixes) do
113 for _, match in ipairs(matches) do 102 for _, match in ipairs(matches) do
114 local bare_jid = occupant.bare_jid 103 local bare_jid = occupant.bare_jid
115 local first, last = match.first, match.last 104 local first, last = match.first, match.last
116 105
117 -- Body only contains nickname or is between spaces, new lines or at the end/start of the body 106 -- Body only contains nickname or is between spaces, new lines or at the end/start of the body
118 if in_list(body:sub(first - 1, first - 1), mention_delimiters) and 107 if mention_delimiters:contains(body:sub(first - 1, first - 1)) and
119 in_list(body:sub(last + 1, last + 1), mention_delimiters) 108 mention_delimiters:contains(body:sub(last + 1, last + 1))
120 then 109 then
121 table.insert(mentions, {bare_jid=bare_jid, first=first, last=last}) 110 table.insert(mentions, {bare_jid=bare_jid, first=first, last=last})
122 else 111 else
123 -- Check if occupant is mentioned using affixes 112 -- Check if occupant is mentioned using affixes
124 local has_preffix = has_nick_prefix(body, first) 113 local has_preffix = has_nick_prefix(body, first)
128 if has_preffix and has_suffix then 117 if has_preffix and has_suffix then
129 table.insert(mentions, {bare_jid=bare_jid, first=first, last=last}) 118 table.insert(mentions, {bare_jid=bare_jid, first=first, last=last})
130 119
131 -- @nickname ... 120 -- @nickname ...
132 elseif has_preffix and not has_suffix then 121 elseif has_preffix and not has_suffix then
133 if in_list(body:sub(last + 1, last + 1), mention_delimiters) then 122 if mention_delimiters:contains(body:sub(last + 1, last + 1)) then
134 table.insert(mentions, {bare_jid=bare_jid, first=first, last=last}) 123 table.insert(mentions, {bare_jid=bare_jid, first=first, last=last})
135 end 124 end
136 125
137 -- nickname: ... 126 -- nickname: ...
138 elseif not has_preffix and has_suffix then 127 elseif not has_preffix and has_suffix then
139 if in_list(body:sub(first - 1, first - 1), mention_delimiters) then 128 if mention_delimiters:contains(body:sub(first - 1, first - 1)) then
140 table.insert(mentions, {bare_jid=bare_jid, first=first, last=last}) 129 table.insert(mentions, {bare_jid=bare_jid, first=first, last=last})
141 end 130 end
142 end 131 end
143 end 132 end
144 end 133 end