plugins/adhoc/mod_adhoc.lua
author Matthew Wild <mwild1@gmail.com>
Fri, 11 Jun 2010 13:23:10 +0100
changeset 3231 ad3fbed1dda5
parent 3220 b3772f9bc359
child 3286 e5234625fc42
permissions -rw-r--r--
mod_adhoc: Scan through list of items on load, in case items have been added before we were loaded
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
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     2
-- 
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     3
-- 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
     4
-- 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
     5
--
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
local st = require "util.stanza";
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     8
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
     9
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
    10
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
    11
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
    12
local commands = {};
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    13
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    14
module:add_feature(xmlns_cmd);
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    15
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    16
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
    17
	local origin, stanza = event.origin, event.stanza;
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    18
	-- TODO: Is this correct, or should is_admin be changed?
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    19
	local privileged = is_admin(stanza.attr.from)
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    20
	    or is_admin(stanza.attr.from, stanza.attr.to); 
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    21
	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
    22
	    and stanza.tags[1].attr.node == xmlns_cmd then
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    23
		reply = st.reply(stanza);
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    24
		reply:tag("query", { xmlns = xmlns_disco.."#items",
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    25
		    node = xmlns_cmd });
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    26
		for node, command in pairs(commands) do
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    27
			if (command.permission == "admin" and privileged)
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    28
			    or (command.permission == "user") then
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    29
				reply:tag("item", { name = command.name,
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    30
				    node = node, jid = module:get_host() });
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    31
				reply:up();
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    32
			end
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    33
		end
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    34
		origin.send(reply);
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    35
		return true;
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    36
	end
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    37
end, 500);
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    38
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    39
module:hook("iq/host", function (event)
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    40
	local origin, stanza = event.origin, event.stanza;
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    41
	if stanza.attr.type == "set" and stanza.tags[1]
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    42
	    and stanza.tags[1].name == "command" then 
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    43
		local node = stanza.tags[1].attr.node
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    44
		-- TODO: Is this correct, or should is_admin be changed?
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    45
		local privileged = is_admin(event.stanza.attr.from)
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    46
		    or is_admin(stanza.attr.from, stanza.attr.to);
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    47
		if commands[node] then
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    48
			if commands[node].permission == "admin"
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    49
			    and not privileged then
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    50
				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
    51
				    :add_child(commands[node]:cmdtag("canceled")
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    52
					: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
    53
				return true
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    54
			end
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    55
			-- User has permission now execute the command
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    56
			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
    57
		end
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    58
	end
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    59
end, 500);
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    60
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
    61
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
    62
	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
    63
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
    64
3220
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    65
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
    66
	return handle_item_added(event.item);
3220
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    67
end, 500);
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    68
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    69
module:hook("item-removed/adhoc", function (event)
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    70
	commands[event.item.node] = nil;
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    71
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
    72
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
    73
-- 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
    74
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
    75
	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
    76
end