-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod_muc_inject_mentions.lua
281 lines (246 loc) · 9.59 KB
/
mod_muc_inject_mentions.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
module:depends("muc");
local jid_resource = require "util.jid".resource;
local st = require "util.stanza";
local prefixes = module:get_option_set("muc_inject_mentions_prefixes", {})
local suffixes = module:get_option_set("muc_inject_mentions_suffixes", {})
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 mention_delimiters = module:get_option_set("muc_inject_mentions_mention_delimiters", {" ", "", "\n", "\t"})
local append_mentions = module:get_option("muc_inject_mentions_append_mentions", false)
local strip_out_prefixes = module:get_option("muc_inject_mentions_strip_out_prefixes", false)
local reserved_nicks = module:get_option("muc_inject_mentions_reserved_nicks", false)
local use_bare_jid = module:get_option("muc_inject_mentions_use_bare_jid", true)
local prefix_mandatory = module:get_option("muc_inject_mentions_prefix_mandatory", false)
local reserved_nicknames = {}
local reference_xmlns = "urn:xmpp:reference:0"
local function update_reserved_nicknames(event)
local room, data, jid = event.room.jid, event.data, event.jid
load_room_reserved_nicknames(event.room)
local nickname = (data or {})["reserved_nickname"]
if nickname then
reserved_nicknames[room][nickname] = jid
else
local nickname_to_remove
for _nickname, _jid in pairs(reserved_nicknames[room]) do
if _jid == jid then
nickname_to_remove = _nickname
break
end
end
if nickname_to_remove then
reserved_nicknames[room][nickname_to_remove] = nil
end
end
end
function load_room_reserved_nicknames(room)
if not reserved_nicknames[room.jid] then
reserved_nicknames[room.jid] = {}
for jid, data in pairs(room._affiliations_data or {}) do
local reserved_nickname = data["reserved_nickname"]
if reserved_nicknames then
reserved_nicknames[room.jid][reserved_nickname] = jid
end
end
end
end
local function get_jid(room, nickname)
local bare_jid = reserved_nicknames[room.jid][nickname]
if bare_jid and use_bare_jid then
return bare_jid
end
if bare_jid and not use_bare_jid then
return room.jid .. "/" .. nickname
end
end
local function get_participants(room)
if not reserved_nicks then
local occupants = room._occupants
local key, occupant = next(occupants)
return function ()
while occupant do -- luacheck: ignore
local nick = jid_resource(occupant.nick);
local bare_jid = occupant.bare_jid
key, occupant = next(occupants, key)
return bare_jid, nick
end
end
else
local generator = room:each_affiliation()
local jid, _, affiliation_data = generator(nil, nil)
return function ()
while jid do
local bare_jid, nick = jid, (affiliation_data or {})["reserved_nickname"]
jid, _, affiliation_data = generator(nil, bare_jid)
if nick then
return bare_jid, nick
end
end
end
end
end
local function add_mention(mentions, bare_jid, first, last, prefix_indices, has_prefix)
if strip_out_prefixes then
if has_prefix then
table.insert(prefix_indices, first-1)
end
first = first - #prefix_indices
last = last - #prefix_indices
end
mentions[first] = {bare_jid=bare_jid, first=first, last=last}
end
local function get_client_mentions(stanza)
local has_mentions = false
local client_mentions = {}
for element in stanza:childtags("reference", reference_xmlns) do
if element.attr.type == "mention" then
local key = tonumber(element.attr.begin) + 1 -- count starts at 0
client_mentions[key] = {bare_jid=element.attr.uri, first=element.attr.begin, last=element.attr["end"]}
has_mentions = true
end
end
return has_mentions, client_mentions
end
local function is_room_eligible(jid)
if not enabled_rooms and not disabled_rooms then return true; end
if enabled_rooms then
for _, _jid in ipairs(enabled_rooms) do
if _jid == jid then
return true
end
end
return false
end
if disabled_rooms then
for _, _jid in ipairs(disabled_rooms) do
if _jid == jid then
return false
end
end
return true
end
end
local function has_nick_prefix(body, first)
-- There are no configured prefixes
if not prefixes or #prefixes < 1 then return false end
-- Prefix must have a space before it,
-- be the first character of the body
-- or be the first character after a new line
if not mention_delimiters:contains(body:sub(first - 2, first - 2)) then
return false
end
local prefix = body:sub(first - 1, first - 1)
for _, _prefix in ipairs(prefixes) do
if prefix == _prefix then
return true
end
end
return false
end
local function has_nick_suffix(body, last)
-- There are no configured suffixes
if not suffixes or #suffixes < 1 then return false end
-- 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 mention_delimiters:contains(body:sub(last + 2, last + 2)) then
return false
end
local suffix = body:sub(last+1, last+1)
for _, _suffix in ipairs(suffixes) do
if suffix == _suffix then
return true
end
end
return false
end
local function search_mentions(room, body, client_mentions)
load_room_reserved_nicknames(room)
local mentions, prefix_indices = {}, {}
local current_word = ""
local current_word_start
for i = 1, #body+1 do
local char = body:sub(i,i)
-- Mention delimiter found, current_word is completed now
if mention_delimiters:contains(char) and current_word_start then
-- Check for nickname without prefix
local jid = get_jid(room, current_word)
if jid then
if not prefix_mandatory then
add_mention(mentions, jid, current_word_start, i - 1, prefix_indices, false)
end
else
-- Check for nickname with affixes
local prefix = prefixes:contains(current_word:sub(1,1))
local suffix = suffixes:contains(current_word:sub(-1))
if prefix and suffix then
jid = get_jid(room, current_word:sub(2, -2))
if jid then
add_mention(mentions, jid, current_word_start + 1, i - 2, prefix_indices, true)
end
elseif prefix then
jid = get_jid(room, current_word:sub(2))
if jid then
add_mention(mentions, jid, current_word_start + 1, i - 1, prefix_indices, true)
end
elseif suffix and not prefix_mandatory then
jid = get_jid(room, current_word:sub(1, -2))
if jid then
add_mention(mentions, jid, current_word_start, i - 2, prefix_indices, false)
end
end
end
current_word = ""
current_word_start = nil
elseif not mention_delimiters:contains(char) then
current_word_start = current_word_start or i
current_word = current_word .. char
end
end
return mentions, prefix_indices
end
local function muc_inject_mentions(event)
local room, stanza = event.room, event.stanza;
local body = stanza:get_child_text("body")
if not body or #body < 1 then return; end
-- Inject mentions only if the room is configured for them
if not is_room_eligible(room.jid) then return; end
-- Only act on messages that do not include mentions
-- unless configuration states otherwise.
local has_mentions, client_mentions = get_client_mentions(stanza)
if has_mentions and not append_mentions then return; end
local mentions, prefix_indices = search_mentions(room, body, client_mentions)
for _, mention in pairs(mentions) do
-- https://xmpp.org/extensions/xep-0372.html#usecase_mention
stanza:tag(
"reference", {
xmlns=reference_xmlns,
begin=tostring(mention.first - 1), -- count starts at 0
["end"]=tostring(mention.last),
type="mention",
uri="xmpp:" .. mention.bare_jid,
}
):up()
end
if strip_out_prefixes then
local body_without_prefixes = ""
local from = 0
if #prefix_indices > 0 then
for _, prefix_index in ipairs(prefix_indices) do
body_without_prefixes = body_without_prefixes .. body:sub(from, prefix_index-1)
from = prefix_index + 1
end
body_without_prefixes = body_without_prefixes .. body:sub(from, #body)
-- Replace original body containing prefixes
stanza:maptags(
function(tag)
if tag.name ~= "body" then
return tag
end
return st.stanza("body"):text(body_without_prefixes)
end
)
end
end
end
module:hook("muc-occupant-groupchat", muc_inject_mentions)
module:hook("muc-set-affiliation", update_reserved_nicknames)