Mercurial > prosody-modules
annotate mod_cloud_notify/mod_cloud_notify.lua @ 2253:97ebd28a8a75
mod_cloud_notify: Apply pre-serialization to publish-options
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Tue, 26 Jul 2016 13:29:51 +0200 |
parents | a96f2d0f8750 |
children | 122cb5f4930f |
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; |
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
|
29 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
|
30 -- MUST contain a 'jid' attribute of the XMPP Push Service being enabled |
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 local push_jid = stanza.tags[1].attr.jid; |
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
|
32 -- SHOULD contain a 'node' 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
|
33 local push_node = stanza.tags[1].attr.node; |
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
|
34 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
|
35 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
|
36 return true; |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
37 end |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
38 local publish_options = stanza.tags[1].tags[1]; |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
39 if publish_options and ( publish_options.name ~= "x" or publish_options.attr.xmlns ~= "jabber:x:data" ) then |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
40 origin.send(st.error_reply(stanza, "modify", "bad-request", "Invalid publish options")); |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
41 return true; |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
42 end |
2201
eb5555a3a535
mod_cloud_notify: Enable persistent storage of user notification settings
Kim Alvefur <zash@zash.se>
parents:
2200
diff
changeset
|
43 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
|
44 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
|
45 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
|
46 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
|
47 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
|
48 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
|
49 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
|
50 count = 0; |
2253
97ebd28a8a75
mod_cloud_notify: Apply pre-serialization to publish-options
Kim Alvefur <zash@zash.se>
parents:
2252
diff
changeset
|
51 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
|
52 }; |
2201
eb5555a3a535
mod_cloud_notify: Enable persistent storage of user notification settings
Kim Alvefur <zash@zash.se>
parents:
2200
diff
changeset
|
53 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
|
54 if not ok then |
eb5555a3a535
mod_cloud_notify: Enable persistent storage of user notification settings
Kim Alvefur <zash@zash.se>
parents:
2200
diff
changeset
|
55 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
|
56 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
|
57 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
|
58 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
|
59 end |
1783
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
60 return true; |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
61 end); |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
62 |
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
|
63 -- 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
|
64 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
|
65 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
|
66 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
|
67 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
|
68 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
|
69 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
|
70 return true; |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
71 end |
2201
eb5555a3a535
mod_cloud_notify: Enable persistent storage of user notification settings
Kim Alvefur <zash@zash.se>
parents:
2200
diff
changeset
|
72 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
|
73 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
|
74 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
|
75 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
|
76 end |
1783
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
77 end |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
78 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
|
79 return true; |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
80 end); |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
81 |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
82 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
|
83 { 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
|
84 { 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
|
85 { 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
|
86 { 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
|
87 { 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
|
88 }; |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
89 |
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
|
90 -- 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
|
91 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
|
92 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
|
93 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
|
94 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
|
95 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
|
96 |
1784
1656d4fd71d0
mod_cloud_notify: Fix syntax errors and name
Kim Alvefur <zash@zash.se>
parents:
1783
diff
changeset
|
97 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
|
98 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
|
99 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
|
100 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
|
101 :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
|
102 :tag("publish", { node = push_node }) |
2051
cb0fc00a7086
mod_cloud_notify: Fix syntax error
Kim Alvefur <zash@zash.se>
parents:
2050
diff
changeset
|
103 :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
|
104 :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
|
105 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
|
106 ["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
|
107 }; |
c7389fe74de7
mod_cloud_notify: Make inclusion of message sender and body optional via config option
Kim Alvefur <zash@zash.se>
parents:
1908
diff
changeset
|
108 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
|
109 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
|
110 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
|
111 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
|
112 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
|
113 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
|
114 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
|
115 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
|
116 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
|
117 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
|
118 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
|
119 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
|
120 end |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
121 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
|
122 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
|
123 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
|
124 |
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 -- 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
|
126 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
|
127 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
|
128 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
|
129 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
|
130 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
|
131 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
|
132 end, 1); |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
133 |
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
|
134 -- 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
|
135 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
|
136 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
|
137 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
|
138 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
|
139 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
|
140 ( 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
|
141 -- 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
|
142 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
|
143 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
|
144 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
|
145 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
|
146 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
|
147 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
|
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 |
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 -- 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
|
151 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
|
152 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
|
153 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
|
154 -- 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
|
155 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
|
156 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
|
157 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
|
158 -- 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
|
159 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
|
160 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
|
161 |
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 -- 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
|
163 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
|
164 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
|
165 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
|
166 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
|
167 |
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 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
|
169 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
|
170 |
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 |
1783
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
172 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
|
173 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
|
174 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
|
175 |
1784
1656d4fd71d0
mod_cloud_notify: Fix syntax errors and name
Kim Alvefur <zash@zash.se>
parents:
1783
diff
changeset
|
176 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
|
177 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
|
178 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
|
179 end |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
180 end |
b31fe2d22310
mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
181 end, 1); |