mod_auto_accept_subscriptions/mod_auto_accept_subscriptions.lua
changeset 601 b1d82ae063e1
child 776 841b5eb5fe52
equal deleted inserted replaced
600:7693724881b3 601:b1d82ae063e1
       
     1 local rostermanager = require "core.rostermanager";
       
     2 local jid = require "util.jid";
       
     3 local st = require "util.stanza";
       
     4 
       
     5 local function handle_inbound_subscription_request(origin, stanza)
       
     6 	local to_bare, from_bare = jid.bare(stanza.attr.to), jid.bare(stanza.attr.from);
       
     7 	local node, host = jid.split(to_bare);
       
     8 	stanza.attr.from, stanza.attr.to = from_bare, to_bare;
       
     9 	module:log("info", "Auto-accepting inbound subscription request from %s to %s", from_bare, to_bare);
       
    10 
       
    11 	if not rostermanager.is_contact_subscribed(node, host, from_bare) then
       
    12 		core_post_stanza(hosts[host], st.presence({from=to_bare, to=from_bare, type="unavailable"}), true); -- acknowledging receipt
       
    13 		module:log("debug", "receipt acknowledged");
       
    14 		if rostermanager.set_contact_pending_in(node, host, from_bare) then
       
    15 			module:log("debug", "set pending in");
       
    16 			if rostermanager.subscribed(node, host, from_bare) then
       
    17 				module:log("debug", "set subscribed");
       
    18 				rostermanager.roster_push(node, host, to_bare);
       
    19 				module:log("debug", "pushed roster item");
       
    20 				local subscribed_stanza = st.reply(stanza);
       
    21 				subscribed_stanza.attr.type = "subscribed";
       
    22 				core_post_stanza(hosts[host], subscribed_stanza);
       
    23 				module:log("debug", "sent subscribed");
       
    24 				hosts[host].modules.presence.send_presence_of_available_resources(node, host, to_bare, origin);
       
    25 				module:log("debug", "sent available presence of all resources");
       
    26 				-- Add return subscription from user to contact
       
    27 				local subscribe_stanza = st.reply(stanza);
       
    28 				subscribed_stanza.attr.type = "subscribe";
       
    29 				if rostermanager.set_contact_pending_out(node, host, from_bare) then
       
    30 					rostermanager.roster_push(node, host, from_bare);
       
    31 				end
       
    32 				core_post_stanza(hosts[host], subscribe_stanza);
       
    33 				return true;
       
    34 			end
       
    35 		end
       
    36 	end	
       
    37 	module:log("warn", "Failed to auto-accept subscription request from %s to %s", from_bare, to_bare);
       
    38 end
       
    39 
       
    40 module:hook("presence/bare", function (event)
       
    41 	local stanza = event.stanza;
       
    42 	if stanza.attr.type == "subscribe" then
       
    43 		handle_inbound_subscription_request(event.origin, stanza);
       
    44 		return true;
       
    45 	end
       
    46 end, 0.1);