Software /
code /
prosody
Annotate
util/xmlrpc.lua @ 874:a554bf5057a0
Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Wed, 04 Mar 2009 22:10:27 +0500 |
child | 888:1059230969c3 |
rev | line source |
---|---|
874
a554bf5057a0
Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
1 -- Prosody IM v0.3 |
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; |
a554bf5057a0
Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
17 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
|
18 |
a554bf5057a0
Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
19 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
|
20 |
a554bf5057a0
Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
21 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
|
22 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
|
23 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
|
24 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
|
25 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
|
26 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
|
27 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
|
28 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
|
29 _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
|
30 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
|
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 end |
a554bf5057a0
Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
33 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
|
34 end; |
a554bf5057a0
Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
35 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
|
36 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
|
37 end; |
a554bf5057a0
Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
38 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
|
39 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
|
40 end; |
a554bf5057a0
Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
41 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
|
42 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
|
43 end; |
a554bf5057a0
Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
44 }; |
a554bf5057a0
Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
45 _lua_to_xmlrpc = 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
|
46 local h = map[type(object)]; |
a554bf5057a0
Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
47 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
|
48 h(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
|
49 else |
a554bf5057a0
Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
50 error("Type not supported by XML-RPC: " .. type(object)); |
a554bf5057a0
Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
51 end |
a554bf5057a0
Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
52 end |
a554bf5057a0
Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
53 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
|
54 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
|
55 _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
|
56 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
|
57 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
|
58 end |
a554bf5057a0
Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
59 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
|
60 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
|
61 _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
|
62 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
|
63 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
|
64 end |
a554bf5057a0
Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
65 |
a554bf5057a0
Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
66 |
a554bf5057a0
Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
67 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
|
68 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
|
69 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
|
70 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
|
71 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
|
72 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
|
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 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
|
75 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
|
76 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
|
77 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
|
78 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
|
79 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
|
80 end; |
a554bf5057a0
Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
81 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
|
82 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
|
83 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
|
84 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
|
85 end; |
a554bf5057a0
Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
86 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
|
87 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
|
88 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
|
89 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
|
90 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
|
91 end |
a554bf5057a0
Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
92 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
|
93 end; |
a554bf5057a0
Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
94 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
|
95 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
|
96 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
|
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 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
|
99 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
|
100 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
|
101 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
|
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 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
|
104 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
|
105 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
|
106 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
|
107 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
|
108 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
|
109 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
|
110 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
|
111 end; |
a554bf5057a0
Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
112 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
|
113 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
|
114 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
|
115 end; |
a554bf5057a0
Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
116 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
|
117 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
|
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 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
|
121 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
|
122 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
|
123 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
|
124 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
|
125 end |
a554bf5057a0
Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
126 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
|
127 end; |
a554bf5057a0
Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
128 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
|
129 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
|
130 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
|
131 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
|
132 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
|
133 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
|
134 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 t; |
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 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
|
138 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
|
139 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
|
140 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
|
141 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
|
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 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
|
144 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
|
145 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
|
146 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
|
147 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
|
148 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 } |
a554bf5057a0
Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
150 _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
|
151 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
|
152 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
|
153 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
|
154 else |
a554bf5057a0
Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
155 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
|
156 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 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 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
|
159 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
|
160 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
|
161 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 |
a554bf5057a0
Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
163 return _M; |