annotate mod_muc_inject_mentions/mod_muc_inject_mentions.lua @ 4210:a0937b5cfdcb

mod_invites_page: Remove preauth URI button This button is incompatible with the majority of XMPP clients around, yet based on feedback from users, many are drawn to click it when they have any XMPP client installed already. In the case where the user already has software installed, we would prefer them to select it from the software list so they can follow the setup process suited to their specific client (we already track which software supports preauth URIs). If their client is not listed, they can still use the manual registration link instead.
author Matthew Wild <mwild1@gmail.com>
date Fri, 16 Oct 2020 11:03:38 +0100
parents a82b0745383b
children aed7038ab2ab
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4138
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
1 module:depends("muc");
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
2
4140
fea4a4831e10 mod_muc_inject_mentions: (W211) unused variable node, (W211) unused variable host
Seve Ferrer <seve@delape.net>
parents: 4139
diff changeset
3 local jid_resource = require "util.jid".resource;
4163
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4162
diff changeset
4 local st = require "util.stanza";
4138
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
5
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
6 local prefixes = module:get_option("muc_inject_mentions_prefixes", nil)
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
7 local suffixes = module:get_option("muc_inject_mentions_suffixes", nil)
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
8 local enabled_rooms = module:get_option("muc_inject_mentions_enabled_rooms", nil)
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
9 local disabled_rooms = module:get_option("muc_inject_mentions_disabled_rooms", nil)
4159
94e3e7753220 mod_muc_inject_mentions: Improve mentions lookup by using a set instead of a list
Seve Ferrer <seve@delape.net>
parents: 4155
diff changeset
10 local mention_delimiters = module:get_option_set("muc_inject_mentions_mention_delimiters", {" ", "", "\n"})
4162
f7bc0e4ab4a2 mod_muc_inject_mentions: Should not append mentions by default
Seve Ferrer <seve@delape.net>
parents: 4161
diff changeset
11 local append_mentions = module:get_option("muc_inject_mentions_append_mentions", false)
4163
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4162
diff changeset
12 local strip_out_prefixes = module:get_option("muc_inject_mentions_strip_out_prefixes", false)
4164
a82b0745383b mod_muc_inject_mentions: Add new configuration setting to choose between registered nicknames or online participants
Seve Ferrer <seve@delape.net>
parents: 4163
diff changeset
13 local reserved_nicks = module:get_option("muc_inject_mentions_reserved_nicks", false)
4138
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
14
4155
308b92b07da6 mod_muc_inject_mentions: Refactor code using in_list utility function to improve readability
Seve Ferrer <seve@delape.net>
parents: 4145
diff changeset
15
4159
94e3e7753220 mod_muc_inject_mentions: Improve mentions lookup by using a set instead of a list
Seve Ferrer <seve@delape.net>
parents: 4155
diff changeset
16 local reference_xmlns = "urn:xmpp:reference:0"
4155
308b92b07da6 mod_muc_inject_mentions: Refactor code using in_list utility function to improve readability
Seve Ferrer <seve@delape.net>
parents: 4145
diff changeset
17
4164
a82b0745383b mod_muc_inject_mentions: Add new configuration setting to choose between registered nicknames or online participants
Seve Ferrer <seve@delape.net>
parents: 4163
diff changeset
18 local function get_participants(room)
a82b0745383b mod_muc_inject_mentions: Add new configuration setting to choose between registered nicknames or online participants
Seve Ferrer <seve@delape.net>
parents: 4163
diff changeset
19 if not reserved_nicks then
a82b0745383b mod_muc_inject_mentions: Add new configuration setting to choose between registered nicknames or online participants
Seve Ferrer <seve@delape.net>
parents: 4163
diff changeset
20 local occupants = room._occupants
a82b0745383b mod_muc_inject_mentions: Add new configuration setting to choose between registered nicknames or online participants
Seve Ferrer <seve@delape.net>
parents: 4163
diff changeset
21 local key, occupant = next(occupants)
a82b0745383b mod_muc_inject_mentions: Add new configuration setting to choose between registered nicknames or online participants
Seve Ferrer <seve@delape.net>
parents: 4163
diff changeset
22 return function ()
a82b0745383b mod_muc_inject_mentions: Add new configuration setting to choose between registered nicknames or online participants
Seve Ferrer <seve@delape.net>
parents: 4163
diff changeset
23 while occupant do -- luacheck: ignore
a82b0745383b mod_muc_inject_mentions: Add new configuration setting to choose between registered nicknames or online participants
Seve Ferrer <seve@delape.net>
parents: 4163
diff changeset
24 local nick = jid_resource(occupant.nick);
a82b0745383b mod_muc_inject_mentions: Add new configuration setting to choose between registered nicknames or online participants
Seve Ferrer <seve@delape.net>
parents: 4163
diff changeset
25 local bare_jid = occupant.bare_jid
a82b0745383b mod_muc_inject_mentions: Add new configuration setting to choose between registered nicknames or online participants
Seve Ferrer <seve@delape.net>
parents: 4163
diff changeset
26 key, occupant = next(occupants, key)
a82b0745383b mod_muc_inject_mentions: Add new configuration setting to choose between registered nicknames or online participants
Seve Ferrer <seve@delape.net>
parents: 4163
diff changeset
27 return bare_jid, nick
a82b0745383b mod_muc_inject_mentions: Add new configuration setting to choose between registered nicknames or online participants
Seve Ferrer <seve@delape.net>
parents: 4163
diff changeset
28 end
a82b0745383b mod_muc_inject_mentions: Add new configuration setting to choose between registered nicknames or online participants
Seve Ferrer <seve@delape.net>
parents: 4163
diff changeset
29 end
a82b0745383b mod_muc_inject_mentions: Add new configuration setting to choose between registered nicknames or online participants
Seve Ferrer <seve@delape.net>
parents: 4163
diff changeset
30 else
a82b0745383b mod_muc_inject_mentions: Add new configuration setting to choose between registered nicknames or online participants
Seve Ferrer <seve@delape.net>
parents: 4163
diff changeset
31 local generator = room:each_affiliation()
a82b0745383b mod_muc_inject_mentions: Add new configuration setting to choose between registered nicknames or online participants
Seve Ferrer <seve@delape.net>
parents: 4163
diff changeset
32 local jid, _, affiliation_data = generator(nil, nil)
a82b0745383b mod_muc_inject_mentions: Add new configuration setting to choose between registered nicknames or online participants
Seve Ferrer <seve@delape.net>
parents: 4163
diff changeset
33 return function ()
a82b0745383b mod_muc_inject_mentions: Add new configuration setting to choose between registered nicknames or online participants
Seve Ferrer <seve@delape.net>
parents: 4163
diff changeset
34 while jid do
a82b0745383b mod_muc_inject_mentions: Add new configuration setting to choose between registered nicknames or online participants
Seve Ferrer <seve@delape.net>
parents: 4163
diff changeset
35 local bare_jid, nick = jid, (affiliation_data or {})["reserved_nickname"]
a82b0745383b mod_muc_inject_mentions: Add new configuration setting to choose between registered nicknames or online participants
Seve Ferrer <seve@delape.net>
parents: 4163
diff changeset
36 jid, _, affiliation_data = generator(nil, bare_jid)
a82b0745383b mod_muc_inject_mentions: Add new configuration setting to choose between registered nicknames or online participants
Seve Ferrer <seve@delape.net>
parents: 4163
diff changeset
37 if nick then
a82b0745383b mod_muc_inject_mentions: Add new configuration setting to choose between registered nicknames or online participants
Seve Ferrer <seve@delape.net>
parents: 4163
diff changeset
38 return bare_jid, nick
a82b0745383b mod_muc_inject_mentions: Add new configuration setting to choose between registered nicknames or online participants
Seve Ferrer <seve@delape.net>
parents: 4163
diff changeset
39 end
a82b0745383b mod_muc_inject_mentions: Add new configuration setting to choose between registered nicknames or online participants
Seve Ferrer <seve@delape.net>
parents: 4163
diff changeset
40 end
a82b0745383b mod_muc_inject_mentions: Add new configuration setting to choose between registered nicknames or online participants
Seve Ferrer <seve@delape.net>
parents: 4163
diff changeset
41 end
a82b0745383b mod_muc_inject_mentions: Add new configuration setting to choose between registered nicknames or online participants
Seve Ferrer <seve@delape.net>
parents: 4163
diff changeset
42 end
a82b0745383b mod_muc_inject_mentions: Add new configuration setting to choose between registered nicknames or online participants
Seve Ferrer <seve@delape.net>
parents: 4163
diff changeset
43 end
a82b0745383b mod_muc_inject_mentions: Add new configuration setting to choose between registered nicknames or online participants
Seve Ferrer <seve@delape.net>
parents: 4163
diff changeset
44
4163
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4162
diff changeset
45 local function add_mention(mentions, bare_jid, first, last, prefix_indices, has_prefix)
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4162
diff changeset
46 if strip_out_prefixes then
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4162
diff changeset
47 if has_prefix then
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4162
diff changeset
48 table.insert(prefix_indices, first-1)
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4162
diff changeset
49 end
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4162
diff changeset
50 first = first - #prefix_indices
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4162
diff changeset
51 last = last - #prefix_indices
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4162
diff changeset
52 end
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4162
diff changeset
53 mentions[first] = {bare_jid=bare_jid, first=first, last=last}
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4162
diff changeset
54 end
4161
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4159
diff changeset
55
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4159
diff changeset
56 local function get_client_mentions(stanza)
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4159
diff changeset
57 local has_mentions = false
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4159
diff changeset
58 local client_mentions = {}
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4159
diff changeset
59
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4159
diff changeset
60 for element in stanza:childtags("reference", reference_xmlns) do
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4159
diff changeset
61 if element.attr.type == "mention" then
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4159
diff changeset
62 local key = tonumber(element.attr.begin) + 1 -- count starts at 0
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4159
diff changeset
63 client_mentions[key] = {bare_jid=element.attr.uri, first=element.attr.begin, last=element.attr["end"]}
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4159
diff changeset
64 has_mentions = true
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4159
diff changeset
65 end
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4159
diff changeset
66 end
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4159
diff changeset
67
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4159
diff changeset
68 return has_mentions, client_mentions
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4159
diff changeset
69 end
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4159
diff changeset
70
4138
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
71 local function is_room_eligible(jid)
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
72 if not enabled_rooms and not disabled_rooms then
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
73 return true;
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
74 end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
75
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
76 if enabled_rooms and not disabled_rooms then
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
77 for _, _jid in ipairs(enabled_rooms) do
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
78 if _jid == jid then
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
79 return true
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
80 end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
81 end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
82 return false
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
83 end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
84
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
85 if disabled_rooms and not enabled_rooms then
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
86 for _, _jid in ipairs(disabled_rooms) do
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
87 if _jid == jid then
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
88 return false
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
89 end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
90 end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
91 return true
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
92 end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
93
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
94 return true
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
95 end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
96
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
97 local function has_nick_prefix(body, first)
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
98 -- There are no configured prefixes
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
99 if not prefixes or #prefixes < 1 then return false end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
100
4163
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4162
diff changeset
101 -- Prefix must have a space before it,
4142
6906562af2ee mod_muc_inject_mentions: Allow preffixes to be used after a new line
Seve Ferrer <seve@delape.net>
parents: 4141
diff changeset
102 -- be the first character of the body
6906562af2ee mod_muc_inject_mentions: Allow preffixes to be used after a new line
Seve Ferrer <seve@delape.net>
parents: 4141
diff changeset
103 -- or be the first character after a new line
4159
94e3e7753220 mod_muc_inject_mentions: Improve mentions lookup by using a set instead of a list
Seve Ferrer <seve@delape.net>
parents: 4155
diff changeset
104 if not mention_delimiters:contains(body:sub(first - 2, first - 2)) then
4138
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
105 return false
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
106 end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
107
4163
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4162
diff changeset
108 local prefix = body:sub(first - 1, first - 1)
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4162
diff changeset
109 for _, _prefix in ipairs(prefixes) do
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4162
diff changeset
110 if prefix == _prefix then
4138
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
111 return true
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
112 end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
113 end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
114
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
115 return false
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
116 end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
117
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
118 local function has_nick_suffix(body, last)
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
119 -- There are no configured suffixes
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
120 if not suffixes or #suffixes < 1 then return false end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
121
4143
b2080f76e0aa mod_muc_inject_mentions: Allow suffixes to be used before a new line
Seve Ferrer <seve@delape.net>
parents: 4142
diff changeset
122 -- Suffix must have a space after it,
b2080f76e0aa mod_muc_inject_mentions: Allow suffixes to be used before a new line
Seve Ferrer <seve@delape.net>
parents: 4142
diff changeset
123 -- be the last character of the body
b2080f76e0aa mod_muc_inject_mentions: Allow suffixes to be used before a new line
Seve Ferrer <seve@delape.net>
parents: 4142
diff changeset
124 -- or be the last character before a new line
4159
94e3e7753220 mod_muc_inject_mentions: Improve mentions lookup by using a set instead of a list
Seve Ferrer <seve@delape.net>
parents: 4155
diff changeset
125 if not mention_delimiters:contains(body:sub(last + 2, last + 2)) then
4138
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
126 return false
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
127 end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
128
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
129 local suffix = body:sub(last+1, last+1)
4139
c6bb64a12f92 mod_muc_inject_mentions: (W213) unused loop variable i
Seve Ferrer <seve@delape.net>
parents: 4138
diff changeset
130 for _, _suffix in ipairs(suffixes) do
4138
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
131 if suffix == _suffix then
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
132 return true
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
133 end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
134 end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
135
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
136 return false
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
137 end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
138
4161
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4159
diff changeset
139 local function search_mentions(room, body, client_mentions)
4163
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4162
diff changeset
140 local mentions, prefix_indices = {}, {}
4138
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
141
4164
a82b0745383b mod_muc_inject_mentions: Add new configuration setting to choose between registered nicknames or online participants
Seve Ferrer <seve@delape.net>
parents: 4163
diff changeset
142 for bare_jid, nick in get_participants(room) do
4138
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
143 -- Check for multiple mentions to the same nickname in a message
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
144 -- Hey @nick remember to... Ah, also @nick please let me know if...
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
145 local matches = {}
4141
aef6f5b9f6fd mod_muc_inject_mentions: (W311) value assigned to variable _first is overwritten on line 104 before use
Seve Ferrer <seve@delape.net>
parents: 4140
diff changeset
146 local _first
aef6f5b9f6fd mod_muc_inject_mentions: (W311) value assigned to variable _first is overwritten on line 104 before use
Seve Ferrer <seve@delape.net>
parents: 4140
diff changeset
147 local _last = 0
4138
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
148 while true do
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
149 -- Use plain search as nick could contain
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
150 -- characters used in Lua patterns
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
151 _first, _last = body:find(nick, _last + 1, true)
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
152 if _first == nil then break end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
153 table.insert(matches, {first=_first, last=_last})
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
154 end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
155
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
156 -- Filter out intentional mentions from unintentional ones
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
157 for _, match in ipairs(matches) do
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
158 local first, last = match.first, match.last
4161
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4159
diff changeset
159 -- Only append new mentions in case the client already sent some
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4159
diff changeset
160 if not client_mentions[first] then
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4159
diff changeset
161 -- Body only contains nickname or is between spaces, new lines or at the end/start of the body
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4159
diff changeset
162 if mention_delimiters:contains(body:sub(first - 1, first - 1)) and
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4159
diff changeset
163 mention_delimiters:contains(body:sub(last + 1, last + 1))
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4159
diff changeset
164 then
4163
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4162
diff changeset
165 add_mention(mentions, bare_jid, first, last, prefix_indices, false)
4161
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4159
diff changeset
166 else
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4159
diff changeset
167 -- Check if occupant is mentioned using affixes
4163
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4162
diff changeset
168 local has_prefix = has_nick_prefix(body, first)
4161
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4159
diff changeset
169 local has_suffix = has_nick_suffix(body, last)
4138
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
170
4161
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4159
diff changeset
171 -- @nickname: ...
4163
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4162
diff changeset
172 if has_prefix and has_suffix then
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4162
diff changeset
173 add_mention(mentions, bare_jid, first, last, prefix_indices, has_prefix)
4138
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
174
4161
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4159
diff changeset
175 -- @nickname ...
4163
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4162
diff changeset
176 elseif has_prefix and not has_suffix then
4161
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4159
diff changeset
177 if mention_delimiters:contains(body:sub(last + 1, last + 1)) then
4163
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4162
diff changeset
178 add_mention(mentions, bare_jid, first, last, prefix_indices, has_prefix)
4161
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4159
diff changeset
179 end
4138
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
180
4161
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4159
diff changeset
181 -- nickname: ...
4163
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4162
diff changeset
182 elseif not has_prefix and has_suffix then
4161
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4159
diff changeset
183 if mention_delimiters:contains(body:sub(first - 1, first - 1)) then
4163
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4162
diff changeset
184 add_mention(mentions, bare_jid, first, last, prefix_indices, has_prefix)
4161
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4159
diff changeset
185 end
4138
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
186 end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
187 end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
188 end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
189 end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
190 end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
191
4163
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4162
diff changeset
192 return mentions, prefix_indices
4138
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
193 end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
194
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
195 local function muc_inject_mentions(event)
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
196 local room, stanza = event.room, event.stanza;
4161
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4159
diff changeset
197 local body = stanza:get_child("body")
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4159
diff changeset
198
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4159
diff changeset
199 if not body then return; end
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4159
diff changeset
200
4138
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
201 -- Inject mentions only if the room is configured for them
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
202 if not is_room_eligible(room.jid) then return; end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
203
4161
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4159
diff changeset
204 -- Only act on messages that do not include mentions
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4159
diff changeset
205 -- unless configuration states otherwise.
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4159
diff changeset
206 local has_mentions, client_mentions = get_client_mentions(stanza)
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4159
diff changeset
207 if has_mentions and not append_mentions then return; end
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4159
diff changeset
208
4163
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4162
diff changeset
209 local body_text = body:get_text()
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4162
diff changeset
210 local mentions, prefix_indices = search_mentions(room, body_text, client_mentions)
4161
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4159
diff changeset
211 for _, mention in pairs(mentions) do
4138
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
212 -- https://xmpp.org/extensions/xep-0372.html#usecase_mention
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
213 stanza:tag(
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
214 "reference", {
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
215 xmlns=reference_xmlns,
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
216 begin=tostring(mention.first - 1), -- count starts at 0
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
217 ["end"]=tostring(mention.last - 1),
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
218 type="mention",
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
219 uri="xmpp:" .. mention.bare_jid,
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
220 }
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
221 ):up()
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
222 end
4163
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4162
diff changeset
223
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4162
diff changeset
224 if strip_out_prefixes then
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4162
diff changeset
225 local body_without_prefixes = ""
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4162
diff changeset
226 local from = 0
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4162
diff changeset
227 if #prefix_indices > 0 then
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4162
diff changeset
228 for _, prefix_index in ipairs(prefix_indices) do
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4162
diff changeset
229 body_without_prefixes = body_without_prefixes .. body_text:sub(from, prefix_index-1)
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4162
diff changeset
230 from = prefix_index + 1
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4162
diff changeset
231 end
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4162
diff changeset
232 body_without_prefixes = body_without_prefixes .. body_text:sub(from, #body_text)
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4162
diff changeset
233
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4162
diff changeset
234 -- Replace original body containing prefixes
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4162
diff changeset
235 stanza:maptags(
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4162
diff changeset
236 function(tag)
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4162
diff changeset
237 if tag.name ~= "body" then
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4162
diff changeset
238 return tag
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4162
diff changeset
239 end
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4162
diff changeset
240 return st.stanza("body"):text(body_without_prefixes)
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4162
diff changeset
241 end
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4162
diff changeset
242 )
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4162
diff changeset
243 end
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4162
diff changeset
244 end
4138
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
245 end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
246
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
247 module:hook("muc-occupant-groupchat", muc_inject_mentions)