annotate mod_cloud_notify/mod_mobile_notify.lua @ 1783:b31fe2d22310

mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
author Kim Alvefur <zash@zash.se>
date Mon, 24 Aug 2015 23:09:58 +0200
parents
children
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)
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2 -- Copyright (C) 2015 Kim Alvefur
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;
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10 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
11
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 module:add_feature(xmlns_push);
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 local push_enabled = module:shared("push-enabled-users");
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 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
17 local origin, stanza = event.origin, event.stanza;
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 local push_jid, push_node = stanza.tags[1].attr.jid, stanza.tags[1].attr.node;
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 if not push_jid or not push_node then
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 origin.send(st.error_reply(stanza, "modify", "bad-request", "Missing jid or node"));
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 return true;
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 end
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 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
24 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
25 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
26 return true;
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 end
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 local user_push_services = push_enabled[origin.username];
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 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
30 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
31 end
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 user_push_services[push_jid .. "<" .. push_node] = {
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 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
34 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
35 count = 0;
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 options = publish_options;
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 };
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 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
39 return true;
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 end);
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 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
43 local origin, stanza = event.origin, event.stanza;
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 local push_jid, push_node = stanza.tags[1].attr.jid, stanza.tags[1].attr.node;
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 push_jid or not push_node then
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 origin.send(st.error_reply(stanza, "modify", "bad-request", "Missing jid or node"));
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 return true;
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48 end
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 local user_push_services = push_enabled[origin.username];
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 if 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
51 user_push_services[push_jid .. "<" .. push_node] = nil;
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 end
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53 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
54 return true;
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55 end);
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57 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
58 { 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
59 { 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
60 { 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
61 { 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
62 { 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
63 };
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
64
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("message/offline/handle", 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;
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
67 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
68 local node = to and jid.split(to) or origin.username;
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69 local user_push_services = push_enabled[origin.username];
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
70 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
71
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72 for _, push_info in pairs(push_info) do
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
73 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
74 local push_jid, push_node = push_info.jid, push_info.node;
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
75 local push_publish = st.iq({ to = push_jid, from = module.host, type = "set", id = "push" })
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
76 :tag("pubsub", { xmlns = "http://jabber.org/protocol/pubsub" })
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
77 :tag("publish", { 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
78 push_publish:add_child(push_form:form({
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
79 ["message-count"] = tostring(push_info.count);
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
80 ["last-message-sender"] = stanza.attr.from;
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
81 ["last-message-body"] = stanza:get_child_text("body");
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 push_publish:up(); -- / publish
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
84 if push_info.options then
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
85 push_publish:tag("publish-options"):add_child(push_info.options);
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
86 end
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
87 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
88 end
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
89 end, 1);
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 module:hook("message/offline/broadcast", function(event)
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
92 local user_push_services = push_enabled[origin.username];
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
93 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
94
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
95 for _, push_info in pairs(push_info) do
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
96 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
97 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
98 end
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
99 end
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
100 end, 1);