Mercurial > prosody-modules
annotate mod_cloud_notify/mod_cloud_notify.lua @ 2258:3abc51faf945
mod_cloud_notify: Log message if no dataform is found
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Thu, 28 Jul 2016 12:35:57 +0200 |
parents | f84b51f9aa82 |
children | e0808be13d77 |
rev | line source |
---|---|
1783
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
1 -- XEP-0357: Push (aka: My mobile OS vendor won't let me have persistent TCP connections) |
2247
d09014d8c901
mod_cloud_notify: Update copyright year
Kim Alvefur <zash@zash.se>
parents:
2246
diff
changeset
|
2 -- Copyright (C) 2015-2016 Kim Alvefur |
1783
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
3 -- |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
4 -- This file is MIT/X11 licensed. |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
5 |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
6 local st = require"util.stanza"; |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
7 local jid = require"util.jid"; |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
8 local dataform = require"util.dataforms".new; |
2141
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
9 local filters = require "util.filters"; |
1783
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
10 |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
11 local xmlns_push = "urn:xmpp:push:0"; |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
12 |
1909
c7389fe74de7
mod_cloud_notify: Make inclusion of message sender and body optional via config option
Kim Alvefur <zash@zash.se>
parents:
1908
diff
changeset
|
13 -- configuration |
2246
a3e3dc9131e7
mod_cloud_notify: Use typed config API
Kim Alvefur <zash@zash.se>
parents:
2201
diff
changeset
|
14 local include_body = module:get_option_boolean("push_notification_with_body", false); |
a3e3dc9131e7
mod_cloud_notify: Use typed config API
Kim Alvefur <zash@zash.se>
parents:
2201
diff
changeset
|
15 local include_sender = module:get_option_boolean("push_notification_with_sender", false); |
1909
c7389fe74de7
mod_cloud_notify: Make inclusion of message sender and body optional via config option
Kim Alvefur <zash@zash.se>
parents:
1908
diff
changeset
|
16 |
1908
eba279ddc050
mod_cloud_notify: Add some comments describing code blocks
Kim Alvefur <zash@zash.se>
parents:
1907
diff
changeset
|
17 -- For keeping state across reloads |
2201
eb5555a3a535
mod_cloud_notify: Enable persistent storage of user notification settings
Kim Alvefur <zash@zash.se>
parents:
2200
diff
changeset
|
18 local push_enabled = module:open_store(); |
eb5555a3a535
mod_cloud_notify: Enable persistent storage of user notification settings
Kim Alvefur <zash@zash.se>
parents:
2200
diff
changeset
|
19 -- TODO map store would be better here |
1783
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
20 |
1908
eba279ddc050
mod_cloud_notify: Add some comments describing code blocks
Kim Alvefur <zash@zash.se>
parents:
1907
diff
changeset
|
21 -- http://xmpp.org/extensions/xep-0357.html#disco |
2200
e9e38ae8037f
mod_cloud_notify: Advertise feature on bare jid disco (thanks iNPUTmice)
Kim Alvefur <zash@zash.se>
parents:
2198
diff
changeset
|
22 module:hook("account-disco-info", function(event) |
e9e38ae8037f
mod_cloud_notify: Advertise feature on bare jid disco (thanks iNPUTmice)
Kim Alvefur <zash@zash.se>
parents:
2198
diff
changeset
|
23 (event.reply or event.stanza):tag("feature", {var=xmlns_push}):up(); |
e9e38ae8037f
mod_cloud_notify: Advertise feature on bare jid disco (thanks iNPUTmice)
Kim Alvefur <zash@zash.se>
parents:
2198
diff
changeset
|
24 end); |
1783
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
25 |
1907
7fe7bd7b33b6
mod_cloud_notify: Allow the 'node' to be left out when disabling, clients MAY include it (and therefore leave it out)
Kim Alvefur <zash@zash.se>
parents:
1784
diff
changeset
|
26 -- http://xmpp.org/extensions/xep-0357.html#enabling |
1783
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
27 module:hook("iq-set/self/"..xmlns_push..":enable", function (event) |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
28 local origin, stanza = event.origin, event.stanza; |
2254
122cb5f4930f
mod_cloud_notify: Cache <enable> in local
Kim Alvefur <zash@zash.se>
parents:
2253
diff
changeset
|
29 local enable = stanza.tags[1]; |
2252
a96f2d0f8750
mod_cloud_notify: Add some logging when a client attempts to enable push notifications
Kim Alvefur <zash@zash.se>
parents:
2247
diff
changeset
|
30 origin.log("debug", "Attempting to enable push notifications"); |
1907
7fe7bd7b33b6
mod_cloud_notify: Allow the 'node' to be left out when disabling, clients MAY include it (and therefore leave it out)
Kim Alvefur <zash@zash.se>
parents:
1784
diff
changeset
|
31 -- MUST contain a 'jid' attribute of the XMPP Push Service being enabled |
2254
122cb5f4930f
mod_cloud_notify: Cache <enable> in local
Kim Alvefur <zash@zash.se>
parents:
2253
diff
changeset
|
32 local push_jid = enable.attr.jid; |
1907
7fe7bd7b33b6
mod_cloud_notify: Allow the 'node' to be left out when disabling, clients MAY include it (and therefore leave it out)
Kim Alvefur <zash@zash.se>
parents:
1784
diff
changeset
|
33 -- SHOULD contain a 'node' attribute |
2254
122cb5f4930f
mod_cloud_notify: Cache <enable> in local
Kim Alvefur <zash@zash.se>
parents:
2253
diff
changeset
|
34 local push_node = enable.attr.node; |
1907
7fe7bd7b33b6
mod_cloud_notify: Allow the 'node' to be left out when disabling, clients MAY include it (and therefore leave it out)
Kim Alvefur <zash@zash.se>
parents:
1784
diff
changeset
|
35 if not push_jid then |
2257
f84b51f9aa82
mod_cloud_notify: Log message when 'jid' is missing from enable request
Kim Alvefur <zash@zash.se>
parents:
2255
diff
changeset
|
36 origin.log("debug", "Push notification enable request missing the 'jid' field"); |
1907
7fe7bd7b33b6
mod_cloud_notify: Allow the 'node' to be left out when disabling, clients MAY include it (and therefore leave it out)
Kim Alvefur <zash@zash.se>
parents:
1784
diff
changeset
|
37 origin.send(st.error_reply(stanza, "modify", "bad-request", "Missing jid")); |
1783
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
38 return true; |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
39 end |
2255
cdfc917a8cc7
mod_cloud_notify: Retrieve data form by name and namespace so unknown elements are ignored
Kim Alvefur <zash@zash.se>
parents:
2254
diff
changeset
|
40 local publish_options = enable:get_child("x", "jabber:x:data"); |
2258
3abc51faf945
mod_cloud_notify: Log message if no dataform is found
Kim Alvefur <zash@zash.se>
parents:
2257
diff
changeset
|
41 if not publish_options then |
3abc51faf945
mod_cloud_notify: Log message if no dataform is found
Kim Alvefur <zash@zash.se>
parents:
2257
diff
changeset
|
42 -- Could be intentional |
3abc51faf945
mod_cloud_notify: Log message if no dataform is found
Kim Alvefur <zash@zash.se>
parents:
2257
diff
changeset
|
43 origin.log("debug", "No publish options in request"); |
3abc51faf945
mod_cloud_notify: Log message if no dataform is found
Kim Alvefur <zash@zash.se>
parents:
2257
diff
changeset
|
44 end |
2201
eb5555a3a535
mod_cloud_notify: Enable persistent storage of user notification settings
Kim Alvefur <zash@zash.se>
parents:
2200
diff
changeset
|
45 local user_push_services = push_enabled:get(origin.username); |
1783
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
46 if not user_push_services then |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
47 user_push_services = {}; |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
48 end |
1907
7fe7bd7b33b6
mod_cloud_notify: Allow the 'node' to be left out when disabling, clients MAY include it (and therefore leave it out)
Kim Alvefur <zash@zash.se>
parents:
1784
diff
changeset
|
49 user_push_services[push_jid .. "<" .. (push_node or "")] = { |
1783
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
50 jid = push_jid; |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
51 node = push_node; |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
52 count = 0; |
2253
97ebd28a8a75
mod_cloud_notify: Apply pre-serialization to publish-options
Kim Alvefur <zash@zash.se>
parents:
2252
diff
changeset
|
53 options = publish_options and st.preserialize(publish_options); |
1783
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
54 }; |
2201
eb5555a3a535
mod_cloud_notify: Enable persistent storage of user notification settings
Kim Alvefur <zash@zash.se>
parents:
2200
diff
changeset
|
55 local ok, err = push_enabled:set(origin.username, user_push_services); |
eb5555a3a535
mod_cloud_notify: Enable persistent storage of user notification settings
Kim Alvefur <zash@zash.se>
parents:
2200
diff
changeset
|
56 if not ok then |
eb5555a3a535
mod_cloud_notify: Enable persistent storage of user notification settings
Kim Alvefur <zash@zash.se>
parents:
2200
diff
changeset
|
57 origin.send(st.error_reply(stanza, "wait", "internal-server-error")); |
eb5555a3a535
mod_cloud_notify: Enable persistent storage of user notification settings
Kim Alvefur <zash@zash.se>
parents:
2200
diff
changeset
|
58 else |
2252
a96f2d0f8750
mod_cloud_notify: Add some logging when a client attempts to enable push notifications
Kim Alvefur <zash@zash.se>
parents:
2247
diff
changeset
|
59 origin.log("info", "Push notifications enabled"); |
2201
eb5555a3a535
mod_cloud_notify: Enable persistent storage of user notification settings
Kim Alvefur <zash@zash.se>
parents:
2200
diff
changeset
|
60 origin.send(st.reply(stanza)); |
eb5555a3a535
mod_cloud_notify: Enable persistent storage of user notification settings
Kim Alvefur <zash@zash.se>
parents:
2200
diff
changeset
|
61 end |
1783
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
62 return true; |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
63 end); |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
64 |
1907
7fe7bd7b33b6
mod_cloud_notify: Allow the 'node' to be left out when disabling, clients MAY include it (and therefore leave it out)
Kim Alvefur <zash@zash.se>
parents:
1784
diff
changeset
|
65 -- http://xmpp.org/extensions/xep-0357.html#disabling |
1783
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
66 module:hook("iq-set/self/"..xmlns_push..":disable", function (event) |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
67 local origin, stanza = event.origin, event.stanza; |
1907
7fe7bd7b33b6
mod_cloud_notify: Allow the 'node' to be left out when disabling, clients MAY include it (and therefore leave it out)
Kim Alvefur <zash@zash.se>
parents:
1784
diff
changeset
|
68 local push_jid = stanza.tags[1].attr.jid; -- MUST include a 'jid' attribute |
7fe7bd7b33b6
mod_cloud_notify: Allow the 'node' to be left out when disabling, clients MAY include it (and therefore leave it out)
Kim Alvefur <zash@zash.se>
parents:
1784
diff
changeset
|
69 local push_node = stanza.tags[1].attr.node; -- A 'node' attribute MAY be included |
7fe7bd7b33b6
mod_cloud_notify: Allow the 'node' to be left out when disabling, clients MAY include it (and therefore leave it out)
Kim Alvefur <zash@zash.se>
parents:
1784
diff
changeset
|
70 if not push_jid then |
7fe7bd7b33b6
mod_cloud_notify: Allow the 'node' to be left out when disabling, clients MAY include it (and therefore leave it out)
Kim Alvefur <zash@zash.se>
parents:
1784
diff
changeset
|
71 origin.send(st.error_reply(stanza, "modify", "bad-request", "Missing jid")); |
1783
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
72 return true; |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
73 end |
2201
eb5555a3a535
mod_cloud_notify: Enable persistent storage of user notification settings
Kim Alvefur <zash@zash.se>
parents:
2200
diff
changeset
|
74 local user_push_services = push_enabled:get(origin.username); |
1907
7fe7bd7b33b6
mod_cloud_notify: Allow the 'node' to be left out when disabling, clients MAY include it (and therefore leave it out)
Kim Alvefur <zash@zash.se>
parents:
1784
diff
changeset
|
75 for key, push_info in pairs(user_push_services) do |
7fe7bd7b33b6
mod_cloud_notify: Allow the 'node' to be left out when disabling, clients MAY include it (and therefore leave it out)
Kim Alvefur <zash@zash.se>
parents:
1784
diff
changeset
|
76 if push_info.jid == push_jid and (not push_node or push_info.node == push_node) then |
7fe7bd7b33b6
mod_cloud_notify: Allow the 'node' to be left out when disabling, clients MAY include it (and therefore leave it out)
Kim Alvefur <zash@zash.se>
parents:
1784
diff
changeset
|
77 user_push_services[key] = nil; |
7fe7bd7b33b6
mod_cloud_notify: Allow the 'node' to be left out when disabling, clients MAY include it (and therefore leave it out)
Kim Alvefur <zash@zash.se>
parents:
1784
diff
changeset
|
78 end |
1783
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
79 end |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
80 origin.send(st.reply(stanza)); |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
81 return true; |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
82 end); |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
83 |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
84 local push_form = dataform { |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
85 { name = "FORM_TYPE"; type = "hidden"; value = "urn:xmpp:push:summary"; }; |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
86 { name = "message-count"; type = "text-single"; }; |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
87 { name = "pending-subscription-count"; type = "text-single"; }; |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
88 { name = "last-message-sender"; type = "jid-single"; }; |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
89 { name = "last-message-body"; type = "text-single"; }; |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
90 }; |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
91 |
1907
7fe7bd7b33b6
mod_cloud_notify: Allow the 'node' to be left out when disabling, clients MAY include it (and therefore leave it out)
Kim Alvefur <zash@zash.se>
parents:
1784
diff
changeset
|
92 -- http://xmpp.org/extensions/xep-0357.html#publishing |
2141
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
93 local function handle_notify_request(origin, stanza) |
1783
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
94 local to = stanza.attr.to; |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
95 local node = to and jid.split(to) or origin.username; |
2201
eb5555a3a535
mod_cloud_notify: Enable persistent storage of user notification settings
Kim Alvefur <zash@zash.se>
parents:
2200
diff
changeset
|
96 local user_push_services = push_enabled:get(node); |
1783
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
97 if not user_push_services then return end |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
98 |
1784
1656d4fd71d0
mod_cloud_notify: Fix syntax errors and name
Kim Alvefur <zash@zash.se>
parents:
1783
diff
changeset
|
99 for _, push_info in pairs(user_push_services) do |
1783
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
100 push_info.count = push_info.count + 1; |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
101 local push_jid, push_node = push_info.jid, push_info.node; |
1923
b030e46ec640
mod_cloud_notify: Send notification from bare user JID per http://xmpp.org/extensions/xep-0357.html#publishing
Kim Alvefur <zash@zash.se>
parents:
1922
diff
changeset
|
102 local push_publish = st.iq({ to = push_jid, from = node .. "@" .. module.host, type = "set", id = "push" }) |
1783
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
103 :tag("pubsub", { xmlns = "http://jabber.org/protocol/pubsub" }) |
1924
c84cf61ca0f3
mod_cloud_notify: Wrap notification form in <item>
Kim Alvefur <zash@zash.se>
parents:
1923
diff
changeset
|
104 :tag("publish", { node = push_node }) |
2051
cb0fc00a7086
mod_cloud_notify: Fix syntax error
Kim Alvefur <zash@zash.se>
parents:
2050
diff
changeset
|
105 :tag("item") |
2050
49cc6a555dc7
mod_cloud_notify: Wrap form in namespaced element per the XEP (fixes #630)
Kim Alvefur <zash@zash.se>
parents:
2046
diff
changeset
|
106 :tag("notification", { xmlns = xmlns_push }); |
1909
c7389fe74de7
mod_cloud_notify: Make inclusion of message sender and body optional via config option
Kim Alvefur <zash@zash.se>
parents:
1908
diff
changeset
|
107 local form_data = { |
1783
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
108 ["message-count"] = tostring(push_info.count); |
1909
c7389fe74de7
mod_cloud_notify: Make inclusion of message sender and body optional via config option
Kim Alvefur <zash@zash.se>
parents:
1908
diff
changeset
|
109 }; |
c7389fe74de7
mod_cloud_notify: Make inclusion of message sender and body optional via config option
Kim Alvefur <zash@zash.se>
parents:
1908
diff
changeset
|
110 if include_sender then |
c7389fe74de7
mod_cloud_notify: Make inclusion of message sender and body optional via config option
Kim Alvefur <zash@zash.se>
parents:
1908
diff
changeset
|
111 form_data["last-message-sender"] = stanza.attr.from; |
c7389fe74de7
mod_cloud_notify: Make inclusion of message sender and body optional via config option
Kim Alvefur <zash@zash.se>
parents:
1908
diff
changeset
|
112 end |
c7389fe74de7
mod_cloud_notify: Make inclusion of message sender and body optional via config option
Kim Alvefur <zash@zash.se>
parents:
1908
diff
changeset
|
113 if include_body then |
c7389fe74de7
mod_cloud_notify: Make inclusion of message sender and body optional via config option
Kim Alvefur <zash@zash.se>
parents:
1908
diff
changeset
|
114 form_data["last-message-body"] = stanza:get_child_text("body"); |
c7389fe74de7
mod_cloud_notify: Make inclusion of message sender and body optional via config option
Kim Alvefur <zash@zash.se>
parents:
1908
diff
changeset
|
115 end |
c7389fe74de7
mod_cloud_notify: Make inclusion of message sender and body optional via config option
Kim Alvefur <zash@zash.se>
parents:
1908
diff
changeset
|
116 push_publish:add_child(push_form:form(form_data)); |
2050
49cc6a555dc7
mod_cloud_notify: Wrap form in namespaced element per the XEP (fixes #630)
Kim Alvefur <zash@zash.se>
parents:
2046
diff
changeset
|
117 push_publish:up(); -- / notification |
1783
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
118 push_publish:up(); -- / publish |
2046
980e5f5bd62c
mod_cloud_notify: put publish-options into <pubsub> not into <publish>
Daniel Gultsch <daniel@gultsch.de>
parents:
1924
diff
changeset
|
119 push_publish:up(); -- / pubsub |
1783
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
120 if push_info.options then |
2253
97ebd28a8a75
mod_cloud_notify: Apply pre-serialization to publish-options
Kim Alvefur <zash@zash.se>
parents:
2252
diff
changeset
|
121 push_publish:tag("publish-options"):add_child(st.deserialize(push_info.options)); |
1783
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
122 end |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
123 module:send(push_publish); |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
124 end |
2141
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
125 end |
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
126 |
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
127 -- publish on offline message |
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
128 module:hook("message/offline/handle", function(event) |
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
129 if event.stanza._notify then |
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
130 event.stanza._notify = nil; |
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
131 return; |
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
132 end |
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
133 return handle_notify_request(event.origin, event.stanza); |
1783
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
134 end, 1); |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
135 |
2141
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
136 -- publish on unacked smacks message |
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
137 local function process_new_stanza(stanza, session) |
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
138 if getmetatable(stanza) ~= st.stanza_mt then |
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
139 return stanza; -- Things we don't want to touch |
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
140 end |
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
141 if stanza.name == "message" and stanza.attr.xmlns == nil and |
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
142 ( stanza.attr.type == "chat" or ( stanza.attr.type or "normal" ) == "normal" ) and |
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
143 -- not already notified via cloud |
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
144 not stanza._notify then |
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
145 stanza._notify = true; |
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
146 session.log("debug", "Invoking cloud handle_notify_request for new smacks hibernated stanza..."); |
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
147 handle_notify_request(session, stanza) |
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
148 end |
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
149 return stanza; |
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
150 end |
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
151 |
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
152 -- smacks hibernation is started |
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
153 local function hibernate_session(event) |
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
154 local session = event.origin; |
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
155 local queue = event.queue; |
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
156 -- process already unacked stanzas |
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
157 for i=1,#queue do |
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
158 process_new_stanza(queue[i], session); |
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
159 end |
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
160 -- process future unacked (hibernated) stanzas |
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
161 filters.add_filter(session, "stanzas/out", process_new_stanza); |
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
162 end |
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
163 |
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
164 -- smacks hibernation is ended |
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
165 local function restore_session(event) |
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
166 local session = event.origin; |
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
167 filters.remove_filter(session, "stanzas/out", process_new_stanza); |
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
168 end |
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
169 |
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
170 module:hook("smacks-hibernation-start", hibernate_session); |
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
171 module:hook("smacks-hibernation-end", restore_session); |
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
172 |
218a3d3f7f97
mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents:
2051
diff
changeset
|
173 |
1783
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
174 module:hook("message/offline/broadcast", function(event) |
2201
eb5555a3a535
mod_cloud_notify: Enable persistent storage of user notification settings
Kim Alvefur <zash@zash.se>
parents:
2200
diff
changeset
|
175 local user_push_services = push_enabled:get(event.origin.username); |
1783
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
176 if not user_push_services then return end |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
177 |
1784
1656d4fd71d0
mod_cloud_notify: Fix syntax errors and name
Kim Alvefur <zash@zash.se>
parents:
1783
diff
changeset
|
178 for _, push_info in pairs(user_push_services) do |
1783
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
179 if push_info then |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
180 push_info.count = 0; |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
181 end |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
182 end |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
183 end, 1); |