2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 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 select = select;
18 local st = require "util.stanza";
24 table=function(stanza, object)
26 for name, value in pairs(object) do
28 stanza:tag("name"):text(tostring(name)):up();
30 _lua_to_xmlrpc(stanza, value);
36 boolean=function(stanza, object)
37 stanza:tag("boolean"):text(object and "1" or "0"):up();
39 string=function(stanza, object)
40 stanza:tag("string"):text(object):up();
42 number=function(stanza, object)
43 stanza:tag("int"):text(tostring(object)):up();
45 ["nil"]=function(stanza, object) -- nil extension
46 stanza:tag("nil"):up();
49 _lua_to_xmlrpc = function(stanza, object)
50 local h = map[type(object)];
54 error("Type not supported by XML-RPC: " .. type(object));
57 function create_response(object)
58 local stanza = st.stanza("methodResponse"):tag("params"):tag("param"):tag("value");
59 _lua_to_xmlrpc(stanza, object);
60 stanza:up():up():up();
63 function create_error_response(faultCode, faultString)
64 local stanza = st.stanza("methodResponse"):tag("fault"):tag("value");
65 _lua_to_xmlrpc(stanza, {faultCode=faultCode, faultString=faultString});
70 function create_request(method_name, ...)
71 local stanza = st.stanza("methodCall")
72 :tag("methodName"):text(method_name):up()
74 for i=1,select('#', ...) do
75 stanza:tag("param"):tag("value");
76 _lua_to_xmlrpc(stanza, select(i, ...));
79 stanza:up():up():up();
84 local int_parse = function(stanza)
85 if #stanza.tags ~= 0 or #stanza == 0 then error("<"..stanza.name.."> must have a single text child"); end
86 local n = tonumber(t_concat(stanza));
87 if n then return n; end
88 error("Failed to parse content of <"..stanza.name..">");
91 methodCall=function(stanza)
92 if #stanza.tags ~= 2 then error("<methodCall> must have exactly two subtags"); end -- FIXME <params> is optional
93 if stanza.tags[1].name ~= "methodName" then error("First <methodCall> child tag must be <methodName>") end
94 if stanza.tags[2].name ~= "params" then error("Second <methodCall> child tag must be <params>") end
95 return _xmlrpc_to_lua(stanza.tags[1]), _xmlrpc_to_lua(stanza.tags[2]);
97 methodName=function(stanza)
98 if #stanza.tags ~= 0 then error("<methodName> must not have any subtags"); end
99 if #stanza == 0 then error("<methodName> must have text content"); end
100 return t_concat(stanza);
102 params=function(stanza)
104 for _, child in pairs(stanza.tags) do
105 if child.name ~= "param" then error("<params> can only have <param> children"); end;
106 t_insert(t, _xmlrpc_to_lua(child));
110 param=function(stanza)
111 if not(#stanza.tags == 1 and stanza.tags[1].name == "value") then error("<param> must have exactly one <value> child"); end
112 return _xmlrpc_to_lua(stanza.tags[1]);
114 value=function(stanza)
115 if #stanza.tags == 0 then return t_concat(stanza); end
116 if #stanza.tags ~= 1 then error("<value> must have a single child"); end
117 return _xmlrpc_to_lua(stanza.tags[1]);
122 boolean=function(stanza)
123 if #stanza.tags ~= 0 or #stanza == 0 then error("<boolean> must have a single text child"); end
124 local b = t_concat(stanza);
125 if b ~= "1" and b ~= "0" then error("Failed to parse content of <boolean>"); end
126 return b == "1" and true or false;
128 string=function(stanza)
129 if #stanza.tags ~= 0 then error("<string> must have a single text child"); end
130 return t_concat(stanza);
132 array=function(stanza)
133 if #stanza.tags ~= 1 then error("<array> must have a single <data> child"); end
134 return _xmlrpc_to_lua(stanza.tags[1]);
136 data=function(stanza)
138 for _,child in pairs(stanza.tags) do
139 if child.name ~= "value" then error("<data> can only have <value> children"); end
140 t_insert(t, _xmlrpc_to_lua(child));
144 struct=function(stanza)
146 for _,child in pairs(stanza.tags) do
147 if child.name ~= "member" then error("<struct> can only have <member> children"); end
148 local name, value = _xmlrpc_to_lua(child);
153 member=function(stanza)
154 if #stanza.tags ~= 2 then error("<member> must have exactly two subtags"); end -- FIXME <params> is optional
155 if stanza.tags[1].name ~= "name" then error("First <member> child tag must be <name>") end
156 if stanza.tags[2].name ~= "value" then error("Second <member> child tag must be <value>") end
157 return _xmlrpc_to_lua(stanza.tags[1]), _xmlrpc_to_lua(stanza.tags[2]);
159 name=function(stanza)
160 if #stanza.tags ~= 0 then error("<name> must have a single text child"); end
161 local n = t_concat(stanza)
162 if tostring(tonumber(n)) == n then n = tonumber(n); end
165 ["nil"]=function(stanza) -- nil extension
169 _xmlrpc_to_lua = function(stanza)
170 local h = rmap[stanza.name];
174 error("Unknown element: "..stanza.name);
177 function translate_request(stanza)
178 if stanza.name ~= "methodCall" then error("XML-RPC requests must have <methodCall> as root element"); end
179 return _xmlrpc_to_lua(stanza);