2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
13 local t_concat = table.concat;
14 local t_insert = table.insert;
15 local tostring = tostring;
16 local tonumber = tonumber;
17 local st = require "util.stanza";
23 table=function(stanza, object)
25 for name, value in pairs(object) do
27 stanza:tag("name"):text(tostring(name)):up();
29 _lua_to_xmlrpc(stanza, value);
35 boolean=function(stanza, object)
36 stanza:tag("boolean"):text(object and "1" or "0"):up();
38 string=function(stanza, object)
39 stanza:tag("string"):text(object):up();
41 number=function(stanza, object)
42 stanza:tag("int"):text(tostring(object)):up();
44 ["nil"]=function(stanza, object) -- nil extension
45 stanza:tag("nil"):up();
48 _lua_to_xmlrpc = function(stanza, object)
49 local h = map[type(object)];
53 error("Type not supported by XML-RPC: " .. type(object));
56 function create_response(object)
57 local stanza = st.stanza("methodResponse"):tag("params"):tag("param"):tag("value");
58 _lua_to_xmlrpc(stanza, object);
59 stanza:up():up():up();
62 function create_error_response(faultCode, faultString)
63 local stanza = st.stanza("methodResponse"):tag("fault"):tag("value");
64 _lua_to_xmlrpc(stanza, {faultCode=faultCode, faultString=faultString});
71 local int_parse = function(stanza)
72 if #stanza.tags ~= 0 or #stanza == 0 then error("<"..stanza.name.."> must have a single text child"); end
73 local n = tonumber(t_concat(stanza));
74 if n then return n; end
75 error("Failed to parse content of <"..stanza.name..">");
78 methodCall=function(stanza)
79 if #stanza.tags ~= 2 then error("<methodCall> must have exactly two subtags"); end -- FIXME <params> is optional
80 if stanza.tags[1].name ~= "methodName" then error("First <methodCall> child tag must be <methodName>") end
81 if stanza.tags[2].name ~= "params" then error("Second <methodCall> child tag must be <params>") end
82 return _xmlrpc_to_lua(stanza.tags[1]), _xmlrpc_to_lua(stanza.tags[2]);
84 methodName=function(stanza)
85 if #stanza.tags ~= 0 then error("<methodName> must not have any subtags"); end
86 if #stanza == 0 then error("<methodName> must have text content"); end
87 return t_concat(stanza);
89 params=function(stanza)
91 for _, child in pairs(stanza.tags) do
92 if child.name ~= "param" then error("<params> can only have <param> children"); end;
93 t_insert(t, _xmlrpc_to_lua(child));
97 param=function(stanza)
98 if not(#stanza.tags == 1 and stanza.tags[1].name == "value") then error("<param> must have exactly one <value> child"); end
99 return _xmlrpc_to_lua(stanza.tags[1]);
101 value=function(stanza)
102 if #stanza.tags == 0 then return t_concat(stanza); end
103 if #stanza.tags ~= 1 then error("<value> must have a single child"); end
104 return _xmlrpc_to_lua(stanza.tags[1]);
109 boolean=function(stanza)
110 if #stanza.tags ~= 0 or #stanza == 0 then error("<boolean> must have a single text child"); end
111 local b = t_concat(stanza);
112 if b ~= "1" and b ~= "0" then error("Failed to parse content of <boolean>"); end
113 return b == "1" and true or false;
115 string=function(stanza)
116 if #stanza.tags ~= 0 then error("<string> must have a single text child"); end
117 return t_concat(stanza);
119 array=function(stanza)
120 if #stanza.tags ~= 1 then error("<array> must have a single <data> child"); end
121 return _xmlrpc_to_lua(stanza.tags[1]);
123 data=function(stanza)
125 for _,child in pairs(stanza.tags) do
126 if child.name ~= "value" then error("<data> can only have <value> children"); end
127 t_insert(t, _xmlrpc_to_lua(child));
131 struct=function(stanza)
133 for _,child in pairs(stanza.tags) do
134 if child.name ~= "member" then error("<struct> can only have <member> children"); end
135 local name, value = _xmlrpc_to_lua(child);
140 member=function(stanza)
141 if #stanza.tags ~= 2 then error("<member> must have exactly two subtags"); end -- FIXME <params> is optional
142 if stanza.tags[1].name ~= "name" then error("First <member> child tag must be <name>") end
143 if stanza.tags[2].name ~= "value" then error("Second <member> child tag must be <value>") end
144 return _xmlrpc_to_lua(stanza.tags[1]), _xmlrpc_to_lua(stanza.tags[2]);
146 name=function(stanza)
147 if #stanza.tags ~= 0 then error("<name> must have a single text child"); end
148 local n = t_concat(stanza)
149 if tostring(tonumber(n)) == n then n = tonumber(n); end
152 ["nil"]=function(stanza) -- nil extension
156 _xmlrpc_to_lua = function(stanza)
157 local h = rmap[stanza.name];
161 error("Unknown element: "..stanza.name);
164 function translate_request(stanza)
165 if stanza.name ~= "methodCall" then error("XML-RPC requests must have <methodCall> as root element"); end
166 return _xmlrpc_to_lua(stanza);