# HG changeset patch # User Florian Zeitz # Date 1255216816 -7200 # Node ID 58d326d86a9a14e4b0268a69ecb2d28400373972 # Parent 3c49411d4aa3e4c28aac8386171092ab1ed9ff39 mod_adhoc: add adhoc.lib.lua to ease implementing new commands (as a consequence mod_adhoc is a directory now) mod_adhoc_cmd_*: Convert to use adhoc.lib.lua diff -r 3c49411d4aa3 -r 58d326d86a9a mod_adhoc/adhoc/adhoc.lib.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mod_adhoc/adhoc/adhoc.lib.lua Sun Oct 11 01:20:16 2009 +0200 @@ -0,0 +1,19 @@ +local st = require "util.stanza"; + +local xmlns_cmd = "http://jabber.org/protocol/commands"; + +local _M = {}; + +function _cmdtag(desc, status, sessionid, action) + local cmd = st.stanza("command", { xmlns = xmlns_cmd, node = desc.node, status = status }); + if sessionid then cmd.attr.sessionid = sessionid; end + if action then cmd.attr.action = action; end + + return cmd; +end + +function _M.new(name, node, handler) + return { name = name, node = node, handler = handler, cmdtag = _cmdtag }; +end + +return _M; diff -r 3c49411d4aa3 -r 58d326d86a9a mod_adhoc/adhoc/mod_adhoc.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mod_adhoc/adhoc/mod_adhoc.lua Sun Oct 11 01:20:16 2009 +0200 @@ -0,0 +1,52 @@ +-- Copyright (C) 2009 Thilo Cestonaro +-- +-- This file is MIT/X11 licensed. Please see the +-- COPYING file in the source package for more information. +-- + +local st = require "util.stanza"; +local commands = {}; + +module:add_feature("http://jabber.org/protocol/commands"); + +module:hook("iq/host/http://jabber.org/protocol/disco#items:query", function (event) + local origin, stanza = event.origin, event.stanza; + if stanza.attr.type == "get" and stanza.tags[1].attr.node and stanza.tags[1].attr.node == "http://jabber.org/protocol/commands" then + reply = st.reply(stanza); + reply:tag("query", {xmlns="http://jabber.org/protocol/disco#items", node="http://jabber.org/protocol/commands"}) + for i = 1, #commands do + -- module:log("info", "adding command %s", commands[i].name); + reply:tag("item", {name=commands[i].name, node=commands[i].node, jid=module:get_host()}); + reply:up(); + end + origin.send(reply); + return true; + end +end, 500); + +module:hook("iq/host", function (event) + local origin, stanza = event.origin, event.stanza; + if stanza.attr.type == "set" and stanza.tags[1] and stanza.tags[1].name == "command" then + local node = stanza.tags[1].attr.node + for i = 1, #commands do + if commands[i].node == node then + return commands[i].handler(commands[i], origin, stanza); + end + end + end +end, 500); + +module:hook("item-added/adhoc", function (event) + commands[ # commands + 1] = event.item; +end, 500); + +local _G = _G; +local t_remove = _G.table.remove; +module:hook("item-removed/adhoc", function (event) + for i = 1, #commands do + if commands[i].node == event.item.node then + t_remove(commands, i); + break; + end + end +end, 500); diff -r 3c49411d4aa3 -r 58d326d86a9a mod_adhoc/mod_adhoc.lua --- a/mod_adhoc/mod_adhoc.lua Sat Oct 10 09:33:44 2009 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,52 +0,0 @@ --- Copyright (C) 2009 Thilo Cestonaro --- --- This file is MIT/X11 licensed. Please see the --- COPYING file in the source package for more information. --- - -local st = require "util.stanza"; -local commands = {}; - -module:add_feature("http://jabber.org/protocol/commands"); - -module:hook("iq/host/http://jabber.org/protocol/disco#items:query", function (event) - local origin, stanza = event.origin, event.stanza; - if stanza.attr.type == "get" and stanza.tags[1].attr.node and stanza.tags[1].attr.node == "http://jabber.org/protocol/commands" then - reply = st.reply(stanza); - reply:tag("query", {xmlns="http://jabber.org/protocol/disco#items", node="http://jabber.org/protocol/commands"}) - for i = 1, #commands do - -- module:log("info", "adding command %s", commands[i].name); - reply:tag("item", {name=commands[i].name, node=commands[i].node, jid=module:get_host()}); - reply:up(); - end - origin.send(reply); - return true; - end -end, 500); - -module:hook("iq/host", function (event) - local origin, stanza = event.origin, event.stanza; - if stanza.attr.type == "set" and stanza.tags[1] and stanza.tags[1].name == "command" then - local node = stanza.tags[1].attr.node - for i = 1, #commands do - if commands[i].node == node then - return commands[i].handler(commands[i], origin, stanza); - end - end - end -end, 500); - -module:hook("item-added/adhoc", function (event) - commands[ # commands + 1] = event.item; -end, 500); - -local _G = _G; -local t_remove = _G.table.remove; -module:hook("item-removed/adhoc", function (event) - for i = 1, #commands do - if commands[i].node == event.item.node then - t_remove(commands, i); - break; - end - end -end, 500); diff -r 3c49411d4aa3 -r 58d326d86a9a mod_adhoc_cmd_admin/mod_adhoc_cmd_admin.lua --- a/mod_adhoc_cmd_admin/mod_adhoc_cmd_admin.lua Sat Oct 10 09:33:44 2009 +0100 +++ b/mod_adhoc_cmd_admin/mod_adhoc_cmd_admin.lua Sun Oct 11 01:20:16 2009 +0200 @@ -4,13 +4,13 @@ -- COPYING file in the source package for more information. -- -local st, jid, uuid = require "util.stanza", require "util.jid", require "util.uuid"; -local dataforms_new = require "util.dataforms".new; local usermanager_user_exists = require "core.usermanager".user_exists; local usermanager_create_user = require "core.usermanager".create_user; +local is_admin = require "core.usermanager".is_admin; -local is_admin = require "core.usermanager".is_admin; -local admins = set.new(config.get(module:get_host(), "core", "admins")); +local st, jid, uuid = require "util.stanza", require "util.jid", require "util.uuid"; +local dataforms_new = require "util.dataforms".new; +local adhoc_new = module:require "adhoc".new; local sessions = {}; @@ -28,45 +28,38 @@ if not is_admin(stanza.attr.from) then module:log("warn", "Non-admin %s tried to add a user", tostring(jid.bare(stanza.attr.from))); origin.send(st.error_reply(stanza, "auth", "forbidden", "You don't have permission to add a user"):up() - :tag("command", {xmlns="http://jabber.org/protocol/commands", - node="http://jabber.org/protocol/admin#add-user", status="canceled"}) - :tag("note", {type="error"}):text("You don't have permission to add a user")); + :add_child(item:cmdtag("canceled") + :tag("note", {type="error"}):text("You don't have permission to add a user"))); return true; end if stanza.tags[1].attr.sessionid and sessions[stanza.tags[1].attr.sessionid] then if stanza.tags[1].attr.action == "cancel" then - origin.send(st.reply(stanza):tag("command", {xmlns="http://jabber.org/protocol/commands", - node="http://jabber.org/protocol/admin#add-user", - sessionid=stanza.tags[1].attr.sessionid, status="canceled"})); + origin.send(st.reply(stanza):add_child(item:cmdtag("canceled", stanza.tags[1].attr.sessionid))); sessions[stanza.tags[1].attr.sessionid] = nil; return true; end - form = stanza.tags[1]:find_child_with_ns("jabber:x:data"); + form = stanza.tags[1]:child_with_ns("jabber:x:data"); local fields = add_user_layout:data(form); local username, host, resource = jid.split(fields.accountjid); if (fields.password == fields["password-verify"]) and username and host and host == stanza.attr.to then if usermanager_user_exists(username, host) then origin.send(st.error_reply(stanza, "cancel", "conflict", "Account already exists"):up() - :tag("command", {xmlns="http://jabber.org/protocol/commands", - node="http://jabber.org/protocol/admin#add-user", status="canceled"}) - :tag("note", {type="error"}):text("Account already exists")); + :add_child(item:cmdtag("canceled", stanza.tags[1].attr.sessionid) + :tag("note", {type="error"}):text("Account already exists"))); sessions[stanza.tags[1].attr.sessionid] = nil; return true; else if usermanager_create_user(username, fields.password, host) then - origin.send(st.reply(stanza):tag("command", {xmlns="http://jabber.org/protocol/commands", - node="http://jabber.org/protocol/admin#add-user", - sessionid=stanza.tags[1].attr.sessionid, status="completed"}) - :tag("note", {type="info"}):text("Account successfully created")); + origin.send(st.reply(stanza):add_child(item:cmdtag("completed", stanza.tags[1].attr.sessionid) + :tag("note", {type="info"}):text("Account successfully created"))); sessions[stanza.tags[1].attr.sessionid] = nil; module:log("debug", "Created new account " .. username.."@"..host); return true; else origin.send(st.error_reply(stanza, "wait", "internal-server-error", "Failed to write data to disk"):up() - :tag("command", {xmlns="http://jabber.org/protocol/commands", - node="http://jabber.org/protocol/admin#add-user", status="canceled"}) - :tag("note", {type="error"}):text("Failed to write data to disk")); + :add_child(item:cmdtag("canceled", stanza.tags[1].attr.sessionid) + :tag("note", {type="error"}):text("Failed to write data to disk"))); sessions[stanza.tags[1].attr.sessionid] = nil; return true; end @@ -75,23 +68,20 @@ module:log("debug", fields.accountjid .. " " .. fields.password .. " " .. fields["password-verify"]); origin.send(st.error_reply(stanza, "cancel", "conflict", "Invalid data.\nPassword mismatch, or empty username"):up() - :tag("command", {xmlns="http://jabber.org/protocol/commands", - node="http://jabber.org/protocol/admin#add-user", status="canceled"}) - :tag("note", {type="error"}):text("Invalid data.\nPassword mismatch, or empty username")); + :add_child(item:cmdtag("canceled", stanza.tags[1].attr.sessionid) + :tag("note", {type="error"}):text("Invalid data.\nPassword mismatch, or empty username"))); sessions[stanza.tags[1].attr.sessionid] = nil; return true; end else local sessionid=uuid.generate(); sessions[sessionid] = "executing"; - origin.send(st.reply(stanza):tag("command", {xmlns="http://jabber.org/protocol/commands", - node="http://jabber.org/protocol/admin#add-user", sessionid=sessionid, - status="executing"}):add_child(add_user_layout:form())); + origin.send(st.reply(stanza):add_child(item:cmdtag("executing", sessionid):add_child(add_user_layout:form()))); end return true; end -local descriptor = { name="Add User", node="http://jabber.org/protocol/admin#add-user", handler=add_user_command_handler }; +local descriptor = adhoc_new("Add User", "http://jabber.org/protocol/admin#add-user", add_user_command_handler) function module.unload() module:remove_item("adhoc", descriptor); diff -r 3c49411d4aa3 -r 58d326d86a9a mod_adhoc_cmd_ping/mod_adhoc_cmd_ping.lua --- a/mod_adhoc_cmd_ping/mod_adhoc_cmd_ping.lua Sat Oct 10 09:33:44 2009 +0100 +++ b/mod_adhoc_cmd_ping/mod_adhoc_cmd_ping.lua Sun Oct 11 01:20:16 2009 +0200 @@ -5,15 +5,15 @@ -- local st = require "util.stanza"; +local adhoc_new = module:require "adhoc".new; function ping_command_handler (item, origin, stanza) local now = os.date("%Y-%m-%dT%X"); - origin.send(st.reply(stanza):tag("command", {xmlns="http://jabber.org/protocol/commands", status="completed", node=item.node, sessionid=now}) - :tag("note", {type="info"}):text("Pong\n" .. now)); + origin.send(st.reply(stanza):add_child(item:cmdtag("completed", now):tag("note", {type="info"}):text("Pong\n" .. now))); return true; end -local descriptor = { name="Ping", node="ping", handler=ping_command_handler }; +local descriptor = adhoc_new("Ping", "ping", ping_command_handler); function module.unload() module:remove_item("adhoc", descriptor);