Software /
code /
prosody-modules
Comparison
mod_muc_inject_mentions/mod_muc_inject_mentions.lua @ 4155:308b92b07da6
mod_muc_inject_mentions: Refactor code using in_list utility function to improve readability
author | Seve Ferrer <seve@delape.net> |
---|---|
date | Mon, 28 Sep 2020 15:30:22 +0200 |
parent | 4145:837c4340653f |
child | 4159:94e3e7753220 |
comparison
equal
deleted
inserted
replaced
4151:fa79d19d0fdd | 4155:308b92b07da6 |
---|---|
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 | 9 |
10 local reference_xmlns = "urn:xmpp:reference:0" | 10 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 | |
11 | 24 |
12 local function is_room_eligible(jid) | 25 local function is_room_eligible(jid) |
13 if not enabled_rooms and not disabled_rooms then | 26 if not enabled_rooms and not disabled_rooms then |
14 return true; | 27 return true; |
15 end | 28 end |
34 | 47 |
35 return true | 48 return true |
36 end | 49 end |
37 | 50 |
38 local function has_nick_prefix(body, first) | 51 local function has_nick_prefix(body, first) |
39 -- There is no prefix | |
40 -- but mention could still be valid | |
41 if first == 1 then return true end | |
42 | |
43 -- There are no configured prefixes | 52 -- There are no configured prefixes |
44 if not prefixes or #prefixes < 1 then return false end | 53 if not prefixes or #prefixes < 1 then return false end |
45 | 54 |
46 -- Preffix must have a space before it, | 55 -- Preffix must have a space before it, |
47 -- be the first character of the body | 56 -- be the first character of the body |
48 -- or be the first character after a new line | 57 -- or be the first character after a new line |
49 if body:sub(first - 2, first - 2) ~= "" and | 58 if not in_list(body:sub(first - 2, first - 2), mention_delimiters) then |
50 body:sub(first - 2, first - 2) ~= " " and | |
51 body:sub(first - 2, first - 2) ~= "\n" | |
52 then | |
53 return false | 59 return false |
54 end | 60 end |
55 | 61 |
56 local preffix = body:sub(first - 1, first - 1) | 62 local preffix = body:sub(first - 1, first - 1) |
57 for _, _preffix in ipairs(prefixes) do | 63 for _, _preffix in ipairs(prefixes) do |
62 | 68 |
63 return false | 69 return false |
64 end | 70 end |
65 | 71 |
66 local function has_nick_suffix(body, last) | 72 local function has_nick_suffix(body, last) |
67 -- There is no suffix | |
68 -- but mention could still be valid | |
69 if last == #body then return true end | |
70 | |
71 -- There are no configured suffixes | 73 -- There are no configured suffixes |
72 if not suffixes or #suffixes < 1 then return false end | 74 if not suffixes or #suffixes < 1 then return false end |
73 | 75 |
74 -- Suffix must have a space after it, | 76 -- Suffix must have a space after it, |
75 -- be the last character of the body | 77 -- be the last character of the body |
76 -- or be the last character before a new line | 78 -- or be the last character before a new line |
77 if body:sub(last + 2, last + 2) ~= "" and | 79 if not in_list(body:sub(last + 2, last + 2), mention_delimiters) then |
78 body:sub(last + 2, last + 2) ~= " " and | |
79 body:sub(last + 2, last + 2) ~= "\n" | |
80 then | |
81 return false | 80 return false |
82 end | 81 end |
83 | 82 |
84 local suffix = body:sub(last+1, last+1) | 83 local suffix = body:sub(last+1, last+1) |
85 for _, _suffix in ipairs(suffixes) do | 84 for _, _suffix in ipairs(suffixes) do |
113 -- Filter out intentional mentions from unintentional ones | 112 -- Filter out intentional mentions from unintentional ones |
114 for _, match in ipairs(matches) do | 113 for _, match in ipairs(matches) do |
115 local bare_jid = occupant.bare_jid | 114 local bare_jid = occupant.bare_jid |
116 local first, last = match.first, match.last | 115 local first, last = match.first, match.last |
117 | 116 |
118 -- Body only contains nickname | 117 -- Body only contains nickname or is between spaces, new lines or at the end/start of the body |
119 if first == 1 and last == #body then | 118 if in_list(body:sub(first - 1, first - 1), mention_delimiters) and |
120 table.insert(mentions, {bare_jid=bare_jid, first=first, last=last}) | 119 in_list(body:sub(last + 1, last + 1), mention_delimiters) |
121 | |
122 -- Nickname between spaces or new lines | |
123 elseif body:sub(first - 1, first - 1) == " " or body:sub(first - 1, first - 1) == "\n" and | |
124 body:sub(last + 1, last + 1) == " " or body:sub(last + 1, last + 1) == "\n" | |
125 then | 120 then |
126 table.insert(mentions, {bare_jid=bare_jid, first=first, last=last}) | 121 table.insert(mentions, {bare_jid=bare_jid, first=first, last=last}) |
127 else | 122 else |
128 -- Check if occupant is mentioned using affixes | 123 -- Check if occupant is mentioned using affixes |
129 local has_preffix = has_nick_prefix(body, first) | 124 local has_preffix = has_nick_prefix(body, first) |
133 if has_preffix and has_suffix then | 128 if has_preffix and has_suffix then |
134 table.insert(mentions, {bare_jid=bare_jid, first=first, last=last}) | 129 table.insert(mentions, {bare_jid=bare_jid, first=first, last=last}) |
135 | 130 |
136 -- @nickname ... | 131 -- @nickname ... |
137 elseif has_preffix and not has_suffix then | 132 elseif has_preffix and not has_suffix then |
138 if body:sub(last + 1, last + 1) == " " or | 133 if in_list(body:sub(last + 1, last + 1), mention_delimiters) then |
139 body:sub(last + 1, last + 1) == "\n" | |
140 then | |
141 table.insert(mentions, {bare_jid=bare_jid, first=first, last=last}) | 134 table.insert(mentions, {bare_jid=bare_jid, first=first, last=last}) |
142 end | 135 end |
143 | 136 |
144 -- nickname: ... | 137 -- nickname: ... |
145 elseif not has_preffix and has_suffix then | 138 elseif not has_preffix and has_suffix then |
146 if body:sub(first - 1, first - 1) == " " or | 139 if in_list(body:sub(first - 1, first - 1), mention_delimiters) then |
147 body:sub(first - 1, first - 1) == "\n" | |
148 then | |
149 table.insert(mentions, {bare_jid=bare_jid, first=first, last=last}) | 140 table.insert(mentions, {bare_jid=bare_jid, first=first, last=last}) |
150 end | 141 end |
151 end | 142 end |
152 end | 143 end |
153 end | 144 end |