comparison mod_post_msg/mod_post_msg.lua @ 216:ac5289d5ac8c

mod_post_msg: A plugin that recives a single message by HTTP POST and relays it.
author Kim Alvefur <zash@zash.se>
date Thu, 22 Jul 2010 18:47:15 +0200
parents
children b78009874ce5
comparison
equal deleted inserted replaced
215:281db5eefcb4 216:ac5289d5ac8c
1 -- Recive a HTTP POST and relay it
2 -- By Kim Alvefur <zash@zash.se>
3 -- Some code borrowed from mod_webpresence
4 --
5 -- Example usage:
6 -- curl http://example.com:5280/msg/user -u me@example.com:mypassword -d "Hello there"
7 -- This would send a message to user@example.com from me@example.com
8
9
10 local jid_split = require "util.jid".split;
11 local jid_prep = require "util.jid".prep;
12 local msg = require "util.stanza".message;
13 local test_password = require "core.usermanager".test_password;
14 local b64_decode = require "util.encodings".base64.decode;
15 local urldecode = require "net.http".urldecode;
16
17 local function http_response(code, message, extra_headers)
18 local response = {
19 status = code .. " " .. message;
20 body = message .. "\n"; }
21 if extra_headers then response.headers = extra_headers; end
22 return response
23 end
24
25 local function handle_request(method, body, request)
26 if request.method ~= "POST" then return http_response(405, "Method Not Allowed"); end
27
28 -- message to?
29 local path_jid = request.url.path:match("[^/]+$");
30 if not path_jid or not body then return http_response(400, "Bad Request"); end
31 local to_user, to_host = jid_split(urldecode(path_jid));
32 if to_host and not to_user and request.headers.host then
33 to_user, to_host = to_host, request.headers.host;
34 if to_host then to_host = to_host:gsub(":%d+$", ""); end
35 end
36 if not to_host or not to_user then return http_response(400, "Bad Request"); end
37 local to_jid = jid_prep(to_user .. "@" .. to_host)
38 if not to_jid then return http_response(400, "Bad Request"); end
39
40 -- message from?
41 if not request.headers["authorization"] then
42 return http_response(401, "Unauthorized",
43 {["WWW-Authenticate"]='Basic realm="WallyWorld"'})
44 end
45 local from_jid, password = b64_decode(request.headers.authorization
46 :gmatch("[^ ]*$")() or ""):gmatch("([^:]*):(.*)")();
47 from_jid = jid_prep(from_jid)
48 if not from_jid or not password then return http_response(400, "Bad Request"); end
49 local from_user, from_host = jid_split(from_jid)
50 if not hosts[from_host] then return http_response(401, "Unauthorized"); end
51
52 -- auth
53 module:log("debug", "testing authz %s", from_jid)
54 if not test_password(from_user, from_host, password) then
55 return http_response(401, "Unauthorized"); end
56
57 module:log("debug", "message for %s", to_jid)
58 core_post_stanza(hosts[module.host], msg({to=to_jid, from=from_jid, type="chat"})
59 :tag("body"):text(body))
60 return http_response(202, "Accepted")
61 end
62
63 local ports = config.get(module.host, "core", "post_msg_ports") or { 5280 };
64 require "net.httpserver".new_from_config(ports, "msg", handle_request);