Software / code / prosody-modules
Annotate
mod_push2/mod_push2.lua @ 6336:6e80b2cb5fe6
mod_http_oauth2: Show scope descriptions
This should help the user understand and provide _informed_ consent,
although the texts can certainly be improved. Explaining these scopes is
non-trivial.
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Tue, 15 Jul 2025 16:28:40 +0200 |
| parent | 6312:2488254c1bc2 |
| rev | line source |
|---|---|
|
5682
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
1 local os_time = os.time; |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
2 local st = require"util.stanza"; |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
3 local jid = require"util.jid"; |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
4 local hashes = require"util.hashes"; |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
5 local random = require"util.random"; |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
6 local watchdog = require "util.watchdog"; |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
7 local uuid = require "util.uuid"; |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
8 local base64 = require "util.encodings".base64; |
|
6033
8cb37a497e4c
mod_push2: Switch from patched luaossl to prosody-trunk methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
5998
diff
changeset
|
9 local crypto = require "util.crypto"; |
|
5682
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
10 local jwt = require "util.jwt"; |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
11 |
|
6213
811bd0872682
mod_push2: Do not push MUC reflections
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6212
diff
changeset
|
12 pcall(function() module:depends("track_muc_joins") end) |
|
811bd0872682
mod_push2: Do not push MUC reflections
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6212
diff
changeset
|
13 |
|
5682
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
14 local xmlns_push = "urn:xmpp:push2:0"; |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
15 |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
16 -- configuration |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
17 local contact_uri = module:get_option_string("contact_uri", "xmpp:" .. module.host) |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
18 local extended_hibernation_timeout = module:get_option_number("push_max_hibernation_timeout", 72*3600) -- use same timeout like ejabberd |
|
6214
fe9f2c618e8a
mod_push2: option to keep hibernating after first push
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6213
diff
changeset
|
19 local hibernate_past_first_push = module:get_option_boolean("hibernate_past_first_push", true) |
|
5682
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
20 |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
21 local host_sessions = prosody.hosts[module.host].sessions |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
22 local push2_registrations = module:open_store("push2_registrations", "keyval") |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
23 |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
24 if _VERSION:match("5%.1") or _VERSION:match("5%.2") then |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
25 module:log("warn", "This module may behave incorrectly on Lua before 5.3. It is recommended to upgrade to a newer Lua version.") |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
26 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
27 |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
28 local function account_dico_info(event) |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
29 (event.reply or event.stanza):tag("feature", {var=xmlns_push}):up() |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
30 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
31 module:hook("account-disco-info", account_dico_info); |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
32 |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
33 local function parse_match(matchel) |
|
6215
e53f0967520c
mod_push: Allow filtering pushes for particular chats
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6214
diff
changeset
|
34 local match = { match = matchel.attr.profile, chats = {} } |
|
e53f0967520c
mod_push: Allow filtering pushes for particular chats
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6214
diff
changeset
|
35 |
|
6233
1c16bb49f6f6
mod_push2: rename chat to filter
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6219
diff
changeset
|
36 for chatel in matchel:childtags("filter") do |
|
6215
e53f0967520c
mod_push: Allow filtering pushes for particular chats
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6214
diff
changeset
|
37 local chat = {} |
|
e53f0967520c
mod_push: Allow filtering pushes for particular chats
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6214
diff
changeset
|
38 if chatel:get_child("mention") then |
|
e53f0967520c
mod_push: Allow filtering pushes for particular chats
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6214
diff
changeset
|
39 chat.mention = true |
|
e53f0967520c
mod_push: Allow filtering pushes for particular chats
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6214
diff
changeset
|
40 end |
|
e53f0967520c
mod_push: Allow filtering pushes for particular chats
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6214
diff
changeset
|
41 if chatel:get_child("reply") then |
|
e53f0967520c
mod_push: Allow filtering pushes for particular chats
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6214
diff
changeset
|
42 chat.reply = true |
|
e53f0967520c
mod_push: Allow filtering pushes for particular chats
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6214
diff
changeset
|
43 end |
|
e53f0967520c
mod_push: Allow filtering pushes for particular chats
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6214
diff
changeset
|
44 match.chats[chatel.attr.jid] = chat |
|
e53f0967520c
mod_push: Allow filtering pushes for particular chats
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6214
diff
changeset
|
45 end |
|
e53f0967520c
mod_push: Allow filtering pushes for particular chats
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6214
diff
changeset
|
46 |
|
6216
2f2539ce8f3b
mod_push2: implement grace period
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6215
diff
changeset
|
47 match.grace = matchel:get_child_text("grace") |
|
2f2539ce8f3b
mod_push2: implement grace period
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6215
diff
changeset
|
48 if match.grace then match.grace = tonumber(match.grace) end |
|
2f2539ce8f3b
mod_push2: implement grace period
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6215
diff
changeset
|
49 |
|
5682
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
50 local send = matchel:get_child("send", "urn:xmpp:push2:send:notify-only:0") |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
51 if send then |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
52 match.send = send.attr.xmlns |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
53 return match |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
54 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
55 |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
56 send = matchel:get_child("send", "urn:xmpp:push2:send:sce+rfc8291+rfc8292:0") |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
57 if send then |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
58 match.send = send.attr.xmlns |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
59 match.ua_public = send:get_child_text("ua-public") |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
60 match.auth_secret = send:get_child_text("auth-secret") |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
61 match.jwt_alg = send:get_child_text("jwt-alg") |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
62 match.jwt_key = send:get_child_text("jwt-key") |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
63 match.jwt_claims = {} |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
64 for claim in send:childtags("jwt-claim") do |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
65 match.jwt_claims[claim.attr.name] = claim:get_text() |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
66 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
67 return match |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
68 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
69 |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
70 return nil |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
71 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
72 |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
73 local function push_enable(event) |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
74 local origin, stanza = event.origin, event.stanza; |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
75 local enable = stanza.tags[1]; |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
76 origin.log("debug", "Attempting to enable push notifications") |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
77 -- MUST contain a jid of the push service being enabled |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
78 local service_jid = enable:get_child_text("service") |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
79 -- MUST contain a string to identify the client fo the push service |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
80 local client = enable:get_child_text("client") |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
81 if not service_jid then |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
82 origin.log("debug", "Push notification enable request missing service") |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
83 origin.send(st.error_reply(stanza, "modify", "bad-request", "Missing service")) |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
84 return true |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
85 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
86 if not client then |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
87 origin.log("debug", "Push notification enable request missing client") |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
88 origin.send(st.error_reply(stanza, "modify", "bad-request", "Missing client")) |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
89 return true |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
90 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
91 if service_jid == stanza.attr.from then |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
92 origin.log("debug", "Push notification enable request service JID identical to our own") |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
93 origin.send(st.error_reply(stanza, "modify", "bad-request", "JID must be different from ours")) |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
94 return true |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
95 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
96 local matches = {} |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
97 for matchel in enable:childtags("match") do |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
98 local match = parse_match(matchel) |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
99 if match then |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
100 matches[#matches + 1] = match |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
101 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
102 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
103 -- Tie registration to client, via client_id with sasl2 or else fallback to resource |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
104 local registration_id = origin.client_id or origin.resource |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
105 local push_registration = { |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
106 service = service_jid; |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
107 client = client; |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
108 timestamp = os_time(); |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
109 matches = matches; |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
110 }; |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
111 -- TODO: can we move to keyval+ on trunk? |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
112 local registrations = push2_registrations:get(origin.username) or {} |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
113 registrations[registration_id] = push_registration |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
114 if not push2_registrations:set(origin.username, registrations) then |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
115 origin.send(st.error_reply(stanza, "wait", "internal-server-error")); |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
116 else |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
117 origin.push_registration_id = registration_id |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
118 origin.push_registration = push_registration |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
119 origin.first_hibernated_push = nil |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
120 origin.log("info", "Push notifications enabled for %s (%s)", tostring(stanza.attr.from), tostring(service_jid)) |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
121 origin.send(st.reply(stanza)) |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
122 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
123 return true |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
124 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
125 module:hook("iq-set/self/"..xmlns_push..":enable", push_enable) |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
126 |
|
6190
aa240145aa22
mod_push2: support for disabling push notifications
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6186
diff
changeset
|
127 local function push_disable(event) |
|
aa240145aa22
mod_push2: support for disabling push notifications
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6186
diff
changeset
|
128 local origin, stanza = event.origin, event.stanza; |
|
aa240145aa22
mod_push2: support for disabling push notifications
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6186
diff
changeset
|
129 local enable = stanza.tags[1]; |
|
aa240145aa22
mod_push2: support for disabling push notifications
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6186
diff
changeset
|
130 origin.log("debug", "Attempting to disable push notifications") |
|
aa240145aa22
mod_push2: support for disabling push notifications
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6186
diff
changeset
|
131 -- Tie registration to client, via client_id with sasl2 or else fallback to resource |
|
aa240145aa22
mod_push2: support for disabling push notifications
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6186
diff
changeset
|
132 local registration_id = origin.client_id or origin.resource |
|
aa240145aa22
mod_push2: support for disabling push notifications
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6186
diff
changeset
|
133 -- TODO: can we move to keyval+ on trunk? |
|
aa240145aa22
mod_push2: support for disabling push notifications
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6186
diff
changeset
|
134 local registrations = push2_registrations:get(origin.username) or {} |
|
aa240145aa22
mod_push2: support for disabling push notifications
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6186
diff
changeset
|
135 registrations[registration_id] = nil |
|
aa240145aa22
mod_push2: support for disabling push notifications
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6186
diff
changeset
|
136 if not push2_registrations:set(origin.username, registrations) then |
|
aa240145aa22
mod_push2: support for disabling push notifications
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6186
diff
changeset
|
137 origin.send(st.error_reply(stanza, "wait", "internal-server-error")); |
|
aa240145aa22
mod_push2: support for disabling push notifications
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6186
diff
changeset
|
138 else |
|
aa240145aa22
mod_push2: support for disabling push notifications
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6186
diff
changeset
|
139 origin.push_registration_id = nil |
|
aa240145aa22
mod_push2: support for disabling push notifications
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6186
diff
changeset
|
140 origin.push_registration = nil |
|
aa240145aa22
mod_push2: support for disabling push notifications
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6186
diff
changeset
|
141 origin.first_hibernated_push = nil |
|
aa240145aa22
mod_push2: support for disabling push notifications
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6186
diff
changeset
|
142 origin.log("info", "Push notifications disabled for %s (%s)", tostring(stanza.attr.from), registration_id) |
|
aa240145aa22
mod_push2: support for disabling push notifications
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6186
diff
changeset
|
143 origin.send(st.reply(stanza)) |
|
aa240145aa22
mod_push2: support for disabling push notifications
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6186
diff
changeset
|
144 end |
|
aa240145aa22
mod_push2: support for disabling push notifications
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6186
diff
changeset
|
145 return true |
|
aa240145aa22
mod_push2: support for disabling push notifications
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6186
diff
changeset
|
146 end |
|
aa240145aa22
mod_push2: support for disabling push notifications
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6186
diff
changeset
|
147 module:hook("iq-set/self/"..xmlns_push..":disable", push_disable) |
|
aa240145aa22
mod_push2: support for disabling push notifications
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6186
diff
changeset
|
148 |
|
5682
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
149 -- urgent stanzas should be delivered without delay |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
150 local function is_voip(stanza) |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
151 if stanza.name == "message" then |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
152 if stanza:get_child("propose", "urn:xmpp:jingle-message:0") then |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
153 return true, "jingle call" |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
154 end |
|
6312
2488254c1bc2
mod_push2: Also push call retractions
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6311
diff
changeset
|
155 |
|
2488254c1bc2
mod_push2: Also push call retractions
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6311
diff
changeset
|
156 if stanza:get_child("retract", "urn:xmpp:jingle-message:0") then |
|
2488254c1bc2
mod_push2: Also push call retractions
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6311
diff
changeset
|
157 return true, "jingle call retract" |
|
2488254c1bc2
mod_push2: Also push call retractions
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6311
diff
changeset
|
158 end |
|
5682
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
159 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
160 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
161 |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
162 local function has_body(stanza) |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
163 -- We can't check for body contents in encrypted messages, so let's treat them as important |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
164 -- Some clients don't even set a body or an empty body for encrypted messages |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
165 |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
166 -- check omemo https://xmpp.org/extensions/inbox/omemo.html |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
167 if stanza:get_child("encrypted", "eu.siacs.conversations.axolotl") or stanza:get_child("encrypted", "urn:xmpp:omemo:0") then return true; end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
168 |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
169 -- check xep27 pgp https://xmpp.org/extensions/xep-0027.html |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
170 if stanza:get_child("x", "jabber:x:encrypted") then return true; end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
171 |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
172 -- check xep373 pgp (OX) https://xmpp.org/extensions/xep-0373.html |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
173 if stanza:get_child("openpgp", "urn:xmpp:openpgp:0") then return true; end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
174 |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
175 local body = stanza:get_child_text("body"); |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
176 |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
177 return body ~= nil and body ~= "" |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
178 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
179 |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
180 -- is this push a high priority one |
|
6213
811bd0872682
mod_push2: Do not push MUC reflections
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6212
diff
changeset
|
181 local function is_important(stanza, session) |
|
5682
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
182 local is_voip_stanza, urgent_reason = is_voip(stanza) |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
183 if is_voip_stanza then return true; end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
184 |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
185 local st_name = stanza and stanza.name or nil |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
186 if not st_name then return false; end -- nonzas are never important here |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
187 if st_name == "presence" then |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
188 return false; -- same for presences |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
189 elseif st_name == "message" then |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
190 -- unpack carbon copied message stanzas |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
191 local carbon = stanza:find("{urn:xmpp:carbons:2}/{urn:xmpp:forward:0}/{jabber:client}message") |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
192 local stanza_direction = carbon and stanza:child_with_name("sent") and "out" or "in" |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
193 if carbon then stanza = carbon; end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
194 local st_type = stanza.attr.type |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
195 |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
196 -- headline message are always not important |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
197 if st_type == "headline" then return false; end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
198 |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
199 -- carbon copied outgoing messages are not important |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
200 if carbon and stanza_direction == "out" then return false; end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
201 |
|
6213
811bd0872682
mod_push2: Do not push MUC reflections
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6212
diff
changeset
|
202 -- groupchat reflections are not important here |
|
811bd0872682
mod_push2: Do not push MUC reflections
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6212
diff
changeset
|
203 if st_type == "groupchat" and session and session.rooms_joined then |
|
811bd0872682
mod_push2: Do not push MUC reflections
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6212
diff
changeset
|
204 local muc = jid.bare(stanza.attr.from) |
|
811bd0872682
mod_push2: Do not push MUC reflections
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6212
diff
changeset
|
205 local from_nick = jid.resource(stanza.attr.from) |
|
811bd0872682
mod_push2: Do not push MUC reflections
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6212
diff
changeset
|
206 if from_nick == session.rooms_joined[muc] then |
|
811bd0872682
mod_push2: Do not push MUC reflections
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6212
diff
changeset
|
207 return false |
|
811bd0872682
mod_push2: Do not push MUC reflections
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6212
diff
changeset
|
208 end |
|
5682
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
209 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
210 |
|
6217
8ecd53452af8
mod_push2: LMC stanzas are not important
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6216
diff
changeset
|
211 -- edits are not imporatnt |
|
8ecd53452af8
mod_push2: LMC stanzas are not important
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6216
diff
changeset
|
212 if stanza:get_child("replace", "urn:xmpp:message-correct:0") then |
|
8ecd53452af8
mod_push2: LMC stanzas are not important
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6216
diff
changeset
|
213 return false |
|
8ecd53452af8
mod_push2: LMC stanzas are not important
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6216
diff
changeset
|
214 end |
|
8ecd53452af8
mod_push2: LMC stanzas are not important
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6216
diff
changeset
|
215 |
|
5682
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
216 -- empty bodies are not important |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
217 return has_body(stanza) |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
218 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
219 return false; -- this stanza wasn't one of the above cases --> it is not important, too |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
220 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
221 |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
222 local function add_sce_rfc8291(match, stanza, push_notification_payload) |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
223 local max_data_size = 2847 -- https://github.com/web-push-libs/web-push-php/issues/108 |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
224 local stanza_clone = st.clone(stanza) |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
225 stanza_clone.attr.xmlns = "jabber:client" |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
226 local envelope = st.stanza("envelope", { xmlns = "urn:xmpp:sce:1" }) |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
227 :tag("content") |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
228 :tag("forwarded", { xmlns = "urn:xmpp:forward:0" }) |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
229 :add_child(stanza_clone) |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
230 :up():up():up() |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
231 local envelope_bytes = tostring(envelope) |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
232 if string.len(envelope_bytes) > max_data_size then |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
233 -- If stanza is too big, remove extra elements |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
234 stanza_clone:maptags(function(el) |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
235 if el.attr.xmlns == nil or |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
236 el.attr.xmlns == "jabber:client" or |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
237 el.attr.xmlns == "jabber:x:oob" or |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
238 (el.attr.xmlns == "urn:xmpp:sid:0" and el.name == "stanza-id") or |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
239 el.attr.xmlns == "eu.siacs.conversations.axolotl" or |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
240 el.attr.xmlns == "urn:xmpp:omemo:0" or |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
241 el.attr.xmlns == "jabber:x:encrypted" or |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
242 el.attr.xmlns == "urn:xmpp:openpgp:0" or |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
243 el.attr.xmlns == "urn:xmpp:sce:1" or |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
244 el.attr.xmlns == "urn:xmpp:jingle-message:0" or |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
245 el.attr.xmlns == "jabber:x:conference" |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
246 then |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
247 return el |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
248 else |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
249 return nil |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
250 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
251 end) |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
252 envelope_bytes = tostring(envelope) |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
253 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
254 if string.len(envelope_bytes) > max_data_size then |
|
5683
bebb10fa5787
mod_push2: Add back body truncation logic
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
5682
diff
changeset
|
255 local body = stanza:get_child_text("body") |
|
6212
051974b4c900
mod_push2: Fix counting bugs
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6190
diff
changeset
|
256 if body and string.len(body) > 50 then |
|
5683
bebb10fa5787
mod_push2: Add back body truncation logic
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
5682
diff
changeset
|
257 stanza_clone:maptags(function(el) |
|
bebb10fa5787
mod_push2: Add back body truncation logic
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
5682
diff
changeset
|
258 if el.name == "body" then |
|
bebb10fa5787
mod_push2: Add back body truncation logic
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
5682
diff
changeset
|
259 return nil |
|
bebb10fa5787
mod_push2: Add back body truncation logic
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
5682
diff
changeset
|
260 else |
|
bebb10fa5787
mod_push2: Add back body truncation logic
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
5682
diff
changeset
|
261 return el |
|
bebb10fa5787
mod_push2: Add back body truncation logic
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
5682
diff
changeset
|
262 end |
|
bebb10fa5787
mod_push2: Add back body truncation logic
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
5682
diff
changeset
|
263 end) |
|
bebb10fa5787
mod_push2: Add back body truncation logic
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
5682
diff
changeset
|
264 |
|
bebb10fa5787
mod_push2: Add back body truncation logic
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
5682
diff
changeset
|
265 body = string.gsub(string.gsub("\n" .. body, "\n>[^\n]*", ""), "^%s", "") |
|
bebb10fa5787
mod_push2: Add back body truncation logic
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
5682
diff
changeset
|
266 stanza_clone:body(body:sub(1, utf8.offset(body, 50)) .. "…") |
|
bebb10fa5787
mod_push2: Add back body truncation logic
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
5682
diff
changeset
|
267 envelope_bytes = tostring(envelope) |
|
bebb10fa5787
mod_push2: Add back body truncation logic
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
5682
diff
changeset
|
268 end |
|
bebb10fa5787
mod_push2: Add back body truncation logic
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
5682
diff
changeset
|
269 end |
|
bebb10fa5787
mod_push2: Add back body truncation logic
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
5682
diff
changeset
|
270 if string.len(envelope_bytes) > max_data_size then |
|
5682
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
271 -- If still too big, get aggressive |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
272 stanza_clone:maptags(function(el) |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
273 if el.name == "body" or |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
274 (el.attr.xmlns == "urn:xmpp:sid:0" and el.name == "stanza-id") or |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
275 el.attr.xmlns == "urn:xmpp:jingle-message:0" or |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
276 el.attr.xmlns == "jabber:x:conference" |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
277 then |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
278 return el |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
279 else |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
280 return nil |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
281 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
282 end) |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
283 envelope_bytes = tostring(envelope) |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
284 end |
|
6212
051974b4c900
mod_push2: Fix counting bugs
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6190
diff
changeset
|
285 local padding_size = math.min(150, max_data_size/3 - string.len(envelope_bytes)) |
|
051974b4c900
mod_push2: Fix counting bugs
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6190
diff
changeset
|
286 if padding_size > 0 then |
|
051974b4c900
mod_push2: Fix counting bugs
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6190
diff
changeset
|
287 envelope:text_tag("rpad", base64.encode(random.bytes(padding_size))) |
|
5682
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
288 envelope_bytes = tostring(envelope) |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
289 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
290 |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
291 local p256dh_raw = base64.decode(match.ua_public .. "==") |
|
6033
8cb37a497e4c
mod_push2: Switch from patched luaossl to prosody-trunk methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
5998
diff
changeset
|
292 local p256dh = crypto.import_public_ec_raw(p256dh_raw, "prime256v1") |
|
8cb37a497e4c
mod_push2: Switch from patched luaossl to prosody-trunk methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
5998
diff
changeset
|
293 local one_time_key = crypto.generate_p256_keypair() |
|
8cb37a497e4c
mod_push2: Switch from patched luaossl to prosody-trunk methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
5998
diff
changeset
|
294 local one_time_key_public = one_time_key:public_raw() |
|
5682
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
295 local info = "WebPush: info\0" .. p256dh_raw .. one_time_key_public |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
296 local auth_secret = base64.decode(match.auth_secret .. "==") |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
297 local salt = random.bytes(16) |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
298 local shared_secret = one_time_key:derive(p256dh) |
|
6033
8cb37a497e4c
mod_push2: Switch from patched luaossl to prosody-trunk methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
5998
diff
changeset
|
299 local ikm = hashes.hkdf_hmac_sha256(32, shared_secret, auth_secret, info) |
|
8cb37a497e4c
mod_push2: Switch from patched luaossl to prosody-trunk methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
5998
diff
changeset
|
300 local key = hashes.hkdf_hmac_sha256(16, ikm, salt, "Content-Encoding: aes128gcm\0") |
|
8cb37a497e4c
mod_push2: Switch from patched luaossl to prosody-trunk methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
5998
diff
changeset
|
301 local nonce = hashes.hkdf_hmac_sha256(12, ikm, salt, "Content-Encoding: nonce\0") |
|
5682
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
302 local header = salt .. "\0\0\16\0" .. string.char(string.len(one_time_key_public)) .. one_time_key_public |
|
6033
8cb37a497e4c
mod_push2: Switch from patched luaossl to prosody-trunk methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
5998
diff
changeset
|
303 local encrypted = crypto.aes_128_gcm_encrypt(key, nonce, envelope_bytes .. "\2") |
|
5682
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
304 |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
305 push_notification_payload |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
306 :tag("encrypted", { xmlns = "urn:xmpp:sce:rfc8291:0" }) |
|
6033
8cb37a497e4c
mod_push2: Switch from patched luaossl to prosody-trunk methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
5998
diff
changeset
|
307 :text_tag("payload", base64.encode(header .. encrypted)) |
|
5682
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
308 :up() |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
309 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
310 |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
311 local function add_rfc8292(match, stanza, push_notification_payload) |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
312 if not match.jwt_alg then return; end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
313 local key = match.jwt_key |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
314 if match.jwt_alg ~= "HS256" then |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
315 -- keypairs are in PKCS#8 PEM format without header/footer |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
316 key = "-----BEGIN PRIVATE KEY-----\n"..key.."\n-----END PRIVATE KEY-----" |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
317 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
318 |
|
6033
8cb37a497e4c
mod_push2: Switch from patched luaossl to prosody-trunk methods
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
5998
diff
changeset
|
319 local public_key = crypto.import_private_pem(key):public_raw() |
|
5682
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
320 local signer = jwt.new_signer(match.jwt_alg, key) |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
321 local payload = {} |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
322 for k, v in pairs(match.jwt_claims or {}) do |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
323 payload[k] = v |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
324 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
325 payload.sub = contact_uri |
|
5686
a1d22d6efb3d
mod_push2: Need to include the public key with the JWT
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
5683
diff
changeset
|
326 push_notification_payload:text_tag("jwt", signer(payload), { key = base64.encode(public_key) }) |
|
5682
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
327 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
328 |
|
6213
811bd0872682
mod_push2: Do not push MUC reflections
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6212
diff
changeset
|
329 local function handle_notify_request(stanza, node, user_push_services, session, log_push_decline) |
|
5682
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
330 local pushes = 0; |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
331 if not #user_push_services then return pushes end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
332 |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
333 local notify_push_services = {}; |
|
6213
811bd0872682
mod_push2: Do not push MUC reflections
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6212
diff
changeset
|
334 if is_important(stanza, session) then |
|
5682
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
335 notify_push_services = user_push_services |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
336 else |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
337 for identifier, push_info in pairs(user_push_services) do |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
338 for _, match in ipairs(push_info.matches) do |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
339 if match.match == "urn:xmpp:push2:match:important" then |
|
6034
b4bf44765ce6
mod_push2: fix some log lines
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6033
diff
changeset
|
340 module:log("debug", "Not pushing because not important") |
|
5682
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
341 else |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
342 notify_push_services[identifier] = push_info; |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
343 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
344 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
345 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
346 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
347 |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
348 for push_registration_id, push_info in pairs(notify_push_services) do |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
349 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 |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
350 if stanza then |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
351 if not stanza._push_notify2 then stanza._push_notify2 = {}; end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
352 if stanza._push_notify2[push_registration_id] then |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
353 if log_push_decline then |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
354 module:log("debug", "Already sent push notification for %s@%s to %s (%s)", node, module.host, push_info.jid, tostring(push_info.node)); |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
355 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
356 send_push = false; |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
357 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
358 stanza._push_notify2[push_registration_id] = true; |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
359 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
360 |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
361 if send_push then |
|
6266
6164849469f1
mod2_push2: suppress send if none match
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6233
diff
changeset
|
362 local any_match = false; |
|
5682
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
363 local push_notification_payload = st.stanza("notification", { xmlns = xmlns_push }) |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
364 push_notification_payload:text_tag("client", push_info.client) |
|
6213
811bd0872682
mod_push2: Do not push MUC reflections
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6212
diff
changeset
|
365 push_notification_payload:text_tag("priority", is_voip(stanza) and "high" or (is_important(stanza, session) and "normal" or "low")) |
|
5682
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
366 if is_voip(stanza) then |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
367 push_notification_payload:tag("voip"):up() |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
368 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
369 |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
370 local sends_added = {}; |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
371 for _, match in ipairs(push_info.matches) do |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
372 local does_match = false; |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
373 if match.match == "urn:xmpp:push2:match:all" then |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
374 does_match = true |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
375 elseif match.match == "urn:xmpp:push2:match:important" then |
|
6213
811bd0872682
mod_push2: Do not push MUC reflections
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6212
diff
changeset
|
376 does_match = is_important(stanza, session) |
|
5682
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
377 elseif match.match == "urn:xmpp:push2:match:archived" then |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
378 does_match = stanza:get_child("stana-id", "urn:xmpp:sid:0") |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
379 elseif match.match == "urn:xmpp:push2:match:archived-with-body" then |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
380 does_match = stanza:get_child("stana-id", "urn:xmpp:sid:0") and has_body(stanza) |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
381 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
382 |
|
6216
2f2539ce8f3b
mod_push2: implement grace period
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6215
diff
changeset
|
383 local to_user, to_host = jid.split(stanza.attr.to) |
|
2f2539ce8f3b
mod_push2: implement grace period
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6215
diff
changeset
|
384 to_user = to_user or session.username |
|
2f2539ce8f3b
mod_push2: implement grace period
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6215
diff
changeset
|
385 to_host = to_host or module.host |
|
2f2539ce8f3b
mod_push2: implement grace period
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6215
diff
changeset
|
386 |
|
2f2539ce8f3b
mod_push2: implement grace period
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6215
diff
changeset
|
387 -- If another session has recent activity within configured grace period, don't send push |
|
6311
27bb0bc3f4b5
mod_push2: always push voip notifications
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6267
diff
changeset
|
388 if does_match and match.grace and not is_voip(stanza) and to_host == module.host and host_sessions[to_user] then |
|
6216
2f2539ce8f3b
mod_push2: implement grace period
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6215
diff
changeset
|
389 local now = os_time() |
|
2f2539ce8f3b
mod_push2: implement grace period
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6215
diff
changeset
|
390 for _, session in pairs(host_sessions[to_user].sessions) do |
|
2f2539ce8f3b
mod_push2: implement grace period
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6215
diff
changeset
|
391 if session.last_activity and session.push_registration_id ~= push_registration_id and (now - session.last_activity) < match.grace then |
|
2f2539ce8f3b
mod_push2: implement grace period
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6215
diff
changeset
|
392 does_match = false |
|
2f2539ce8f3b
mod_push2: implement grace period
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6215
diff
changeset
|
393 end |
|
2f2539ce8f3b
mod_push2: implement grace period
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6215
diff
changeset
|
394 end |
|
2f2539ce8f3b
mod_push2: implement grace period
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6215
diff
changeset
|
395 end |
|
2f2539ce8f3b
mod_push2: implement grace period
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6215
diff
changeset
|
396 |
|
6215
e53f0967520c
mod_push: Allow filtering pushes for particular chats
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6214
diff
changeset
|
397 local chat = match.chats and (match.chats[stanza.attr.from] or match.chats[jid.bare(stanza.attr.from)] or match.chats[jid.host(stanza.attr.from)]) |
|
e53f0967520c
mod_push: Allow filtering pushes for particular chats
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6214
diff
changeset
|
398 if does_match and chat then |
|
e53f0967520c
mod_push: Allow filtering pushes for particular chats
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6214
diff
changeset
|
399 does_match = false |
|
e53f0967520c
mod_push: Allow filtering pushes for particular chats
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6214
diff
changeset
|
400 |
|
6218
277db84b0c95
mod_push2: guard against no rooms tracked on this session
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6217
diff
changeset
|
401 local nick = (session.rooms_joined and session.rooms_joined[jid.bare(stanza.attr.from)]) or to_user |
|
6215
e53f0967520c
mod_push: Allow filtering pushes for particular chats
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6214
diff
changeset
|
402 |
|
e53f0967520c
mod_push: Allow filtering pushes for particular chats
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6214
diff
changeset
|
403 if not does_match and chat.mention then |
|
e53f0967520c
mod_push: Allow filtering pushes for particular chats
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6214
diff
changeset
|
404 local body = stanza:get_child_text("body") |
|
e53f0967520c
mod_push: Allow filtering pushes for particular chats
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6214
diff
changeset
|
405 if body and body:find(nick, 1, true) then |
|
e53f0967520c
mod_push: Allow filtering pushes for particular chats
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6214
diff
changeset
|
406 does_match = true |
|
e53f0967520c
mod_push: Allow filtering pushes for particular chats
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6214
diff
changeset
|
407 end |
|
e53f0967520c
mod_push: Allow filtering pushes for particular chats
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6214
diff
changeset
|
408 end |
|
e53f0967520c
mod_push: Allow filtering pushes for particular chats
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6214
diff
changeset
|
409 if not does_match and chat.reply then |
|
e53f0967520c
mod_push: Allow filtering pushes for particular chats
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6214
diff
changeset
|
410 local reply = stanza:get_child("reply", "urn:xmpp:reply:0") |
|
e53f0967520c
mod_push: Allow filtering pushes for particular chats
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6214
diff
changeset
|
411 if reply and (reply.attr.to == to_user.."@"..to_host or (jid.bare(reply.attr.to) == jid.bare(stanza.attr.from) and jid.resource(reply.attr.to) == nick)) then |
|
e53f0967520c
mod_push: Allow filtering pushes for particular chats
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6214
diff
changeset
|
412 does_match = true |
|
e53f0967520c
mod_push: Allow filtering pushes for particular chats
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6214
diff
changeset
|
413 end |
|
e53f0967520c
mod_push: Allow filtering pushes for particular chats
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6214
diff
changeset
|
414 end |
|
e53f0967520c
mod_push: Allow filtering pushes for particular chats
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6214
diff
changeset
|
415 end |
|
e53f0967520c
mod_push: Allow filtering pushes for particular chats
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6214
diff
changeset
|
416 |
|
5682
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
417 if does_match and not sends_added[match.send] then |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
418 sends_added[match.send] = true |
|
6266
6164849469f1
mod2_push2: suppress send if none match
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6233
diff
changeset
|
419 any_match = true |
|
5682
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
420 if match.send == "urn:xmpp:push2:send:notify-only" then |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
421 -- Nothing more to add |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
422 elseif match.send == "urn:xmpp:push2:send:sce+rfc8291+rfc8292:0" then |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
423 add_sce_rfc8291(match, stanza, push_notification_payload) |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
424 add_rfc8292(match, stanza, push_notification_payload) |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
425 else |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
426 module:log("debug", "Unkonwn send profile: " .. push_info.send) |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
427 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
428 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
429 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
430 |
|
6266
6164849469f1
mod2_push2: suppress send if none match
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6233
diff
changeset
|
431 if any_match then |
|
6164849469f1
mod2_push2: suppress send if none match
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6233
diff
changeset
|
432 local push_publish = st.message({ to = push_info.service, from = module.host, id = uuid.generate() }) |
|
6164849469f1
mod2_push2: suppress send if none match
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6233
diff
changeset
|
433 :add_child(push_notification_payload):up() |
|
5682
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
434 |
|
6266
6164849469f1
mod2_push2: suppress send if none match
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6233
diff
changeset
|
435 -- TODO: watch for message error replies and count or something |
|
6164849469f1
mod2_push2: suppress send if none match
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6233
diff
changeset
|
436 module:send(push_publish) |
|
6164849469f1
mod2_push2: suppress send if none match
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6233
diff
changeset
|
437 pushes = pushes + 1 |
|
6164849469f1
mod2_push2: suppress send if none match
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6233
diff
changeset
|
438 end |
|
5682
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
439 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
440 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
441 |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
442 return pushes |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
443 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
444 |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
445 -- small helper function to extract relevant push settings |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
446 local function get_push_settings(stanza, session) |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
447 local to = stanza.attr.to |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
448 local node = to and jid.split(to) or session.username |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
449 local user_push_services = push2_registrations:get(node) |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
450 return node, (user_push_services or {}) |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
451 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
452 |
|
5687
4b052598e435
mod_push2: restore offline message hook
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
5686
diff
changeset
|
453 -- publish on offline message |
|
4b052598e435
mod_push2: restore offline message hook
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
5686
diff
changeset
|
454 module:hook("message/offline/handle", function(event) |
|
4b052598e435
mod_push2: restore offline message hook
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
5686
diff
changeset
|
455 local node, user_push_services = get_push_settings(event.stanza, event.origin); |
|
4b052598e435
mod_push2: restore offline message hook
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
5686
diff
changeset
|
456 module:log("debug", "Invoking handle_notify_request() for offline stanza"); |
|
6213
811bd0872682
mod_push2: Do not push MUC reflections
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6212
diff
changeset
|
457 handle_notify_request(event.stanza, node, user_push_services, event.origin, true); |
|
5687
4b052598e435
mod_push2: restore offline message hook
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
5686
diff
changeset
|
458 end, 1); |
|
4b052598e435
mod_push2: restore offline message hook
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
5686
diff
changeset
|
459 |
|
5682
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
460 -- publish on bare groupchat |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
461 -- this picks up MUC messages when there are no devices connected |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
462 module:hook("message/bare/groupchat", function(event) |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
463 local node, user_push_services = get_push_settings(event.stanza, event.origin); |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
464 local notify_push_services = {}; |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
465 for identifier, push_info in pairs(user_push_services) do |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
466 for _, match in ipairs(push_info.matches) do |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
467 if match.match == "urn:xmpp:push2:match:archived-with-body" or match.match == "urn:xmpp:push2:match:archived" then |
|
5998
fd1927e51791
mod_push2: fix broken log statement
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
5869
diff
changeset
|
468 module:log("debug", "Not pushing because we are not archiving this stanza") |
|
5682
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
469 else |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
470 notify_push_services[identifier] = push_info; |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
471 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
472 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
473 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
474 |
|
6213
811bd0872682
mod_push2: Do not push MUC reflections
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6212
diff
changeset
|
475 handle_notify_request(event.stanza, node, notify_push_services, event.origin, true); |
|
5682
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
476 end, 1); |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
477 |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
478 local function process_stanza_queue(queue, session, queue_type) |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
479 if not session.push_registration_id then return; end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
480 local notified = { unimportant = false; important = false } |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
481 for i=1, #queue do |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
482 local stanza = queue[i]; |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
483 -- fast ignore of already pushed stanzas |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
484 if stanza and not (stanza._push_notify2 and stanza._push_notify2[session.push_registration_id]) then |
|
6035
9b50ee822638
mod_push2: fix push during smacks hibernate
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6034
diff
changeset
|
485 local node, all_push_services = get_push_settings(stanza, session) |
|
9b50ee822638
mod_push2: fix push during smacks hibernate
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6034
diff
changeset
|
486 local user_push_services = {[session.push_registration_id] = all_push_services[session.push_registration_id]} |
|
5682
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
487 local stanza_type = "unimportant"; |
|
6213
811bd0872682
mod_push2: Do not push MUC reflections
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6212
diff
changeset
|
488 if is_important(stanza, session) then stanza_type = "important"; end |
|
5682
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
489 if not notified[stanza_type] then -- only notify if we didn't try to push for this stanza type already |
|
6213
811bd0872682
mod_push2: Do not push MUC reflections
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6212
diff
changeset
|
490 if handle_notify_request(stanza, node, user_push_services, session, false) ~= 0 then |
|
5682
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
491 if session.hibernating and not session.first_hibernated_push then |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
492 -- if the message was important |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
493 -- then record the time of first push in the session for the smack module which will extend its hibernation |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
494 -- timeout based on the value of session.first_hibernated_push |
|
6214
fe9f2c618e8a
mod_push2: option to keep hibernating after first push
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6213
diff
changeset
|
495 if is_important(stanza, session) and not hibernate_past_first_push then |
|
5682
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
496 session.first_hibernated_push = os_time(); |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
497 -- check for prosody 0.12 mod_smacks |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
498 if session.hibernating_watchdog and session.original_smacks_callback and session.original_smacks_timeout then |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
499 -- restore old smacks watchdog (--> the start of our original timeout will be delayed until first push) |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
500 session.hibernating_watchdog:cancel(); |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
501 session.hibernating_watchdog = watchdog.new(session.original_smacks_timeout, session.original_smacks_callback); |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
502 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
503 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
504 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
505 notified[stanza_type] = true |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
506 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
507 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
508 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
509 if notified.unimportant and notified.important then break; end -- stop processing the queue if all push types are exhausted |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
510 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
511 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
512 |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
513 -- publish on unacked smacks message (use timer to send out push for all stanzas submitted in a row only once) |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
514 local function process_stanza(session, stanza) |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
515 if session.push_registration_id then |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
516 session.log("debug", "adding new stanza to push_queue"); |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
517 if not session.push_queue then session.push_queue = {}; end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
518 local queue = session.push_queue; |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
519 queue[#queue+1] = st.clone(stanza); |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
520 if not session.awaiting_push_timer then -- timer not already running --> start new timer |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
521 session.awaiting_push_timer = module:add_timer(1.0, function () |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
522 process_stanza_queue(session.push_queue, session, "push"); |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
523 session.push_queue = {}; -- clean up queue after push |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
524 session.awaiting_push_timer = nil; |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
525 end); |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
526 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
527 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
528 return stanza; |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
529 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
530 |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
531 local function process_smacks_stanza(event) |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
532 local session = event.origin; |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
533 local stanza = event.stanza; |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
534 if not session.push_registration_id then |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
535 session.log("debug", "NOT invoking handle_notify_request() for newly smacks queued stanza (session.push_registration_id is not set: %s)", |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
536 session.push_registration_id |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
537 ); |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
538 else |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
539 process_stanza(session, stanza) |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
540 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
541 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
542 |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
543 -- smacks hibernation is started |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
544 local function hibernate_session(event) |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
545 local session = event.origin; |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
546 local queue = event.queue; |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
547 session.first_hibernated_push = nil; |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
548 if session.push_registration_id and session.hibernating_watchdog then -- check for prosody 0.12 mod_smacks |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
549 -- save old watchdog callback and timeout |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
550 session.original_smacks_callback = session.hibernating_watchdog.callback; |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
551 session.original_smacks_timeout = session.hibernating_watchdog.timeout; |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
552 -- cancel old watchdog and create a new watchdog with extended timeout |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
553 session.hibernating_watchdog:cancel(); |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
554 session.hibernating_watchdog = watchdog.new(extended_hibernation_timeout, function() |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
555 session.log("debug", "Push-extended smacks watchdog triggered"); |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
556 if session.original_smacks_callback then |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
557 session.log("debug", "Calling original smacks watchdog handler"); |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
558 session.original_smacks_callback(); |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
559 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
560 end); |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
561 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
562 -- process unacked stanzas |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
563 process_stanza_queue(queue, session, "smacks"); |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
564 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
565 |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
566 -- smacks hibernation is ended |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
567 local function restore_session(event) |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
568 local session = event.resumed; |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
569 if session then -- older smacks module versions send only the "intermediate" session in event.session and no session.resumed one |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
570 if session.awaiting_push_timer then |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
571 session.awaiting_push_timer:stop(); |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
572 session.awaiting_push_timer = nil; |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
573 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
574 session.first_hibernated_push = nil; |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
575 -- the extended smacks watchdog will be canceled by the smacks module, no need to anything here |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
576 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
577 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
578 |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
579 -- smacks ack is delayed |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
580 local function ack_delayed(event) |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
581 local session = event.origin; |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
582 local queue = event.queue; |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
583 local stanza = event.stanza; |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
584 if not session.push_registration_id then return; end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
585 if stanza then process_stanza(session, stanza); return; end -- don't iterate through smacks queue if we know which stanza triggered this |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
586 for i=1, #queue do |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
587 local queued_stanza = queue[i]; |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
588 -- process unacked stanzas (handle_notify_request() will only send push requests for new stanzas) |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
589 process_stanza(session, queued_stanza); |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
590 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
591 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
592 |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
593 -- archive message added |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
594 local function archive_message_added(event) |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
595 -- event is: { origin = origin, stanza = stanza, for_user = store_user, id = id } |
|
5687
4b052598e435
mod_push2: restore offline message hook
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
5686
diff
changeset
|
596 -- only notify for new mam messages when at least one device is online |
|
4b052598e435
mod_push2: restore offline message hook
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
5686
diff
changeset
|
597 if not event.for_user or not host_sessions[event.for_user] then return; end |
|
5682
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
598 -- Note that the stanza in the event is a clone not the same as other hooks, so dedupe doesn't work |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
599 -- This is a problem if you wan to to also hook offline message storage for example |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
600 local stanza = st.clone(event.stanza) |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
601 stanza:tag("stanza-id", { xmlns = "urn:xmpp:sid:0", by = event.for_user.."@"..module.host, id = event.id }):up() |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
602 local user_session = host_sessions[event.for_user] and host_sessions[event.for_user].sessions or {} |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
603 local to = stanza.attr.to |
|
6186
1cf563a94620
mod_push2: this expression was throwing away the second value
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6185
diff
changeset
|
604 local to_user, to_host = jid.split(to) |
|
6185
c887820cd884
Check both localpart and host
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6035
diff
changeset
|
605 to_user = to_user or event.origin.username |
|
c887820cd884
Check both localpart and host
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6035
diff
changeset
|
606 to_host = to_host or module.host |
|
5682
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
607 |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
608 -- only notify if the stanza destination is the mam user we store it for |
|
6185
c887820cd884
Check both localpart and host
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6035
diff
changeset
|
609 if event.for_user == to_user and to_host == module.host then |
|
c887820cd884
Check both localpart and host
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6035
diff
changeset
|
610 local user_push_services = push2_registrations:get(to_user) or {} |
|
5682
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
611 |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
612 -- Urgent stanzas are time-sensitive (e.g. calls) and should |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
613 -- be pushed immediately to avoid getting stuck in the smacks |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
614 -- queue in case of dead connections, for example |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
615 local is_voip_stanza, urgent_reason = is_voip(stanza); |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
616 |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
617 local notify_push_services; |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
618 if is_voip_stanza then |
|
6185
c887820cd884
Check both localpart and host
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6035
diff
changeset
|
619 module:log("debug", "Urgent push for %s@%s (%s)", to_user, to_host, urgent_reason); |
|
5682
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
620 notify_push_services = user_push_services; |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
621 else |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
622 -- only notify nodes with no active sessions (smacks is counted as active and handled separate) |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
623 notify_push_services = {}; |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
624 for identifier, push_info in pairs(user_push_services) do |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
625 local identifier_found = nil; |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
626 for _, session in pairs(user_session) do |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
627 if session.push_registration_id == identifier then |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
628 identifier_found = session; |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
629 break; |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
630 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
631 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
632 if identifier_found then |
|
6034
b4bf44765ce6
mod_push2: fix some log lines
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6033
diff
changeset
|
633 module:log("debug", "Not pushing '%s' of new MAM stanza (session still alive)", identifier) |
|
5682
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
634 else |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
635 notify_push_services[identifier] = push_info |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
636 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
637 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
638 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
639 |
|
6213
811bd0872682
mod_push2: Do not push MUC reflections
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6212
diff
changeset
|
640 handle_notify_request(stanza, to_user, notify_push_services, event.origin, true); |
|
5682
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
641 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
642 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
643 |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
644 module:hook("smacks-hibernation-start", hibernate_session); |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
645 module:hook("smacks-hibernation-end", restore_session); |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
646 module:hook("smacks-ack-delayed", ack_delayed); |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
647 module:hook("smacks-hibernation-stanza-queued", process_smacks_stanza); |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
648 module:hook("archive-message-added", archive_message_added); |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
649 |
|
6219
06621ab30be0
mod_push2: Improve session activity tracking
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6218
diff
changeset
|
650 local function track_activity(event) |
|
06621ab30be0
mod_push2: Improve session activity tracking
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6218
diff
changeset
|
651 if has_body(event.stanza) or event.stanza:child_with_ns("http://jabber.org/protocol/chatstates") then |
|
06621ab30be0
mod_push2: Improve session activity tracking
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6218
diff
changeset
|
652 event.origin.last_activity = os_time() |
|
06621ab30be0
mod_push2: Improve session activity tracking
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6218
diff
changeset
|
653 end |
|
06621ab30be0
mod_push2: Improve session activity tracking
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6218
diff
changeset
|
654 end |
|
06621ab30be0
mod_push2: Improve session activity tracking
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6218
diff
changeset
|
655 |
|
06621ab30be0
mod_push2: Improve session activity tracking
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6218
diff
changeset
|
656 module:hook("pre-message/bare", track_activity) |
|
06621ab30be0
mod_push2: Improve session activity tracking
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6218
diff
changeset
|
657 module:hook("pre-message/full", track_activity) |
|
06621ab30be0
mod_push2: Improve session activity tracking
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
6218
diff
changeset
|
658 |
|
5682
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
659 module:log("info", "Module loaded"); |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
660 function module.unload() |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
661 module:log("info", "Unloading module"); |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
662 -- cleanup some settings, reloading this module can cause process_smacks_stanza() to stop working otherwise |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
663 for user, _ in pairs(host_sessions) do |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
664 for _, session in pairs(host_sessions[user].sessions) do |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
665 if session.awaiting_push_timer then session.awaiting_push_timer:stop(); end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
666 session.awaiting_push_timer = nil; |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
667 session.push_queue = nil; |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
668 session.first_hibernated_push = nil; |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
669 -- check for prosody 0.12 mod_smacks |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
670 if session.hibernating_watchdog and session.original_smacks_callback and session.original_smacks_timeout then |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
671 -- restore old smacks watchdog |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
672 session.hibernating_watchdog:cancel(); |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
673 session.hibernating_watchdog = watchdog.new(session.original_smacks_timeout, session.original_smacks_callback); |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
674 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
675 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
676 end |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
677 module:log("info", "Module unloaded"); |
|
4d1a3de56c3d
Initial work on Push 2.0
Stephen Paul Weber <singpolyma@singpolyma.net>
parents:
diff
changeset
|
678 end |