annotate mod_cloud_notify/mod_cloud_notify.lua @ 2254:122cb5f4930f

mod_cloud_notify: Cache <enable> in local
author Kim Alvefur <zash@zash.se>
date Thu, 28 Jul 2016 12:10:09 +0200
parents 97ebd28a8a75
children cdfc917a8cc7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
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
36 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
37 return true;
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 end
2254
122cb5f4930f mod_cloud_notify: Cache <enable> in local
Kim Alvefur <zash@zash.se>
parents: 2253
diff changeset
39 local publish_options = enable.tags[1];
1783
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 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
41 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
42 return true;
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 end
2201
eb5555a3a535 mod_cloud_notify: Enable persistent storage of user notification settings
Kim Alvefur <zash@zash.se>
parents: 2200
diff changeset
44 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
45 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
46 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
47 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
48 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
49 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
50 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
51 count = 0;
2253
97ebd28a8a75 mod_cloud_notify: Apply pre-serialization to publish-options
Kim Alvefur <zash@zash.se>
parents: 2252
diff changeset
52 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
53 };
2201
eb5555a3a535 mod_cloud_notify: Enable persistent storage of user notification settings
Kim Alvefur <zash@zash.se>
parents: 2200
diff changeset
54 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
55 if not ok then
eb5555a3a535 mod_cloud_notify: Enable persistent storage of user notification settings
Kim Alvefur <zash@zash.se>
parents: 2200
diff changeset
56 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
57 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
58 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
59 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
60 end
1783
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
61 return true;
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
62 end);
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63
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
64 -- 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
65 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
66 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
67 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
68 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
69 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
70 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
71 return true;
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72 end
2201
eb5555a3a535 mod_cloud_notify: Enable persistent storage of user notification settings
Kim Alvefur <zash@zash.se>
parents: 2200
diff changeset
73 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
74 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
75 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
76 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
77 end
1783
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
78 end
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
79 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
80 return true;
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
81 end);
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
83 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
84 { 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
85 { 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
86 { 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
87 { 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
88 { 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
89 };
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
90
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
91 -- 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
92 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
93 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
94 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
95 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
96 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
97
1784
1656d4fd71d0 mod_cloud_notify: Fix syntax errors and name
Kim Alvefur <zash@zash.se>
parents: 1783
diff changeset
98 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
99 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
100 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
101 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
102 :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
103 :tag("publish", { node = push_node })
2051
cb0fc00a7086 mod_cloud_notify: Fix syntax error
Kim Alvefur <zash@zash.se>
parents: 2050
diff changeset
104 :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
105 :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
106 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
107 ["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
108 };
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 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
110 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
111 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
112 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
113 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
114 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
115 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
116 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
117 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
118 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
119 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
120 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
121 end
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
122 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
123 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
124 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
125
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 -- 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
127 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
128 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
129 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
130 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
131 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
132 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
133 end, 1);
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
134
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
135 -- 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
136 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
137 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
138 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
139 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
140 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
141 ( 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
142 -- 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
143 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
144 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
145 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
146 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
147 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
148 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
149 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
150
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 -- 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
152 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
153 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
154 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
155 -- 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
156 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
157 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
158 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
159 -- 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
160 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
161 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
162
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 -- 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
164 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
165 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
166 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
167 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
168
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-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
170 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
171
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
1783
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
173 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
174 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
175 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
176
1784
1656d4fd71d0 mod_cloud_notify: Fix syntax errors and name
Kim Alvefur <zash@zash.se>
parents: 1783
diff changeset
177 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
178 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
179 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
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
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
182 end, 1);