util/discohelper.lua
author Matthew Wild <mwild1@gmail.com>
Wed, 03 Dec 2008 14:39:07 +0000
changeset 519 cccd610a0ef9
parent 387 700e95c00c5b
child 539 cbcadb1a6166
permissions -rw-r--r--
Insert copyright/license headers
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 387
diff changeset
     1
-- Prosody IM v0.1
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 387
diff changeset
     2
-- Copyright (C) 2008 Matthew Wild
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 387
diff changeset
     3
-- Copyright (C) 2008 Waqas Hussain
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 387
diff changeset
     4
-- 
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 387
diff changeset
     5
-- This program is free software; you can redistribute it and/or
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 387
diff changeset
     6
-- modify it under the terms of the GNU General Public License
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 387
diff changeset
     7
-- as published by the Free Software Foundation; either version 2
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 387
diff changeset
     8
-- of the License, or (at your option) any later version.
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 387
diff changeset
     9
-- 
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 387
diff changeset
    10
-- This program is distributed in the hope that it will be useful,
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 387
diff changeset
    11
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 387
diff changeset
    12
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 387
diff changeset
    13
-- GNU General Public License for more details.
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 387
diff changeset
    14
-- 
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 387
diff changeset
    15
-- You should have received a copy of the GNU General Public License
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 387
diff changeset
    16
-- along with this program; if not, write to the Free Software
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 387
diff changeset
    17
-- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 387
diff changeset
    18
--
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 387
diff changeset
    19
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 387
diff changeset
    20
387
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    21
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    22
local t_insert = table.insert;
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    23
local jid_split = require "util.jid".split;
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    24
local ipairs = ipairs;
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    25
local st = require "util.stanza";
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    26
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    27
module "discohelper";
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    28
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    29
local function addDiscoItemsHandler(self, jid, func)
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    30
	if self.item_handlers[jid] then
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    31
		t_insert(self.item_handlers[jid], func);
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    32
	else
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    33
		self.item_handlers[jid] = {func};
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    34
	end
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    35
end
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    36
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    37
local function addDiscoInfoHandler(self, jid, func)
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    38
	if self.info_handlers[jid] then
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    39
		t_insert(self.info_handlers[jid], func);
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    40
	else
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    41
		self.info_handlers[jid] = {func};
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    42
	end
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    43
end
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    44
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    45
local function handle(self, stanza)
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    46
	if stanza.name == "iq" and stanza.tags[1].name == "query" then
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    47
		local query = stanza.tags[1];
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    48
		local to = stanza.attr.to;
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    49
		local from = stanza.attr.from
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    50
		local node = query.attr.node or "";
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    51
		local to_node, to_host = jid_split(to);
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    52
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    53
		local reply = st.reply(stanza):query(query.attr.xmlns);
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    54
		local handlers;
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    55
		if query.attr.xmlns == "http://jabber.org/protocol/disco#info" then -- select handler set
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    56
			handlers = self.info_handlers;
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    57
		elseif query.attr.xmlns == "http://jabber.org/protocol/disco#items" then
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    58
			handlers = self.item_handlers;
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    59
		end
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    60
		local handler = handlers[to]; -- get the handler
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    61
		if not handler then -- if not found then use default handler
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    62
			if to_node then
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    63
				handler = handlers["*defaultnode"];
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    64
			else
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    65
				handler = handlers["*defaulthost"];
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    66
			end
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    67
		end
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    68
		local found; -- to keep track of any handlers found
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    69
		if handler then
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    70
			for _, h in ipairs(handler) do
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    71
				if h(reply, to, from, node) then found = true; end
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    72
			end
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    73
		end
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    74
		if to_node then -- handlers which get called always
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    75
			handler = handlers["*node"];
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    76
		else
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    77
			handler = handlers["*host"];
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    78
		end
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    79
		if handler then -- call always called handler
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    80
			for _, h in ipairs(handler) do
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    81
				if h(reply, to, from, node) then found = true; end
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    82
			end
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    83
		end
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    84
		if found then return reply; end -- return the reply if there was one
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    85
		return st.error_reply(stanza, "cancel", "service-unavailable");
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    86
	end
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    87
end
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    88
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    89
function new()
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    90
	return {
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    91
		item_handlers = {};
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    92
		info_handlers = {};
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    93
		addDiscoItemsHandler = addDiscoItemsHandler;
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    94
		addDiscoInfoHandler = addDiscoInfoHandler;
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    95
		handle = handle;
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    96
	};
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    97
end
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    98
700e95c00c5b Added discohelper
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    99
return _M;