Annotate

plugins/mod_xmlrpc.lua @ 875:e73b6ef579c7

Added mod_xmlrpc
author Waqas Hussain <waqas20@gmail.com>
date Wed, 04 Mar 2009 22:12:52 +0500
child 877:0bababc930dd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
875
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
1 -- Prosody IM v0.3
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
2 -- Copyright (C) 2008-2009 Matthew Wild
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
3 -- Copyright (C) 2008-2009 Waqas Hussain
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
4 --
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
6 -- COPYING file in the source package for more information.
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
7 --
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
8
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
9
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
10 module.host = "*" -- Global module
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
11
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
12 local httpserver = require "net.httpserver";
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
13 local st = require "util.stanza";
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
14 local pcall = pcall;
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
15 local unpack = unpack;
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
16 local tostring = tostring;
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
17
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
18 local translate_request = require "util.xmlrpc".translate_request;
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
19 local create_response = require "util.xmlrpc".create_response;
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
20 local create_error_response = require "util.xmlrpc".create_error_response;
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
21
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
22 local entity_map = setmetatable({
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
23 ["amp"] = "&";
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
24 ["gt"] = ">";
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
25 ["lt"] = "<";
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
26 ["apos"] = "'";
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
27 ["quot"] = "\"";
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
28 }, {__index = function(_, s)
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
29 if s:sub(1,1) == "#" then
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
30 if s:sub(2,2) == "x" then
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
31 return string.char(tonumber(s:sub(3), 16));
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
32 else
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
33 return string.char(tonumber(s:sub(2)));
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
34 end
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
35 end
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
36 end
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
37 });
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
38 local function xml_unescape(str)
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
39 return (str:gsub("&(.-);", entity_map));
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
40 end
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
41 local function parse_xml(xml)
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
42 local stanza = st.stanza("root");
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
43 local regexp = "<([^>]*)>([^<]*)";
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
44 for elem, text in xml:gmatch(regexp) do
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
45 --print("[<"..elem..">|"..text.."]");
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
46 if elem:sub(1,1) == "!" or elem:sub(1,1) == "?" then -- neglect comments and processing-instructions
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
47 elseif elem:sub(1,1) == "/" then -- end tag
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
48 elem = elem:sub(2);
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
49 stanza:up(); -- TODO check for start-end tag name match
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
50 elseif elem:sub(-1,-1) == "/" then -- empty tag
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
51 elem = elem:sub(1,-2);
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
52 stanza:tag(elem):up();
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
53 else -- start tag
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
54 stanza:tag(elem);
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
55 end
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
56 if #text ~= 0 then -- text
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
57 stanza:text(xml_unescape(text));
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
58 end
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
59 end
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
60 return stanza.tags[1];
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
61 end
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
62
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
63 local function get_method(method)
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
64 return function(...)
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
65 return {method = method; args = {...}};
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
66 end
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
67 end
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
68
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
69 local function handle_xmlrpc_request(method, args)
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
70 method = get_method(method);
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
71 if not method then return create_error_response(404, "method not found"); end
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
72 args = args or {};
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
73 local success, result = pcall(method, unpack(args));
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
74 if success then
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
75 success, result = pcall(create_response, result or "nil");
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
76 if success then
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
77 return result;
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
78 end
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
79 return create_error_response(500, "Error in creating response: "..result);
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
80 end
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
81 return create_error_response(0, result or "nil");
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
82 end
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
83
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
84 --[[local function handle_xmpp_request(origin, stanza)
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
85 local query = stanza.tags[1];
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
86 if query.name == "query" then
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
87 if #query.tags == 1 then
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
88 local success, method, args = pcall(translate_request, query.tags[1]);
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
89 if success then
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
90 local result = handle_xmlrpc_request(method, args);
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
91 origin.send(st.reply(stanza):tag('query', {xmlns='jabber:iq:rpc'}):add_child(result));
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
92 else
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
93 origin.send(st.error_reply(stanza, "modify", "bad-request", method));
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
94 end
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
95 else
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
96 origin.send(st.error_reply(stanza, "modify", "bad-request", "No content in XML-RPC request"));
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
97 end
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
98 else
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
99 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
100 end
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
101 end
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
102 module:add_iq_handler({"c2s", "s2sin"}, "jabber:iq:rpc", handle_xmpp_request);]]
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
103
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
104 local function handle_http_request(method, body, request)
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
105 local stanza = body and parse_xml(body);
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
106 if (not stanza) or request.method ~= "POST" then
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
107 return "<html><body>You really don't look like an XML-RPC client to me... what do you want?</body></html>";
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
108 end
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
109 local success, method, args = pcall(translate_request, stanza);
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
110 if success then
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
111 return tostring(handle_xmlrpc_request(method, args));
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
112 end
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
113 return "<html><body>You really don't look like an XML-RPC client to me... what do you want?</body></html>";
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
114 end
e73b6ef579c7 Added mod_xmlrpc
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
115 httpserver.new{ port = 9000, base = "xmlrpc", handler = handle_http_request }