Software /
code /
prosody-modules
Annotate
mod_muc_inject_mentions/mod_muc_inject_mentions.lua @ 4138:e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
author | Seve Ferrer <seve@delape.net> |
---|---|
date | Sun, 20 Sep 2020 10:31:02 +0200 |
child | 4139:c6bb64a12f92 |
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 |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
3 local jid_split = require "util.jid".split; |
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) |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
9 |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
10 local reference_xmlns = "urn:xmpp:reference:0" |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
11 |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
12 local function is_room_eligible(jid) |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
13 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
|
14 return true; |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
15 end |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
16 |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
17 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
|
18 for _, _jid in ipairs(enabled_rooms) do |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
19 if _jid == jid then |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
20 return true |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
21 end |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
22 end |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
23 return false |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
24 end |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
25 |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
26 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
|
27 for _, _jid in ipairs(disabled_rooms) do |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
28 if _jid == jid then |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
29 return false |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
30 end |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
31 end |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
32 return true |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
33 end |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
34 |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
35 return true |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
36 end |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
37 |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
38 local function has_nick_prefix(body, first) |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
39 -- There is no prefix |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
40 -- but mention could still be valid |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
41 if first == 1 then return true end |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
42 |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
43 -- There are no configured prefixes |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
44 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
|
45 |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
46 -- Preffix must have a space before it |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
47 -- or be the first character of the body |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
48 if body:sub(first - 2, first - 2) ~= "" and |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
49 body:sub(first - 2, first - 2) ~= " " |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
50 then |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
51 return false |
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 local preffix = body:sub(first - 1, first - 1) |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
55 for i, _preffix in ipairs(prefixes) do |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
56 if preffix == _preffix then |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
57 return true |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
58 end |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
59 end |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
60 |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
61 return false |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
62 end |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
63 |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
64 local function has_nick_suffix(body, last) |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
65 -- There is no suffix |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
66 -- but mention could still be valid |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
67 if last == #body then return true end |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
68 |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
69 -- There are no configured suffixes |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
70 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
|
71 |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
72 -- Suffix must have a space after it |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
73 -- or be the last character of the body |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
74 if body:sub(last + 2, last + 2) ~= "" and |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
75 body:sub(last + 2, last + 2) ~= " " |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
76 then |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
77 return false |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
78 end |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
79 |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
80 local suffix = body:sub(last+1, last+1) |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
81 for i, _suffix in ipairs(suffixes) do |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
82 if suffix == _suffix then |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
83 return true |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
84 end |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
85 end |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
86 |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
87 return false |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
88 end |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
89 |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
90 local function search_mentions(room, stanza) |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
91 local body = stanza:get_child("body"):get_text(); |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
92 local mentions = {} |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
93 |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
94 for _, occupant in pairs(room._occupants) do |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
95 local node, host, nick = jid_split(occupant.nick); |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
96 -- 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
|
97 -- 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
|
98 local matches = {} |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
99 local _first, _last = 0, 0 |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
100 while true do |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
101 -- Use plain search as nick could contain |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
102 -- characters used in Lua patterns |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
103 _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
|
104 if _first == nil then break end |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
105 table.insert(matches, {first=_first, last=_last}) |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
106 end |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
107 |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
108 -- Filter out intentional mentions from unintentional ones |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
109 for _, match in ipairs(matches) do |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
110 local bare_jid = occupant.bare_jid |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
111 local first, last = match.first, match.last |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
112 |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
113 -- Body only contains nickname |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
114 if first == 1 and last == #body then |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
115 table.insert(mentions, {bare_jid=bare_jid, first=first, last=last}) |
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 -- Nickname between spaces |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
118 elseif body:sub(first - 1, first - 1) == " " and |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
119 body:sub(last + 1, last + 1) == " " |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
120 then |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
121 table.insert(mentions, {bare_jid=bare_jid, first=first, last=last}) |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
122 else |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
123 -- Check if occupant is mentioned using affixes |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
124 local has_preffix = has_nick_prefix(body, first) |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
125 local has_suffix = has_nick_suffix(body, last) |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
126 |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
127 -- @nickname: ... |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
128 if has_preffix and has_suffix then |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
129 table.insert(mentions, {bare_jid=bare_jid, first=first, last=last}) |
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 -- @nickname ... |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
132 elseif has_preffix and not has_suffix then |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
133 if body:sub(last + 1, last + 1) == " " then |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
134 table.insert(mentions, {bare_jid=bare_jid, first=first, last=last}) |
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 |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
137 -- nickname: ... |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
138 elseif not has_preffix and has_suffix then |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
139 if body:sub(first - 1, first - 1) == " " then |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
140 table.insert(mentions, {bare_jid=bare_jid, first=first, last=last}) |
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 end |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
143 end |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
144 end |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
145 end |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
146 |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
147 return mentions |
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 |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
150 local function muc_inject_mentions(event) |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
151 local room, stanza = event.room, event.stanza; |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
152 -- 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
|
153 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
|
154 -- Only act on messages that do not include references. |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
155 -- If references are found, it is assumed the client has mentions support |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
156 if stanza:get_child("reference", reference_xmlns) then return; end |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
157 |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
158 local mentions = search_mentions(room, stanza) |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
159 for _, mention in ipairs(mentions) do |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
160 -- 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
|
161 stanza:tag( |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
162 "reference", { |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
163 xmlns=reference_xmlns, |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
164 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
|
165 ["end"]=tostring(mention.last - 1), |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
166 type="mention", |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
167 uri="xmpp:" .. mention.bare_jid, |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
168 } |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
169 ):up() |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
170 end |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
171 end |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
172 |
e8c1b35bc25b
mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
173 module:hook("muc-occupant-groupchat", muc_inject_mentions) |