plugins/mod_disco.lua
author Matthew Wild <mwild1@gmail.com>
Fri, 09 Jul 2010 13:20:00 +0100
changeset 3343 c70c6d5bf270
parent 2925 692b3c6c5bd2
child 3347 99f56bed5228
permissions -rw-r--r--
mod_disco: Support for putting the server's caps hash in stream:features to allow the client to cache disco#info for the server instead of requesting it at each login.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1523
841d61be198f Remove version number from copyright headers
Matthew Wild <mwild1@gmail.com>
parents: 1510
diff changeset
     1
-- Prosody IM
2923
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 1704
diff changeset
     2
-- Copyright (C) 2008-2010 Matthew Wild
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 1704
diff changeset
     3
-- Copyright (C) 2008-2010 Waqas Hussain
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
     4
-- 
758
b1885732e979 GPL->MIT!
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
     5
-- This project is MIT/X11 licensed. Please see the
b1885732e979 GPL->MIT!
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
     6
-- COPYING file in the source package for more information.
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
     7
--
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
     8
1701
ceab61010f87 mod_disco: Handle disco#items queries using new APIs
Waqas Hussain <waqas20@gmail.com>
parents: 1700
diff changeset
     9
local componentmanager_get_children = require "core.componentmanager".get_children;
2383
29a30884aadd mod_disco: Handle and fire events for service discovery queries for bare account JIDs (thanks darkrain).
Waqas Hussain <waqas20@gmail.com>
parents: 1704
diff changeset
    10
local is_contact_subscribed = require "core.rostermanager".is_contact_subscribed;
29a30884aadd mod_disco: Handle and fire events for service discovery queries for bare account JIDs (thanks darkrain).
Waqas Hussain <waqas20@gmail.com>
parents: 1704
diff changeset
    11
local jid_split = require "util.jid".split;
29a30884aadd mod_disco: Handle and fire events for service discovery queries for bare account JIDs (thanks darkrain).
Waqas Hussain <waqas20@gmail.com>
parents: 1704
diff changeset
    12
local jid_bare = require "util.jid".bare;
1704
9b445d2427e2 mod_disco: Rearranged some lines, and added a FIXME comment
Waqas Hussain <waqas20@gmail.com>
parents: 1703
diff changeset
    13
local st = require "util.stanza"
3343
c70c6d5bf270 mod_disco: Support for putting the server's caps hash in stream:features to allow the client to cache disco#info for the server instead of requesting it at each login.
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
    14
local calculate_hash = require "util.caps".calculate_hash;
1510
83b2e07e9039 mod_disco: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
    15
2491
4be6810914eb mod_disco: Added option 'disco_items' to allow appending items to a host's disco#items result.
Waqas Hussain <waqas20@gmail.com>
parents: 2383
diff changeset
    16
local disco_items = module:get_option("disco_items") or {};
4be6810914eb mod_disco: Added option 'disco_items' to allow appending items to a host's disco#items result.
Waqas Hussain <waqas20@gmail.com>
parents: 2383
diff changeset
    17
do -- validate disco_items
4be6810914eb mod_disco: Added option 'disco_items' to allow appending items to a host's disco#items result.
Waqas Hussain <waqas20@gmail.com>
parents: 2383
diff changeset
    18
	for _, item in ipairs(disco_items) do
4be6810914eb mod_disco: Added option 'disco_items' to allow appending items to a host's disco#items result.
Waqas Hussain <waqas20@gmail.com>
parents: 2383
diff changeset
    19
		local err;
4be6810914eb mod_disco: Added option 'disco_items' to allow appending items to a host's disco#items result.
Waqas Hussain <waqas20@gmail.com>
parents: 2383
diff changeset
    20
		if type(item) ~= "table" then
4be6810914eb mod_disco: Added option 'disco_items' to allow appending items to a host's disco#items result.
Waqas Hussain <waqas20@gmail.com>
parents: 2383
diff changeset
    21
			err = "item is not a table";
4be6810914eb mod_disco: Added option 'disco_items' to allow appending items to a host's disco#items result.
Waqas Hussain <waqas20@gmail.com>
parents: 2383
diff changeset
    22
		elseif type(item[1]) ~= "string" then
4be6810914eb mod_disco: Added option 'disco_items' to allow appending items to a host's disco#items result.
Waqas Hussain <waqas20@gmail.com>
parents: 2383
diff changeset
    23
			err = "item jid is not a string";
4be6810914eb mod_disco: Added option 'disco_items' to allow appending items to a host's disco#items result.
Waqas Hussain <waqas20@gmail.com>
parents: 2383
diff changeset
    24
		elseif item[2] and type(item[2]) ~= "string" then
4be6810914eb mod_disco: Added option 'disco_items' to allow appending items to a host's disco#items result.
Waqas Hussain <waqas20@gmail.com>
parents: 2383
diff changeset
    25
			err = "item name is not a string";
4be6810914eb mod_disco: Added option 'disco_items' to allow appending items to a host's disco#items result.
Waqas Hussain <waqas20@gmail.com>
parents: 2383
diff changeset
    26
		end
4be6810914eb mod_disco: Added option 'disco_items' to allow appending items to a host's disco#items result.
Waqas Hussain <waqas20@gmail.com>
parents: 2383
diff changeset
    27
		if err then
4be6810914eb mod_disco: Added option 'disco_items' to allow appending items to a host's disco#items result.
Waqas Hussain <waqas20@gmail.com>
parents: 2383
diff changeset
    28
			module:log("error", "option disco_items is malformed: %s", err);
4be6810914eb mod_disco: Added option 'disco_items' to allow appending items to a host's disco#items result.
Waqas Hussain <waqas20@gmail.com>
parents: 2383
diff changeset
    29
			disco_items = {}; -- TODO clean up data instead of removing it?
4be6810914eb mod_disco: Added option 'disco_items' to allow appending items to a host's disco#items result.
Waqas Hussain <waqas20@gmail.com>
parents: 2383
diff changeset
    30
			break;
4be6810914eb mod_disco: Added option 'disco_items' to allow appending items to a host's disco#items result.
Waqas Hussain <waqas20@gmail.com>
parents: 2383
diff changeset
    31
		end
4be6810914eb mod_disco: Added option 'disco_items' to allow appending items to a host's disco#items result.
Waqas Hussain <waqas20@gmail.com>
parents: 2383
diff changeset
    32
	end
4be6810914eb mod_disco: Added option 'disco_items' to allow appending items to a host's disco#items result.
Waqas Hussain <waqas20@gmail.com>
parents: 2383
diff changeset
    33
end
4be6810914eb mod_disco: Added option 'disco_items' to allow appending items to a host's disco#items result.
Waqas Hussain <waqas20@gmail.com>
parents: 2383
diff changeset
    34
1704
9b445d2427e2 mod_disco: Rearranged some lines, and added a FIXME comment
Waqas Hussain <waqas20@gmail.com>
parents: 1703
diff changeset
    35
module:add_identity("server", "im", "Prosody"); -- FIXME should be in the non-existing mod_router
1510
83b2e07e9039 mod_disco: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
    36
module:add_feature("http://jabber.org/protocol/disco#info");
83b2e07e9039 mod_disco: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
    37
module:add_feature("http://jabber.org/protocol/disco#items");
83b2e07e9039 mod_disco: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
    38
3343
c70c6d5bf270 mod_disco: Support for putting the server's caps hash in stream:features to allow the client to cache disco#info for the server instead of requesting it at each login.
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
    39
-- Handle disco requests to the server
c70c6d5bf270 mod_disco: Support for putting the server's caps hash in stream:features to allow the client to cache disco#info for the server instead of requesting it at each login.
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
    40
c70c6d5bf270 mod_disco: Support for putting the server's caps hash in stream:features to allow the client to cache disco#info for the server instead of requesting it at each login.
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
    41
local function build_server_disco_info(stanza)
c70c6d5bf270 mod_disco: Support for putting the server's caps hash in stream:features to allow the client to cache disco#info for the server instead of requesting it at each login.
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
    42
	local done = {};
c70c6d5bf270 mod_disco: Support for putting the server's caps hash in stream:features to allow the client to cache disco#info for the server instead of requesting it at each login.
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
    43
	for _,identity in ipairs(module:get_host_items("identity")) do
c70c6d5bf270 mod_disco: Support for putting the server's caps hash in stream:features to allow the client to cache disco#info for the server instead of requesting it at each login.
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
    44
		local identity_s = identity.category.."\0"..identity.type;
c70c6d5bf270 mod_disco: Support for putting the server's caps hash in stream:features to allow the client to cache disco#info for the server instead of requesting it at each login.
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
    45
		if not done[identity_s] then
c70c6d5bf270 mod_disco: Support for putting the server's caps hash in stream:features to allow the client to cache disco#info for the server instead of requesting it at each login.
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
    46
			stanza:tag("identity", identity):up();
c70c6d5bf270 mod_disco: Support for putting the server's caps hash in stream:features to allow the client to cache disco#info for the server instead of requesting it at each login.
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
    47
			done[identity_s] = true;
c70c6d5bf270 mod_disco: Support for putting the server's caps hash in stream:features to allow the client to cache disco#info for the server instead of requesting it at each login.
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
    48
		end
c70c6d5bf270 mod_disco: Support for putting the server's caps hash in stream:features to allow the client to cache disco#info for the server instead of requesting it at each login.
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
    49
	end
c70c6d5bf270 mod_disco: Support for putting the server's caps hash in stream:features to allow the client to cache disco#info for the server instead of requesting it at each login.
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
    50
	for _,feature in ipairs(module:get_host_items("feature")) do
c70c6d5bf270 mod_disco: Support for putting the server's caps hash in stream:features to allow the client to cache disco#info for the server instead of requesting it at each login.
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
    51
		if not done[feature] then
c70c6d5bf270 mod_disco: Support for putting the server's caps hash in stream:features to allow the client to cache disco#info for the server instead of requesting it at each login.
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
    52
			stanza:tag("feature", {var=feature}):up();
c70c6d5bf270 mod_disco: Support for putting the server's caps hash in stream:features to allow the client to cache disco#info for the server instead of requesting it at each login.
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
    53
			done[feature] = true;
c70c6d5bf270 mod_disco: Support for putting the server's caps hash in stream:features to allow the client to cache disco#info for the server instead of requesting it at each login.
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
    54
		end
c70c6d5bf270 mod_disco: Support for putting the server's caps hash in stream:features to allow the client to cache disco#info for the server instead of requesting it at each login.
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
    55
	end
c70c6d5bf270 mod_disco: Support for putting the server's caps hash in stream:features to allow the client to cache disco#info for the server instead of requesting it at each login.
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
    56
end
1699
53d16a28ee00 mod_disco: Handle disco#info queries using new APIs
Waqas Hussain <waqas20@gmail.com>
parents: 1523
diff changeset
    57
module:hook("iq/host/http://jabber.org/protocol/disco#info:query", function(event)
53d16a28ee00 mod_disco: Handle disco#info queries using new APIs
Waqas Hussain <waqas20@gmail.com>
parents: 1523
diff changeset
    58
	local origin, stanza = event.origin, event.stanza;
53d16a28ee00 mod_disco: Handle disco#info queries using new APIs
Waqas Hussain <waqas20@gmail.com>
parents: 1523
diff changeset
    59
	if stanza.attr.type ~= "get" then return; end
53d16a28ee00 mod_disco: Handle disco#info queries using new APIs
Waqas Hussain <waqas20@gmail.com>
parents: 1523
diff changeset
    60
	local node = stanza.tags[1].attr.node;
53d16a28ee00 mod_disco: Handle disco#info queries using new APIs
Waqas Hussain <waqas20@gmail.com>
parents: 1523
diff changeset
    61
	if node and node ~= "" then return; end -- TODO fire event?
53d16a28ee00 mod_disco: Handle disco#info queries using new APIs
Waqas Hussain <waqas20@gmail.com>
parents: 1523
diff changeset
    62
	local reply = st.reply(stanza):query("http://jabber.org/protocol/disco#info");
3343
c70c6d5bf270 mod_disco: Support for putting the server's caps hash in stream:features to allow the client to cache disco#info for the server instead of requesting it at each login.
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
    63
	build_server_disco_info(reply);
1699
53d16a28ee00 mod_disco: Handle disco#info queries using new APIs
Waqas Hussain <waqas20@gmail.com>
parents: 1523
diff changeset
    64
	origin.send(reply);
53d16a28ee00 mod_disco: Handle disco#info queries using new APIs
Waqas Hussain <waqas20@gmail.com>
parents: 1523
diff changeset
    65
	return true;
53d16a28ee00 mod_disco: Handle disco#info queries using new APIs
Waqas Hussain <waqas20@gmail.com>
parents: 1523
diff changeset
    66
end);
1701
ceab61010f87 mod_disco: Handle disco#items queries using new APIs
Waqas Hussain <waqas20@gmail.com>
parents: 1700
diff changeset
    67
module:hook("iq/host/http://jabber.org/protocol/disco#items:query", function(event)
ceab61010f87 mod_disco: Handle disco#items queries using new APIs
Waqas Hussain <waqas20@gmail.com>
parents: 1700
diff changeset
    68
	local origin, stanza = event.origin, event.stanza;
ceab61010f87 mod_disco: Handle disco#items queries using new APIs
Waqas Hussain <waqas20@gmail.com>
parents: 1700
diff changeset
    69
	if stanza.attr.type ~= "get" then return; end
ceab61010f87 mod_disco: Handle disco#items queries using new APIs
Waqas Hussain <waqas20@gmail.com>
parents: 1700
diff changeset
    70
	local node = stanza.tags[1].attr.node;
ceab61010f87 mod_disco: Handle disco#items queries using new APIs
Waqas Hussain <waqas20@gmail.com>
parents: 1700
diff changeset
    71
	if node and node ~= "" then return; end -- TODO fire event?
ceab61010f87 mod_disco: Handle disco#items queries using new APIs
Waqas Hussain <waqas20@gmail.com>
parents: 1700
diff changeset
    72
ceab61010f87 mod_disco: Handle disco#items queries using new APIs
Waqas Hussain <waqas20@gmail.com>
parents: 1700
diff changeset
    73
	local reply = st.reply(stanza):query("http://jabber.org/protocol/disco#items");
ceab61010f87 mod_disco: Handle disco#items queries using new APIs
Waqas Hussain <waqas20@gmail.com>
parents: 1700
diff changeset
    74
	for jid in pairs(componentmanager_get_children(module.host)) do
ceab61010f87 mod_disco: Handle disco#items queries using new APIs
Waqas Hussain <waqas20@gmail.com>
parents: 1700
diff changeset
    75
		reply:tag("item", {jid = jid}):up();
ceab61010f87 mod_disco: Handle disco#items queries using new APIs
Waqas Hussain <waqas20@gmail.com>
parents: 1700
diff changeset
    76
	end
2491
4be6810914eb mod_disco: Added option 'disco_items' to allow appending items to a host's disco#items result.
Waqas Hussain <waqas20@gmail.com>
parents: 2383
diff changeset
    77
	for _, item in ipairs(disco_items) do
4be6810914eb mod_disco: Added option 'disco_items' to allow appending items to a host's disco#items result.
Waqas Hussain <waqas20@gmail.com>
parents: 2383
diff changeset
    78
		reply:tag("item", {jid=item[1], name=item[2]}):up();
4be6810914eb mod_disco: Added option 'disco_items' to allow appending items to a host's disco#items result.
Waqas Hussain <waqas20@gmail.com>
parents: 2383
diff changeset
    79
	end
1701
ceab61010f87 mod_disco: Handle disco#items queries using new APIs
Waqas Hussain <waqas20@gmail.com>
parents: 1700
diff changeset
    80
	origin.send(reply);
ceab61010f87 mod_disco: Handle disco#items queries using new APIs
Waqas Hussain <waqas20@gmail.com>
parents: 1700
diff changeset
    81
	return true;
ceab61010f87 mod_disco: Handle disco#items queries using new APIs
Waqas Hussain <waqas20@gmail.com>
parents: 1700
diff changeset
    82
end);
3343
c70c6d5bf270 mod_disco: Support for putting the server's caps hash in stream:features to allow the client to cache disco#info for the server instead of requesting it at each login.
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
    83
c70c6d5bf270 mod_disco: Support for putting the server's caps hash in stream:features to allow the client to cache disco#info for the server instead of requesting it at each login.
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
    84
-- Server caps hash calculation
c70c6d5bf270 mod_disco: Support for putting the server's caps hash in stream:features to allow the client to cache disco#info for the server instead of requesting it at each login.
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
    85
local caps_hash_feature;
c70c6d5bf270 mod_disco: Support for putting the server's caps hash in stream:features to allow the client to cache disco#info for the server instead of requesting it at each login.
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
    86
c70c6d5bf270 mod_disco: Support for putting the server's caps hash in stream:features to allow the client to cache disco#info for the server instead of requesting it at each login.
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
    87
local function recalculate_server_caps()
c70c6d5bf270 mod_disco: Support for putting the server's caps hash in stream:features to allow the client to cache disco#info for the server instead of requesting it at each login.
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
    88
	local caps_hash = calculate_hash(st.stanza());
c70c6d5bf270 mod_disco: Support for putting the server's caps hash in stream:features to allow the client to cache disco#info for the server instead of requesting it at each login.
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
    89
	caps_hash_feature = st.stanza("c", {
c70c6d5bf270 mod_disco: Support for putting the server's caps hash in stream:features to allow the client to cache disco#info for the server instead of requesting it at each login.
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
    90
		xmlns = "http://jabber.org/protocol/caps";
c70c6d5bf270 mod_disco: Support for putting the server's caps hash in stream:features to allow the client to cache disco#info for the server instead of requesting it at each login.
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
    91
		hash = "sha-1";
c70c6d5bf270 mod_disco: Support for putting the server's caps hash in stream:features to allow the client to cache disco#info for the server instead of requesting it at each login.
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
    92
		node = "http://prosody.im";
c70c6d5bf270 mod_disco: Support for putting the server's caps hash in stream:features to allow the client to cache disco#info for the server instead of requesting it at each login.
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
    93
		ver = caps_hash;
c70c6d5bf270 mod_disco: Support for putting the server's caps hash in stream:features to allow the client to cache disco#info for the server instead of requesting it at each login.
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
    94
	});
c70c6d5bf270 mod_disco: Support for putting the server's caps hash in stream:features to allow the client to cache disco#info for the server instead of requesting it at each login.
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
    95
end
c70c6d5bf270 mod_disco: Support for putting the server's caps hash in stream:features to allow the client to cache disco#info for the server instead of requesting it at each login.
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
    96
recalculate_server_caps();
c70c6d5bf270 mod_disco: Support for putting the server's caps hash in stream:features to allow the client to cache disco#info for the server instead of requesting it at each login.
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
    97
c70c6d5bf270 mod_disco: Support for putting the server's caps hash in stream:features to allow the client to cache disco#info for the server instead of requesting it at each login.
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
    98
module:hook("item-added/identity", recalculate_server_caps);
c70c6d5bf270 mod_disco: Support for putting the server's caps hash in stream:features to allow the client to cache disco#info for the server instead of requesting it at each login.
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
    99
module:hook("item-added/feature", recalculate_server_caps);
c70c6d5bf270 mod_disco: Support for putting the server's caps hash in stream:features to allow the client to cache disco#info for the server instead of requesting it at each login.
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
   100
c70c6d5bf270 mod_disco: Support for putting the server's caps hash in stream:features to allow the client to cache disco#info for the server instead of requesting it at each login.
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
   101
module:hook("stream-features", function (event)
c70c6d5bf270 mod_disco: Support for putting the server's caps hash in stream:features to allow the client to cache disco#info for the server instead of requesting it at each login.
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
   102
	if caps_hash_feature then
c70c6d5bf270 mod_disco: Support for putting the server's caps hash in stream:features to allow the client to cache disco#info for the server instead of requesting it at each login.
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
   103
		event.features:add_child(caps_hash_feature);
c70c6d5bf270 mod_disco: Support for putting the server's caps hash in stream:features to allow the client to cache disco#info for the server instead of requesting it at each login.
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
   104
	end
c70c6d5bf270 mod_disco: Support for putting the server's caps hash in stream:features to allow the client to cache disco#info for the server instead of requesting it at each login.
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
   105
end);
c70c6d5bf270 mod_disco: Support for putting the server's caps hash in stream:features to allow the client to cache disco#info for the server instead of requesting it at each login.
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
   106
c70c6d5bf270 mod_disco: Support for putting the server's caps hash in stream:features to allow the client to cache disco#info for the server instead of requesting it at each login.
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
   107
-- Handle disco requests to user accounts
2383
29a30884aadd mod_disco: Handle and fire events for service discovery queries for bare account JIDs (thanks darkrain).
Waqas Hussain <waqas20@gmail.com>
parents: 1704
diff changeset
   108
module:hook("iq/bare/http://jabber.org/protocol/disco#info:query", function(event)
29a30884aadd mod_disco: Handle and fire events for service discovery queries for bare account JIDs (thanks darkrain).
Waqas Hussain <waqas20@gmail.com>
parents: 1704
diff changeset
   109
	local origin, stanza = event.origin, event.stanza;
29a30884aadd mod_disco: Handle and fire events for service discovery queries for bare account JIDs (thanks darkrain).
Waqas Hussain <waqas20@gmail.com>
parents: 1704
diff changeset
   110
	if stanza.attr.type ~= "get" then return; end
29a30884aadd mod_disco: Handle and fire events for service discovery queries for bare account JIDs (thanks darkrain).
Waqas Hussain <waqas20@gmail.com>
parents: 1704
diff changeset
   111
	local node = stanza.tags[1].attr.node;
29a30884aadd mod_disco: Handle and fire events for service discovery queries for bare account JIDs (thanks darkrain).
Waqas Hussain <waqas20@gmail.com>
parents: 1704
diff changeset
   112
	if node and node ~= "" then return; end -- TODO fire event?
29a30884aadd mod_disco: Handle and fire events for service discovery queries for bare account JIDs (thanks darkrain).
Waqas Hussain <waqas20@gmail.com>
parents: 1704
diff changeset
   113
	local username = jid_split(stanza.attr.to) or origin.username;
29a30884aadd mod_disco: Handle and fire events for service discovery queries for bare account JIDs (thanks darkrain).
Waqas Hussain <waqas20@gmail.com>
parents: 1704
diff changeset
   114
	if not stanza.attr.to or is_contact_subscribed(username, module.host, jid_bare(stanza.attr.from)) then
29a30884aadd mod_disco: Handle and fire events for service discovery queries for bare account JIDs (thanks darkrain).
Waqas Hussain <waqas20@gmail.com>
parents: 1704
diff changeset
   115
		local reply = st.reply(stanza):tag('query', {xmlns='http://jabber.org/protocol/disco#info'});
29a30884aadd mod_disco: Handle and fire events for service discovery queries for bare account JIDs (thanks darkrain).
Waqas Hussain <waqas20@gmail.com>
parents: 1704
diff changeset
   116
		if not reply.attr.from then reply.attr.from = origin.username.."@"..origin.host; end -- COMPAT To satisfy Psi when querying own account
29a30884aadd mod_disco: Handle and fire events for service discovery queries for bare account JIDs (thanks darkrain).
Waqas Hussain <waqas20@gmail.com>
parents: 1704
diff changeset
   117
		module:fire_event("account-disco-info", { session = origin, stanza = reply });
29a30884aadd mod_disco: Handle and fire events for service discovery queries for bare account JIDs (thanks darkrain).
Waqas Hussain <waqas20@gmail.com>
parents: 1704
diff changeset
   118
		origin.send(reply);
29a30884aadd mod_disco: Handle and fire events for service discovery queries for bare account JIDs (thanks darkrain).
Waqas Hussain <waqas20@gmail.com>
parents: 1704
diff changeset
   119
		return true;
29a30884aadd mod_disco: Handle and fire events for service discovery queries for bare account JIDs (thanks darkrain).
Waqas Hussain <waqas20@gmail.com>
parents: 1704
diff changeset
   120
	end
29a30884aadd mod_disco: Handle and fire events for service discovery queries for bare account JIDs (thanks darkrain).
Waqas Hussain <waqas20@gmail.com>
parents: 1704
diff changeset
   121
end);
29a30884aadd mod_disco: Handle and fire events for service discovery queries for bare account JIDs (thanks darkrain).
Waqas Hussain <waqas20@gmail.com>
parents: 1704
diff changeset
   122
module:hook("iq/bare/http://jabber.org/protocol/disco#items:query", function(event)
29a30884aadd mod_disco: Handle and fire events for service discovery queries for bare account JIDs (thanks darkrain).
Waqas Hussain <waqas20@gmail.com>
parents: 1704
diff changeset
   123
	local origin, stanza = event.origin, event.stanza;
29a30884aadd mod_disco: Handle and fire events for service discovery queries for bare account JIDs (thanks darkrain).
Waqas Hussain <waqas20@gmail.com>
parents: 1704
diff changeset
   124
	if stanza.attr.type ~= "get" then return; end
29a30884aadd mod_disco: Handle and fire events for service discovery queries for bare account JIDs (thanks darkrain).
Waqas Hussain <waqas20@gmail.com>
parents: 1704
diff changeset
   125
	local node = stanza.tags[1].attr.node;
29a30884aadd mod_disco: Handle and fire events for service discovery queries for bare account JIDs (thanks darkrain).
Waqas Hussain <waqas20@gmail.com>
parents: 1704
diff changeset
   126
	if node and node ~= "" then return; end -- TODO fire event?
29a30884aadd mod_disco: Handle and fire events for service discovery queries for bare account JIDs (thanks darkrain).
Waqas Hussain <waqas20@gmail.com>
parents: 1704
diff changeset
   127
	local username = jid_split(stanza.attr.to) or origin.username;
29a30884aadd mod_disco: Handle and fire events for service discovery queries for bare account JIDs (thanks darkrain).
Waqas Hussain <waqas20@gmail.com>
parents: 1704
diff changeset
   128
	if not stanza.attr.to or is_contact_subscribed(username, module.host, jid_bare(stanza.attr.from)) then
29a30884aadd mod_disco: Handle and fire events for service discovery queries for bare account JIDs (thanks darkrain).
Waqas Hussain <waqas20@gmail.com>
parents: 1704
diff changeset
   129
		local reply = st.reply(stanza):tag('query', {xmlns='http://jabber.org/protocol/disco#items'});
29a30884aadd mod_disco: Handle and fire events for service discovery queries for bare account JIDs (thanks darkrain).
Waqas Hussain <waqas20@gmail.com>
parents: 1704
diff changeset
   130
		if not reply.attr.from then reply.attr.from = origin.username.."@"..origin.host; end -- COMPAT To satisfy Psi when querying own account
29a30884aadd mod_disco: Handle and fire events for service discovery queries for bare account JIDs (thanks darkrain).
Waqas Hussain <waqas20@gmail.com>
parents: 1704
diff changeset
   131
		module:fire_event("account-disco-items", { session = origin, stanza = reply });
29a30884aadd mod_disco: Handle and fire events for service discovery queries for bare account JIDs (thanks darkrain).
Waqas Hussain <waqas20@gmail.com>
parents: 1704
diff changeset
   132
		origin.send(reply);
29a30884aadd mod_disco: Handle and fire events for service discovery queries for bare account JIDs (thanks darkrain).
Waqas Hussain <waqas20@gmail.com>
parents: 1704
diff changeset
   133
		return true;
29a30884aadd mod_disco: Handle and fire events for service discovery queries for bare account JIDs (thanks darkrain).
Waqas Hussain <waqas20@gmail.com>
parents: 1704
diff changeset
   134
	end
29a30884aadd mod_disco: Handle and fire events for service discovery queries for bare account JIDs (thanks darkrain).
Waqas Hussain <waqas20@gmail.com>
parents: 1704
diff changeset
   135
end);