Mercurial > prosody-modules
annotate mod_cloud_notify/mod_cloud_notify.lua @ 2628:fbb26a56a667
mod_mam_muc: Include stanza-id in history replay
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Tue, 21 Mar 2017 04:48:37 +0100 |
parents | 8c6562f16496 |
children | 0f1421af7f6a |
rev | line source |
---|---|
1783
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
1 -- XEP-0357: Push (aka: My mobile OS vendor won't let me have persistent TCP connections) |
2247
d09014d8c901
mod_cloud_notify: Update copyright year
Kim Alvefur <zash@zash.se>
parents:
2246
diff
changeset
|
2 -- Copyright (C) 2015-2016 Kim Alvefur |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
3 -- Copyright (C) 2017 Thilo Molitor |
1783
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
4 -- |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
5 -- This file is MIT/X11 licensed. |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
6 |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
7 local st = require"util.stanza"; |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
8 local jid = require"util.jid"; |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
9 local dataform = require"util.dataforms".new; |
2625
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
10 local filters = require"util.filters"; |
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
11 local hashes = require"util.hashes"; |
1783
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
12 |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
13 local xmlns_push = "urn:xmpp:push:0"; |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
14 |
1909
c7389fe74de7
mod_cloud_notify: Make inclusion of message sender and body optional via config option
Kim Alvefur <zash@zash.se>
parents:
1908
diff
changeset
|
15 -- configuration |
2246
a3e3dc9131e7
mod_cloud_notify: Use typed config API
Kim Alvefur <zash@zash.se>
parents:
2201
diff
changeset
|
16 local include_body = module:get_option_boolean("push_notification_with_body", false); |
a3e3dc9131e7
mod_cloud_notify: Use typed config API
Kim Alvefur <zash@zash.se>
parents:
2201
diff
changeset
|
17 local include_sender = module:get_option_boolean("push_notification_with_sender", false); |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
18 local max_push_errors = module:get_option_number("push_max_errors", 50); |
1909
c7389fe74de7
mod_cloud_notify: Make inclusion of message sender and body optional via config option
Kim Alvefur <zash@zash.se>
parents:
1908
diff
changeset
|
19 |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
20 local host_sessions = prosody.hosts[module.host].sessions; |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
21 local push_errors = {}; |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
22 |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
23 -- For keeping state across reloads while caching reads |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
24 local push_store = (function() |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
25 local store = module:open_store(); |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
26 local push_services = {}; |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
27 local api = {}; |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
28 function api:get(user) |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
29 if not push_services[user] then |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
30 local err; |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
31 push_services[user], err = store:get(user); |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
32 if not push_services[user] and err then |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
33 module:log("warn", "Error reading push notification storage for user '%s': %s", user, tostring(err)); |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
34 push_services[user] = {}; |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
35 return push_services[user], false; |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
36 end |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
37 end |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
38 if not push_services[user] then push_services[user] = {} end |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
39 return push_services[user], true; |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
40 end |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
41 function api:set(user, data) |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
42 push_services[user] = data; |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
43 local ok, err = store:set(user, push_services[user]); |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
44 if not ok then |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
45 module:log("error", "Error writing push notification storage for user '%s': %s", user, tostring(err)); |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
46 return false; |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
47 end |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
48 return true; |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
49 end |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
50 function api:set_identifier(user, push_identifier, data) |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
51 local services = self:get(user); |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
52 services[push_identifier] = data; |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
53 return self:set(user, services); |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
54 end |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
55 return api; |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
56 end)(); |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
57 |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
58 local function handle_push_error(event) |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
59 local stanza = event.stanza; |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
60 local error_type, condition = stanza:get_error(); |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
61 local node = jid.split(stanza.attr.to); |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
62 local from = stanza.attr.from; |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
63 local user_push_services = push_store:get(node); |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
64 |
2625
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
65 for push_identifier, _ in pairs(user_push_services) do |
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
66 if hashes.sha256(push_identifier, true) == stanza.attr.id then |
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
67 if user_push_services[push_identifier] and user_push_services[push_identifier].jid == from and error_type ~= "wait" then |
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
68 push_errors[push_identifier] = push_errors[push_identifier] + 1; |
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
69 module:log("info", "Got error of type '%s' (%s) for identifier '%s': " |
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
70 .."error count for this identifier is now at %s", error_type, condition, push_identifier, |
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
71 tostring(push_errors[push_identifier])); |
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
72 if push_errors[push_identifier] >= max_push_errors then |
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
73 module:log("warn", "Disabling push notifications for identifier '%s'", push_identifier); |
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
74 -- remove push settings from sessions |
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
75 for _, session in pairs(host_sessions[node].sessions) do |
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
76 if session.push_identifier == push_identifier then |
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
77 session.push_identifier = nil; |
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
78 session.push_settings = nil; |
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
79 end |
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
80 end |
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
81 -- save changed global config |
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
82 push_store:set_identifier(node, push_identifier, nil); |
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
83 push_errors[push_identifier] = nil; |
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
84 -- unhook iq handlers for this identifier |
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
85 module:unhook("iq-error/bare/"..hashes.sha256(push_identifier, true), handle_push_error); |
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
86 module:unhook("iq-result/bare/"..hashes.sha256(push_identifier, true), handle_push_success); |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
87 end |
2625
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
88 elseif user_push_services[push_identifier] and user_push_services[push_identifier].jid == from and error_type == "wait" then |
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
89 module:log("debug", "Got error of type '%s' (%s) for identifier '%s': " |
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
90 .."NOT increasing error count for this identifier", error_type, condition, push_identifier); |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
91 end |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
92 end |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
93 end |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
94 return true; |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
95 end |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
96 |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
97 local function handle_push_success(event) |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
98 local stanza = event.stanza; |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
99 local node = jid.split(stanza.attr.to); |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
100 local from = stanza.attr.from; |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
101 local user_push_services = push_store:get(node); |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
102 |
2625
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
103 for push_identifier, _ in pairs(user_push_services) do |
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
104 if hashes.sha256(push_identifier, true) == stanza.attr.id then |
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
105 if user_push_services[push_identifier] and user_push_services[push_identifier].jid == from and push_errors[push_identifier] then |
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
106 push_errors[push_identifier] = 0; |
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
107 module:log("debug", "Push succeeded, error count for identifier '%s' is now at %s", push_identifier, tostring(push_errors[push_identifier])); |
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
108 end |
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
109 end |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
110 end |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
111 return true; |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
112 end |
1783
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
113 |
1908
eba279ddc050
mod_cloud_notify: Add some comments describing code blocks
Kim Alvefur <zash@zash.se>
parents:
1907
diff
changeset
|
114 -- http://xmpp.org/extensions/xep-0357.html#disco |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
115 local function account_dico_info(event) |
2200
e9e38ae8037f
mod_cloud_notify: Advertise feature on bare jid disco (thanks iNPUTmice)
Kim Alvefur <zash@zash.se>
parents:
2198
diff
changeset
|
116 (event.reply or event.stanza):tag("feature", {var=xmlns_push}):up(); |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
117 end |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
118 module:hook("account-disco-info", account_dico_info); |
1783
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
119 |
1907
7fe7bd7b33b6
mod_cloud_notify: Allow the 'node' to be left out when disabling, clients MAY include it (and therefore leave it out)
Kim Alvefur <zash@zash.se>
parents:
1784
diff
changeset
|
120 -- http://xmpp.org/extensions/xep-0357.html#enabling |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
121 local function push_enable(event) |
1783
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
122 local origin, stanza = event.origin, event.stanza; |
2254
122cb5f4930f
mod_cloud_notify: Cache <enable> in local
Kim Alvefur <zash@zash.se>
parents:
2253
diff
changeset
|
123 local enable = stanza.tags[1]; |
2252
a96f2d0f8750
mod_cloud_notify: Add some logging when a client attempts to enable push notifications
Kim Alvefur <zash@zash.se>
parents:
2247
diff
changeset
|
124 origin.log("debug", "Attempting to enable push notifications"); |
1907
7fe7bd7b33b6
mod_cloud_notify: Allow the 'node' to be left out when disabling, clients MAY include it (and therefore leave it out)
Kim Alvefur <zash@zash.se>
parents:
1784
diff
changeset
|
125 -- MUST contain a 'jid' attribute of the XMPP Push Service being enabled |
2254
122cb5f4930f
mod_cloud_notify: Cache <enable> in local
Kim Alvefur <zash@zash.se>
parents:
2253
diff
changeset
|
126 local push_jid = enable.attr.jid; |
1907
7fe7bd7b33b6
mod_cloud_notify: Allow the 'node' to be left out when disabling, clients MAY include it (and therefore leave it out)
Kim Alvefur <zash@zash.se>
parents:
1784
diff
changeset
|
127 -- SHOULD contain a 'node' attribute |
2254
122cb5f4930f
mod_cloud_notify: Cache <enable> in local
Kim Alvefur <zash@zash.se>
parents:
2253
diff
changeset
|
128 local push_node = enable.attr.node; |
1907
7fe7bd7b33b6
mod_cloud_notify: Allow the 'node' to be left out when disabling, clients MAY include it (and therefore leave it out)
Kim Alvefur <zash@zash.se>
parents:
1784
diff
changeset
|
129 if not push_jid then |
2257
f84b51f9aa82
mod_cloud_notify: Log message when 'jid' is missing from enable request
Kim Alvefur <zash@zash.se>
parents:
2255
diff
changeset
|
130 origin.log("debug", "Push notification enable request missing the 'jid' field"); |
1907
7fe7bd7b33b6
mod_cloud_notify: Allow the 'node' to be left out when disabling, clients MAY include it (and therefore leave it out)
Kim Alvefur <zash@zash.se>
parents:
1784
diff
changeset
|
131 origin.send(st.error_reply(stanza, "modify", "bad-request", "Missing jid")); |
1783
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
132 return true; |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
133 end |
2255
cdfc917a8cc7
mod_cloud_notify: Retrieve data form by name and namespace so unknown elements are ignored
Kim Alvefur <zash@zash.se>
parents:
2254
diff
changeset
|
134 local publish_options = enable:get_child("x", "jabber:x:data"); |
2258
3abc51faf945
mod_cloud_notify: Log message if no dataform is found
Kim Alvefur <zash@zash.se>
parents:
2257
diff
changeset
|
135 if not publish_options then |
3abc51faf945
mod_cloud_notify: Log message if no dataform is found
Kim Alvefur <zash@zash.se>
parents:
2257
diff
changeset
|
136 -- Could be intentional |
3abc51faf945
mod_cloud_notify: Log message if no dataform is found
Kim Alvefur <zash@zash.se>
parents:
2257
diff
changeset
|
137 origin.log("debug", "No publish options in request"); |
3abc51faf945
mod_cloud_notify: Log message if no dataform is found
Kim Alvefur <zash@zash.se>
parents:
2257
diff
changeset
|
138 end |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
139 local push_identifier = push_jid .. "<" .. (push_node or ""); |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
140 local push_service = { |
1783
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
141 jid = push_jid; |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
142 node = push_node; |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
143 count = 0; |
2253
97ebd28a8a75
mod_cloud_notify: Apply pre-serialization to publish-options
Kim Alvefur <zash@zash.se>
parents:
2252
diff
changeset
|
144 options = publish_options and st.preserialize(publish_options); |
1783
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
145 }; |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
146 local ok = push_store:set_identifier(origin.username, push_identifier, push_service); |
2201
eb5555a3a535
mod_cloud_notify: Enable persistent storage of user notification settings
Kim Alvefur <zash@zash.se>
parents:
2200
diff
changeset
|
147 if not ok then |
eb5555a3a535
mod_cloud_notify: Enable persistent storage of user notification settings
Kim Alvefur <zash@zash.se>
parents:
2200
diff
changeset
|
148 origin.send(st.error_reply(stanza, "wait", "internal-server-error")); |
eb5555a3a535
mod_cloud_notify: Enable persistent storage of user notification settings
Kim Alvefur <zash@zash.se>
parents:
2200
diff
changeset
|
149 else |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
150 origin.push_identifier = push_identifier; |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
151 origin.push_settings = push_service; |
2625
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
152 origin.log("info", "Push notifications enabled for %s (%s)", tostring(stanza.attr.from), tostring(origin.push_identifier)); |
2201
eb5555a3a535
mod_cloud_notify: Enable persistent storage of user notification settings
Kim Alvefur <zash@zash.se>
parents:
2200
diff
changeset
|
153 origin.send(st.reply(stanza)); |
eb5555a3a535
mod_cloud_notify: Enable persistent storage of user notification settings
Kim Alvefur <zash@zash.se>
parents:
2200
diff
changeset
|
154 end |
1783
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
155 return true; |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
156 end |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
157 module:hook("iq-set/self/"..xmlns_push..":enable", push_enable); |
1783
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
158 |
1907
7fe7bd7b33b6
mod_cloud_notify: Allow the 'node' to be left out when disabling, clients MAY include it (and therefore leave it out)
Kim Alvefur <zash@zash.se>
parents:
1784
diff
changeset
|
159 -- http://xmpp.org/extensions/xep-0357.html#disabling |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
160 local function push_disable(event) |
1783
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
161 local origin, stanza = event.origin, event.stanza; |
1907
7fe7bd7b33b6
mod_cloud_notify: Allow the 'node' to be left out when disabling, clients MAY include it (and therefore leave it out)
Kim Alvefur <zash@zash.se>
parents:
1784
diff
changeset
|
162 local push_jid = stanza.tags[1].attr.jid; -- MUST include a 'jid' attribute |
7fe7bd7b33b6
mod_cloud_notify: Allow the 'node' to be left out when disabling, clients MAY include it (and therefore leave it out)
Kim Alvefur <zash@zash.se>
parents:
1784
diff
changeset
|
163 local push_node = stanza.tags[1].attr.node; -- A 'node' attribute MAY be included |
7fe7bd7b33b6
mod_cloud_notify: Allow the 'node' to be left out when disabling, clients MAY include it (and therefore leave it out)
Kim Alvefur <zash@zash.se>
parents:
1784
diff
changeset
|
164 if not push_jid then |
7fe7bd7b33b6
mod_cloud_notify: Allow the 'node' to be left out when disabling, clients MAY include it (and therefore leave it out)
Kim Alvefur <zash@zash.se>
parents:
1784
diff
changeset
|
165 origin.send(st.error_reply(stanza, "modify", "bad-request", "Missing jid")); |
1783
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
166 return true; |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
167 end |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
168 local user_push_services = push_store:get(origin.username); |
1907
7fe7bd7b33b6
mod_cloud_notify: Allow the 'node' to be left out when disabling, clients MAY include it (and therefore leave it out)
Kim Alvefur <zash@zash.se>
parents:
1784
diff
changeset
|
169 for key, push_info in pairs(user_push_services) do |
7fe7bd7b33b6
mod_cloud_notify: Allow the 'node' to be left out when disabling, clients MAY include it (and therefore leave it out)
Kim Alvefur <zash@zash.se>
parents:
1784
diff
changeset
|
170 if push_info.jid == push_jid and (not push_node or push_info.node == push_node) then |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
171 origin.log("info", "Push notifications disabled (%s)", tostring(key)); |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
172 if origin.push_identifier == key then |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
173 origin.push_identifier = nil; |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
174 origin.push_settings = nil; |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
175 end |
1907
7fe7bd7b33b6
mod_cloud_notify: Allow the 'node' to be left out when disabling, clients MAY include it (and therefore leave it out)
Kim Alvefur <zash@zash.se>
parents:
1784
diff
changeset
|
176 user_push_services[key] = nil; |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
177 push_errors[key] = nil; |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
178 module:unhook("iq-error/bare/"..key, handle_push_error); |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
179 module:unhook("iq-result/bare/"..key, handle_push_success); |
1907
7fe7bd7b33b6
mod_cloud_notify: Allow the 'node' to be left out when disabling, clients MAY include it (and therefore leave it out)
Kim Alvefur <zash@zash.se>
parents:
1784
diff
changeset
|
180 end |
1783
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
181 end |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
182 local ok = push_store:set(origin.username, user_push_services); |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
183 if not ok then |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
184 origin.send(st.error_reply(stanza, "wait", "internal-server-error")); |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
185 else |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
186 origin.send(st.reply(stanza)); |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
187 end |
1783
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
188 return true; |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
189 end |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
190 module:hook("iq-set/self/"..xmlns_push..":disable", push_disable); |
1783
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
191 |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
192 local push_form = dataform { |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
193 { name = "FORM_TYPE"; type = "hidden"; value = "urn:xmpp:push:summary"; }; |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
194 { name = "message-count"; type = "text-single"; }; |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
195 { name = "pending-subscription-count"; type = "text-single"; }; |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
196 { name = "last-message-sender"; type = "jid-single"; }; |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
197 { name = "last-message-body"; type = "text-single"; }; |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
198 }; |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
199 |
1907
7fe7bd7b33b6
mod_cloud_notify: Allow the 'node' to be left out when disabling, clients MAY include it (and therefore leave it out)
Kim Alvefur <zash@zash.se>
parents:
1784
diff
changeset
|
200 -- http://xmpp.org/extensions/xep-0357.html#publishing |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
201 local function handle_notify_request(stanza, node, user_push_services) |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
202 if not user_push_services or not #user_push_services then return end |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
203 |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
204 for push_identifier, push_info in pairs(user_push_services) do |
2625
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
205 if stanza then |
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
206 if not stanza._push_notify then stanza._push_notify = {}; end |
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
207 if stanza._push_notify[push_identifier] then |
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
208 module:log("debug", "Already sent push notification for %s@%s to %s (%s)", node, module.host, push_info.jid, tostring(push_info.node)); |
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
209 return; |
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
210 end |
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
211 stanza._push_notify[push_identifier] = true; |
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
212 end |
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
213 |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
214 -- increment count and save it |
1783
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
215 push_info.count = push_info.count + 1; |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
216 push_store:set_identifier(node, push_identifier, push_info); |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
217 -- construct push stanza |
2625
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
218 local push_publish = st.iq({ to = push_info.jid, from = node .. "@" .. module.host, type = "set", id = hashes.sha256(push_identifier, true) }) |
1783
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
219 :tag("pubsub", { xmlns = "http://jabber.org/protocol/pubsub" }) |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
220 :tag("publish", { node = push_info.node }) |
2051
cb0fc00a7086
mod_cloud_notify: Fix syntax error
Kim Alvefur <zash@zash.se>
parents:
2050
diff
changeset
|
221 :tag("item") |
2050
49cc6a555dc7
mod_cloud_notify: Wrap form in namespaced element per the XEP (fixes #630)
Kim Alvefur <zash@zash.se>
parents:
2046
diff
changeset
|
222 :tag("notification", { xmlns = xmlns_push }); |
1909
c7389fe74de7
mod_cloud_notify: Make inclusion of message sender and body optional via config option
Kim Alvefur <zash@zash.se>
parents:
1908
diff
changeset
|
223 local form_data = { |
1783
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
224 ["message-count"] = tostring(push_info.count); |
1909
c7389fe74de7
mod_cloud_notify: Make inclusion of message sender and body optional via config option
Kim Alvefur <zash@zash.se>
parents:
1908
diff
changeset
|
225 }; |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
226 if stanza and include_sender then |
1909
c7389fe74de7
mod_cloud_notify: Make inclusion of message sender and body optional via config option
Kim Alvefur <zash@zash.se>
parents:
1908
diff
changeset
|
227 form_data["last-message-sender"] = stanza.attr.from; |
c7389fe74de7
mod_cloud_notify: Make inclusion of message sender and body optional via config option
Kim Alvefur <zash@zash.se>
parents:
1908
diff
changeset
|
228 end |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
229 if stanza and include_body then |
1909
c7389fe74de7
mod_cloud_notify: Make inclusion of message sender and body optional via config option
Kim Alvefur <zash@zash.se>
parents:
1908
diff
changeset
|
230 form_data["last-message-body"] = stanza:get_child_text("body"); |
c7389fe74de7
mod_cloud_notify: Make inclusion of message sender and body optional via config option
Kim Alvefur <zash@zash.se>
parents:
1908
diff
changeset
|
231 end |
c7389fe74de7
mod_cloud_notify: Make inclusion of message sender and body optional via config option
Kim Alvefur <zash@zash.se>
parents:
1908
diff
changeset
|
232 push_publish:add_child(push_form:form(form_data)); |
2050
49cc6a555dc7
mod_cloud_notify: Wrap form in namespaced element per the XEP (fixes #630)
Kim Alvefur <zash@zash.se>
parents:
2046
diff
changeset
|
233 push_publish:up(); -- / notification |
1783
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
234 push_publish:up(); -- / publish |
2046
980e5f5bd62c
mod_cloud_notify: put publish-options into <pubsub> not into <publish>
Daniel Gultsch <daniel@gultsch.de>
parents:
1924
diff
changeset
|
235 push_publish:up(); -- / pubsub |
1783
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
236 if push_info.options then |
2253
97ebd28a8a75
mod_cloud_notify: Apply pre-serialization to publish-options
Kim Alvefur <zash@zash.se>
parents:
2252
diff
changeset
|
237 push_publish:tag("publish-options"):add_child(st.deserialize(push_info.options)); |
1783
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
238 end |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
239 -- send out push |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
240 module:log("debug", "Sending push notification for %s@%s to %s (%s)", node, module.host, push_info.jid, tostring(push_info.node)); |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
241 -- handle push errors for this node |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
242 if push_errors[push_identifier] == nil then |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
243 push_errors[push_identifier] = 0; |
2625
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
244 module:hook("iq-error/bare/"..hashes.sha256(push_identifier, true), handle_push_error); |
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
245 module:hook("iq-result/bare/"..hashes.sha256(push_identifier, true), handle_push_success); |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
246 end |
1783
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
247 module:send(push_publish); |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
248 end |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
249 end |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
250 |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
251 -- small helper function to extract relevant push settings |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
252 local function get_push_settings(stanza, session) |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
253 local to = stanza.attr.to; |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
254 local node = to and jid.split(to) or session.username; |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
255 local user_push_services = push_store:get(node); |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
256 return node, user_push_services; |
2141
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
257 end |
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
258 |
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
259 -- publish on offline message |
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
260 module:hook("message/offline/handle", function(event) |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
261 local node, user_push_services = get_push_settings(event.stanza, event.origin); |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
262 return handle_notify_request(event.stanza, node, user_push_services); |
1783
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
263 end, 1); |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
264 |
2141
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
265 -- publish on unacked smacks message |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
266 local function process_smacks_stanza(stanza, session) |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
267 if session.push_identifier then |
2625
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
268 session.log("debug", "Invoking cloud handle_notify_request for smacks queued stanza"); |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
269 local user_push_services = {[session.push_identifier] = session.push_settings}; |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
270 local node = get_push_settings(stanza, session); |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
271 handle_notify_request(stanza, node, user_push_services); |
2141
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
272 end |
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
273 return stanza; |
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
274 end |
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
275 |
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
276 -- smacks hibernation is started |
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
277 local function hibernate_session(event) |
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
278 local session = event.origin; |
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
279 local queue = event.queue; |
2395
2e641ab995b3
mod_cloud_notify: added code to respond to the new event "smacks-ack-delayed" issued by mod_smacks when acks are delayed for a certain amount of time. This allows to send out notification requests before the read timeout or connection close event really happens, thus allowing conversations to be smoother.
tmolitor <thilo@eightysoft.de>
parents:
2263
diff
changeset
|
280 -- process unacked stanzas |
2141
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
281 for i=1,#queue do |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
282 process_smacks_stanza(queue[i], session); |
2141
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
283 end |
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
284 -- process future unacked (hibernated) stanzas |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
285 filters.add_filter(session, "stanzas/out", process_smacks_stanza); |
2141
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
286 end |
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
287 |
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
288 -- smacks hibernation is ended |
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
289 local function restore_session(event) |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
290 local session = event.resumed; |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
291 if session then -- older smacks module versions send only the "intermediate" session in event.session and no session.resumed one |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
292 filters.remove_filter(session, "stanzas/out", process_smacks_stanza); |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
293 -- this means the counter of outstanding push messages can be reset as well |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
294 if session.push_settings then |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
295 session.push_settings.count = 0; |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
296 push_store:set_identifier(session.username, session.push_identifier, session.push_settings); |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
297 end |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
298 end |
2141
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
299 end |
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
300 |
2395
2e641ab995b3
mod_cloud_notify: added code to respond to the new event "smacks-ack-delayed" issued by mod_smacks when acks are delayed for a certain amount of time. This allows to send out notification requests before the read timeout or connection close event really happens, thus allowing conversations to be smoother.
tmolitor <thilo@eightysoft.de>
parents:
2263
diff
changeset
|
301 -- smacks ack is delayed |
2e641ab995b3
mod_cloud_notify: added code to respond to the new event "smacks-ack-delayed" issued by mod_smacks when acks are delayed for a certain amount of time. This allows to send out notification requests before the read timeout or connection close event really happens, thus allowing conversations to be smoother.
tmolitor <thilo@eightysoft.de>
parents:
2263
diff
changeset
|
302 local function ack_delayed(event) |
2e641ab995b3
mod_cloud_notify: added code to respond to the new event "smacks-ack-delayed" issued by mod_smacks when acks are delayed for a certain amount of time. This allows to send out notification requests before the read timeout or connection close event really happens, thus allowing conversations to be smoother.
tmolitor <thilo@eightysoft.de>
parents:
2263
diff
changeset
|
303 local session = event.origin; |
2e641ab995b3
mod_cloud_notify: added code to respond to the new event "smacks-ack-delayed" issued by mod_smacks when acks are delayed for a certain amount of time. This allows to send out notification requests before the read timeout or connection close event really happens, thus allowing conversations to be smoother.
tmolitor <thilo@eightysoft.de>
parents:
2263
diff
changeset
|
304 local queue = event.queue; |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
305 -- process unacked stanzas (handle_notify_request() will only send push requests for new stanzas) |
2395
2e641ab995b3
mod_cloud_notify: added code to respond to the new event "smacks-ack-delayed" issued by mod_smacks when acks are delayed for a certain amount of time. This allows to send out notification requests before the read timeout or connection close event really happens, thus allowing conversations to be smoother.
tmolitor <thilo@eightysoft.de>
parents:
2263
diff
changeset
|
306 for i=1,#queue do |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
307 process_smacks_stanza(queue[i], session); |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
308 end |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
309 end |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
310 |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
311 -- archive message added |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
312 local function archive_message_added(event) |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
313 -- event is: { origin = origin, stanza = stanza, for_user = store_user, id = id } |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
314 -- only notify for new mam messages when at least one device is only |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
315 if not event.for_user or not host_sessions[event.for_user] then return; end |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
316 local stanza = event.stanza; |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
317 local user_session = host_sessions[event.for_user].sessions; |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
318 local to = stanza.attr.to; |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
319 to = to and jid.split(to) or event.origin.username; |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
320 |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
321 -- only notify if the stanza destination is the mam user we store it for |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
322 if event.for_user == to then |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
323 local user_push_services = push_store:get(to); |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
324 if not #user_push_services then return end |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
325 |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
326 -- only notify nodes with no active sessions (smacks is counted as active and handled separate) |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
327 local notify_push_sevices = {}; |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
328 for identifier, push_info in pairs(user_push_services) do |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
329 local identifier_found = nil; |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
330 for _, session in pairs(user_session) do |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
331 -- module:log("debug", "searching for '%s': identifier '%s' for session %s", tostring(identifier), tostring(session.push_identifier), tostring(session.full_jid)); |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
332 if session.push_identifier == identifier then |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
333 identifier_found = session; |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
334 break; |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
335 end |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
336 end |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
337 if identifier_found then |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
338 identifier_found.log("debug", "Not notifying '%s' of new MAM stanza (session still alive)", identifier); |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
339 else |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
340 notify_push_sevices[identifier] = push_info; |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
341 end |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
342 end |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
343 |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
344 return handle_notify_request(event.stanza, to, notify_push_sevices); |
2395
2e641ab995b3
mod_cloud_notify: added code to respond to the new event "smacks-ack-delayed" issued by mod_smacks when acks are delayed for a certain amount of time. This allows to send out notification requests before the read timeout or connection close event really happens, thus allowing conversations to be smoother.
tmolitor <thilo@eightysoft.de>
parents:
2263
diff
changeset
|
345 end |
2e641ab995b3
mod_cloud_notify: added code to respond to the new event "smacks-ack-delayed" issued by mod_smacks when acks are delayed for a certain amount of time. This allows to send out notification requests before the read timeout or connection close event really happens, thus allowing conversations to be smoother.
tmolitor <thilo@eightysoft.de>
parents:
2263
diff
changeset
|
346 end |
2e641ab995b3
mod_cloud_notify: added code to respond to the new event "smacks-ack-delayed" issued by mod_smacks when acks are delayed for a certain amount of time. This allows to send out notification requests before the read timeout or connection close event really happens, thus allowing conversations to be smoother.
tmolitor <thilo@eightysoft.de>
parents:
2263
diff
changeset
|
347 |
2141
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
348 module:hook("smacks-hibernation-start", hibernate_session); |
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
349 module:hook("smacks-hibernation-end", restore_session); |
2395
2e641ab995b3
mod_cloud_notify: added code to respond to the new event "smacks-ack-delayed" issued by mod_smacks when acks are delayed for a certain amount of time. This allows to send out notification requests before the read timeout or connection close event really happens, thus allowing conversations to be smoother.
tmolitor <thilo@eightysoft.de>
parents:
2263
diff
changeset
|
350 module:hook("smacks-ack-delayed", ack_delayed); |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
351 module:hook("archive-message-added", archive_message_added); |
2141
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
352 |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
353 local function send_ping(event) |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
354 local user = event.user; |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
355 local user_push_services = push_store:get(user); |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
356 local push_services = event.push_services or user_push_services; |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
357 return handle_notify_request(nil, user, push_services); |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
358 end |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
359 -- can be used by other modules to ping one or more (or all) push endpoints |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
360 module:hook("cloud-notify-ping", send_ping); |
1783
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
361 |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
362 -- TODO: this has to be done on first connect not on offline broadcast, else the counter will be incorrect |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
363 -- TODO: it seems this is already done, so this could be safely removed, couldn't it? |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
364 -- module:hook("message/offline/broadcast", function(event) |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
365 -- local origin = event.origin; |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
366 -- local user_push_services = push_store:get(origin.username); |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
367 -- if not #user_push_services then return end |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
368 -- |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
369 -- for _, push_info in pairs(user_push_services) do |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
370 -- if push_info then |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
371 -- push_info.count = 0; |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
372 -- end |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
373 -- end |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
374 -- push_store:set(origin.username, user_push_services); |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
375 -- end, 1); |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
376 |
2625
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
377 module:log("info", "Module loaded"); |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
378 function module.unload() |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
379 module:unhook("account-disco-info", account_dico_info); |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
380 module:unhook("iq-set/self/"..xmlns_push..":enable", push_enable); |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
381 module:unhook("iq-set/self/"..xmlns_push..":disable", push_disable); |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
382 |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
383 module:unhook("smacks-hibernation-start", hibernate_session); |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
384 module:unhook("smacks-hibernation-end", restore_session); |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
385 module:unhook("smacks-ack-delayed", ack_delayed); |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
386 module:unhook("archive-message-added", archive_message_added); |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
387 module:unhook("cloud-notify-ping", send_ping); |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
388 |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
389 for push_identifier, _ in pairs(push_errors) do |
2625
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
390 module:hook("iq-error/bare/"..hashes.sha256(push_identifier, true), handle_push_error); |
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
391 module:hook("iq-result/bare/"..hashes.sha256(push_identifier, true), handle_push_success); |
1783
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
392 end |
2625
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
393 |
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
394 module:log("info", "Module unloaded"); |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
395 end |