mod_blocking/mod_blocking.lua
changeset 174 d40982d130f0
parent 173 4f58ddade1db
child 175 92a72435721a
equal deleted inserted replaced
173:4f58ddade1db 174:d40982d130f0
       
     1 local jid_split = require "util.jid".split;
       
     2 local st = require "util.stanza";
       
     3 
       
     4 local xmlns_blocking = "urn:xmpp:blocking";
       
     5 
     1 module:add_feature("urn:xmpp:blocking");
     6 module:add_feature("urn:xmpp:blocking");
     2 
     7 
     3 -- Add JID to default privacy list
     8 -- Add JID to default privacy list
     4 function add_blocked_jid(username, host, jid)
     9 function add_blocked_jid(username, host, jid)
     5 	local privacy_lists = datamanager.load(username, host, "privacy") or {};
    10 	local privacy_lists = datamanager.load(username, host, "privacy") or {};
     6 	local default_list_name = privacy_lists.default;
    11 	local default_list_name = privacy_lists.default;
     7 	if not default_list_name then
    12 	if not default_list_name then
     8 		default_list_name = "blocklist";
    13 		default_list_name = "blocklist";
     9 		privacy_lists.default = default_list_name;
    14 		privacy_lists.default = default_list_name;
    10 	end
    15 	end
    11 	local default_list = privacy_lists.list[default_list_name];
    16 	local default_list = privacy_lists.lists[default_list_name];
    12 	if not default_list then
    17 	if not default_list then
    13 		default_list = { name = default_list_name, items = {} };
    18 		default_list = { name = default_list_name, items = {} };
    14 		privacy_lists.lists[default_list_name] = default_list;
    19 		privacy_lists.lists[default_list_name] = default_list;
    15 	end
    20 	end
    16 	local items = default_list.items;
    21 	local items = default_list.items;
    17 	local order = items[1].order; -- Must come first
    22 	local order = items[1] and items[1].order or 0; -- Must come first
    18 	for i=1,#items do -- order must be unique
    23 	for i=1,#items do -- order must be unique
    19 		items[i].order = items[i].order + 1;
    24 		items[i].order = items[i].order + 1;
    20 	end
    25 	end
    21 	table.insert(items, 1, { type = "jid"
    26 	table.insert(items, 1, { type = "jid"
    22 		, action = "deny"
    27 		, action = "deny"
    33 -- Remove JID from default privacy list
    38 -- Remove JID from default privacy list
    34 function remove_blocked_jid(username, host, jid)
    39 function remove_blocked_jid(username, host, jid)
    35 	local privacy_lists = datamanager.load(username, host, "privacy") or {};
    40 	local privacy_lists = datamanager.load(username, host, "privacy") or {};
    36 	local default_list_name = privacy_lists.default;
    41 	local default_list_name = privacy_lists.default;
    37 	if not default_list_name then return; end
    42 	if not default_list_name then return; end
    38 	local default_list = privacy_lists.list[default_list_name];
    43 	local default_list = privacy_lists.lists[default_list_name];
    39 	if not default_list then return; end
    44 	if not default_list then return; end
    40 	local items = default_list.items;
    45 	local items = default_list.items;
    41 	local item;
    46 	local item;
    42 	for i=1,#items do -- order must be unique
    47 	for i=1,#items do -- order must be unique
    43 		item = items[i];
    48 		item = items[i];
    51 
    56 
    52 function remove_all_blocked_jids(username, host)
    57 function remove_all_blocked_jids(username, host)
    53 	local privacy_lists = datamanager.load(username, host, "privacy") or {};
    58 	local privacy_lists = datamanager.load(username, host, "privacy") or {};
    54 	local default_list_name = privacy_lists.default;
    59 	local default_list_name = privacy_lists.default;
    55 	if not default_list_name then return; end
    60 	if not default_list_name then return; end
    56 	local default_list = privacy_lists.list[default_list_name];
    61 	local default_list = privacy_lists.lists[default_list_name];
    57 	if not default_list then return; end
    62 	if not default_list then return; end
    58 	local items = default_list.items;
    63 	local items = default_list.items;
    59 	local item;
    64 	local item;
    60 	for i=#items,1 do -- order must be unique
    65 	for i=#items,1 do -- order must be unique
    61 		item = items[i];
    66 		item = items[i];
    69 function get_blocked_jids(username, host)
    74 function get_blocked_jids(username, host)
    70 	-- Return array of blocked JIDs in default privacy list
    75 	-- Return array of blocked JIDs in default privacy list
    71 	local privacy_lists = datamanager.load(username, host, "privacy") or {};
    76 	local privacy_lists = datamanager.load(username, host, "privacy") or {};
    72 	local default_list_name = privacy_lists.default;
    77 	local default_list_name = privacy_lists.default;
    73 	if not default_list_name then return {}; end
    78 	if not default_list_name then return {}; end
    74 	local default_list = privacy_lists.list[default_list_name];
    79 	local default_list = privacy_lists.lists[default_list_name];
    75 	if not default_list then return {}; end
    80 	if not default_list then return {}; end
    76 	local items = default_list.items;
    81 	local items = default_list.items;
    77 	local item;
    82 	local item;
    78 	local jid_list = {};
    83 	local jid_list = {};
    79 	for i=1,#items do -- order must be unique
    84 	for i=1,#items do -- order must be unique
    87 
    92 
    88 function handle_blocking_command(session, stanza)
    93 function handle_blocking_command(session, stanza)
    89 	local username, host = jid_split(stanza.attr.from);
    94 	local username, host = jid_split(stanza.attr.from);
    90 	if stanza.attr.type == "set" then
    95 	if stanza.attr.type == "set" then
    91 		if stanza.tags[1].name == "block" then
    96 		if stanza.tags[1].name == "block" then
    92 			local block = stanza.tags[1]:get_child("block");
    97 			local block = stanza.tags[1];
    93 			local block_jid_list = {};
    98 			local block_jid_list = {};
    94 			for item in block:childtags() do
    99 			for item in block:childtags() do
    95 				block_jid_list[#block_jid_list+1] = item.attr.jid;
   100 				block_jid_list[#block_jid_list+1] = item.attr.jid;
    96 			end
   101 			end
    97 			if #block_jid_list == 0 then
   102 			if #block_jid_list == 0 then