Skip to content

Instantly share code, notes, and snippets.

@legastero
Created May 28, 2014 07:42
Show Gist options
  • Save legastero/e62f1ec8ecd227867831 to your computer and use it in GitHub Desktop.
Save legastero/e62f1ec8ecd227867831 to your computer and use it in GitHub Desktop.
mod_push.lua
local st = require "util.stanza";
local jid_bare = require "util.jid".bare;
local xmlns_push = "urn:xmpp:push:0";
local full_sessions, bare_sessions = full_sessions, bare_sessions;
local recipients = {};
local function toggle_push(event)
local origin, stanza = event.origin, event.stanza;
local user = stanza.attr.to or (origin.username..'@'..origin.host);
local endpoint = stanza:get_child_text("register", xmlns_push);
local action = nil;
if stanza.tags[1].name == "register" then
action = true;
end
recipients[user] = recipients[user] or {};
recipients[user][endpoint] = action;
return origin.send(st.reply(stanza));
end
local function offline_handler(event)
local origin, stanza = event.origin, event.stanza;
local user = jid_bare(stanza.attr.to) or origin.username..'@'..origin.host;
module:log("debug", "Push notification for %s triggered.", user);
for recipient, _ in pairs(recipients[user] or {}) do
module:log("debug", "Push notification for %s sent to %s.", user, recipient);
local out = st.message({to = recipient, from = user})
:tag("push", {xmlns=xmlns_push})
:tag("body"):text("PUSH: " .. stanza:child_with_name("body"):get_text());
module:send(out);
end
end
module:hook("iq-set/self/"..xmlns_push..":register", toggle_push);
module:hook("iq-set/self/"..xmlns_push..":unregister", toggle_push);
module:hook("message/offline/handle", offline_handler, 1);
module:add_feature(xmlns_push);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment