--- a/mod_block_strangers/mod_block_strangers.lua Tue Jan 20 11:02:14 2015 +0000
+++ b/mod_block_strangers/mod_block_strangers.lua Sun Jan 25 13:04:02 2015 +0100
@@ -1,20 +1,58 @@
+module:depends("adhoc");
local jid_split = require "util.jid".split;
local jid_bare = require "util.jid".bare;
local is_contact_subscribed = require "core.rostermanager".is_contact_subscribed;
+local adhoc_new = module:require "adhoc".new;
+local bare_sessions = bare_sessions
-function check_subscribed(event)
+local storage = module:open_store();
+
+local function filter_stanza(event)
local stanza = event.stanza;
local to_user, to_host, to_resource = jid_split(stanza.attr.to);
- local from_jid = jid_bare(stanza.attr.from);
- if to_user and not is_contact_subscribed(to_user, to_host, from_jid) then
- if to_resource and stanza.attr.type == "groupchat" then
- return nil; -- Pass through
+ if bare_sessions[to_user.."@"..to_host].block_strangers then
+ local from_jid = jid_bare(stanza.attr.from);
+ if to_user and not is_contact_subscribed(to_user, to_host, from_jid) then
+ if to_resource and stanza.attr.type == "groupchat" then
+ return nil; -- Pass through
+ end
+ return true; -- Drop stanza
end
- return true; -- Drop stanza
end
end
-module:hook("message/bare", check_subscribed, 200);
-module:hook("message/full", check_subscribed, 200);
-module:hook("iq/full", check_subscribed, 200);
+local function load_blocking_status(event)
+ local origin = event.origin;
+ local jid = jid_bare(origin.full_jid);
+ bare_sessions[jid].block_strangers = not not storage:get(username);
+end
+
+module:hook("message/bare", filter_stanza, 200);
+module:hook("message/full", filter_stanza, 200);
+module:hook("iq/full", filter_stanza, 200);
+
+module:hook("presence/bare", load_blocking_status);
+
+
+-- Expose blocking/unblocking through ad-hoc commands.
+
+local function toggle_global_blocking(self, data)
+ local username, host = jid_split(data.from);
+ local enabling = (self.node == "mod_block_strangers#enable");
+ local noun = enabling and "blocked" or "unblocked";
+ storage:set(username, enabling and {enabled=true} or {});
+ bare_sessions[username.."@"..host].block_strangers = enabling;
+ return { info = "Strangers successfully "..noun, status = "completed" };
+end
+
+local function get_blocking_status(self, data)
+ local jid = jid_bare(data.from);
+ local enabled = bare_sessions[jid].block_strangers;
+ local noun = enabled and "blocked" or "unblocked";
+ return { info = "Strangers are currently "..noun, status = "completed" };
+end
+
+module:add_item("adhoc", adhoc_new("Block strangers", "mod_block_strangers#enable", toggle_global_blocking));
+module:add_item("adhoc", adhoc_new("Unblock strangers", "mod_block_strangers#disable", toggle_global_blocking));
+module:add_item("adhoc", adhoc_new("Get strangers block status", "mod_block_strangers#status", get_blocking_status));