annotate mod_muc_inject_mentions/mod_muc_inject_mentions.lua @ 4281:3c80e46e26f2

mod_muc_rai: Use log systems string formatting facilities Weird things happen if you put %s in your JID here.
author Kim Alvefur <zash@zash.se>
date Tue, 01 Dec 2020 22:12:16 +0100
parents 32b4901a9d8d
children a6c253bc63a5
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
4243
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
6 local prefixes = module:get_option_set("muc_inject_mentions_prefixes", {})
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
7 local suffixes = module:get_option_set("muc_inject_mentions_suffixes", {})
4138
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)
4243
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
10 local mention_delimiters = module:get_option_set("muc_inject_mentions_mention_delimiters", {" ", "", "\n", "\t"})
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)
4253
32b4901a9d8d mod_muc_inject_mentions: Add new setting to trigger mentions only if a prefix is found
Seve Ferrer <seve@delape.net>
parents: 4243
diff changeset
14 local use_bare_jid = module:get_option("muc_inject_mentions_use_bare_jid", true)
32b4901a9d8d mod_muc_inject_mentions: Add new setting to trigger mentions only if a prefix is found
Seve Ferrer <seve@delape.net>
parents: 4243
diff changeset
15 local prefix_mandatory = module:get_option("muc_inject_mentions_prefix_mandatory", false)
4243
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
16 local reserved_nicknames = {}
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
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
18 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
19
4243
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
20 local function update_reserved_nicknames(event)
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
21 local room, data, jid = event.room.jid, event.data, event.jid
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
22 load_room_reserved_nicknames(event.room)
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
23 local nickname = (data or {})["reserved_nickname"]
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
24
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
25 if nickname then
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
26 reserved_nicknames[room][nickname] = jid
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
27 else
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
28 local nickname_to_remove
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
29 for _nickname, _jid in pairs(reserved_nicknames[room]) do
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
30 if _jid == jid then
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
31 nickname_to_remove = _nickname
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
32 break
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
33 end
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
34 end
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
35 if nickname_to_remove then
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
36 reserved_nicknames[room][nickname_to_remove] = nil
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
37 end
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
38 end
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
39 end
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
40
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
41 function load_room_reserved_nicknames(room)
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
42 if not reserved_nicknames[room.jid] then
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
43 reserved_nicknames[room.jid] = {}
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
44 for jid, data in pairs(room._affiliations_data or {}) do
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
45 local reserved_nickname = data["reserved_nickname"]
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
46 if reserved_nicknames then
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
47 reserved_nicknames[room.jid][reserved_nickname] = jid
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
48 end
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
49 end
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
50 end
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
51 end
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
52
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
53 local function get_jid(room, nickname)
4253
32b4901a9d8d mod_muc_inject_mentions: Add new setting to trigger mentions only if a prefix is found
Seve Ferrer <seve@delape.net>
parents: 4243
diff changeset
54 local bare_jid = reserved_nicknames[room.jid][nickname]
32b4901a9d8d mod_muc_inject_mentions: Add new setting to trigger mentions only if a prefix is found
Seve Ferrer <seve@delape.net>
parents: 4243
diff changeset
55 if bare_jid and use_bare_jid then
32b4901a9d8d mod_muc_inject_mentions: Add new setting to trigger mentions only if a prefix is found
Seve Ferrer <seve@delape.net>
parents: 4243
diff changeset
56 return bare_jid
4243
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
57 end
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
58
4253
32b4901a9d8d mod_muc_inject_mentions: Add new setting to trigger mentions only if a prefix is found
Seve Ferrer <seve@delape.net>
parents: 4243
diff changeset
59 if bare_jid and not use_bare_jid then
4243
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
60 return room.jid .. "/" .. nickname
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
61 end
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
62 end
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
63
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
64 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
65 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
66 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
67 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
68 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
69 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
70 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
71 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
72 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
73 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
74 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
75 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
76 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
77 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
78 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
79 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
80 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
81 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
82 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
83 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
84 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
85 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
86 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
87 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
88 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
89 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
90
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
91 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
92 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
93 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
94 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
95 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
96 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
97 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
98 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
99 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
100 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
101
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
102 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
103 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
104 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
105
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
106 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
107 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
108 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
109 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
110 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
111 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
112 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
113
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
114 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
115 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
116
4138
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
117 local function is_room_eligible(jid)
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
118 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
119 return true;
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
120 end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
121
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
122 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
123 for _, _jid in ipairs(enabled_rooms) do
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
124 if _jid == jid then
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
125 return true
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
126 end
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 return false
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
129 end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
130
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
131 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
132 for _, _jid in ipairs(disabled_rooms) do
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
133 if _jid == jid then
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
134 return false
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
135 end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
136 end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
137 return true
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
138 end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
139
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
140 return true
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
141 end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
142
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
143 local function has_nick_prefix(body, first)
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
144 -- There are no configured prefixes
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
145 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
146
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
147 -- 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
148 -- 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
149 -- 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
150 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
151 return false
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
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
154 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
155 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
156 if prefix == _prefix then
4138
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
157 return true
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
158 end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
159 end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
160
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
161 return false
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
162 end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
163
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
164 local function has_nick_suffix(body, last)
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
165 -- There are no configured suffixes
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
166 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
167
4143
b2080f76e0aa mod_muc_inject_mentions: Allow suffixes to be used before a new line
Seve Ferrer <seve@delape.net>
parents: 4142
diff changeset
168 -- 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
169 -- 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
170 -- 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
171 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
172 return false
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
173 end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
174
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
175 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
176 for _, _suffix in ipairs(suffixes) do
4138
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
177 if suffix == _suffix then
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
178 return true
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
179 end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
180 end
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 return false
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
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 local function search_mentions(room, body, client_mentions)
4243
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
186 load_room_reserved_nicknames(room)
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
187 local mentions, prefix_indices = {}, {}
4243
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
188 local current_word = ""
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
189 local current_word_start
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
190 for i = 1, #body+1 do
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
191 local char = body:sub(i,i)
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
192 -- Mention delimiter found, current_word is completed now
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
193 if mention_delimiters:contains(char) and current_word_start then
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
194 -- Check for nickname without prefix
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
195 local jid = get_jid(room, current_word)
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
196 if jid then
4253
32b4901a9d8d mod_muc_inject_mentions: Add new setting to trigger mentions only if a prefix is found
Seve Ferrer <seve@delape.net>
parents: 4243
diff changeset
197 if not prefix_mandatory then
32b4901a9d8d mod_muc_inject_mentions: Add new setting to trigger mentions only if a prefix is found
Seve Ferrer <seve@delape.net>
parents: 4243
diff changeset
198 add_mention(mentions, jid, current_word_start, i - 1, prefix_indices, false)
32b4901a9d8d mod_muc_inject_mentions: Add new setting to trigger mentions only if a prefix is found
Seve Ferrer <seve@delape.net>
parents: 4243
diff changeset
199 end
4243
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
200 else
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
201 -- Check for nickname with affixes
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
202 local prefix = prefixes:contains(current_word:sub(1,1))
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
203 local suffix = suffixes:contains(current_word:sub(-1))
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
204 if prefix and suffix then
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
205 jid = get_jid(room, current_word:sub(2, -2))
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
206 if jid then
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
207 add_mention(mentions, jid, current_word_start + 1, i - 2, prefix_indices, true)
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
208 end
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
209 elseif prefix then
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
210 jid = get_jid(room, current_word:sub(2))
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
211 if jid then
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
212 add_mention(mentions, jid, current_word_start + 1, i - 1, prefix_indices, true)
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
213 end
4253
32b4901a9d8d mod_muc_inject_mentions: Add new setting to trigger mentions only if a prefix is found
Seve Ferrer <seve@delape.net>
parents: 4243
diff changeset
214 elseif suffix and not prefix_mandatory then
4243
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
215 jid = get_jid(room, current_word:sub(1, -2))
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
216 if jid then
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
217 add_mention(mentions, jid, current_word_start, i - 2, prefix_indices, false)
4138
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
218 end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
219 end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
220 end
4243
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
221
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
222 current_word = ""
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
223 current_word_start = nil
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
224 elseif not mention_delimiters:contains(char) then
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
225 current_word_start = current_word_start or i
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
226 current_word = current_word .. char
4138
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
227 end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
228 end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
229
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
230 return mentions, prefix_indices
4138
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
231 end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
232
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
233 local function muc_inject_mentions(event)
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
234 local room, stanza = event.room, event.stanza;
4243
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
235 local body = stanza:get_child_text("body")
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
236
4243
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
237 if not body or #body < 1 then return; 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
238
4138
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
239 -- 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
240 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
241
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
242 -- 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
243 -- 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
244 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
245 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
246
4243
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
247 local mentions, prefix_indices = search_mentions(room, body, 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
248 for _, mention in pairs(mentions) do
4138
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
249 -- 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
250 stanza:tag(
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
251 "reference", {
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
252 xmlns=reference_xmlns,
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
253 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
254 ["end"]=tostring(mention.last - 1),
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
255 type="mention",
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
256 uri="xmpp:" .. mention.bare_jid,
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
257 }
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
258 ):up()
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
259 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
260
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4162
diff changeset
261 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
262 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
263 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
264 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
265 for _, prefix_index in ipairs(prefix_indices) do
4243
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
266 body_without_prefixes = body_without_prefixes .. body:sub(from, prefix_index-1)
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
267 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
268 end
4243
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
269 body_without_prefixes = body_without_prefixes .. body:sub(from, #body)
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
270
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4162
diff changeset
271 -- 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
272 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
273 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
274 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
275 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
276 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
277 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
278 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
279 )
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4162
diff changeset
280 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
281 end
4138
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
282 end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
283
4243
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
284 module:hook("muc-occupant-groupchat", muc_inject_mentions)
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4164
diff changeset
285 module:hook("muc-set-affiliation", update_reserved_nicknames)