Software /
code /
prosody
Annotate
util/xmlrpc.lua @ 4093:36555949bd16
util.datamanager: Use prosody.paths.data as the initial value for data_path
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Fri, 07 Jan 2011 11:55:19 +0000 |
parent | 2923:b7049746bd29 |
rev | line source |
---|---|
1523
841d61be198f
Remove version number from copyright headers
Matthew Wild <mwild1@gmail.com>
parents:
1498
diff
changeset
|
1 -- Prosody IM |
2923
b7049746bd29
Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents:
1554
diff
changeset
|
2 -- Copyright (C) 2008-2010 Matthew Wild |
b7049746bd29
Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents:
1554
diff
changeset
|
3 -- Copyright (C) 2008-2010 Waqas Hussain |
874
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 }; |
1554
06030af44fad
util.xmlrpc: Fixed table serialization (regression introduced in previous change)
Waqas Hussain <waqas20@gmail.com>
parents:
1523
diff
changeset
|
49 _lua_to_xmlrpc = function(stanza, object) |
06030af44fad
util.xmlrpc: Fixed table serialization (regression introduced in previous change)
Waqas Hussain <waqas20@gmail.com>
parents:
1523
diff
changeset
|
50 local h = map[type(object)]; |
06030af44fad
util.xmlrpc: Fixed table serialization (regression introduced in previous change)
Waqas Hussain <waqas20@gmail.com>
parents:
1523
diff
changeset
|
51 if h then |
06030af44fad
util.xmlrpc: Fixed table serialization (regression introduced in previous change)
Waqas Hussain <waqas20@gmail.com>
parents:
1523
diff
changeset
|
52 h(stanza, object); |
06030af44fad
util.xmlrpc: Fixed table serialization (regression introduced in previous change)
Waqas Hussain <waqas20@gmail.com>
parents:
1523
diff
changeset
|
53 else |
06030af44fad
util.xmlrpc: Fixed table serialization (regression introduced in previous change)
Waqas Hussain <waqas20@gmail.com>
parents:
1523
diff
changeset
|
54 error("Type not supported by XML-RPC: " .. type(object)); |
874
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 end |
a554bf5057a0
Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
57 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
|
58 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
|
59 _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
|
60 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
|
61 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
|
62 end |
a554bf5057a0
Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
63 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
|
64 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
|
65 _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
|
66 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
|
67 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
|
68 end |
a554bf5057a0
Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
69 |
1498
481dfc89047a
util.xmlrpc: Support for multiple parameters in requests
Matthew Wild <mwild1@gmail.com>
parents:
1497
diff
changeset
|
70 function create_request(method_name, ...) |
1497
4c2c403ca04b
util.xmlrpc: Add create_request
Matthew Wild <mwild1@gmail.com>
parents:
896
diff
changeset
|
71 local stanza = st.stanza("methodCall") |
4c2c403ca04b
util.xmlrpc: Add create_request
Matthew Wild <mwild1@gmail.com>
parents:
896
diff
changeset
|
72 :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
|
73 :tag("params"); |
1554
06030af44fad
util.xmlrpc: Fixed table serialization (regression introduced in previous change)
Waqas Hussain <waqas20@gmail.com>
parents:
1523
diff
changeset
|
74 for i=1,select('#', ...) do |
06030af44fad
util.xmlrpc: Fixed table serialization (regression introduced in previous change)
Waqas Hussain <waqas20@gmail.com>
parents:
1523
diff
changeset
|
75 stanza:tag("param"):tag("value"); |
06030af44fad
util.xmlrpc: Fixed table serialization (regression introduced in previous change)
Waqas Hussain <waqas20@gmail.com>
parents:
1523
diff
changeset
|
76 _lua_to_xmlrpc(stanza, select(i, ...)); |
06030af44fad
util.xmlrpc: Fixed table serialization (regression introduced in previous change)
Waqas Hussain <waqas20@gmail.com>
parents:
1523
diff
changeset
|
77 stanza:up():up(); |
06030af44fad
util.xmlrpc: Fixed table serialization (regression introduced in previous change)
Waqas Hussain <waqas20@gmail.com>
parents:
1523
diff
changeset
|
78 end |
1497
4c2c403ca04b
util.xmlrpc: Add create_request
Matthew Wild <mwild1@gmail.com>
parents:
896
diff
changeset
|
79 stanza:up():up():up(); |
4c2c403ca04b
util.xmlrpc: Add create_request
Matthew Wild <mwild1@gmail.com>
parents:
896
diff
changeset
|
80 return stanza; |
4c2c403ca04b
util.xmlrpc: Add create_request
Matthew Wild <mwild1@gmail.com>
parents:
896
diff
changeset
|
81 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
|
82 |
a554bf5057a0
Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
83 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
|
84 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
|
85 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
|
86 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
|
87 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
|
88 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
|
89 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 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
|
91 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
|
92 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
|
93 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
|
94 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
|
95 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
|
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 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
|
98 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
|
99 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
|
100 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
|
101 end; |
a554bf5057a0
Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
102 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
|
103 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
|
104 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
|
105 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
|
106 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
|
107 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 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
|
109 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 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
|
111 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
|
112 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
|
113 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 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
|
115 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
|
116 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
|
117 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
|
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 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
|
120 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
|
121 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
|
122 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
|
123 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
|
124 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
|
125 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
|
126 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
|
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 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
|
129 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
|
130 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
|
131 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 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
|
133 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
|
134 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
|
135 end; |
a554bf5057a0
Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
136 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
|
137 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
|
138 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
|
139 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
|
140 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
|
141 end |
a554bf5057a0
Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
142 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
|
143 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 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
|
145 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
|
146 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
|
147 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
|
148 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
|
149 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
|
150 end |
a554bf5057a0
Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
151 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
|
152 end; |
a554bf5057a0
Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
153 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
|
154 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
|
155 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
|
156 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
|
157 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
|
158 end; |
a554bf5057a0
Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
159 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
|
160 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
|
161 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
|
162 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
|
163 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
|
164 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
|
165 ["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
|
166 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
|
167 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
|
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 _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
|
170 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
|
171 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
|
172 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
|
173 else |
a554bf5057a0
Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
174 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
|
175 end |
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 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
|
178 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
|
179 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
|
180 end |
a554bf5057a0
Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
181 |
a554bf5057a0
Added util/xmlrpc.lua - functions for converting between XML-RPC stanzas and Lua objects
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
182 return _M; |