Comparison

mod_adhoc_blacklist/mod_adhoc_blacklist.lua @ 1734:d82b03e79d8b

mod_adhoc_blacklist: Provides the Edit Blacklist command described in XEP-0133 and uses this to block s2s connections
author Kim Alvefur <zash@zash.se>
date Thu, 14 May 2015 00:33:32 +0200
child 1736:efbb73851af9
comparison
equal deleted inserted replaced
1733:9abd3dce619a 1734:d82b03e79d8b
1 -- mod_adhoc_blacklist
2 --
3 -- http://xmpp.org/extensions/xep-0133.html#edit-blacklist
4 --
5 -- Copyright (C) 2015 Kim Alvefur
6 --
7 -- This file is MIT/X11 licensed.
8 --
9
10 module:depends("adhoc");
11 local adhoc = module:require "adhoc";
12 local st = require"util.stanza";
13 local set = require"util.set";
14 local dataform = require"util.dataforms";
15 local adhoc_inital_data = require "util.adhoc".new_initial_data_form;
16
17 local blocklist_form = dataform.new {
18 title = "Editing the Blacklist";
19 instructions = "Fill out this form to edit the list of entities with whom communications are disallowed.";
20 {
21 type = "hidden";
22 name = "FORM_TYPE";
23 value = "http://jabber.org/protocol/admin";
24 };
25 {
26 type = "jid-multi";
27 name = "blacklistjids";
28 label = "The blacklist";
29 };
30 }
31
32 local blocklists = module:open_store("blocklist");
33
34 local blocklist_handler = adhoc_inital_data(blocklist_form, function ()
35 local blacklistjids = {};
36 local blacklist = blocklists:get();
37 for jid in pairs(blacklist) do
38 table.insert(blacklistjids, jid);
39 end
40 return { blacklistjids = blacklistjids };
41 end, function(fields, form_err)
42 if form_err then
43 return { status = "completed", error = { message = "Problem in submitted form" } };
44 end
45 local blacklistjids = set.new(fields.blacklistjids);
46 local ok, err = blocklists:set(nil, blacklistjids._items);
47 if ok then
48 return { status = "completed", info = "Blacklist updated" };
49 else
50 return { status = "completed", error = { message = "Error saving blacklist: "..err } };
51 end
52 end);
53
54 module:add_item("adhoc", adhoc.new("Edit Blacklist", "http://jabber.org/protocol/admin#edit-blacklist", blocklist_handler, "admin"));
55
56 local function is_blocked(host)
57 local blacklistjids = blocklists:get();
58 return blacklistjids and blacklistjids[host];
59 end
60
61 module:hook("route/remote", function (event)
62 local origin, stanza = event.origin, event.stanza;
63 if is_blocked(event.to_host) then
64 if origin and stanza then
65 origin.send(st.error_reply(stanza, "cancel", "not-allowed", "Communication with this domain is not allowed"));
66 return true;
67 end
68 return false;
69 end
70 end, 1000);
71
72
73 module:hook("s2s-stream-features", function (event)
74 local session = event.origin;
75 if is_blocked(session.from_host) then
76 session:close("policy-violation");
77 return false;
78 end
79 end, 1000);
80
81 module:hook("stanza/http://etherx.jabber.org/streams:features", function (event)
82 local session = event.origin;
83 if is_blocked(session.to_host) then
84 session:close("policy-violation");
85 return true;
86 end
87 end, 1000);
88