File

mod_http_prebind/mod_http_prebind.lua @ 6302:06fbbd45ba75

mod_cloud_notify: Readme: fix links and labels that were removed in the last commit diff --git a/mod_cloud_notify/README.md b/mod_cloud_notify/README.md --- a/mod_cloud_notify/README.md +++ b/mod_cloud_notify/README.md @@ -1,3 +1,9 @@ +---- +-labels: +-- 'Stage-Beta' +-summary: 'XEP-0357: Cloud push notifications' +---- + # Introduction This module enables support for sending "push notifications" to clients @@ -32,15 +38,15 @@ notification to your device. When your d it will display it or wake up the app so it can connect to XMPP and receive any pending messages. -This protocol is described for developers in \[XEP-0357: Push -Notifications\]. +This protocol is described for developers in [XEP-0357: Push +Notifications]. -For this module to work reliably, you must have \[mod_smacks\], -\[mod_mam\] and \[mod_carbons\] also enabled on your server. +For this module to work reliably, you must have [mod_smacks], +[mod_mam] and [mod_carbons] also enabled on your server. Some clients, notably Siskin and Snikket iOS need some additional extensions that are not currently defined in a standard XEP. To support -these clients, see \[mod_cloud_notify_extensions\]. +these clients, see [mod_cloud_notify_extensions]. # Configuration @@ -58,18 +64,18 @@ these clients, see \[mod_cloud_notify_ex # Internal design notes App servers are notified about offline messages, messages stored by -\[mod_mam\] or messages waiting in the smacks queue. The business rules +[mod_mam] or messages waiting in the smacks queue. The business rules outlined [here](//mail.jabber.org/pipermail/standards/2016-February/030925.html) are all honored[^2]. -To cooperate with \[mod_smacks\] this module consumes some events: +To cooperate with [mod_smacks] this module consumes some events: `smacks-ack-delayed`, `smacks-hibernation-start` and `smacks-hibernation-end`. These events allow this module to send out notifications for messages received while the session is hibernated by -\[mod_smacks\] or even when smacks acknowledgements for messages are +[mod_smacks] or even when smacks acknowledgements for messages are delayed by a certain amount of seconds configurable with the -\[mod_smacks\] setting `smacks_max_ack_delay`. +[mod_smacks] setting `smacks_max_ack_delay`. The `smacks_max_ack_delay` setting allows to send out notifications to clients which aren't already in smacks hibernation state (because the
author Menel <menel@snikket.de>
date Fri, 13 Jun 2025 10:44:37 +0200
parent 4230:495a23d61418
line wrap: on
line source

module:depends("http");

local http = require "net.http";
local format = require "util.format".format;
local json_encode = require "util.json".encode;
local promise = require "util.promise";
local xml = require "util.xml";
local t_insert = table.insert;

local function new_options(host)
	return {
		headers = {
			["Content-Type"] = "text/xml; charset=utf-8",
			["Host"] = host,
		},
		method = "POST",
	};
end

local function connect_to_bosh(url, hostname)
	local rid = math.random(100000, 100000000)
	local options = new_options(hostname);
	options.body = format([[<body content='text/xml; charset=utf-8'
	      hold='1'
	      rid='%d'
	      to='%s'
	      wait='60'
	      xml:lang='en'
	      xmpp:version='1.0'
	      xmlns='http://jabber.org/protocol/httpbind'
	      xmlns:xmpp='urn:xmpp:xbosh'/>]], rid, hostname);
	local rid = rid + 1;
	return promise.new(function (on_fulfilled, on_error)
		assert(http.request(url, options, function (body, code)
			if code ~= 200 then
				on_error("Failed to fetch, HTTP error code "..code);
				return;
			end
			local body = xml.parse(body);
			local sid = body.attr.sid;
			local mechanisms = {};
			for mechanism in body:get_child("features", "http://etherx.jabber.org/streams")
				:get_child("mechanisms", "urn:ietf:params:xml:ns:xmpp-sasl")
					:childtags("mechanism", "urn:ietf:params:xml:ns:xmpp-sasl") do
				mechanisms[mechanism:get_text()] = true;
			end
			on_fulfilled({ url = url, sid = sid, rid = rid, mechanisms = mechanisms });
		end));
	end);
end

local function authenticate(data)
	local options = new_options();
	options.body = format([[<body sid='%s'
	      rid='%d'
	      xmlns='http://jabber.org/protocol/httpbind'>
		<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl'
		      mechanism='ANONYMOUS'/>
	</body>]], data.sid, data.rid);
	data.rid = data.rid + 1;
	return promise.new(function (on_fulfilled, on_error)
		if data.mechanisms["ANONYMOUS"] == nil then
			on_error("No SASL ANONYMOUS mechanism supported on this host.");
			return;
		end
		assert(http.request(data.url, options, function (body, code)
			if code ~= 200 then
				on_error("Failed to fetch, HTTP error code "..code);
				return;
			end
			local body = xml.parse(body);
			local success = body:get_child("success", "urn:ietf:params:xml:ns:xmpp-sasl");
			if success then
				data.mechanisms = nil;
				on_fulfilled(data);
			else
				on_error("Authentication failed.");
			end
		end));
	end);
end;

local function restart_stream(data)
	local options = new_options();
	options.body = format([[
	<body sid='%s'
	      rid='%d'
	      xml:lang='en'
	      xmlns='http://jabber.org/protocol/httpbind'
	      xmlns:xmpp='urn:xmpp:xbosh'
	      xmpp:restart='true'/>]], data.sid, data.rid);
	data.rid = data.rid + 1;
	return promise.new(function (on_fulfilled, on_error)
		assert(http.request(data.url, options, function (body, code)
			if code ~= 200 then
				on_error("Failed to fetch, HTTP error code "..code);
				return;
			end
			local body = xml.parse(body);
			on_fulfilled(data);
		end));
	end);
end;

local function bind(data)
	local options = new_options();
	options.body = format([[
	<body sid='%s'
	      rid='%d'
	      xmlns='http://jabber.org/protocol/httpbind'>
		<iq xmlns='jabber:client'
		    type='set'>
			<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'/>
		</iq>
	</body>]], data.sid, data.rid);
	data.rid = data.rid + 1;
	return promise.new(function (on_fulfilled, on_error)
		assert(http.request(data.url, options, function (body, code)
			if code ~= 200 then
				on_error("Failed to fetch, HTTP error code "..code);
				return;
			end
			local body = xml.parse(body);
			local jid = body:get_child("iq", "jabber:client")
				:get_child("bind", "urn:ietf:params:xml:ns:xmpp-bind")
					:get_child_text("jid", "urn:ietf:params:xml:ns:xmpp-bind");
			on_fulfilled(json_encode({rid = data.rid, sid = data.sid, jid = jid}));
		end));
	end);
end;

module:provides("http", {
	route = {
		["GET"] = function (event)
			return connect_to_bosh("http://[::1]:5280/http-bind", module.host)
				:next(authenticate)
				:next(restart_stream)
				:next(bind);
		end;
	};
});