mod_cloud_notify/mod_cloud_notify.lua
author tmolitor <thilo@eightysoft.de>
Sun, 08 Mar 2020 20:00:23 +0100
changeset 3944 675726ab06d3
parent 3631 9639c493f4b9
child 3947 f5e6368a1c39
permissions -rw-r--r--
mod_cloud_notify: Fix bug in prosody 0.9
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
3623
74aa35aeb08a mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents: 3112
diff changeset
     3
-- Copyright (C) 2017-2019 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;
2980
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
     8
local s_match = string.match;
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
     9
local s_sub = string.sub;
3059
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    10
local os_time = os.time;
3083
2a918a8c47db mod_cloud_notify: use next() instead of # operator and update README
tmolitor <thilo@eightysoft.de>
parents: 3082
diff changeset
    11
local next = next;
1787
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    12
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
    13
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
    14
local dataform = require"util.dataforms".new;
2629
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
    15
local filters = require"util.filters";
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
    16
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
    17
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    18
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
    19
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
-- configuration
2250
a3e3dc9131e7 mod_cloud_notify: Use typed config API
Kim Alvefur <zash@zash.se>
parents: 2205
diff changeset
    21
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
    22
local include_sender = module:get_option_boolean("push_notification_with_sender", false);
3059
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    23
local max_push_errors = module:get_option_number("push_max_errors", 16);
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    24
local max_push_devices = module:get_option_number("push_max_devices", 5);
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    25
local dummy_body = module:get_option_string("push_notification_important_body", "New Message!");
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
    26
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
    27
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
    28
local push_errors = {};
2795
008cf272b7ea mod_cloud_notify: Fix regression in error handling
tmolitor <thilo@eightysoft.de>
parents: 2755
diff changeset
    29
local id2node = {};
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
    30
3059
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    31
-- ordered table iterator, allow to iterate on the natural order of the keys of a table,
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    32
-- see http://lua-users.org/wiki/SortedIteration
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    33
local function __genOrderedIndex( t )
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    34
	local orderedIndex = {}
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    35
	for key in pairs(t) do
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    36
		table.insert( orderedIndex, key )
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    37
	end
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    38
	-- sort in reverse order (newest one first)
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    39
	table.sort( orderedIndex, function(a, b)
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    40
		if a == nil or t[a] == nil or b == nil or t[b] == nil then return false end
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    41
		-- only one timestamp given, this is the newer one
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    42
		if t[a].timestamp ~= nil and t[b].timestamp == nil then return true end
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    43
		if t[a].timestamp == nil and t[b].timestamp ~= nil then return false end
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    44
		-- both timestamps given, sort normally
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    45
		if t[a].timestamp ~= nil and t[b].timestamp ~= nil then return t[a].timestamp > t[b].timestamp end
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    46
		return false	-- normally not reached
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    47
	end)
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    48
	return orderedIndex
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    49
end
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    50
local function orderedNext(t, state)
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    51
	-- Equivalent of the next function, but returns the keys in timestamp
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    52
	-- order. We use a temporary ordered key table that is stored in the
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    53
	-- table being iterated.
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    54
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    55
	local key = nil
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    56
	--print("orderedNext: state = "..tostring(state) )
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    57
	if state == nil then
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    58
		-- the first time, generate the index
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    59
		t.__orderedIndex = __genOrderedIndex( t )
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    60
		key = t.__orderedIndex[1]
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    61
	else
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    62
		-- fetch the next value
3082
6b860de18a53 mod_cloud_notify: Don't use deprecated table.getn
tmolitor <thilo@eightysoft.de>
parents: 3059
diff changeset
    63
		for i = 1, #t.__orderedIndex do
3059
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    64
			if t.__orderedIndex[i] == state then
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    65
				key = t.__orderedIndex[i+1]
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    66
			end
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    67
		end
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    68
	end
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    69
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    70
	if key then
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    71
		return key, t[key]
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    72
	end
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    73
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    74
	-- no more value to return, cleanup
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    75
	t.__orderedIndex = nil
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    76
	return
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    77
end
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    78
local function orderedPairs(t)
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    79
	-- Equivalent of the pairs() function on tables. Allows to iterate
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    80
	-- in order
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    81
	return orderedNext, t, nil
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    82
end
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    83
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    84
-- small helper function to return new table with only "maximum" elements containing only the newest entries
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    85
local function reduce_table(table, maximum)
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    86
	local count = 0;
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    87
	local result = {};
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    88
	for key, value in orderedPairs(table) do
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    89
		count = count + 1;
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    90
		if count > maximum then break end
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    91
		result[key] = value;
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    92
	end
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    93
	return result;
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    94
end
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
    95
3623
74aa35aeb08a mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents: 3112
diff changeset
    96
local function stoppable_timer(delay, callback)
74aa35aeb08a mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents: 3112
diff changeset
    97
	local stopped = false;
74aa35aeb08a mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents: 3112
diff changeset
    98
	local timer = module:add_timer(delay, function (t)
74aa35aeb08a mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents: 3112
diff changeset
    99
		if stopped then return; end
74aa35aeb08a mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents: 3112
diff changeset
   100
		return callback(t);
74aa35aeb08a mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents: 3112
diff changeset
   101
	end);
3626
21f870e1ba55 mod_cloud_notify: fix prosody 0.9 compatibility
tmolitor <thilo@eightysoft.de>
parents: 3623
diff changeset
   102
	if timer and timer.stop then return timer; end		-- new prosody api includes stop() function
3623
74aa35aeb08a mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents: 3112
diff changeset
   103
	return {
3944
675726ab06d3 mod_cloud_notify: Fix bug in prosody 0.9
tmolitor <thilo@eightysoft.de>
parents: 3631
diff changeset
   104
		stop = function(self) stopped = true end;
3623
74aa35aeb08a mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents: 3112
diff changeset
   105
		timer;
74aa35aeb08a mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents: 3112
diff changeset
   106
	};
74aa35aeb08a mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents: 3112
diff changeset
   107
end
74aa35aeb08a mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents: 3112
diff changeset
   108
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   109
-- 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
   110
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
   111
	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
   112
	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
   113
	local api = {};
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   114
	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
   115
		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
   116
			local err;
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   117
			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
   118
			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
   119
				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
   120
				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
   121
				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
   122
			end
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   123
		end
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   124
		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
   125
		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
   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
	function api:set(user, data)
3059
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
   128
		push_services[user] = reduce_table(data, max_push_devices);
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   129
		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
   130
		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
   131
			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
   132
			return false;
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   133
		end
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   134
		return true;
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   135
	end
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   136
	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
   137
		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
   138
		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
   139
		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
   140
	end
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   141
	return api;
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   142
end)();
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   143
3112
cfcb020bcd1d mod_cloud_notify: inform mod_smacks of first push in hibernated state
tmolitor <thilo@eightysoft.de>
parents: 3089
diff changeset
   144
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
   145
-- 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
   146
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
   147
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
   148
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
   149
	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
   150
	local error_type, condition = stanza:get_error();
2795
008cf272b7ea mod_cloud_notify: Fix regression in error handling
tmolitor <thilo@eightysoft.de>
parents: 2755
diff changeset
   151
	local node = id2node[stanza.attr.id];
008cf272b7ea mod_cloud_notify: Fix regression in error handling
tmolitor <thilo@eightysoft.de>
parents: 2755
diff changeset
   152
	if node == nil then return false; end		-- unknown stanza? Ignore for now!
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   153
	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
   154
	local user_push_services = push_store:get(node);
3089
1ea6861b533f mod_cloud_notify: Don't change table while iterating it
tmolitor <thilo@eightysoft.de>
parents: 3083
diff changeset
   155
	local changed = false;
2795
008cf272b7ea mod_cloud_notify: Fix regression in error handling
tmolitor <thilo@eightysoft.de>
parents: 2755
diff changeset
   156
	
2629
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
   157
	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
   158
		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
   159
		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
   160
			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
   161
				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
   162
				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
   163
					.."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
   164
					tostring(push_errors[push_identifier]));
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
   165
				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
   166
					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
   167
					-- remove push settings from sessions
2753
9756211fcbe3 mod_cloud_notify: Fix small bug.
tmolitor <thilo@eightysoft.de>
parents: 2740
diff changeset
   168
					if host_sessions[node] then
9756211fcbe3 mod_cloud_notify: Fix small bug.
tmolitor <thilo@eightysoft.de>
parents: 2740
diff changeset
   169
						for _, session in pairs(host_sessions[node].sessions) do
9756211fcbe3 mod_cloud_notify: Fix small bug.
tmolitor <thilo@eightysoft.de>
parents: 2740
diff changeset
   170
							if session.push_identifier == push_identifier then
9756211fcbe3 mod_cloud_notify: Fix small bug.
tmolitor <thilo@eightysoft.de>
parents: 2740
diff changeset
   171
								session.push_identifier = nil;
9756211fcbe3 mod_cloud_notify: Fix small bug.
tmolitor <thilo@eightysoft.de>
parents: 2740
diff changeset
   172
								session.push_settings = nil;
3112
cfcb020bcd1d mod_cloud_notify: inform mod_smacks of first push in hibernated state
tmolitor <thilo@eightysoft.de>
parents: 3089
diff changeset
   173
								session.first_hibernated_push = nil;
2753
9756211fcbe3 mod_cloud_notify: Fix small bug.
tmolitor <thilo@eightysoft.de>
parents: 2740
diff changeset
   174
							end
2629
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
   175
						end
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
   176
					end
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
   177
					-- save changed global config
3089
1ea6861b533f mod_cloud_notify: Don't change table while iterating it
tmolitor <thilo@eightysoft.de>
parents: 3083
diff changeset
   178
					changed = true;
1ea6861b533f mod_cloud_notify: Don't change table while iterating it
tmolitor <thilo@eightysoft.de>
parents: 3083
diff changeset
   179
					user_push_services[push_identifier] = nil
2629
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
   180
					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
   181
					-- 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
   182
					if module.unhook then
2795
008cf272b7ea mod_cloud_notify: Fix regression in error handling
tmolitor <thilo@eightysoft.de>
parents: 2755
diff changeset
   183
						module:unhook("iq-error/host/"..stanza_id, handle_push_error);
008cf272b7ea mod_cloud_notify: Fix regression in error handling
tmolitor <thilo@eightysoft.de>
parents: 2755
diff changeset
   184
						module:unhook("iq-result/host/"..stanza_id, handle_push_success);
008cf272b7ea mod_cloud_notify: Fix regression in error handling
tmolitor <thilo@eightysoft.de>
parents: 2755
diff changeset
   185
						id2node[stanza_id] = 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
   186
					end
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   187
				end
2629
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
   188
			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
   189
				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
   190
					.."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
   191
			end
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   192
		end
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   193
	end
3089
1ea6861b533f mod_cloud_notify: Don't change table while iterating it
tmolitor <thilo@eightysoft.de>
parents: 3083
diff changeset
   194
	if changed then
1ea6861b533f mod_cloud_notify: Don't change table while iterating it
tmolitor <thilo@eightysoft.de>
parents: 3083
diff changeset
   195
		push_store:set(node, user_push_services);
1ea6861b533f mod_cloud_notify: Don't change table while iterating it
tmolitor <thilo@eightysoft.de>
parents: 3083
diff changeset
   196
	end
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   197
	return true;
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   198
end
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   199
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
   200
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
   201
	local stanza = event.stanza;
2795
008cf272b7ea mod_cloud_notify: Fix regression in error handling
tmolitor <thilo@eightysoft.de>
parents: 2755
diff changeset
   202
	local node = id2node[stanza.attr.id];
008cf272b7ea mod_cloud_notify: Fix regression in error handling
tmolitor <thilo@eightysoft.de>
parents: 2755
diff changeset
   203
	if node == nil then return false; end		-- unknown stanza? Ignore for now!
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   204
	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
   205
	local user_push_services = push_store:get(node);
2795
008cf272b7ea mod_cloud_notify: Fix regression in error handling
tmolitor <thilo@eightysoft.de>
parents: 2755
diff changeset
   206
	
2629
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
   207
	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
   208
		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
   209
			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
   210
				push_errors[push_identifier] = 0;
3623
74aa35aeb08a mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents: 3112
diff changeset
   211
				-- unhook iq handlers for this identifier (if possible)
74aa35aeb08a mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents: 3112
diff changeset
   212
				if module.unhook then
74aa35aeb08a mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents: 3112
diff changeset
   213
					module:unhook("iq-error/host/"..stanza.attr.id, handle_push_error);
74aa35aeb08a mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents: 3112
diff changeset
   214
					module:unhook("iq-result/host/"..stanza.attr.id, handle_push_success);
74aa35aeb08a mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents: 3112
diff changeset
   215
					id2node[stanza.attr.id] = nil;
74aa35aeb08a mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents: 3112
diff changeset
   216
				end
2740
fff185e7ab73 mod_cloud_notify: Implement the "stripped stanzas" proposal.
tmolitor <thilo@eightysoft.de>
parents: 2718
diff changeset
   217
				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
   218
			end
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
   219
		end
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   220
	end
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   221
	return true;
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   222
end
1787
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   223
1912
eba279ddc050 mod_cloud_notify: Add some comments describing code blocks
Kim Alvefur <zash@zash.se>
parents: 1911
diff changeset
   224
-- 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
   225
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
   226
	(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
   227
end
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   228
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
   229
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
   230
-- 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
   231
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
   232
	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
   233
	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
   234
	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
   235
	-- 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
   236
	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
   237
	-- SHOULD contain a 'node' attribute
2258
122cb5f4930f mod_cloud_notify: Cache <enable> in local
Kim Alvefur <zash@zash.se>
parents: 2257
diff changeset
   238
	local push_node = enable.attr.node;
2740
fff185e7ab73 mod_cloud_notify: Implement the "stripped stanzas" proposal.
tmolitor <thilo@eightysoft.de>
parents: 2718
diff changeset
   239
	-- CAN contain a 'include_payload' attribute
fff185e7ab73 mod_cloud_notify: Implement the "stripped stanzas" proposal.
tmolitor <thilo@eightysoft.de>
parents: 2718
diff changeset
   240
	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
   241
	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
   242
		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
   243
		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
   244
		return true;
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   245
	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
   246
	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
   247
	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
   248
		-- Could be intentional
3abc51faf945 mod_cloud_notify: Log message if no dataform is found
Kim Alvefur <zash@zash.se>
parents: 2261
diff changeset
   249
		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
   250
	end
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   251
	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
   252
	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
   253
		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
   254
		node = push_node;
2740
fff185e7ab73 mod_cloud_notify: Implement the "stripped stanzas" proposal.
tmolitor <thilo@eightysoft.de>
parents: 2718
diff changeset
   255
		include_payload = include_payload;
2257
97ebd28a8a75 mod_cloud_notify: Apply pre-serialization to publish-options
Kim Alvefur <zash@zash.se>
parents: 2256
diff changeset
   256
		options = publish_options and st.preserialize(publish_options);
3059
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
   257
		timestamp = os_time();
1787
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   258
	};
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   259
	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
   260
	if not ok then
eb5555a3a535 mod_cloud_notify: Enable persistent storage of user notification settings
Kim Alvefur <zash@zash.se>
parents: 2204
diff changeset
   261
		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
   262
	else
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   263
		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
   264
		origin.push_settings = push_service;
3112
cfcb020bcd1d mod_cloud_notify: inform mod_smacks of first push in hibernated state
tmolitor <thilo@eightysoft.de>
parents: 3089
diff changeset
   265
		origin.first_hibernated_push = nil;
2629
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
   266
		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
   267
		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
   268
	end
1787
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   269
	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
   270
end
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   271
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
   272
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
   273
-- 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
   274
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
   275
	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
   276
	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
   277
	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
   278
	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
   279
		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
   280
		return true;
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   281
	end
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   282
	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
   283
	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
   284
		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
   285
			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
   286
			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
   287
				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
   288
				origin.push_settings = nil;
3112
cfcb020bcd1d mod_cloud_notify: inform mod_smacks of first push in hibernated state
tmolitor <thilo@eightysoft.de>
parents: 3089
diff changeset
   289
				origin.first_hibernated_push = nil;
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   290
			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
   291
			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
   292
			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
   293
			if module.unhook then
3623
74aa35aeb08a mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents: 3112
diff changeset
   294
				local stanza_id = hashes.sha256(key, true)
74aa35aeb08a mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents: 3112
diff changeset
   295
				module:unhook("iq-error/host/"..stanza_id, handle_push_error);
74aa35aeb08a mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents: 3112
diff changeset
   296
				module:unhook("iq-result/host/"..stanza_id, handle_push_success);
74aa35aeb08a mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents: 3112
diff changeset
   297
				id2node[stanza_id] = 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
   298
			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
   299
		end
1787
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   300
	end
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   301
	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
   302
	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
   303
		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
   304
	else
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   305
		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
   306
	end
1787
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   307
	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
   308
end
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   309
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
   310
2980
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   311
-- Patched version of util.stanza:find() that supports giving stanza names
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   312
-- without their namespace, allowing for every namespace.
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   313
local function find(self, path)
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   314
	local pos = 1;
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   315
	local len = #path + 1;
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   316
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   317
	repeat
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   318
		local xmlns, name, text;
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   319
		local char = s_sub(path, pos, pos);
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   320
		if char == "@" then
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   321
			return self.attr[s_sub(path, pos + 1)];
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   322
		elseif char == "{" then
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   323
			xmlns, pos = s_match(path, "^([^}]+)}()", pos + 1);
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   324
		end
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   325
		name, text, pos = s_match(path, "^([^@/#]*)([/#]?)()", pos);
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   326
		name = name ~= "" and name or nil;
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   327
		if pos == len then
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   328
			if text == "#" then
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   329
				local child = xmlns ~= nil and self:get_child(name, xmlns) or self:child_with_name(name);
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   330
				return child and child:get_text() or nil;
2740
fff185e7ab73 mod_cloud_notify: Implement the "stripped stanzas" proposal.
tmolitor <thilo@eightysoft.de>
parents: 2718
diff changeset
   331
			end
2980
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   332
			return xmlns ~= nil and self:get_child(name, xmlns) or self:child_with_name(name);
2740
fff185e7ab73 mod_cloud_notify: Implement the "stripped stanzas" proposal.
tmolitor <thilo@eightysoft.de>
parents: 2718
diff changeset
   333
		end
2980
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   334
		self = xmlns ~= nil and self:get_child(name, xmlns) or self:child_with_name(name);
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   335
	until not self
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   336
	return nil;
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   337
end
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   338
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   339
-- is this push a high priority one (this is needed for ios apps not using voip pushes)
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   340
local function is_important(stanza)
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   341
	local st_name = stanza and stanza.name or nil;
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   342
	if not st_name then return false; end	-- nonzas are never important here
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   343
	if st_name == "presence" then
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   344
		return false;						-- same for presences
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   345
	elseif st_name == "message" then
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   346
		-- unpack carbon copies
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   347
		local stanza_direction = "in";
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   348
		local carbon;
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   349
		local st_type;
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   350
		-- support carbon copied message stanzas having an arbitrary message-namespace or no message-namespace at all
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   351
		if not carbon then carbon = find(stanza, "{urn:xmpp:carbons:2}/forwarded/message"); end
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   352
		if not carbon then carbon = find(stanza, "{urn:xmpp:carbons:1}/forwarded/message"); end
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   353
		stanza_direction = carbon and stanza:child_with_name("sent") and "out" or "in";
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   354
		if carbon then stanza = carbon; end
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   355
		st_type = stanza.attr.type;
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   356
		
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   357
		-- headline message are always not important
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   358
		if st_type == "headline" then return false; end
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   359
		
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   360
		-- carbon copied outgoing messages are not important
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   361
		if carbon and stanza_direction == "out" then return false; end
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   362
		
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   363
		-- We can't check for body contents in encrypted messages, so let's treat them as important
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   364
		-- Some clients don't even set a body or an empty body for encrypted messages
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   365
		
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   366
		-- check omemo https://xmpp.org/extensions/inbox/omemo.html
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   367
		if stanza:get_child("encrypted", "eu.siacs.conversations.axolotl") or stanza:get_child("encrypted", "urn:xmpp:omemo:0") then return true; end
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   368
		
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   369
		-- check xep27 pgp https://xmpp.org/extensions/xep-0027.html
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   370
		if stanza:get_child("x", "jabber:x:encrypted") then return true; end
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   371
		
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   372
		-- check xep373 pgp (OX) https://xmpp.org/extensions/xep-0373.html
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   373
		if stanza:get_child("openpgp", "urn:xmpp:openpgp:0") then return true; end
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   374
		
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   375
		local body = stanza:get_child_text("body");
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   376
		if st_type == "groupchat" and stanza:get_child_text("subject") then return false; end		-- groupchat subjects are not important here
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   377
		return body ~= nil and body ~= "";			-- empty bodies are not important
2740
fff185e7ab73 mod_cloud_notify: Implement the "stripped stanzas" proposal.
tmolitor <thilo@eightysoft.de>
parents: 2718
diff changeset
   378
	end
2980
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   379
	return false;		-- this stanza wasn't one of the above cases --> it is not important, too
2740
fff185e7ab73 mod_cloud_notify: Implement the "stripped stanzas" proposal.
tmolitor <thilo@eightysoft.de>
parents: 2718
diff changeset
   380
end
fff185e7ab73 mod_cloud_notify: Implement the "stripped stanzas" proposal.
tmolitor <thilo@eightysoft.de>
parents: 2718
diff changeset
   381
1787
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   382
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
   383
	{ 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
   384
	{ 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
   385
	{ 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
   386
	{ 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
   387
	{ 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
   388
};
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   389
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
   390
-- http://xmpp.org/extensions/xep-0357.html#publishing
3112
cfcb020bcd1d mod_cloud_notify: inform mod_smacks of first push in hibernated state
tmolitor <thilo@eightysoft.de>
parents: 3089
diff changeset
   391
local function handle_notify_request(stanza, node, user_push_services, log_push_decline)
2716
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   392
	local pushes = 0;
3083
2a918a8c47db mod_cloud_notify: use next() instead of # operator and update README
tmolitor <thilo@eightysoft.de>
parents: 3082
diff changeset
   393
	if not user_push_services or next(user_push_services) == nil 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
   394
	
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   395
	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
   396
		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
   397
		if stanza then
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
   398
			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
   399
			if stanza._push_notify[push_identifier] then
3112
cfcb020bcd1d mod_cloud_notify: inform mod_smacks of first push in hibernated state
tmolitor <thilo@eightysoft.de>
parents: 3089
diff changeset
   400
				if log_push_decline then
cfcb020bcd1d mod_cloud_notify: inform mod_smacks of first push in hibernated state
tmolitor <thilo@eightysoft.de>
parents: 3089
diff changeset
   401
					module:log("debug", "Already sent push notification for %s@%s to %s (%s)", node, module.host, push_info.jid, tostring(push_info.node));
cfcb020bcd1d mod_cloud_notify: inform mod_smacks of first push in hibernated state
tmolitor <thilo@eightysoft.de>
parents: 3089
diff changeset
   402
				end
2716
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   403
				send_push = false;
2629
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
   404
			end
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
   405
			stanza._push_notify[push_identifier] = true;
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
   406
		end
2716
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   407
		
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   408
		if send_push then
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   409
			-- construct push stanza
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   410
			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
   411
			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
   412
				: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
   413
					: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
   414
						:tag("item")
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   415
							:tag("notification", { xmlns = xmlns_push });
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   416
			local form_data = {
3014
7ee59f417c16 mod_cloud_notify: remove useless counter (hardcode it to 1)
tmolitor <thilo@eightysoft.de>
parents: 2980
diff changeset
   417
				-- hardcode to 1 because other numbers are just meaningless (the XEP does not specify *what exactly* to count)
7ee59f417c16 mod_cloud_notify: remove useless counter (hardcode it to 1)
tmolitor <thilo@eightysoft.de>
parents: 2980
diff changeset
   418
				["message-count"] = "1";
2716
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   419
			};
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   420
			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
   421
				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
   422
			end
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   423
			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
   424
				form_data["last-message-body"] = stanza:get_child_text("body");
3112
cfcb020bcd1d mod_cloud_notify: inform mod_smacks of first push in hibernated state
tmolitor <thilo@eightysoft.de>
parents: 3089
diff changeset
   425
			elseif stanza and dummy_body and is_important(stanza) then
cfcb020bcd1d mod_cloud_notify: inform mod_smacks of first push in hibernated state
tmolitor <thilo@eightysoft.de>
parents: 3089
diff changeset
   426
				form_data["last-message-body"] = tostring(dummy_body);
2716
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   427
			end
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   428
			push_publish:add_child(push_form:form(form_data));
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   429
			push_publish:up(); -- / notification
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   430
			push_publish:up(); -- / publish
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   431
			push_publish:up(); -- / pubsub
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   432
			if push_info.options then
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   433
				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
   434
			end
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   435
			-- send out push
3112
cfcb020bcd1d mod_cloud_notify: inform mod_smacks of first push in hibernated state
tmolitor <thilo@eightysoft.de>
parents: 3089
diff changeset
   436
			module:log("debug", "Sending%s push notification for %s@%s to %s (%s)", form_data["last-message-body"] and " important" or "", 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
   437
			-- 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
   438
			-- 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
   439
			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
   440
				push_errors[push_identifier] = 0;
2795
008cf272b7ea mod_cloud_notify: Fix regression in error handling
tmolitor <thilo@eightysoft.de>
parents: 2755
diff changeset
   441
				module:hook("iq-error/host/"..stanza_id, handle_push_error);
008cf272b7ea mod_cloud_notify: Fix regression in error handling
tmolitor <thilo@eightysoft.de>
parents: 2755
diff changeset
   442
				module:hook("iq-result/host/"..stanza_id, handle_push_success);
008cf272b7ea mod_cloud_notify: Fix regression in error handling
tmolitor <thilo@eightysoft.de>
parents: 2755
diff changeset
   443
				id2node[stanza_id] = node;
2716
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   444
			end
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   445
			module:send(push_publish);
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   446
			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
   447
		end
1787
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   448
	end
2716
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   449
	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
   450
end
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   451
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   452
-- 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
   453
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
   454
	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
   455
	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
   456
	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
   457
	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
   458
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
   459
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
   460
-- 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
   461
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
   462
	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
   463
	module:log("debug", "Invoking cloud handle_notify_request() for offline stanza");
3112
cfcb020bcd1d mod_cloud_notify: inform mod_smacks of first push in hibernated state
tmolitor <thilo@eightysoft.de>
parents: 3089
diff changeset
   464
	handle_notify_request(event.stanza, node, user_push_services, true);
1787
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   465
end, 1);
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   466
3623
74aa35aeb08a mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents: 3112
diff changeset
   467
local function process_stanza_queue(queue, session, queue_type)
2718
75b137cf869a mod_cloud_notify: Don't notify for all smacks queued stanzas in a row
tmolitor <thilo@eightysoft.de>
parents: 2716
diff changeset
   468
	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
   469
	local user_push_services = {[session.push_identifier] = session.push_settings};
3112
cfcb020bcd1d mod_cloud_notify: inform mod_smacks of first push in hibernated state
tmolitor <thilo@eightysoft.de>
parents: 3089
diff changeset
   470
	local notified = { unimportant = false; important = false }
2718
75b137cf869a mod_cloud_notify: Don't notify for all smacks queued stanzas in a row
tmolitor <thilo@eightysoft.de>
parents: 2716
diff changeset
   471
	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
   472
		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
   473
		local node = get_push_settings(stanza, session);
3112
cfcb020bcd1d mod_cloud_notify: inform mod_smacks of first push in hibernated state
tmolitor <thilo@eightysoft.de>
parents: 3089
diff changeset
   474
		stanza_type = "unimportant"
cfcb020bcd1d mod_cloud_notify: inform mod_smacks of first push in hibernated state
tmolitor <thilo@eightysoft.de>
parents: 3089
diff changeset
   475
		if dummy_body and is_important(stanza) then stanza_type = "important"; end
cfcb020bcd1d mod_cloud_notify: inform mod_smacks of first push in hibernated state
tmolitor <thilo@eightysoft.de>
parents: 3089
diff changeset
   476
		if not notified[stanza_type] then		-- only notify if we didn't try to push for this stanza type already
cfcb020bcd1d mod_cloud_notify: inform mod_smacks of first push in hibernated state
tmolitor <thilo@eightysoft.de>
parents: 3089
diff changeset
   477
			-- session.log("debug", "Invoking cloud handle_notify_request() for smacks queued stanza: %d", i);
cfcb020bcd1d mod_cloud_notify: inform mod_smacks of first push in hibernated state
tmolitor <thilo@eightysoft.de>
parents: 3089
diff changeset
   478
			if handle_notify_request(stanza, node, user_push_services, false) ~= 0 then
cfcb020bcd1d mod_cloud_notify: inform mod_smacks of first push in hibernated state
tmolitor <thilo@eightysoft.de>
parents: 3089
diff changeset
   479
				if session.hibernating and not session.first_hibernated_push then
cfcb020bcd1d mod_cloud_notify: inform mod_smacks of first push in hibernated state
tmolitor <thilo@eightysoft.de>
parents: 3089
diff changeset
   480
					-- if important stanzas are treated differently (pushed with last-message-body field set to dummy string)
cfcb020bcd1d mod_cloud_notify: inform mod_smacks of first push in hibernated state
tmolitor <thilo@eightysoft.de>
parents: 3089
diff changeset
   481
					-- and the message was important (e.g. had a last-message-body field) OR if we treat all pushes equally,
cfcb020bcd1d mod_cloud_notify: inform mod_smacks of first push in hibernated state
tmolitor <thilo@eightysoft.de>
parents: 3089
diff changeset
   482
					-- then record the time of first push in the session for the smack module which will extend its hibernation
cfcb020bcd1d mod_cloud_notify: inform mod_smacks of first push in hibernated state
tmolitor <thilo@eightysoft.de>
parents: 3089
diff changeset
   483
					-- timeout based on the value of session.first_hibernated_push
cfcb020bcd1d mod_cloud_notify: inform mod_smacks of first push in hibernated state
tmolitor <thilo@eightysoft.de>
parents: 3089
diff changeset
   484
					if not dummy_body or (dummy_body and is_important(stanza)) then
cfcb020bcd1d mod_cloud_notify: inform mod_smacks of first push in hibernated state
tmolitor <thilo@eightysoft.de>
parents: 3089
diff changeset
   485
						session.first_hibernated_push = os_time();
cfcb020bcd1d mod_cloud_notify: inform mod_smacks of first push in hibernated state
tmolitor <thilo@eightysoft.de>
parents: 3089
diff changeset
   486
					end
cfcb020bcd1d mod_cloud_notify: inform mod_smacks of first push in hibernated state
tmolitor <thilo@eightysoft.de>
parents: 3089
diff changeset
   487
				end
3623
74aa35aeb08a mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents: 3112
diff changeset
   488
				session.log("debug", "Cloud handle_notify_request() > 0, not notifying for other %s queued stanzas of type %s", queue_type, stanza_type);
3112
cfcb020bcd1d mod_cloud_notify: inform mod_smacks of first push in hibernated state
tmolitor <thilo@eightysoft.de>
parents: 3089
diff changeset
   489
				notified[stanza_type] = true
cfcb020bcd1d mod_cloud_notify: inform mod_smacks of first push in hibernated state
tmolitor <thilo@eightysoft.de>
parents: 3089
diff changeset
   490
			end
2718
75b137cf869a mod_cloud_notify: Don't notify for all smacks queued stanzas in a row
tmolitor <thilo@eightysoft.de>
parents: 2716
diff changeset
   491
		end
75b137cf869a mod_cloud_notify: Don't notify for all smacks queued stanzas in a row
tmolitor <thilo@eightysoft.de>
parents: 2716
diff changeset
   492
	end
75b137cf869a mod_cloud_notify: Don't notify for all smacks queued stanzas in a row
tmolitor <thilo@eightysoft.de>
parents: 2716
diff changeset
   493
end
75b137cf869a mod_cloud_notify: Don't notify for all smacks queued stanzas in a row
tmolitor <thilo@eightysoft.de>
parents: 2716
diff changeset
   494
3623
74aa35aeb08a mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents: 3112
diff changeset
   495
-- publish on unacked smacks message (use timer to send out push for all stanzas submitted in a row only once)
74aa35aeb08a mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents: 3112
diff changeset
   496
local function process_smacks_stanza(stanza, session)
74aa35aeb08a mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents: 3112
diff changeset
   497
	if session.push_identifier then
74aa35aeb08a mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents: 3112
diff changeset
   498
		if not session.push_queue then session.push_queue = {}; end
74aa35aeb08a mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents: 3112
diff changeset
   499
		local queue = session.push_queue;
74aa35aeb08a mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents: 3112
diff changeset
   500
		queue[#queue+1] = st.clone(stanza);
74aa35aeb08a mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents: 3112
diff changeset
   501
		if #queue == 1 then		-- first stanza --> start timer
74aa35aeb08a mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents: 3112
diff changeset
   502
			session.log("debug", "Invoking cloud handle_notify_request() for newly smacks queued stanza (in a moment)");
74aa35aeb08a mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents: 3112
diff changeset
   503
			session.awaiting_push_timer = stoppable_timer(1e-06, function ()
74aa35aeb08a mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents: 3112
diff changeset
   504
				session.log("debug", "Invoking cloud handle_notify_request() for newly smacks queued stanzas (now in timer)");
74aa35aeb08a mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents: 3112
diff changeset
   505
				process_stanza_queue(session.push_queue, session, "push");
74aa35aeb08a mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents: 3112
diff changeset
   506
				session.push_queue = {};		-- clean up queue after push
74aa35aeb08a mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents: 3112
diff changeset
   507
			end);
74aa35aeb08a mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents: 3112
diff changeset
   508
		end
74aa35aeb08a mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents: 3112
diff changeset
   509
	end
74aa35aeb08a mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents: 3112
diff changeset
   510
	return stanza;
74aa35aeb08a mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents: 3112
diff changeset
   511
end
74aa35aeb08a mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents: 3112
diff changeset
   512
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
   513
-- 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
   514
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
   515
	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
   516
	local queue = event.queue;
3112
cfcb020bcd1d mod_cloud_notify: inform mod_smacks of first push in hibernated state
tmolitor <thilo@eightysoft.de>
parents: 3089
diff changeset
   517
	session.first_hibernated_push = nil;
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
   518
	-- process unacked stanzas
3623
74aa35aeb08a mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents: 3112
diff changeset
   519
	process_stanza_queue(queue, session, "smacks");
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
   520
	-- process future unacked (hibernated) stanzas
2740
fff185e7ab73 mod_cloud_notify: Implement the "stripped stanzas" proposal.
tmolitor <thilo@eightysoft.de>
parents: 2718
diff changeset
   521
	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
   522
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
   523
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
   524
-- 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
   525
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
   526
	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
   527
	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
   528
		filters.remove_filter(session, "stanzas/out", process_smacks_stanza);
3623
74aa35aeb08a mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents: 3112
diff changeset
   529
		if session.awaiting_push_timer then session.awaiting_push_timer:stop(); end
3112
cfcb020bcd1d mod_cloud_notify: inform mod_smacks of first push in hibernated state
tmolitor <thilo@eightysoft.de>
parents: 3089
diff changeset
   530
		session.first_hibernated_push = nil;
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   531
	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
   532
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
   533
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
   534
-- 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
   535
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
   536
	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
   537
	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
   538
	-- process unacked stanzas (handle_notify_request() will only send push requests for new stanzas)
3623
74aa35aeb08a mod_cloud_notify: only push once on csi queue flush in hibernated state, unhook response handlers
tmolitor <thilo@eightysoft.de>
parents: 3112
diff changeset
   539
	process_stanza_queue(queue, session, "smacks");
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   540
end
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   541
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   542
-- 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
   543
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
   544
	-- event is: { origin = origin, stanza = stanza, for_user = store_user, id = id }
3059
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
   545
	-- only notify for new mam messages when at least one device is online
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   546
	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
   547
	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
   548
	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
   549
	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
   550
	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
   551
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   552
	-- 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
   553
	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
   554
		local user_push_services = push_store:get(to);
3083
2a918a8c47db mod_cloud_notify: use next() instead of # operator and update README
tmolitor <thilo@eightysoft.de>
parents: 3082
diff changeset
   555
		if next(user_push_services) == nil then return end
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   556
		
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   557
		-- only notify nodes with no active sessions (smacks is counted as active and handled separate)
3112
cfcb020bcd1d mod_cloud_notify: inform mod_smacks of first push in hibernated state
tmolitor <thilo@eightysoft.de>
parents: 3089
diff changeset
   558
		local notify_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
   559
		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
   560
			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
   561
			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
   562
				-- 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
   563
				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
   564
					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
   565
					break;
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   566
				end
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   567
			end
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   568
			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
   569
				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
   570
			else
3112
cfcb020bcd1d mod_cloud_notify: inform mod_smacks of first push in hibernated state
tmolitor <thilo@eightysoft.de>
parents: 3089
diff changeset
   571
				notify_push_services[identifier] = push_info;
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   572
			end
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   573
		end
2647
777d07e0cd73 mod_cloud_notify: Whitespace fixes
Matthew Wild <mwild1@gmail.com>
parents: 2646
diff changeset
   574
3112
cfcb020bcd1d mod_cloud_notify: inform mod_smacks of first push in hibernated state
tmolitor <thilo@eightysoft.de>
parents: 3089
diff changeset
   575
		handle_notify_request(event.stanza, to, notify_push_services, true);
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
   576
	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
   577
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
   578
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
   579
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
   580
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
   581
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
   582
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
   583
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   584
local function send_ping(event)
3630
c84bbf36c878 mod_cloud_notify: fix local variable usage
tmolitor <thilo@eightysoft.de>
parents: 3626
diff changeset
   585
	local user = event.user;
3631
9639c493f4b9 mod_cloud_notify: fix typo (used || instead of or)
tmolitor <thilo@eightysoft.de>
parents: 3630
diff changeset
   586
	local push_services = event.push_services or push_store:get(user);
3112
cfcb020bcd1d mod_cloud_notify: inform mod_smacks of first push in hibernated state
tmolitor <thilo@eightysoft.de>
parents: 3089
diff changeset
   587
	handle_notify_request(nil, user, push_services, true);
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   588
end
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   589
-- 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
   590
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
   591
2629
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
   592
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
   593
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
   594
	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
   595
		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
   596
		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
   597
		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
   598
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
   599
		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
   600
		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
   601
		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
   602
		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
   603
		module:unhook("cloud-notify-ping", send_ping);
2647
777d07e0cd73 mod_cloud_notify: Whitespace fixes
Matthew Wild <mwild1@gmail.com>
parents: 2646
diff changeset
   604
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
   605
		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
   606
			local stanza_id = hashes.sha256(push_identifier, true);
2795
008cf272b7ea mod_cloud_notify: Fix regression in error handling
tmolitor <thilo@eightysoft.de>
parents: 2755
diff changeset
   607
			module:unhook("iq-error/host/"..stanza_id, handle_push_error);
008cf272b7ea mod_cloud_notify: Fix regression in error handling
tmolitor <thilo@eightysoft.de>
parents: 2755
diff changeset
   608
			module:unhook("iq-result/host/"..stanza_id, handle_push_success);
008cf272b7ea mod_cloud_notify: Fix regression in error handling
tmolitor <thilo@eightysoft.de>
parents: 2755
diff changeset
   609
			id2node[stanza_id] = 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
   610
		end
1787
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   611
	end
2647
777d07e0cd73 mod_cloud_notify: Whitespace fixes
Matthew Wild <mwild1@gmail.com>
parents: 2646
diff changeset
   612
2629
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
   613
	module:log("info", "Module unloaded");
2647
777d07e0cd73 mod_cloud_notify: Whitespace fixes
Matthew Wild <mwild1@gmail.com>
parents: 2646
diff changeset
   614
end