annotate mod_post_msg/mod_post_msg.lua @ 225:95d4d53f09a1

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