annotate mod_blocking/mod_blocking.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 1856c3aae92d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
174
d40982d130f0 mod_blocking: Various small changes to make it actually work, which I forgot to commit
Matthew Wild <mwild1@gmail.com>
parents: 173
diff changeset
1 local jid_split = require "util.jid".split;
d40982d130f0 mod_blocking: Various small changes to make it actually work, which I forgot to commit
Matthew Wild <mwild1@gmail.com>
parents: 173
diff changeset
2 local st = require "util.stanza";
1796
1e9a06caa866 mod_blocking: Import datamanager (thanks kriztan)
Kim Alvefur <zash@zash.se>
parents: 1215
diff changeset
3 local datamanager = require"util.datamanager";
174
d40982d130f0 mod_blocking: Various small changes to make it actually work, which I forgot to commit
Matthew Wild <mwild1@gmail.com>
parents: 173
diff changeset
4
d40982d130f0 mod_blocking: Various small changes to make it actually work, which I forgot to commit
Matthew Wild <mwild1@gmail.com>
parents: 173
diff changeset
5 local xmlns_blocking = "urn:xmpp:blocking";
d40982d130f0 mod_blocking: Various small changes to make it actually work, which I forgot to commit
Matthew Wild <mwild1@gmail.com>
parents: 173
diff changeset
6
161
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 module:add_feature("urn:xmpp:blocking");
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 -- Add JID to default privacy list
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 function add_blocked_jid(username, host, jid)
372
000f1d1c6ca5 mod_blocking: Properly initialize the bootstrap privacy storage
Paul Aurich <paul@darkrain42.org>
parents: 262
diff changeset
11 local privacy_lists = datamanager.load(username, host, "privacy") or {lists = {}};
161
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 local default_list_name = privacy_lists.default;
1215
1b55d8f86644 mod_blocking: Make sure that there is a lists item in the privacy store
Kim Alvefur <zash@zash.se>
parents: 940
diff changeset
13 if not privacy_lists.lists then
1b55d8f86644 mod_blocking: Make sure that there is a lists item in the privacy store
Kim Alvefur <zash@zash.se>
parents: 940
diff changeset
14 privacy_lists.lists = {}
1b55d8f86644 mod_blocking: Make sure that there is a lists item in the privacy store
Kim Alvefur <zash@zash.se>
parents: 940
diff changeset
15 end
161
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 if not default_list_name then
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 default_list_name = "blocklist";
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 privacy_lists.default = default_list_name;
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 end
174
d40982d130f0 mod_blocking: Various small changes to make it actually work, which I forgot to commit
Matthew Wild <mwild1@gmail.com>
parents: 173
diff changeset
20 local default_list = privacy_lists.lists[default_list_name];
161
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 if not default_list then
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 default_list = { name = default_list_name, items = {} };
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 privacy_lists.lists[default_list_name] = default_list;
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 end
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 local items = default_list.items;
174
d40982d130f0 mod_blocking: Various small changes to make it actually work, which I forgot to commit
Matthew Wild <mwild1@gmail.com>
parents: 173
diff changeset
26 local order = items[1] and items[1].order or 0; -- Must come first
161
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 for i=1,#items do -- order must be unique
176
26bb69a57749 mod_blocking: Ensure that a JID can be in the blocklist at most once, and have helper functions return true/false on success/error
Matthew Wild <mwild1@gmail.com>
parents: 175
diff changeset
28 local item = items[i];
26bb69a57749 mod_blocking: Ensure that a JID can be in the blocklist at most once, and have helper functions return true/false on success/error
Matthew Wild <mwild1@gmail.com>
parents: 175
diff changeset
29 item.order = item.order + 1;
26bb69a57749 mod_blocking: Ensure that a JID can be in the blocklist at most once, and have helper functions return true/false on success/error
Matthew Wild <mwild1@gmail.com>
parents: 175
diff changeset
30 if item.type == "jid" and item.action == "deny" and item.value == jid then
26bb69a57749 mod_blocking: Ensure that a JID can be in the blocklist at most once, and have helper functions return true/false on success/error
Matthew Wild <mwild1@gmail.com>
parents: 175
diff changeset
31 return false;
26bb69a57749 mod_blocking: Ensure that a JID can be in the blocklist at most once, and have helper functions return true/false on success/error
Matthew Wild <mwild1@gmail.com>
parents: 175
diff changeset
32 end
161
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 end
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 table.insert(items, 1, { type = "jid"
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 , action = "deny"
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 , value = jid
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 , message = false
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 , ["presence-out"] = false
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 , ["presence-in"] = false
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 , iq = false
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 , order = order
173
4f58ddade1db mod_blocking: Fixed a syntax error.
Waqas Hussain <waqas20@gmail.com>
parents: 164
diff changeset
42 });
161
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 datamanager.store(username, host, "privacy", privacy_lists);
176
26bb69a57749 mod_blocking: Ensure that a JID can be in the blocklist at most once, and have helper functions return true/false on success/error
Matthew Wild <mwild1@gmail.com>
parents: 175
diff changeset
44 return true;
161
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 end
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 -- Remove JID from default privacy list
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 function remove_blocked_jid(username, host, jid)
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 local privacy_lists = datamanager.load(username, host, "privacy") or {};
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 local default_list_name = privacy_lists.default;
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 if not default_list_name then return; end
174
d40982d130f0 mod_blocking: Various small changes to make it actually work, which I forgot to commit
Matthew Wild <mwild1@gmail.com>
parents: 173
diff changeset
52 local default_list = privacy_lists.lists[default_list_name];
161
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 if not default_list then return; end
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 local items = default_list.items;
176
26bb69a57749 mod_blocking: Ensure that a JID can be in the blocklist at most once, and have helper functions return true/false on success/error
Matthew Wild <mwild1@gmail.com>
parents: 175
diff changeset
55 local item, removed = nil, false;
161
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 for i=1,#items do -- order must be unique
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 item = items[i];
163
9fe6d314fd07 mod_blocking: Only count rules with action == "deny" as blocked JIDs
Matthew Wild <mwild1@gmail.com>
parents: 161
diff changeset
58 if item.type == "jid" and item.action == "deny" and item.value == jid then
161
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 table.remove(items, i);
176
26bb69a57749 mod_blocking: Ensure that a JID can be in the blocklist at most once, and have helper functions return true/false on success/error
Matthew Wild <mwild1@gmail.com>
parents: 175
diff changeset
60 removed = true;
26bb69a57749 mod_blocking: Ensure that a JID can be in the blocklist at most once, and have helper functions return true/false on success/error
Matthew Wild <mwild1@gmail.com>
parents: 175
diff changeset
61 break;
161
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 end
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 end
176
26bb69a57749 mod_blocking: Ensure that a JID can be in the blocklist at most once, and have helper functions return true/false on success/error
Matthew Wild <mwild1@gmail.com>
parents: 175
diff changeset
64 if removed then
26bb69a57749 mod_blocking: Ensure that a JID can be in the blocklist at most once, and have helper functions return true/false on success/error
Matthew Wild <mwild1@gmail.com>
parents: 175
diff changeset
65 datamanager.store(username, host, "privacy", privacy_lists);
26bb69a57749 mod_blocking: Ensure that a JID can be in the blocklist at most once, and have helper functions return true/false on success/error
Matthew Wild <mwild1@gmail.com>
parents: 175
diff changeset
66 end
26bb69a57749 mod_blocking: Ensure that a JID can be in the blocklist at most once, and have helper functions return true/false on success/error
Matthew Wild <mwild1@gmail.com>
parents: 175
diff changeset
67 return removed;
164
0b238b2b0801 mod_blocking: Support for the "unblock all JIDs" case, and fix saving of rules after removing a JID
Matthew Wild <mwild1@gmail.com>
parents: 163
diff changeset
68 end
0b238b2b0801 mod_blocking: Support for the "unblock all JIDs" case, and fix saving of rules after removing a JID
Matthew Wild <mwild1@gmail.com>
parents: 163
diff changeset
69
0b238b2b0801 mod_blocking: Support for the "unblock all JIDs" case, and fix saving of rules after removing a JID
Matthew Wild <mwild1@gmail.com>
parents: 163
diff changeset
70 function remove_all_blocked_jids(username, host)
0b238b2b0801 mod_blocking: Support for the "unblock all JIDs" case, and fix saving of rules after removing a JID
Matthew Wild <mwild1@gmail.com>
parents: 163
diff changeset
71 local privacy_lists = datamanager.load(username, host, "privacy") or {};
0b238b2b0801 mod_blocking: Support for the "unblock all JIDs" case, and fix saving of rules after removing a JID
Matthew Wild <mwild1@gmail.com>
parents: 163
diff changeset
72 local default_list_name = privacy_lists.default;
0b238b2b0801 mod_blocking: Support for the "unblock all JIDs" case, and fix saving of rules after removing a JID
Matthew Wild <mwild1@gmail.com>
parents: 163
diff changeset
73 if not default_list_name then return; end
174
d40982d130f0 mod_blocking: Various small changes to make it actually work, which I forgot to commit
Matthew Wild <mwild1@gmail.com>
parents: 173
diff changeset
74 local default_list = privacy_lists.lists[default_list_name];
164
0b238b2b0801 mod_blocking: Support for the "unblock all JIDs" case, and fix saving of rules after removing a JID
Matthew Wild <mwild1@gmail.com>
parents: 163
diff changeset
75 if not default_list then return; end
0b238b2b0801 mod_blocking: Support for the "unblock all JIDs" case, and fix saving of rules after removing a JID
Matthew Wild <mwild1@gmail.com>
parents: 163
diff changeset
76 local items = default_list.items;
0b238b2b0801 mod_blocking: Support for the "unblock all JIDs" case, and fix saving of rules after removing a JID
Matthew Wild <mwild1@gmail.com>
parents: 163
diff changeset
77 local item;
177
bcd7dc51a5e3 mod_blocking: Fix to iterate over blocklist correctly when removing a JID
Matthew Wild <mwild1@gmail.com>
parents: 176
diff changeset
78 for i=#items,1,-1 do -- order must be unique
164
0b238b2b0801 mod_blocking: Support for the "unblock all JIDs" case, and fix saving of rules after removing a JID
Matthew Wild <mwild1@gmail.com>
parents: 163
diff changeset
79 item = items[i];
0b238b2b0801 mod_blocking: Support for the "unblock all JIDs" case, and fix saving of rules after removing a JID
Matthew Wild <mwild1@gmail.com>
parents: 163
diff changeset
80 if item.type == "jid" and item.action == "deny" then
0b238b2b0801 mod_blocking: Support for the "unblock all JIDs" case, and fix saving of rules after removing a JID
Matthew Wild <mwild1@gmail.com>
parents: 163
diff changeset
81 table.remove(items, i);
0b238b2b0801 mod_blocking: Support for the "unblock all JIDs" case, and fix saving of rules after removing a JID
Matthew Wild <mwild1@gmail.com>
parents: 163
diff changeset
82 end
0b238b2b0801 mod_blocking: Support for the "unblock all JIDs" case, and fix saving of rules after removing a JID
Matthew Wild <mwild1@gmail.com>
parents: 163
diff changeset
83 end
0b238b2b0801 mod_blocking: Support for the "unblock all JIDs" case, and fix saving of rules after removing a JID
Matthew Wild <mwild1@gmail.com>
parents: 163
diff changeset
84 datamanager.store(username, host, "privacy", privacy_lists);
176
26bb69a57749 mod_blocking: Ensure that a JID can be in the blocklist at most once, and have helper functions return true/false on success/error
Matthew Wild <mwild1@gmail.com>
parents: 175
diff changeset
85 return true;
161
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 end
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 function get_blocked_jids(username, host)
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 -- Return array of blocked JIDs in default privacy list
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 local privacy_lists = datamanager.load(username, host, "privacy") or {};
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 local default_list_name = privacy_lists.default;
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 if not default_list_name then return {}; end
174
d40982d130f0 mod_blocking: Various small changes to make it actually work, which I forgot to commit
Matthew Wild <mwild1@gmail.com>
parents: 173
diff changeset
93 local default_list = privacy_lists.lists[default_list_name];
161
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 if not default_list then return {}; end
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 local items = default_list.items;
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 local item;
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 local jid_list = {};
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 for i=1,#items do -- order must be unique
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 item = items[i];
163
9fe6d314fd07 mod_blocking: Only count rules with action == "deny" as blocked JIDs
Matthew Wild <mwild1@gmail.com>
parents: 161
diff changeset
100 if item.type == "jid" and item.action == "deny" then
161
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 jid_list[#jid_list+1] = item.value;
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 end
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 end
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 return jid_list;
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 end
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106
940
80ede103d7a3 mod_blocking: Fix handling of unblocking command. Send out un-/block pushes to all resources.
Tobias Markmann <tm@ayena.de>
parents: 372
diff changeset
107 local function send_push_iqs(username, host, command_type, jids)
80ede103d7a3 mod_blocking: Fix handling of unblocking command. Send out un-/block pushes to all resources.
Tobias Markmann <tm@ayena.de>
parents: 372
diff changeset
108 local bare_jid = username.."@"..host;
80ede103d7a3 mod_blocking: Fix handling of unblocking command. Send out un-/block pushes to all resources.
Tobias Markmann <tm@ayena.de>
parents: 372
diff changeset
109
80ede103d7a3 mod_blocking: Fix handling of unblocking command. Send out un-/block pushes to all resources.
Tobias Markmann <tm@ayena.de>
parents: 372
diff changeset
110 local stanza_content = st.stanza(command_type, { xmlns = xmlns_blocking });
80ede103d7a3 mod_blocking: Fix handling of unblocking command. Send out un-/block pushes to all resources.
Tobias Markmann <tm@ayena.de>
parents: 372
diff changeset
111 for _, jid in ipairs(jids) do
80ede103d7a3 mod_blocking: Fix handling of unblocking command. Send out un-/block pushes to all resources.
Tobias Markmann <tm@ayena.de>
parents: 372
diff changeset
112 stanza_content:tag("item", { jid = jid }):up();
80ede103d7a3 mod_blocking: Fix handling of unblocking command. Send out un-/block pushes to all resources.
Tobias Markmann <tm@ayena.de>
parents: 372
diff changeset
113 end
80ede103d7a3 mod_blocking: Fix handling of unblocking command. Send out un-/block pushes to all resources.
Tobias Markmann <tm@ayena.de>
parents: 372
diff changeset
114
80ede103d7a3 mod_blocking: Fix handling of unblocking command. Send out un-/block pushes to all resources.
Tobias Markmann <tm@ayena.de>
parents: 372
diff changeset
115 for resource, session in pairs(prosody.bare_sessions[bare_jid].sessions) do
3318
1856c3aae92d mod_blocking: Update to set id attribute on iq stanza
Matthew Wild <mwild1@gmail.com>
parents: 1796
diff changeset
116 local iq_push_stanza = st.iq({ type = "set", to = bare_jid.."/"..resource, id = "blocking-push" });
940
80ede103d7a3 mod_blocking: Fix handling of unblocking command. Send out un-/block pushes to all resources.
Tobias Markmann <tm@ayena.de>
parents: 372
diff changeset
117 iq_push_stanza:add_child(stanza_content);
80ede103d7a3 mod_blocking: Fix handling of unblocking command. Send out un-/block pushes to all resources.
Tobias Markmann <tm@ayena.de>
parents: 372
diff changeset
118 session.send(iq_push_stanza);
80ede103d7a3 mod_blocking: Fix handling of unblocking command. Send out un-/block pushes to all resources.
Tobias Markmann <tm@ayena.de>
parents: 372
diff changeset
119 end
80ede103d7a3 mod_blocking: Fix handling of unblocking command. Send out un-/block pushes to all resources.
Tobias Markmann <tm@ayena.de>
parents: 372
diff changeset
120 end
80ede103d7a3 mod_blocking: Fix handling of unblocking command. Send out un-/block pushes to all resources.
Tobias Markmann <tm@ayena.de>
parents: 372
diff changeset
121
262
4986ffe35704 mod_blocking: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
122 function handle_blocking_command(event)
4986ffe35704 mod_blocking: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
123 local session, stanza = event.origin, event.stanza;
4986ffe35704 mod_blocking: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
124
161
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 local username, host = jid_split(stanza.attr.from);
164
0b238b2b0801 mod_blocking: Support for the "unblock all JIDs" case, and fix saving of rules after removing a JID
Matthew Wild <mwild1@gmail.com>
parents: 163
diff changeset
126 if stanza.attr.type == "set" then
0b238b2b0801 mod_blocking: Support for the "unblock all JIDs" case, and fix saving of rules after removing a JID
Matthew Wild <mwild1@gmail.com>
parents: 163
diff changeset
127 if stanza.tags[1].name == "block" then
174
d40982d130f0 mod_blocking: Various small changes to make it actually work, which I forgot to commit
Matthew Wild <mwild1@gmail.com>
parents: 173
diff changeset
128 local block = stanza.tags[1];
164
0b238b2b0801 mod_blocking: Support for the "unblock all JIDs" case, and fix saving of rules after removing a JID
Matthew Wild <mwild1@gmail.com>
parents: 163
diff changeset
129 local block_jid_list = {};
0b238b2b0801 mod_blocking: Support for the "unblock all JIDs" case, and fix saving of rules after removing a JID
Matthew Wild <mwild1@gmail.com>
parents: 163
diff changeset
130 for item in block:childtags() do
0b238b2b0801 mod_blocking: Support for the "unblock all JIDs" case, and fix saving of rules after removing a JID
Matthew Wild <mwild1@gmail.com>
parents: 163
diff changeset
131 block_jid_list[#block_jid_list+1] = item.attr.jid;
161
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132 end
164
0b238b2b0801 mod_blocking: Support for the "unblock all JIDs" case, and fix saving of rules after removing a JID
Matthew Wild <mwild1@gmail.com>
parents: 163
diff changeset
133 if #block_jid_list == 0 then
262
4986ffe35704 mod_blocking: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
134 session.send(st.error_reply(stanza, "modify", "bad-request"));
164
0b238b2b0801 mod_blocking: Support for the "unblock all JIDs" case, and fix saving of rules after removing a JID
Matthew Wild <mwild1@gmail.com>
parents: 163
diff changeset
135 else
0b238b2b0801 mod_blocking: Support for the "unblock all JIDs" case, and fix saving of rules after removing a JID
Matthew Wild <mwild1@gmail.com>
parents: 163
diff changeset
136 for _, jid in ipairs(block_jid_list) do
0b238b2b0801 mod_blocking: Support for the "unblock all JIDs" case, and fix saving of rules after removing a JID
Matthew Wild <mwild1@gmail.com>
parents: 163
diff changeset
137 add_blocked_jid(username, host, jid);
0b238b2b0801 mod_blocking: Support for the "unblock all JIDs" case, and fix saving of rules after removing a JID
Matthew Wild <mwild1@gmail.com>
parents: 163
diff changeset
138 end
0b238b2b0801 mod_blocking: Support for the "unblock all JIDs" case, and fix saving of rules after removing a JID
Matthew Wild <mwild1@gmail.com>
parents: 163
diff changeset
139 session.send(st.reply(stanza));
940
80ede103d7a3 mod_blocking: Fix handling of unblocking command. Send out un-/block pushes to all resources.
Tobias Markmann <tm@ayena.de>
parents: 372
diff changeset
140 send_push_iqs(username, host, "block", block_jid_list);
164
0b238b2b0801 mod_blocking: Support for the "unblock all JIDs" case, and fix saving of rules after removing a JID
Matthew Wild <mwild1@gmail.com>
parents: 163
diff changeset
141 end
262
4986ffe35704 mod_blocking: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
142 return true;
164
0b238b2b0801 mod_blocking: Support for the "unblock all JIDs" case, and fix saving of rules after removing a JID
Matthew Wild <mwild1@gmail.com>
parents: 163
diff changeset
143 elseif stanza.tags[1].name == "unblock" then
940
80ede103d7a3 mod_blocking: Fix handling of unblocking command. Send out un-/block pushes to all resources.
Tobias Markmann <tm@ayena.de>
parents: 372
diff changeset
144 local unblock = stanza.tags[1];
80ede103d7a3 mod_blocking: Fix handling of unblocking command. Send out un-/block pushes to all resources.
Tobias Markmann <tm@ayena.de>
parents: 372
diff changeset
145 local unblock_jid_list = {};
80ede103d7a3 mod_blocking: Fix handling of unblocking command. Send out un-/block pushes to all resources.
Tobias Markmann <tm@ayena.de>
parents: 372
diff changeset
146 for item in unblock:childtags() do
80ede103d7a3 mod_blocking: Fix handling of unblocking command. Send out un-/block pushes to all resources.
Tobias Markmann <tm@ayena.de>
parents: 372
diff changeset
147 unblock_jid_list[#unblock_jid_list+1] = item.attr.jid;
80ede103d7a3 mod_blocking: Fix handling of unblocking command. Send out un-/block pushes to all resources.
Tobias Markmann <tm@ayena.de>
parents: 372
diff changeset
148 end
80ede103d7a3 mod_blocking: Fix handling of unblocking command. Send out un-/block pushes to all resources.
Tobias Markmann <tm@ayena.de>
parents: 372
diff changeset
149 if #unblock_jid_list == 0 then
80ede103d7a3 mod_blocking: Fix handling of unblocking command. Send out un-/block pushes to all resources.
Tobias Markmann <tm@ayena.de>
parents: 372
diff changeset
150 remove_all_blocked_jids(username, host);
80ede103d7a3 mod_blocking: Fix handling of unblocking command. Send out un-/block pushes to all resources.
Tobias Markmann <tm@ayena.de>
parents: 372
diff changeset
151 else
80ede103d7a3 mod_blocking: Fix handling of unblocking command. Send out un-/block pushes to all resources.
Tobias Markmann <tm@ayena.de>
parents: 372
diff changeset
152 for _, jid_to_unblock in ipairs(unblock_jid_list) do
80ede103d7a3 mod_blocking: Fix handling of unblocking command. Send out un-/block pushes to all resources.
Tobias Markmann <tm@ayena.de>
parents: 372
diff changeset
153 remove_blocked_jid(username, host, jid_to_unblock);
80ede103d7a3 mod_blocking: Fix handling of unblocking command. Send out un-/block pushes to all resources.
Tobias Markmann <tm@ayena.de>
parents: 372
diff changeset
154 end
80ede103d7a3 mod_blocking: Fix handling of unblocking command. Send out un-/block pushes to all resources.
Tobias Markmann <tm@ayena.de>
parents: 372
diff changeset
155 end
161
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
156 session.send(st.reply(stanza));
940
80ede103d7a3 mod_blocking: Fix handling of unblocking command. Send out un-/block pushes to all resources.
Tobias Markmann <tm@ayena.de>
parents: 372
diff changeset
157 send_push_iqs(username, host, "unblock", unblock_jid_list);
262
4986ffe35704 mod_blocking: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
158 return true;
161
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
159 end
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
160 elseif stanza.attr.type == "get" and stanza.tags[1].name == "blocklist" then
175
92a72435721a mod_blocking: Fixed a nil global access.
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
161 local reply = st.reply(stanza):tag("blocklist", { xmlns = xmlns_blocking });
161
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
162 local blocked_jids = get_blocked_jids(username, host);
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
163 for _, jid in ipairs(blocked_jids) do
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
164 reply:tag("item", { jid = jid }):up();
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
165 end
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
166 session.send(reply);
262
4986ffe35704 mod_blocking: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
167 return true;
161
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
168 end
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
169 end
fda7faee7677 mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
170
262
4986ffe35704 mod_blocking: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
171 module:hook("iq/self/urn:xmpp:blocking:blocklist", handle_blocking_command);
4986ffe35704 mod_blocking: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
172 module:hook("iq/self/urn:xmpp:blocking:block", handle_blocking_command);
4986ffe35704 mod_blocking: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
173 module:hook("iq/self/urn:xmpp:blocking:unblock", handle_blocking_command);