Mercurial > prosody-modules
comparison mod_slack_webhooks/mod_slack_webhooks.lua @ 3443:284d8c18060e
mod_slack_webhooks: Fix use with 0.11+ MUC API
Copied from mod_muc_badge
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Tue, 15 Jan 2019 16:41:58 +0100 |
parents | 8c7b8b2c3237 |
children | 724003f24308 |
comparison
equal
deleted
inserted
replaced
3442:05725785e3a6 | 3443:284d8c18060e |
---|---|
4 -- | 4 -- |
5 -- This file is MIT/X11 licensed. | 5 -- This file is MIT/X11 licensed. |
6 | 6 |
7 module:depends"http" | 7 module:depends"http" |
8 | 8 |
9 local host_session = prosody.hosts[module.host]; | |
10 local msg = require "util.stanza".message; | 9 local msg = require "util.stanza".message; |
11 local jid = require "util.jid"; | 10 local jid = require "util.jid"; |
12 local now = require "util.datetime".datetime; | 11 local now = require "util.datetime".datetime; |
13 local json = require "util.json" | 12 local json = require "util.json" |
14 local formdecode = require "net.http".formdecode; | 13 local formdecode = require "net.http".formdecode; |
15 local http = require "net.http"; | 14 local http = require "net.http"; |
16 local dataform = require "util.dataforms"; | 15 local dataform = require "util.dataforms"; |
17 | 16 |
18 local function get_room_from_jid(mod_muc, room_jid) | 17 local mod_muc = module:depends"muc"; |
19 if mod_muc.get_room_from_jid then | 18 local rooms = rawget(mod_muc, "rooms"); |
20 return mod_muc.get_room_from_jid(room_jid); | 19 local get_room_from_jid = rawget(mod_muc, "get_room_from_jid") or |
21 elseif mod_muc.rooms then | 20 function (room_jid) |
22 return mod_muc.rooms[room_jid]; -- COMPAT 0.9, 0.10 | 21 return rooms[room_jid]; |
23 end | 22 end |
24 end | |
25 | 23 |
26 local button_ns = "xmpp:prosody.im/community/mod_slack_webhooks#buttons"; | 24 local button_ns = "xmpp:prosody.im/community/mod_slack_webhooks#buttons"; |
27 local routing = module:get_option("outgoing_webhook_routing") or {}; | 25 local routing = module:get_option("outgoing_webhook_routing") or {}; |
28 local listen_path = module:get_option("incoming_webhook_path") or "/webhook"; | 26 local listen_path = module:get_option("incoming_webhook_path") or "/webhook"; |
29 local default_from_nick = module:get_option("incoming_webhook_default_nick") or "Bot"; | 27 local default_from_nick = module:get_option("incoming_webhook_default_nick") or "Bot"; |
32 module:log("debug", "HTTP result %d", code) | 30 module:log("debug", "HTTP result %d", code) |
33 end | 31 end |
34 | 32 |
35 function check_message(data) | 33 function check_message(data) |
36 local stanza = data.stanza; | 34 local stanza = data.stanza; |
37 local mod_muc = host_session.muc; | 35 |
38 if not mod_muc then return; end | 36 local this_room = get_room_from_jid(stanza.attr.to); |
39 | |
40 local this_room = get_room_from_jid(mod_muc, stanza.attr.to); | |
41 if not this_room then return; end -- no such room | 37 if not this_room then return; end -- no such room |
42 | 38 |
43 local from_room_jid = this_room._jid_nick[stanza.attr.from]; | 39 local from_room_jid = this_room._jid_nick[stanza.attr.from]; |
44 if not from_room_jid then return; end -- no such nick | 40 if not from_room_jid then return; end -- no such nick |
45 | 41 |
103 module:hook("message/bare", check_message, 10); | 99 module:hook("message/bare", check_message, 10); |
104 | 100 |
105 local function route_post(f) | 101 local function route_post(f) |
106 return function(event, path) | 102 return function(event, path) |
107 local bare_room = jid.join(path, module.host); | 103 local bare_room = jid.join(path, module.host); |
108 local mod_muc = host_session.muc; | 104 if not get_room_from_jid(bare_room) then |
109 if not get_room_from_jid(mod_muc, bare_room) then | |
110 module:log("warn", "mod_slack_webhook: invalid JID: %s", bare_room); | 105 module:log("warn", "mod_slack_webhook: invalid JID: %s", bare_room); |
111 return 404; | 106 return 404; |
112 end | 107 end |
113 -- Check secret? | 108 -- Check secret? |
114 return f(event, path) | 109 return f(event, path) |
115 end | 110 end |
116 end | 111 end |
117 | 112 |
118 local function handle_post(event, path) | 113 local function handle_post(event, path) |
119 local mod_muc = host_session.muc; | |
120 local request = event.request; | 114 local request = event.request; |
121 local headers = request.headers; | 115 local headers = request.headers; |
122 | 116 |
123 local body_type = headers.content_type; | 117 local body_type = headers.content_type; |
124 local post_body; | 118 local post_body; |
131 end | 125 end |
132 else | 126 else |
133 return 422; | 127 return 422; |
134 end | 128 end |
135 local bare_room = jid.join(path, module.host); | 129 local bare_room = jid.join(path, module.host); |
136 local dest_room = get_room_from_jid(mod_muc, bare_room); | 130 local dest_room = get_room_from_jid(bare_room); |
137 local from_nick = default_from_nick; | 131 local from_nick = default_from_nick; |
138 if post_body["username"] then | 132 if post_body["username"] then |
139 from_nick = post_body["username"]; | 133 from_nick = post_body["username"]; |
140 end | 134 end |
141 local sender = jid.join(path, module.host, from_nick); | 135 local sender = jid.join(path, module.host, from_nick); |