97 lines
3.9 KiB
Lua
97 lines
3.9 KiB
Lua
local usermanager = require "core.usermanager";
|
|
local rostermanager = require "core.rostermanager";
|
|
local storagemanager = require "core.storagemanager";
|
|
local jid = require "util.jid";
|
|
local st = require "util.stanza";
|
|
local core_post_stanza = prosody.core_post_stanza;
|
|
|
|
-- Unsubscribes user from contact (not contact from user, if subscribed).
|
|
function unsubscribe(user_jid, contact_jid)
|
|
local user_username, user_host = jid.split(user_jid);
|
|
local contact_username, contact_host = jid.split(contact_jid);
|
|
if not hosts[user_host] then
|
|
warn("The host '%s' is not configured for this server.", user_host);
|
|
return;
|
|
end
|
|
if hosts[user_host].users.name == "null" then
|
|
storagemanager.initialize_host(user_host);
|
|
usermanager.initialize_host(user_host);
|
|
end
|
|
-- Update user's roster to say subscription is cancelled...
|
|
rostermanager.unsubscribe(user_username, user_host, contact_jid);
|
|
if hosts[contact_host] then
|
|
if contact_host ~= user_host and hosts[contact_host].users.name == "null" then
|
|
storagemanager.initialize_host(contact_host);
|
|
usermanager.initialize_host(contact_host);
|
|
end
|
|
-- Update contact's roster to say subscription is cancelled...
|
|
rostermanager.unsubscribed(contact_username, contact_host, user_jid);
|
|
end
|
|
end
|
|
|
|
|
|
-- Cancel any subscription in either direction.
|
|
function unsubscribe_both(jid1, jid2)
|
|
unsubscribe(jid1, jid2);
|
|
unsubscribe(jid2, jid1);
|
|
end
|
|
|
|
|
|
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));
|
|
|
|
local do_process = host or nil;
|
|
if host then
|
|
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("info", "receipt acknowledged");
|
|
if rostermanager.set_contact_pending_in(node, host, from_bare) then
|
|
-- module:log("info", "set pending in");
|
|
if rostermanager.subscribed(node, host, from_bare) then
|
|
-- module:log("info", "set subscribed");
|
|
rostermanager.roster_push(node, host, to_bare);
|
|
-- module:log("info", "pushed roster item");
|
|
local subscribed_stanza = st.reply(stanza);
|
|
subscribed_stanza.attr.type = "subscribed";
|
|
core_post_stanza(hosts[host], subscribed_stanza);
|
|
-- module:log("info", "sent subscribed");
|
|
hosts[host].modules.presence.send_presence_of_available_resources(node, host, to_bare, origin);
|
|
-- module:log("info", "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
|
|
else
|
|
module:log("warn", "Failed to auto-accept subscription request from %s to %s", tostring(from_bare), tostring(to_bare));
|
|
return true;
|
|
end
|
|
end
|
|
|
|
local function handle_inbound_unsubscription_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-unsubscribing subscription request from %s to %s", tostring(from_bare), tostring(to_bare));
|
|
unsubscribe_both(from_bare, 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;
|
|
elseif stanza.attr.type == "unsubscribed" then
|
|
handle_inbound_unsubscription_request(event.origin, stanza);
|
|
end
|
|
end, 0.1);
|