plugins/mod_blocklist.lua
author Kim Alvefur <zash@zash.se>
Tue, 12 Aug 2014 15:40:00 +0200
changeset 6351 10dc228a45a4
parent 6350 bba5f4ffe75a
child 6352 b703e6930e4c
permissions -rw-r--r--
mod_blocklist: Correct comment
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6344
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     1
-- Prosody IM
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     2
-- Copyright (C) 2009-2010 Matthew Wild
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     3
-- Copyright (C) 2009-2010 Waqas Hussain
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     4
-- Copyright (C) 2014 Kim Alvefur
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     5
--
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     6
-- This project is MIT/X11 licensed. Please see the
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     7
-- COPYING file in the source package for more information.
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     8
--
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     9
-- This module implements XEP-0191: Blocking Command
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    10
--
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    11
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    12
local user_exists = require"core.usermanager".user_exists;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    13
local is_contact_subscribed = require"core.rostermanager".is_contact_subscribed;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    14
local st = require"util.stanza";
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    15
local st_error_reply = st.error_reply;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    16
local jid_prep, jid_split = import("jid", "prep", "split");
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    17
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    18
local host = module.host;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    19
local storage = module:open_store();
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    20
local sessions = prosody.hosts[host].sessions;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    21
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    22
-- Cache of blocklists used since module was loaded
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    23
local cache = {};
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    24
if module:get_option_boolean("blocklist_weak_cache") then
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    25
	-- Lower memory usage, more IO and latency
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    26
	setmetatable(cache, { __mode = "v" });
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    27
end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    28
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    29
local null_blocklist = {};
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    30
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    31
module:add_feature("urn:xmpp:blocking");
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    32
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    33
local function set_blocklist(username, blocklist)
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    34
	local ok, err = storage:set(username, blocklist);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    35
	if not ok then
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    36
		return ok, err;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    37
	end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    38
	-- Successful save, update the cache
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    39
	cache[username] = blocklist;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    40
	return true;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    41
end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    42
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    43
-- Migrates from the old mod_privacy storage
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    44
local function migrate_privacy_list(username)
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    45
	local migrated_data = { [false] = "not empty" };
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    46
	module:log("info", "Migrating blocklist from mod_privacy storage for user '%s'", username);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    47
	local legacy_data = module:open_store("privacy"):get(username);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    48
	if legacy_data and legacy_data.lists and legacy_data.default then
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    49
		legacy_data = legacy_data.lists[legacy_data.default];
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    50
		legacy_data = legacy_data and legacy_data.items;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    51
	else
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    52
		return migrated_data;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    53
	end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    54
	if legacy_data then
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    55
		local item, jid;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    56
		for i = 1, #legacy_data do
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    57
			item = legacy_data[i];
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    58
			if item.type == "jid" and item.action == "deny" then
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    59
				jid = jid_prep(item.value);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    60
				if not jid then
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    61
					module:log("warn", "Invalid JID in privacy store for user '%s' not migrated: %s", username, tostring(item.value));
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    62
				else
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    63
					migrated_data[jid] = true;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    64
				end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    65
			end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    66
		end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    67
	end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    68
	set_blocklist(username, migrated_data);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    69
	return migrated_data;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    70
end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    71
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    72
local function get_blocklist(username)
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    73
	local blocklist = cache[username];
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    74
	if not blocklist then
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    75
		if not user_exists(username, host) then
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    76
			return null_blocklist;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    77
		end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    78
		blocklist = storage:get(username);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    79
		if not blocklist then
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    80
			blocklist = migrate_privacy_list(username);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    81
		end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    82
		cache[username] = blocklist;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    83
	end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    84
	return blocklist;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    85
end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    86
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    87
module:hook("iq-get/self/urn:xmpp:blocking:blocklist", function (event)
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    88
	local origin, stanza = event.origin, event.stanza;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    89
	local username = origin.username;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    90
	local reply = st.reply(stanza):tag("blocklist", { xmlns = "urn:xmpp:blocking" });
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    91
	local blocklist = get_blocklist(username);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    92
	for jid in pairs(blocklist) do
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    93
		if jid then
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    94
			reply:tag("item", { jid = jid }):up();
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    95
		end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    96
	end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    97
	origin.interested_blocklist = true; -- Gets notified about changes
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    98
	return origin.send(reply);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    99
end);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   100
6351
10dc228a45a4 mod_blocklist: Correct comment
Kim Alvefur <zash@zash.se>
parents: 6350
diff changeset
   101
-- Add or remove some jid(s) from the blocklist
6344
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   102
-- We want this to be atomic and not do a partial update
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   103
local function edit_blocklist(event)
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   104
	local origin, stanza = event.origin, event.stanza;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   105
	local username = origin.username;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   106
	local act = stanza.tags[1];
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   107
	local new = {};
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   108
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   109
	local jid;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   110
	for item in act:childtags("item") do
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   111
		jid = jid_prep(item.attr.jid);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   112
		if not jid then
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   113
			return origin.send(st_error_reply(stanza, "modify", "jid-malformed"));
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   114
		end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   115
		item.attr.jid = jid; -- echo back prepped
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   116
		new[jid] = is_contact_subscribed(username, host, jid) or false;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   117
	end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   118
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   119
	local mode = act.name == "block" or nil;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   120
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   121
	if mode and not next(new) then
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   122
		-- <block/> element does not contain at least one <item/> child element
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   123
		return origin.send(st_error_reply(stanza, "modify", "bad-request"));
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   124
	end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   125
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   126
	local blocklist = get_blocklist(username);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   127
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   128
	local new_blocklist = {};
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   129
6350
bba5f4ffe75a mod_blocklist: Fix any unblock emptying the blocklist
Kim Alvefur <zash@zash.se>
parents: 6344
diff changeset
   130
	if mode or next(new) then
6344
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   131
		for jid in pairs(blocklist) do
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   132
			new_blocklist[jid] = true;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   133
		end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   134
		for jid in pairs(new) do
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   135
			new_blocklist[jid] = mode;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   136
		end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   137
		-- else empty the blocklist
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   138
	end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   139
	new_blocklist[false] = "not empty"; -- In order to avoid doing the migration thing twice
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   140
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   141
	local ok, err = set_blocklist(username, new_blocklist);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   142
	if ok then
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   143
		origin.send(st.reply(stanza));
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   144
	else
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   145
		return origin.send(st_error_reply(stanza, "wait", "internal-server-error", err));
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   146
	end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   147
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   148
	if mode then
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   149
		for jid, in_roster in pairs(new) do
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   150
			if not blocklist[jid] and in_roster and sessions[username] then
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   151
				for _, session in pairs(sessions[username].sessions) do
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   152
					module:send(st.presence({ type = "unavailable", to = jid, from = session.full_jid }));
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   153
				end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   154
			end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   155
		end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   156
	end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   157
	if sessions[username] then
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   158
		local blocklist_push = st.iq({ type = "set", id = "blocklist-push" })
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   159
			:add_child(act); -- I am lazy
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   160
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   161
		for _, session in pairs(sessions[username].sessions) do
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   162
			if session.interested_blocklist then
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   163
				blocklist_push.attr.to = session.full_jid;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   164
				session.send(blocklist_push);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   165
			end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   166
		end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   167
	end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   168
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   169
	return true;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   170
end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   171
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   172
module:hook("iq-set/self/urn:xmpp:blocking:block", edit_blocklist);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   173
module:hook("iq-set/self/urn:xmpp:blocking:unblock", edit_blocklist);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   174
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   175
-- Cache invalidation, solved!
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   176
module:hook_global("user-deleted", function (event)
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   177
	if event.host == host then
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   178
		cache[event.username] = nil;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   179
	end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   180
end);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   181
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   182
-- Buggy clients
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   183
module:hook("iq-error/self/blocklist-push", function (event)
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   184
	local type, condition, text = event.stanza:get_error();
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   185
	(event.origin.log or module._log)("warn", "client returned an error in response to notification from mod_%s: %s%s%s", module.name, condition, text and ": " or "", text or "");
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   186
	return true;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   187
end);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   188
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   189
local function is_blocked(user, jid)
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   190
	local blocklist = cache[user] or get_blocklist(user);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   191
	if blocklist[jid] then return true; end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   192
	local node, host = jid_split(jid);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   193
	return blocklist[host] or node and blocklist[node..'@'..host];
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   194
end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   195
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   196
-- Event handlers for bouncing or dropping stanzas
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   197
local function drop_stanza(event)
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   198
	local stanza = event.stanza;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   199
	local attr = stanza.attr;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   200
	local to, from = attr.to, attr.from;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   201
	to = to and jid_split(to);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   202
	if to and from then
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   203
		return is_blocked(to, from);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   204
	end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   205
end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   206
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   207
local function bounce_stanza(event)
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   208
	local origin, stanza = event.origin, event.stanza;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   209
	if drop_stanza(event) then
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   210
		return origin.send(st_error_reply(stanza, "cancel", "service-unavailable"));
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   211
	end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   212
end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   213
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   214
local function bounce_iq(event)
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   215
	local type = event.stanza.attr.type;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   216
	if type == "set" or type == "get" then
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   217
		return bounce_stanza(event);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   218
	end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   219
	return drop_stanza(event); -- result or error
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   220
end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   221
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   222
local function bounce_message(event)
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   223
	local type = event.stanza.attr.type;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   224
	if type == "chat" or not type or type == "normal" then
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   225
		return bounce_stanza(event);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   226
	end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   227
	return drop_stanza(event); -- drop headlines, groupchats etc
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   228
end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   229
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   230
local function drop_outgoing(event)
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   231
	local origin, stanza = event.origin, event.stanza;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   232
	local username = origin.username or jid_split(stanza.attr.from);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   233
	if not username then return end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   234
	local to = stanza.attr.to;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   235
	if to then return is_blocked(username, to); end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   236
	-- nil 'to' means a self event, don't bock those
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   237
end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   238
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   239
local function bounce_outgoing(event)
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   240
	local origin, stanza = event.origin, event.stanza;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   241
	local type = stanza.attr.type;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   242
	if type == "error" or stanza.name == "iq" and type == "result" then
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   243
		return drop_outgoing(event);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   244
	end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   245
	if drop_outgoing(event) then
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   246
		return origin.send(st_error_reply(stanza, "cancel", "not-acceptable", "You have blocked this JID")
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   247
			:tag("blocked", { xmlns = "urn:xmpp:blocking:errors" }));
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   248
	end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   249
end
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   250
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   251
-- Hook all the events!
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   252
local prio_in, prio_out = 100, 100;
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   253
module:hook("presence/bare", drop_stanza, prio_in);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   254
module:hook("presence/full", drop_stanza, prio_in);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   255
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   256
module:hook("message/bare", bounce_message, prio_in);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   257
module:hook("message/full", bounce_message, prio_in);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   258
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   259
module:hook("iq/bare", bounce_iq, prio_in);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   260
module:hook("iq/full", bounce_iq, prio_in);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   261
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   262
module:hook("pre-message/bare", bounce_outgoing, prio_out);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   263
module:hook("pre-message/full", bounce_outgoing, prio_out);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   264
module:hook("pre-message/host", bounce_outgoing, prio_out);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   265
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   266
module:hook("pre-presence/bare", drop_outgoing, prio_out);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   267
module:hook("pre-presence/full", drop_outgoing, prio_out);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   268
module:hook("pre-presence/host", drop_outgoing, prio_out);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   269
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   270
module:hook("pre-iq/bare", bounce_outgoing, prio_out);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   271
module:hook("pre-iq/full", bounce_outgoing, prio_out);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   272
module:hook("pre-iq/host", bounce_outgoing, prio_out);
68b5c1ed18dd mod_blocklist: XEP-0191 implementation written for speed and independence from mod_privacy
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   273