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