mod_cloud_notify/mod_cloud_notify.lua
author tmolitor <thilo@eightysoft.de>
Thu, 24 Aug 2017 19:06:21 +0200
changeset 2755 6b710a8bdf03
parent 2753 9756211fcbe3
child 2795 008cf272b7ea
permissions -rw-r--r--
mod_cloud_notify: Implement version 0.3 of XEP-0357
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1787
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)
2251
d09014d8c901 mod_cloud_notify: Update copyright year
Kim Alvefur <zash@zash.se>
parents: 2250
diff changeset
     2
-- Copyright (C) 2015-2016 Kim Alvefur
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
     3
-- Copyright (C) 2017 Thilo Molitor
1787
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     4
--
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     5
-- 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
     6
2740
fff185e7ab73 mod_cloud_notify: Implement the "stripped stanzas" proposal.
tmolitor <thilo@eightysoft.de>
parents: 2718
diff changeset
     7
local t_insert = table.insert;
1787
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     8
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
     9
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
    10
local dataform = require"util.dataforms".new;
2629
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
    11
local filters = require"util.filters";
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
    12
local hashes = require"util.hashes";
1787
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 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
    15
1913
c7389fe74de7 mod_cloud_notify: Make inclusion of message sender and body optional via config option
Kim Alvefur <zash@zash.se>
parents: 1912
diff changeset
    16
-- configuration
2250
a3e3dc9131e7 mod_cloud_notify: Use typed config API
Kim Alvefur <zash@zash.se>
parents: 2205
diff changeset
    17
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: 2205
diff changeset
    18
local include_sender = module:get_option_boolean("push_notification_with_sender", false);
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
    19
local max_push_errors = module:get_option_number("push_max_errors", 50);
1913
c7389fe74de7 mod_cloud_notify: Make inclusion of message sender and body optional via config option
Kim Alvefur <zash@zash.se>
parents: 1912
diff changeset
    20
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
    21
local host_sessions = prosody.hosts[module.host].sessions;
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
    22
local push_errors = {};
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
    23
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
    24
-- For keeping state across reloads while caching reads
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
    25
local push_store = (function()
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
    26
	local store = module:open_store();
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
    27
	local push_services = {};
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
    28
	local api = {};
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
    29
	function api:get(user)
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
    30
		if not push_services[user] then
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
    31
			local err;
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
    32
			push_services[user], err = store:get(user);
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
    33
			if not push_services[user] and err then
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
    34
				module:log("warn", "Error reading push notification storage for user '%s': %s", user, tostring(err));
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
    35
				push_services[user] = {};
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
    36
				return push_services[user], false;
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
    37
			end
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
    38
		end
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
    39
		if not push_services[user] then push_services[user] = {} end
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
    40
		return push_services[user], true;
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
    41
	end
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
    42
	function api:set(user, data)
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
    43
		push_services[user] = data;
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
    44
		local ok, err = store:set(user, push_services[user]);
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
    45
		if not ok then
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
    46
			module:log("error", "Error writing push notification storage for user '%s': %s", user, tostring(err));
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
    47
			return false;
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
    48
		end
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
    49
		return true;
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
    50
	end
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
    51
	function api:set_identifier(user, push_identifier, data)
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
    52
		local services = self:get(user);
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
    53
		services[push_identifier] = data;
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
    54
		return self:set(user, services);
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
    55
	end
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
    56
	return api;
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
    57
end)();
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
    58
2646
0f1421af7f6a mod_cloud_notify: Move declarations of handle_push_success/error to fix referencing undefined variables (introduced in 6ab46ff685d0)
Matthew Wild <mwild1@gmail.com>
parents: 2629
diff changeset
    59
-- Forward declarations, as both functions need to reference each other
0f1421af7f6a mod_cloud_notify: Move declarations of handle_push_success/error to fix referencing undefined variables (introduced in 6ab46ff685d0)
Matthew Wild <mwild1@gmail.com>
parents: 2629
diff changeset
    60
local handle_push_success, handle_push_error;
0f1421af7f6a mod_cloud_notify: Move declarations of handle_push_success/error to fix referencing undefined variables (introduced in 6ab46ff685d0)
Matthew Wild <mwild1@gmail.com>
parents: 2629
diff changeset
    61
0f1421af7f6a mod_cloud_notify: Move declarations of handle_push_success/error to fix referencing undefined variables (introduced in 6ab46ff685d0)
Matthew Wild <mwild1@gmail.com>
parents: 2629
diff changeset
    62
function handle_push_error(event)
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
    63
	local stanza = event.stanza;
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
    64
	local error_type, condition = stanza:get_error();
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
    65
	local node = jid.split(stanza.attr.to);
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
    66
	local from = stanza.attr.from;
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
    67
	local user_push_services = push_store:get(node);
2647
777d07e0cd73 mod_cloud_notify: Whitespace fixes
Matthew Wild <mwild1@gmail.com>
parents: 2646
diff changeset
    68
2629
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
    69
	for push_identifier, _ in pairs(user_push_services) do
2673
e6d243ed88ca mod_cloud_notify: Fix module:unhook calls not available in prosody 0.9, fixes #874
tmolitor <thilo@eightysoft.de>
parents: 2647
diff changeset
    70
		local stanza_id = hashes.sha256(push_identifier, true);
e6d243ed88ca mod_cloud_notify: Fix module:unhook calls not available in prosody 0.9, fixes #874
tmolitor <thilo@eightysoft.de>
parents: 2647
diff changeset
    71
		if stanza_id == stanza.attr.id then
2629
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
    72
			if user_push_services[push_identifier] and user_push_services[push_identifier].jid == from and error_type ~= "wait" then
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
    73
				push_errors[push_identifier] = push_errors[push_identifier] + 1;
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
    74
				module:log("info", "Got error of type '%s' (%s) for identifier '%s': "
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
    75
					.."error count for this identifier is now at %s", error_type, condition, push_identifier,
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
    76
					tostring(push_errors[push_identifier]));
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
    77
				if push_errors[push_identifier] >= max_push_errors then
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
    78
					module:log("warn", "Disabling push notifications for identifier '%s'", push_identifier);
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
    79
					-- remove push settings from sessions
2753
9756211fcbe3 mod_cloud_notify: Fix small bug.
tmolitor <thilo@eightysoft.de>
parents: 2740
diff changeset
    80
					if host_sessions[node] then
9756211fcbe3 mod_cloud_notify: Fix small bug.
tmolitor <thilo@eightysoft.de>
parents: 2740
diff changeset
    81
						for _, session in pairs(host_sessions[node].sessions) do
9756211fcbe3 mod_cloud_notify: Fix small bug.
tmolitor <thilo@eightysoft.de>
parents: 2740
diff changeset
    82
							if session.push_identifier == push_identifier then
9756211fcbe3 mod_cloud_notify: Fix small bug.
tmolitor <thilo@eightysoft.de>
parents: 2740
diff changeset
    83
								session.push_identifier = nil;
9756211fcbe3 mod_cloud_notify: Fix small bug.
tmolitor <thilo@eightysoft.de>
parents: 2740
diff changeset
    84
								session.push_settings = nil;
9756211fcbe3 mod_cloud_notify: Fix small bug.
tmolitor <thilo@eightysoft.de>
parents: 2740
diff changeset
    85
							end
2629
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
    86
						end
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
    87
					end
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
    88
					-- save changed global config
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
    89
					push_store:set_identifier(node, push_identifier, nil);
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
    90
					push_errors[push_identifier] = nil;
2673
e6d243ed88ca mod_cloud_notify: Fix module:unhook calls not available in prosody 0.9, fixes #874
tmolitor <thilo@eightysoft.de>
parents: 2647
diff changeset
    91
					-- unhook iq handlers for this identifier (if possible)
e6d243ed88ca mod_cloud_notify: Fix module:unhook calls not available in prosody 0.9, fixes #874
tmolitor <thilo@eightysoft.de>
parents: 2647
diff changeset
    92
					if module.unhook then
e6d243ed88ca mod_cloud_notify: Fix module:unhook calls not available in prosody 0.9, fixes #874
tmolitor <thilo@eightysoft.de>
parents: 2647
diff changeset
    93
						module:unhook("iq-error/bare/"..stanza_id, handle_push_error);
e6d243ed88ca mod_cloud_notify: Fix module:unhook calls not available in prosody 0.9, fixes #874
tmolitor <thilo@eightysoft.de>
parents: 2647
diff changeset
    94
						module:unhook("iq-result/bare/"..stanza_id, handle_push_success);
e6d243ed88ca mod_cloud_notify: Fix module:unhook calls not available in prosody 0.9, fixes #874
tmolitor <thilo@eightysoft.de>
parents: 2647
diff changeset
    95
					end
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
    96
				end
2629
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
    97
			elseif user_push_services[push_identifier] and user_push_services[push_identifier].jid == from and error_type == "wait" then
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
    98
				module:log("debug", "Got error of type '%s' (%s) for identifier '%s': "
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
    99
					.."NOT increasing error count for this identifier", error_type, condition, push_identifier);
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   100
			end
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   101
		end
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   102
	end
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   103
	return true;
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   104
end
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   105
2646
0f1421af7f6a mod_cloud_notify: Move declarations of handle_push_success/error to fix referencing undefined variables (introduced in 6ab46ff685d0)
Matthew Wild <mwild1@gmail.com>
parents: 2629
diff changeset
   106
function handle_push_success(event)
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   107
	local stanza = event.stanza;
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   108
	local node = jid.split(stanza.attr.to);
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   109
	local from = stanza.attr.from;
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   110
	local user_push_services = push_store:get(node);
2647
777d07e0cd73 mod_cloud_notify: Whitespace fixes
Matthew Wild <mwild1@gmail.com>
parents: 2646
diff changeset
   111
2629
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
   112
	for push_identifier, _ in pairs(user_push_services) do
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
   113
		if hashes.sha256(push_identifier, true) == stanza.attr.id then
2674
6e01878103c0 mod_smacks: Ignore user when writing or reading session_cache on prosody 0.9
tmolitor <thilo@eightysoft.de>
parents: 2673
diff changeset
   114
			if user_push_services[push_identifier] and user_push_services[push_identifier].jid == from and push_errors[push_identifier] > 0 then
2629
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
   115
				push_errors[push_identifier] = 0;
2740
fff185e7ab73 mod_cloud_notify: Implement the "stripped stanzas" proposal.
tmolitor <thilo@eightysoft.de>
parents: 2718
diff changeset
   116
				module:log("debug", "Push succeeded, error count for identifier '%s' is now at %s again", push_identifier, tostring(push_errors[push_identifier]));
2629
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
   117
			end
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
   118
		end
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   119
	end
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   120
	return true;
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   121
end
1787
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   122
1912
eba279ddc050 mod_cloud_notify: Add some comments describing code blocks
Kim Alvefur <zash@zash.se>
parents: 1911
diff changeset
   123
-- http://xmpp.org/extensions/xep-0357.html#disco
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   124
local function account_dico_info(event)
2204
e9e38ae8037f mod_cloud_notify: Advertise feature on bare jid disco (thanks iNPUTmice)
Kim Alvefur <zash@zash.se>
parents: 2202
diff changeset
   125
	(event.reply or event.stanza):tag("feature", {var=xmlns_push}):up();
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   126
end
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   127
module:hook("account-disco-info", account_dico_info);
1787
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   128
1911
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: 1788
diff changeset
   129
-- http://xmpp.org/extensions/xep-0357.html#enabling
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   130
local function push_enable(event)
1787
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   131
	local origin, stanza = event.origin, event.stanza;
2258
122cb5f4930f mod_cloud_notify: Cache <enable> in local
Kim Alvefur <zash@zash.se>
parents: 2257
diff changeset
   132
	local enable = stanza.tags[1];
2256
a96f2d0f8750 mod_cloud_notify: Add some logging when a client attempts to enable push notifications
Kim Alvefur <zash@zash.se>
parents: 2251
diff changeset
   133
	origin.log("debug", "Attempting to enable push notifications");
1911
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: 1788
diff changeset
   134
	-- MUST contain a 'jid' attribute of the XMPP Push Service being enabled
2258
122cb5f4930f mod_cloud_notify: Cache <enable> in local
Kim Alvefur <zash@zash.se>
parents: 2257
diff changeset
   135
	local push_jid = enable.attr.jid;
1911
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: 1788
diff changeset
   136
	-- SHOULD contain a 'node' attribute
2258
122cb5f4930f mod_cloud_notify: Cache <enable> in local
Kim Alvefur <zash@zash.se>
parents: 2257
diff changeset
   137
	local push_node = enable.attr.node;
2740
fff185e7ab73 mod_cloud_notify: Implement the "stripped stanzas" proposal.
tmolitor <thilo@eightysoft.de>
parents: 2718
diff changeset
   138
	-- CAN contain a 'include_payload' attribute
fff185e7ab73 mod_cloud_notify: Implement the "stripped stanzas" proposal.
tmolitor <thilo@eightysoft.de>
parents: 2718
diff changeset
   139
	local include_payload = enable.attr.include_payload;
1911
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: 1788
diff changeset
   140
	if not push_jid then
2261
f84b51f9aa82 mod_cloud_notify: Log message when 'jid' is missing from enable request
Kim Alvefur <zash@zash.se>
parents: 2259
diff changeset
   141
		origin.log("debug", "Push notification enable request missing the 'jid' field");
1911
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: 1788
diff changeset
   142
		origin.send(st.error_reply(stanza, "modify", "bad-request", "Missing jid"));
1787
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   143
		return true;
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   144
	end
2259
cdfc917a8cc7 mod_cloud_notify: Retrieve data form by name and namespace so unknown elements are ignored
Kim Alvefur <zash@zash.se>
parents: 2258
diff changeset
   145
	local publish_options = enable:get_child("x", "jabber:x:data");
2262
3abc51faf945 mod_cloud_notify: Log message if no dataform is found
Kim Alvefur <zash@zash.se>
parents: 2261
diff changeset
   146
	if not publish_options then
3abc51faf945 mod_cloud_notify: Log message if no dataform is found
Kim Alvefur <zash@zash.se>
parents: 2261
diff changeset
   147
		-- Could be intentional
3abc51faf945 mod_cloud_notify: Log message if no dataform is found
Kim Alvefur <zash@zash.se>
parents: 2261
diff changeset
   148
		origin.log("debug", "No publish options in request");
3abc51faf945 mod_cloud_notify: Log message if no dataform is found
Kim Alvefur <zash@zash.se>
parents: 2261
diff changeset
   149
	end
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   150
	local push_identifier = push_jid .. "<" .. (push_node or "");
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   151
	local push_service = {
1787
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   152
		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
   153
		node = push_node;
2740
fff185e7ab73 mod_cloud_notify: Implement the "stripped stanzas" proposal.
tmolitor <thilo@eightysoft.de>
parents: 2718
diff changeset
   154
		include_payload = include_payload;
1787
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   155
		count = 0;
2257
97ebd28a8a75 mod_cloud_notify: Apply pre-serialization to publish-options
Kim Alvefur <zash@zash.se>
parents: 2256
diff changeset
   156
		options = publish_options and st.preserialize(publish_options);
1787
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   157
	};
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   158
	local ok = push_store:set_identifier(origin.username, push_identifier, push_service);
2205
eb5555a3a535 mod_cloud_notify: Enable persistent storage of user notification settings
Kim Alvefur <zash@zash.se>
parents: 2204
diff changeset
   159
	if not ok then
eb5555a3a535 mod_cloud_notify: Enable persistent storage of user notification settings
Kim Alvefur <zash@zash.se>
parents: 2204
diff changeset
   160
		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: 2204
diff changeset
   161
	else
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   162
		origin.push_identifier = push_identifier;
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   163
		origin.push_settings = push_service;
2629
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
   164
		origin.log("info", "Push notifications enabled for %s (%s)", tostring(stanza.attr.from), tostring(origin.push_identifier));
2205
eb5555a3a535 mod_cloud_notify: Enable persistent storage of user notification settings
Kim Alvefur <zash@zash.se>
parents: 2204
diff changeset
   165
		origin.send(st.reply(stanza));
eb5555a3a535 mod_cloud_notify: Enable persistent storage of user notification settings
Kim Alvefur <zash@zash.se>
parents: 2204
diff changeset
   166
	end
1787
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   167
	return true;
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   168
end
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   169
module:hook("iq-set/self/"..xmlns_push..":enable", push_enable);
1787
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   170
1911
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: 1788
diff changeset
   171
-- http://xmpp.org/extensions/xep-0357.html#disabling
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   172
local function push_disable(event)
1787
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   173
	local origin, stanza = event.origin, event.stanza;
1911
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: 1788
diff changeset
   174
	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: 1788
diff changeset
   175
	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: 1788
diff changeset
   176
	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: 1788
diff changeset
   177
		origin.send(st.error_reply(stanza, "modify", "bad-request", "Missing jid"));
1787
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   178
		return true;
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   179
	end
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   180
	local user_push_services = push_store:get(origin.username);
1911
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: 1788
diff changeset
   181
	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: 1788
diff changeset
   182
		if push_info.jid == push_jid and (not push_node or push_info.node == push_node) then
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   183
			origin.log("info", "Push notifications disabled (%s)", tostring(key));
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   184
			if origin.push_identifier == key then
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   185
				origin.push_identifier = nil;
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   186
				origin.push_settings = nil;
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   187
			end
1911
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: 1788
diff changeset
   188
			user_push_services[key] = nil;
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   189
			push_errors[key] = nil;
2673
e6d243ed88ca mod_cloud_notify: Fix module:unhook calls not available in prosody 0.9, fixes #874
tmolitor <thilo@eightysoft.de>
parents: 2647
diff changeset
   190
			if module.unhook then
e6d243ed88ca mod_cloud_notify: Fix module:unhook calls not available in prosody 0.9, fixes #874
tmolitor <thilo@eightysoft.de>
parents: 2647
diff changeset
   191
				module:unhook("iq-error/bare/"..key, handle_push_error);
e6d243ed88ca mod_cloud_notify: Fix module:unhook calls not available in prosody 0.9, fixes #874
tmolitor <thilo@eightysoft.de>
parents: 2647
diff changeset
   192
				module:unhook("iq-result/bare/"..key, handle_push_success);
e6d243ed88ca mod_cloud_notify: Fix module:unhook calls not available in prosody 0.9, fixes #874
tmolitor <thilo@eightysoft.de>
parents: 2647
diff changeset
   193
			end
1911
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: 1788
diff changeset
   194
		end
1787
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   195
	end
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   196
	local ok = push_store:set(origin.username, user_push_services);
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   197
	if not ok then
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   198
		origin.send(st.error_reply(stanza, "wait", "internal-server-error"));
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   199
	else
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   200
		origin.send(st.reply(stanza));
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   201
	end
1787
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   202
	return true;
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   203
end
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   204
module:hook("iq-set/self/"..xmlns_push..":disable", push_disable);
1787
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   205
2740
fff185e7ab73 mod_cloud_notify: Implement the "stripped stanzas" proposal.
tmolitor <thilo@eightysoft.de>
parents: 2718
diff changeset
   206
-- clone a stanza and strip it
fff185e7ab73 mod_cloud_notify: Implement the "stripped stanzas" proposal.
tmolitor <thilo@eightysoft.de>
parents: 2718
diff changeset
   207
local function strip_stanza(stanza)
fff185e7ab73 mod_cloud_notify: Implement the "stripped stanzas" proposal.
tmolitor <thilo@eightysoft.de>
parents: 2718
diff changeset
   208
	local tags = {};
fff185e7ab73 mod_cloud_notify: Implement the "stripped stanzas" proposal.
tmolitor <thilo@eightysoft.de>
parents: 2718
diff changeset
   209
	local new = { name = stanza.name, attr = { xmlns = stanza.attr.xmlns, type = stanza.attr.type }, tags = tags };
fff185e7ab73 mod_cloud_notify: Implement the "stripped stanzas" proposal.
tmolitor <thilo@eightysoft.de>
parents: 2718
diff changeset
   210
	for i=1,#stanza do
fff185e7ab73 mod_cloud_notify: Implement the "stripped stanzas" proposal.
tmolitor <thilo@eightysoft.de>
parents: 2718
diff changeset
   211
		local child = stanza[i];
fff185e7ab73 mod_cloud_notify: Implement the "stripped stanzas" proposal.
tmolitor <thilo@eightysoft.de>
parents: 2718
diff changeset
   212
		if type(child) == "table" then		-- don't add raw text nodes
fff185e7ab73 mod_cloud_notify: Implement the "stripped stanzas" proposal.
tmolitor <thilo@eightysoft.de>
parents: 2718
diff changeset
   213
			if child.name then
fff185e7ab73 mod_cloud_notify: Implement the "stripped stanzas" proposal.
tmolitor <thilo@eightysoft.de>
parents: 2718
diff changeset
   214
				child = strip_stanza(child);
fff185e7ab73 mod_cloud_notify: Implement the "stripped stanzas" proposal.
tmolitor <thilo@eightysoft.de>
parents: 2718
diff changeset
   215
				t_insert(tags, child);
fff185e7ab73 mod_cloud_notify: Implement the "stripped stanzas" proposal.
tmolitor <thilo@eightysoft.de>
parents: 2718
diff changeset
   216
			end
fff185e7ab73 mod_cloud_notify: Implement the "stripped stanzas" proposal.
tmolitor <thilo@eightysoft.de>
parents: 2718
diff changeset
   217
			t_insert(new, child);
fff185e7ab73 mod_cloud_notify: Implement the "stripped stanzas" proposal.
tmolitor <thilo@eightysoft.de>
parents: 2718
diff changeset
   218
		end
fff185e7ab73 mod_cloud_notify: Implement the "stripped stanzas" proposal.
tmolitor <thilo@eightysoft.de>
parents: 2718
diff changeset
   219
	end
fff185e7ab73 mod_cloud_notify: Implement the "stripped stanzas" proposal.
tmolitor <thilo@eightysoft.de>
parents: 2718
diff changeset
   220
	return setmetatable(new, st.stanza_mt);
fff185e7ab73 mod_cloud_notify: Implement the "stripped stanzas" proposal.
tmolitor <thilo@eightysoft.de>
parents: 2718
diff changeset
   221
end
fff185e7ab73 mod_cloud_notify: Implement the "stripped stanzas" proposal.
tmolitor <thilo@eightysoft.de>
parents: 2718
diff changeset
   222
1787
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   223
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
   224
	{ 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
   225
	{ 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
   226
	{ 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
   227
	{ 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
   228
	{ 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
   229
};
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   230
1911
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: 1788
diff changeset
   231
-- http://xmpp.org/extensions/xep-0357.html#publishing
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   232
local function handle_notify_request(stanza, node, user_push_services)
2716
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   233
	local pushes = 0;
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   234
	if not user_push_services or not #user_push_services then return pushes end
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   235
	
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   236
	for push_identifier, push_info in pairs(user_push_services) do
2716
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   237
		local send_push = true;		-- only send push to this node when not already done for this stanza or if no stanza is given at all
2629
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
   238
		if stanza then
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
   239
			if not stanza._push_notify then stanza._push_notify = {}; end
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
   240
			if stanza._push_notify[push_identifier] then
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
   241
				module:log("debug", "Already sent push notification for %s@%s to %s (%s)", node, module.host, push_info.jid, tostring(push_info.node));
2716
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   242
				send_push = false;
2629
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
   243
			end
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
   244
			stanza._push_notify[push_identifier] = true;
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
   245
		end
2716
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   246
		
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   247
		if send_push then
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   248
			-- increment count and save it
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   249
			push_info.count = push_info.count + 1;
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   250
			push_store:set_identifier(node, push_identifier, push_info);
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   251
			-- construct push stanza
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   252
			local stanza_id = hashes.sha256(push_identifier, true);
2755
6b710a8bdf03 mod_cloud_notify: Implement version 0.3 of XEP-0357
tmolitor <thilo@eightysoft.de>
parents: 2753
diff changeset
   253
			local push_publish = st.iq({ to = push_info.jid, from = module.host, type = "set", id = stanza_id })
2716
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   254
				:tag("pubsub", { xmlns = "http://jabber.org/protocol/pubsub" })
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   255
					:tag("publish", { node = push_info.node })
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   256
						:tag("item")
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   257
							:tag("notification", { xmlns = xmlns_push });
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   258
			local form_data = {
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   259
				["message-count"] = tostring(push_info.count);
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   260
			};
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   261
			if stanza and include_sender then
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   262
				form_data["last-message-sender"] = stanza.attr.from;
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   263
			end
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   264
			if stanza and include_body then
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   265
				form_data["last-message-body"] = stanza:get_child_text("body");
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   266
			end
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   267
			push_publish:add_child(push_form:form(form_data));
2740
fff185e7ab73 mod_cloud_notify: Implement the "stripped stanzas" proposal.
tmolitor <thilo@eightysoft.de>
parents: 2718
diff changeset
   268
			if stanza and push_info.include_payload == "stripped" then
fff185e7ab73 mod_cloud_notify: Implement the "stripped stanzas" proposal.
tmolitor <thilo@eightysoft.de>
parents: 2718
diff changeset
   269
				push_publish:tag("payload", { type = "stripped" })
fff185e7ab73 mod_cloud_notify: Implement the "stripped stanzas" proposal.
tmolitor <thilo@eightysoft.de>
parents: 2718
diff changeset
   270
					:add_child(strip_stanza(stanza));
fff185e7ab73 mod_cloud_notify: Implement the "stripped stanzas" proposal.
tmolitor <thilo@eightysoft.de>
parents: 2718
diff changeset
   271
				push_publish:up(); -- / payload
fff185e7ab73 mod_cloud_notify: Implement the "stripped stanzas" proposal.
tmolitor <thilo@eightysoft.de>
parents: 2718
diff changeset
   272
			end
fff185e7ab73 mod_cloud_notify: Implement the "stripped stanzas" proposal.
tmolitor <thilo@eightysoft.de>
parents: 2718
diff changeset
   273
			if stanza and push_info.include_payload == "full" then
fff185e7ab73 mod_cloud_notify: Implement the "stripped stanzas" proposal.
tmolitor <thilo@eightysoft.de>
parents: 2718
diff changeset
   274
				push_publish:tag("payload", { type = "full" })
fff185e7ab73 mod_cloud_notify: Implement the "stripped stanzas" proposal.
tmolitor <thilo@eightysoft.de>
parents: 2718
diff changeset
   275
					:add_child(st.clone(stanza));
fff185e7ab73 mod_cloud_notify: Implement the "stripped stanzas" proposal.
tmolitor <thilo@eightysoft.de>
parents: 2718
diff changeset
   276
				push_publish:up(); -- / payload
fff185e7ab73 mod_cloud_notify: Implement the "stripped stanzas" proposal.
tmolitor <thilo@eightysoft.de>
parents: 2718
diff changeset
   277
			end
2716
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   278
			push_publish:up(); -- / notification
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   279
			push_publish:up(); -- / publish
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   280
			push_publish:up(); -- / pubsub
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   281
			if push_info.options then
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   282
				push_publish:tag("publish-options"):add_child(st.deserialize(push_info.options));
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   283
			end
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   284
			-- send out push
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   285
			module:log("debug", "Sending push notification for %s@%s to %s (%s)", node, module.host, push_info.jid, tostring(push_info.node));
2740
fff185e7ab73 mod_cloud_notify: Implement the "stripped stanzas" proposal.
tmolitor <thilo@eightysoft.de>
parents: 2718
diff changeset
   286
			-- module:log("debug", "PUSH STANZA: %s", tostring(push_publish));
2716
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   287
			-- handle push errors for this node
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   288
			if push_errors[push_identifier] == nil then
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   289
				push_errors[push_identifier] = 0;
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   290
				module:hook("iq-error/bare/"..stanza_id, handle_push_error);
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   291
				module:hook("iq-result/bare/"..stanza_id, handle_push_success);
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   292
			end
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   293
			module:send(push_publish);
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   294
			pushes = pushes + 1;
1913
c7389fe74de7 mod_cloud_notify: Make inclusion of message sender and body optional via config option
Kim Alvefur <zash@zash.se>
parents: 1912
diff changeset
   295
		end
1787
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   296
	end
2716
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   297
	return pushes;
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   298
end
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   299
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   300
-- small helper function to extract relevant push settings
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   301
local function get_push_settings(stanza, session)
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   302
	local to = stanza.attr.to;
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   303
	local node = to and jid.split(to) or session.username;
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   304
	local user_push_services = push_store:get(node);
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   305
	return node, user_push_services;
2145
218a3d3f7f97 mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents: 2055
diff changeset
   306
end
218a3d3f7f97 mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents: 2055
diff changeset
   307
218a3d3f7f97 mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents: 2055
diff changeset
   308
-- 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: 2055
diff changeset
   309
module:hook("message/offline/handle", function(event)
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   310
	local node, user_push_services = get_push_settings(event.stanza, event.origin);
2740
fff185e7ab73 mod_cloud_notify: Implement the "stripped stanzas" proposal.
tmolitor <thilo@eightysoft.de>
parents: 2718
diff changeset
   311
	module:log("debug", "Invoking cloud handle_notify_request() for offline stanza");
2716
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   312
	handle_notify_request(event.stanza, node, user_push_services);
1787
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   313
end, 1);
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   314
2145
218a3d3f7f97 mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents: 2055
diff changeset
   315
-- publish on unacked smacks message
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   316
local function process_smacks_stanza(stanza, session)
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   317
	if session.push_identifier then
2718
75b137cf869a mod_cloud_notify: Don't notify for all smacks queued stanzas in a row
tmolitor <thilo@eightysoft.de>
parents: 2716
diff changeset
   318
		session.log("debug", "Invoking cloud handle_notify_request() for smacks queued stanza");
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   319
		local user_push_services = {[session.push_identifier] = session.push_settings};
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   320
		local node = get_push_settings(stanza, session);
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   321
		handle_notify_request(stanza, node, user_push_services);
2145
218a3d3f7f97 mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents: 2055
diff changeset
   322
	end
218a3d3f7f97 mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents: 2055
diff changeset
   323
	return stanza;
218a3d3f7f97 mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents: 2055
diff changeset
   324
end
218a3d3f7f97 mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents: 2055
diff changeset
   325
2718
75b137cf869a mod_cloud_notify: Don't notify for all smacks queued stanzas in a row
tmolitor <thilo@eightysoft.de>
parents: 2716
diff changeset
   326
local function process_smacks_queue(queue, session)
75b137cf869a mod_cloud_notify: Don't notify for all smacks queued stanzas in a row
tmolitor <thilo@eightysoft.de>
parents: 2716
diff changeset
   327
	if not session.push_identifier then return; end
75b137cf869a mod_cloud_notify: Don't notify for all smacks queued stanzas in a row
tmolitor <thilo@eightysoft.de>
parents: 2716
diff changeset
   328
	local user_push_services = {[session.push_identifier] = session.push_settings};
75b137cf869a mod_cloud_notify: Don't notify for all smacks queued stanzas in a row
tmolitor <thilo@eightysoft.de>
parents: 2716
diff changeset
   329
	for i=1, #queue do
75b137cf869a mod_cloud_notify: Don't notify for all smacks queued stanzas in a row
tmolitor <thilo@eightysoft.de>
parents: 2716
diff changeset
   330
		local stanza = queue[i];
75b137cf869a mod_cloud_notify: Don't notify for all smacks queued stanzas in a row
tmolitor <thilo@eightysoft.de>
parents: 2716
diff changeset
   331
		local node = get_push_settings(stanza, session);
75b137cf869a mod_cloud_notify: Don't notify for all smacks queued stanzas in a row
tmolitor <thilo@eightysoft.de>
parents: 2716
diff changeset
   332
		session.log("debug", "Invoking cloud handle_notify_request() for smacks queued stanza: %d", i);
75b137cf869a mod_cloud_notify: Don't notify for all smacks queued stanzas in a row
tmolitor <thilo@eightysoft.de>
parents: 2716
diff changeset
   333
		if handle_notify_request(stanza, node, user_push_services) ~= 0 then
75b137cf869a mod_cloud_notify: Don't notify for all smacks queued stanzas in a row
tmolitor <thilo@eightysoft.de>
parents: 2716
diff changeset
   334
			session.log("debug", "Cloud handle_notify_request() > 0, not notifying for other queued stanzas");
75b137cf869a mod_cloud_notify: Don't notify for all smacks queued stanzas in a row
tmolitor <thilo@eightysoft.de>
parents: 2716
diff changeset
   335
			return;		-- only notify for one stanza in the queue, not for all in a row
75b137cf869a mod_cloud_notify: Don't notify for all smacks queued stanzas in a row
tmolitor <thilo@eightysoft.de>
parents: 2716
diff changeset
   336
		end
75b137cf869a mod_cloud_notify: Don't notify for all smacks queued stanzas in a row
tmolitor <thilo@eightysoft.de>
parents: 2716
diff changeset
   337
	end
75b137cf869a mod_cloud_notify: Don't notify for all smacks queued stanzas in a row
tmolitor <thilo@eightysoft.de>
parents: 2716
diff changeset
   338
end
75b137cf869a mod_cloud_notify: Don't notify for all smacks queued stanzas in a row
tmolitor <thilo@eightysoft.de>
parents: 2716
diff changeset
   339
2145
218a3d3f7f97 mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents: 2055
diff changeset
   340
-- 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: 2055
diff changeset
   341
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: 2055
diff changeset
   342
	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: 2055
diff changeset
   343
	local queue = event.queue;
2399
2e641ab995b3 mod_cloud_notify: added code to respond to the new event "smacks-ack-delayed" issued by mod_smacks when acks are delayed for a certain amount of time. This allows to send out notification requests before the read timeout or connection close event really happens, thus allowing conversations to be smoother.
tmolitor <thilo@eightysoft.de>
parents: 2267
diff changeset
   344
	-- process unacked stanzas
2718
75b137cf869a mod_cloud_notify: Don't notify for all smacks queued stanzas in a row
tmolitor <thilo@eightysoft.de>
parents: 2716
diff changeset
   345
	process_smacks_queue(queue, session);
2145
218a3d3f7f97 mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents: 2055
diff changeset
   346
	-- process future unacked (hibernated) stanzas
2740
fff185e7ab73 mod_cloud_notify: Implement the "stripped stanzas" proposal.
tmolitor <thilo@eightysoft.de>
parents: 2718
diff changeset
   347
	filters.add_filter(session, "stanzas/out", process_smacks_stanza, -990);
2145
218a3d3f7f97 mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents: 2055
diff changeset
   348
end
218a3d3f7f97 mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents: 2055
diff changeset
   349
218a3d3f7f97 mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents: 2055
diff changeset
   350
-- 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: 2055
diff changeset
   351
local function restore_session(event)
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   352
	local session = event.resumed;
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   353
	if session then		-- older smacks module versions send only the "intermediate" session in event.session and no session.resumed one
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   354
		filters.remove_filter(session, "stanzas/out", process_smacks_stanza);
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   355
		-- this means the counter of outstanding push messages can be reset as well
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   356
		if session.push_settings then
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   357
			session.push_settings.count = 0;
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   358
			push_store:set_identifier(session.username, session.push_identifier, session.push_settings);
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   359
		end
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   360
	end
2145
218a3d3f7f97 mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents: 2055
diff changeset
   361
end
218a3d3f7f97 mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents: 2055
diff changeset
   362
2399
2e641ab995b3 mod_cloud_notify: added code to respond to the new event "smacks-ack-delayed" issued by mod_smacks when acks are delayed for a certain amount of time. This allows to send out notification requests before the read timeout or connection close event really happens, thus allowing conversations to be smoother.
tmolitor <thilo@eightysoft.de>
parents: 2267
diff changeset
   363
-- smacks ack is delayed
2e641ab995b3 mod_cloud_notify: added code to respond to the new event "smacks-ack-delayed" issued by mod_smacks when acks are delayed for a certain amount of time. This allows to send out notification requests before the read timeout or connection close event really happens, thus allowing conversations to be smoother.
tmolitor <thilo@eightysoft.de>
parents: 2267
diff changeset
   364
local function ack_delayed(event)
2e641ab995b3 mod_cloud_notify: added code to respond to the new event "smacks-ack-delayed" issued by mod_smacks when acks are delayed for a certain amount of time. This allows to send out notification requests before the read timeout or connection close event really happens, thus allowing conversations to be smoother.
tmolitor <thilo@eightysoft.de>
parents: 2267
diff changeset
   365
	local session = event.origin;
2e641ab995b3 mod_cloud_notify: added code to respond to the new event "smacks-ack-delayed" issued by mod_smacks when acks are delayed for a certain amount of time. This allows to send out notification requests before the read timeout or connection close event really happens, thus allowing conversations to be smoother.
tmolitor <thilo@eightysoft.de>
parents: 2267
diff changeset
   366
	local queue = event.queue;
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   367
	-- process unacked stanzas (handle_notify_request() will only send push requests for new stanzas)
2718
75b137cf869a mod_cloud_notify: Don't notify for all smacks queued stanzas in a row
tmolitor <thilo@eightysoft.de>
parents: 2716
diff changeset
   368
	process_smacks_queue(queue, session);
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   369
end
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   370
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   371
-- archive message added
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   372
local function archive_message_added(event)
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   373
	-- event is: { origin = origin, stanza = stanza, for_user = store_user, id = id }
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   374
	-- only notify for new mam messages when at least one device is only
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   375
	if not event.for_user or not host_sessions[event.for_user] then return; end
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   376
	local stanza = event.stanza;
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   377
	local user_session = host_sessions[event.for_user].sessions;
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   378
	local to = stanza.attr.to;
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   379
	to = to and jid.split(to) or event.origin.username;
2647
777d07e0cd73 mod_cloud_notify: Whitespace fixes
Matthew Wild <mwild1@gmail.com>
parents: 2646
diff changeset
   380
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   381
	-- only notify if the stanza destination is the mam user we store it for
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   382
	if event.for_user == to then
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   383
		local user_push_services = push_store:get(to);
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   384
		if not #user_push_services then return end
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   385
		
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   386
		-- only notify nodes with no active sessions (smacks is counted as active and handled separate)
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   387
		local notify_push_sevices = {};
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   388
		for identifier, push_info in pairs(user_push_services) do
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   389
			local identifier_found = nil;
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   390
			for _, session in pairs(user_session) do
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   391
				-- module:log("debug", "searching for '%s': identifier '%s' for session %s", tostring(identifier), tostring(session.push_identifier), tostring(session.full_jid));
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   392
				if session.push_identifier == identifier then
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   393
					identifier_found = session;
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   394
					break;
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   395
				end
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   396
			end
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   397
			if identifier_found then
2718
75b137cf869a mod_cloud_notify: Don't notify for all smacks queued stanzas in a row
tmolitor <thilo@eightysoft.de>
parents: 2716
diff changeset
   398
				identifier_found.log("debug", "Not cloud notifying '%s' of new MAM stanza (session still alive)", identifier);
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   399
			else
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   400
				notify_push_sevices[identifier] = push_info;
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   401
			end
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   402
		end
2647
777d07e0cd73 mod_cloud_notify: Whitespace fixes
Matthew Wild <mwild1@gmail.com>
parents: 2646
diff changeset
   403
2716
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   404
		handle_notify_request(event.stanza, to, notify_push_sevices);
2399
2e641ab995b3 mod_cloud_notify: added code to respond to the new event "smacks-ack-delayed" issued by mod_smacks when acks are delayed for a certain amount of time. This allows to send out notification requests before the read timeout or connection close event really happens, thus allowing conversations to be smoother.
tmolitor <thilo@eightysoft.de>
parents: 2267
diff changeset
   405
	end
2e641ab995b3 mod_cloud_notify: added code to respond to the new event "smacks-ack-delayed" issued by mod_smacks when acks are delayed for a certain amount of time. This allows to send out notification requests before the read timeout or connection close event really happens, thus allowing conversations to be smoother.
tmolitor <thilo@eightysoft.de>
parents: 2267
diff changeset
   406
end
2e641ab995b3 mod_cloud_notify: added code to respond to the new event "smacks-ack-delayed" issued by mod_smacks when acks are delayed for a certain amount of time. This allows to send out notification requests before the read timeout or connection close event really happens, thus allowing conversations to be smoother.
tmolitor <thilo@eightysoft.de>
parents: 2267
diff changeset
   407
2145
218a3d3f7f97 mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents: 2055
diff changeset
   408
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: 2055
diff changeset
   409
module:hook("smacks-hibernation-end", restore_session);
2399
2e641ab995b3 mod_cloud_notify: added code to respond to the new event "smacks-ack-delayed" issued by mod_smacks when acks are delayed for a certain amount of time. This allows to send out notification requests before the read timeout or connection close event really happens, thus allowing conversations to be smoother.
tmolitor <thilo@eightysoft.de>
parents: 2267
diff changeset
   410
module:hook("smacks-ack-delayed", ack_delayed);
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   411
module:hook("archive-message-added", archive_message_added);
2145
218a3d3f7f97 mod_cloud_notify: added ability to notify even if the session is hibernated by mod_smacks
tmolitor <thilo@eightysoft.de>
parents: 2055
diff changeset
   412
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   413
local function send_ping(event)
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   414
	local user = event.user;
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   415
	local user_push_services = push_store:get(user);
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   416
	local push_services = event.push_services or user_push_services;
2716
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   417
	handle_notify_request(nil, user, push_services);
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   418
end
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   419
-- can be used by other modules to ping one or more (or all) push endpoints
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   420
module:hook("cloud-notify-ping", send_ping);
1787
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   421
2629
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
   422
module:log("info", "Module loaded");
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   423
function module.unload()
2673
e6d243ed88ca mod_cloud_notify: Fix module:unhook calls not available in prosody 0.9, fixes #874
tmolitor <thilo@eightysoft.de>
parents: 2647
diff changeset
   424
	if module.unhook then
e6d243ed88ca mod_cloud_notify: Fix module:unhook calls not available in prosody 0.9, fixes #874
tmolitor <thilo@eightysoft.de>
parents: 2647
diff changeset
   425
		module:unhook("account-disco-info", account_dico_info);
e6d243ed88ca mod_cloud_notify: Fix module:unhook calls not available in prosody 0.9, fixes #874
tmolitor <thilo@eightysoft.de>
parents: 2647
diff changeset
   426
		module:unhook("iq-set/self/"..xmlns_push..":enable", push_enable);
e6d243ed88ca mod_cloud_notify: Fix module:unhook calls not available in prosody 0.9, fixes #874
tmolitor <thilo@eightysoft.de>
parents: 2647
diff changeset
   427
		module:unhook("iq-set/self/"..xmlns_push..":disable", push_disable);
2647
777d07e0cd73 mod_cloud_notify: Whitespace fixes
Matthew Wild <mwild1@gmail.com>
parents: 2646
diff changeset
   428
2673
e6d243ed88ca mod_cloud_notify: Fix module:unhook calls not available in prosody 0.9, fixes #874
tmolitor <thilo@eightysoft.de>
parents: 2647
diff changeset
   429
		module:unhook("smacks-hibernation-start", hibernate_session);
e6d243ed88ca mod_cloud_notify: Fix module:unhook calls not available in prosody 0.9, fixes #874
tmolitor <thilo@eightysoft.de>
parents: 2647
diff changeset
   430
		module:unhook("smacks-hibernation-end", restore_session);
e6d243ed88ca mod_cloud_notify: Fix module:unhook calls not available in prosody 0.9, fixes #874
tmolitor <thilo@eightysoft.de>
parents: 2647
diff changeset
   431
		module:unhook("smacks-ack-delayed", ack_delayed);
e6d243ed88ca mod_cloud_notify: Fix module:unhook calls not available in prosody 0.9, fixes #874
tmolitor <thilo@eightysoft.de>
parents: 2647
diff changeset
   432
		module:unhook("archive-message-added", archive_message_added);
e6d243ed88ca mod_cloud_notify: Fix module:unhook calls not available in prosody 0.9, fixes #874
tmolitor <thilo@eightysoft.de>
parents: 2647
diff changeset
   433
		module:unhook("cloud-notify-ping", send_ping);
2647
777d07e0cd73 mod_cloud_notify: Whitespace fixes
Matthew Wild <mwild1@gmail.com>
parents: 2646
diff changeset
   434
2673
e6d243ed88ca mod_cloud_notify: Fix module:unhook calls not available in prosody 0.9, fixes #874
tmolitor <thilo@eightysoft.de>
parents: 2647
diff changeset
   435
		for push_identifier, _ in pairs(push_errors) do
e6d243ed88ca mod_cloud_notify: Fix module:unhook calls not available in prosody 0.9, fixes #874
tmolitor <thilo@eightysoft.de>
parents: 2647
diff changeset
   436
			local stanza_id = hashes.sha256(push_identifier, true);
e6d243ed88ca mod_cloud_notify: Fix module:unhook calls not available in prosody 0.9, fixes #874
tmolitor <thilo@eightysoft.de>
parents: 2647
diff changeset
   437
			module:unhook("iq-error/bare/"..stanza_id, handle_push_error);
e6d243ed88ca mod_cloud_notify: Fix module:unhook calls not available in prosody 0.9, fixes #874
tmolitor <thilo@eightysoft.de>
parents: 2647
diff changeset
   438
			module:unhook("iq-result/bare/"..stanza_id, handle_push_success);
e6d243ed88ca mod_cloud_notify: Fix module:unhook calls not available in prosody 0.9, fixes #874
tmolitor <thilo@eightysoft.de>
parents: 2647
diff changeset
   439
		end
1787
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   440
	end
2647
777d07e0cd73 mod_cloud_notify: Whitespace fixes
Matthew Wild <mwild1@gmail.com>
parents: 2646
diff changeset
   441
2629
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
   442
	module:log("info", "Module unloaded");
2647
777d07e0cd73 mod_cloud_notify: Whitespace fixes
Matthew Wild <mwild1@gmail.com>
parents: 2646
diff changeset
   443
end