aboutsummaryrefslogtreecommitdiffstats
path: root/util
diff options
context:
space:
mode:
Diffstat (limited to 'util')
-rw-r--r--util/debug.lua97
-rw-r--r--util/helpers.lua31
-rw-r--r--util/httpstream.lua3
-rw-r--r--util/openssl.lua156
-rw-r--r--util/prosodyctl.lua21
-rw-r--r--util/rfc3484.lua4
-rw-r--r--util/stanza.lua6
-rw-r--r--util/template.lua16
-rw-r--r--util/termcolours.lua23
-rw-r--r--util/timer.lua16
-rw-r--r--util/x509.lua4
-rw-r--r--util/xmlrpc.lua182
-rw-r--r--util/xmppstream.lua33
13 files changed, 345 insertions, 247 deletions
diff --git a/util/debug.lua b/util/debug.lua
index 2170a6d1..bff0e347 100644
--- a/util/debug.lua
+++ b/util/debug.lua
@@ -7,8 +7,24 @@ local censored_names = {
pass = true;
pwd = true;
};
+local optimal_line_length = 65;
-local function get_locals_table(level)
+local termcolours = require "util.termcolours";
+local getstring = termcolours.getstring;
+local styles;
+do
+ _ = termcolours.getstyle;
+ styles = {
+ boundary_padding = _("bright");
+ filename = _("bright", "blue");
+ level_num = _("green");
+ funcname = _("yellow");
+ location = _("yellow");
+ };
+end
+module("debugx", package.seeall);
+
+function get_locals_table(level)
level = level + 1; -- Skip this function itself
local locals = {};
for local_num = 1, math.huge do
@@ -19,7 +35,7 @@ local function get_locals_table(level)
return locals;
end
-local function get_upvalues_table(func)
+function get_upvalues_table(func)
local upvalues = {};
if func then
for upvalue_num = 1, math.huge do
@@ -31,7 +47,7 @@ local function get_upvalues_table(func)
return upvalues;
end
-local function string_from_var_table(var_table, max_line_len, indent_str)
+function string_from_var_table(var_table, max_line_len, indent_str)
local var_string = {};
local col_pos = 0;
max_line_len = max_line_len or math.huge;
@@ -72,46 +88,72 @@ function get_traceback_table(thread, start_level)
for level = start_level, math.huge do
local info;
if thread then
- info = debug.getinfo(thread, level);
+ info = debug.getinfo(thread, level+1);
else
- info = debug.getinfo(level);
+ info = debug.getinfo(level+1);
end
if not info then break; end
levels[(level-start_level)+1] = {
level = level;
info = info;
- locals = get_locals_table(level);
+ locals = get_locals_table(level+1);
upvalues = get_upvalues_table(info.func);
};
end
return levels;
end
-function debug.traceback(thread, message, level)
- if type(thread) ~= "thread" then
- thread, message, level = coroutine.running(), thread, message;
+function traceback(...)
+ local ok, ret = pcall(_traceback, ...);
+ if not ok then
+ return "Error in error handling: "..ret;
end
- if level and type(message) ~= "string" then
- return nil, "invalid message";
- elseif not level then
- level = message or 2;
+ return ret;
+end
+
+local function build_source_boundary_marker(last_source_desc)
+ local padding = string.rep("-", math.floor(((optimal_line_length - 6) - #last_source_desc)/2));
+ return getstring(styles.boundary_padding, "v"..padding).." "..getstring(styles.filename, last_source_desc).." "..getstring(styles.boundary_padding, padding..(#last_source_desc%2==0 and "-v" or "v "));
+end
+
+function _traceback(thread, message, level)
+
+ -- Lua manual says: debug.traceback ([thread,] [message [, level]])
+ -- I fathom this to mean one of:
+ -- ()
+ -- (thread)
+ -- (message, level)
+ -- (thread, message, level)
+
+ if thread == nil then -- Defaults
+ thread, message, level = coroutine.running(), message, level;
+ elseif type(thread) == "string" then
+ thread, message, level = coroutine.running(), thread, message;
+ elseif type(thread) ~= "thread" then
+ return nil; -- debug.traceback() does this
end
-
+
+ level = level or 1;
+
message = message and (message.."\n") or "";
- local levels = get_traceback_table(thread, level+2);
+ -- +3 counts for this function, and the pcall() and wrapper above us
+ local levels = get_traceback_table(thread, level+3);
+
+ local last_source_desc;
local lines = {};
for nlevel, level in ipairs(levels) do
local info = level.info;
local line = "...";
local func_type = info.namewhat.." ";
+ local source_desc = (info.short_src == "[C]" and "C code") or info.short_src or "Unknown";
if func_type == " " then func_type = ""; end;
if info.short_src == "[C]" then
- line = "[ C ] "..func_type.."C function "..(info.name and ("%q"):format(info.name) or "(unknown name)")
+ line = "[ C ] "..func_type.."C function "..getstring(styles.location, (info.name and ("%q"):format(info.name) or "(unknown name)"));
elseif info.what == "main" then
- line = "[Lua] "..info.short_src.." line "..info.currentline;
+ line = "[Lua] "..getstring(styles.location, info.short_src.." line "..info.currentline);
else
local name = info.name or " ";
if name ~= " " then
@@ -120,19 +162,32 @@ function debug.traceback(thread, message, level)
if func_type == "global " or func_type == "local " then
func_type = func_type.."function ";
end
- line = "[Lua] "..info.short_src.." line "..info.currentline.." in "..func_type..name.." defined on line "..info.linedefined;
+ line = "[Lua] "..getstring(styles.location, info.short_src.." line "..info.currentline).." in "..func_type..getstring(styles.funcname, name).." (defined on line "..info.linedefined..")";
+ end
+ if source_desc ~= last_source_desc then -- Venturing into a new source, add marker for previous
+ last_source_desc = source_desc;
+ table.insert(lines, "\t "..build_source_boundary_marker(last_source_desc));
end
nlevel = nlevel-1;
- table.insert(lines, "\t"..(nlevel==0 and ">" or " ").."("..nlevel..") "..line);
+ table.insert(lines, "\t"..(nlevel==0 and ">" or " ")..getstring(styles.level_num, "("..nlevel..") ")..line);
local npadding = (" "):rep(#tostring(nlevel));
- local locals_str = string_from_var_table(level.locals, 65, "\t "..npadding);
+ local locals_str = string_from_var_table(level.locals, optimal_line_length, "\t "..npadding);
if locals_str then
table.insert(lines, "\t "..npadding.."Locals: "..locals_str);
end
- local upvalues_str = string_from_var_table(level.upvalues, 65, "\t "..npadding);
+ local upvalues_str = string_from_var_table(level.upvalues, optimal_line_length, "\t "..npadding);
if upvalues_str then
table.insert(lines, "\t "..npadding.."Upvals: "..upvalues_str);
end
end
+
+-- table.insert(lines, "\t "..build_source_boundary_marker(last_source_desc));
+
return message.."stack traceback:\n"..table.concat(lines, "\n");
end
+
+function use()
+ debug.traceback = traceback;
+end
+
+return _M;
diff --git a/util/helpers.lua b/util/helpers.lua
index a8c8c612..6103a319 100644
--- a/util/helpers.lua
+++ b/util/helpers.lua
@@ -6,6 +6,8 @@
-- COPYING file in the source package for more information.
--
+local debug = require "util.debug";
+
module("helpers", package.seeall);
-- Helper functions for debugging
@@ -31,6 +33,35 @@ function revert_log_events(events)
events.fire_event, events[events.fire_event] = events[events.fire_event], nil; -- :))
end
+function show_events(events, specific_event)
+ local event_handlers = events._handlers;
+ local events_array = {};
+ local event_handler_arrays = {};
+ for event in pairs(events._event_map) do
+ local handlers = event_handlers[event];
+ if handlers and (event == specific_event or not specific_event) then
+ table.insert(events_array, event);
+ local handler_strings = {};
+ for i, handler in ipairs(handlers) do
+ local upvals = debug.string_from_var_table(debug.get_upvalues_table(handler));
+ handler_strings[i] = " "..i..": "..tostring(handler)..(upvals and ("\n "..upvals) or "");
+ end
+ event_handler_arrays[event] = handler_strings;
+ end
+ end
+ table.sort(events_array);
+ local i = 1;
+ while i <= #events_array do
+ local handlers = event_handler_arrays[events_array[i]];
+ for j=#handlers, 1, -1 do
+ table.insert(events_array, i+1, handlers[j]);
+ end
+ if i > 1 then events_array[i] = "\n"..events_array[i]; end
+ i = i + #handlers + 1
+ end
+ return table.concat(events_array, "\n");
+end
+
function get_upvalue(f, get_name)
local i, name, value = 0;
repeat
diff --git a/util/httpstream.lua b/util/httpstream.lua
index bdc3fce7..190b3ed6 100644
--- a/util/httpstream.lua
+++ b/util/httpstream.lua
@@ -107,9 +107,6 @@ local function parser(success_cb, parser_type, options_cb)
httpversion = httpversion;
headers = headers;
body = body;
- -- COMPAT the properties below are deprecated
- responseversion = httpversion;
- responseheaders = headers;
});
end
else coroutine.yield("unknown-parser-type"); end
diff --git a/util/openssl.lua b/util/openssl.lua
new file mode 100644
index 00000000..8fdb9b4a
--- /dev/null
+++ b/util/openssl.lua
@@ -0,0 +1,156 @@
+local type, tostring, pairs, ipairs = type, tostring, pairs, ipairs;
+local t_insert, t_concat = table.insert, table.concat;
+local s_format = string.format;
+
+local oid_xmppaddr = "1.3.6.1.5.5.7.8.5"; -- [XMPP-CORE]
+local oid_dnssrv = "1.3.6.1.5.5.7.8.7"; -- [SRV-ID]
+
+local idna_to_ascii = require "util.encodings".idna.to_ascii;
+
+local _M = {};
+local config = {};
+_M.config = config;
+
+local ssl_config = {};
+local ssl_config_mt = {__index=ssl_config};
+
+function config.new()
+ return setmetatable({
+ req = {
+ distinguished_name = "distinguished_name",
+ req_extensions = "v3_extensions",
+ x509_extensions = "v3_extensions",
+ prompt = "no",
+ },
+ distinguished_name = {
+ commonName = "example.com",
+ countryName = "GB",
+ localityName = "The Internet",
+ organizationName = "Your Organisation",
+ organizationalUnitName = "XMPP Department",
+ emailAddress = "xmpp@example.com",
+ },
+ v3_extensions = {
+ basicConstraints = "CA:FALSE",
+ keyUsage = "digitalSignature,keyEncipherment",
+ extendedKeyUsage = "serverAuth,clientAuth",
+ subjectAltName = "@subject_alternative_name",
+ },
+ subject_alternative_name = {
+ DNS = {},
+ otherName = {},
+ },
+ }, ssl_config_mt);
+end
+
+function ssl_config:serialize()
+ local s = "";
+ for k, t in pairs(self) do
+ s = s .. ("[%s]\n"):format(k);
+ if k == "subject_alternative_name" then
+ for san, n in pairs(t) do
+ for i = 1,#n do
+ s = s .. s_format("%s.%d = %s\n", san, i -1, n[i]);
+ end
+ end
+ else
+ for k, v in pairs(t) do
+ s = s .. ("%s = %s\n"):format(k, v);
+ end
+ end
+ s = s .. "\n";
+ end
+ return s;
+end
+
+local function utf8string(s)
+ -- This is how we tell openssl not to encode UTF-8 strings as fake Latin1
+ return s_format("FORMAT:UTF8,UTF8:%s", s);
+end
+
+local function ia5string(s)
+ return s_format("IA5STRING:%s", s);
+end
+
+local util = {};
+_M.util = {
+ utf8string = utf8string,
+ ia5string = ia5string,
+};
+
+local function xmppAddr(t, host)
+end
+
+function ssl_config:add_dNSName(host)
+ t_insert(self.subject_alternative_name.DNS, idna_to_ascii(host));
+end
+
+function ssl_config:add_sRVName(host, service)
+ t_insert(self.subject_alternative_name.otherName,
+ s_format("%s;%s", oid_dnssrv, ia5string("_" .. service .."." .. idna_to_ascii(host))));
+end
+
+function ssl_config:add_xmppAddr(host)
+ t_insert(self.subject_alternative_name.otherName,
+ s_format("%s;%s", oid_xmppaddr, utf8string(host)));
+end
+
+function ssl_config:from_prosody(hosts, config, certhosts, raw)
+ -- TODO Decide if this should go elsewhere
+ local found_matching_hosts = false;
+ for i = 1,#certhosts do
+ local certhost = certhosts[i];
+ for name, host in pairs(hosts) do
+ if name == certhost or name:sub(-1-#certhost) == "."..certhost then
+ found_matching_hosts = true;
+ self:add_dNSName(name);
+ --print(name .. "#component_module: " .. (config.get(name, "core", "component_module") or "nil"));
+ if config.get(name, "core", "component_module") == nil then
+ self:add_sRVName(name, "xmpp-client");
+ end
+ --print(name .. "#anonymous_login: " .. tostring(config.get(name, "core", "anonymous_login")));
+ if not (config.get(name, "core", "anonymous_login") or
+ config.get(name, "core", "authentication") == "anonymous") then
+ self:add_sRVName(name, "xmpp-server");
+ end
+ self:add_xmppAddr(name);
+ end
+ end
+ end
+ if not found_matching_hosts then
+ return nil, "no-matching-hosts";
+ end
+end
+
+do -- Lua to shell calls.
+ local function shell_escape(s)
+ return s:gsub("'",[['\'']]);
+ end
+
+ local function serialize(f,o)
+ local r = {"openssl", f};
+ for k,v in pairs(o) do
+ if type(k) == "string" then
+ t_insert(r, ("-%s"):format(k));
+ if v ~= true then
+ t_insert(r, ("'%s'"):format(shell_escape(tostring(v))));
+ end
+ end
+ end
+ for k,v in ipairs(o) do
+ t_insert(r, ("'%s'"):format(shell_escape(tostring(v))));
+ end
+ return t_concat(r, " ");
+ end
+
+ local os_execute = os.execute;
+ setmetatable(_M, {
+ __index=function(self,f)
+ return function(opts)
+ return 0 == os_execute(serialize(f, type(opts) == "table" and opts or {}));
+ end;
+ end;
+ });
+end
+
+return _M;
diff --git a/util/prosodyctl.lua b/util/prosodyctl.lua
index d0045abc..439de551 100644
--- a/util/prosodyctl.lua
+++ b/util/prosodyctl.lua
@@ -16,6 +16,7 @@ local signal = require "util.signal";
local set = require "util.set";
local lfs = require "lfs";
local pcall = pcall;
+local type = type;
local nodeprep, nameprep = stringprep.nodeprep, stringprep.nameprep;
@@ -63,6 +64,13 @@ function getchar(n)
end
end
+function getline()
+ local ok, line = pcall(io.read, "*l");
+ if ok then
+ return line;
+ end
+end
+
function getpass()
local stty_ret = os.execute("stty -echo 2>/dev/null");
if stty_ret ~= 0 then
@@ -112,6 +120,13 @@ function read_password()
return password;
end
+function show_prompt(prompt)
+ io.write(prompt, " ");
+ local line = getline();
+ line = line and line:gsub("\n$","");
+ return (line and #line > 0) and line or nil;
+end
+
-- Server control
function adduser(params)
local user, host, password = nodeprep(params.user), nameprep(params.host), params.password;
@@ -121,7 +136,11 @@ function adduser(params)
return false, "invalid-hostname";
end
- local provider = prosody.hosts[host].users;
+ local host_session = prosody.hosts[host];
+ if not host_session then
+ return false, "no-such-host";
+ end
+ local provider = host_session.users;
if not(provider) or provider.name == "null" then
usermanager.initialize_host(host);
end
diff --git a/util/rfc3484.lua b/util/rfc3484.lua
index dd855a84..5ee572a0 100644
--- a/util/rfc3484.lua
+++ b/util/rfc3484.lua
@@ -19,7 +19,7 @@ local function t_sort(t, comp)
end
end
-function source(dest, candidates)
+local function source(dest, candidates)
local function comp(ipA, ipB)
-- Rule 1: Prefer same address
if dest == ipA then
@@ -70,7 +70,7 @@ function source(dest, candidates)
return candidates[1];
end
-function destination(candidates, sources)
+local function destination(candidates, sources)
local sourceAddrs = {};
local function comp(ipA, ipB)
local ipAsource = sourceAddrs[ipA];
diff --git a/util/stanza.lua b/util/stanza.lua
index 600212a4..1449f707 100644
--- a/util/stanza.lua
+++ b/util/stanza.lua
@@ -8,22 +8,16 @@
local t_insert = table.insert;
-local t_concat = table.concat;
local t_remove = table.remove;
local t_concat = table.concat;
local s_format = string.format;
local s_match = string.match;
local tostring = tostring;
local setmetatable = setmetatable;
-local getmetatable = getmetatable;
local pairs = pairs;
local ipairs = ipairs;
local type = type;
-local next = next;
-local print = print;
-local unpack = unpack;
local s_gsub = string.gsub;
-local s_char = string.char;
local s_find = string.find;
local os = os;
diff --git a/util/template.lua b/util/template.lua
index ebd8be14..5e9b479e 100644
--- a/util/template.lua
+++ b/util/template.lua
@@ -7,6 +7,7 @@ local ipairs = ipairs;
local error = error;
local loadstring = loadstring;
local debug = debug;
+local t_remove = table.remove;
module("template")
@@ -42,7 +43,6 @@ local parse_xml = (function()
stanza:tag(name, attr);
end
function handler:CharacterData(data)
- data = data:gsub("^%s*", ""):gsub("%s*$", "");
stanza:text(data);
end
function handler:EndElement(tagname)
@@ -60,6 +60,19 @@ local parse_xml = (function()
end;
end)();
+local function trim_xml(stanza)
+ for i=#stanza,1,-1 do
+ local child = stanza[i];
+ if child.name then
+ trim_xml(child);
+ else
+ child = child:gsub("^%s*", ""):gsub("%s*$", "");
+ stanza[i] = child;
+ if child == "" then t_remove(stanza, i); end
+ end
+ end
+end
+
local function create_string_string(str)
str = ("%q"):format(str);
str = str:gsub("{([^}]*)}", function(s)
@@ -118,6 +131,7 @@ local template_mt = { __tostring = function(t) return t.name end };
local function create_template(templates, text)
local stanza, err = parse_xml(text);
if not stanza then error(err); end
+ trim_xml(stanza);
local info = debug.getinfo(3, "Sl");
info = info and ("template(%s:%d)"):format(info.short_src:match("[^\\/]*$"), info.currentline) or "template(unknown)";
diff --git a/util/termcolours.lua b/util/termcolours.lua
index df204688..6ef3b689 100644
--- a/util/termcolours.lua
+++ b/util/termcolours.lua
@@ -9,6 +9,7 @@
local t_concat, t_insert = table.concat, table.insert;
local char, format = string.char, string.format;
+local tonumber = tonumber;
local ipairs = ipairs;
local io_write = io.write;
@@ -34,6 +35,15 @@ local winstylemap = {
["1;31"] = 4+8 -- bold red
}
+local cssmap = {
+ [1] = "font-weight: bold", [2] = "opacity: 0.5", [4] = "text-decoration: underline", [8] = "visibility: hidden",
+ [30] = "color:black", [31] = "color:red", [32]="color:green", [33]="color:#FFD700",
+ [34] = "color:blue", [35] = "color: magenta", [36] = "color:cyan", [37] = "color: white",
+ [40] = "background-color:black", [41] = "background-color:red", [42]="background-color:green",
+ [43]="background-color:yellow", [44] = "background-color:blue", [45] = "background-color: magenta",
+ [46] = "background-color:cyan", [47] = "background-color: white";
+};
+
local fmt_string = char(0x1B).."[%sm%s"..char(0x1B).."[0m";
function getstring(style, text)
if style then
@@ -76,4 +86,17 @@ if windows then
end
end
+local function ansi2css(ansi_codes)
+ if ansi_codes == "0" then return "</span>"; end
+ local css = {};
+ for code in ansi_codes:gmatch("[^;]+") do
+ t_insert(css, cssmap[tonumber(code)]);
+ end
+ return "</span><span style='"..t_concat(css, ";").."'>";
+end
+
+function tohtml(input)
+ return input:gsub("\027%[(.-)m", ansi2css);
+end
+
return _M;
diff --git a/util/timer.lua b/util/timer.lua
index d5b66473..e52a6917 100644
--- a/util/timer.lua
+++ b/util/timer.lua
@@ -6,17 +6,12 @@
-- COPYING file in the source package for more information.
--
-
-local ns_addtimer = require "net.server".addtimer;
-local event = require "net.server".event;
-local event_base = require "net.server".event_base;
-
+local server = require "net.server";
local math_min = math.min
local math_huge = math.huge
local get_time = require "socket".gettime;
local t_insert = table.insert;
-local t_remove = table.remove;
-local ipairs, pairs = ipairs, pairs;
+local pairs = pairs;
local type = type;
local data = {};
@@ -25,7 +20,7 @@ local new_data = {};
module "timer"
local _add_task;
-if not event then
+if not server.event then
function _add_task(delay, callback)
local current_time = get_time();
delay = delay + current_time;
@@ -39,7 +34,7 @@ if not event then
end
end
- ns_addtimer(function()
+ server._addtimer(function()
local current_time = get_time();
if #new_data > 0 then
for _, d in pairs(new_data) do
@@ -65,7 +60,10 @@ if not event then
return next_time;
end);
else
+ local event = server.event;
+ local event_base = server.event_base;
local EVENT_LEAVE = (event.core and event.core.LEAVE) or -1;
+
function _add_task(delay, callback)
local event_handle;
event_handle = event_base:addevent(nil, 0, function ()
diff --git a/util/x509.lua b/util/x509.lua
index d3c55bb4..19d4ec6d 100644
--- a/util/x509.lua
+++ b/util/x509.lua
@@ -21,6 +21,10 @@
local nameprep = require "util.encodings".stringprep.nameprep;
local idna_to_ascii = require "util.encodings".idna.to_ascii;
local log = require "util.logger".init("x509");
+local pairs, ipairs = pairs, ipairs;
+local s_format = string.format;
+local t_insert = table.insert;
+local t_concat = table.concat;
module "x509"
diff --git a/util/xmlrpc.lua b/util/xmlrpc.lua
deleted file mode 100644
index 29815b0d..00000000
--- a/util/xmlrpc.lua
+++ /dev/null
@@ -1,182 +0,0 @@
--- Prosody IM
--- Copyright (C) 2008-2010 Matthew Wild
--- Copyright (C) 2008-2010 Waqas Hussain
---
--- This project is MIT/X11 licensed. Please see the
--- COPYING file in the source package for more information.
---
-
-
-local pairs = pairs;
-local type = type;
-local error = error;
-local t_concat = table.concat;
-local t_insert = table.insert;
-local tostring = tostring;
-local tonumber = tonumber;
-local select = select;
-local st = require "util.stanza";
-
-module "xmlrpc"
-
-local _lua_to_xmlrpc;
-local map = {
- table=function(stanza, object)
- stanza:tag("struct");
- for name, value in pairs(object) do
- stanza:tag("member");
- stanza:tag("name"):text(tostring(name)):up();
- stanza:tag("value");
- _lua_to_xmlrpc(stanza, value);
- stanza:up();
- stanza:up();
- end
- stanza:up();
- end;
- boolean=function(stanza, object)
- stanza:tag("boolean"):text(object and "1" or "0"):up();
- end;
- string=function(stanza, object)
- stanza:tag("string"):text(object):up();
- end;
- number=function(stanza, object)
- stanza:tag("int"):text(tostring(object)):up();
- end;
- ["nil"]=function(stanza, object) -- nil extension
- stanza:tag("nil"):up();
- end;
-};
-_lua_to_xmlrpc = function(stanza, object)
- local h = map[type(object)];
- if h then
- h(stanza, object);
- else
- error("Type not supported by XML-RPC: " .. type(object));
- end
-end
-function create_response(object)
- local stanza = st.stanza("methodResponse"):tag("params"):tag("param"):tag("value");
- _lua_to_xmlrpc(stanza, object);
- stanza:up():up():up();
- return stanza;
-end
-function create_error_response(faultCode, faultString)
- local stanza = st.stanza("methodResponse"):tag("fault"):tag("value");
- _lua_to_xmlrpc(stanza, {faultCode=faultCode, faultString=faultString});
- stanza:up():up();
- return stanza;
-end
-
-function create_request(method_name, ...)
- local stanza = st.stanza("methodCall")
- :tag("methodName"):text(method_name):up()
- :tag("params");
- for i=1,select('#', ...) do
- stanza:tag("param"):tag("value");
- _lua_to_xmlrpc(stanza, select(i, ...));
- stanza:up():up();
- end
- stanza:up():up():up();
- return stanza;
-end
-
-local _xmlrpc_to_lua;
-local int_parse = function(stanza)
- if #stanza.tags ~= 0 or #stanza == 0 then error("<"..stanza.name.."> must have a single text child"); end
- local n = tonumber(t_concat(stanza));
- if n then return n; end
- error("Failed to parse content of <"..stanza.name..">");
-end
-local rmap = {
- methodCall=function(stanza)
- if #stanza.tags ~= 2 then error("<methodCall> must have exactly two subtags"); end -- FIXME <params> is optional
- if stanza.tags[1].name ~= "methodName" then error("First <methodCall> child tag must be <methodName>") end
- if stanza.tags[2].name ~= "params" then error("Second <methodCall> child tag must be <params>") end
- return _xmlrpc_to_lua(stanza.tags[1]), _xmlrpc_to_lua(stanza.tags[2]);
- end;
- methodName=function(stanza)
- if #stanza.tags ~= 0 then error("<methodName> must not have any subtags"); end
- if #stanza == 0 then error("<methodName> must have text content"); end
- return t_concat(stanza);
- end;
- params=function(stanza)
- local t = {};
- for _, child in pairs(stanza.tags) do
- if child.name ~= "param" then error("<params> can only have <param> children"); end;
- t_insert(t, _xmlrpc_to_lua(child));
- end
- return t;
- end;
- param=function(stanza)
- if not(#stanza.tags == 1 and stanza.tags[1].name == "value") then error("<param> must have exactly one <value> child"); end
- return _xmlrpc_to_lua(stanza.tags[1]);
- end;
- value=function(stanza)
- if #stanza.tags == 0 then return t_concat(stanza); end
- if #stanza.tags ~= 1 then error("<value> must have a single child"); end
- return _xmlrpc_to_lua(stanza.tags[1]);
- end;
- int=int_parse;
- i4=int_parse;
- double=int_parse;
- boolean=function(stanza)
- if #stanza.tags ~= 0 or #stanza == 0 then error("<boolean> must have a single text child"); end
- local b = t_concat(stanza);
- if b ~= "1" and b ~= "0" then error("Failed to parse content of <boolean>"); end
- return b == "1" and true or false;
- end;
- string=function(stanza)
- if #stanza.tags ~= 0 then error("<string> must have a single text child"); end
- return t_concat(stanza);
- end;
- array=function(stanza)
- if #stanza.tags ~= 1 then error("<array> must have a single <data> child"); end
- return _xmlrpc_to_lua(stanza.tags[1]);
- end;
- data=function(stanza)
- local t = {};
- for _,child in pairs(stanza.tags) do
- if child.name ~= "value" then error("<data> can only have <value> children"); end
- t_insert(t, _xmlrpc_to_lua(child));
- end
- return t;
- end;
- struct=function(stanza)
- local t = {};
- for _,child in pairs(stanza.tags) do
- if child.name ~= "member" then error("<struct> can only have <member> children"); end
- local name, value = _xmlrpc_to_lua(child);
- t[name] = value;
- end
- return t;
- end;
- member=function(stanza)
- if #stanza.tags ~= 2 then error("<member> must have exactly two subtags"); end -- FIXME <params> is optional
- if stanza.tags[1].name ~= "name" then error("First <member> child tag must be <name>") end
- if stanza.tags[2].name ~= "value" then error("Second <member> child tag must be <value>") end
- return _xmlrpc_to_lua(stanza.tags[1]), _xmlrpc_to_lua(stanza.tags[2]);
- end;
- name=function(stanza)
- if #stanza.tags ~= 0 then error("<name> must have a single text child"); end
- local n = t_concat(stanza)
- if tostring(tonumber(n)) == n then n = tonumber(n); end
- return n;
- end;
- ["nil"]=function(stanza) -- nil extension
- return nil;
- end;
-}
-_xmlrpc_to_lua = function(stanza)
- local h = rmap[stanza.name];
- if h then
- return h(stanza);
- else
- error("Unknown element: "..stanza.name);
- end
-end
-function translate_request(stanza)
- if stanza.name ~= "methodCall" then error("XML-RPC requests must have <methodCall> as root element"); end
- return _xmlrpc_to_lua(stanza);
-end
-
-return _M;
diff --git a/util/xmppstream.lua b/util/xmppstream.lua
index 0f80742d..f1793b4f 100644
--- a/util/xmppstream.lua
+++ b/util/xmppstream.lua
@@ -25,8 +25,11 @@ module "xmppstream"
local new_parser = lxp.new;
-local ns_prefixes = {
- ["http://www.w3.org/XML/1998/namespace"] = "xml";
+local xml_namespace = {
+ ["http://www.w3.org/XML/1998/namespace\1lang"] = "xml:lang";
+ ["http://www.w3.org/XML/1998/namespace\1space"] = "xml:space";
+ ["http://www.w3.org/XML/1998/namespace\1base"] = "xml:base";
+ ["http://www.w3.org/XML/1998/namespace\1id"] = "xml:id";
};
local xmlns_streams = "http://etherx.jabber.org/streams";
@@ -73,17 +76,13 @@ function new_sax_handlers(session, stream_callbacks)
non_streamns_depth = non_streamns_depth + 1;
end
- -- FIXME !!!!!
for i=1,#attr do
local k = attr[i];
attr[i] = nil;
- local ns, nm = k:match(ns_pattern);
- if nm ~= "" then
- ns = ns_prefixes[ns];
- if ns then
- attr[ns..":"..nm] = attr[k];
- attr[k] = nil;
- end
+ local xmlk = xml_namespace[k];
+ if xmlk then
+ attr[xmlk] = attr[k];
+ attr[k] = nil;
end
end
@@ -140,19 +139,9 @@ function new_sax_handlers(session, stream_callbacks)
stanza = t_remove(stack);
end
else
- if tagname == stream_tag then
- if cb_streamclosed then
- cb_streamclosed(session);
- end
- else
- local curr_ns,name = tagname:match(ns_pattern);
- if name == "" then
- curr_ns, name = "", curr_ns;
- end
- cb_error(session, "parse-error", "unexpected-element-close", name);
+ if cb_streamclosed then
+ cb_streamclosed(session);
end
- stanza, chardata = nil, {};
- stack = {};
end
end