Software /
code /
prosody-modules
Annotate
mod_muc_cloud_notify/mod_muc_cloud_notify.lua @ 3882:3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
author | tmolitor <thilo@eightysoft.de> |
---|---|
date | Wed, 05 Feb 2020 23:27:33 +0100 |
parent | 3570:8a93af85f319 |
child | 3883:571249f69577 |
rev | line source |
---|---|
3319 | 1 -- XEP-XXX: MUC Push Notifications |
2 -- Copyright (C) 2015-2016 Kim Alvefur | |
3 -- Copyright (C) 2017-2018 Thilo Molitor | |
4 -- | |
5 -- This file is MIT/X11 licensed. | |
6 | |
7 local s_match = string.match; | |
8 local s_sub = string.sub; | |
9 local os_time = os.time; | |
10 local next = next; | |
11 local st = require"util.stanza"; | |
12 local jid = require"util.jid"; | |
13 local dataform = require"util.dataforms".new; | |
14 local hashes = require"util.hashes"; | |
15 | |
16 local xmlns_push = "urn:xmpp:push:0"; | |
17 | |
18 -- configuration | |
19 local include_body = module:get_option_boolean("push_notification_with_body", false); | |
20 local include_sender = module:get_option_boolean("push_notification_with_sender", false); | |
21 local max_push_errors = module:get_option_number("push_max_errors", 16); | |
22 local max_push_devices = module:get_option_number("push_max_devices", 5); | |
23 local dummy_body = module:get_option_string("push_notification_important_body", "New Message!"); | |
24 | |
25 local host_sessions = prosody.hosts[module.host].sessions; | |
26 local push_errors = {}; | |
3882
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
27 local id2room = {} |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
28 local id2user = {}; |
3319 | 29 |
30 module:depends("muc"); | |
31 | |
32 -- For keeping state across reloads while caching reads | |
33 local push_store = (function() | |
34 local store = module:open_store(); | |
35 local push_services = {}; | |
36 local api = {}; | |
3882
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
37 local function load_room(room) |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
38 if not push_services[room] then |
3319 | 39 local err; |
3882
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
40 push_services[room], err = store:get(room); |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
41 if not push_services[room] and err then |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
42 module:log("warn", "Error reading push notification storage for room '%s': %s", room, tostring(err)); |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
43 push_services[room] = {}; |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
44 return false; |
3319 | 45 end |
46 end | |
3882
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
47 return true; |
3319 | 48 end |
3882
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
49 function api:get(room, user) |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
50 load_room(room); |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
51 if not push_services[room] then push_services[room] = {}; push_services[room][user] = {}; end |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
52 return push_services[room][user], true; |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
53 end |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
54 function api:set(room, user, data) |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
55 push_services[room][user] = data; |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
56 local ok, err = store:set(room, push_services[room]); |
3319 | 57 if not ok then |
3882
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
58 module:log("error", "Error writing push notification storage for room '%s' on behalf of user '%s': %s", room, user, tostring(err)); |
3319 | 59 return false; |
60 end | |
61 return true; | |
62 end | |
3882
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
63 function api:get_room_users(room) |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
64 local users = {}; |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
65 load_room(room); |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
66 for k, v in pairs(push_services[room]) do |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
67 table.insert(users, k); |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
68 end |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
69 return users; |
3319 | 70 end |
71 return api; | |
72 end)(); | |
73 | |
74 | |
75 -- Forward declarations, as both functions need to reference each other | |
76 local handle_push_success, handle_push_error; | |
77 | |
78 function handle_push_error(event) | |
79 local stanza = event.stanza; | |
80 local error_type, condition = stanza:get_error(); | |
3882
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
81 local room = id2room[stanza.attr.id]; |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
82 local user = id2user[stanza.attr.id]; |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
83 if room == nil or user == nil then return false; end -- unknown stanza? Ignore for now! |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
84 local push_service = push_store:get(room, user); |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
85 local push_identifier = room.."<"..user..">"; |
3319 | 86 |
3882
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
87 local stanza_id = hashes.sha256(push_identifier, true); |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
88 if stanza_id == stanza.attr.id then |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
89 if push_service and push_service.push_jid == stanza.attr.from and error_type ~= "wait" then |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
90 push_errors[push_identifier] = push_errors[push_identifier] + 1; |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
91 module:log("info", "Got error of type '%s' (%s) for identifier '%s': " |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
92 .."error count for this identifier is now at %s", error_type, condition, push_identifier, |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
93 tostring(push_errors[push_identifier])); |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
94 if push_errors[push_identifier] >= max_push_errors then |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
95 module:log("warn", "Disabling push notifications for identifier '%s'", push_identifier); |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
96 -- save changed global config |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
97 push_store:set(room, user, nil); |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
98 push_errors[push_identifier] = nil; |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
99 -- unhook iq handlers for this identifier (if possible) |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
100 if module.unhook then |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
101 module:unhook("iq-error/host/"..stanza_id, handle_push_error); |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
102 module:unhook("iq-result/host/"..stanza_id, handle_push_success); |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
103 id2room[stanza_id] = nil; |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
104 id2user[stanza_id] = nil; |
3319 | 105 end |
106 end | |
3882
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
107 elseif push_service and push_service.push_jid == stanza.attr.from and error_type == "wait" then |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
108 module:log("debug", "Got error of type '%s' (%s) for identifier '%s': " |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
109 .."NOT increasing error count for this identifier", error_type, condition, push_identifier); |
3319 | 110 end |
111 end | |
112 return true; | |
113 end | |
114 | |
115 function handle_push_success(event) | |
116 local stanza = event.stanza; | |
3882
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
117 local room = id2room[stanza.attr.id]; |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
118 local user = id2user[stanza.attr.id]; |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
119 if room == nil or user == nil then return false; end -- unknown stanza? Ignore for now! |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
120 local push_service = push_store:get(room, user); |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
121 local push_identifier = room.."<"..user..">"; |
3319 | 122 |
3882
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
123 if hashes.sha256(push_identifier, true) == stanza.attr.id then |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
124 if push_service and push_service.push_jid == stanza.attr.from and push_errors[push_identifier] > 0 then |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
125 push_errors[push_identifier] = 0; |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
126 -- unhook iq handlers for this identifier (if possible) |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
127 if module.unhook then |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
128 module:unhook("iq-error/host/"..stanza.attr.id, handle_push_error); |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
129 module:unhook("iq-result/host/"..stanza.attr.id, handle_push_success); |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
130 id2room[stanza.attr.id] = nil; |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
131 id2user[stanza.attr.id] = nil; |
3319 | 132 end |
3882
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
133 module:log("debug", "Push succeeded, error count for identifier '%s' is now at %s again", push_identifier, tostring(push_errors[push_identifier])); |
3319 | 134 end |
135 end | |
136 return true; | |
137 end | |
138 | |
3882
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
139 -- http://xmpp.org/extensions/xep-xxxx.html#disco |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
140 module:hook("muc-disco#info", function(event) |
3319 | 141 (event.reply or event.stanza):tag("feature", {var=xmlns_push}):up(); |
3882
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
142 end); |
3319 | 143 |
144 -- http://xmpp.org/extensions/xep-0357.html#enabling | |
145 local function push_enable(event) | |
146 local origin, stanza = event.origin, event.stanza; | |
3882
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
147 local room = jid.split(stanza.attr.to); |
3319 | 148 local enable = stanza.tags[1]; |
149 origin.log("debug", "Attempting to enable push notifications"); | |
150 -- MUST contain a 'jid' attribute of the XMPP Push Service being enabled | |
151 local push_jid = enable.attr.jid; | |
152 if not push_jid then | |
153 origin.log("debug", "MUC Push notification enable request missing the 'jid' field"); | |
154 origin.send(st.error_reply(stanza, "modify", "bad-request", "Missing jid")); | |
155 return true; | |
156 end | |
157 local publish_options = enable:get_child("x", "jabber:x:data"); | |
158 if not publish_options then | |
159 -- Could be intentional | |
160 origin.log("debug", "No publish options in request"); | |
161 end | |
162 local push_service = { | |
3882
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
163 push_jid = push_jid; |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
164 device = stanza.attr.from; |
3319 | 165 options = publish_options and st.preserialize(publish_options); |
166 timestamp = os_time(); | |
167 }; | |
3882
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
168 |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
169 local ok = push_store:set(room, stanza.attr.from, push_service); |
3319 | 170 if not ok then |
171 origin.send(st.error_reply(stanza, "wait", "internal-server-error")); | |
172 else | |
3882
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
173 origin.log("info", "MUC Push notifications enabled for room %s by %s (%s)", |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
174 tostring(room), |
3319 | 175 tostring(stanza.attr.from), |
3882
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
176 tostring(push_jid) |
3319 | 177 ); |
178 origin.send(st.reply(stanza)); | |
179 end | |
180 return true; | |
181 end | |
182 module:hook("iq-set/host/"..xmlns_push..":enable", push_enable); | |
183 | |
184 | |
185 -- http://xmpp.org/extensions/xep-0357.html#disabling | |
186 local function push_disable(event) | |
187 local origin, stanza = event.origin, event.stanza; | |
3882
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
188 local room = jid.split(stanza.attr.to); |
3319 | 189 local push_jid = stanza.tags[1].attr.jid; -- MUST include a 'jid' attribute |
190 if not push_jid then | |
191 origin.send(st.error_reply(stanza, "modify", "bad-request", "Missing jid")); | |
192 return true; | |
193 end | |
3882
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
194 local push_identifier = room.."<"..stanza.attr.from..">"; |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
195 local push_service = push_store:get(room, stanza.attr.from); |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
196 local ok = true; |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
197 if push_service.push_jid == push_jid then |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
198 origin.log("info", "Push notifications disabled for room %s by %s (%s)", |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
199 tostring(room), |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
200 sotring(stanza.attr.from), |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
201 tostring(push_jid) |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
202 ); |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
203 ok = push_store:set(room, stanza.attr.from, nil); |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
204 push_errors[push_identifier] = nil; |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
205 if module.unhook then |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
206 local stanza_id = hashes.sha256(push_identifier, true); |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
207 module:unhook("iq-error/host/"..stanza_id, handle_push_error); |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
208 module:unhook("iq-result/host/"..stanza_id, handle_push_success); |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
209 id2room[stanza_id] = nil; |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
210 id2user[stanza_id] = nil; |
3319 | 211 end |
212 end | |
213 if not ok then | |
214 origin.send(st.error_reply(stanza, "wait", "internal-server-error")); | |
215 else | |
216 origin.send(st.reply(stanza)); | |
217 end | |
218 return true; | |
219 end | |
220 module:hook("iq-set/host/"..xmlns_push..":disable", push_disable); | |
221 | |
222 -- Patched version of util.stanza:find() that supports giving stanza names | |
223 -- without their namespace, allowing for every namespace. | |
224 local function find(self, path) | |
225 local pos = 1; | |
226 local len = #path + 1; | |
227 | |
228 repeat | |
229 local xmlns, name, text; | |
230 local char = s_sub(path, pos, pos); | |
231 if char == "@" then | |
232 return self.attr[s_sub(path, pos + 1)]; | |
233 elseif char == "{" then | |
234 xmlns, pos = s_match(path, "^([^}]+)}()", pos + 1); | |
235 end | |
236 name, text, pos = s_match(path, "^([^@/#]*)([/#]?)()", pos); | |
237 name = name ~= "" and name or nil; | |
238 if pos == len then | |
239 if text == "#" then | |
240 local child = xmlns ~= nil and self:get_child(name, xmlns) or self:child_with_name(name); | |
241 return child and child:get_text() or nil; | |
242 end | |
243 return xmlns ~= nil and self:get_child(name, xmlns) or self:child_with_name(name); | |
244 end | |
245 self = xmlns ~= nil and self:get_child(name, xmlns) or self:child_with_name(name); | |
246 until not self | |
247 return nil; | |
248 end | |
249 | |
250 -- is this push a high priority one (this is needed for ios apps not using voip pushes) | |
251 local function is_important(stanza) | |
252 local st_name = stanza and stanza.name or nil; | |
253 if not st_name then return false; end -- nonzas are never important here | |
254 if st_name == "presence" then | |
255 return false; -- same for presences | |
256 elseif st_name == "message" then | |
257 -- unpack carbon copies | |
258 local stanza_direction = "in"; | |
259 local carbon; | |
260 local st_type; | |
261 -- support carbon copied message stanzas having an arbitrary message-namespace or no message-namespace at all | |
262 if not carbon then carbon = find(stanza, "{urn:xmpp:carbons:2}/forwarded/message"); end | |
263 if not carbon then carbon = find(stanza, "{urn:xmpp:carbons:1}/forwarded/message"); end | |
264 stanza_direction = carbon and stanza:child_with_name("sent") and "out" or "in"; | |
265 if carbon then stanza = carbon; end | |
266 st_type = stanza.attr.type; | |
267 | |
268 -- headline message are always not important | |
269 if st_type == "headline" then return false; end | |
270 | |
271 -- carbon copied outgoing messages are not important | |
272 if carbon and stanza_direction == "out" then return false; end | |
273 | |
274 -- We can't check for body contents in encrypted messages, so let's treat them as important | |
275 -- Some clients don't even set a body or an empty body for encrypted messages | |
276 | |
277 -- check omemo https://xmpp.org/extensions/inbox/omemo.html | |
278 if stanza:get_child("encrypted", "eu.siacs.conversations.axolotl") or stanza:get_child("encrypted", "urn:xmpp:omemo:0") then return true; end | |
279 | |
280 -- check xep27 pgp https://xmpp.org/extensions/xep-0027.html | |
281 if stanza:get_child("x", "jabber:x:encrypted") then return true; end | |
282 | |
283 -- check xep373 pgp (OX) https://xmpp.org/extensions/xep-0373.html | |
284 if stanza:get_child("openpgp", "urn:xmpp:openpgp:0") then return true; end | |
285 | |
286 local body = stanza:get_child_text("body"); | |
287 if st_type == "groupchat" and stanza:get_child_text("subject") then return false; end -- groupchat subjects are not important here | |
288 return body ~= nil and body ~= ""; -- empty bodies are not important | |
289 end | |
290 return false; -- this stanza wasn't one of the above cases --> it is not important, too | |
291 end | |
292 | |
293 local push_form = dataform { | |
3882
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
294 { name = "FORM_TYPE"; type = "hidden"; value = "urn:xmpp:muc_push:summary"; }; |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
295 --{ name = "dummy"; type = "text-single"; }; |
3319 | 296 }; |
297 | |
298 -- http://xmpp.org/extensions/xep-0357.html#publishing | |
3882
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
299 local function handle_notify_request(stanza, user, user_push_services) |
3319 | 300 local pushes = 0; |
301 if not user_push_services or next(user_push_services) == nil then return pushes end | |
302 | |
303 for push_identifier, push_info in pairs(user_push_services) do | |
304 local send_push = true; -- only send push to this node when not already done for this stanza or if no stanza is given at all | |
305 if stanza then | |
306 if not stanza._push_notify then stanza._push_notify = {}; end | |
307 if stanza._push_notify[push_identifier] then | |
308 if log_push_decline then | |
3882
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
309 module:log("debug", "Already sent push notification for %s to %s (%s)", user, push_info.push_jid, tostring(push_info.node)); |
3319 | 310 end |
311 send_push = false; | |
312 end | |
313 stanza._push_notify[push_identifier] = true; | |
314 end | |
315 | |
316 if send_push then | |
317 -- construct push stanza | |
318 local stanza_id = hashes.sha256(push_identifier, true); | |
319 local push_publish = st.iq({ to = push_info.jid, from = module.host, type = "set", id = stanza_id }) | |
320 :tag("pubsub", { xmlns = "http://jabber.org/protocol/pubsub" }) | |
321 :tag("publish", { node = push_info.node }) | |
322 :tag("item") | |
323 :tag("notification", { xmlns = xmlns_push }); | |
324 local form_data = { | |
325 -- hardcode to 1 because other numbers are just meaningless (the XEP does not specify *what exactly* to count) | |
326 ["message-count"] = "1"; | |
327 }; | |
328 if stanza and include_sender then | |
329 form_data["last-message-sender"] = stanza.attr.from; | |
330 end | |
331 if stanza and include_body then | |
332 form_data["last-message-body"] = stanza:get_child_text("body"); | |
333 elseif stanza and dummy_body and is_important(stanza) then | |
334 form_data["last-message-body"] = tostring(dummy_body); | |
335 end | |
336 push_publish:add_child(push_form:form(form_data)); | |
337 push_publish:up(); -- / notification | |
338 push_publish:up(); -- / publish | |
339 push_publish:up(); -- / pubsub | |
340 if push_info.options then | |
341 push_publish:tag("publish-options"):add_child(st.deserialize(push_info.options)); | |
342 end | |
343 -- send out push | |
344 module:log("debug", "Sending%s push notification for %s@%s to %s (%s)", form_data["last-message-body"] and " important" or "", node, module.host, push_info.jid, tostring(push_info.node)); | |
345 -- module:log("debug", "PUSH STANZA: %s", tostring(push_publish)); | |
346 -- handle push errors for this node | |
347 if push_errors[push_identifier] == nil then | |
348 push_errors[push_identifier] = 0; | |
349 module:hook("iq-error/host/"..stanza_id, handle_push_error); | |
350 module:hook("iq-result/host/"..stanza_id, handle_push_success); | |
351 id2node[stanza_id] = node; | |
352 end | |
353 module:send(push_publish); | |
354 pushes = pushes + 1; | |
355 end | |
356 end | |
357 return pushes; | |
358 end | |
359 | |
3570
8a93af85f319
mod_muc_cloud_notify: Count codepoints instead of bytes
Matthew Wild <mwild1@gmail.com>
parents:
3319
diff
changeset
|
360 local function extract_reference(text, i, j) |
8a93af85f319
mod_muc_cloud_notify: Count codepoints instead of bytes
Matthew Wild <mwild1@gmail.com>
parents:
3319
diff
changeset
|
361 -- COMPAT w/ pre-Lua 5.3 |
8a93af85f319
mod_muc_cloud_notify: Count codepoints instead of bytes
Matthew Wild <mwild1@gmail.com>
parents:
3319
diff
changeset
|
362 local c, pos, p1 = 0, 0, nil; |
8a93af85f319
mod_muc_cloud_notify: Count codepoints instead of bytes
Matthew Wild <mwild1@gmail.com>
parents:
3319
diff
changeset
|
363 for char in text:gmatch("([%z\1-\127\194-\244][\128-\191]*)") do |
8a93af85f319
mod_muc_cloud_notify: Count codepoints instead of bytes
Matthew Wild <mwild1@gmail.com>
parents:
3319
diff
changeset
|
364 c, pos = c + 1, pos + #char; |
8a93af85f319
mod_muc_cloud_notify: Count codepoints instead of bytes
Matthew Wild <mwild1@gmail.com>
parents:
3319
diff
changeset
|
365 if not p1 and i < c then |
8a93af85f319
mod_muc_cloud_notify: Count codepoints instead of bytes
Matthew Wild <mwild1@gmail.com>
parents:
3319
diff
changeset
|
366 p1 = pos; |
8a93af85f319
mod_muc_cloud_notify: Count codepoints instead of bytes
Matthew Wild <mwild1@gmail.com>
parents:
3319
diff
changeset
|
367 end |
8a93af85f319
mod_muc_cloud_notify: Count codepoints instead of bytes
Matthew Wild <mwild1@gmail.com>
parents:
3319
diff
changeset
|
368 if c == j then |
8a93af85f319
mod_muc_cloud_notify: Count codepoints instead of bytes
Matthew Wild <mwild1@gmail.com>
parents:
3319
diff
changeset
|
369 return text:sub(p1, pos); |
8a93af85f319
mod_muc_cloud_notify: Count codepoints instead of bytes
Matthew Wild <mwild1@gmail.com>
parents:
3319
diff
changeset
|
370 end |
8a93af85f319
mod_muc_cloud_notify: Count codepoints instead of bytes
Matthew Wild <mwild1@gmail.com>
parents:
3319
diff
changeset
|
371 end |
8a93af85f319
mod_muc_cloud_notify: Count codepoints instead of bytes
Matthew Wild <mwild1@gmail.com>
parents:
3319
diff
changeset
|
372 end |
3319 | 373 |
374 -- archive message added | |
375 local function archive_message_added(event) | |
376 -- event is: { origin = origin, stanza = stanza, for_user = store_user, id = id } | |
377 local room = event.room; | |
378 local stanza = event.stanza; | |
3882
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
379 local room_name = jid.split(room.jid); |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
380 |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
381 -- extract all real ocupant jids in room |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
382 occupants = {}; |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
383 for nick, occupant in room:each_occupant() do |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
384 for jid in occupant:each_session() do |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
385 occupants[jid] = true; |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
386 end |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
387 end |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
388 |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
389 -- check all push registered users against occupants list |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
390 for _, user in pairs(push_store:get_room_users(room_name)) do |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
391 -- send push if not found in occupants list |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
392 if not occupants[user] then |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
393 local push_service = push_store:get(room_name, user); |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
394 handle_notify_request(event.stanza, user, push_service); |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
395 end |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
396 end |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
397 |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
398 |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
399 |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
400 |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
401 liste der registrierten push user eines raumes durchgehen |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
402 jeder user der NICHT im muc ist, wird gepusht |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
403 |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
404 |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
405 handle_notify_request(event.stanza, jid, user_push_services, true); |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
406 |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
407 |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
408 |
3319 | 409 for reference in stanza:childtags("reference", "urn:xmpp:reference:0") do |
410 if reference.attr['type'] == 'mention' and reference.attr['begin'] and reference.attr['end'] then | |
3570
8a93af85f319
mod_muc_cloud_notify: Count codepoints instead of bytes
Matthew Wild <mwild1@gmail.com>
parents:
3319
diff
changeset
|
411 local nick = extract_reference(body, reference.attr['begin'], reference.attr['end']); |
3319 | 412 local jid = room:get_registered_jid(nick); |
413 | |
414 if room._occupants[room.jid..'/'..nick] then | |
415 -- We only notify for members not currently in the room | |
416 module:log("debug", "Not notifying %s, because he's currently in the room", jid); | |
417 else | |
418 -- We only need to notify once, even when there are multiple mentions. | |
419 local user_push_services = push_store:get(jid); | |
420 handle_notify_request(event.stanza, jid, user_push_services, true); | |
421 return | |
422 end | |
423 end | |
424 end | |
425 end | |
426 | |
427 module:hook("muc-add-history", archive_message_added); | |
428 | |
429 local function send_ping(event) | |
3882
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
430 local push_services = event.push_services; |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
431 if not push_services then |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
432 local room = event.room; |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
433 local user = event.user; |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
434 push_services = push_store:get(room, user); |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
435 end |
3319 | 436 handle_notify_request(nil, user, push_services, true); |
437 end | |
438 -- can be used by other modules to ping one or more (or all) push endpoints | |
3882
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
439 module:hook("muc-cloud-notify-ping", send_ping); |
3319 | 440 |
441 module:log("info", "Module loaded"); | |
442 function module.unload() | |
443 if module.unhook then | |
444 module:unhook("account-disco-info", account_dico_info); | |
445 module:unhook("iq-set/host/"..xmlns_push..":enable", push_enable); | |
446 module:unhook("iq-set/host/"..xmlns_push..":disable", push_disable); | |
447 | |
448 module:unhook("muc-add-history", archive_message_added); | |
449 module:unhook("cloud-notify-ping", send_ping); | |
450 | |
451 for push_identifier, _ in pairs(push_errors) do | |
452 local stanza_id = hashes.sha256(push_identifier, true); | |
453 module:unhook("iq-error/host/"..stanza_id, handle_push_error); | |
454 module:unhook("iq-result/host/"..stanza_id, handle_push_success); | |
3882
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
455 id2room[stanza_id] = nil; |
3b8f4f3b1718
mod_reload_modules: Ignore removed hosts...
tmolitor <thilo@eightysoft.de>
parents:
3570
diff
changeset
|
456 id2user[stanza_id] = nil; |
3319 | 457 end |
458 end | |
459 | |
460 module:log("info", "Module unloaded"); | |
461 end |