Software /
code /
prosody
Annotate
util/xmlrpc.lua @ 908:b0ecd18e0558
net.connlisteners: Allow listeners to specify default interface
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sun, 22 Mar 2009 14:59:28 +0000 |
parent | 896:2c0b9e3c11c3 |
child | 1497:4c2c403ca04b |
rev | line source |
---|---|
896 | 1 -- Prosody IM v0.4 |
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; |
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; |
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
|
44 ["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
|
45 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
|
46 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
|
47 }; |
a554bf5057a0
Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
48 _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
|
49 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
|
50 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
|
51 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
|
52 else |
a554bf5057a0
Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
53 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
|
54 end |
a554bf5057a0
Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
55 end |
a554bf5057a0
Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
56 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
|
57 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
|
58 _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
|
59 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
|
60 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
|
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_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
|
63 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
|
64 _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
|
65 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
|
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 |
a554bf5057a0
Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
69 |
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 _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
|
71 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
|
72 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
|
73 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
|
74 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
|
75 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
|
76 end |
a554bf5057a0
Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
77 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
|
78 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
|
79 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
|
80 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
|
81 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
|
82 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
|
83 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 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
|
85 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
|
86 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
|
87 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
|
88 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 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
|
90 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
|
91 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
|
92 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
|
93 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
|
94 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 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
|
96 end; |
a554bf5057a0
Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
97 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
|
98 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
|
99 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
|
100 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 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
|
102 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
|
103 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
|
104 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
|
105 end; |
a554bf5057a0
Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
106 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
|
107 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
|
108 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
|
109 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
|
110 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
|
111 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
|
112 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
|
113 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
|
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 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
|
116 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
|
117 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
|
118 end; |
a554bf5057a0
Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
119 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
|
120 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
|
121 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
|
122 end; |
a554bf5057a0
Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
123 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
|
124 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
|
125 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
|
126 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
|
127 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
|
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 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
|
130 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 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
|
132 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
|
133 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
|
134 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
|
135 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
|
136 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
|
137 end |
a554bf5057a0
Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
138 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
|
139 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 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
|
141 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
|
142 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
|
143 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
|
144 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
|
145 end; |
a554bf5057a0
Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
146 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
|
147 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
|
148 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
|
149 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
|
150 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
|
151 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
|
152 ["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
|
153 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
|
154 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
|
155 } |
a554bf5057a0
Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
156 _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
|
157 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
|
158 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
|
159 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
|
160 else |
a554bf5057a0
Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
161 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
|
162 end |
a554bf5057a0
Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
163 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 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
|
165 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
|
166 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
|
167 end |
a554bf5057a0
Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
168 |
a554bf5057a0
Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
169 return _M; |