Software /
code /
prosody-modules
Annotate
mod_muc_mention_notifications/mod_muc_mention_notifications.lua @ 4307:af7297e49885
muc_mention_notifications: Rename module
No need for the additional "room" in the name.
author | JC Brand <jc@opkode.com> |
---|---|
date | Mon, 21 Dec 2020 15:43:35 +0100 |
parent | 4306:mod_muc_room_mention_notifications/mod_muc_room_mention_notifications.lua@747a14017d00 |
child | 4308:2d42dd45638a |
rev | line source |
---|---|
4280
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
1 local jid = require "util.jid"; |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
2 local st = require "util.stanza"; |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
3 local datetime = require "util.datetime"; |
4305
2ca55a4da3ea
muc_room_mention_notifications: Handle referenced JID being a MUC nickname JID
JC Brand <jc@opkode.com>
parents:
4280
diff
changeset
|
4 local jid_resource = require "util.jid".resource; |
4280
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
5 |
4307
af7297e49885
muc_mention_notifications: Rename module
JC Brand <jc@opkode.com>
parents:
4306
diff
changeset
|
6 local notify_unaffiliated_users = module:get_option("muc_mmn_notify_unaffiliated_users", false) |
4280
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
7 |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
8 local muc_affiliation_store = module:open_store("config", "map"); |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
9 |
4306
747a14017d00
muc_room_mention_notifications: Set xmlns to what's used in the protoXEP
JC Brand <jc@opkode.com>
parents:
4305
diff
changeset
|
10 local mmn_xmlns = "urn:xmpp:mmn:0"; |
4280
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
11 local reference_xmlns = "urn:xmpp:reference:0"; |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
12 local forwarded_xmlns = "urn:xmpp:forward:0"; |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
13 local deplay_xmlns = "urn:xmpp:delay"; |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
14 |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
15 |
4307
af7297e49885
muc_mention_notifications: Rename module
JC Brand <jc@opkode.com>
parents:
4306
diff
changeset
|
16 module:log("debug", "**************************************") |
af7297e49885
muc_mention_notifications: Rename module
JC Brand <jc@opkode.com>
parents:
4306
diff
changeset
|
17 module:log("debug", "**************************************") |
af7297e49885
muc_mention_notifications: Rename module
JC Brand <jc@opkode.com>
parents:
4306
diff
changeset
|
18 module:log("debug", "**************************************") |
af7297e49885
muc_mention_notifications: Rename module
JC Brand <jc@opkode.com>
parents:
4306
diff
changeset
|
19 module:log("debug", "**************************************") |
af7297e49885
muc_mention_notifications: Rename module
JC Brand <jc@opkode.com>
parents:
4306
diff
changeset
|
20 module:log("debug", "**************************************") |
af7297e49885
muc_mention_notifications: Rename module
JC Brand <jc@opkode.com>
parents:
4306
diff
changeset
|
21 module:log("debug", "**************************************") |
af7297e49885
muc_mention_notifications: Rename module
JC Brand <jc@opkode.com>
parents:
4306
diff
changeset
|
22 module:log("debug", "**************************************") |
af7297e49885
muc_mention_notifications: Rename module
JC Brand <jc@opkode.com>
parents:
4306
diff
changeset
|
23 module:log("debug", "**************************************") |
af7297e49885
muc_mention_notifications: Rename module
JC Brand <jc@opkode.com>
parents:
4306
diff
changeset
|
24 module:log("debug", "**************************************") |
af7297e49885
muc_mention_notifications: Rename module
JC Brand <jc@opkode.com>
parents:
4306
diff
changeset
|
25 module:log("debug", "**************************************") |
af7297e49885
muc_mention_notifications: Rename module
JC Brand <jc@opkode.com>
parents:
4306
diff
changeset
|
26 module:log("debug", "**************************************") |
af7297e49885
muc_mention_notifications: Rename module
JC Brand <jc@opkode.com>
parents:
4306
diff
changeset
|
27 |
4280
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
28 -- Returns a set of rooms the user is affiliated to |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
29 local function get_user_rooms(user_bare_jid) |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
30 return muc_affiliation_store:get_all(user_bare_jid); |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
31 end |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
32 |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
33 local function is_eligible(user_bare_jid, room) |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
34 if notify_unaffiliated_users then return true; end |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
35 |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
36 local user_rooms, err = get_user_rooms(user_bare_jid); |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
37 if not user_rooms then |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
38 if err then |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
39 return false, err; |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
40 end |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
41 return false; |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
42 end |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
43 |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
44 local room_node = jid.node(room.jid) |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
45 if user_rooms[room_node] then |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
46 return true; |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
47 end |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
48 |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
49 return false |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
50 end |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
51 |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
52 -- Send a single notification for a room, updating data structures as needed |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
53 local function send_single_notification(user_bare_jid, room_jid, mention_stanza) |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
54 local notification = st.message({ to = user_bare_jid, from = module.host }) |
4306
747a14017d00
muc_room_mention_notifications: Set xmlns to what's used in the protoXEP
JC Brand <jc@opkode.com>
parents:
4305
diff
changeset
|
55 :tag("mentions", { xmlns = mmn_xmlns }) |
4280
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
56 :tag("forwarded", {xmlns = forwarded_xmlns}) |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
57 :tag("delay", {xmlns = deplay_xmlns, stamp = datetime.datetime()}):up() |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
58 :add_child(mention_stanza) |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
59 :reset(); |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
60 module:log("debug", "Sending mention notification from %s to %s", room_jid, user_bare_jid); |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
61 return module:send(notification); |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
62 end |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
63 |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
64 local function notify_mentioned_users(room, client_mentions, mention_stanza) |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
65 module:log("debug", "NOTIFYING FOR %s", room.jid) |
4305
2ca55a4da3ea
muc_room_mention_notifications: Handle referenced JID being a MUC nickname JID
JC Brand <jc@opkode.com>
parents:
4280
diff
changeset
|
66 for mentioned_jid in pairs(client_mentions) do |
2ca55a4da3ea
muc_room_mention_notifications: Handle referenced JID being a MUC nickname JID
JC Brand <jc@opkode.com>
parents:
4280
diff
changeset
|
67 local user_bare_jid = mentioned_jid; |
2ca55a4da3ea
muc_room_mention_notifications: Handle referenced JID being a MUC nickname JID
JC Brand <jc@opkode.com>
parents:
4280
diff
changeset
|
68 if (string.match(mentioned_jid, room.jid)) then |
2ca55a4da3ea
muc_room_mention_notifications: Handle referenced JID being a MUC nickname JID
JC Brand <jc@opkode.com>
parents:
4280
diff
changeset
|
69 local nick = jid_resource(mentioned_jid); |
2ca55a4da3ea
muc_room_mention_notifications: Handle referenced JID being a MUC nickname JID
JC Brand <jc@opkode.com>
parents:
4280
diff
changeset
|
70 user_bare_jid = room:get_registered_jid(nick); |
2ca55a4da3ea
muc_room_mention_notifications: Handle referenced JID being a MUC nickname JID
JC Brand <jc@opkode.com>
parents:
4280
diff
changeset
|
71 end |
4280
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
72 if is_eligible(user_bare_jid, room) then |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
73 send_single_notification(user_bare_jid, room.jid, mention_stanza); |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
74 end |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
75 end |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
76 end |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
77 |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
78 local function get_mentions(stanza) |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
79 local has_mentions = false |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
80 local client_mentions = {} |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
81 |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
82 for element in stanza:childtags("reference", reference_xmlns) do |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
83 if element.attr.type == "mention" then |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
84 local user_bare_jid = element.attr.uri:match("^xmpp:(.+)$"); |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
85 if user_bare_jid then |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
86 client_mentions[user_bare_jid] = user_bare_jid; |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
87 has_mentions = true |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
88 end |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
89 end |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
90 end |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
91 |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
92 return has_mentions, client_mentions |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
93 end |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
94 |
4307
af7297e49885
muc_mention_notifications: Rename module
JC Brand <jc@opkode.com>
parents:
4306
diff
changeset
|
95 if rawget(_G, "setfenv") == nil then |
af7297e49885
muc_mention_notifications: Rename module
JC Brand <jc@opkode.com>
parents:
4306
diff
changeset
|
96 rawset(_G, "setfenv", false) |
af7297e49885
muc_mention_notifications: Rename module
JC Brand <jc@opkode.com>
parents:
4306
diff
changeset
|
97 end |
af7297e49885
muc_mention_notifications: Rename module
JC Brand <jc@opkode.com>
parents:
4306
diff
changeset
|
98 if rawget(_G, "getfenv") == nil then |
af7297e49885
muc_mention_notifications: Rename module
JC Brand <jc@opkode.com>
parents:
4306
diff
changeset
|
99 rawset(_G, "getfenv", false) |
af7297e49885
muc_mention_notifications: Rename module
JC Brand <jc@opkode.com>
parents:
4306
diff
changeset
|
100 end |
af7297e49885
muc_mention_notifications: Rename module
JC Brand <jc@opkode.com>
parents:
4306
diff
changeset
|
101 |
4280
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
102 module:hook("muc-broadcast-message", function (event) |
4307
af7297e49885
muc_mention_notifications: Rename module
JC Brand <jc@opkode.com>
parents:
4306
diff
changeset
|
103 |
af7297e49885
muc_mention_notifications: Rename module
JC Brand <jc@opkode.com>
parents:
4306
diff
changeset
|
104 require("mobdebug").start() |
4280
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
105 local room, stanza = event.room, event.stanza; |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
106 local body = stanza:get_child_text("body") |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
107 if not body or #body < 1 then return; end |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
108 local correction = stanza:get_child("replace", "urn:xmpp:message-correct:0"); |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
109 if correction then return; end -- Do not notify on message corrections |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
110 |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
111 local has_mentions, client_mentions = get_mentions(stanza) |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
112 if not has_mentions then return; end |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
113 |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
114 -- Notify any users that need to be notified |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
115 notify_mentioned_users(room, client_mentions, stanza); |
278f2998ce49
mod_muc_room_mention_notifications: Publish module
Seve Ferrer <seve@delape.net>
parents:
diff
changeset
|
116 end, -1); |