annotate mod_muc_rtbl/mod_muc_rtbl.lua @ 6299:5cf5ee23b361

mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs. diff --git a/mod_voipms/README.md b/mod_voipms/README.md new file mode 100644 --- /dev/null +++ b/mod_voipms/README.md @@ -0,0 +1,51 @@ +--- +labels: +- 'Stage-Alpha' +- 'Type-Web' +summary: Send and receive SMS/MMS via VoIP.ms APIs. +rockspec: + build: + modules: + mod_voipms: mod_voipms.lua +... + +Introduction +============ + +This is a Prosody module to map JIDs to DIDs on VoIP.ms and support sending/receiving SMS/MMS. + +Configuration +============= + +| option | type | default | +|-----------------------|--------|---------| +| voipms\_api\_username | string | nil | +| voipms\_api\_password | string | nil | +| voipms\_query\_key | string | nil | +| voipms\_jid\_map | table | nil | +``` +VirtualHost "sms.example.com" +modules_enabled = { + "voipms"; +} +voipms_api_username = john@example.com -- E-mail registered with VoIP.ms +voipms_api_password = abcd1234 -- API password configured in VoIP.ms +voipms_query_key = some_query_key -- query param 'key' part of your URL callback +voipms_jid_map = { + ["your_jid@your_domain.com"] = "+1234567890" +} +``` + +HTTP +==== + +The module is served on Prosody's default HTTP ports at the path /voipms. More details on configuring HTTP modules in Prosody can be found in the HTTP documentation. + +VoIP.ms Webhook URL +=================== + +This module receives the VoIP.ms Webhook URL (POST) at the /voipms endpoint. It uses the sendSMS/sendMMS GET methods against the VoIP.ms APIs. This is an example webhook to use in VoIP.ms: + +``` +https://sms.example.com/voipms?key=some_query_key +``` diff --git a/mod_voipms/mod_voipms.lua b/mod_voipms/mod_voipms.lua new file mode 100644 --- /dev/null +++ b/mod_voipms/mod_voipms.lua @@ -0,0 +1,166 @@ +local http = require "net.http" +local json = require "util.json" +local st = require "util.stanza" + +local api_username = module:get_option_string("voipms_api_username") +local api_password = module:get_option_string("voipms_api_password") +local query_key = module:get_option("voipms_query_key") +local jid_map = module:get_option("voipms_jid_map") or {} +local rest_endpoint = "https://voip.ms/api/v1/rest.php" + +if not api_username or not api_password or not query_key then + module:log("error", "Missing required config values (voipms_api_username, voipms_api_password, voipms_query_key)") + return +end + +module:depends("http") + +local function normalize_number(num) + if not num then return nil end + if num:sub(1, 1) ~= "+" then + return "+1" .. num + end + return num +end + +local function extract_query_key(event) + return (event.request.url.query or ""):match("key=([^&]+)") +end + +module:provides("http", { + route = { + ["POST"] = function(event) + local req = event.request + local body = req.body or "" + + if extract_query_key(event) ~= query_key then + module:log("warn", "Unauthorized webhook: missing or invalid key") + return { status_code = 403 } + end + + local json_payload, err = json.decode(body) + if not json_payload then + module:log("warn", "Invalid JSON: %s", err or "unknown error") + return { status_code = 400 } + end + + local payload = json_payload.data and json_payload.data.payload + if not payload then + module:log("warn", "Missing payload in JSON") + return { status_code = 400 } + end + + local from = payload.from and payload.from.phone_number + local to_list = payload.to or {} + local to = #to_list > 0 and to_list[1].phone_number or nil + + if not from or not to then + module:log("warn", "Missing phone numbers (from: %s, to: %s)", tostring(from), tostring(to)) + return { status_code = 400 } + end + + local normalized_to = normalize_number(to) + local target_jid = nil + + for jid, did in pairs(jid_map) do + if normalize_number(did) == normalized_to then + target_jid = jid + break + end + end + + if not target_jid then + module:log("warn", "No JID mapping for DID %s", normalized_to) + return { status_code = 404 } + end + + local normalized_from = normalize_number(from) + local message_text = payload.text or "" + + local message = st.message({ + from = normalized_from .. "@" .. module.host, + to = target_jid, + type = "chat" + }):tag("body"):text(message_text):up() + + if payload.media and #payload.media > 0 then + for _, media_item in ipairs(payload.media) do + if media_item.url then + message_text = message_text .. "\n" .. media_item.url + message:tag("x", { xmlns = "jabber:x:oob" }) + :tag("url"):text(media_item.url):up():up() + end + end + end + + module:send(message) + module:log("info", "Delivered SMS from %s to %s", normalized_from, target_jid) + + return { status_code = 204 } + end + } +}) + +module:hook("message/bare", function(event) + local stanza = event.stanza + if stanza.attr.type ~= "chat" then return end + + local from_jid = stanza.attr.from + local to_jid = stanza.attr.to + local body = stanza:get_child_text("body") + if not body or body == "" then return end + + local from_number = jid_map[from_jid] + if not from_number then + module:log("warn", "No DID mapping for JID %s", from_jid) + return + end + + local to_number = to_jid:match("^([^@]+)") + if not to_number then + module:log("warn", "Malformed JID in message.to: %s", to_jid) + return + end + + to_number = normalize_number(to_number) + + local media_urls = {} + for line in body:gmatch("[^\r\n]+") do + if line:match("^https?://") then + table.insert(media_urls, line) + end + end + + local method = (#media_urls > 0) and "sendMMS" or "sendSMS" + local query = { + api_username = api_username, + api_password = api_password, + method = method, + did = from_number, + dst = to_number, + message = body + } + + if method == "sendMMS" then + for i, url in ipairs(media_urls) do + query["media_url[" .. (i - 1) .. "]"] = url + end + end + + local query_str = http.formencode(query) + + http.request(rest_endpoint .. "?" .. query_str, { + method = "GET"; + }, function(response_body, code) + if code == 200 then + local resp, err = json.decode(response_body) + if not resp or resp.status ~= "success" then + module:log("error", "Failed to send %s: %s", method, err or (resp and resp.status) or "unknown") + else + module:log("info", "Sent %s from %s to %s", method, from_number, to_number) + end + else + module:log("error", "HTTP error sending %s: code %s", method, tostring(code)) + end + end) +end)
author Chaz <menel@snikket.de>
date Mon, 21 Jul 2025 23:57:37 +0200
parents ba71fdc8ea73
children 6e2458b69e33
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4808
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
1 local array = require "util.array";
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
2 local it = require "util.iterators";
4807
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 local jid = require "util.jid";
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 local sha256 = require "util.hashes".sha256;
4808
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
5 local set = require "util.set";
4807
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 local st = require "util.stanza";
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 local rtbl_service_jid = assert(module:get_option_string("muc_rtbl_jid"), "No RTBL JID supplied");
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 local rtbl_node = module:get_option_string("muc_rtbl_node", "muc_bans_sha256");
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 local banned_hashes = module:shared("banned_hashes");
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 module:depends("pubsub_subscription");
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 module:add_item("pubsub-subscription", {
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 service = rtbl_service_jid;
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 node = rtbl_node;
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 -- Callbacks:
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 on_subscribed = function()
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 module:log("info", "RTBL active");
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 end;
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 on_error = function(err)
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 module:log("error", "Failed to subscribe to RTBL: %s::%s: %s", err.type, err.condition, err.text);
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 end;
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 on_item = function(event)
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 local hash = event.item.attr.id;
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 if not hash then return; end
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 module:log("debug", "Received new hash: %s", hash);
4808
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
32 banned_hashes[hash] = true;
4807
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 end;
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 on_retract = function (event)
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 local hash = event.item.attr.id;
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 if not hash then return; end
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 module:log("debug", "Retracted hash: %s", hash);
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 banned_hashes[hash] = nil;
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 end;
5274
8cfbcbc0fb89 mod_muc_rtbl: Handle node purge
Kim Alvefur <zash@zash.se>
parents: 5177
diff changeset
41
8cfbcbc0fb89 mod_muc_rtbl: Handle node purge
Kim Alvefur <zash@zash.se>
parents: 5177
diff changeset
42 purge = function()
8cfbcbc0fb89 mod_muc_rtbl: Handle node purge
Kim Alvefur <zash@zash.se>
parents: 5177
diff changeset
43 module:log("debug", "Purge all hashes");
8cfbcbc0fb89 mod_muc_rtbl: Handle node purge
Kim Alvefur <zash@zash.se>
parents: 5177
diff changeset
44 for hash in pairs(banned_hashes) do
8cfbcbc0fb89 mod_muc_rtbl: Handle node purge
Kim Alvefur <zash@zash.se>
parents: 5177
diff changeset
45 banned_hashes[hash] = nil;
8cfbcbc0fb89 mod_muc_rtbl: Handle node purge
Kim Alvefur <zash@zash.se>
parents: 5177
diff changeset
46 end
8cfbcbc0fb89 mod_muc_rtbl: Handle node purge
Kim Alvefur <zash@zash.se>
parents: 5177
diff changeset
47 end;
4807
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 });
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49
4808
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
50 function request_list()
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
51 local items_request = st.iq({ to = rtbl_service_jid, from = module.host, type = "get", id = "rtbl-request" })
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
52 :tag("pubsub", { xmlns = "http://jabber.org/protocol/pubsub" })
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
53 :tag("items", { node = rtbl_node }):up()
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
54 :up();
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
55
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
56 module:send(items_request);
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
57 end
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
58
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
59 function update_list(event)
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
60 local from_jid = event.stanza.attr.from;
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
61 if from_jid ~= rtbl_service_jid then
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
62 module:log("debug", "Ignoring RTBL response from unknown sender");
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
63 return;
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
64 end
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
65 local items_el = event.stanza:find("{http://jabber.org/protocol/pubsub}pubsub/items");
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
66 if not items_el then
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
67 module:log("warn", "Invalid items response from RTBL service");
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
68 return;
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
69 end
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
70
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
71 local old_entries = set.new(array.collect(it.keys(banned_hashes)));
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
72
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
73 local n_added, n_removed, n_total = 0, 0, 0;
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
74 for item in items_el:childtags("item") do
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
75 local hash = item.attr.id;
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
76 if hash then
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
77 n_total = n_total + 1;
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
78 if not old_entries:contains(hash) then
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
79 -- New entry
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
80 n_added = n_added + 1;
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
81 banned_hashes[hash] = true;
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
82 else
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
83 -- Entry already existed
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
84 old_entries:remove(hash);
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
85 end
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
86 end
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
87 end
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
88
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
89 -- Remove old entries that weren't in the received list
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
90 for hash in old_entries do
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
91 n_removed = n_removed + 1;
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
92 banned_hashes[hash] = nil;
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
93 end
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
94
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
95 module:log("info", "%d RTBL entries received from %s (%d added, %d removed)", n_total, from_jid, n_added, n_removed);
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
96 return true;
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
97 end
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
98
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
99 module:hook("iq-result/host/rtbl-request", update_list);
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
100
5173
460f78654864 mod_muc_rtbl: also filter messages
Jonas Schäfer <jonas@wielicki.name>
parents: 4813
diff changeset
101 function update_hashes(occupant)
5174
354832098f2f mod_muc_rtbl: move use of "private" attributes to single function
Jonas Schäfer <jonas@wielicki.name>
parents: 5173
diff changeset
102 local bare_hash, host_hash;
5173
460f78654864 mod_muc_rtbl: also filter messages
Jonas Schäfer <jonas@wielicki.name>
parents: 4813
diff changeset
103 if not occupant.mod_muc_rtbl_bare_hash then
5175
432587ad1642 mod_muc_rtbl: fix traceback because of scoping error
Jonas Schäfer <jonas@wielicki.name>
parents: 5174
diff changeset
104 bare_hash = sha256(jid.bare(occupant.bare_jid), true);
5173
460f78654864 mod_muc_rtbl: also filter messages
Jonas Schäfer <jonas@wielicki.name>
parents: 4813
diff changeset
105 occupant.mod_muc_rtbl_bare_hash = bare_hash;
5174
354832098f2f mod_muc_rtbl: move use of "private" attributes to single function
Jonas Schäfer <jonas@wielicki.name>
parents: 5173
diff changeset
106 else
354832098f2f mod_muc_rtbl: move use of "private" attributes to single function
Jonas Schäfer <jonas@wielicki.name>
parents: 5173
diff changeset
107 bare_hash = occupant.mod_muc_rtbl_bare_hash;
5173
460f78654864 mod_muc_rtbl: also filter messages
Jonas Schäfer <jonas@wielicki.name>
parents: 4813
diff changeset
108 end
460f78654864 mod_muc_rtbl: also filter messages
Jonas Schäfer <jonas@wielicki.name>
parents: 4813
diff changeset
109 if not occupant.mod_muc_rtbl_host_hash then
5175
432587ad1642 mod_muc_rtbl: fix traceback because of scoping error
Jonas Schäfer <jonas@wielicki.name>
parents: 5174
diff changeset
110 host_hash = sha256(jid.host(occupant.bare_jid), true);
5177
f6b5f04d4b28 mod_muc_rtbl: fix more incorrect more references to "event"
Jonas Schäfer <jonas@wielicki.name>
parents: 5176
diff changeset
111 occupant.mod_muc_rtbl_host_hash = host_hash;
5174
354832098f2f mod_muc_rtbl: move use of "private" attributes to single function
Jonas Schäfer <jonas@wielicki.name>
parents: 5173
diff changeset
112 else
5177
f6b5f04d4b28 mod_muc_rtbl: fix more incorrect more references to "event"
Jonas Schäfer <jonas@wielicki.name>
parents: 5176
diff changeset
113 host_hash = occupant.mod_muc_rtbl_host_hash;
5173
460f78654864 mod_muc_rtbl: also filter messages
Jonas Schäfer <jonas@wielicki.name>
parents: 4813
diff changeset
114 end
5174
354832098f2f mod_muc_rtbl: move use of "private" attributes to single function
Jonas Schäfer <jonas@wielicki.name>
parents: 5173
diff changeset
115 return bare_hash, host_hash
5173
460f78654864 mod_muc_rtbl: also filter messages
Jonas Schäfer <jonas@wielicki.name>
parents: 4813
diff changeset
116 end
460f78654864 mod_muc_rtbl: also filter messages
Jonas Schäfer <jonas@wielicki.name>
parents: 4813
diff changeset
117
4807
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 module:hook("muc-occupant-pre-join", function (event)
4813
0a257d1402c3 mod_muc_rtbl: Optimize case with zero hashes
Kim Alvefur <zash@zash.se>
parents: 4812
diff changeset
119 if next(banned_hashes) == nil then return end
0a257d1402c3 mod_muc_rtbl: Optimize case with zero hashes
Kim Alvefur <zash@zash.se>
parents: 4812
diff changeset
120
4807
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 local from_bare = jid.bare(event.stanza.attr.from);
4810
181738ae4117 mod_muc_rtbl: Skip check if user has any explicit affiliation with the MUC
Matthew Wild <mwild1@gmail.com>
parents: 4809
diff changeset
122
181738ae4117 mod_muc_rtbl: Skip check if user has any explicit affiliation with the MUC
Matthew Wild <mwild1@gmail.com>
parents: 4809
diff changeset
123 local affiliation = event.room:get_affiliation(from_bare);
4811
a1fe59c06c48 mod_muc_rtbl: Fix typo in variable name in previous commit (thanks luacheck)
Matthew Wild <mwild1@gmail.com>
parents: 4810
diff changeset
124 if affiliation and affiliation ~= "none" then
4810
181738ae4117 mod_muc_rtbl: Skip check if user has any explicit affiliation with the MUC
Matthew Wild <mwild1@gmail.com>
parents: 4809
diff changeset
125 -- Skip check for affiliated users
181738ae4117 mod_muc_rtbl: Skip check if user has any explicit affiliation with the MUC
Matthew Wild <mwild1@gmail.com>
parents: 4809
diff changeset
126 return;
181738ae4117 mod_muc_rtbl: Skip check if user has any explicit affiliation with the MUC
Matthew Wild <mwild1@gmail.com>
parents: 4809
diff changeset
127 end
181738ae4117 mod_muc_rtbl: Skip check if user has any explicit affiliation with the MUC
Matthew Wild <mwild1@gmail.com>
parents: 4809
diff changeset
128
5174
354832098f2f mod_muc_rtbl: move use of "private" attributes to single function
Jonas Schäfer <jonas@wielicki.name>
parents: 5173
diff changeset
129 local bare_hash, host_hash = update_hashes(event.occupant);
354832098f2f mod_muc_rtbl: move use of "private" attributes to single function
Jonas Schäfer <jonas@wielicki.name>
parents: 5173
diff changeset
130 if banned_hashes[bare_hash] or banned_hashes[host_hash] then
4807
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131 module:log("info", "Blocked user <%s> from room <%s> due to RTBL match", from_bare, event.stanza.attr.to);
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132 local error_reply = st.error_reply(event.stanza, "cancel", "forbidden", "You are banned from this service", event.room.jid);
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 event.origin.send(error_reply);
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134 return true;
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135 end
62a65c52c3f5 mod_muc_rtbl: Real-time blocklist checks for MUC services
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136 end);
4808
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
137
5173
460f78654864 mod_muc_rtbl: also filter messages
Jonas Schäfer <jonas@wielicki.name>
parents: 4813
diff changeset
138 module:hook("muc-occupant-groupchat", function(event)
5176
4791e0412ff3 mod_muc_rtbl: ignore blocklist for affiliated users for messages
Jonas Schäfer <jonas@wielicki.name>
parents: 5175
diff changeset
139 local affiliation = event.room:get_affiliation(event.occupant.bare_jid);
4791e0412ff3 mod_muc_rtbl: ignore blocklist for affiliated users for messages
Jonas Schäfer <jonas@wielicki.name>
parents: 5175
diff changeset
140 if affiliation and affiliation ~= "none" then
4791e0412ff3 mod_muc_rtbl: ignore blocklist for affiliated users for messages
Jonas Schäfer <jonas@wielicki.name>
parents: 5175
diff changeset
141 -- Skip check for affiliated users
4791e0412ff3 mod_muc_rtbl: ignore blocklist for affiliated users for messages
Jonas Schäfer <jonas@wielicki.name>
parents: 5175
diff changeset
142 return;
4791e0412ff3 mod_muc_rtbl: ignore blocklist for affiliated users for messages
Jonas Schäfer <jonas@wielicki.name>
parents: 5175
diff changeset
143 end
4791e0412ff3 mod_muc_rtbl: ignore blocklist for affiliated users for messages
Jonas Schäfer <jonas@wielicki.name>
parents: 5175
diff changeset
144
5174
354832098f2f mod_muc_rtbl: move use of "private" attributes to single function
Jonas Schäfer <jonas@wielicki.name>
parents: 5173
diff changeset
145 local bare_hash, host_hash = update_hashes(event.occupant);
354832098f2f mod_muc_rtbl: move use of "private" attributes to single function
Jonas Schäfer <jonas@wielicki.name>
parents: 5173
diff changeset
146 if banned_hashes[bare_hash] or banned_hashes[host_hash] then
5173
460f78654864 mod_muc_rtbl: also filter messages
Jonas Schäfer <jonas@wielicki.name>
parents: 4813
diff changeset
147 module:log("debug", "Blocked message from user <%s> to room <%s> due to RTBL match", event.stanza.attr.from, event.stanza.attr.to);
460f78654864 mod_muc_rtbl: also filter messages
Jonas Schäfer <jonas@wielicki.name>
parents: 4813
diff changeset
148 local error_reply = st.error_reply(event.stanza, "cancel", "forbidden", "You are banned from this service", event.room.jid);
460f78654864 mod_muc_rtbl: also filter messages
Jonas Schäfer <jonas@wielicki.name>
parents: 4813
diff changeset
149 event.origin.send(error_reply);
460f78654864 mod_muc_rtbl: also filter messages
Jonas Schäfer <jonas@wielicki.name>
parents: 4813
diff changeset
150 return true;
460f78654864 mod_muc_rtbl: also filter messages
Jonas Schäfer <jonas@wielicki.name>
parents: 4813
diff changeset
151 end
460f78654864 mod_muc_rtbl: also filter messages
Jonas Schäfer <jonas@wielicki.name>
parents: 4813
diff changeset
152 end);
460f78654864 mod_muc_rtbl: also filter messages
Jonas Schäfer <jonas@wielicki.name>
parents: 4813
diff changeset
153
460f78654864 mod_muc_rtbl: also filter messages
Jonas Schäfer <jonas@wielicki.name>
parents: 4813
diff changeset
154 module:hook("muc-private-message", function(event)
460f78654864 mod_muc_rtbl: also filter messages
Jonas Schäfer <jonas@wielicki.name>
parents: 4813
diff changeset
155 local occupant = event.room:get_occupant_by_nick(event.stanza.attr.from);
5352
f6577cdb1d91 mod_muc_rtbl: Use correct occupant object
Matthew Wild <mwild1@gmail.com>
parents: 5274
diff changeset
156 local affiliation = event.room:get_affiliation(occupant.bare_jid);
5176
4791e0412ff3 mod_muc_rtbl: ignore blocklist for affiliated users for messages
Jonas Schäfer <jonas@wielicki.name>
parents: 5175
diff changeset
157 if affiliation and affiliation ~= "none" then
4791e0412ff3 mod_muc_rtbl: ignore blocklist for affiliated users for messages
Jonas Schäfer <jonas@wielicki.name>
parents: 5175
diff changeset
158 -- Skip check for affiliated users
4791e0412ff3 mod_muc_rtbl: ignore blocklist for affiliated users for messages
Jonas Schäfer <jonas@wielicki.name>
parents: 5175
diff changeset
159 return;
4791e0412ff3 mod_muc_rtbl: ignore blocklist for affiliated users for messages
Jonas Schäfer <jonas@wielicki.name>
parents: 5175
diff changeset
160 end
4791e0412ff3 mod_muc_rtbl: ignore blocklist for affiliated users for messages
Jonas Schäfer <jonas@wielicki.name>
parents: 5175
diff changeset
161
5174
354832098f2f mod_muc_rtbl: move use of "private" attributes to single function
Jonas Schäfer <jonas@wielicki.name>
parents: 5173
diff changeset
162 local bare_hash, host_hash = update_hashes(occupant);
354832098f2f mod_muc_rtbl: move use of "private" attributes to single function
Jonas Schäfer <jonas@wielicki.name>
parents: 5173
diff changeset
163 if banned_hashes[bare_hash] or banned_hashes[host_hash] then
5173
460f78654864 mod_muc_rtbl: also filter messages
Jonas Schäfer <jonas@wielicki.name>
parents: 4813
diff changeset
164 module:log("debug", "Blocked private message from user <%s> from room <%s> due to RTBL match", occupant.bare_jid, event.stanza.attr.to);
460f78654864 mod_muc_rtbl: also filter messages
Jonas Schäfer <jonas@wielicki.name>
parents: 4813
diff changeset
165 local error_reply = st.error_reply(event.stanza, "cancel", "forbidden", "You are banned from this service", event.room.jid);
460f78654864 mod_muc_rtbl: also filter messages
Jonas Schäfer <jonas@wielicki.name>
parents: 4813
diff changeset
166 event.origin.send(error_reply);
5890
ba71fdc8ea73 mod_muc_rtbl: Fix blocking of PMs from RTBL matches
Matthew Wild <mwild1@gmail.com>
parents: 5352
diff changeset
167 return false; -- Don't route it
5173
460f78654864 mod_muc_rtbl: also filter messages
Jonas Schäfer <jonas@wielicki.name>
parents: 4813
diff changeset
168 end
460f78654864 mod_muc_rtbl: also filter messages
Jonas Schäfer <jonas@wielicki.name>
parents: 4813
diff changeset
169 end);
460f78654864 mod_muc_rtbl: also filter messages
Jonas Schäfer <jonas@wielicki.name>
parents: 4813
diff changeset
170
4808
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
171 if prosody.start_time then
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
172 request_list();
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
173 else
4809
9e9ec0f0b128 mod_muc_rtbl: Fix to hook server-started globally, to fetch entries at startup (thanks mirux)
Matthew Wild <mwild1@gmail.com>
parents: 4808
diff changeset
174 module:hook_global("server-started", function ()
9e9ec0f0b128 mod_muc_rtbl: Fix to hook server-started globally, to fetch entries at startup (thanks mirux)
Matthew Wild <mwild1@gmail.com>
parents: 4808
diff changeset
175 request_list();
9e9ec0f0b128 mod_muc_rtbl: Fix to hook server-started globally, to fetch entries at startup (thanks mirux)
Matthew Wild <mwild1@gmail.com>
parents: 4808
diff changeset
176 end);
4808
8a63a0daf129 mod_muc_rtbl: Sync existing list entries when first loaded
Matthew Wild <mwild1@gmail.com>
parents: 4807
diff changeset
177 end