mod_cloud_notify/mod_cloud_notify.lua
author Kim Alvefur <zash@zash.se>
Sun, 03 Mar 2024 11:23:40 +0100
changeset 5857 97c9b76867ca
parent 5219 fd6cb4365438
permissions -rw-r--r--
mod_log_ringbuffer: Detach event handlers on logging reload (thanks Menel) Otherwise the global event handlers accumulate, one added each time logging is reoladed, and each invocation of the signal or event triggers one dump of each created ringbuffer.
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
3059
6abee021d9db mod_cloud_notify: Limit number of devices to 5 and change some default settings
tmolitor <thilo@eightysoft.de>
parents: 3014
diff changeset
     7
local os_time = 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
     8
local st = require"util.stanza";
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     9
local jid = require"util.jid";
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    10
local dataform = require"util.dataforms".new;
2629
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
    11
local hashes = require"util.hashes";
3947
f5e6368a1c39 mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents: 3944
diff changeset
    12
local random = require"util.random";
f5e6368a1c39 mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents: 3944
diff changeset
    13
local cache = require"util.cache";
4972
487f1eb829cf mod_cloud_notify: Compat for prosody 0.12
tmolitor <thilo@eightysoft.de>
parents: 4831
diff changeset
    14
local watchdog = require "util.watchdog";
1787
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    15
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    16
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
    17
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
    18
-- configuration
2250
a3e3dc9131e7 mod_cloud_notify: Use typed config API
Kim Alvefur <zash@zash.se>
parents: 2205
diff changeset
    19
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
    20
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
    21
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
    22
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
    23
local dummy_body = module:get_option_string("push_notification_important_body", "New Message!");
5057
00e8cc6860cb mod_cloud_notify: Fix default extended timeout from 72 days -> 72 hours
Matthew Wild <mwild1@gmail.com>
parents: 5056
diff changeset
    24
local extended_hibernation_timeout = module:get_option_number("push_max_hibernation_timeout", 72*3600);  -- use same timeout like ejabberd
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
    25
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
    26
local host_sessions = prosody.hosts[module.host].sessions;
4371
33f82988d7a9 mod_cloud_notify: Make push_errors a shared table to persist across reloads and share with other modules
Matthew Wild <mwild1@gmail.com>
parents: 4359
diff changeset
    27
local push_errors = module:shared("push_errors");
2795
008cf272b7ea mod_cloud_notify: Fix regression in error handling
tmolitor <thilo@eightysoft.de>
parents: 2755
diff changeset
    28
local id2node = {};
3947
f5e6368a1c39 mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents: 3944
diff changeset
    29
local id2identifier = {};
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
    30
5219
fd6cb4365438 mod_cloud_notify: Log warning when used on Lua 5.1
Matthew Wild <mwild1@gmail.com>
parents: 5061
diff changeset
    31
if _VERSION:match("5%.1") then
fd6cb4365438 mod_cloud_notify: Log warning when used on Lua 5.1
Matthew Wild <mwild1@gmail.com>
parents: 5061
diff changeset
    32
	module:log("warn", "This module may behave incorrectly on Lua 5.1. It is recommended to upgrade to a newer Lua version.");
fd6cb4365438 mod_cloud_notify: Log warning when used on Lua 5.1
Matthew Wild <mwild1@gmail.com>
parents: 5061
diff changeset
    33
end
fd6cb4365438 mod_cloud_notify: Log warning when used on Lua 5.1
Matthew Wild <mwild1@gmail.com>
parents: 5061
diff changeset
    34
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
    35
-- For keeping state across reloads while caching reads
3947
f5e6368a1c39 mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents: 3944
diff changeset
    36
-- This uses util.cache for caching the most recent devices and removing all old devices when max_push_devices is reached
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
    37
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
    38
	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
    39
	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
    40
	local api = {};
4299
d44a8d3dd571 mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents: 4277
diff changeset
    41
	--luacheck: ignore 212/self
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
    42
	function api:get(user)
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
    43
		if not push_services[user] then
3947
f5e6368a1c39 mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents: 3944
diff changeset
    44
			local loaded, err = store:get(user);
f5e6368a1c39 mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents: 3944
diff changeset
    45
			if not loaded and err then
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
    46
				module:log("warn", "Error reading push notification storage for user '%s': %s", user, tostring(err));
3947
f5e6368a1c39 mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents: 3944
diff changeset
    47
				push_services[user] = cache.new(max_push_devices):table();
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
    48
				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
    49
			end
3947
f5e6368a1c39 mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents: 3944
diff changeset
    50
			if loaded then
f5e6368a1c39 mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents: 3944
diff changeset
    51
				push_services[user] = cache.new(max_push_devices):table();
f5e6368a1c39 mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents: 3944
diff changeset
    52
				-- copy over plain table loaded from disk into our cache
f5e6368a1c39 mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents: 3944
diff changeset
    53
				for k, v in pairs(loaded) do push_services[user][k] = v; end
f5e6368a1c39 mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents: 3944
diff changeset
    54
			else
f5e6368a1c39 mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents: 3944
diff changeset
    55
				push_services[user] = cache.new(max_push_devices):table();
f5e6368a1c39 mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents: 3944
diff changeset
    56
			end
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
    57
		end
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
    58
		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
    59
	end
3947
f5e6368a1c39 mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents: 3944
diff changeset
    60
	function api:flush_to_disk(user)
f5e6368a1c39 mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents: 3944
diff changeset
    61
		local plain_table = {};
f5e6368a1c39 mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents: 3944
diff changeset
    62
		for k, v in pairs(push_services[user]) do plain_table[k] = v; end
f5e6368a1c39 mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents: 3944
diff changeset
    63
		local ok, err = store:set(user, plain_table);
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
    64
		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
    65
			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
    66
			return false;
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
    67
		end
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
    68
		return true;
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
    69
	end
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
    70
	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
    71
		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
    72
		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
    73
	end
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
    74
	return api;
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
    75
end)();
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
    76
3112
cfcb020bcd1d mod_cloud_notify: inform mod_smacks of first push in hibernated state
tmolitor <thilo@eightysoft.de>
parents: 3089
diff changeset
    77
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
    78
-- 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
    79
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
    80
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
    81
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
    82
	local stanza = event.stanza;
4831
fe5303da99cb mod_cloud_notify: Include extra error details if available
Kim Alvefur <zash@zash.se>
parents: 4734
diff changeset
    83
	local error_type, condition, error_text = stanza:get_error();
2795
008cf272b7ea mod_cloud_notify: Fix regression in error handling
tmolitor <thilo@eightysoft.de>
parents: 2755
diff changeset
    84
	local node = id2node[stanza.attr.id];
3947
f5e6368a1c39 mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents: 3944
diff changeset
    85
	local identifier = id2identifier[stanza.attr.id];
5061
c728e82265a7 mod_cloud_notify: Improve logging for various error cases
Matthew Wild <mwild1@gmail.com>
parents: 5057
diff changeset
    86
	if node == nil then
c728e82265a7 mod_cloud_notify: Improve logging for various error cases
Matthew Wild <mwild1@gmail.com>
parents: 5057
diff changeset
    87
		module:log("warn", "Received push error with unrecognised id: %s", stanza.attr.id);
c728e82265a7 mod_cloud_notify: Improve logging for various error cases
Matthew Wild <mwild1@gmail.com>
parents: 5057
diff changeset
    88
		return false; -- unknown stanza? Ignore for now!
c728e82265a7 mod_cloud_notify: Improve logging for various error cases
Matthew Wild <mwild1@gmail.com>
parents: 5057
diff changeset
    89
	end
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
    90
	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
    91
	local user_push_services = push_store:get(node);
5061
c728e82265a7 mod_cloud_notify: Improve logging for various error cases
Matthew Wild <mwild1@gmail.com>
parents: 5057
diff changeset
    92
	local found, changed = false, false;
4299
d44a8d3dd571 mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents: 4277
diff changeset
    93
2629
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
    94
	for push_identifier, _ in pairs(user_push_services) do
3947
f5e6368a1c39 mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents: 3944
diff changeset
    95
		if push_identifier == identifier then
5061
c728e82265a7 mod_cloud_notify: Improve logging for various error cases
Matthew Wild <mwild1@gmail.com>
parents: 5057
diff changeset
    96
			found = true;
2629
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
    97
			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
    98
				push_errors[push_identifier] = push_errors[push_identifier] + 1;
4831
fe5303da99cb mod_cloud_notify: Include extra error details if available
Kim Alvefur <zash@zash.se>
parents: 4734
diff changeset
    99
				module:log("info", "Got error <%s:%s:%s> for identifier '%s': "
fe5303da99cb mod_cloud_notify: Include extra error details if available
Kim Alvefur <zash@zash.se>
parents: 4734
diff changeset
   100
					.."error count for this identifier is now at %s", error_type, condition, error_text or "", push_identifier,
2629
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
   101
					tostring(push_errors[push_identifier]));
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
   102
				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
   103
					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
   104
					-- remove push settings from sessions
2753
9756211fcbe3 mod_cloud_notify: Fix small bug.
tmolitor <thilo@eightysoft.de>
parents: 2740
diff changeset
   105
					if host_sessions[node] then
9756211fcbe3 mod_cloud_notify: Fix small bug.
tmolitor <thilo@eightysoft.de>
parents: 2740
diff changeset
   106
						for _, session in pairs(host_sessions[node].sessions) do
9756211fcbe3 mod_cloud_notify: Fix small bug.
tmolitor <thilo@eightysoft.de>
parents: 2740
diff changeset
   107
							if session.push_identifier == push_identifier then
9756211fcbe3 mod_cloud_notify: Fix small bug.
tmolitor <thilo@eightysoft.de>
parents: 2740
diff changeset
   108
								session.push_identifier = nil;
9756211fcbe3 mod_cloud_notify: Fix small bug.
tmolitor <thilo@eightysoft.de>
parents: 2740
diff changeset
   109
								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
   110
								session.first_hibernated_push = nil;
4972
487f1eb829cf mod_cloud_notify: Compat for prosody 0.12
tmolitor <thilo@eightysoft.de>
parents: 4831
diff changeset
   111
								-- check for prosody 0.12 mod_smacks
487f1eb829cf mod_cloud_notify: Compat for prosody 0.12
tmolitor <thilo@eightysoft.de>
parents: 4831
diff changeset
   112
								if session.hibernating_watchdog and session.original_smacks_callback and session.original_smacks_timeout then
487f1eb829cf mod_cloud_notify: Compat for prosody 0.12
tmolitor <thilo@eightysoft.de>
parents: 4831
diff changeset
   113
									-- restore old smacks watchdog
487f1eb829cf mod_cloud_notify: Compat for prosody 0.12
tmolitor <thilo@eightysoft.de>
parents: 4831
diff changeset
   114
									session.hibernating_watchdog:cancel();
487f1eb829cf mod_cloud_notify: Compat for prosody 0.12
tmolitor <thilo@eightysoft.de>
parents: 4831
diff changeset
   115
									session.hibernating_watchdog = watchdog.new(session.original_smacks_timeout, session.original_smacks_callback);
487f1eb829cf mod_cloud_notify: Compat for prosody 0.12
tmolitor <thilo@eightysoft.de>
parents: 4831
diff changeset
   116
								end
2753
9756211fcbe3 mod_cloud_notify: Fix small bug.
tmolitor <thilo@eightysoft.de>
parents: 2740
diff changeset
   117
							end
2629
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
   118
						end
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
   119
					end
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
   120
					-- save changed global config
3089
1ea6861b533f mod_cloud_notify: Don't change table while iterating it
tmolitor <thilo@eightysoft.de>
parents: 3083
diff changeset
   121
					changed = true;
1ea6861b533f mod_cloud_notify: Don't change table while iterating it
tmolitor <thilo@eightysoft.de>
parents: 3083
diff changeset
   122
					user_push_services[push_identifier] = nil
2629
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
   123
					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
   124
					-- unhook iq handlers for this identifier (if possible)
3947
f5e6368a1c39 mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents: 3944
diff changeset
   125
					module:unhook("iq-error/host/"..stanza.attr.id, handle_push_error);
f5e6368a1c39 mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents: 3944
diff changeset
   126
					module:unhook("iq-result/host/"..stanza.attr.id, handle_push_success);
f5e6368a1c39 mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents: 3944
diff changeset
   127
					id2node[stanza.attr.id] = nil;
f5e6368a1c39 mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents: 3944
diff changeset
   128
					id2identifier[stanza.attr.id] = nil;
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   129
				end
2629
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
   130
			elseif user_push_services[push_identifier] and user_push_services[push_identifier].jid == from and error_type == "wait" then
4831
fe5303da99cb mod_cloud_notify: Include extra error details if available
Kim Alvefur <zash@zash.se>
parents: 4734
diff changeset
   131
				module:log("debug", "Got error <%s:%s:%s> for identifier '%s': "
fe5303da99cb mod_cloud_notify: Include extra error details if available
Kim Alvefur <zash@zash.se>
parents: 4734
diff changeset
   132
					.."NOT increasing error count for this identifier", error_type, condition, error_text or "", push_identifier);
5061
c728e82265a7 mod_cloud_notify: Improve logging for various error cases
Matthew Wild <mwild1@gmail.com>
parents: 5057
diff changeset
   133
			else
c728e82265a7 mod_cloud_notify: Improve logging for various error cases
Matthew Wild <mwild1@gmail.com>
parents: 5057
diff changeset
   134
				module:log("debug", "Unhandled push error <%s:%s:%s> from %s for identifier '%s'",
c728e82265a7 mod_cloud_notify: Improve logging for various error cases
Matthew Wild <mwild1@gmail.com>
parents: 5057
diff changeset
   135
					error_type, condition, error_text or "", from, push_identifier
c728e82265a7 mod_cloud_notify: Improve logging for various error cases
Matthew Wild <mwild1@gmail.com>
parents: 5057
diff changeset
   136
				);
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   137
			end
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   138
		end
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   139
	end
3089
1ea6861b533f mod_cloud_notify: Don't change table while iterating it
tmolitor <thilo@eightysoft.de>
parents: 3083
diff changeset
   140
	if changed then
3947
f5e6368a1c39 mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents: 3944
diff changeset
   141
		push_store:flush_to_disk(node);
5061
c728e82265a7 mod_cloud_notify: Improve logging for various error cases
Matthew Wild <mwild1@gmail.com>
parents: 5057
diff changeset
   142
	elseif not found then
c728e82265a7 mod_cloud_notify: Improve logging for various error cases
Matthew Wild <mwild1@gmail.com>
parents: 5057
diff changeset
   143
		module:log("warn", "Unable to find matching registration for push error <%s:%s:%s> from %s", error_type, condition, error_text or "", from);
3089
1ea6861b533f mod_cloud_notify: Don't change table while iterating it
tmolitor <thilo@eightysoft.de>
parents: 3083
diff changeset
   144
	end
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   145
	return true;
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   146
end
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   147
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
   148
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
   149
	local stanza = event.stanza;
2795
008cf272b7ea mod_cloud_notify: Fix regression in error handling
tmolitor <thilo@eightysoft.de>
parents: 2755
diff changeset
   150
	local node = id2node[stanza.attr.id];
3947
f5e6368a1c39 mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents: 3944
diff changeset
   151
	local identifier = id2identifier[stanza.attr.id];
2795
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);
4299
d44a8d3dd571 mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents: 4277
diff changeset
   155
2629
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
   156
	for push_identifier, _ in pairs(user_push_services) do
3947
f5e6368a1c39 mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents: 3944
diff changeset
   157
		if push_identifier == identifier 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
   158
			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
   159
				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
   160
				-- unhook iq handlers for this identifier (if possible)
3947
f5e6368a1c39 mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents: 3944
diff changeset
   161
				module:unhook("iq-error/host/"..stanza.attr.id, handle_push_error);
f5e6368a1c39 mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents: 3944
diff changeset
   162
				module:unhook("iq-result/host/"..stanza.attr.id, handle_push_success);
f5e6368a1c39 mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents: 3944
diff changeset
   163
				id2node[stanza.attr.id] = nil;
f5e6368a1c39 mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents: 3944
diff changeset
   164
				id2identifier[stanza.attr.id] = nil;
4299
d44a8d3dd571 mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents: 4277
diff changeset
   165
				module:log("debug", "Push succeeded, error count for identifier '%s' is now at %s again",
d44a8d3dd571 mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents: 4277
diff changeset
   166
					push_identifier, tostring(push_errors[push_identifier])
d44a8d3dd571 mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents: 4277
diff changeset
   167
				);
2629
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
   168
			end
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
   169
		end
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   170
	end
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   171
	return true;
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   172
end
1787
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   173
1912
eba279ddc050 mod_cloud_notify: Add some comments describing code blocks
Kim Alvefur <zash@zash.se>
parents: 1911
diff changeset
   174
-- 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
   175
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
   176
	(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
   177
end
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   178
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
   179
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
   180
-- 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
   181
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
   182
	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
   183
	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
   184
	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
   185
	-- 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
   186
	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
   187
	-- SHOULD contain a 'node' attribute
2258
122cb5f4930f mod_cloud_notify: Cache <enable> in local
Kim Alvefur <zash@zash.se>
parents: 2257
diff changeset
   188
	local push_node = enable.attr.node;
2740
fff185e7ab73 mod_cloud_notify: Implement the "stripped stanzas" proposal.
tmolitor <thilo@eightysoft.de>
parents: 2718
diff changeset
   189
	-- CAN contain a 'include_payload' attribute
fff185e7ab73 mod_cloud_notify: Implement the "stripped stanzas" proposal.
tmolitor <thilo@eightysoft.de>
parents: 2718
diff changeset
   190
	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
   191
	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
   192
		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
   193
		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
   194
		return true;
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   195
	end
4000
42682505e692 mod_cloud_notify: Forbid user from registering their own JID as their push server
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3983
diff changeset
   196
	if push_jid == stanza.attr.from then
42682505e692 mod_cloud_notify: Forbid user from registering their own JID as their push server
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3983
diff changeset
   197
		origin.log("debug", "Push notification enable request 'jid' field identical to our own");
42682505e692 mod_cloud_notify: Forbid user from registering their own JID as their push server
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3983
diff changeset
   198
		origin.send(st.error_reply(stanza, "modify", "bad-request", "JID must be different from ours"));
42682505e692 mod_cloud_notify: Forbid user from registering their own JID as their push server
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3983
diff changeset
   199
		return true;
42682505e692 mod_cloud_notify: Forbid user from registering their own JID as their push server
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3983
diff changeset
   200
	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
   201
	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
   202
	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
   203
		-- Could be intentional
3abc51faf945 mod_cloud_notify: Log message if no dataform is found
Kim Alvefur <zash@zash.se>
parents: 2261
diff changeset
   204
		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
   205
	end
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   206
	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
   207
	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
   208
		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
   209
		node = push_node;
2740
fff185e7ab73 mod_cloud_notify: Implement the "stripped stanzas" proposal.
tmolitor <thilo@eightysoft.de>
parents: 2718
diff changeset
   210
		include_payload = include_payload;
2257
97ebd28a8a75 mod_cloud_notify: Apply pre-serialization to publish-options
Kim Alvefur <zash@zash.se>
parents: 2256
diff changeset
   211
		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
   212
		timestamp = os_time();
4720
8b3e91249cff mod_cloud_notify: Move client info out of sub-object to improve usability with map store API
Matthew Wild <mwild1@gmail.com>
parents: 4717
diff changeset
   213
		client_id = origin.client_id;
4722
6e3254e13fb7 mod_cloud_notify: Fix traceback for clients without a stable client_id
Matthew Wild <mwild1@gmail.com>
parents: 4720
diff changeset
   214
		resource = not origin.client_id and origin.resource or nil;
4720
8b3e91249cff mod_cloud_notify: Move client info out of sub-object to improve usability with map store API
Matthew Wild <mwild1@gmail.com>
parents: 4717
diff changeset
   215
		language = stanza.attr["xml:lang"];
1787
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   216
	};
4329
9b95241c6ae5 mod_cloud_notify: Fire register and push events for integration with other modules
Matthew Wild <mwild1@gmail.com>
parents: 4328
diff changeset
   217
	local allow_registration = module:fire_event("cloud_notify/registration", {
9b95241c6ae5 mod_cloud_notify: Fire register and push events for integration with other modules
Matthew Wild <mwild1@gmail.com>
parents: 4328
diff changeset
   218
		origin = origin, stanza = stanza, push_info = push_service;
9b95241c6ae5 mod_cloud_notify: Fire register and push events for integration with other modules
Matthew Wild <mwild1@gmail.com>
parents: 4328
diff changeset
   219
	});
9b95241c6ae5 mod_cloud_notify: Fire register and push events for integration with other modules
Matthew Wild <mwild1@gmail.com>
parents: 4328
diff changeset
   220
	if allow_registration == false then
9b95241c6ae5 mod_cloud_notify: Fire register and push events for integration with other modules
Matthew Wild <mwild1@gmail.com>
parents: 4328
diff changeset
   221
		return true; -- Assume error reply already sent
9b95241c6ae5 mod_cloud_notify: Fire register and push events for integration with other modules
Matthew Wild <mwild1@gmail.com>
parents: 4328
diff changeset
   222
	end
3947
f5e6368a1c39 mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents: 3944
diff changeset
   223
	push_store:set_identifier(origin.username, push_identifier, push_service);
f5e6368a1c39 mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents: 3944
diff changeset
   224
	local ok = push_store:flush_to_disk(origin.username);
2205
eb5555a3a535 mod_cloud_notify: Enable persistent storage of user notification settings
Kim Alvefur <zash@zash.se>
parents: 2204
diff changeset
   225
	if not ok then
eb5555a3a535 mod_cloud_notify: Enable persistent storage of user notification settings
Kim Alvefur <zash@zash.se>
parents: 2204
diff changeset
   226
		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
   227
	else
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   228
		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
   229
		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
   230
		origin.first_hibernated_push = nil;
2629
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
   231
		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
   232
		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
   233
	end
1787
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   234
	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
   235
end
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   236
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
   237
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
   238
-- 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
   239
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
   240
	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
   241
	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
   242
	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
   243
	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
   244
		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
   245
		return true;
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   246
	end
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   247
	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
   248
	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
   249
		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
   250
			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
   251
			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
   252
				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
   253
				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
   254
				origin.first_hibernated_push = nil;
4972
487f1eb829cf mod_cloud_notify: Compat for prosody 0.12
tmolitor <thilo@eightysoft.de>
parents: 4831
diff changeset
   255
				-- check for prosody 0.12 mod_smacks
4984
da151f9af861 replaced 'session' with 'origin' in push_disable
arcseconds
parents: 4972
diff changeset
   256
				if origin.hibernating_watchdog and origin.original_smacks_callback and origin.original_smacks_timeout then
4972
487f1eb829cf mod_cloud_notify: Compat for prosody 0.12
tmolitor <thilo@eightysoft.de>
parents: 4831
diff changeset
   257
					-- restore old smacks watchdog
4984
da151f9af861 replaced 'session' with 'origin' in push_disable
arcseconds
parents: 4972
diff changeset
   258
					origin.hibernating_watchdog:cancel();
da151f9af861 replaced 'session' with 'origin' in push_disable
arcseconds
parents: 4972
diff changeset
   259
					origin.hibernating_watchdog = watchdog.new(origin.original_smacks_timeout, origin.original_smacks_callback);
4972
487f1eb829cf mod_cloud_notify: Compat for prosody 0.12
tmolitor <thilo@eightysoft.de>
parents: 4831
diff changeset
   260
				end
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   261
			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
   262
			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
   263
			push_errors[key] = nil;
3947
f5e6368a1c39 mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents: 3944
diff changeset
   264
			for stanza_id, identifier in pairs(id2identifier) do
f5e6368a1c39 mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents: 3944
diff changeset
   265
				if identifier == key then
f5e6368a1c39 mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents: 3944
diff changeset
   266
					module:unhook("iq-error/host/"..stanza_id, handle_push_error);
f5e6368a1c39 mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents: 3944
diff changeset
   267
					module:unhook("iq-result/host/"..stanza_id, handle_push_success);
f5e6368a1c39 mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents: 3944
diff changeset
   268
					id2node[stanza_id] = nil;
f5e6368a1c39 mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents: 3944
diff changeset
   269
					id2identifier[stanza_id] = nil;
f5e6368a1c39 mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents: 3944
diff changeset
   270
				end
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
   271
			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
   272
		end
1787
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   273
	end
3947
f5e6368a1c39 mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents: 3944
diff changeset
   274
	local ok = push_store:flush_to_disk(origin.username);
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   275
	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
   276
		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
   277
	else
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   278
		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
   279
	end
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;
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   281
end
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   282
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
   283
4734
1da4b815d2fe mod_cloud_notify: Identify (and immediately push) urgent stanzas, e.g. calls
Matthew Wild <mwild1@gmail.com>
parents: 4722
diff changeset
   284
-- urgent stanzas should be delivered without delay
1da4b815d2fe mod_cloud_notify: Identify (and immediately push) urgent stanzas, e.g. calls
Matthew Wild <mwild1@gmail.com>
parents: 4722
diff changeset
   285
local function is_urgent(stanza)
1da4b815d2fe mod_cloud_notify: Identify (and immediately push) urgent stanzas, e.g. calls
Matthew Wild <mwild1@gmail.com>
parents: 4722
diff changeset
   286
	-- TODO
1da4b815d2fe mod_cloud_notify: Identify (and immediately push) urgent stanzas, e.g. calls
Matthew Wild <mwild1@gmail.com>
parents: 4722
diff changeset
   287
	if stanza.name == "message" then
1da4b815d2fe mod_cloud_notify: Identify (and immediately push) urgent stanzas, e.g. calls
Matthew Wild <mwild1@gmail.com>
parents: 4722
diff changeset
   288
		if stanza:get_child("propose", "urn:xmpp:jingle-message:0") then
1da4b815d2fe mod_cloud_notify: Identify (and immediately push) urgent stanzas, e.g. calls
Matthew Wild <mwild1@gmail.com>
parents: 4722
diff changeset
   289
			return true, "jingle call";
1da4b815d2fe mod_cloud_notify: Identify (and immediately push) urgent stanzas, e.g. calls
Matthew Wild <mwild1@gmail.com>
parents: 4722
diff changeset
   290
		end
1da4b815d2fe mod_cloud_notify: Identify (and immediately push) urgent stanzas, e.g. calls
Matthew Wild <mwild1@gmail.com>
parents: 4722
diff changeset
   291
	end
1da4b815d2fe mod_cloud_notify: Identify (and immediately push) urgent stanzas, e.g. calls
Matthew Wild <mwild1@gmail.com>
parents: 4722
diff changeset
   292
end
1da4b815d2fe mod_cloud_notify: Identify (and immediately push) urgent stanzas, e.g. calls
Matthew Wild <mwild1@gmail.com>
parents: 4722
diff changeset
   293
2980
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   294
-- 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
   295
local function is_important(stanza)
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   296
	local st_name = stanza and stanza.name or nil;
4299
d44a8d3dd571 mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents: 4277
diff changeset
   297
	if not st_name then return false; end -- nonzas are never important here
2980
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   298
	if st_name == "presence" then
4299
d44a8d3dd571 mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents: 4277
diff changeset
   299
		return false; -- same for presences
2980
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   300
	elseif st_name == "message" then
4299
d44a8d3dd571 mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents: 4277
diff changeset
   301
		-- unpack carbon copied message stanzas
d44a8d3dd571 mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents: 4277
diff changeset
   302
		local carbon = stanza:find("{urn:xmpp:carbons:2}/{urn:xmpp:forward:0}/{jabber:client}message");
d44a8d3dd571 mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents: 4277
diff changeset
   303
		local stanza_direction = carbon and stanza:child_with_name("sent") and "out" or "in";
2980
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   304
		if carbon then stanza = carbon; end
4299
d44a8d3dd571 mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents: 4277
diff changeset
   305
		local st_type = stanza.attr.type;
d44a8d3dd571 mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents: 4277
diff changeset
   306
2980
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   307
		-- headline message are always not important
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   308
		if st_type == "headline" then return false; end
4299
d44a8d3dd571 mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents: 4277
diff changeset
   309
2980
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   310
		-- 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
   311
		if carbon and stanza_direction == "out" then return false; end
4299
d44a8d3dd571 mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents: 4277
diff changeset
   312
2980
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   313
		-- 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
   314
		-- Some clients don't even set a body or an empty body for encrypted messages
4299
d44a8d3dd571 mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents: 4277
diff changeset
   315
2980
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   316
		-- 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
   317
		if stanza:get_child("encrypted", "eu.siacs.conversations.axolotl") or stanza:get_child("encrypted", "urn:xmpp:omemo:0") then return true; end
4299
d44a8d3dd571 mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents: 4277
diff changeset
   318
2980
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   319
		-- 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
   320
		if stanza:get_child("x", "jabber:x:encrypted") then return true; end
4299
d44a8d3dd571 mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents: 4277
diff changeset
   321
2980
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   322
		-- 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
   323
		if stanza:get_child("openpgp", "urn:xmpp:openpgp:0") then return true; end
4299
d44a8d3dd571 mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents: 4277
diff changeset
   324
4468
ea820de69265 mod_cloud_notify: Consider incoming XEP-0353 call requests as important
Matthew Wild <mwild1@gmail.com>
parents: 4433
diff changeset
   325
		-- XEP-0353: Jingle Message Initiation (incoming call request)
ea820de69265 mod_cloud_notify: Consider incoming XEP-0353 call requests as important
Matthew Wild <mwild1@gmail.com>
parents: 4433
diff changeset
   326
		if stanza:get_child("propose", "urn:xmpp:jingle-message:0") then return true; end
ea820de69265 mod_cloud_notify: Consider incoming XEP-0353 call requests as important
Matthew Wild <mwild1@gmail.com>
parents: 4433
diff changeset
   327
2980
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   328
		local body = stanza:get_child_text("body");
4299
d44a8d3dd571 mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents: 4277
diff changeset
   329
d44a8d3dd571 mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents: 4277
diff changeset
   330
		-- groupchat subjects are not important here
d44a8d3dd571 mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents: 4277
diff changeset
   331
		if st_type == "groupchat" and stanza:get_child_text("subject") then
d44a8d3dd571 mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents: 4277
diff changeset
   332
			return false;
d44a8d3dd571 mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents: 4277
diff changeset
   333
		end
d44a8d3dd571 mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents: 4277
diff changeset
   334
d44a8d3dd571 mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents: 4277
diff changeset
   335
		-- empty bodies are not important
d44a8d3dd571 mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents: 4277
diff changeset
   336
		return body ~= nil and body ~= "";
2740
fff185e7ab73 mod_cloud_notify: Implement the "stripped stanzas" proposal.
tmolitor <thilo@eightysoft.de>
parents: 2718
diff changeset
   337
	end
2980
df86ce6bb0b4 Implement dummy body message to indicate high priority push
tmolitor <thilo@eightysoft.de>
parents: 2796
diff changeset
   338
	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
   339
end
fff185e7ab73 mod_cloud_notify: Implement the "stripped stanzas" proposal.
tmolitor <thilo@eightysoft.de>
parents: 2718
diff changeset
   340
1787
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   341
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
   342
	{ 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
   343
	{ 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
   344
	{ 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
   345
	{ 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
   346
	{ 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
   347
};
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   348
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
   349
-- 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
   350
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
   351
	local pushes = 0;
3947
f5e6368a1c39 mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents: 3944
diff changeset
   352
	if not #user_push_services then return pushes end
4299
d44a8d3dd571 mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents: 4277
diff changeset
   353
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   354
	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
   355
		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
   356
		if stanza then
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
   357
			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
   358
			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
   359
				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
   360
					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
   361
				end
2716
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   362
				send_push = false;
2629
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
   363
			end
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
   364
			stanza._push_notify[push_identifier] = true;
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
   365
		end
4299
d44a8d3dd571 mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents: 4277
diff changeset
   366
2716
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   367
		if send_push then
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   368
			-- construct push stanza
3947
f5e6368a1c39 mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents: 3944
diff changeset
   369
			local stanza_id = hashes.sha256(random.bytes(8), true);
4336
7a5ca4b285cb mod_cloud_notify: Some restructuring to allow direct access to notification element within push event
Matthew Wild <mwild1@gmail.com>
parents: 4332
diff changeset
   370
			local push_notification_payload = st.stanza("notification", { xmlns = xmlns_push });
2716
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   371
			local form_data = {
3014
7ee59f417c16 mod_cloud_notify: remove useless counter (hardcode it to 1)
tmolitor <thilo@eightysoft.de>
parents: 2980
diff changeset
   372
				-- 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
   373
				["message-count"] = "1";
2716
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   374
			};
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   375
			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
   376
				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
   377
			end
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   378
			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
   379
				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
   380
			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
   381
				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
   382
			end
4336
7a5ca4b285cb mod_cloud_notify: Some restructuring to allow direct access to notification element within push event
Matthew Wild <mwild1@gmail.com>
parents: 4332
diff changeset
   383
4433
157fa4e535b0 mod_cloud_notify: Fix nesting of push form (thanks ivucica/Andrzej)
Matthew Wild <mwild1@gmail.com>
parents: 4416
diff changeset
   384
			push_notification_payload:add_child(push_form:form(form_data));
157fa4e535b0 mod_cloud_notify: Fix nesting of push form (thanks ivucica/Andrzej)
Matthew Wild <mwild1@gmail.com>
parents: 4416
diff changeset
   385
4336
7a5ca4b285cb mod_cloud_notify: Some restructuring to allow direct access to notification element within push event
Matthew Wild <mwild1@gmail.com>
parents: 4332
diff changeset
   386
			local push_publish = st.iq({ to = push_info.jid, from = module.host, type = "set", id = stanza_id })
7a5ca4b285cb mod_cloud_notify: Some restructuring to allow direct access to notification element within push event
Matthew Wild <mwild1@gmail.com>
parents: 4332
diff changeset
   387
				:tag("pubsub", { xmlns = "http://jabber.org/protocol/pubsub" })
7a5ca4b285cb mod_cloud_notify: Some restructuring to allow direct access to notification element within push event
Matthew Wild <mwild1@gmail.com>
parents: 4332
diff changeset
   388
					:tag("publish", { node = push_info.node })
7a5ca4b285cb mod_cloud_notify: Some restructuring to allow direct access to notification element within push event
Matthew Wild <mwild1@gmail.com>
parents: 4332
diff changeset
   389
						:tag("item")
7a5ca4b285cb mod_cloud_notify: Some restructuring to allow direct access to notification element within push event
Matthew Wild <mwild1@gmail.com>
parents: 4332
diff changeset
   390
							:add_child(push_notification_payload)
7a5ca4b285cb mod_cloud_notify: Some restructuring to allow direct access to notification element within push event
Matthew Wild <mwild1@gmail.com>
parents: 4332
diff changeset
   391
						:up()
7a5ca4b285cb mod_cloud_notify: Some restructuring to allow direct access to notification element within push event
Matthew Wild <mwild1@gmail.com>
parents: 4332
diff changeset
   392
					:up();
7a5ca4b285cb mod_cloud_notify: Some restructuring to allow direct access to notification element within push event
Matthew Wild <mwild1@gmail.com>
parents: 4332
diff changeset
   393
2716
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   394
			if push_info.options then
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   395
				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
   396
			end
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   397
			-- send out push
4299
d44a8d3dd571 mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents: 4277
diff changeset
   398
			module:log("debug", "Sending %s push notification for %s@%s to %s (%s)",
d44a8d3dd571 mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents: 4277
diff changeset
   399
				form_data["last-message-body"] and "important" or "unimportant",
d44a8d3dd571 mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents: 4277
diff changeset
   400
				node, module.host, push_info.jid, tostring(push_info.node)
d44a8d3dd571 mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents: 4277
diff changeset
   401
			);
2740
fff185e7ab73 mod_cloud_notify: Implement the "stripped stanzas" proposal.
tmolitor <thilo@eightysoft.de>
parents: 2718
diff changeset
   402
			-- module:log("debug", "PUSH STANZA: %s", tostring(push_publish));
4329
9b95241c6ae5 mod_cloud_notify: Fire register and push events for integration with other modules
Matthew Wild <mwild1@gmail.com>
parents: 4328
diff changeset
   403
			local push_event = {
9b95241c6ae5 mod_cloud_notify: Fire register and push events for integration with other modules
Matthew Wild <mwild1@gmail.com>
parents: 4328
diff changeset
   404
				notification_stanza = push_publish;
4336
7a5ca4b285cb mod_cloud_notify: Some restructuring to allow direct access to notification element within push event
Matthew Wild <mwild1@gmail.com>
parents: 4332
diff changeset
   405
				notification_payload = push_notification_payload;
4329
9b95241c6ae5 mod_cloud_notify: Fire register and push events for integration with other modules
Matthew Wild <mwild1@gmail.com>
parents: 4328
diff changeset
   406
				original_stanza = stanza;
5000
031e0dd90f4b mod_cloud_notify: Rename field in event for clarity ('node' is ambiguous here)
Matthew Wild <mwild1@gmail.com>
parents: 4984
diff changeset
   407
				username = node;
4329
9b95241c6ae5 mod_cloud_notify: Fire register and push events for integration with other modules
Matthew Wild <mwild1@gmail.com>
parents: 4328
diff changeset
   408
				push_info = push_info;
9b95241c6ae5 mod_cloud_notify: Fire register and push events for integration with other modules
Matthew Wild <mwild1@gmail.com>
parents: 4328
diff changeset
   409
				push_summary = form_data;
4332
5a30daf2ac02 mod_cloud_notify: Include 'important' flag in push event data
Matthew Wild <mwild1@gmail.com>
parents: 4329
diff changeset
   410
				important = not not form_data["last-message-body"];
4329
9b95241c6ae5 mod_cloud_notify: Fire register and push events for integration with other modules
Matthew Wild <mwild1@gmail.com>
parents: 4328
diff changeset
   411
			};
9b95241c6ae5 mod_cloud_notify: Fire register and push events for integration with other modules
Matthew Wild <mwild1@gmail.com>
parents: 4328
diff changeset
   412
9b95241c6ae5 mod_cloud_notify: Fire register and push events for integration with other modules
Matthew Wild <mwild1@gmail.com>
parents: 4328
diff changeset
   413
			if module:fire_event("cloud_notify/push", push_event) then
9b95241c6ae5 mod_cloud_notify: Fire register and push events for integration with other modules
Matthew Wild <mwild1@gmail.com>
parents: 4328
diff changeset
   414
				module:log("debug", "Push was blocked by event handler: %s", push_event.reason or "Unknown reason");
9b95241c6ae5 mod_cloud_notify: Fire register and push events for integration with other modules
Matthew Wild <mwild1@gmail.com>
parents: 4328
diff changeset
   415
			else
9b95241c6ae5 mod_cloud_notify: Fire register and push events for integration with other modules
Matthew Wild <mwild1@gmail.com>
parents: 4328
diff changeset
   416
				-- handle push errors for this node
9b95241c6ae5 mod_cloud_notify: Fire register and push events for integration with other modules
Matthew Wild <mwild1@gmail.com>
parents: 4328
diff changeset
   417
				if push_errors[push_identifier] == nil then
9b95241c6ae5 mod_cloud_notify: Fire register and push events for integration with other modules
Matthew Wild <mwild1@gmail.com>
parents: 4328
diff changeset
   418
					push_errors[push_identifier] = 0;
9b95241c6ae5 mod_cloud_notify: Fire register and push events for integration with other modules
Matthew Wild <mwild1@gmail.com>
parents: 4328
diff changeset
   419
				end
9b95241c6ae5 mod_cloud_notify: Fire register and push events for integration with other modules
Matthew Wild <mwild1@gmail.com>
parents: 4328
diff changeset
   420
				module:hook("iq-error/host/"..stanza_id, handle_push_error);
9b95241c6ae5 mod_cloud_notify: Fire register and push events for integration with other modules
Matthew Wild <mwild1@gmail.com>
parents: 4328
diff changeset
   421
				module:hook("iq-result/host/"..stanza_id, handle_push_success);
9b95241c6ae5 mod_cloud_notify: Fire register and push events for integration with other modules
Matthew Wild <mwild1@gmail.com>
parents: 4328
diff changeset
   422
				id2node[stanza_id] = node;
9b95241c6ae5 mod_cloud_notify: Fire register and push events for integration with other modules
Matthew Wild <mwild1@gmail.com>
parents: 4328
diff changeset
   423
				id2identifier[stanza_id] = push_identifier;
9b95241c6ae5 mod_cloud_notify: Fire register and push events for integration with other modules
Matthew Wild <mwild1@gmail.com>
parents: 4328
diff changeset
   424
				module:send(push_publish);
9b95241c6ae5 mod_cloud_notify: Fire register and push events for integration with other modules
Matthew Wild <mwild1@gmail.com>
parents: 4328
diff changeset
   425
				pushes = pushes + 1;
2716
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   426
			end
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
   427
		end
1787
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   428
	end
2716
d89ab70808f6 mod_cloud_notify: fix bug when multiple resources are used
tmolitor <thilo@eightysoft.de>
parents: 2674
diff changeset
   429
	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
   430
end
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   431
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   432
-- 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
   433
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
   434
	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
   435
	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
   436
	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
   437
	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
   438
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
   439
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
   440
-- 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
   441
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
   442
	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
   443
	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
   444
	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
   445
end, 1);
b31fe2d22310 mod_cloud_notify: XEP-0357: Push - the server bits ("app server" not included)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   446
4702
4157773ed4d3 mod_cloud_notify: notify on groupchat messages sent to bare JID
arcseconds <arcseconds@zoho.com>
parents: 4468
diff changeset
   447
-- publish on bare groupchat
4716
48d7a5c16f2b mod_cloud_notify: Fix some minor luacheck offences
Matthew Wild <mwild1@gmail.com>
parents: 4702
diff changeset
   448
-- this picks up MUC messages when there are no devices connected
4702
4157773ed4d3 mod_cloud_notify: notify on groupchat messages sent to bare JID
arcseconds <arcseconds@zoho.com>
parents: 4468
diff changeset
   449
module:hook("message/bare/groupchat", function(event)
4157773ed4d3 mod_cloud_notify: notify on groupchat messages sent to bare JID
arcseconds <arcseconds@zoho.com>
parents: 4468
diff changeset
   450
	module:log("debug", "Invoking cloud handle_notify_request() for bare groupchat stanza");
4157773ed4d3 mod_cloud_notify: notify on groupchat messages sent to bare JID
arcseconds <arcseconds@zoho.com>
parents: 4468
diff changeset
   451
	local node, user_push_services = get_push_settings(event.stanza, event.origin);
4157773ed4d3 mod_cloud_notify: notify on groupchat messages sent to bare JID
arcseconds <arcseconds@zoho.com>
parents: 4468
diff changeset
   452
	handle_notify_request(event.stanza, node, user_push_services, true);
4157773ed4d3 mod_cloud_notify: notify on groupchat messages sent to bare JID
arcseconds <arcseconds@zoho.com>
parents: 4468
diff changeset
   453
end, 1);
4157773ed4d3 mod_cloud_notify: notify on groupchat messages sent to bare JID
arcseconds <arcseconds@zoho.com>
parents: 4468
diff changeset
   454
4157773ed4d3 mod_cloud_notify: notify on groupchat messages sent to bare JID
arcseconds <arcseconds@zoho.com>
parents: 4468
diff changeset
   455
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
   456
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
   457
	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
   458
	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
   459
	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
   460
	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
   461
		local stanza = queue[i];
3983
6bf362008052 mod_cloud_notify: make stanza queue processing faster
tmolitor <thilo@eightysoft.de>
parents: 3947
diff changeset
   462
		-- fast ignore of already pushed stanzas
6bf362008052 mod_cloud_notify: make stanza queue processing faster
tmolitor <thilo@eightysoft.de>
parents: 3947
diff changeset
   463
		if stanza and not (stanza._push_notify and stanza._push_notify[session.push_identifier]) then
6bf362008052 mod_cloud_notify: make stanza queue processing faster
tmolitor <thilo@eightysoft.de>
parents: 3947
diff changeset
   464
			local node = get_push_settings(stanza, session);
4299
d44a8d3dd571 mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents: 4277
diff changeset
   465
			local stanza_type = "unimportant";
3983
6bf362008052 mod_cloud_notify: make stanza queue processing faster
tmolitor <thilo@eightysoft.de>
parents: 3947
diff changeset
   466
			if dummy_body and is_important(stanza) then stanza_type = "important"; end
6bf362008052 mod_cloud_notify: make stanza queue processing faster
tmolitor <thilo@eightysoft.de>
parents: 3947
diff changeset
   467
			if not notified[stanza_type] then		-- only notify if we didn't try to push for this stanza type already
6bf362008052 mod_cloud_notify: make stanza queue processing faster
tmolitor <thilo@eightysoft.de>
parents: 3947
diff changeset
   468
				-- session.log("debug", "Invoking cloud handle_notify_request() for smacks queued stanza: %d", i);
6bf362008052 mod_cloud_notify: make stanza queue processing faster
tmolitor <thilo@eightysoft.de>
parents: 3947
diff changeset
   469
				if handle_notify_request(stanza, node, user_push_services, false) ~= 0 then
6bf362008052 mod_cloud_notify: make stanza queue processing faster
tmolitor <thilo@eightysoft.de>
parents: 3947
diff changeset
   470
					if session.hibernating and not session.first_hibernated_push then
6bf362008052 mod_cloud_notify: make stanza queue processing faster
tmolitor <thilo@eightysoft.de>
parents: 3947
diff changeset
   471
						-- if important stanzas are treated differently (pushed with last-message-body field set to dummy string)
6bf362008052 mod_cloud_notify: make stanza queue processing faster
tmolitor <thilo@eightysoft.de>
parents: 3947
diff changeset
   472
						-- if the message was important (e.g. had a last-message-body field) OR if we treat all pushes equally,
6bf362008052 mod_cloud_notify: make stanza queue processing faster
tmolitor <thilo@eightysoft.de>
parents: 3947
diff changeset
   473
						-- then record the time of first push in the session for the smack module which will extend its hibernation
6bf362008052 mod_cloud_notify: make stanza queue processing faster
tmolitor <thilo@eightysoft.de>
parents: 3947
diff changeset
   474
						-- timeout based on the value of session.first_hibernated_push
6bf362008052 mod_cloud_notify: make stanza queue processing faster
tmolitor <thilo@eightysoft.de>
parents: 3947
diff changeset
   475
						if not dummy_body or (dummy_body and is_important(stanza)) then
6bf362008052 mod_cloud_notify: make stanza queue processing faster
tmolitor <thilo@eightysoft.de>
parents: 3947
diff changeset
   476
							session.first_hibernated_push = os_time();
4972
487f1eb829cf mod_cloud_notify: Compat for prosody 0.12
tmolitor <thilo@eightysoft.de>
parents: 4831
diff changeset
   477
							-- check for prosody 0.12 mod_smacks
487f1eb829cf mod_cloud_notify: Compat for prosody 0.12
tmolitor <thilo@eightysoft.de>
parents: 4831
diff changeset
   478
							if session.hibernating_watchdog and session.original_smacks_callback and session.original_smacks_timeout then
487f1eb829cf mod_cloud_notify: Compat for prosody 0.12
tmolitor <thilo@eightysoft.de>
parents: 4831
diff changeset
   479
								-- restore old smacks watchdog (--> the start of our original timeout will be delayed until first push)
487f1eb829cf mod_cloud_notify: Compat for prosody 0.12
tmolitor <thilo@eightysoft.de>
parents: 4831
diff changeset
   480
								session.hibernating_watchdog:cancel();
487f1eb829cf mod_cloud_notify: Compat for prosody 0.12
tmolitor <thilo@eightysoft.de>
parents: 4831
diff changeset
   481
								session.hibernating_watchdog = watchdog.new(session.original_smacks_timeout, session.original_smacks_callback);
487f1eb829cf mod_cloud_notify: Compat for prosody 0.12
tmolitor <thilo@eightysoft.de>
parents: 4831
diff changeset
   482
							end
3983
6bf362008052 mod_cloud_notify: make stanza queue processing faster
tmolitor <thilo@eightysoft.de>
parents: 3947
diff changeset
   483
						end
3112
cfcb020bcd1d mod_cloud_notify: inform mod_smacks of first push in hibernated state
tmolitor <thilo@eightysoft.de>
parents: 3089
diff changeset
   484
					end
3983
6bf362008052 mod_cloud_notify: make stanza queue processing faster
tmolitor <thilo@eightysoft.de>
parents: 3947
diff changeset
   485
					session.log("debug", "Cloud handle_notify_request() > 0, not notifying for other %s queued stanzas of type %s", queue_type, stanza_type);
6bf362008052 mod_cloud_notify: make stanza queue processing faster
tmolitor <thilo@eightysoft.de>
parents: 3947
diff changeset
   486
					notified[stanza_type] = true
3112
cfcb020bcd1d mod_cloud_notify: inform mod_smacks of first push in hibernated state
tmolitor <thilo@eightysoft.de>
parents: 3089
diff changeset
   487
				end
cfcb020bcd1d mod_cloud_notify: inform mod_smacks of first push in hibernated state
tmolitor <thilo@eightysoft.de>
parents: 3089
diff changeset
   488
			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
   489
		end
3983
6bf362008052 mod_cloud_notify: make stanza queue processing faster
tmolitor <thilo@eightysoft.de>
parents: 3947
diff changeset
   490
		if notified.unimportant and notified.important then break; end		-- stop processing the queue if all push types are exhausted
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
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
   494
-- publish on unacked smacks message (use timer to send out push for all stanzas submitted in a row only once)
4328
45dcf5d4cd6c mod_cloud_notify: fix push flooding on delayed acks
tmolitor <thilo@eightysoft.de>
parents: 4299
diff changeset
   495
local function process_stanza(session, 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
   496
	if session.push_identifier then
4277
8bf83e883593 mod_cloud_notify: Modernize interface to mod_smacks
tmolitor <thilo@eightysoft.de>
parents: 4225
diff changeset
   497
		session.log("debug", "adding new stanza to push_queue");
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
   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);
4359
31afa4f314cc mod_cloud_notify: Fix handling of push_queue
tmolitor <thilo@eightysoft.de>
parents: 4336
diff changeset
   501
		if not session.awaiting_push_timer then		-- timer not already running --> start new timer
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
   502
			session.log("debug", "Invoking cloud handle_notify_request() for newly smacks queued stanza (in a moment)");
4328
45dcf5d4cd6c mod_cloud_notify: fix push flooding on delayed acks
tmolitor <thilo@eightysoft.de>
parents: 4299
diff changeset
   503
			session.awaiting_push_timer = module:add_timer(1.0, 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
   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
4328
45dcf5d4cd6c mod_cloud_notify: fix push flooding on delayed acks
tmolitor <thilo@eightysoft.de>
parents: 4299
diff changeset
   507
				session.awaiting_push_timer = nil;
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
   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
4328
45dcf5d4cd6c mod_cloud_notify: fix push flooding on delayed acks
tmolitor <thilo@eightysoft.de>
parents: 4299
diff changeset
   510
	end
45dcf5d4cd6c mod_cloud_notify: fix push flooding on delayed acks
tmolitor <thilo@eightysoft.de>
parents: 4299
diff changeset
   511
	return stanza;
45dcf5d4cd6c mod_cloud_notify: fix push flooding on delayed acks
tmolitor <thilo@eightysoft.de>
parents: 4299
diff changeset
   512
end
45dcf5d4cd6c mod_cloud_notify: fix push flooding on delayed acks
tmolitor <thilo@eightysoft.de>
parents: 4299
diff changeset
   513
45dcf5d4cd6c mod_cloud_notify: fix push flooding on delayed acks
tmolitor <thilo@eightysoft.de>
parents: 4299
diff changeset
   514
local function process_smacks_stanza(event)
45dcf5d4cd6c mod_cloud_notify: fix push flooding on delayed acks
tmolitor <thilo@eightysoft.de>
parents: 4299
diff changeset
   515
	local session = event.origin;
45dcf5d4cd6c mod_cloud_notify: fix push flooding on delayed acks
tmolitor <thilo@eightysoft.de>
parents: 4299
diff changeset
   516
	local stanza = event.stanza;
45dcf5d4cd6c mod_cloud_notify: fix push flooding on delayed acks
tmolitor <thilo@eightysoft.de>
parents: 4299
diff changeset
   517
	if not session.push_identifier then
4299
d44a8d3dd571 mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents: 4277
diff changeset
   518
		session.log("debug", "NOT invoking cloud handle_notify_request() for newly smacks queued stanza (session.push_identifier is not set: %s)",
d44a8d3dd571 mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents: 4277
diff changeset
   519
			session.push_identifier
d44a8d3dd571 mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents: 4277
diff changeset
   520
		);
4328
45dcf5d4cd6c mod_cloud_notify: fix push flooding on delayed acks
tmolitor <thilo@eightysoft.de>
parents: 4299
diff changeset
   521
	else
45dcf5d4cd6c mod_cloud_notify: fix push flooding on delayed acks
tmolitor <thilo@eightysoft.de>
parents: 4299
diff changeset
   522
		process_stanza(session, 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
   523
	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
   524
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
   525
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
   526
-- 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
   527
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
   528
	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
   529
	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
   530
	session.first_hibernated_push = nil;
5056
d7ece68de066 mod_cloud_notify: Only delay hibernation timeout for push-enabled clients
Matthew Wild <mwild1@gmail.com>
parents: 5000
diff changeset
   531
	if session.push_identifier and session.hibernating_watchdog then -- check for prosody 0.12 mod_smacks
4972
487f1eb829cf mod_cloud_notify: Compat for prosody 0.12
tmolitor <thilo@eightysoft.de>
parents: 4831
diff changeset
   532
		-- save old watchdog callback and timeout
487f1eb829cf mod_cloud_notify: Compat for prosody 0.12
tmolitor <thilo@eightysoft.de>
parents: 4831
diff changeset
   533
		session.original_smacks_callback = session.hibernating_watchdog.callback;
487f1eb829cf mod_cloud_notify: Compat for prosody 0.12
tmolitor <thilo@eightysoft.de>
parents: 4831
diff changeset
   534
		session.original_smacks_timeout = session.hibernating_watchdog.timeout;
487f1eb829cf mod_cloud_notify: Compat for prosody 0.12
tmolitor <thilo@eightysoft.de>
parents: 4831
diff changeset
   535
		-- cancel old watchdog and create a new watchdog with extended timeout
487f1eb829cf mod_cloud_notify: Compat for prosody 0.12
tmolitor <thilo@eightysoft.de>
parents: 4831
diff changeset
   536
		session.hibernating_watchdog:cancel();
487f1eb829cf mod_cloud_notify: Compat for prosody 0.12
tmolitor <thilo@eightysoft.de>
parents: 4831
diff changeset
   537
		session.hibernating_watchdog = watchdog.new(extended_hibernation_timeout, function()
487f1eb829cf mod_cloud_notify: Compat for prosody 0.12
tmolitor <thilo@eightysoft.de>
parents: 4831
diff changeset
   538
			session.log("debug", "Push-extended smacks watchdog triggered");
487f1eb829cf mod_cloud_notify: Compat for prosody 0.12
tmolitor <thilo@eightysoft.de>
parents: 4831
diff changeset
   539
			if session.original_smacks_callback then
487f1eb829cf mod_cloud_notify: Compat for prosody 0.12
tmolitor <thilo@eightysoft.de>
parents: 4831
diff changeset
   540
				session.log("debug", "Calling original smacks watchdog handler");
487f1eb829cf mod_cloud_notify: Compat for prosody 0.12
tmolitor <thilo@eightysoft.de>
parents: 4831
diff changeset
   541
				session.original_smacks_callback();
487f1eb829cf mod_cloud_notify: Compat for prosody 0.12
tmolitor <thilo@eightysoft.de>
parents: 4831
diff changeset
   542
			end
487f1eb829cf mod_cloud_notify: Compat for prosody 0.12
tmolitor <thilo@eightysoft.de>
parents: 4831
diff changeset
   543
		end);
487f1eb829cf mod_cloud_notify: Compat for prosody 0.12
tmolitor <thilo@eightysoft.de>
parents: 4831
diff changeset
   544
	end
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
   545
	-- 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
   546
	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
   547
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
   548
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
   549
-- 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
   550
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
   551
	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
   552
	if session then		-- older smacks module versions send only the "intermediate" session in event.session and no session.resumed one
4416
e5493a10c4d1 mod_cloud_notify: use stanza from mod_smacks to behave O(1)
tmolitor <thilo@eightysoft.de>
parents: 4371
diff changeset
   553
		if session.awaiting_push_timer then
e5493a10c4d1 mod_cloud_notify: use stanza from mod_smacks to behave O(1)
tmolitor <thilo@eightysoft.de>
parents: 4371
diff changeset
   554
			session.awaiting_push_timer:stop();
e5493a10c4d1 mod_cloud_notify: use stanza from mod_smacks to behave O(1)
tmolitor <thilo@eightysoft.de>
parents: 4371
diff changeset
   555
			session.awaiting_push_timer = nil;
e5493a10c4d1 mod_cloud_notify: use stanza from mod_smacks to behave O(1)
tmolitor <thilo@eightysoft.de>
parents: 4371
diff changeset
   556
		end
3112
cfcb020bcd1d mod_cloud_notify: inform mod_smacks of first push in hibernated state
tmolitor <thilo@eightysoft.de>
parents: 3089
diff changeset
   557
		session.first_hibernated_push = nil;
4972
487f1eb829cf mod_cloud_notify: Compat for prosody 0.12
tmolitor <thilo@eightysoft.de>
parents: 4831
diff changeset
   558
		-- the extended smacks watchdog will be canceled by the smacks module, no need to anything here
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   559
	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
   560
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
   561
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
   562
-- 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
   563
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
   564
	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
   565
	local queue = event.queue;
4416
e5493a10c4d1 mod_cloud_notify: use stanza from mod_smacks to behave O(1)
tmolitor <thilo@eightysoft.de>
parents: 4371
diff changeset
   566
	local stanza = event.stanza;
4328
45dcf5d4cd6c mod_cloud_notify: fix push flooding on delayed acks
tmolitor <thilo@eightysoft.de>
parents: 4299
diff changeset
   567
	if not session.push_identifier then return; end
4416
e5493a10c4d1 mod_cloud_notify: use stanza from mod_smacks to behave O(1)
tmolitor <thilo@eightysoft.de>
parents: 4371
diff changeset
   568
	if stanza then process_stanza(session, stanza); return; end		-- don't iterate through smacks queue if we know which stanza triggered this
4328
45dcf5d4cd6c mod_cloud_notify: fix push flooding on delayed acks
tmolitor <thilo@eightysoft.de>
parents: 4299
diff changeset
   569
	for i=1, #queue do
4716
48d7a5c16f2b mod_cloud_notify: Fix some minor luacheck offences
Matthew Wild <mwild1@gmail.com>
parents: 4702
diff changeset
   570
		local queued_stanza = queue[i];
4328
45dcf5d4cd6c mod_cloud_notify: fix push flooding on delayed acks
tmolitor <thilo@eightysoft.de>
parents: 4299
diff changeset
   571
		-- process unacked stanzas (handle_notify_request() will only send push requests for new stanzas)
4716
48d7a5c16f2b mod_cloud_notify: Fix some minor luacheck offences
Matthew Wild <mwild1@gmail.com>
parents: 4702
diff changeset
   572
		process_stanza(session, queued_stanza);
4328
45dcf5d4cd6c mod_cloud_notify: fix push flooding on delayed acks
tmolitor <thilo@eightysoft.de>
parents: 4299
diff changeset
   573
	end
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   574
end
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   575
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   576
-- 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
   577
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
   578
	-- 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
   579
	-- 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
   580
	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
   581
	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
   582
	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
   583
	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
   584
	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
   585
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   586
	-- 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
   587
	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
   588
		local user_push_services = push_store:get(to);
4299
d44a8d3dd571 mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents: 4277
diff changeset
   589
4734
1da4b815d2fe mod_cloud_notify: Identify (and immediately push) urgent stanzas, e.g. calls
Matthew Wild <mwild1@gmail.com>
parents: 4722
diff changeset
   590
		-- Urgent stanzas are time-sensitive (e.g. calls) and should
1da4b815d2fe mod_cloud_notify: Identify (and immediately push) urgent stanzas, e.g. calls
Matthew Wild <mwild1@gmail.com>
parents: 4722
diff changeset
   591
		-- be pushed immediately to avoid getting stuck in the smacks
1da4b815d2fe mod_cloud_notify: Identify (and immediately push) urgent stanzas, e.g. calls
Matthew Wild <mwild1@gmail.com>
parents: 4722
diff changeset
   592
		-- queue in case of dead connections, for example
1da4b815d2fe mod_cloud_notify: Identify (and immediately push) urgent stanzas, e.g. calls
Matthew Wild <mwild1@gmail.com>
parents: 4722
diff changeset
   593
		local is_urgent_stanza, urgent_reason = is_urgent(event.stanza);
1da4b815d2fe mod_cloud_notify: Identify (and immediately push) urgent stanzas, e.g. calls
Matthew Wild <mwild1@gmail.com>
parents: 4722
diff changeset
   594
1da4b815d2fe mod_cloud_notify: Identify (and immediately push) urgent stanzas, e.g. calls
Matthew Wild <mwild1@gmail.com>
parents: 4722
diff changeset
   595
		local notify_push_services;
1da4b815d2fe mod_cloud_notify: Identify (and immediately push) urgent stanzas, e.g. calls
Matthew Wild <mwild1@gmail.com>
parents: 4722
diff changeset
   596
		if is_urgent_stanza then
1da4b815d2fe mod_cloud_notify: Identify (and immediately push) urgent stanzas, e.g. calls
Matthew Wild <mwild1@gmail.com>
parents: 4722
diff changeset
   597
			module:log("debug", "Urgent push for %s (%s)", to, urgent_reason);
1da4b815d2fe mod_cloud_notify: Identify (and immediately push) urgent stanzas, e.g. calls
Matthew Wild <mwild1@gmail.com>
parents: 4722
diff changeset
   598
			notify_push_services = user_push_services;
1da4b815d2fe mod_cloud_notify: Identify (and immediately push) urgent stanzas, e.g. calls
Matthew Wild <mwild1@gmail.com>
parents: 4722
diff changeset
   599
		else
1da4b815d2fe mod_cloud_notify: Identify (and immediately push) urgent stanzas, e.g. calls
Matthew Wild <mwild1@gmail.com>
parents: 4722
diff changeset
   600
			-- only notify nodes with no active sessions (smacks is counted as active and handled separate)
1da4b815d2fe mod_cloud_notify: Identify (and immediately push) urgent stanzas, e.g. calls
Matthew Wild <mwild1@gmail.com>
parents: 4722
diff changeset
   601
			notify_push_services = {};
1da4b815d2fe mod_cloud_notify: Identify (and immediately push) urgent stanzas, e.g. calls
Matthew Wild <mwild1@gmail.com>
parents: 4722
diff changeset
   602
			for identifier, push_info in pairs(user_push_services) do
1da4b815d2fe mod_cloud_notify: Identify (and immediately push) urgent stanzas, e.g. calls
Matthew Wild <mwild1@gmail.com>
parents: 4722
diff changeset
   603
				local identifier_found = nil;
1da4b815d2fe mod_cloud_notify: Identify (and immediately push) urgent stanzas, e.g. calls
Matthew Wild <mwild1@gmail.com>
parents: 4722
diff changeset
   604
				for _, session in pairs(user_session) do
1da4b815d2fe mod_cloud_notify: Identify (and immediately push) urgent stanzas, e.g. calls
Matthew Wild <mwild1@gmail.com>
parents: 4722
diff changeset
   605
					if session.push_identifier == identifier then
1da4b815d2fe mod_cloud_notify: Identify (and immediately push) urgent stanzas, e.g. calls
Matthew Wild <mwild1@gmail.com>
parents: 4722
diff changeset
   606
						identifier_found = session;
1da4b815d2fe mod_cloud_notify: Identify (and immediately push) urgent stanzas, e.g. calls
Matthew Wild <mwild1@gmail.com>
parents: 4722
diff changeset
   607
						break;
1da4b815d2fe mod_cloud_notify: Identify (and immediately push) urgent stanzas, e.g. calls
Matthew Wild <mwild1@gmail.com>
parents: 4722
diff changeset
   608
					end
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   609
				end
4734
1da4b815d2fe mod_cloud_notify: Identify (and immediately push) urgent stanzas, e.g. calls
Matthew Wild <mwild1@gmail.com>
parents: 4722
diff changeset
   610
				if identifier_found then
1da4b815d2fe mod_cloud_notify: Identify (and immediately push) urgent stanzas, e.g. calls
Matthew Wild <mwild1@gmail.com>
parents: 4722
diff changeset
   611
					identifier_found.log("debug", "Not cloud notifying '%s' of new MAM stanza (session still alive)", identifier);
1da4b815d2fe mod_cloud_notify: Identify (and immediately push) urgent stanzas, e.g. calls
Matthew Wild <mwild1@gmail.com>
parents: 4722
diff changeset
   612
				else
1da4b815d2fe mod_cloud_notify: Identify (and immediately push) urgent stanzas, e.g. calls
Matthew Wild <mwild1@gmail.com>
parents: 4722
diff changeset
   613
					notify_push_services[identifier] = push_info;
1da4b815d2fe mod_cloud_notify: Identify (and immediately push) urgent stanzas, e.g. calls
Matthew Wild <mwild1@gmail.com>
parents: 4722
diff changeset
   614
				end
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   615
			end
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   616
		end
2647
777d07e0cd73 mod_cloud_notify: Whitespace fixes
Matthew Wild <mwild1@gmail.com>
parents: 2646
diff changeset
   617
3112
cfcb020bcd1d mod_cloud_notify: inform mod_smacks of first push in hibernated state
tmolitor <thilo@eightysoft.de>
parents: 3089
diff changeset
   618
		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
   619
	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
   620
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
   621
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
   622
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
   623
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
   624
module:hook("smacks-ack-delayed", ack_delayed);
4277
8bf83e883593 mod_cloud_notify: Modernize interface to mod_smacks
tmolitor <thilo@eightysoft.de>
parents: 4225
diff changeset
   625
module:hook("smacks-hibernation-stanza-queued", process_smacks_stanza);
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   626
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
   627
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   628
local function send_ping(event)
3630
c84bbf36c878 mod_cloud_notify: fix local variable usage
tmolitor <thilo@eightysoft.de>
parents: 3626
diff changeset
   629
	local user = event.user;
3631
9639c493f4b9 mod_cloud_notify: fix typo (used || instead of or)
tmolitor <thilo@eightysoft.de>
parents: 3630
diff changeset
   630
	local push_services = event.push_services or push_store:get(user);
3947
f5e6368a1c39 mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents: 3944
diff changeset
   631
	module:log("debug", "Handling event 'cloud-notify-ping' for user '%s'", user);
f5e6368a1c39 mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents: 3944
diff changeset
   632
	local retval = handle_notify_request(nil, user, push_services, true);
f5e6368a1c39 mod_cloud_notify: Cleanup code and drop support for prosody 0.9
tmolitor <thilo@eightysoft.de>
parents: 3944
diff changeset
   633
	module:log("debug", "handle_notify_request() returned %s", tostring(retval));
2613
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   634
end
6ab46ff685d0 mod_cloud_notify: Respect Daniel's business rules and remove endpoints on error
tmolitor <thilo@eightysoft.de>
parents: 2399
diff changeset
   635
-- 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
   636
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
   637
2629
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
   638
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
   639
function module.unload()
4277
8bf83e883593 mod_cloud_notify: Modernize interface to mod_smacks
tmolitor <thilo@eightysoft.de>
parents: 4225
diff changeset
   640
	module:log("info", "Unloading module");
8bf83e883593 mod_cloud_notify: Modernize interface to mod_smacks
tmolitor <thilo@eightysoft.de>
parents: 4225
diff changeset
   641
	-- cleanup some settings, reloading this module can cause process_smacks_stanza() to stop working otherwise
8bf83e883593 mod_cloud_notify: Modernize interface to mod_smacks
tmolitor <thilo@eightysoft.de>
parents: 4225
diff changeset
   642
	for user, _ in pairs(host_sessions) do
4299
d44a8d3dd571 mod_cloud_notify: Some code cleanup, now luacheck-clean. No functionality changes.
Matthew Wild <mwild1@gmail.com>
parents: 4277
diff changeset
   643
		for _, session in pairs(host_sessions[user].sessions) do
4277
8bf83e883593 mod_cloud_notify: Modernize interface to mod_smacks
tmolitor <thilo@eightysoft.de>
parents: 4225
diff changeset
   644
			if session.awaiting_push_timer then session.awaiting_push_timer:stop(); end
8bf83e883593 mod_cloud_notify: Modernize interface to mod_smacks
tmolitor <thilo@eightysoft.de>
parents: 4225
diff changeset
   645
			session.awaiting_push_timer = nil;
4972
487f1eb829cf mod_cloud_notify: Compat for prosody 0.12
tmolitor <thilo@eightysoft.de>
parents: 4831
diff changeset
   646
			session.push_queue = nil;
4277
8bf83e883593 mod_cloud_notify: Modernize interface to mod_smacks
tmolitor <thilo@eightysoft.de>
parents: 4225
diff changeset
   647
			session.first_hibernated_push = nil;
4972
487f1eb829cf mod_cloud_notify: Compat for prosody 0.12
tmolitor <thilo@eightysoft.de>
parents: 4831
diff changeset
   648
			-- check for prosody 0.12 mod_smacks
487f1eb829cf mod_cloud_notify: Compat for prosody 0.12
tmolitor <thilo@eightysoft.de>
parents: 4831
diff changeset
   649
			if session.hibernating_watchdog and session.original_smacks_callback and session.original_smacks_timeout then
487f1eb829cf mod_cloud_notify: Compat for prosody 0.12
tmolitor <thilo@eightysoft.de>
parents: 4831
diff changeset
   650
				-- restore old smacks watchdog
487f1eb829cf mod_cloud_notify: Compat for prosody 0.12
tmolitor <thilo@eightysoft.de>
parents: 4831
diff changeset
   651
				session.hibernating_watchdog:cancel();
487f1eb829cf mod_cloud_notify: Compat for prosody 0.12
tmolitor <thilo@eightysoft.de>
parents: 4831
diff changeset
   652
				session.hibernating_watchdog = watchdog.new(session.original_smacks_timeout, session.original_smacks_callback);
487f1eb829cf mod_cloud_notify: Compat for prosody 0.12
tmolitor <thilo@eightysoft.de>
parents: 4831
diff changeset
   653
			end
4277
8bf83e883593 mod_cloud_notify: Modernize interface to mod_smacks
tmolitor <thilo@eightysoft.de>
parents: 4225
diff changeset
   654
		end
8bf83e883593 mod_cloud_notify: Modernize interface to mod_smacks
tmolitor <thilo@eightysoft.de>
parents: 4225
diff changeset
   655
	end
2629
8c6562f16496 mod_cloud_notify: Fixed error in push deduplication
tmolitor <thilo@eightysoft.de>
parents: 2613
diff changeset
   656
	module:log("info", "Module unloaded");
2647
777d07e0cd73 mod_cloud_notify: Whitespace fixes
Matthew Wild <mwild1@gmail.com>
parents: 2646
diff changeset
   657
end