view mod_auto_accept_subscriptions/mod_auto_accept_subscriptions.lua @ 4326:f6fdefc5c6ac

mod_roster_command: Fix subscription when the "user JID" is a bare domain. Do not attempt to update the roster when the user is bare domain (e.g. a component), since they don't have rosters and the attempt results in an error: $ prosodyctl mod_roster_command subscribe proxy.example.com contact@example.com xxxxxxxxxxFailed to execute command: Error: /usr/lib/prosody/core/rostermanager.lua:104: attempt to concatenate local 'username' (a nil value) stack traceback: /usr/lib/prosody/core/rostermanager.lua:104: in function 'load_roster' /usr/lib/prosody/core/rostermanager.lua:305: in function 'set_contact_pending_out' mod_roster_command.lua:44: in function 'subscribe'
author Boris Grozev <boris@jitsi.org>
date Tue, 05 Jan 2021 13:15:00 -0600
parents 6d2ec330fbcf
children
line wrap: on
line source

local rostermanager = require "core.rostermanager";
local jid = require "util.jid";
local st = require "util.stanza";
local core_post_stanza = prosody.core_post_stanza;

local function handle_inbound_subscription_request(origin, stanza)
	local to_bare, from_bare = jid.bare(stanza.attr.to), jid.bare(stanza.attr.from);
	local node, host = jid.split(to_bare);
	stanza.attr.from, stanza.attr.to = from_bare, to_bare;
	module:log("info", "Auto-accepting inbound subscription request from %s to %s", tostring(from_bare), tostring(to_bare));

	if not rostermanager.is_contact_subscribed(node, host, from_bare) then
		core_post_stanza(hosts[host], st.presence({from=to_bare, to=from_bare, type="unavailable"}), true); -- acknowledging receipt
		module:log("debug", "receipt acknowledged");
		if rostermanager.set_contact_pending_in(node, host, from_bare) then
			module:log("debug", "set pending in");
			if rostermanager.subscribed(node, host, from_bare) then
				module:log("debug", "set subscribed");
				rostermanager.roster_push(node, host, to_bare);
				module:log("debug", "pushed roster item");
				local subscribed_stanza = st.reply(stanza);
				subscribed_stanza.attr.type = "subscribed";
				core_post_stanza(hosts[host], subscribed_stanza);
				module:log("debug", "sent subscribed");
				hosts[host].modules.presence.send_presence_of_available_resources(node, host, to_bare, origin);
				module:log("debug", "sent available presence of all resources");
				-- Add return subscription from user to contact
				local subscribe_stanza = st.reply(stanza);
				subscribe_stanza.attr.type = "subscribe";
				if rostermanager.set_contact_pending_out(node, host, from_bare) then
					rostermanager.roster_push(node, host, from_bare);
				end
				core_post_stanza(hosts[host], subscribe_stanza);
				return true;
			end
		end
	end
	module:log("warn", "Failed to auto-accept subscription request from %s to %s", tostring(from_bare), tostring(to_bare));
end

module:hook("presence/bare", function (event)
	local stanza = event.stanza;
	if stanza.attr.type == "subscribe" then
		handle_inbound_subscription_request(event.origin, stanza);
		return true;
	end
end, 0.1);