Annotate

util/xmlrpc.lua @ 1545:e6bd29342f65

Merge with 0.5
author Matthew Wild <mwild1@gmail.com>
date Sun, 12 Jul 2009 20:33:09 +0100
parent 1523:841d61be198f
child 1554:06030af44fad
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1523
841d61be198f Remove version number from copyright headers
Matthew Wild <mwild1@gmail.com>
parents: 1498
diff changeset
1 -- Prosody IM
874
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
2 -- Copyright (C) 2008-2009 Matthew Wild
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
3 -- Copyright (C) 2008-2009 Waqas Hussain
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
4 --
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
6 -- COPYING file in the source package for more information.
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
7 --
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
8
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
9
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
10 local pairs = pairs;
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
11 local type = type;
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
12 local error = error;
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
13 local t_concat = table.concat;
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
14 local t_insert = table.insert;
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
15 local tostring = tostring;
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
16 local tonumber = tonumber;
1498
481dfc89047a util.xmlrpc: Support for multiple parameters in requests
Matthew Wild <mwild1@gmail.com>
parents: 1497
diff changeset
17 local select = select;
874
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
18 local st = require "util.stanza";
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
19
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
20 module "xmlrpc"
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
21
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
22 local _lua_to_xmlrpc;
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
23 local map = {
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
24 table=function(stanza, object)
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
25 stanza:tag("struct");
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
26 for name, value in pairs(object) do
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
27 stanza:tag("member");
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
28 stanza:tag("name"):text(tostring(name)):up();
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
29 stanza:tag("value");
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
30 _lua_to_xmlrpc(stanza, value);
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
31 stanza:up();
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
32 stanza:up();
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
33 end
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
34 stanza:up();
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
35 end;
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
36 boolean=function(stanza, object)
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
37 stanza:tag("boolean"):text(object and "1" or "0"):up();
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
38 end;
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
39 string=function(stanza, object)
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
40 stanza:tag("string"):text(object):up();
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
41 end;
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
42 number=function(stanza, object)
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
43 stanza:tag("int"):text(tostring(object)):up();
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
44 end;
888
1059230969c3 util/xmlrpc: Added <nil/> extension to RPC functions (allowing nil parameters and return values)
Waqas Hussain <waqas20@gmail.com>
parents: 874
diff changeset
45 ["nil"]=function(stanza, object) -- nil extension
1059230969c3 util/xmlrpc: Added <nil/> extension to RPC functions (allowing nil parameters and return values)
Waqas Hussain <waqas20@gmail.com>
parents: 874
diff changeset
46 stanza:tag("nil"):up();
1059230969c3 util/xmlrpc: Added <nil/> extension to RPC functions (allowing nil parameters and return values)
Waqas Hussain <waqas20@gmail.com>
parents: 874
diff changeset
47 end;
874
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
48 };
1498
481dfc89047a util.xmlrpc: Support for multiple parameters in requests
Matthew Wild <mwild1@gmail.com>
parents: 1497
diff changeset
49 _lua_to_xmlrpc = function(stanza, ...)
481dfc89047a util.xmlrpc: Support for multiple parameters in requests
Matthew Wild <mwild1@gmail.com>
parents: 1497
diff changeset
50 for i=1,select('#', ...) do
481dfc89047a util.xmlrpc: Support for multiple parameters in requests
Matthew Wild <mwild1@gmail.com>
parents: 1497
diff changeset
51 stanza:tag("param"):tag("value");
481dfc89047a util.xmlrpc: Support for multiple parameters in requests
Matthew Wild <mwild1@gmail.com>
parents: 1497
diff changeset
52 local object = select(i, ...);
481dfc89047a util.xmlrpc: Support for multiple parameters in requests
Matthew Wild <mwild1@gmail.com>
parents: 1497
diff changeset
53 local h = map[type(object)];
481dfc89047a util.xmlrpc: Support for multiple parameters in requests
Matthew Wild <mwild1@gmail.com>
parents: 1497
diff changeset
54 if h then
481dfc89047a util.xmlrpc: Support for multiple parameters in requests
Matthew Wild <mwild1@gmail.com>
parents: 1497
diff changeset
55 h(stanza, object);
481dfc89047a util.xmlrpc: Support for multiple parameters in requests
Matthew Wild <mwild1@gmail.com>
parents: 1497
diff changeset
56 else
481dfc89047a util.xmlrpc: Support for multiple parameters in requests
Matthew Wild <mwild1@gmail.com>
parents: 1497
diff changeset
57 error("Type not supported by XML-RPC: " .. type(object));
481dfc89047a util.xmlrpc: Support for multiple parameters in requests
Matthew Wild <mwild1@gmail.com>
parents: 1497
diff changeset
58 end
481dfc89047a util.xmlrpc: Support for multiple parameters in requests
Matthew Wild <mwild1@gmail.com>
parents: 1497
diff changeset
59 stanza:up():up();
874
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
60 end
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
61 end
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
62 function create_response(object)
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
63 local stanza = st.stanza("methodResponse"):tag("params"):tag("param"):tag("value");
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
64 _lua_to_xmlrpc(stanza, object);
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
65 stanza:up():up():up();
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
66 return stanza;
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
67 end
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
68 function create_error_response(faultCode, faultString)
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
69 local stanza = st.stanza("methodResponse"):tag("fault"):tag("value");
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
70 _lua_to_xmlrpc(stanza, {faultCode=faultCode, faultString=faultString});
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
71 stanza:up():up();
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
72 return stanza;
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
73 end
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
74
1498
481dfc89047a util.xmlrpc: Support for multiple parameters in requests
Matthew Wild <mwild1@gmail.com>
parents: 1497
diff changeset
75 function create_request(method_name, ...)
1497
4c2c403ca04b util.xmlrpc: Add create_request
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
76 local stanza = st.stanza("methodCall")
4c2c403ca04b util.xmlrpc: Add create_request
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
77 :tag("methodName"):text(method_name):up()
1498
481dfc89047a util.xmlrpc: Support for multiple parameters in requests
Matthew Wild <mwild1@gmail.com>
parents: 1497
diff changeset
78 :tag("params");
481dfc89047a util.xmlrpc: Support for multiple parameters in requests
Matthew Wild <mwild1@gmail.com>
parents: 1497
diff changeset
79 _lua_to_xmlrpc(stanza, ...);
1497
4c2c403ca04b util.xmlrpc: Add create_request
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
80 stanza:up():up():up();
4c2c403ca04b util.xmlrpc: Add create_request
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
81 return stanza;
4c2c403ca04b util.xmlrpc: Add create_request
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
82 end
874
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
83
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
84 local _xmlrpc_to_lua;
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
85 local int_parse = function(stanza)
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
86 if #stanza.tags ~= 0 or #stanza == 0 then error("<"..stanza.name.."> must have a single text child"); end
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
87 local n = tonumber(t_concat(stanza));
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
88 if n then return n; end
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
89 error("Failed to parse content of <"..stanza.name..">");
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
90 end
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
91 local rmap = {
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
92 methodCall=function(stanza)
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
93 if #stanza.tags ~= 2 then error("<methodCall> must have exactly two subtags"); end -- FIXME <params> is optional
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
94 if stanza.tags[1].name ~= "methodName" then error("First <methodCall> child tag must be <methodName>") end
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
95 if stanza.tags[2].name ~= "params" then error("Second <methodCall> child tag must be <params>") end
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
96 return _xmlrpc_to_lua(stanza.tags[1]), _xmlrpc_to_lua(stanza.tags[2]);
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
97 end;
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
98 methodName=function(stanza)
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
99 if #stanza.tags ~= 0 then error("<methodName> must not have any subtags"); end
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
100 if #stanza == 0 then error("<methodName> must have text content"); end
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
101 return t_concat(stanza);
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
102 end;
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
103 params=function(stanza)
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
104 local t = {};
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
105 for _, child in pairs(stanza.tags) do
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
106 if child.name ~= "param" then error("<params> can only have <param> children"); end;
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
107 t_insert(t, _xmlrpc_to_lua(child));
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
108 end
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
109 return t;
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
110 end;
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
111 param=function(stanza)
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
112 if not(#stanza.tags == 1 and stanza.tags[1].name == "value") then error("<param> must have exactly one <value> child"); end
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
113 return _xmlrpc_to_lua(stanza.tags[1]);
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
114 end;
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
115 value=function(stanza)
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
116 if #stanza.tags == 0 then return t_concat(stanza); end
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
117 if #stanza.tags ~= 1 then error("<value> must have a single child"); end
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
118 return _xmlrpc_to_lua(stanza.tags[1]);
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
119 end;
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
120 int=int_parse;
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
121 i4=int_parse;
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
122 double=int_parse;
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
123 boolean=function(stanza)
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
124 if #stanza.tags ~= 0 or #stanza == 0 then error("<boolean> must have a single text child"); end
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
125 local b = t_concat(stanza);
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
126 if b ~= "1" and b ~= "0" then error("Failed to parse content of <boolean>"); end
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
127 return b == "1" and true or false;
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
128 end;
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
129 string=function(stanza)
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
130 if #stanza.tags ~= 0 then error("<string> must have a single text child"); end
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
131 return t_concat(stanza);
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
132 end;
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
133 array=function(stanza)
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
134 if #stanza.tags ~= 1 then error("<array> must have a single <data> child"); end
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
135 return _xmlrpc_to_lua(stanza.tags[1]);
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
136 end;
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
137 data=function(stanza)
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
138 local t = {};
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
139 for _,child in pairs(stanza.tags) do
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
140 if child.name ~= "value" then error("<data> can only have <value> children"); end
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
141 t_insert(t, _xmlrpc_to_lua(child));
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
142 end
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
143 return t;
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
144 end;
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
145 struct=function(stanza)
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
146 local t = {};
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
147 for _,child in pairs(stanza.tags) do
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
148 if child.name ~= "member" then error("<struct> can only have <member> children"); end
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
149 local name, value = _xmlrpc_to_lua(child);
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
150 t[name] = value;
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
151 end
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
152 return t;
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
153 end;
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
154 member=function(stanza)
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
155 if #stanza.tags ~= 2 then error("<member> must have exactly two subtags"); end -- FIXME <params> is optional
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
156 if stanza.tags[1].name ~= "name" then error("First <member> child tag must be <name>") end
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
157 if stanza.tags[2].name ~= "value" then error("Second <member> child tag must be <value>") end
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
158 return _xmlrpc_to_lua(stanza.tags[1]), _xmlrpc_to_lua(stanza.tags[2]);
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
159 end;
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
160 name=function(stanza)
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
161 if #stanza.tags ~= 0 then error("<name> must have a single text child"); end
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
162 local n = t_concat(stanza)
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
163 if tostring(tonumber(n)) == n then n = tonumber(n); end
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
164 return n;
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
165 end;
888
1059230969c3 util/xmlrpc: Added <nil/> extension to RPC functions (allowing nil parameters and return values)
Waqas Hussain <waqas20@gmail.com>
parents: 874
diff changeset
166 ["nil"]=function(stanza) -- nil extension
1059230969c3 util/xmlrpc: Added <nil/> extension to RPC functions (allowing nil parameters and return values)
Waqas Hussain <waqas20@gmail.com>
parents: 874
diff changeset
167 return nil;
1059230969c3 util/xmlrpc: Added <nil/> extension to RPC functions (allowing nil parameters and return values)
Waqas Hussain <waqas20@gmail.com>
parents: 874
diff changeset
168 end;
874
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
169 }
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
170 _xmlrpc_to_lua = function(stanza)
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
171 local h = rmap[stanza.name];
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
172 if h then
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
173 return h(stanza);
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
174 else
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
175 error("Unknown element: "..stanza.name);
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
176 end
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
177 end
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
178 function translate_request(stanza)
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
179 if stanza.name ~= "methodCall" then error("XML-RPC requests must have <methodCall> as root element"); end
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
180 return _xmlrpc_to_lua(stanza);
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
181 end
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
182
a554bf5057a0 Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
183 return _M;