Mercurial > prosody-hg
annotate plugins/mod_cloud_notify.lua @ 14176:2bcabbeac9b0 13.0
util.ringbuffer: Fix incorrect returned position from :find() for #needle~=1
And add a test for :find()
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Mon, 25 May 2026 14:41:06 +0100 |
| parents | f149652cb5ff |
| children |
| rev | line source |
|---|---|
|
13616
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 -- XEP-0357: Push (aka: My mobile OS vendor won't let me have persistent TCP connections) |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 -- Copyright (C) 2015-2016 Kim Alvefur |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 -- Copyright (C) 2017-2019 Thilo Molitor |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 -- |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 -- This file is MIT/X11 licensed. |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 local os_time = os.time; |
|
13701
1aa7efabeacb
mod_cloud_notify, mod_cron, mod_invites: Add 'prosody.' prefix to requires
Matthew Wild <mwild1@gmail.com>
parents:
13616
diff
changeset
|
8 local st = require"prosody.util.stanza"; |
|
1aa7efabeacb
mod_cloud_notify, mod_cron, mod_invites: Add 'prosody.' prefix to requires
Matthew Wild <mwild1@gmail.com>
parents:
13616
diff
changeset
|
9 local jid = require"prosody.util.jid"; |
|
1aa7efabeacb
mod_cloud_notify, mod_cron, mod_invites: Add 'prosody.' prefix to requires
Matthew Wild <mwild1@gmail.com>
parents:
13616
diff
changeset
|
10 local dataform = require"prosody.util.dataforms".new; |
|
1aa7efabeacb
mod_cloud_notify, mod_cron, mod_invites: Add 'prosody.' prefix to requires
Matthew Wild <mwild1@gmail.com>
parents:
13616
diff
changeset
|
11 local hashes = require"prosody.util.hashes"; |
|
1aa7efabeacb
mod_cloud_notify, mod_cron, mod_invites: Add 'prosody.' prefix to requires
Matthew Wild <mwild1@gmail.com>
parents:
13616
diff
changeset
|
12 local random = require"prosody.util.random"; |
|
1aa7efabeacb
mod_cloud_notify, mod_cron, mod_invites: Add 'prosody.' prefix to requires
Matthew Wild <mwild1@gmail.com>
parents:
13616
diff
changeset
|
13 local cache = require"prosody.util.cache"; |
|
1aa7efabeacb
mod_cloud_notify, mod_cron, mod_invites: Add 'prosody.' prefix to requires
Matthew Wild <mwild1@gmail.com>
parents:
13616
diff
changeset
|
14 local watchdog = require "prosody.util.watchdog"; |
|
13616
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 local xmlns_push = "urn:xmpp:push:0"; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 -- configuration |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 local include_body = module:get_option_boolean("push_notification_with_body", false); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 local include_sender = module:get_option_boolean("push_notification_with_sender", false); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 local max_push_errors = module:get_option_number("push_max_errors", 16); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 local max_push_devices = module:get_option_number("push_max_devices", 5); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 local dummy_body = module:get_option_string("push_notification_important_body", "New Message!"); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 local extended_hibernation_timeout = module:get_option_number("push_max_hibernation_timeout", 72*3600); -- use same timeout like ejabberd |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 local host_sessions = prosody.hosts[module.host].sessions; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 local push_errors = module:shared("push_errors"); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 local id2node = {}; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 local id2identifier = {}; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 -- For keeping state across reloads while caching reads |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 -- This uses util.cache for caching the most recent devices and removing all old devices when max_push_devices is reached |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 local push_store = (function() |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 local store = module:open_store(); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 local push_services = {}; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 local api = {}; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 --luacheck: ignore 212/self |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 function api:get(user) |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 if not push_services[user] then |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 local loaded, err = store:get(user); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 if not loaded and err then |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 module:log("warn", "Error reading push notification storage for user '%s': %s", user, tostring(err)); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 push_services[user] = cache.new(max_push_devices):table(); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 return push_services[user], false; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 if loaded then |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 push_services[user] = cache.new(max_push_devices):table(); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 -- copy over plain table loaded from disk into our cache |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 for k, v in pairs(loaded) do push_services[user][k] = v; end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 else |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 push_services[user] = cache.new(max_push_devices):table(); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 return push_services[user], true; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 function api:flush_to_disk(user) |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 local plain_table = {}; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 for k, v in pairs(push_services[user]) do plain_table[k] = v; end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 local ok, err = store:set(user, plain_table); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 if not ok then |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 module:log("error", "Error writing push notification storage for user '%s': %s", user, tostring(err)); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 return false; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 return true; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 function api:set_identifier(user, push_identifier, data) |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 local services = self:get(user); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 services[push_identifier] = data; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 return api; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 end)(); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 -- Forward declarations, as both functions need to reference each other |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 local handle_push_success, handle_push_error; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
76 |
|
14134
041c7ff18f76
mod_cloud_notify: Fix leaking iq response handlers by using send_iq()
Matthew Wild <mwild1@gmail.com>
parents:
13701
diff
changeset
|
77 function handle_push_error(stanza) |
|
13616
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
78 local error_type, condition, error_text = stanza:get_error(); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
79 local node = id2node[stanza.attr.id]; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
80 local identifier = id2identifier[stanza.attr.id]; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
81 if node == nil then |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
82 module:log("warn", "Received push error with unrecognised id: %s", stanza.attr.id); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
83 return false; -- unknown stanza? Ignore for now! |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
84 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
85 local from = stanza.attr.from; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
86 local user_push_services = push_store:get(node); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
87 local found, changed = false, false; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
88 |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
89 for push_identifier, _ in pairs(user_push_services) do |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
90 if push_identifier == identifier then |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
91 found = true; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
92 if user_push_services[push_identifier] and user_push_services[push_identifier].jid == from and error_type ~= "wait" then |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
93 push_errors[push_identifier] = push_errors[push_identifier] + 1; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
94 module:log("info", "Got error <%s:%s:%s> for identifier '%s': " |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
95 .."error count for this identifier is now at %s", error_type, condition, error_text or "", push_identifier, |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
96 tostring(push_errors[push_identifier])); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
97 if push_errors[push_identifier] >= max_push_errors then |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
98 module:log("warn", "Disabling push notifications for identifier '%s'", push_identifier); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
99 -- remove push settings from sessions |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
100 if host_sessions[node] then |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
101 for _, session in pairs(host_sessions[node].sessions) do |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
102 if session.push_identifier == push_identifier then |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
103 session.push_identifier = nil; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
104 session.push_settings = nil; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
105 session.first_hibernated_push = nil; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
106 -- check for prosody 0.12 mod_smacks |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
107 if session.hibernating_watchdog and session.original_smacks_callback and session.original_smacks_timeout then |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
108 -- restore old smacks watchdog |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
109 session.hibernating_watchdog:cancel(); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
110 session.hibernating_watchdog = watchdog.new(session.original_smacks_timeout, session.original_smacks_callback); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
111 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
112 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
113 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
114 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
115 -- save changed global config |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
116 changed = true; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
117 user_push_services[push_identifier] = nil |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
118 push_errors[push_identifier] = nil; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
119 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
120 elseif user_push_services[push_identifier] and user_push_services[push_identifier].jid == from and error_type == "wait" then |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
121 module:log("debug", "Got error <%s:%s:%s> for identifier '%s': " |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
122 .."NOT increasing error count for this identifier", error_type, condition, error_text or "", push_identifier); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
123 else |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
124 module:log("debug", "Unhandled push error <%s:%s:%s> from %s for identifier '%s'", |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
125 error_type, condition, error_text or "", from, push_identifier |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
126 ); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
127 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
128 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
129 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
130 if changed then |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
131 push_store:flush_to_disk(node); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
132 elseif not found then |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
133 module:log("warn", "Unable to find matching registration for push error <%s:%s:%s> from %s", error_type, condition, error_text or "", from); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
134 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
135 return true; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
136 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
137 |
|
14134
041c7ff18f76
mod_cloud_notify: Fix leaking iq response handlers by using send_iq()
Matthew Wild <mwild1@gmail.com>
parents:
13701
diff
changeset
|
138 function handle_push_success(stanza) |
|
13616
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
139 local node = id2node[stanza.attr.id]; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
140 local identifier = id2identifier[stanza.attr.id]; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
141 if node == nil then return false; end -- unknown stanza? Ignore for now! |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
142 local from = stanza.attr.from; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
143 local user_push_services = push_store:get(node); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
144 |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
145 for push_identifier, _ in pairs(user_push_services) do |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
146 if push_identifier == identifier then |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
147 if user_push_services[push_identifier] and user_push_services[push_identifier].jid == from and push_errors[push_identifier] > 0 then |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
148 push_errors[push_identifier] = 0; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
149 module:log("debug", "Push succeeded, error count for identifier '%s' is now at %s again", |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
150 push_identifier, tostring(push_errors[push_identifier]) |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
151 ); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
152 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
153 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
154 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
155 return true; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
156 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
157 |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
158 -- http://xmpp.org/extensions/xep-0357.html#disco |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
159 local function account_dico_info(event) |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
160 (event.reply or event.stanza):tag("feature", {var=xmlns_push}):up(); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
161 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
162 module:hook("account-disco-info", account_dico_info); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
163 |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
164 -- http://xmpp.org/extensions/xep-0357.html#enabling |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
165 local function push_enable(event) |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
166 local origin, stanza = event.origin, event.stanza; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
167 local enable = stanza.tags[1]; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
168 origin.log("debug", "Attempting to enable push notifications"); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
169 -- MUST contain a 'jid' attribute of the XMPP Push Service being enabled |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
170 local push_jid = enable.attr.jid; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
171 -- SHOULD contain a 'node' attribute |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
172 local push_node = enable.attr.node; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
173 -- CAN contain a 'include_payload' attribute |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
174 local include_payload = enable.attr.include_payload; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
175 if not push_jid then |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
176 origin.log("debug", "Push notification enable request missing the 'jid' field"); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
177 origin.send(st.error_reply(stanza, "modify", "bad-request", "Missing jid")); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
178 return true; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
179 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
180 if push_jid == stanza.attr.from then |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
181 origin.log("debug", "Push notification enable request 'jid' field identical to our own"); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
182 origin.send(st.error_reply(stanza, "modify", "bad-request", "JID must be different from ours")); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
183 return true; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
184 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
185 local publish_options = enable:get_child("x", "jabber:x:data"); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
186 if not publish_options then |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
187 -- Could be intentional |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
188 origin.log("debug", "No publish options in request"); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
189 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
190 local push_identifier = push_jid .. "<" .. (push_node or ""); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
191 local push_service = { |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
192 jid = push_jid; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
193 node = push_node; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
194 include_payload = include_payload; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
195 options = publish_options and st.preserialize(publish_options); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
196 timestamp = os_time(); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
197 client_id = origin.client_id; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
198 resource = not origin.client_id and origin.resource or nil; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
199 language = stanza.attr["xml:lang"]; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
200 }; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
201 local allow_registration = module:fire_event("cloud_notify/registration", { |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
202 origin = origin, stanza = stanza, push_info = push_service; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
203 }); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
204 if allow_registration == false then |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
205 return true; -- Assume error reply already sent |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
206 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
207 push_store:set_identifier(origin.username, push_identifier, push_service); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
208 local ok = push_store:flush_to_disk(origin.username); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
209 if not ok then |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
210 origin.send(st.error_reply(stanza, "wait", "internal-server-error")); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
211 else |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
212 origin.push_identifier = push_identifier; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
213 origin.push_settings = push_service; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
214 origin.first_hibernated_push = nil; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
215 origin.log("info", "Push notifications enabled for %s (%s)", tostring(stanza.attr.from), tostring(origin.push_identifier)); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
216 origin.send(st.reply(stanza)); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
217 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
218 return true; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
219 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
220 module:hook("iq-set/self/"..xmlns_push..":enable", push_enable); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
221 |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
222 -- http://xmpp.org/extensions/xep-0357.html#disabling |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
223 local function push_disable(event) |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
224 local origin, stanza = event.origin, event.stanza; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
225 local push_jid = stanza.tags[1].attr.jid; -- MUST include a 'jid' attribute |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
226 local push_node = stanza.tags[1].attr.node; -- A 'node' attribute MAY be included |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
227 if not push_jid then |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
228 origin.send(st.error_reply(stanza, "modify", "bad-request", "Missing jid")); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
229 return true; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
230 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
231 local user_push_services = push_store:get(origin.username); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
232 for key, push_info in pairs(user_push_services) do |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
233 if push_info.jid == push_jid and (not push_node or push_info.node == push_node) then |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
234 origin.log("info", "Push notifications disabled (%s)", tostring(key)); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
235 if origin.push_identifier == key then |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
236 origin.push_identifier = nil; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
237 origin.push_settings = nil; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
238 origin.first_hibernated_push = nil; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
239 -- check for prosody 0.12 mod_smacks |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
240 if origin.hibernating_watchdog and origin.original_smacks_callback and origin.original_smacks_timeout then |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
241 -- restore old smacks watchdog |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
242 origin.hibernating_watchdog:cancel(); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
243 origin.hibernating_watchdog = watchdog.new(origin.original_smacks_timeout, origin.original_smacks_callback); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
244 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
245 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
246 user_push_services[key] = nil; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
247 push_errors[key] = nil; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
248 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
249 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
250 local ok = push_store:flush_to_disk(origin.username); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
251 if not ok then |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
252 origin.send(st.error_reply(stanza, "wait", "internal-server-error")); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
253 else |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
254 origin.send(st.reply(stanza)); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
255 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
256 return true; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
257 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
258 module:hook("iq-set/self/"..xmlns_push..":disable", push_disable); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
259 |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
260 -- urgent stanzas should be delivered without delay |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
261 local function is_urgent(stanza) |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
262 -- TODO |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
263 if stanza.name == "message" then |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
264 if stanza:get_child("propose", "urn:xmpp:jingle-message:0") then |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
265 return true, "jingle call"; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
266 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
267 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
268 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
269 |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
270 -- is this push a high priority one (this is needed for ios apps not using voip pushes) |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
271 local function is_important(stanza) |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
272 local st_name = stanza and stanza.name or nil; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
273 if not st_name then return false; end -- nonzas are never important here |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
274 if st_name == "presence" then |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
275 return false; -- same for presences |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
276 elseif st_name == "message" then |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
277 -- unpack carbon copied message stanzas |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
278 local carbon = stanza:find("{urn:xmpp:carbons:2}/{urn:xmpp:forward:0}/{jabber:client}message"); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
279 local stanza_direction = carbon and stanza:child_with_name("sent") and "out" or "in"; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
280 if carbon then stanza = carbon; end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
281 local st_type = stanza.attr.type; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
282 |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
283 -- headline message are always not important |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
284 if st_type == "headline" then return false; end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
285 |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
286 -- carbon copied outgoing messages are not important |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
287 if carbon and stanza_direction == "out" then return false; end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
288 |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
289 -- We can't check for body contents in encrypted messages, so let's treat them as important |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
290 -- Some clients don't even set a body or an empty body for encrypted messages |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
291 |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
292 -- check omemo https://xmpp.org/extensions/inbox/omemo.html |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
293 if stanza:get_child("encrypted", "eu.siacs.conversations.axolotl") or stanza:get_child("encrypted", "urn:xmpp:omemo:0") then return true; end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
294 |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
295 -- check xep27 pgp https://xmpp.org/extensions/xep-0027.html |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
296 if stanza:get_child("x", "jabber:x:encrypted") then return true; end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
297 |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
298 -- check xep373 pgp (OX) https://xmpp.org/extensions/xep-0373.html |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
299 if stanza:get_child("openpgp", "urn:xmpp:openpgp:0") then return true; end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
300 |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
301 -- XEP-0353: Jingle Message Initiation (incoming call request) |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
302 if stanza:get_child("propose", "urn:xmpp:jingle-message:0") then return true; end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
303 |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
304 local body = stanza:get_child_text("body"); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
305 |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
306 -- groupchat subjects are not important here |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
307 if st_type == "groupchat" and stanza:get_child_text("subject") then |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
308 return false; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
309 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
310 |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
311 -- empty bodies are not important |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
312 return body ~= nil and body ~= ""; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
313 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
314 return false; -- this stanza wasn't one of the above cases --> it is not important, too |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
315 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
316 |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
317 local push_form = dataform { |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
318 { name = "FORM_TYPE"; type = "hidden"; value = "urn:xmpp:push:summary"; }; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
319 { name = "message-count"; type = "text-single"; }; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
320 { name = "pending-subscription-count"; type = "text-single"; }; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
321 { name = "last-message-sender"; type = "jid-single"; }; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
322 { name = "last-message-body"; type = "text-single"; }; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
323 }; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
324 |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
325 -- http://xmpp.org/extensions/xep-0357.html#publishing |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
326 local function handle_notify_request(stanza, node, user_push_services, log_push_decline) |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
327 local pushes = 0; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
328 if not #user_push_services then return pushes end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
329 |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
330 for push_identifier, push_info in pairs(user_push_services) do |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
331 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 |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
332 if stanza then |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
333 if not stanza._push_notify then stanza._push_notify = {}; end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
334 if stanza._push_notify[push_identifier] then |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
335 if log_push_decline then |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
336 module:log("debug", "Already sent push notification for %s@%s to %s (%s)", node, module.host, push_info.jid, tostring(push_info.node)); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
337 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
338 send_push = false; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
339 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
340 stanza._push_notify[push_identifier] = true; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
341 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
342 |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
343 if send_push then |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
344 -- construct push stanza |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
345 local stanza_id = hashes.sha256(random.bytes(8), true); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
346 local push_notification_payload = st.stanza("notification", { xmlns = xmlns_push }); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
347 local form_data = { |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
348 -- hardcode to 1 because other numbers are just meaningless (the XEP does not specify *what exactly* to count) |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
349 ["message-count"] = "1"; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
350 }; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
351 if stanza and include_sender then |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
352 form_data["last-message-sender"] = stanza.attr.from; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
353 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
354 if stanza and include_body then |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
355 form_data["last-message-body"] = stanza:get_child_text("body"); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
356 elseif stanza and dummy_body and is_important(stanza) then |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
357 form_data["last-message-body"] = tostring(dummy_body); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
358 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
359 |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
360 push_notification_payload:add_child(push_form:form(form_data)); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
361 |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
362 local push_publish = st.iq({ to = push_info.jid, from = module.host, type = "set", id = stanza_id }) |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
363 :tag("pubsub", { xmlns = "http://jabber.org/protocol/pubsub" }) |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
364 :tag("publish", { node = push_info.node }) |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
365 :tag("item") |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
366 :add_child(push_notification_payload) |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
367 :up() |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
368 :up(); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
369 |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
370 if push_info.options then |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
371 push_publish:tag("publish-options"):add_child(st.deserialize(push_info.options)); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
372 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
373 -- send out push |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
374 module:log("debug", "Sending %s push notification for %s@%s to %s (%s)", |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
375 form_data["last-message-body"] and "important" or "unimportant", |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
376 node, module.host, push_info.jid, tostring(push_info.node) |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
377 ); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
378 -- module:log("debug", "PUSH STANZA: %s", tostring(push_publish)); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
379 local push_event = { |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
380 notification_stanza = push_publish; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
381 notification_payload = push_notification_payload; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
382 original_stanza = stanza; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
383 username = node; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
384 push_info = push_info; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
385 push_summary = form_data; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
386 important = not not form_data["last-message-body"]; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
387 }; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
388 |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
389 if module:fire_event("cloud_notify/push", push_event) then |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
390 module:log("debug", "Push was blocked by event handler: %s", push_event.reason or "Unknown reason"); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
391 else |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
392 -- handle push errors for this node |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
393 if push_errors[push_identifier] == nil then |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
394 push_errors[push_identifier] = 0; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
395 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
396 id2node[stanza_id] = node; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
397 id2identifier[stanza_id] = push_identifier; |
|
14134
041c7ff18f76
mod_cloud_notify: Fix leaking iq response handlers by using send_iq()
Matthew Wild <mwild1@gmail.com>
parents:
13701
diff
changeset
|
398 module:send_iq(push_publish):next(handle_push_success, handle_push_error):finally(function () |
|
041c7ff18f76
mod_cloud_notify: Fix leaking iq response handlers by using send_iq()
Matthew Wild <mwild1@gmail.com>
parents:
13701
diff
changeset
|
399 -- unhook iq handlers for this identifier (if possible) |
|
14167
f149652cb5ff
mod_cloud_notify: Use correct stanza id when clearing table entries (mem leak)
Matthew Wild <mwild1@gmail.com>
parents:
14134
diff
changeset
|
400 id2node[stanza_id] = nil; |
|
f149652cb5ff
mod_cloud_notify: Use correct stanza id when clearing table entries (mem leak)
Matthew Wild <mwild1@gmail.com>
parents:
14134
diff
changeset
|
401 id2identifier[stanza_id] = nil; |
|
14134
041c7ff18f76
mod_cloud_notify: Fix leaking iq response handlers by using send_iq()
Matthew Wild <mwild1@gmail.com>
parents:
13701
diff
changeset
|
402 end); |
|
13616
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
403 pushes = pushes + 1; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
404 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
405 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
406 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
407 return pushes; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
408 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
409 |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
410 -- small helper function to extract relevant push settings |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
411 local function get_push_settings(stanza, session) |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
412 local to = stanza.attr.to; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
413 local node = to and jid.split(to) or session.username; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
414 local user_push_services = push_store:get(node); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
415 return node, user_push_services; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
416 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
417 |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
418 -- publish on offline message |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
419 module:hook("message/offline/handle", function(event) |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
420 local node, user_push_services = get_push_settings(event.stanza, event.origin); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
421 module:log("debug", "Invoking cloud handle_notify_request() for offline stanza"); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
422 handle_notify_request(event.stanza, node, user_push_services, true); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
423 end, 1); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
424 |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
425 -- publish on bare groupchat |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
426 -- this picks up MUC messages when there are no devices connected |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
427 module:hook("message/bare/groupchat", function(event) |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
428 module:log("debug", "Invoking cloud handle_notify_request() for bare groupchat stanza"); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
429 local node, user_push_services = get_push_settings(event.stanza, event.origin); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
430 handle_notify_request(event.stanza, node, user_push_services, true); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
431 end, 1); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
432 |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
433 |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
434 local function process_stanza_queue(queue, session, queue_type) |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
435 if not session.push_identifier then return; end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
436 local user_push_services = {[session.push_identifier] = session.push_settings}; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
437 local notified = { unimportant = false; important = false } |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
438 for i=1, #queue do |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
439 local stanza = queue[i]; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
440 -- fast ignore of already pushed stanzas |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
441 if stanza and not (stanza._push_notify and stanza._push_notify[session.push_identifier]) then |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
442 local node = get_push_settings(stanza, session); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
443 local stanza_type = "unimportant"; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
444 if dummy_body and is_important(stanza) then stanza_type = "important"; end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
445 if not notified[stanza_type] then -- only notify if we didn't try to push for this stanza type already |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
446 -- session.log("debug", "Invoking cloud handle_notify_request() for smacks queued stanza: %d", i); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
447 if handle_notify_request(stanza, node, user_push_services, false) ~= 0 then |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
448 if session.hibernating and not session.first_hibernated_push then |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
449 -- if important stanzas are treated differently (pushed with last-message-body field set to dummy string) |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
450 -- if the message was important (e.g. had a last-message-body field) OR if we treat all pushes equally, |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
451 -- then record the time of first push in the session for the smack module which will extend its hibernation |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
452 -- timeout based on the value of session.first_hibernated_push |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
453 if not dummy_body or (dummy_body and is_important(stanza)) then |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
454 session.first_hibernated_push = os_time(); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
455 -- check for prosody 0.12 mod_smacks |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
456 if session.hibernating_watchdog and session.original_smacks_callback and session.original_smacks_timeout then |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
457 -- restore old smacks watchdog (--> the start of our original timeout will be delayed until first push) |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
458 session.hibernating_watchdog:cancel(); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
459 session.hibernating_watchdog = watchdog.new(session.original_smacks_timeout, session.original_smacks_callback); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
460 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
461 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
462 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
463 session.log("debug", "Cloud handle_notify_request() > 0, not notifying for other %s queued stanzas of type %s", queue_type, stanza_type); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
464 notified[stanza_type] = true |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
465 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
466 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
467 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
468 if notified.unimportant and notified.important then break; end -- stop processing the queue if all push types are exhausted |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
469 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
470 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
471 |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
472 -- publish on unacked smacks message (use timer to send out push for all stanzas submitted in a row only once) |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
473 local function process_stanza(session, stanza) |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
474 if session.push_identifier then |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
475 session.log("debug", "adding new stanza to push_queue"); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
476 if not session.push_queue then session.push_queue = {}; end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
477 local queue = session.push_queue; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
478 queue[#queue+1] = st.clone(stanza); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
479 if not session.awaiting_push_timer then -- timer not already running --> start new timer |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
480 session.log("debug", "Invoking cloud handle_notify_request() for newly smacks queued stanza (in a moment)"); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
481 session.awaiting_push_timer = module:add_timer(1.0, function () |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
482 session.log("debug", "Invoking cloud handle_notify_request() for newly smacks queued stanzas (now in timer)"); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
483 process_stanza_queue(session.push_queue, session, "push"); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
484 session.push_queue = {}; -- clean up queue after push |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
485 session.awaiting_push_timer = nil; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
486 end); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
487 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
488 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
489 return stanza; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
490 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
491 |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
492 local function process_smacks_stanza(event) |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
493 local session = event.origin; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
494 local stanza = event.stanza; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
495 if not session.push_identifier then |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
496 session.log("debug", "NOT invoking cloud handle_notify_request() for newly smacks queued stanza (session.push_identifier is not set: %s)", |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
497 session.push_identifier |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
498 ); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
499 else |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
500 process_stanza(session, stanza) |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
501 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
502 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
503 |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
504 -- smacks hibernation is started |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
505 local function hibernate_session(event) |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
506 local session = event.origin; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
507 local queue = event.queue; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
508 session.first_hibernated_push = nil; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
509 if session.push_identifier and session.hibernating_watchdog then -- check for prosody 0.12 mod_smacks |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
510 -- save old watchdog callback and timeout |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
511 session.original_smacks_callback = session.hibernating_watchdog.callback; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
512 session.original_smacks_timeout = session.hibernating_watchdog.timeout; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
513 -- cancel old watchdog and create a new watchdog with extended timeout |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
514 session.hibernating_watchdog:cancel(); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
515 session.hibernating_watchdog = watchdog.new(extended_hibernation_timeout, function() |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
516 session.log("debug", "Push-extended smacks watchdog triggered"); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
517 if session.original_smacks_callback then |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
518 session.log("debug", "Calling original smacks watchdog handler"); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
519 session.original_smacks_callback(); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
520 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
521 end); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
522 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
523 -- process unacked stanzas |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
524 process_stanza_queue(queue, session, "smacks"); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
525 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
526 |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
527 -- smacks hibernation is ended |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
528 local function restore_session(event) |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
529 local session = event.resumed; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
530 if session then -- older smacks module versions send only the "intermediate" session in event.session and no session.resumed one |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
531 if session.awaiting_push_timer then |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
532 session.awaiting_push_timer:stop(); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
533 session.awaiting_push_timer = nil; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
534 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
535 session.first_hibernated_push = nil; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
536 -- the extended smacks watchdog will be canceled by the smacks module, no need to anything here |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
537 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
538 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
539 |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
540 -- smacks ack is delayed |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
541 local function ack_delayed(event) |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
542 local session = event.origin; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
543 local queue = event.queue; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
544 local stanza = event.stanza; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
545 if not session.push_identifier then return; end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
546 if stanza then process_stanza(session, stanza); return; end -- don't iterate through smacks queue if we know which stanza triggered this |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
547 for i=1, #queue do |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
548 local queued_stanza = queue[i]; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
549 -- process unacked stanzas (handle_notify_request() will only send push requests for new stanzas) |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
550 process_stanza(session, queued_stanza); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
551 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
552 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
553 |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
554 -- archive message added |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
555 local function archive_message_added(event) |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
556 -- event is: { origin = origin, stanza = stanza, for_user = store_user, id = id } |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
557 -- only notify for new mam messages when at least one device is online |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
558 if not event.for_user or not host_sessions[event.for_user] then return; end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
559 local stanza = event.stanza; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
560 local user_session = host_sessions[event.for_user].sessions; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
561 local to = stanza.attr.to; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
562 to = to and jid.split(to) or event.origin.username; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
563 |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
564 -- only notify if the stanza destination is the mam user we store it for |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
565 if event.for_user == to then |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
566 local user_push_services = push_store:get(to); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
567 |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
568 -- Urgent stanzas are time-sensitive (e.g. calls) and should |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
569 -- be pushed immediately to avoid getting stuck in the smacks |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
570 -- queue in case of dead connections, for example |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
571 local is_urgent_stanza, urgent_reason = is_urgent(event.stanza); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
572 |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
573 local notify_push_services; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
574 if is_urgent_stanza then |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
575 module:log("debug", "Urgent push for %s (%s)", to, urgent_reason); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
576 notify_push_services = user_push_services; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
577 else |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
578 -- only notify nodes with no active sessions (smacks is counted as active and handled separate) |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
579 notify_push_services = {}; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
580 for identifier, push_info in pairs(user_push_services) do |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
581 local identifier_found = nil; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
582 for _, session in pairs(user_session) do |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
583 if session.push_identifier == identifier then |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
584 identifier_found = session; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
585 break; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
586 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
587 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
588 if identifier_found then |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
589 identifier_found.log("debug", "Not cloud notifying '%s' of new MAM stanza (session still alive)", identifier); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
590 else |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
591 notify_push_services[identifier] = push_info; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
592 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
593 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
594 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
595 |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
596 handle_notify_request(event.stanza, to, notify_push_services, true); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
597 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
598 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
599 |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
600 module:hook("smacks-hibernation-start", hibernate_session); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
601 module:hook("smacks-hibernation-end", restore_session); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
602 module:hook("smacks-ack-delayed", ack_delayed); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
603 module:hook("smacks-hibernation-stanza-queued", process_smacks_stanza); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
604 module:hook("archive-message-added", archive_message_added); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
605 |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
606 local function send_ping(event) |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
607 local user = event.user; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
608 local push_services = event.push_services or push_store:get(user); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
609 module:log("debug", "Handling event 'cloud-notify-ping' for user '%s'", user); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
610 local retval = handle_notify_request(nil, user, push_services, true); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
611 module:log("debug", "handle_notify_request() returned %s", tostring(retval)); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
612 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
613 -- can be used by other modules to ping one or more (or all) push endpoints |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
614 module:hook("cloud-notify-ping", send_ping); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
615 |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
616 module:log("info", "Module loaded"); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
617 function module.unload() |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
618 module:log("info", "Unloading module"); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
619 -- cleanup some settings, reloading this module can cause process_smacks_stanza() to stop working otherwise |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
620 for user, _ in pairs(host_sessions) do |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
621 for _, session in pairs(host_sessions[user].sessions) do |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
622 if session.awaiting_push_timer then session.awaiting_push_timer:stop(); end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
623 session.awaiting_push_timer = nil; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
624 session.push_queue = nil; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
625 session.first_hibernated_push = nil; |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
626 -- check for prosody 0.12 mod_smacks |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
627 if session.hibernating_watchdog and session.original_smacks_callback and session.original_smacks_timeout then |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
628 -- restore old smacks watchdog |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
629 session.hibernating_watchdog:cancel(); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
630 session.hibernating_watchdog = watchdog.new(session.original_smacks_timeout, session.original_smacks_callback); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
631 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
632 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
633 end |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
634 module:log("info", "Module unloaded"); |
|
2f38f3275a74
mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
635 end |
