plugins/adhoc/mod_adhoc.lua
author Florian Zeitz <florob@babelmonkeys.de>
Sun, 22 Aug 2010 20:53:40 +0200
changeset 3457 24d2c9be0149
parent 3456 1201a743fe63
child 3485 3eee60c6a436
permissions -rw-r--r--
mod_adhoc: Answer disco#info (This is a MUST in XEP-0050)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3220
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     1
-- Copyright (C) 2009 Thilo Cestonaro
3456
1201a743fe63 mod_adhoc: Code restructuring
Florian Zeitz <florob@babelmonkeys.de>
parents: 3286
diff changeset
     2
-- Copyright (C) 2009-2010 Florian Zeitz
1201a743fe63 mod_adhoc: Code restructuring
Florian Zeitz <florob@babelmonkeys.de>
parents: 3286
diff changeset
     3
--
3220
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     4
-- This file is MIT/X11 licensed. Please see the
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     5
-- COPYING file in the source package for more information.
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     6
--
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     7
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     8
local st = require "util.stanza";
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     9
local is_admin = require "core.usermanager".is_admin;
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    10
local adhoc_handle_cmd = module:require "adhoc".handle_cmd;
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    11
local xmlns_cmd = "http://jabber.org/protocol/commands";
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    12
local xmlns_disco = "http://jabber.org/protocol/disco";
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    13
local commands = {};
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    14
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    15
module:add_feature(xmlns_cmd);
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    16
3457
24d2c9be0149 mod_adhoc: Answer disco#info (This is a MUST in XEP-0050)
Florian Zeitz <florob@babelmonkeys.de>
parents: 3456
diff changeset
    17
module:hook("iq/host/"..xmlns_disco.."#info:query", function (event)
24d2c9be0149 mod_adhoc: Answer disco#info (This is a MUST in XEP-0050)
Florian Zeitz <florob@babelmonkeys.de>
parents: 3456
diff changeset
    18
	local origin, stanza = event.origin, event.stanza;
24d2c9be0149 mod_adhoc: Answer disco#info (This is a MUST in XEP-0050)
Florian Zeitz <florob@babelmonkeys.de>
parents: 3456
diff changeset
    19
	local node = stanza.tags[1].attr.node;
24d2c9be0149 mod_adhoc: Answer disco#info (This is a MUST in XEP-0050)
Florian Zeitz <florob@babelmonkeys.de>
parents: 3456
diff changeset
    20
	if stanza.attr.type == "get" and node
24d2c9be0149 mod_adhoc: Answer disco#info (This is a MUST in XEP-0050)
Florian Zeitz <florob@babelmonkeys.de>
parents: 3456
diff changeset
    21
	    and commands[node] then
24d2c9be0149 mod_adhoc: Answer disco#info (This is a MUST in XEP-0050)
Florian Zeitz <florob@babelmonkeys.de>
parents: 3456
diff changeset
    22
		-- Required for Prosody <= 0.7
24d2c9be0149 mod_adhoc: Answer disco#info (This is a MUST in XEP-0050)
Florian Zeitz <florob@babelmonkeys.de>
parents: 3456
diff changeset
    23
		local privileged = is_admin(stanza.attr.from)
24d2c9be0149 mod_adhoc: Answer disco#info (This is a MUST in XEP-0050)
Florian Zeitz <florob@babelmonkeys.de>
parents: 3456
diff changeset
    24
		    or is_admin(stanza.attr.from, stanza.attr.to);
24d2c9be0149 mod_adhoc: Answer disco#info (This is a MUST in XEP-0050)
Florian Zeitz <florob@babelmonkeys.de>
parents: 3456
diff changeset
    25
		if (commands[node].permission == "admin" and privileged)
24d2c9be0149 mod_adhoc: Answer disco#info (This is a MUST in XEP-0050)
Florian Zeitz <florob@babelmonkeys.de>
parents: 3456
diff changeset
    26
		    or (commands[node].permission == "user") then
24d2c9be0149 mod_adhoc: Answer disco#info (This is a MUST in XEP-0050)
Florian Zeitz <florob@babelmonkeys.de>
parents: 3456
diff changeset
    27
			reply = st.reply(stanza);
24d2c9be0149 mod_adhoc: Answer disco#info (This is a MUST in XEP-0050)
Florian Zeitz <florob@babelmonkeys.de>
parents: 3456
diff changeset
    28
			reply:tag("query", { xmlns = xmlns_disco.."#info",
24d2c9be0149 mod_adhoc: Answer disco#info (This is a MUST in XEP-0050)
Florian Zeitz <florob@babelmonkeys.de>
parents: 3456
diff changeset
    29
			    node = node });
24d2c9be0149 mod_adhoc: Answer disco#info (This is a MUST in XEP-0050)
Florian Zeitz <florob@babelmonkeys.de>
parents: 3456
diff changeset
    30
			reply:tag("identity", { name = commands[node].name,
24d2c9be0149 mod_adhoc: Answer disco#info (This is a MUST in XEP-0050)
Florian Zeitz <florob@babelmonkeys.de>
parents: 3456
diff changeset
    31
			    category = "automation", type = "command-node" }):up();
24d2c9be0149 mod_adhoc: Answer disco#info (This is a MUST in XEP-0050)
Florian Zeitz <florob@babelmonkeys.de>
parents: 3456
diff changeset
    32
			reply:tag("feature", { var = xmlns_cmd }):up();
24d2c9be0149 mod_adhoc: Answer disco#info (This is a MUST in XEP-0050)
Florian Zeitz <florob@babelmonkeys.de>
parents: 3456
diff changeset
    33
			reply:tag("feature", { var = "jabber:x:data" }):up();
24d2c9be0149 mod_adhoc: Answer disco#info (This is a MUST in XEP-0050)
Florian Zeitz <florob@babelmonkeys.de>
parents: 3456
diff changeset
    34
		else
24d2c9be0149 mod_adhoc: Answer disco#info (This is a MUST in XEP-0050)
Florian Zeitz <florob@babelmonkeys.de>
parents: 3456
diff changeset
    35
			reply = st.error_reply(stanza, "auth", "forbidden", "This item is not available to you");
24d2c9be0149 mod_adhoc: Answer disco#info (This is a MUST in XEP-0050)
Florian Zeitz <florob@babelmonkeys.de>
parents: 3456
diff changeset
    36
		end
24d2c9be0149 mod_adhoc: Answer disco#info (This is a MUST in XEP-0050)
Florian Zeitz <florob@babelmonkeys.de>
parents: 3456
diff changeset
    37
		origin.send(reply);
24d2c9be0149 mod_adhoc: Answer disco#info (This is a MUST in XEP-0050)
Florian Zeitz <florob@babelmonkeys.de>
parents: 3456
diff changeset
    38
		return true;
24d2c9be0149 mod_adhoc: Answer disco#info (This is a MUST in XEP-0050)
Florian Zeitz <florob@babelmonkeys.de>
parents: 3456
diff changeset
    39
	end
24d2c9be0149 mod_adhoc: Answer disco#info (This is a MUST in XEP-0050)
Florian Zeitz <florob@babelmonkeys.de>
parents: 3456
diff changeset
    40
end);
24d2c9be0149 mod_adhoc: Answer disco#info (This is a MUST in XEP-0050)
Florian Zeitz <florob@babelmonkeys.de>
parents: 3456
diff changeset
    41
3220
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    42
module:hook("iq/host/"..xmlns_disco.."#items:query", function (event)
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    43
	local origin, stanza = event.origin, event.stanza;
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    44
	if stanza.attr.type == "get" and stanza.tags[1].attr.node
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    45
	    and stanza.tags[1].attr.node == xmlns_cmd then
3456
1201a743fe63 mod_adhoc: Code restructuring
Florian Zeitz <florob@babelmonkeys.de>
parents: 3286
diff changeset
    46
		local privileged = is_admin(stanza.attr.from, stanza.attr.to);
3220
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    47
		reply = st.reply(stanza);
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    48
		reply:tag("query", { xmlns = xmlns_disco.."#items",
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    49
		    node = xmlns_cmd });
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    50
		for node, command in pairs(commands) do
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    51
			if (command.permission == "admin" and privileged)
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    52
			    or (command.permission == "user") then
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    53
				reply:tag("item", { name = command.name,
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    54
				    node = node, jid = module:get_host() });
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    55
				reply:up();
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    56
			end
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    57
		end
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    58
		origin.send(reply);
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    59
		return true;
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    60
	end
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    61
end, 500);
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    62
3456
1201a743fe63 mod_adhoc: Code restructuring
Florian Zeitz <florob@babelmonkeys.de>
parents: 3286
diff changeset
    63
module:hook("iq/host/"..xmlns_cmd..":command", function (event)
3220
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    64
	local origin, stanza = event.origin, event.stanza;
3456
1201a743fe63 mod_adhoc: Code restructuring
Florian Zeitz <florob@babelmonkeys.de>
parents: 3286
diff changeset
    65
	if stanza.attr.type == "set" then
3220
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    66
		local node = stanza.tags[1].attr.node
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    67
		if commands[node] then
3456
1201a743fe63 mod_adhoc: Code restructuring
Florian Zeitz <florob@babelmonkeys.de>
parents: 3286
diff changeset
    68
			local privileged = is_admin(stanza.attr.from, stanza.attr.to);
3220
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    69
			if commands[node].permission == "admin"
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    70
			    and not privileged then
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    71
				origin.send(st.error_reply(stanza, "auth", "forbidden", "You don't have permission to execute this command"):up()
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    72
				    :add_child(commands[node]:cmdtag("canceled")
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    73
					:tag("note", {type="error"}):text("You don't have permission to execute this command")));
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    74
				return true
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    75
			end
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    76
			-- User has permission now execute the command
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    77
			return adhoc_handle_cmd(commands[node], origin, stanza);
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    78
		end
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    79
	end
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    80
end, 500);
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    81
3231
ad3fbed1dda5 mod_adhoc: Scan through list of items on load, in case items have been added before we were loaded
Matthew Wild <mwild1@gmail.com>
parents: 3220
diff changeset
    82
local function handle_item_added(item)
ad3fbed1dda5 mod_adhoc: Scan through list of items on load, in case items have been added before we were loaded
Matthew Wild <mwild1@gmail.com>
parents: 3220
diff changeset
    83
	commands[item.node] = item;
ad3fbed1dda5 mod_adhoc: Scan through list of items on load, in case items have been added before we were loaded
Matthew Wild <mwild1@gmail.com>
parents: 3220
diff changeset
    84
end
ad3fbed1dda5 mod_adhoc: Scan through list of items on load, in case items have been added before we were loaded
Matthew Wild <mwild1@gmail.com>
parents: 3220
diff changeset
    85
3220
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    86
module:hook("item-added/adhoc", function (event)
3231
ad3fbed1dda5 mod_adhoc: Scan through list of items on load, in case items have been added before we were loaded
Matthew Wild <mwild1@gmail.com>
parents: 3220
diff changeset
    87
	return handle_item_added(event.item);
3220
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    88
end, 500);
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    89
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    90
module:hook("item-removed/adhoc", function (event)
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    91
	commands[event.item.node] = nil;
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    92
end, 500);
3231
ad3fbed1dda5 mod_adhoc: Scan through list of items on load, in case items have been added before we were loaded
Matthew Wild <mwild1@gmail.com>
parents: 3220
diff changeset
    93
ad3fbed1dda5 mod_adhoc: Scan through list of items on load, in case items have been added before we were loaded
Matthew Wild <mwild1@gmail.com>
parents: 3220
diff changeset
    94
-- Pick up any items that are already added
ad3fbed1dda5 mod_adhoc: Scan through list of items on load, in case items have been added before we were loaded
Matthew Wild <mwild1@gmail.com>
parents: 3220
diff changeset
    95
for _, item in ipairs(module:get_host_items("adhoc")) do
ad3fbed1dda5 mod_adhoc: Scan through list of items on load, in case items have been added before we were loaded
Matthew Wild <mwild1@gmail.com>
parents: 3220
diff changeset
    96
	handle_item_added(item);
ad3fbed1dda5 mod_adhoc: Scan through list of items on load, in case items have been added before we were loaded
Matthew Wild <mwild1@gmail.com>
parents: 3220
diff changeset
    97
end