Mercurial > prosody-modules
comparison mod_component_http/mod_component_http.lua @ 2950:18e6d437003f
mod_component_http: Allow implementing a component over HTTP
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Mon, 26 Mar 2018 13:49:13 +0100 |
| parents | |
| children | e8462d6dbc6d |
comparison
equal
deleted
inserted
replaced
| 2949:39994c6bb314 | 2950:18e6d437003f |
|---|---|
| 1 local http = require "net.http"; | |
| 2 local json = require "util.json"; | |
| 3 local st = require "util.stanza"; | |
| 4 local xml = require "util.xml"; | |
| 5 local unpack = rawget(_G, "unpack") or table.unpack; | |
| 6 | |
| 7 local url = module:get_option_string("component_post_url"); | |
| 8 assert(url, "Missing required config option 'component_post_url'"); | |
| 9 | |
| 10 local stanza_kinds = module:get_option_set("post_stanza_types", { "message" }); | |
| 11 | |
| 12 local http_error_map = { | |
| 13 [0] = { "cancel", "remote-server-timeout", "Connection failure" }; | |
| 14 -- 4xx | |
| 15 [400] = { "modify", "bad-request" }; | |
| 16 [401] = { "auth", "not-authorized" }; | |
| 17 [402] = { "auth", "forbidden", "Payment required" }; | |
| 18 [403] = { "auth", "forbidden" }; | |
| 19 [404] = { "cancel", "item-not-found" }; | |
| 20 [410] = { "cancel", "gone" }; | |
| 21 -- 5xx | |
| 22 [500] = { "cancel", "internal-server-error" }; | |
| 23 [501] = { "cancel", "feature-not-implemented" }; | |
| 24 [502] = { "cancel", "remote-server-timeout", "Bad gateway" }; | |
| 25 [503] = { "wait", "remote-server-timeout", "Service temporarily unavailable" }; | |
| 26 [504] = { "wait", "remote-server-timeout", "Gateway timeout" }; | |
| 27 } | |
| 28 | |
| 29 local function error_reply(stanza, code) | |
| 30 local error = http_error_map[code] or { "cancel", "service-unavailable" }; | |
| 31 return st.error_reply(stanza, unpack(error, 1, 3)); | |
| 32 end | |
| 33 | |
| 34 function handle_stanza(event) | |
| 35 local origin, stanza = event.origin, event.stanza; | |
| 36 local request_body = json.encode({ | |
| 37 to = stanza.attr.to; | |
| 38 from = stanza.attr.from; | |
| 39 kind = stanza.name; | |
| 40 body = stanza.name == "message" and stanza:get_child_text("body") or nil; | |
| 41 stanza = tostring(stanza); | |
| 42 }); | |
| 43 http.request(url, { | |
| 44 body = request_body; | |
| 45 }, function (response_text, code, req, response) | |
| 46 if stanza.attr.type == "error" then return; end -- Avoid error loops, don't reply to error stanzas | |
| 47 if code == 200 and response_text and response.headers["content-type"] == "application/json" then | |
| 48 local response_data = json.decode(response_text); | |
| 49 if response_data.stanza then | |
| 50 local reply_stanza = xml.parse(response_data.stanza); | |
| 51 if reply_stanza then | |
| 52 reply_stanza.attr.from, reply_stanza.attr.to = stanza.attr.to, stanza.attr.from; | |
| 53 return origin.send(reply_stanza); | |
| 54 else | |
| 55 module:log("warn", "Unable to parse reply stanza"); | |
| 56 end | |
| 57 else | |
| 58 local stanza_kind = response_data.kind or "message"; | |
| 59 local to = response_data.to or stanza.attr.from; | |
| 60 local from = response_data.from or stanza.attr.to; | |
| 61 local reply_stanza = st.stanza(stanza_kind, { | |
| 62 to = to, from = from; | |
| 63 type = response_data.type or (stanza_kind == "message" and "chat") or nil; | |
| 64 }); | |
| 65 if stanza_kind == "message" and response_data.body then | |
| 66 reply_stanza:tag("body"):text(tostring(response_data.body)):up(); | |
| 67 end | |
| 68 module:log("debug", "Sending %s", tostring(reply_stanza)); | |
| 69 return origin.send(reply_stanza); | |
| 70 end | |
| 71 return; | |
| 72 elseif code >= 200 and code <= 299 then | |
| 73 return true; | |
| 74 else | |
| 75 return origin.send(error_reply(stanza, code)); | |
| 76 end | |
| 77 end); | |
| 78 return true; | |
| 79 end | |
| 80 | |
| 81 for stanza_kind in stanza_kinds do | |
| 82 for _, jid_type in ipairs({ "host", "bare", "full" }) do | |
| 83 module:hook(stanza_kind.."/"..jid_type, handle_stanza); | |
| 84 end | |
| 85 end | |
| 86 | |
| 87 -- Simple handler for an always-online JID that allows everyone to subscribe to presence | |
| 88 local function default_presence_handler(event) | |
| 89 local origin, stanza = event.origin, event.stanza; | |
| 90 module:log("debug", "Handling %s", tostring(stanza)); | |
| 91 if stanza.attr.type == "probe" then | |
| 92 module:send(st.presence({ to = stanza.attr.from, from = stanza.attr.to.."/default" })); | |
| 93 elseif stanza.attr.type == "subscribe" then | |
| 94 module:send(st.presence({ type = "subscribed", to = stanza.attr.from, from = stanza.attr.to.."/default" })); | |
| 95 module:send(st.presence({ to = stanza.attr.from, from = stanza.attr.to.."/default" })); | |
| 96 elseif stanza.attr.type == "unsubscribe" then | |
| 97 module:send(st.presence({ type = "unavailable", to = stanza.attr.from, from = stanza.attr.to.."/default" })); | |
| 98 end | |
| 99 return true; | |
| 100 end | |
| 101 | |
| 102 module:hook("presence/bare", default_presence_handler, -1); |
