comparison mod_cloud_notify/mod_cloud_notify.lua @ 2141:218a3d3f7f97

mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
author tmolitor <thilo@eightysoft.de>
date Tue, 29 Mar 2016 19:47:08 +0200
parents cb0fc00a7086
children eb53a830864c
comparison
equal deleted inserted replaced
2140:3a94b3cd31e2 2141:218a3d3f7f97
4 -- This file is MIT/X11 licensed. 4 -- This file is MIT/X11 licensed.
5 5
6 local st = require"util.stanza"; 6 local st = require"util.stanza";
7 local jid = require"util.jid"; 7 local jid = require"util.jid";
8 local dataform = require"util.dataforms".new; 8 local dataform = require"util.dataforms".new;
9 local filters = require "util.filters";
9 10
10 local xmlns_push = "urn:xmpp:push:0"; 11 local xmlns_push = "urn:xmpp:push:0";
11 12
12 -- configuration 13 -- configuration
13 local include_body = module:get_option("push_notification_with_body", true); 14 local include_body = module:get_option("push_notification_with_body", true);
76 { name = "last-message-sender"; type = "jid-single"; }; 77 { name = "last-message-sender"; type = "jid-single"; };
77 { name = "last-message-body"; type = "text-single"; }; 78 { name = "last-message-body"; type = "text-single"; };
78 }; 79 };
79 80
80 -- http://xmpp.org/extensions/xep-0357.html#publishing 81 -- http://xmpp.org/extensions/xep-0357.html#publishing
81 module:hook("message/offline/handle", function(event) 82 local function handle_notify_request(origin, stanza)
82 local origin, stanza = event.origin, event.stanza;
83 local to = stanza.attr.to; 83 local to = stanza.attr.to;
84 local node = to and jid.split(to) or origin.username; 84 local node = to and jid.split(to) or origin.username;
85 local user_push_services = push_enabled[node]; 85 local user_push_services = push_enabled[node];
86 if not user_push_services then return end 86 if not user_push_services then return end
87 87
109 if push_info.options then 109 if push_info.options then
110 push_publish:tag("publish-options"):add_child(push_info.options); 110 push_publish:tag("publish-options"):add_child(push_info.options);
111 end 111 end
112 module:send(push_publish); 112 module:send(push_publish);
113 end 113 end
114 end
115
116 -- publish on offline message
117 module:hook("message/offline/handle", function(event)
118 if event.stanza._notify then
119 event.stanza._notify = nil;
120 return;
121 end
122 return handle_notify_request(event.origin, event.stanza);
114 end, 1); 123 end, 1);
124
125 -- publish on unacked smacks message
126 local function process_new_stanza(stanza, session)
127 if getmetatable(stanza) ~= st.stanza_mt then
128 return stanza; -- Things we don't want to touch
129 end
130 if stanza.name == "message" and stanza.attr.xmlns == nil and
131 ( stanza.attr.type == "chat" or ( stanza.attr.type or "normal" ) == "normal" ) and
132 -- not already notified via cloud
133 not stanza._notify then
134 stanza._notify = true;
135 session.log("debug", "Invoking cloud handle_notify_request for new smacks hibernated stanza...");
136 handle_notify_request(session, stanza)
137 end
138 return stanza;
139 end
140
141 -- smacks hibernation is started
142 local function hibernate_session(event)
143 local session = event.origin;
144 local queue = event.queue;
145 -- process already unacked stanzas
146 for i=1,#queue do
147 process_new_stanza(queue[i], session);
148 end
149 -- process future unacked (hibernated) stanzas
150 filters.add_filter(session, "stanzas/out", process_new_stanza);
151 end
152
153 -- smacks hibernation is ended
154 local function restore_session(event)
155 local session = event.origin;
156 filters.remove_filter(session, "stanzas/out", process_new_stanza);
157 end
158
159 module:hook("smacks-hibernation-start", hibernate_session);
160 module:hook("smacks-hibernation-end", restore_session);
161
115 162
116 module:hook("message/offline/broadcast", function(event) 163 module:hook("message/offline/broadcast", function(event)
117 local user_push_services = push_enabled[event.origin.username]; 164 local user_push_services = push_enabled[event.origin.username];
118 if not user_push_services then return end 165 if not user_push_services then return end
119 166