Mercurial > prosody-modules
annotate mod_muc_inject_mentions/mod_muc_inject_mentions.lua @ 4161:032e1c79d039
mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
author | Seve Ferrer <seve@delape.net> |
---|---|
date | Tue, 29 Sep 2020 17:05:14 +0200 |
parents | 94e3e7753220 |
children | f7bc0e4ab4a2 |
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; |
4138
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
4 |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
5 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
|
6 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
|
7 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
|
8 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
|
9 local mention_delimiters = module:get_option_set("muc_inject_mentions_mention_delimiters", {" ", "", "\n"}) |
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
|
10 local append_mentions = module:get_option("muc_inject_mentions_append_mentions", true) |
4138
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
11 |
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
|
12 |
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
|
13 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
|
14 |
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
|
15 |
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
|
16 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
|
17 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
|
18 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
|
19 |
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
|
20 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
|
21 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
|
22 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
|
23 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
|
24 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
|
25 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
|
26 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
|
27 |
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
|
28 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
|
29 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
|
30 |
4138
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
31 local function is_room_eligible(jid) |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
32 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
|
33 return true; |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
34 end |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
35 |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
36 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
|
37 for _, _jid in ipairs(enabled_rooms) do |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
38 if _jid == jid then |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
39 return true |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
40 end |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
41 end |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
42 return false |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
43 end |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
44 |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
45 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
|
46 for _, _jid in ipairs(disabled_rooms) do |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
47 if _jid == jid then |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
48 return false |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
49 end |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
50 end |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
51 return true |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
52 end |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
53 |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
54 return true |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
55 end |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
56 |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
57 local function has_nick_prefix(body, first) |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
58 -- There are no configured prefixes |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
59 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
|
60 |
4142
6906562af2ee
mod_muc_inject_mentions: Allow preffixes to be used after a new line
Seve Ferrer <seve@delape.net>
parents:
4141
diff
changeset
|
61 -- Preffix must have a space before it, |
6906562af2ee
mod_muc_inject_mentions: Allow preffixes to be used after a new line
Seve Ferrer <seve@delape.net>
parents:
4141
diff
changeset
|
62 -- 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
|
63 -- 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
|
64 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
|
65 return false |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
66 end |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
67 |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
68 local preffix = body:sub(first - 1, first - 1) |
4139
c6bb64a12f92
mod_muc_inject_mentions: (W213) unused loop variable i
Seve Ferrer <seve@delape.net>
parents:
4138
diff
changeset
|
69 for _, _preffix in ipairs(prefixes) do |
4138
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
70 if preffix == _preffix then |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
71 return true |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
72 end |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
73 end |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
74 |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
75 return false |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
76 end |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
77 |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
78 local function has_nick_suffix(body, last) |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
79 -- There are no configured suffixes |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
80 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
|
81 |
4143
b2080f76e0aa
mod_muc_inject_mentions: Allow suffixes to be used before a new line
Seve Ferrer <seve@delape.net>
parents:
4142
diff
changeset
|
82 -- 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
|
83 -- 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
|
84 -- 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
|
85 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
|
86 return false |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
87 end |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
88 |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
89 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
|
90 for _, _suffix in ipairs(suffixes) do |
4138
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
91 if suffix == _suffix then |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
92 return true |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
93 end |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
94 end |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
95 |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
96 return false |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
97 end |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
98 |
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
|
99 local function search_mentions(room, body, client_mentions) |
4138
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
100 local mentions = {} |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
101 |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
102 for _, occupant in pairs(room._occupants) do |
4140
fea4a4831e10
mod_muc_inject_mentions: (W211) unused variable node, (W211) unused variable host
Seve Ferrer <seve@delape.net>
parents:
4139
diff
changeset
|
103 local nick = jid_resource(occupant.nick); |
4138
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
104 -- 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
|
105 -- 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
|
106 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
|
107 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
|
108 local _last = 0 |
4138
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
109 while true do |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
110 -- Use plain search as nick could contain |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
111 -- characters used in Lua patterns |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
112 _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
|
113 if _first == nil then break end |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
114 table.insert(matches, {first=_first, last=_last}) |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
115 end |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
116 |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
117 -- Filter out intentional mentions from unintentional ones |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
118 for _, match in ipairs(matches) do |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
119 local bare_jid = occupant.bare_jid |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
120 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
|
121 -- 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
|
122 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
|
123 -- 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
|
124 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
|
125 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
|
126 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
|
127 mentions[first] = {bare_jid=bare_jid, first=first, last=last} |
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
|
128 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
|
129 -- Check if occupant is mentioned using affixes |
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
|
130 local has_preffix = has_nick_prefix(body, first) |
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
|
131 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
|
132 |
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
|
133 -- @nickname: ... |
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
|
134 if has_preffix and has_suffix 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
|
135 mentions[first] = {bare_jid=bare_jid, first=first, last=last} |
4138
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
136 |
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
|
137 -- @nickname ... |
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
|
138 elseif has_preffix and not has_suffix 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
|
139 if mention_delimiters:contains(body:sub(last + 1, last + 1)) 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
|
140 mentions[first] = {bare_jid=bare_jid, first=first, last=last} |
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
|
141 end |
4138
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
142 |
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
|
143 -- nickname: ... |
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
|
144 elseif not has_preffix and has_suffix 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
|
145 if mention_delimiters:contains(body:sub(first - 1, first - 1)) 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
|
146 mentions[first] = {bare_jid=bare_jid, first=first, last=last} |
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
|
147 end |
4138
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
148 end |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
149 end |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
150 end |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
151 end |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
152 end |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
153 |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
154 return mentions |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
155 end |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
156 |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
157 local function muc_inject_mentions(event) |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
158 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
|
159 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
|
160 |
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 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
|
162 |
4138
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
163 -- 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
|
164 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
|
165 |
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 -- 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
|
167 -- 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
|
168 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
|
169 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
|
170 |
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 local mentions = search_mentions(room, body:get_text(), 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
|
172 for _, mention in pairs(mentions) do |
4138
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
173 -- 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
|
174 stanza:tag( |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
175 "reference", { |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
176 xmlns=reference_xmlns, |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
177 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
|
178 ["end"]=tostring(mention.last - 1), |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
179 type="mention", |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
180 uri="xmpp:" .. mention.bare_jid, |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
181 } |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
182 ):up() |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
183 end |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
184 end |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
185 |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
186 module:hook("muc-occupant-groupchat", muc_inject_mentions) |