Mercurial > prosody-modules
annotate mod_cloud_notify/mod_cloud_notify.lua @ 4299:8006da2cf44c
For deployments that have https://hg.prosody.im/trunk/file/tip/plugins/muc/register.lib.lua#l7 and use https://modules.prosody.im/mod_muc_http_auth.html users can still register to a MUC even if they are not allowed to join. That means they would receive RAI or RMN, for instance.
author | Seve Ferrer <seve@delape.net> |
---|---|
date | Tue, 15 Dec 2020 11:26:29 +0100 |
parents | d44a8d3dd571 |
children | 45dcf5d4cd6c |
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 |
3619
74aa35aeb08a
mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents:
3108
diff
changeset
|
3 -- Copyright (C) 2017-2019 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 |
3055
6abee021d9db
mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents:
3010
diff
changeset
|
7 local os_time = os.time; |
1783
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
8 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
|
9 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
|
10 local dataform = require"util.dataforms".new; |
2625
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
11 local hashes = require"util.hashes"; |
3943
f5e6368a1c39
mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents:
3940
diff
changeset
|
12 local random = require"util.random"; |
f5e6368a1c39
mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents:
3940
diff
changeset
|
13 local cache = require"util.cache"; |
1783
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
14 |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
15 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
|
16 |
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
|
17 -- configuration |
2246
a3e3dc9131e7
mod_cloud_notify: Use typed config API
Kim Alvefur <zash@zash.se>
parents:
2201
diff
changeset
|
18 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
|
19 local include_sender = module:get_option_boolean("push_notification_with_sender", false); |
3055
6abee021d9db
mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents:
3010
diff
changeset
|
20 local max_push_errors = module:get_option_number("push_max_errors", 16); |
6abee021d9db
mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents:
3010
diff
changeset
|
21 local max_push_devices = module:get_option_number("push_max_devices", 5); |
6abee021d9db
mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents:
3010
diff
changeset
|
22 local dummy_body = module:get_option_string("push_notification_important_body", "New Message!"); |
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
|
23 |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
24 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
|
25 local push_errors = {}; |
2791
008cf272b7ea
mod_cloud_notify: Fix regression in error handling
tmolitor <thilo@eightysoft.de>
parents:
2751
diff
changeset
|
26 local id2node = {}; |
3943
f5e6368a1c39
mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents:
3940
diff
changeset
|
27 local id2identifier = {}; |
3619
74aa35aeb08a
mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents:
3108
diff
changeset
|
28 |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
29 -- For keeping state across reloads while caching reads |
3943
f5e6368a1c39
mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents:
3940
diff
changeset
|
30 -- This uses util.cache for caching the most recent devices and removing all old devices when max_push_devices is reached |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
31 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
|
32 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
|
33 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
|
34 local api = {}; |
4295
d44a8d3dd571
mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
4273
diff
changeset
|
35 --luacheck: ignore 212/self |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
36 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
|
37 if not push_services[user] then |
3943
f5e6368a1c39
mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents:
3940
diff
changeset
|
38 local loaded, err = store:get(user); |
f5e6368a1c39
mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents:
3940
diff
changeset
|
39 if not loaded and err then |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
40 module:log("warn", "Error reading push notification storage for user '%s': %s", user, tostring(err)); |
3943
f5e6368a1c39
mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents:
3940
diff
changeset
|
41 push_services[user] = cache.new(max_push_devices):table(); |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
42 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
|
43 end |
3943
f5e6368a1c39
mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents:
3940
diff
changeset
|
44 if loaded then |
f5e6368a1c39
mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents:
3940
diff
changeset
|
45 push_services[user] = cache.new(max_push_devices):table(); |
f5e6368a1c39
mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents:
3940
diff
changeset
|
46 -- copy over plain table loaded from disk into our cache |
f5e6368a1c39
mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents:
3940
diff
changeset
|
47 for k, v in pairs(loaded) do push_services[user][k] = v; end |
f5e6368a1c39
mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents:
3940
diff
changeset
|
48 else |
f5e6368a1c39
mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents:
3940
diff
changeset
|
49 push_services[user] = cache.new(max_push_devices):table(); |
f5e6368a1c39
mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents:
3940
diff
changeset
|
50 end |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
51 end |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
52 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
|
53 end |
3943
f5e6368a1c39
mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents:
3940
diff
changeset
|
54 function api:flush_to_disk(user) |
f5e6368a1c39
mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents:
3940
diff
changeset
|
55 local plain_table = {}; |
f5e6368a1c39
mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents:
3940
diff
changeset
|
56 for k, v in pairs(push_services[user]) do plain_table[k] = v; end |
f5e6368a1c39
mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents:
3940
diff
changeset
|
57 local ok, err = store:set(user, plain_table); |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
58 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
|
59 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
|
60 return false; |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
61 end |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
62 return true; |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
63 end |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
64 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
|
65 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
|
66 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
|
67 end |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
68 return api; |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
69 end)(); |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
70 |
3108
cfcb020bcd1d
mod_cloud_notify: inform mod_smacks of first push in hibernated state
tmolitor <thilo@eightysoft.de>
parents:
3085
diff
changeset
|
71 |
2642
0f1421af7f6a
mod_cloud_notify: Move declarations of handle_push_success/error to fix referencing undefined variables (introduced in 6ab46ff685d0)
Matthew Wild <mwild1@gmail.com>
parents:
2625
diff
changeset
|
72 -- Forward declarations, as both functions need to reference each other |
0f1421af7f6a
mod_cloud_notify: Move declarations of handle_push_success/error to fix referencing undefined variables (introduced in 6ab46ff685d0)
Matthew Wild <mwild1@gmail.com>
parents:
2625
diff
changeset
|
73 local handle_push_success, handle_push_error; |
0f1421af7f6a
mod_cloud_notify: Move declarations of handle_push_success/error to fix referencing undefined variables (introduced in 6ab46ff685d0)
Matthew Wild <mwild1@gmail.com>
parents:
2625
diff
changeset
|
74 |
0f1421af7f6a
mod_cloud_notify: Move declarations of handle_push_success/error to fix referencing undefined variables (introduced in 6ab46ff685d0)
Matthew Wild <mwild1@gmail.com>
parents:
2625
diff
changeset
|
75 function handle_push_error(event) |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
76 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
|
77 local error_type, condition = stanza:get_error(); |
2791
008cf272b7ea
mod_cloud_notify: Fix regression in error handling
tmolitor <thilo@eightysoft.de>
parents:
2751
diff
changeset
|
78 local node = id2node[stanza.attr.id]; |
3943
f5e6368a1c39
mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents:
3940
diff
changeset
|
79 local identifier = id2identifier[stanza.attr.id]; |
2791
008cf272b7ea
mod_cloud_notify: Fix regression in error handling
tmolitor <thilo@eightysoft.de>
parents:
2751
diff
changeset
|
80 if node == nil then return false; end -- unknown stanza? Ignore for now! |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
81 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
|
82 local user_push_services = push_store:get(node); |
3085
1ea6861b533f
mod_cloud_notify: Don't change table while iterating it
tmolitor <thilo@eightysoft.de>
parents:
3079
diff
changeset
|
83 local changed = false; |
4295
d44a8d3dd571
mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
4273
diff
changeset
|
84 |
2625
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
85 for push_identifier, _ in pairs(user_push_services) do |
3943
f5e6368a1c39
mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents:
3940
diff
changeset
|
86 if push_identifier == identifier then |
2625
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
87 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
|
88 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
|
89 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
|
90 .."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
|
91 tostring(push_errors[push_identifier])); |
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
92 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
|
93 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
|
94 -- remove push settings from sessions |
2749
9756211fcbe3
mod_cloud_notify: Fix small bug.
tmolitor <thilo@eightysoft.de>
parents:
2736
diff
changeset
|
95 if host_sessions[node] then |
9756211fcbe3
mod_cloud_notify: Fix small bug.
tmolitor <thilo@eightysoft.de>
parents:
2736
diff
changeset
|
96 for _, session in pairs(host_sessions[node].sessions) do |
9756211fcbe3
mod_cloud_notify: Fix small bug.
tmolitor <thilo@eightysoft.de>
parents:
2736
diff
changeset
|
97 if session.push_identifier == push_identifier then |
9756211fcbe3
mod_cloud_notify: Fix small bug.
tmolitor <thilo@eightysoft.de>
parents:
2736
diff
changeset
|
98 session.push_identifier = nil; |
9756211fcbe3
mod_cloud_notify: Fix small bug.
tmolitor <thilo@eightysoft.de>
parents:
2736
diff
changeset
|
99 session.push_settings = nil; |
3108
cfcb020bcd1d
mod_cloud_notify: inform mod_smacks of first push in hibernated state
tmolitor <thilo@eightysoft.de>
parents:
3085
diff
changeset
|
100 session.first_hibernated_push = nil; |
2749
9756211fcbe3
mod_cloud_notify: Fix small bug.
tmolitor <thilo@eightysoft.de>
parents:
2736
diff
changeset
|
101 end |
2625
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
102 end |
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
103 end |
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
104 -- save changed global config |
3085
1ea6861b533f
mod_cloud_notify: Don't change table while iterating it
tmolitor <thilo@eightysoft.de>
parents:
3079
diff
changeset
|
105 changed = true; |
1ea6861b533f
mod_cloud_notify: Don't change table while iterating it
tmolitor <thilo@eightysoft.de>
parents:
3079
diff
changeset
|
106 user_push_services[push_identifier] = nil |
2625
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
107 push_errors[push_identifier] = nil; |
2669
e6d243ed88ca
mod_cloud_notify: Fix module:unhook calls not available in prosody 0.9, fixes #874
tmolitor <thilo@eightysoft.de>
parents:
2643
diff
changeset
|
108 -- unhook iq handlers for this identifier (if possible) |
3943
f5e6368a1c39
mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents:
3940
diff
changeset
|
109 module:unhook("iq-error/host/"..stanza.attr.id, handle_push_error); |
f5e6368a1c39
mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents:
3940
diff
changeset
|
110 module:unhook("iq-result/host/"..stanza.attr.id, handle_push_success); |
f5e6368a1c39
mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents:
3940
diff
changeset
|
111 id2node[stanza.attr.id] = nil; |
f5e6368a1c39
mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents:
3940
diff
changeset
|
112 id2identifier[stanza.attr.id] = nil; |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
113 end |
2625
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
114 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
|
115 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
|
116 .."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
|
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 end |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
119 end |
3085
1ea6861b533f
mod_cloud_notify: Don't change table while iterating it
tmolitor <thilo@eightysoft.de>
parents:
3079
diff
changeset
|
120 if changed then |
3943
f5e6368a1c39
mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents:
3940
diff
changeset
|
121 push_store:flush_to_disk(node); |
3085
1ea6861b533f
mod_cloud_notify: Don't change table while iterating it
tmolitor <thilo@eightysoft.de>
parents:
3079
diff
changeset
|
122 end |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
123 return true; |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
124 end |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
125 |
2642
0f1421af7f6a
mod_cloud_notify: Move declarations of handle_push_success/error to fix referencing undefined variables (introduced in 6ab46ff685d0)
Matthew Wild <mwild1@gmail.com>
parents:
2625
diff
changeset
|
126 function handle_push_success(event) |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
127 local stanza = event.stanza; |
2791
008cf272b7ea
mod_cloud_notify: Fix regression in error handling
tmolitor <thilo@eightysoft.de>
parents:
2751
diff
changeset
|
128 local node = id2node[stanza.attr.id]; |
3943
f5e6368a1c39
mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents:
3940
diff
changeset
|
129 local identifier = id2identifier[stanza.attr.id]; |
2791
008cf272b7ea
mod_cloud_notify: Fix regression in error handling
tmolitor <thilo@eightysoft.de>
parents:
2751
diff
changeset
|
130 if node == nil then return false; end -- unknown stanza? Ignore for now! |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
131 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
|
132 local user_push_services = push_store:get(node); |
4295
d44a8d3dd571
mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
4273
diff
changeset
|
133 |
2625
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
134 for push_identifier, _ in pairs(user_push_services) do |
3943
f5e6368a1c39
mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents:
3940
diff
changeset
|
135 if push_identifier == identifier then |
2670
6e01878103c0
mod_smacks: Ignore user when writing or reading session_cache on prosody 0.9
tmolitor <thilo@eightysoft.de>
parents:
2669
diff
changeset
|
136 if user_push_services[push_identifier] and user_push_services[push_identifier].jid == from and push_errors[push_identifier] > 0 then |
2625
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
137 push_errors[push_identifier] = 0; |
3619
74aa35aeb08a
mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents:
3108
diff
changeset
|
138 -- unhook iq handlers for this identifier (if possible) |
3943
f5e6368a1c39
mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents:
3940
diff
changeset
|
139 module:unhook("iq-error/host/"..stanza.attr.id, handle_push_error); |
f5e6368a1c39
mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents:
3940
diff
changeset
|
140 module:unhook("iq-result/host/"..stanza.attr.id, handle_push_success); |
f5e6368a1c39
mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents:
3940
diff
changeset
|
141 id2node[stanza.attr.id] = nil; |
f5e6368a1c39
mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents:
3940
diff
changeset
|
142 id2identifier[stanza.attr.id] = nil; |
4295
d44a8d3dd571
mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
4273
diff
changeset
|
143 module:log("debug", "Push succeeded, error count for identifier '%s' is now at %s again", |
d44a8d3dd571
mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
4273
diff
changeset
|
144 push_identifier, tostring(push_errors[push_identifier]) |
d44a8d3dd571
mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
4273
diff
changeset
|
145 ); |
2625
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
146 end |
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
147 end |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
148 end |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
149 return true; |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
150 end |
1783
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
151 |
1908
eba279ddc050
mod_cloud_notify: Add some comments describing code blocks
Kim Alvefur <zash@zash.se>
parents:
1907
diff
changeset
|
152 -- 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
|
153 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
|
154 (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
|
155 end |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
156 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
|
157 |
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
|
158 -- 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
|
159 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
|
160 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
|
161 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
|
162 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
|
163 -- 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
|
164 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
|
165 -- SHOULD contain a 'node' attribute |
2254
122cb5f4930f
mod_cloud_notify: Cache <enable> in local
Kim Alvefur <zash@zash.se>
parents:
2253
diff
changeset
|
166 local push_node = enable.attr.node; |
2736
fff185e7ab73
mod_cloud_notify: Implement the "stripped stanzas" proposal.
tmolitor <thilo@eightysoft.de>
parents:
2714
diff
changeset
|
167 -- CAN contain a 'include_payload' attribute |
fff185e7ab73
mod_cloud_notify: Implement the "stripped stanzas" proposal.
tmolitor <thilo@eightysoft.de>
parents:
2714
diff
changeset
|
168 local include_payload = enable.attr.include_payload; |
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 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
|
170 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
|
171 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
|
172 return true; |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
173 end |
3996
42682505e692
mod_cloud_notify: Forbid user from registering their own JID as their push server
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3979
diff
changeset
|
174 if push_jid == stanza.attr.from then |
42682505e692
mod_cloud_notify: Forbid user from registering their own JID as their push server
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3979
diff
changeset
|
175 origin.log("debug", "Push notification enable request 'jid' field identical to our own"); |
42682505e692
mod_cloud_notify: Forbid user from registering their own JID as their push server
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3979
diff
changeset
|
176 origin.send(st.error_reply(stanza, "modify", "bad-request", "JID must be different from ours")); |
42682505e692
mod_cloud_notify: Forbid user from registering their own JID as their push server
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3979
diff
changeset
|
177 return true; |
42682505e692
mod_cloud_notify: Forbid user from registering their own JID as their push server
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3979
diff
changeset
|
178 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
|
179 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
|
180 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
|
181 -- Could be intentional |
3abc51faf945
mod_cloud_notify: Log message if no dataform is found
Kim Alvefur <zash@zash.se>
parents:
2257
diff
changeset
|
182 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
|
183 end |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
184 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
|
185 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
|
186 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
|
187 node = push_node; |
2736
fff185e7ab73
mod_cloud_notify: Implement the "stripped stanzas" proposal.
tmolitor <thilo@eightysoft.de>
parents:
2714
diff
changeset
|
188 include_payload = include_payload; |
2253
97ebd28a8a75
mod_cloud_notify: Apply pre-serialization to publish-options
Kim Alvefur <zash@zash.se>
parents:
2252
diff
changeset
|
189 options = publish_options and st.preserialize(publish_options); |
3055
6abee021d9db
mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents:
3010
diff
changeset
|
190 timestamp = os_time(); |
1783
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
191 }; |
3943
f5e6368a1c39
mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents:
3940
diff
changeset
|
192 push_store:set_identifier(origin.username, push_identifier, push_service); |
f5e6368a1c39
mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents:
3940
diff
changeset
|
193 local ok = push_store:flush_to_disk(origin.username); |
2201
eb5555a3a535
mod_cloud_notify: Enable persistent storage of user notification settings
Kim Alvefur <zash@zash.se>
parents:
2200
diff
changeset
|
194 if not ok then |
eb5555a3a535
mod_cloud_notify: Enable persistent storage of user notification settings
Kim Alvefur <zash@zash.se>
parents:
2200
diff
changeset
|
195 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
|
196 else |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
197 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
|
198 origin.push_settings = push_service; |
3108
cfcb020bcd1d
mod_cloud_notify: inform mod_smacks of first push in hibernated state
tmolitor <thilo@eightysoft.de>
parents:
3085
diff
changeset
|
199 origin.first_hibernated_push = nil; |
2625
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
200 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
|
201 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
|
202 end |
1783
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
203 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
|
204 end |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
205 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
|
206 |
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
|
207 -- 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
|
208 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
|
209 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
|
210 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
|
211 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
|
212 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
|
213 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
|
214 return true; |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
215 end |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
216 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
|
217 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
|
218 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
|
219 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
|
220 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
|
221 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
|
222 origin.push_settings = nil; |
3108
cfcb020bcd1d
mod_cloud_notify: inform mod_smacks of first push in hibernated state
tmolitor <thilo@eightysoft.de>
parents:
3085
diff
changeset
|
223 origin.first_hibernated_push = nil; |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
224 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
|
225 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
|
226 push_errors[key] = nil; |
3943
f5e6368a1c39
mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents:
3940
diff
changeset
|
227 for stanza_id, identifier in pairs(id2identifier) do |
f5e6368a1c39
mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents:
3940
diff
changeset
|
228 if identifier == key then |
f5e6368a1c39
mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents:
3940
diff
changeset
|
229 module:unhook("iq-error/host/"..stanza_id, handle_push_error); |
f5e6368a1c39
mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents:
3940
diff
changeset
|
230 module:unhook("iq-result/host/"..stanza_id, handle_push_success); |
f5e6368a1c39
mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents:
3940
diff
changeset
|
231 id2node[stanza_id] = nil; |
f5e6368a1c39
mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents:
3940
diff
changeset
|
232 id2identifier[stanza_id] = nil; |
f5e6368a1c39
mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents:
3940
diff
changeset
|
233 end |
2669
e6d243ed88ca
mod_cloud_notify: Fix module:unhook calls not available in prosody 0.9, fixes #874
tmolitor <thilo@eightysoft.de>
parents:
2643
diff
changeset
|
234 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
|
235 end |
1783
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
236 end |
3943
f5e6368a1c39
mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents:
3940
diff
changeset
|
237 local ok = push_store:flush_to_disk(origin.username); |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
238 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
|
239 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
|
240 else |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
241 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
|
242 end |
1783
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
243 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
|
244 end |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
245 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
|
246 |
2976
df86ce6bb0b4
Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents:
2792
diff
changeset
|
247 -- is this push a high priority one (this is needed for ios apps not using voip pushes) |
df86ce6bb0b4
Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents:
2792
diff
changeset
|
248 local function is_important(stanza) |
df86ce6bb0b4
Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents:
2792
diff
changeset
|
249 local st_name = stanza and stanza.name or nil; |
4295
d44a8d3dd571
mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
4273
diff
changeset
|
250 if not st_name then return false; end -- nonzas are never important here |
2976
df86ce6bb0b4
Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents:
2792
diff
changeset
|
251 if st_name == "presence" then |
4295
d44a8d3dd571
mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
4273
diff
changeset
|
252 return false; -- same for presences |
2976
df86ce6bb0b4
Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents:
2792
diff
changeset
|
253 elseif st_name == "message" then |
4295
d44a8d3dd571
mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
4273
diff
changeset
|
254 -- unpack carbon copied message stanzas |
d44a8d3dd571
mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
4273
diff
changeset
|
255 local carbon = stanza:find("{urn:xmpp:carbons:2}/{urn:xmpp:forward:0}/{jabber:client}message"); |
d44a8d3dd571
mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
4273
diff
changeset
|
256 local stanza_direction = carbon and stanza:child_with_name("sent") and "out" or "in"; |
2976
df86ce6bb0b4
Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents:
2792
diff
changeset
|
257 if carbon then stanza = carbon; end |
4295
d44a8d3dd571
mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
4273
diff
changeset
|
258 local st_type = stanza.attr.type; |
d44a8d3dd571
mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
4273
diff
changeset
|
259 |
2976
df86ce6bb0b4
Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents:
2792
diff
changeset
|
260 -- headline message are always not important |
df86ce6bb0b4
Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents:
2792
diff
changeset
|
261 if st_type == "headline" then return false; end |
4295
d44a8d3dd571
mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
4273
diff
changeset
|
262 |
2976
df86ce6bb0b4
Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents:
2792
diff
changeset
|
263 -- carbon copied outgoing messages are not important |
df86ce6bb0b4
Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents:
2792
diff
changeset
|
264 if carbon and stanza_direction == "out" then return false; end |
4295
d44a8d3dd571
mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
4273
diff
changeset
|
265 |
2976
df86ce6bb0b4
Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents:
2792
diff
changeset
|
266 -- We can't check for body contents in encrypted messages, so let's treat them as important |
df86ce6bb0b4
Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents:
2792
diff
changeset
|
267 -- Some clients don't even set a body or an empty body for encrypted messages |
4295
d44a8d3dd571
mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
4273
diff
changeset
|
268 |
2976
df86ce6bb0b4
Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents:
2792
diff
changeset
|
269 -- check omemo https://xmpp.org/extensions/inbox/omemo.html |
df86ce6bb0b4
Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents:
2792
diff
changeset
|
270 if stanza:get_child("encrypted", "eu.siacs.conversations.axolotl") or stanza:get_child("encrypted", "urn:xmpp:omemo:0") then return true; end |
4295
d44a8d3dd571
mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
4273
diff
changeset
|
271 |
2976
df86ce6bb0b4
Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents:
2792
diff
changeset
|
272 -- check xep27 pgp https://xmpp.org/extensions/xep-0027.html |
df86ce6bb0b4
Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents:
2792
diff
changeset
|
273 if stanza:get_child("x", "jabber:x:encrypted") then return true; end |
4295
d44a8d3dd571
mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
4273
diff
changeset
|
274 |
2976
df86ce6bb0b4
Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents:
2792
diff
changeset
|
275 -- check xep373 pgp (OX) https://xmpp.org/extensions/xep-0373.html |
df86ce6bb0b4
Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents:
2792
diff
changeset
|
276 if stanza:get_child("openpgp", "urn:xmpp:openpgp:0") then return true; end |
4295
d44a8d3dd571
mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
4273
diff
changeset
|
277 |
2976
df86ce6bb0b4
Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents:
2792
diff
changeset
|
278 local body = stanza:get_child_text("body"); |
4295
d44a8d3dd571
mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
4273
diff
changeset
|
279 |
d44a8d3dd571
mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
4273
diff
changeset
|
280 -- groupchat subjects are not important here |
d44a8d3dd571
mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
4273
diff
changeset
|
281 if st_type == "groupchat" and stanza:get_child_text("subject") then |
d44a8d3dd571
mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
4273
diff
changeset
|
282 return false; |
d44a8d3dd571
mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
4273
diff
changeset
|
283 end |
d44a8d3dd571
mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
4273
diff
changeset
|
284 |
d44a8d3dd571
mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
4273
diff
changeset
|
285 -- empty bodies are not important |
d44a8d3dd571
mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
4273
diff
changeset
|
286 return body ~= nil and body ~= ""; |
2736
fff185e7ab73
mod_cloud_notify: Implement the "stripped stanzas" proposal.
tmolitor <thilo@eightysoft.de>
parents:
2714
diff
changeset
|
287 end |
2976
df86ce6bb0b4
Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents:
2792
diff
changeset
|
288 return false; -- this stanza wasn't one of the above cases --> it is not important, too |
2736
fff185e7ab73
mod_cloud_notify: Implement the "stripped stanzas" proposal.
tmolitor <thilo@eightysoft.de>
parents:
2714
diff
changeset
|
289 end |
fff185e7ab73
mod_cloud_notify: Implement the "stripped stanzas" proposal.
tmolitor <thilo@eightysoft.de>
parents:
2714
diff
changeset
|
290 |
1783
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
291 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
|
292 { 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
|
293 { 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
|
294 { 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
|
295 { 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
|
296 { 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
|
297 }; |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
298 |
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
|
299 -- http://xmpp.org/extensions/xep-0357.html#publishing |
3108
cfcb020bcd1d
mod_cloud_notify: inform mod_smacks of first push in hibernated state
tmolitor <thilo@eightysoft.de>
parents:
3085
diff
changeset
|
300 local function handle_notify_request(stanza, node, user_push_services, log_push_decline) |
2712
d89ab70808f6
mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents:
2670
diff
changeset
|
301 local pushes = 0; |
3943
f5e6368a1c39
mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents:
3940
diff
changeset
|
302 if not #user_push_services then return pushes end |
4295
d44a8d3dd571
mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
4273
diff
changeset
|
303 |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
304 for push_identifier, push_info in pairs(user_push_services) do |
2712
d89ab70808f6
mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents:
2670
diff
changeset
|
305 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 |
2625
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
306 if stanza then |
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
307 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
|
308 if stanza._push_notify[push_identifier] then |
3108
cfcb020bcd1d
mod_cloud_notify: inform mod_smacks of first push in hibernated state
tmolitor <thilo@eightysoft.de>
parents:
3085
diff
changeset
|
309 if log_push_decline then |
cfcb020bcd1d
mod_cloud_notify: inform mod_smacks of first push in hibernated state
tmolitor <thilo@eightysoft.de>
parents:
3085
diff
changeset
|
310 module:log("debug", "Already sent push notification for %s@%s to %s (%s)", node, module.host, push_info.jid, tostring(push_info.node)); |
cfcb020bcd1d
mod_cloud_notify: inform mod_smacks of first push in hibernated state
tmolitor <thilo@eightysoft.de>
parents:
3085
diff
changeset
|
311 end |
2712
d89ab70808f6
mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents:
2670
diff
changeset
|
312 send_push = false; |
2625
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
313 end |
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
314 stanza._push_notify[push_identifier] = true; |
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
315 end |
4295
d44a8d3dd571
mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
4273
diff
changeset
|
316 |
2712
d89ab70808f6
mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents:
2670
diff
changeset
|
317 if send_push then |
d89ab70808f6
mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents:
2670
diff
changeset
|
318 -- construct push stanza |
3943
f5e6368a1c39
mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents:
3940
diff
changeset
|
319 local stanza_id = hashes.sha256(random.bytes(8), true); |
2751
6b710a8bdf03
mod_cloud_notify: Implement version 0.3 of XEP-0357
tmolitor <thilo@eightysoft.de>
parents:
2749
diff
changeset
|
320 local push_publish = st.iq({ to = push_info.jid, from = module.host, type = "set", id = stanza_id }) |
2712
d89ab70808f6
mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents:
2670
diff
changeset
|
321 :tag("pubsub", { xmlns = "http://jabber.org/protocol/pubsub" }) |
d89ab70808f6
mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents:
2670
diff
changeset
|
322 :tag("publish", { node = push_info.node }) |
d89ab70808f6
mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents:
2670
diff
changeset
|
323 :tag("item") |
d89ab70808f6
mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents:
2670
diff
changeset
|
324 :tag("notification", { xmlns = xmlns_push }); |
d89ab70808f6
mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents:
2670
diff
changeset
|
325 local form_data = { |
3010
7ee59f417c16
mod_cloud_notify: remove useless counter (hardcode it to 1)
tmolitor <thilo@eightysoft.de>
parents:
2976
diff
changeset
|
326 -- hardcode to 1 because other numbers are just meaningless (the XEP does not specify *what exactly* to count) |
7ee59f417c16
mod_cloud_notify: remove useless counter (hardcode it to 1)
tmolitor <thilo@eightysoft.de>
parents:
2976
diff
changeset
|
327 ["message-count"] = "1"; |
2712
d89ab70808f6
mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents:
2670
diff
changeset
|
328 }; |
d89ab70808f6
mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents:
2670
diff
changeset
|
329 if stanza and include_sender then |
d89ab70808f6
mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents:
2670
diff
changeset
|
330 form_data["last-message-sender"] = stanza.attr.from; |
d89ab70808f6
mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents:
2670
diff
changeset
|
331 end |
d89ab70808f6
mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents:
2670
diff
changeset
|
332 if stanza and include_body then |
d89ab70808f6
mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents:
2670
diff
changeset
|
333 form_data["last-message-body"] = stanza:get_child_text("body"); |
3108
cfcb020bcd1d
mod_cloud_notify: inform mod_smacks of first push in hibernated state
tmolitor <thilo@eightysoft.de>
parents:
3085
diff
changeset
|
334 elseif stanza and dummy_body and is_important(stanza) then |
cfcb020bcd1d
mod_cloud_notify: inform mod_smacks of first push in hibernated state
tmolitor <thilo@eightysoft.de>
parents:
3085
diff
changeset
|
335 form_data["last-message-body"] = tostring(dummy_body); |
2712
d89ab70808f6
mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents:
2670
diff
changeset
|
336 end |
d89ab70808f6
mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents:
2670
diff
changeset
|
337 push_publish:add_child(push_form:form(form_data)); |
d89ab70808f6
mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents:
2670
diff
changeset
|
338 push_publish:up(); -- / notification |
d89ab70808f6
mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents:
2670
diff
changeset
|
339 push_publish:up(); -- / publish |
d89ab70808f6
mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents:
2670
diff
changeset
|
340 push_publish:up(); -- / pubsub |
d89ab70808f6
mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents:
2670
diff
changeset
|
341 if push_info.options then |
d89ab70808f6
mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents:
2670
diff
changeset
|
342 push_publish:tag("publish-options"):add_child(st.deserialize(push_info.options)); |
d89ab70808f6
mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents:
2670
diff
changeset
|
343 end |
d89ab70808f6
mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents:
2670
diff
changeset
|
344 -- send out push |
4295
d44a8d3dd571
mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
4273
diff
changeset
|
345 module:log("debug", "Sending %s push notification for %s@%s to %s (%s)", |
d44a8d3dd571
mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
4273
diff
changeset
|
346 form_data["last-message-body"] and "important" or "unimportant", |
d44a8d3dd571
mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
4273
diff
changeset
|
347 node, module.host, push_info.jid, tostring(push_info.node) |
d44a8d3dd571
mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
4273
diff
changeset
|
348 ); |
2736
fff185e7ab73
mod_cloud_notify: Implement the "stripped stanzas" proposal.
tmolitor <thilo@eightysoft.de>
parents:
2714
diff
changeset
|
349 -- module:log("debug", "PUSH STANZA: %s", tostring(push_publish)); |
2712
d89ab70808f6
mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents:
2670
diff
changeset
|
350 -- handle push errors for this node |
d89ab70808f6
mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents:
2670
diff
changeset
|
351 if push_errors[push_identifier] == nil then |
d89ab70808f6
mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents:
2670
diff
changeset
|
352 push_errors[push_identifier] = 0; |
d89ab70808f6
mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents:
2670
diff
changeset
|
353 end |
3943
f5e6368a1c39
mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents:
3940
diff
changeset
|
354 module:hook("iq-error/host/"..stanza_id, handle_push_error); |
f5e6368a1c39
mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents:
3940
diff
changeset
|
355 module:hook("iq-result/host/"..stanza_id, handle_push_success); |
f5e6368a1c39
mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents:
3940
diff
changeset
|
356 id2node[stanza_id] = node; |
f5e6368a1c39
mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents:
3940
diff
changeset
|
357 id2identifier[stanza_id] = push_identifier; |
2712
d89ab70808f6
mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents:
2670
diff
changeset
|
358 module:send(push_publish); |
d89ab70808f6
mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents:
2670
diff
changeset
|
359 pushes = pushes + 1; |
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
|
360 end |
1783
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
361 end |
2712
d89ab70808f6
mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents:
2670
diff
changeset
|
362 return pushes; |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
363 end |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
364 |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
365 -- 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
|
366 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
|
367 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
|
368 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
|
369 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
|
370 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
|
371 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
|
372 |
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
|
373 -- 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
|
374 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
|
375 local node, user_push_services = get_push_settings(event.stanza, event.origin); |
2736
fff185e7ab73
mod_cloud_notify: Implement the "stripped stanzas" proposal.
tmolitor <thilo@eightysoft.de>
parents:
2714
diff
changeset
|
376 module:log("debug", "Invoking cloud handle_notify_request() for offline stanza"); |
3108
cfcb020bcd1d
mod_cloud_notify: inform mod_smacks of first push in hibernated state
tmolitor <thilo@eightysoft.de>
parents:
3085
diff
changeset
|
377 handle_notify_request(event.stanza, node, user_push_services, true); |
1783
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
378 end, 1); |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
379 |
3619
74aa35aeb08a
mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents:
3108
diff
changeset
|
380 local function process_stanza_queue(queue, session, queue_type) |
2714
75b137cf869a
mod_cloud_notify: Don't notify for all smacks queued stanzas in a row
tmolitor <thilo@eightysoft.de>
parents:
2712
diff
changeset
|
381 if not session.push_identifier then return; end |
75b137cf869a
mod_cloud_notify: Don't notify for all smacks queued stanzas in a row
tmolitor <thilo@eightysoft.de>
parents:
2712
diff
changeset
|
382 local user_push_services = {[session.push_identifier] = session.push_settings}; |
3108
cfcb020bcd1d
mod_cloud_notify: inform mod_smacks of first push in hibernated state
tmolitor <thilo@eightysoft.de>
parents:
3085
diff
changeset
|
383 local notified = { unimportant = false; important = false } |
2714
75b137cf869a
mod_cloud_notify: Don't notify for all smacks queued stanzas in a row
tmolitor <thilo@eightysoft.de>
parents:
2712
diff
changeset
|
384 for i=1, #queue do |
75b137cf869a
mod_cloud_notify: Don't notify for all smacks queued stanzas in a row
tmolitor <thilo@eightysoft.de>
parents:
2712
diff
changeset
|
385 local stanza = queue[i]; |
3979
6bf362008052
mod_cloud_notify: make stanza queue processing faster
tmolitor <thilo@eightysoft.de>
parents:
3943
diff
changeset
|
386 -- fast ignore of already pushed stanzas |
6bf362008052
mod_cloud_notify: make stanza queue processing faster
tmolitor <thilo@eightysoft.de>
parents:
3943
diff
changeset
|
387 if stanza and not (stanza._push_notify and stanza._push_notify[session.push_identifier]) then |
6bf362008052
mod_cloud_notify: make stanza queue processing faster
tmolitor <thilo@eightysoft.de>
parents:
3943
diff
changeset
|
388 local node = get_push_settings(stanza, session); |
4295
d44a8d3dd571
mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
4273
diff
changeset
|
389 local stanza_type = "unimportant"; |
3979
6bf362008052
mod_cloud_notify: make stanza queue processing faster
tmolitor <thilo@eightysoft.de>
parents:
3943
diff
changeset
|
390 if dummy_body and is_important(stanza) then stanza_type = "important"; end |
6bf362008052
mod_cloud_notify: make stanza queue processing faster
tmolitor <thilo@eightysoft.de>
parents:
3943
diff
changeset
|
391 if not notified[stanza_type] then -- only notify if we didn't try to push for this stanza type already |
6bf362008052
mod_cloud_notify: make stanza queue processing faster
tmolitor <thilo@eightysoft.de>
parents:
3943
diff
changeset
|
392 -- session.log("debug", "Invoking cloud handle_notify_request() for smacks queued stanza: %d", i); |
6bf362008052
mod_cloud_notify: make stanza queue processing faster
tmolitor <thilo@eightysoft.de>
parents:
3943
diff
changeset
|
393 if handle_notify_request(stanza, node, user_push_services, false) ~= 0 then |
6bf362008052
mod_cloud_notify: make stanza queue processing faster
tmolitor <thilo@eightysoft.de>
parents:
3943
diff
changeset
|
394 if session.hibernating and not session.first_hibernated_push then |
6bf362008052
mod_cloud_notify: make stanza queue processing faster
tmolitor <thilo@eightysoft.de>
parents:
3943
diff
changeset
|
395 -- if important stanzas are treated differently (pushed with last-message-body field set to dummy string) |
6bf362008052
mod_cloud_notify: make stanza queue processing faster
tmolitor <thilo@eightysoft.de>
parents:
3943
diff
changeset
|
396 -- if the message was important (e.g. had a last-message-body field) OR if we treat all pushes equally, |
6bf362008052
mod_cloud_notify: make stanza queue processing faster
tmolitor <thilo@eightysoft.de>
parents:
3943
diff
changeset
|
397 -- then record the time of first push in the session for the smack module which will extend its hibernation |
6bf362008052
mod_cloud_notify: make stanza queue processing faster
tmolitor <thilo@eightysoft.de>
parents:
3943
diff
changeset
|
398 -- timeout based on the value of session.first_hibernated_push |
6bf362008052
mod_cloud_notify: make stanza queue processing faster
tmolitor <thilo@eightysoft.de>
parents:
3943
diff
changeset
|
399 if not dummy_body or (dummy_body and is_important(stanza)) then |
6bf362008052
mod_cloud_notify: make stanza queue processing faster
tmolitor <thilo@eightysoft.de>
parents:
3943
diff
changeset
|
400 session.first_hibernated_push = os_time(); |
6bf362008052
mod_cloud_notify: make stanza queue processing faster
tmolitor <thilo@eightysoft.de>
parents:
3943
diff
changeset
|
401 end |
3108
cfcb020bcd1d
mod_cloud_notify: inform mod_smacks of first push in hibernated state
tmolitor <thilo@eightysoft.de>
parents:
3085
diff
changeset
|
402 end |
3979
6bf362008052
mod_cloud_notify: make stanza queue processing faster
tmolitor <thilo@eightysoft.de>
parents:
3943
diff
changeset
|
403 session.log("debug", "Cloud handle_notify_request() > 0, not notifying for other %s queued stanzas of type %s", queue_type, stanza_type); |
6bf362008052
mod_cloud_notify: make stanza queue processing faster
tmolitor <thilo@eightysoft.de>
parents:
3943
diff
changeset
|
404 notified[stanza_type] = true |
3108
cfcb020bcd1d
mod_cloud_notify: inform mod_smacks of first push in hibernated state
tmolitor <thilo@eightysoft.de>
parents:
3085
diff
changeset
|
405 end |
cfcb020bcd1d
mod_cloud_notify: inform mod_smacks of first push in hibernated state
tmolitor <thilo@eightysoft.de>
parents:
3085
diff
changeset
|
406 end |
2714
75b137cf869a
mod_cloud_notify: Don't notify for all smacks queued stanzas in a row
tmolitor <thilo@eightysoft.de>
parents:
2712
diff
changeset
|
407 end |
3979
6bf362008052
mod_cloud_notify: make stanza queue processing faster
tmolitor <thilo@eightysoft.de>
parents:
3943
diff
changeset
|
408 if notified.unimportant and notified.important then break; end -- stop processing the queue if all push types are exhausted |
2714
75b137cf869a
mod_cloud_notify: Don't notify for all smacks queued stanzas in a row
tmolitor <thilo@eightysoft.de>
parents:
2712
diff
changeset
|
409 end |
75b137cf869a
mod_cloud_notify: Don't notify for all smacks queued stanzas in a row
tmolitor <thilo@eightysoft.de>
parents:
2712
diff
changeset
|
410 end |
75b137cf869a
mod_cloud_notify: Don't notify for all smacks queued stanzas in a row
tmolitor <thilo@eightysoft.de>
parents:
2712
diff
changeset
|
411 |
3619
74aa35aeb08a
mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents:
3108
diff
changeset
|
412 -- publish on unacked smacks message (use timer to send out push for all stanzas submitted in a row only once) |
4273
8bf83e883593
mod_cloud_notify: Modernize interface to mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
4221
diff
changeset
|
413 local function process_smacks_stanza(event) |
8bf83e883593
mod_cloud_notify: Modernize interface to mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
4221
diff
changeset
|
414 local session = event.origin; |
8bf83e883593
mod_cloud_notify: Modernize interface to mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
4221
diff
changeset
|
415 local stanza = event.stanza; |
3619
74aa35aeb08a
mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents:
3108
diff
changeset
|
416 if session.push_identifier then |
4273
8bf83e883593
mod_cloud_notify: Modernize interface to mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
4221
diff
changeset
|
417 session.log("debug", "adding new stanza to push_queue"); |
3619
74aa35aeb08a
mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents:
3108
diff
changeset
|
418 if not session.push_queue then session.push_queue = {}; end |
74aa35aeb08a
mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents:
3108
diff
changeset
|
419 local queue = session.push_queue; |
74aa35aeb08a
mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents:
3108
diff
changeset
|
420 queue[#queue+1] = st.clone(stanza); |
74aa35aeb08a
mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents:
3108
diff
changeset
|
421 if #queue == 1 then -- first stanza --> start timer |
74aa35aeb08a
mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents:
3108
diff
changeset
|
422 session.log("debug", "Invoking cloud handle_notify_request() for newly smacks queued stanza (in a moment)"); |
3943
f5e6368a1c39
mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents:
3940
diff
changeset
|
423 session.awaiting_push_timer = module:add_timer(1e-06, function () |
3619
74aa35aeb08a
mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents:
3108
diff
changeset
|
424 session.log("debug", "Invoking cloud handle_notify_request() for newly smacks queued stanzas (now in timer)"); |
74aa35aeb08a
mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents:
3108
diff
changeset
|
425 process_stanza_queue(session.push_queue, session, "push"); |
74aa35aeb08a
mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents:
3108
diff
changeset
|
426 session.push_queue = {}; -- clean up queue after push |
74aa35aeb08a
mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents:
3108
diff
changeset
|
427 end); |
74aa35aeb08a
mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents:
3108
diff
changeset
|
428 end |
4273
8bf83e883593
mod_cloud_notify: Modernize interface to mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
4221
diff
changeset
|
429 else |
4295
d44a8d3dd571
mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
4273
diff
changeset
|
430 session.log("debug", "NOT invoking cloud handle_notify_request() for newly smacks queued stanza (session.push_identifier is not set: %s)", |
d44a8d3dd571
mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
4273
diff
changeset
|
431 session.push_identifier |
d44a8d3dd571
mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
4273
diff
changeset
|
432 ); |
3619
74aa35aeb08a
mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents:
3108
diff
changeset
|
433 end |
74aa35aeb08a
mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents:
3108
diff
changeset
|
434 return stanza; |
74aa35aeb08a
mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents:
3108
diff
changeset
|
435 end |
74aa35aeb08a
mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents:
3108
diff
changeset
|
436 |
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
|
437 -- 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
|
438 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
|
439 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
|
440 local queue = event.queue; |
3108
cfcb020bcd1d
mod_cloud_notify: inform mod_smacks of first push in hibernated state
tmolitor <thilo@eightysoft.de>
parents:
3085
diff
changeset
|
441 session.first_hibernated_push = nil; |
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
|
442 -- process unacked stanzas |
3619
74aa35aeb08a
mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents:
3108
diff
changeset
|
443 process_stanza_queue(queue, session, "smacks"); |
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
|
444 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
|
445 |
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
|
446 -- 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
|
447 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
|
448 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
|
449 if session then -- older smacks module versions send only the "intermediate" session in event.session and no session.resumed one |
3619
74aa35aeb08a
mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents:
3108
diff
changeset
|
450 if session.awaiting_push_timer then session.awaiting_push_timer:stop(); end |
3108
cfcb020bcd1d
mod_cloud_notify: inform mod_smacks of first push in hibernated state
tmolitor <thilo@eightysoft.de>
parents:
3085
diff
changeset
|
451 session.first_hibernated_push = nil; |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
452 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
|
453 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
|
454 |
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
|
455 -- 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
|
456 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
|
457 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
|
458 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
|
459 -- process unacked stanzas (handle_notify_request() will only send push requests for new stanzas) |
3619
74aa35aeb08a
mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents:
3108
diff
changeset
|
460 process_stanza_queue(queue, session, "smacks"); |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
461 end |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
462 |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
463 -- 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
|
464 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
|
465 -- event is: { origin = origin, stanza = stanza, for_user = store_user, id = id } |
3055
6abee021d9db
mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents:
3010
diff
changeset
|
466 -- only notify for new mam messages when at least one device is online |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
467 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
|
468 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
|
469 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
|
470 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
|
471 to = to and jid.split(to) or event.origin.username; |
2643
777d07e0cd73
mod_cloud_notify: Whitespace fixes
Matthew Wild <mwild1@gmail.com>
parents:
2642
diff
changeset
|
472 |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
473 -- 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
|
474 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
|
475 local user_push_services = push_store:get(to); |
4295
d44a8d3dd571
mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
4273
diff
changeset
|
476 |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
477 -- only notify nodes with no active sessions (smacks is counted as active and handled separate) |
3108
cfcb020bcd1d
mod_cloud_notify: inform mod_smacks of first push in hibernated state
tmolitor <thilo@eightysoft.de>
parents:
3085
diff
changeset
|
478 local notify_push_services = {}; |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
479 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
|
480 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
|
481 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
|
482 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
|
483 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
|
484 break; |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
485 end |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
486 end |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
487 if identifier_found then |
2714
75b137cf869a
mod_cloud_notify: Don't notify for all smacks queued stanzas in a row
tmolitor <thilo@eightysoft.de>
parents:
2712
diff
changeset
|
488 identifier_found.log("debug", "Not cloud notifying '%s' of new MAM stanza (session still alive)", identifier); |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
489 else |
3108
cfcb020bcd1d
mod_cloud_notify: inform mod_smacks of first push in hibernated state
tmolitor <thilo@eightysoft.de>
parents:
3085
diff
changeset
|
490 notify_push_services[identifier] = push_info; |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
491 end |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
492 end |
2643
777d07e0cd73
mod_cloud_notify: Whitespace fixes
Matthew Wild <mwild1@gmail.com>
parents:
2642
diff
changeset
|
493 |
3108
cfcb020bcd1d
mod_cloud_notify: inform mod_smacks of first push in hibernated state
tmolitor <thilo@eightysoft.de>
parents:
3085
diff
changeset
|
494 handle_notify_request(event.stanza, to, notify_push_services, true); |
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
|
495 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
|
496 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
|
497 |
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
|
498 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
|
499 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
|
500 module:hook("smacks-ack-delayed", ack_delayed); |
4273
8bf83e883593
mod_cloud_notify: Modernize interface to mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
4221
diff
changeset
|
501 module:hook("smacks-hibernation-stanza-queued", process_smacks_stanza); |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
502 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
|
503 |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
504 local function send_ping(event) |
3626
c84bbf36c878
mod_cloud_notify: fix local variable usage
tmolitor <thilo@eightysoft.de>
parents:
3622
diff
changeset
|
505 local user = event.user; |
3627
9639c493f4b9
mod_cloud_notify: fix typo (used || instead of or)
tmolitor <thilo@eightysoft.de>
parents:
3626
diff
changeset
|
506 local push_services = event.push_services or push_store:get(user); |
3943
f5e6368a1c39
mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents:
3940
diff
changeset
|
507 module:log("debug", "Handling event 'cloud-notify-ping' for user '%s'", user); |
f5e6368a1c39
mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents:
3940
diff
changeset
|
508 local retval = handle_notify_request(nil, user, push_services, true); |
f5e6368a1c39
mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents:
3940
diff
changeset
|
509 module:log("debug", "handle_notify_request() returned %s", tostring(retval)); |
2609
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
510 end |
6ab46ff685d0
mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents:
2395
diff
changeset
|
511 -- 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
|
512 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
|
513 |
2625
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
514 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
|
515 function module.unload() |
4273
8bf83e883593
mod_cloud_notify: Modernize interface to mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
4221
diff
changeset
|
516 module:log("info", "Unloading module"); |
8bf83e883593
mod_cloud_notify: Modernize interface to mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
4221
diff
changeset
|
517 -- cleanup some settings, reloading this module can cause process_smacks_stanza() to stop working otherwise |
8bf83e883593
mod_cloud_notify: Modernize interface to mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
4221
diff
changeset
|
518 for user, _ in pairs(host_sessions) do |
4295
d44a8d3dd571
mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
4273
diff
changeset
|
519 for _, session in pairs(host_sessions[user].sessions) do |
4273
8bf83e883593
mod_cloud_notify: Modernize interface to mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
4221
diff
changeset
|
520 if session.awaiting_push_timer then session.awaiting_push_timer:stop(); end |
8bf83e883593
mod_cloud_notify: Modernize interface to mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
4221
diff
changeset
|
521 session.awaiting_push_timer = nil; |
8bf83e883593
mod_cloud_notify: Modernize interface to mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
4221
diff
changeset
|
522 session.first_hibernated_push = nil; |
8bf83e883593
mod_cloud_notify: Modernize interface to mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
4221
diff
changeset
|
523 session.push_queue = nil; |
8bf83e883593
mod_cloud_notify: Modernize interface to mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
4221
diff
changeset
|
524 end |
8bf83e883593
mod_cloud_notify: Modernize interface to mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
4221
diff
changeset
|
525 end |
2625
8c6562f16496
mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents:
2609
diff
changeset
|
526 module:log("info", "Module unloaded"); |
2643
777d07e0cd73
mod_cloud_notify: Whitespace fixes
Matthew Wild <mwild1@gmail.com>
parents:
2642
diff
changeset
|
527 end |