mod_privacy/mod_privacy.lua
author Thilo Cestonaro <thilo@cestona.ro>
Fri, 16 Oct 2009 20:51:14 +0200
changeset 45 3f5bbd7c90d4
parent 42 bbb3d3a90a70
child 46 ea756d96584f
permissions -rw-r--r--
mod_privacy: it says "from" not "form" (thx flo for reporting!)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
     1
-- Prosody IM
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
     2
-- Copyright (C) 2008-2009 Matthew Wild
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
     3
-- Copyright (C) 2008-2009 Waqas Hussain
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
     4
-- Copyright (C) 2009 Thilo Cestonaro
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
     5
-- 
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
     6
-- This project is MIT/X11 licensed. Please see the
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
     7
-- COPYING file in the source package for more information.
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
     8
--
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
     9
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    10
local prosody = prosody;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    11
local st = require "util.stanza";
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    12
local datamanager = require "util.datamanager";
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    13
local bare_sessions = bare_sessions;
11
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
    14
local util_Jid = require "util.jid";
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
    15
local jid_bare = util_Jid.bare;
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
    16
local jid_split = util_Jid.split;
16
35e74c1094a7 mod_privacy: order must be non-negativ integer and unique, group must be existing in the roster, subscription can only be to,from,both or none, action must be either deny or allow.
Thilo Cestonaro <thilo@cestona.ro>
parents: 15
diff changeset
    17
local load_roster = require "core.rostermanager".load_roster;
35e74c1094a7 mod_privacy: order must be non-negativ integer and unique, group must be existing in the roster, subscription can only be to,from,both or none, action must be either deny or allow.
Thilo Cestonaro <thilo@cestona.ro>
parents: 15
diff changeset
    18
local to_number = _G.tonumber;
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    19
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
    20
function findNamedList (privacy_lists, name)
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    21
	local ret = nil
11
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
    22
	if privacy_lists.lists == nil then
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
    23
		return nil;
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
    24
	end
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    25
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    26
	for i=1, #privacy_lists.lists do
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    27
		if privacy_lists.lists[i].name == name then
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    28
			ret = i;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    29
			break;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    30
		end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    31
	end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    32
	return ret;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    33
end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    34
20
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
    35
function isListUsed(origin, name, privacy_lists)	
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
    36
	if bare_sessions[origin.username.."@"..origin.host].sessions ~= nil then
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
    37
		for resource, session in pairs(bare_sessions[origin.username.."@"..origin.host].sessions) do
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
    38
			if resource ~= origin.resource then
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
    39
				if session.activePrivacyList == name then
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
    40
					return true;
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
    41
				elseif session.activePrivacyList == nil and privacy_lists.default == name then
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
    42
					return true;
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
    43
				end
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
    44
			end
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
    45
		end
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
    46
	end
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
    47
	return false;
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
    48
end
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
    49
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
    50
function isAnotherSessionUsingDefaultList(origin)
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
    51
	local ret = false
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
    52
	if bare_sessions[origin.username.."@"..origin.host].sessions ~= nil then
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
    53
		for resource, session in pairs(bare_sessions[origin.username.."@"..origin.host].sessions) do
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
    54
			if resource ~= origin.resource and session.activePrivacyList == nil then
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
    55
				ret = true;
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
    56
				break;
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
    57
			end
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
    58
		end
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
    59
	end
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
    60
	return ret;
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
    61
end
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
    62
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
    63
function declineList (privacy_lists, origin, stanza, which)
17
ccb07c0efc7e mod_privacy: prepare everything for the "is used" checking stuff...
Thilo Cestonaro <thilo@cestona.ro>
parents: 16
diff changeset
    64
	if which == "default" then
20
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
    65
		if isAnotherSessionUsingDefaultList(origin) then
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
    66
			return { "cancel", "conflict", "Another session is online and using the default list."};
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
    67
		end
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
    68
		privacy_lists.default = nil;
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
    69
		origin.send(st.reply(stanza));
17
ccb07c0efc7e mod_privacy: prepare everything for the "is used" checking stuff...
Thilo Cestonaro <thilo@cestona.ro>
parents: 16
diff changeset
    70
	elseif which == "active" then
ccb07c0efc7e mod_privacy: prepare everything for the "is used" checking stuff...
Thilo Cestonaro <thilo@cestona.ro>
parents: 16
diff changeset
    71
		origin.activePrivacyList = nil;
20
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
    72
		origin.send(st.reply(stanza));
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
    73
	else
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
    74
		return {"modify", "bad-request", "Neither default nor active list specifed to decline."};
17
ccb07c0efc7e mod_privacy: prepare everything for the "is used" checking stuff...
Thilo Cestonaro <thilo@cestona.ro>
parents: 16
diff changeset
    75
	end
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    76
	return true;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    77
end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    78
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
    79
function activateList (privacy_lists, origin, stanza, which, name)
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
    80
	local idx = findNamedList(privacy_lists, name);
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    81
20
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
    82
	if privacy_lists.default == nil then
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
    83
		privacy_lists.default = "";
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
    84
	end
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
    85
	if origin.activePrivacyList == nil then
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
    86
		origin.activePrivacyList = "";
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    87
	end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    88
	
20
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
    89
	if which == "default" and idx ~= nil then
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
    90
		if isAnotherSessionUsingDefaultList(origin) then
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
    91
			return {"cancel", "conflict", "Another session is online and using the default list."};
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
    92
		end
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
    93
		privacy_lists.default = name;
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
    94
		origin.send(st.reply(stanza));
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
    95
	elseif which == "active" and idx ~= nil then
17
ccb07c0efc7e mod_privacy: prepare everything for the "is used" checking stuff...
Thilo Cestonaro <thilo@cestona.ro>
parents: 16
diff changeset
    96
		origin.activePrivacyList = name;
20
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
    97
		origin.send(st.reply(stanza));
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
    98
	else
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
    99
		return {"modify", "bad-request", "Either not active or default given or unknown list name specified."};
17
ccb07c0efc7e mod_privacy: prepare everything for the "is used" checking stuff...
Thilo Cestonaro <thilo@cestona.ro>
parents: 16
diff changeset
   100
	end
20
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
   101
	return true;
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   102
end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   103
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   104
function deleteList (privacy_lists, origin, stanza, name)
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   105
	local idx = findNamedList(privacy_lists, name);
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   106
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   107
	if idx ~= nil then
20
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
   108
		if isListUsed(origin, name, privacy_lists) then
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
   109
			return {"cancel", "conflict", "Another session is online and using the list which should be deleted."};
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
   110
		end
27
d91cb13ef0ee mod_privacy: make the block function work; retrieve the roster groups correctly
Thilo Cestonaro <thilo@cestona.ro>
parents: 20
diff changeset
   111
		if privacy_lists.default == name then
d91cb13ef0ee mod_privacy: make the block function work; retrieve the roster groups correctly
Thilo Cestonaro <thilo@cestona.ro>
parents: 20
diff changeset
   112
			privacy_lists.default = "";
d91cb13ef0ee mod_privacy: make the block function work; retrieve the roster groups correctly
Thilo Cestonaro <thilo@cestona.ro>
parents: 20
diff changeset
   113
		end
d91cb13ef0ee mod_privacy: make the block function work; retrieve the roster groups correctly
Thilo Cestonaro <thilo@cestona.ro>
parents: 20
diff changeset
   114
		if origin.activePrivacyList == name then
d91cb13ef0ee mod_privacy: make the block function work; retrieve the roster groups correctly
Thilo Cestonaro <thilo@cestona.ro>
parents: 20
diff changeset
   115
			origin.activePrivacyList = "";
d91cb13ef0ee mod_privacy: make the block function work; retrieve the roster groups correctly
Thilo Cestonaro <thilo@cestona.ro>
parents: 20
diff changeset
   116
		end
20
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
   117
		table.remove(privacy_lists.lists, idx);
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   118
		origin.send(st.reply(stanza));
20
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
   119
		return true;
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   120
	end
20
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
   121
	return {"modify", "bad-request", "Not existing list specifed to be deleted."};
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   122
end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   123
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   124
local function sortByOrder(a, b)
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   125
	if a.order < b.order then
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   126
		return true;
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   127
	end
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   128
	return false;
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   129
end
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   130
16
35e74c1094a7 mod_privacy: order must be non-negativ integer and unique, group must be existing in the roster, subscription can only be to,from,both or none, action must be either deny or allow.
Thilo Cestonaro <thilo@cestona.ro>
parents: 15
diff changeset
   131
function createOrReplaceList (privacy_lists, origin, stanza, name, entries, roster)
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   132
	local idx = findNamedList(privacy_lists, name);
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   133
	local bare_jid = origin.username.."@"..origin.host;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   134
	
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   135
	if privacy_lists.lists == nil then
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   136
		privacy_lists.lists = {};
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   137
	end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   138
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   139
	if idx == nil then
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   140
		idx = #privacy_lists.lists + 1;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   141
	end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   142
16
35e74c1094a7 mod_privacy: order must be non-negativ integer and unique, group must be existing in the roster, subscription can only be to,from,both or none, action must be either deny or allow.
Thilo Cestonaro <thilo@cestona.ro>
parents: 15
diff changeset
   143
	local orderCheck = {};
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   144
	local list = {};
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   145
	list.name = name;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   146
	list.items = {};
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   147
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   148
	for _,item in ipairs(entries) do
16
35e74c1094a7 mod_privacy: order must be non-negativ integer and unique, group must be existing in the roster, subscription can only be to,from,both or none, action must be either deny or allow.
Thilo Cestonaro <thilo@cestona.ro>
parents: 15
diff changeset
   149
		if to_number(item.attr.order) == nil or to_number(item.attr.order) < 0 or orderCheck[item.attr.order] ~= nil then
20
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
   150
			return {"modify", "bad-request", "Order attribute not valid."};
16
35e74c1094a7 mod_privacy: order must be non-negativ integer and unique, group must be existing in the roster, subscription can only be to,from,both or none, action must be either deny or allow.
Thilo Cestonaro <thilo@cestona.ro>
parents: 15
diff changeset
   151
		end
20
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
   152
		
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
   153
		if item.attr.type ~= nil and item.attr.type ~= "jid" and item.attr.type ~= "subscription" and item.attr.type ~= "group" then
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
   154
			return {"modify", "bad-request", "Type attribute not valid."};
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
   155
		end 
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
   156
		
14
0892941186f2 mod_privacy: Make tmp variable a local
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
   157
		local tmp = {};
16
35e74c1094a7 mod_privacy: order must be non-negativ integer and unique, group must be existing in the roster, subscription can only be to,from,both or none, action must be either deny or allow.
Thilo Cestonaro <thilo@cestona.ro>
parents: 15
diff changeset
   158
		orderCheck[item.attr.order] = true;
35e74c1094a7 mod_privacy: order must be non-negativ integer and unique, group must be existing in the roster, subscription can only be to,from,both or none, action must be either deny or allow.
Thilo Cestonaro <thilo@cestona.ro>
parents: 15
diff changeset
   159
		
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   160
		tmp["type"] = item.attr.type;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   161
		tmp["value"] = item.attr.value;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   162
		tmp["action"] = item.attr.action;
16
35e74c1094a7 mod_privacy: order must be non-negativ integer and unique, group must be existing in the roster, subscription can only be to,from,both or none, action must be either deny or allow.
Thilo Cestonaro <thilo@cestona.ro>
parents: 15
diff changeset
   163
		tmp["order"] = to_number(item.attr.order);
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   164
		tmp["presence-in"] = false;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   165
		tmp["presence-out"] = false;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   166
		tmp["message"] = false;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   167
		tmp["iq"] = false;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   168
		
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   169
		if #item.tags > 0 then
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   170
			for _,tag in ipairs(item.tags) do
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   171
				tmp[tag.name] = true;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   172
			end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   173
		end
16
35e74c1094a7 mod_privacy: order must be non-negativ integer and unique, group must be existing in the roster, subscription can only be to,from,both or none, action must be either deny or allow.
Thilo Cestonaro <thilo@cestona.ro>
parents: 15
diff changeset
   174
		
35e74c1094a7 mod_privacy: order must be non-negativ integer and unique, group must be existing in the roster, subscription can only be to,from,both or none, action must be either deny or allow.
Thilo Cestonaro <thilo@cestona.ro>
parents: 15
diff changeset
   175
		if tmp.type == "group" then
35e74c1094a7 mod_privacy: order must be non-negativ integer and unique, group must be existing in the roster, subscription can only be to,from,both or none, action must be either deny or allow.
Thilo Cestonaro <thilo@cestona.ro>
parents: 15
diff changeset
   176
			local found = false;
35e74c1094a7 mod_privacy: order must be non-negativ integer and unique, group must be existing in the roster, subscription can only be to,from,both or none, action must be either deny or allow.
Thilo Cestonaro <thilo@cestona.ro>
parents: 15
diff changeset
   177
			local roster = load_roster(origin.username, origin.host);
27
d91cb13ef0ee mod_privacy: make the block function work; retrieve the roster groups correctly
Thilo Cestonaro <thilo@cestona.ro>
parents: 20
diff changeset
   178
			for jid,item in pairs(roster) do
d91cb13ef0ee mod_privacy: make the block function work; retrieve the roster groups correctly
Thilo Cestonaro <thilo@cestona.ro>
parents: 20
diff changeset
   179
				if item.groups ~= nil then
d91cb13ef0ee mod_privacy: make the block function work; retrieve the roster groups correctly
Thilo Cestonaro <thilo@cestona.ro>
parents: 20
diff changeset
   180
					for group in pairs(item.groups) do
d91cb13ef0ee mod_privacy: make the block function work; retrieve the roster groups correctly
Thilo Cestonaro <thilo@cestona.ro>
parents: 20
diff changeset
   181
						if group == tmp.value then
d91cb13ef0ee mod_privacy: make the block function work; retrieve the roster groups correctly
Thilo Cestonaro <thilo@cestona.ro>
parents: 20
diff changeset
   182
							found = true;
d91cb13ef0ee mod_privacy: make the block function work; retrieve the roster groups correctly
Thilo Cestonaro <thilo@cestona.ro>
parents: 20
diff changeset
   183
							break;
d91cb13ef0ee mod_privacy: make the block function work; retrieve the roster groups correctly
Thilo Cestonaro <thilo@cestona.ro>
parents: 20
diff changeset
   184
						end
d91cb13ef0ee mod_privacy: make the block function work; retrieve the roster groups correctly
Thilo Cestonaro <thilo@cestona.ro>
parents: 20
diff changeset
   185
					end
d91cb13ef0ee mod_privacy: make the block function work; retrieve the roster groups correctly
Thilo Cestonaro <thilo@cestona.ro>
parents: 20
diff changeset
   186
					if found == true then
d91cb13ef0ee mod_privacy: make the block function work; retrieve the roster groups correctly
Thilo Cestonaro <thilo@cestona.ro>
parents: 20
diff changeset
   187
						break;
d91cb13ef0ee mod_privacy: make the block function work; retrieve the roster groups correctly
Thilo Cestonaro <thilo@cestona.ro>
parents: 20
diff changeset
   188
					end
16
35e74c1094a7 mod_privacy: order must be non-negativ integer and unique, group must be existing in the roster, subscription can only be to,from,both or none, action must be either deny or allow.
Thilo Cestonaro <thilo@cestona.ro>
parents: 15
diff changeset
   189
				end
35e74c1094a7 mod_privacy: order must be non-negativ integer and unique, group must be existing in the roster, subscription can only be to,from,both or none, action must be either deny or allow.
Thilo Cestonaro <thilo@cestona.ro>
parents: 15
diff changeset
   190
			end
35e74c1094a7 mod_privacy: order must be non-negativ integer and unique, group must be existing in the roster, subscription can only be to,from,both or none, action must be either deny or allow.
Thilo Cestonaro <thilo@cestona.ro>
parents: 15
diff changeset
   191
			if found == false then
20
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
   192
				return {"cancel", "item-not-found", "Specifed roster group not existing."};
16
35e74c1094a7 mod_privacy: order must be non-negativ integer and unique, group must be existing in the roster, subscription can only be to,from,both or none, action must be either deny or allow.
Thilo Cestonaro <thilo@cestona.ro>
parents: 15
diff changeset
   193
			end
35e74c1094a7 mod_privacy: order must be non-negativ integer and unique, group must be existing in the roster, subscription can only be to,from,both or none, action must be either deny or allow.
Thilo Cestonaro <thilo@cestona.ro>
parents: 15
diff changeset
   194
		elseif tmp.type == "subscription" then
35e74c1094a7 mod_privacy: order must be non-negativ integer and unique, group must be existing in the roster, subscription can only be to,from,both or none, action must be either deny or allow.
Thilo Cestonaro <thilo@cestona.ro>
parents: 15
diff changeset
   195
			if	tmp.value ~= "both" and
35e74c1094a7 mod_privacy: order must be non-negativ integer and unique, group must be existing in the roster, subscription can only be to,from,both or none, action must be either deny or allow.
Thilo Cestonaro <thilo@cestona.ro>
parents: 15
diff changeset
   196
				tmp.value ~= "to" and
35e74c1094a7 mod_privacy: order must be non-negativ integer and unique, group must be existing in the roster, subscription can only be to,from,both or none, action must be either deny or allow.
Thilo Cestonaro <thilo@cestona.ro>
parents: 15
diff changeset
   197
				tmp.value ~= "from" and
35e74c1094a7 mod_privacy: order must be non-negativ integer and unique, group must be existing in the roster, subscription can only be to,from,both or none, action must be either deny or allow.
Thilo Cestonaro <thilo@cestona.ro>
parents: 15
diff changeset
   198
				tmp.value ~= "none" then
20
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
   199
				return {"cancel", "bad-request", "Subscription value must be both, to, from or none."};
16
35e74c1094a7 mod_privacy: order must be non-negativ integer and unique, group must be existing in the roster, subscription can only be to,from,both or none, action must be either deny or allow.
Thilo Cestonaro <thilo@cestona.ro>
parents: 15
diff changeset
   200
			end
35e74c1094a7 mod_privacy: order must be non-negativ integer and unique, group must be existing in the roster, subscription can only be to,from,both or none, action must be either deny or allow.
Thilo Cestonaro <thilo@cestona.ro>
parents: 15
diff changeset
   201
		end
35e74c1094a7 mod_privacy: order must be non-negativ integer and unique, group must be existing in the roster, subscription can only be to,from,both or none, action must be either deny or allow.
Thilo Cestonaro <thilo@cestona.ro>
parents: 15
diff changeset
   202
		
35e74c1094a7 mod_privacy: order must be non-negativ integer and unique, group must be existing in the roster, subscription can only be to,from,both or none, action must be either deny or allow.
Thilo Cestonaro <thilo@cestona.ro>
parents: 15
diff changeset
   203
		if tmp.action ~= "deny" and tmp.action ~= "allow" then
20
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
   204
			return {"cancel", "bad-request", "Action must be either deny or allow."};
16
35e74c1094a7 mod_privacy: order must be non-negativ integer and unique, group must be existing in the roster, subscription can only be to,from,both or none, action must be either deny or allow.
Thilo Cestonaro <thilo@cestona.ro>
parents: 15
diff changeset
   205
		end
35e74c1094a7 mod_privacy: order must be non-negativ integer and unique, group must be existing in the roster, subscription can only be to,from,both or none, action must be either deny or allow.
Thilo Cestonaro <thilo@cestona.ro>
parents: 15
diff changeset
   206
		
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   207
		list.items[#list.items + 1] = tmp;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   208
	end
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   209
	
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   210
	table.sort(list, sortByOrder);
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   211
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   212
	privacy_lists.lists[idx] = list;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   213
	origin.send(st.reply(stanza));
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   214
	if bare_sessions[bare_jid] ~= nil then
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   215
		iq = st.iq ( { type = "set", id="push1" } );
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   216
		iq:tag ("query", { xmlns = "jabber:iq:privacy" } );
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   217
		iq:tag ("list", { name = list.name } ):up();
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   218
		iq:up();
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   219
		for resource, session in pairs(bare_sessions[bare_jid].sessions) do
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   220
			iq.attr.to = bare_jid.."/"..resource
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   221
			session.send(iq);
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   222
		end
20
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
   223
	else
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
   224
		return {"cancel", "bad-request", "internal error."};
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   225
	end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   226
	return true;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   227
end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   228
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   229
function getList(privacy_lists, origin, stanza, name)
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   230
	local reply = st.reply(stanza);
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   231
	reply:tag("query", {xmlns="jabber:iq:privacy"});
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   232
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   233
	if name == nil then
17
ccb07c0efc7e mod_privacy: prepare everything for the "is used" checking stuff...
Thilo Cestonaro <thilo@cestona.ro>
parents: 16
diff changeset
   234
		reply:tag("active", {name=origin.activePrivacyList or ""}):up();
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   235
		reply:tag("default", {name=privacy_lists.default or ""}):up();
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   236
		if privacy_lists.lists then
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   237
			for _,list in ipairs(privacy_lists.lists) do
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   238
				reply:tag("list", {name=list.name}):up();
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   239
			end
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   240
		end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   241
	else
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   242
		local idx = findNamedList(privacy_lists, name);
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   243
		if idx ~= nil then
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   244
			list = privacy_lists.lists[idx];
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   245
			reply = reply:tag("list", {name=list.name});
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   246
			for _,item in ipairs(list.items) do
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   247
				reply:tag("item", {type=item.type, value=item.value, action=item.action, order=item.order});
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   248
				if item["message"] then reply:tag("message"):up(); end
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   249
				if item["iq"] then reply:tag("iq"):up(); end
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   250
				if item["presence-in"] then reply:tag("presence-in"):up(); end
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   251
				if item["presence-out"] then reply:tag("presence-out"):up(); end
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   252
				reply:up();
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   253
			end
20
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
   254
		else
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
   255
			return {"cancel", "item-not-found", "Unknown list specified."};
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   256
		end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   257
	end
20
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
   258
	
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
   259
	origin.send(reply);
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
   260
	return true;
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   261
end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   262
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   263
module:hook("iq/bare/jabber:iq:privacy:query", function(data)
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   264
	local origin, stanza = data.origin, data.stanza;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   265
	
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   266
	if stanza.attr.to == nil then -- only service requests to own bare JID
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   267
		local query = stanza.tags[1]; -- the query element
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   268
		local valid = false;
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   269
		local privacy_lists = datamanager.load(origin.username, origin.host, "privacy") or {};
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   270
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   271
		if stanza.attr.type == "set" then
17
ccb07c0efc7e mod_privacy: prepare everything for the "is used" checking stuff...
Thilo Cestonaro <thilo@cestona.ro>
parents: 16
diff changeset
   272
			if #query.tags == 1 then --  the <query/> element MUST NOT include more than one child element 
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   273
				for _,tag in ipairs(query.tags) do
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   274
					if tag.name == "active" or tag.name == "default" then
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   275
						if tag.attr.name == nil then -- Client declines the use of active / default list
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   276
							valid = declineList(privacy_lists, origin, stanza, tag.name);
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   277
						else -- Client requests change of active / default list
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   278
							valid = activateList(privacy_lists, origin, stanza, tag.name, tag.attr.name);
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   279
						end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   280
					elseif tag.name == "list" and tag.attr.name then -- Client adds / edits a privacy list
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   281
						if #tag.tags == 0 then -- Client removes a privacy list
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   282
							valid = deleteList(privacy_lists, origin, stanza, tag.attr.name);
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   283
						else -- Client edits a privacy list
42
bbb3d3a90a70 mod_privacy: decrease the log messages count.
Thilo Cestonaro <thilo@cestona.ro>
parents: 41
diff changeset
   284
							valid = createOrReplaceList(privacy_lists, origin, stanza, tag.attr.name, tag.tags);
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   285
						end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   286
					end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   287
				end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   288
			end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   289
		elseif stanza.attr.type == "get" then
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   290
			local name = nil;
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   291
			local listsToRetrieve = 0;
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   292
			if #query.tags >= 1 then
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   293
				for _,tag in ipairs(query.tags) do
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   294
					if tag.name == "list" then -- Client requests a privacy list from server
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   295
						name = tag.attr.name;
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   296
						listsToRetrieve = listsToRetrieve + 1;
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   297
					end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   298
				end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   299
			end
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   300
			if listsToRetrieve == 0 or listsToRetrieve == 1 then
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   301
				valid = getList(privacy_lists, origin, stanza, name);
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   302
			end
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   303
		end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   304
20
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
   305
		if valid ~= true then
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
   306
			if valid[0] == nil then
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
   307
				valid[0] = "cancel";
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   308
			end
20
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
   309
			if valid[1] == nil then
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
   310
				valid[1] = "bad-request";
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
   311
			end
2675dc25445b mod_privacy: overworked error returns while creating, editing, deleting or de/activating lists.
Thilo Cestonaro <thilo@cestona.ro>
parents: 19
diff changeset
   312
			origin.send(st.error_reply(stanza, valid[0], valid[1], valid[2]));
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   313
		else
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   314
			datamanager.store(origin.username, origin.host, "privacy", privacy_lists);
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   315
		end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   316
		return true;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   317
	end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   318
	return false;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   319
end, 500);
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   320
17
ccb07c0efc7e mod_privacy: prepare everything for the "is used" checking stuff...
Thilo Cestonaro <thilo@cestona.ro>
parents: 16
diff changeset
   321
function checkIfNeedToBeBlocked(e, session)
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   322
	local origin, stanza = e.origin, e.stanza;
17
ccb07c0efc7e mod_privacy: prepare everything for the "is used" checking stuff...
Thilo Cestonaro <thilo@cestona.ro>
parents: 16
diff changeset
   323
	local privacy_lists = datamanager.load(session.username, session.host, "privacy") or {};
ccb07c0efc7e mod_privacy: prepare everything for the "is used" checking stuff...
Thilo Cestonaro <thilo@cestona.ro>
parents: 16
diff changeset
   324
	local bare_jid = session.username.."@"..session.host;
39
b84b2b026eb4 mod_privacy: never block communications from one of a user's resources to another.
Thilo Cestonaro <thilo@cestona.ro>
parents: 27
diff changeset
   325
45
3f5bbd7c90d4 mod_privacy: it says "from" not "form" (thx flo for reporting!)
Thilo Cestonaro <thilo@cestona.ro>
parents: 42
diff changeset
   326
	module:log("debug", "stanza: %s, to: %s, from: %s", stanza.name, stanza.attr.to or "nil", stanza.attr.from or "nil");
11
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   327
	
27
d91cb13ef0ee mod_privacy: make the block function work; retrieve the roster groups correctly
Thilo Cestonaro <thilo@cestona.ro>
parents: 20
diff changeset
   328
	if stanza.attr.to ~= nil and stanza.attr.from ~= nil then
d91cb13ef0ee mod_privacy: make the block function work; retrieve the roster groups correctly
Thilo Cestonaro <thilo@cestona.ro>
parents: 20
diff changeset
   329
		if privacy_lists.lists == nil or
d91cb13ef0ee mod_privacy: make the block function work; retrieve the roster groups correctly
Thilo Cestonaro <thilo@cestona.ro>
parents: 20
diff changeset
   330
		   (session.activePrivacyList == nil or session.activePrivacyList == "") and
d91cb13ef0ee mod_privacy: make the block function work; retrieve the roster groups correctly
Thilo Cestonaro <thilo@cestona.ro>
parents: 20
diff changeset
   331
		   (privacy_lists.default == nil     or privacy_lists.default == "")
d91cb13ef0ee mod_privacy: make the block function work; retrieve the roster groups correctly
Thilo Cestonaro <thilo@cestona.ro>
parents: 20
diff changeset
   332
		then 
11
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   333
			return; -- Nothing to block, default is Allow all
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   334
		end
39
b84b2b026eb4 mod_privacy: never block communications from one of a user's resources to another.
Thilo Cestonaro <thilo@cestona.ro>
parents: 27
diff changeset
   335
	    if jid_bare(stanza.attr.from) == bare_jid and jid_bare(stanza.attr.to) == bare_jid then
b84b2b026eb4 mod_privacy: never block communications from one of a user's resources to another.
Thilo Cestonaro <thilo@cestona.ro>
parents: 27
diff changeset
   336
            module:log("debug", "Never block communications from one of a user's resources to another.");
b84b2b026eb4 mod_privacy: never block communications from one of a user's resources to another.
Thilo Cestonaro <thilo@cestona.ro>
parents: 27
diff changeset
   337
            return; -- from one of a user's resource to another => HANDS OFF!
b84b2b026eb4 mod_privacy: never block communications from one of a user's resources to another.
Thilo Cestonaro <thilo@cestona.ro>
parents: 27
diff changeset
   338
        end 
41
0381d5d38c37 mod_privacy: fix bug where priority was nil and compared to a number (Thx flo for reporting!)
Thilo Cestonaro <thilo@cestona.ro>
parents: 39
diff changeset
   339
11
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   340
		local idx;
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   341
		local list;
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   342
		local item;
17
ccb07c0efc7e mod_privacy: prepare everything for the "is used" checking stuff...
Thilo Cestonaro <thilo@cestona.ro>
parents: 16
diff changeset
   343
		local listname = session.activePrivacyList;
27
d91cb13ef0ee mod_privacy: make the block function work; retrieve the roster groups correctly
Thilo Cestonaro <thilo@cestona.ro>
parents: 20
diff changeset
   344
		if listname == nil or listname == "" then
11
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   345
			listname = privacy_lists.default; -- no active list selected, use default list
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   346
		end
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   347
		idx = findNamedList(privacy_lists, listname);
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   348
		if idx == nil then
27
d91cb13ef0ee mod_privacy: make the block function work; retrieve the roster groups correctly
Thilo Cestonaro <thilo@cestona.ro>
parents: 20
diff changeset
   349
			module:log("error", "given privacy listname not found. name: %s", listname);
11
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   350
			return;
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   351
		end
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   352
		list = privacy_lists.lists[idx];
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   353
		if list == nil then
27
d91cb13ef0ee mod_privacy: make the block function work; retrieve the roster groups correctly
Thilo Cestonaro <thilo@cestona.ro>
parents: 20
diff changeset
   354
			module:log("info", "privacy list index wrong. index: %d", idx);
11
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   355
			return;
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   356
		end
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   357
		for _,item in ipairs(list.items) do
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   358
			local apply = false;
17
ccb07c0efc7e mod_privacy: prepare everything for the "is used" checking stuff...
Thilo Cestonaro <thilo@cestona.ro>
parents: 16
diff changeset
   359
			local block = false;
42
bbb3d3a90a70 mod_privacy: decrease the log messages count.
Thilo Cestonaro <thilo@cestona.ro>
parents: 41
diff changeset
   360
			if (
bbb3d3a90a70 mod_privacy: decrease the log messages count.
Thilo Cestonaro <thilo@cestona.ro>
parents: 41
diff changeset
   361
				(stanza.name == "message" and item.message) or
bbb3d3a90a70 mod_privacy: decrease the log messages count.
Thilo Cestonaro <thilo@cestona.ro>
parents: 41
diff changeset
   362
				(stanza.name == "iq" and item.iq) or
bbb3d3a90a70 mod_privacy: decrease the log messages count.
Thilo Cestonaro <thilo@cestona.ro>
parents: 41
diff changeset
   363
				(stanza.name == "presence" and jid_bare(stanza.attr.to) == bare_jid and item["presence-in"]) or
bbb3d3a90a70 mod_privacy: decrease the log messages count.
Thilo Cestonaro <thilo@cestona.ro>
parents: 41
diff changeset
   364
				(stanza.name == "presence" and jid_bare(stanza.attr.from) == bare_jid and item["presence-out"]) or
bbb3d3a90a70 mod_privacy: decrease the log messages count.
Thilo Cestonaro <thilo@cestona.ro>
parents: 41
diff changeset
   365
				(item.message == false and item.iq == false and item["presence-in"] == false and item["presence-in"] == false)
bbb3d3a90a70 mod_privacy: decrease the log messages count.
Thilo Cestonaro <thilo@cestona.ro>
parents: 41
diff changeset
   366
			) then
17
ccb07c0efc7e mod_privacy: prepare everything for the "is used" checking stuff...
Thilo Cestonaro <thilo@cestona.ro>
parents: 16
diff changeset
   367
				apply = true;
11
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   368
			end
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   369
			if apply then
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   370
				local evilJid = {};
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   371
				apply = false;
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   372
				if jid_bare(stanza.attr.to) == bare_jid then
27
d91cb13ef0ee mod_privacy: make the block function work; retrieve the roster groups correctly
Thilo Cestonaro <thilo@cestona.ro>
parents: 20
diff changeset
   373
					module:log("debug", "evil jid is (from): %s", stanza.attr.from);
11
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   374
					evilJid.node, evilJid.host, evilJid.resource = jid_split(stanza.attr.from);
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   375
				else
27
d91cb13ef0ee mod_privacy: make the block function work; retrieve the roster groups correctly
Thilo Cestonaro <thilo@cestona.ro>
parents: 20
diff changeset
   376
					module:log("debug", "evil jid is (to): %s", stanza.attr.to);
11
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   377
					evilJid.node, evilJid.host, evilJid.resource = jid_split(stanza.attr.to);
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   378
				end
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   379
				if	item.type == "jid" and 
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   380
					(evilJid.node and evilJid.host and evilJid.resource and item.value == evilJid.node.."@"..evilJid.host.."/"..evilJid.resource) or
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   381
					(evilJid.node and evilJid.host and item.value == evilJid.node.."@"..evilJid.host) or
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   382
					(evilJid.host and evilJid.resource and item.value == evilJid.host.."/"..evilJid.resource) or
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   383
					(evilJid.host and item.value == evilJid.host) then
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   384
					apply = true;
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   385
					block = (item.action == "deny");
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   386
				elseif item.type == "group" then
17
ccb07c0efc7e mod_privacy: prepare everything for the "is used" checking stuff...
Thilo Cestonaro <thilo@cestona.ro>
parents: 16
diff changeset
   387
					local roster = load_roster(session.username, session.host);
27
d91cb13ef0ee mod_privacy: make the block function work; retrieve the roster groups correctly
Thilo Cestonaro <thilo@cestona.ro>
parents: 20
diff changeset
   388
					local groups = roster[evilJid.node .. "@" .. evilJid.host].groups;
d91cb13ef0ee mod_privacy: make the block function work; retrieve the roster groups correctly
Thilo Cestonaro <thilo@cestona.ro>
parents: 20
diff changeset
   389
					for group in pairs(groups) do
11
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   390
						if group == item.value then
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   391
							apply = true;
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   392
							block = (item.action == "deny");
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   393
							break;
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   394
						end
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   395
					end
27
d91cb13ef0ee mod_privacy: make the block function work; retrieve the roster groups correctly
Thilo Cestonaro <thilo@cestona.ro>
parents: 20
diff changeset
   396
				elseif item.type == "subscription" and evilJid.node ~= nil and evilJid.host ~= nil then -- we need a valid bare evil jid
d91cb13ef0ee mod_privacy: make the block function work; retrieve the roster groups correctly
Thilo Cestonaro <thilo@cestona.ro>
parents: 20
diff changeset
   397
					local roster = load_roster(session.username, session.host);
d91cb13ef0ee mod_privacy: make the block function work; retrieve the roster groups correctly
Thilo Cestonaro <thilo@cestona.ro>
parents: 20
diff changeset
   398
					if roster[evilJid.node .. "@" .. evilJid.host].subscription == item.value then
11
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   399
						apply = true;
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   400
						block = (item.action == "deny");
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   401
					end
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   402
				elseif item.type == nil then
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   403
					apply = true;
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   404
					block = (item.action == "deny");
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   405
				end
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   406
			end
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   407
			if apply then
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   408
				if block then
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   409
					module:log("info", "stanza blocked: %s, to: %s, from: %s", stanza.name, stanza.attr.to or "nil", stanza.attr.from or "nil");
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   410
					if stanza.name == "message" then
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   411
						origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   412
					elseif stanza.name == "iq" and (stanza.attr.type == "get" or stanza.attr.type == "set") then
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   413
						origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   414
					end
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   415
					return true; -- stanza blocked !
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   416
				else
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   417
					module:log("info", "stanza explicit allowed!")
27
d91cb13ef0ee mod_privacy: make the block function work; retrieve the roster groups correctly
Thilo Cestonaro <thilo@cestona.ro>
parents: 20
diff changeset
   418
					return;
11
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   419
				end
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   420
			end
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   421
		end
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   422
	end
11
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   423
	return;
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   424
end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   425
11
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   426
function preCheckIncoming(e)
17
ccb07c0efc7e mod_privacy: prepare everything for the "is used" checking stuff...
Thilo Cestonaro <thilo@cestona.ro>
parents: 16
diff changeset
   427
	local session;
11
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   428
	if e.stanza.attr.to ~= nil then
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   429
		local node, host, resource = jid_split(e.stanza.attr.to);
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   430
		if node == nil or host == nil then
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   431
			return;
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   432
		end
17
ccb07c0efc7e mod_privacy: prepare everything for the "is used" checking stuff...
Thilo Cestonaro <thilo@cestona.ro>
parents: 16
diff changeset
   433
		if resource == nil then
ccb07c0efc7e mod_privacy: prepare everything for the "is used" checking stuff...
Thilo Cestonaro <thilo@cestona.ro>
parents: 16
diff changeset
   434
			local prio = 0;
ccb07c0efc7e mod_privacy: prepare everything for the "is used" checking stuff...
Thilo Cestonaro <thilo@cestona.ro>
parents: 16
diff changeset
   435
			local session_;
18
2df11ec081fe mod_privacy: make finding the right session working
Thilo Cestonaro <thilo@cestona.ro>
parents: 17
diff changeset
   436
			if bare_sessions[node.."@"..host] ~= nil then
2df11ec081fe mod_privacy: make finding the right session working
Thilo Cestonaro <thilo@cestona.ro>
parents: 17
diff changeset
   437
				for resource, session_ in pairs(bare_sessions[node.."@"..host].sessions) do
41
0381d5d38c37 mod_privacy: fix bug where priority was nil and compared to a number (Thx flo for reporting!)
Thilo Cestonaro <thilo@cestona.ro>
parents: 39
diff changeset
   438
					if session_.priority ~= nil and session_.priority > prio then
18
2df11ec081fe mod_privacy: make finding the right session working
Thilo Cestonaro <thilo@cestona.ro>
parents: 17
diff changeset
   439
						session = session_;
2df11ec081fe mod_privacy: make finding the right session working
Thilo Cestonaro <thilo@cestona.ro>
parents: 17
diff changeset
   440
						prio = session_.priority;
2df11ec081fe mod_privacy: make finding the right session working
Thilo Cestonaro <thilo@cestona.ro>
parents: 17
diff changeset
   441
					end
17
ccb07c0efc7e mod_privacy: prepare everything for the "is used" checking stuff...
Thilo Cestonaro <thilo@cestona.ro>
parents: 16
diff changeset
   442
				end
ccb07c0efc7e mod_privacy: prepare everything for the "is used" checking stuff...
Thilo Cestonaro <thilo@cestona.ro>
parents: 16
diff changeset
   443
			end
ccb07c0efc7e mod_privacy: prepare everything for the "is used" checking stuff...
Thilo Cestonaro <thilo@cestona.ro>
parents: 16
diff changeset
   444
		else
ccb07c0efc7e mod_privacy: prepare everything for the "is used" checking stuff...
Thilo Cestonaro <thilo@cestona.ro>
parents: 16
diff changeset
   445
			session = full_sessions[node.."@"..host.."/"..resource];
ccb07c0efc7e mod_privacy: prepare everything for the "is used" checking stuff...
Thilo Cestonaro <thilo@cestona.ro>
parents: 16
diff changeset
   446
		end
18
2df11ec081fe mod_privacy: make finding the right session working
Thilo Cestonaro <thilo@cestona.ro>
parents: 17
diff changeset
   447
		if session ~= nil then
2df11ec081fe mod_privacy: make finding the right session working
Thilo Cestonaro <thilo@cestona.ro>
parents: 17
diff changeset
   448
			return checkIfNeedToBeBlocked(e, session);
2df11ec081fe mod_privacy: make finding the right session working
Thilo Cestonaro <thilo@cestona.ro>
parents: 17
diff changeset
   449
		else
2df11ec081fe mod_privacy: make finding the right session working
Thilo Cestonaro <thilo@cestona.ro>
parents: 17
diff changeset
   450
			module:log("debug", "preCheckIncoming: Couldn't get session for jid: %s@%s/%s", node or "nil", host or "nil", resource or "nil")
2df11ec081fe mod_privacy: make finding the right session working
Thilo Cestonaro <thilo@cestona.ro>
parents: 17
diff changeset
   451
		end
11
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   452
	end
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   453
	return;
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   454
end
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   455
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   456
function preCheckOutgoing(e)
39
b84b2b026eb4 mod_privacy: never block communications from one of a user's resources to another.
Thilo Cestonaro <thilo@cestona.ro>
parents: 27
diff changeset
   457
	local session = e.origin;
b84b2b026eb4 mod_privacy: never block communications from one of a user's resources to another.
Thilo Cestonaro <thilo@cestona.ro>
parents: 27
diff changeset
   458
	if e.stanza.attr.from == nil then
45
3f5bbd7c90d4 mod_privacy: it says "from" not "form" (thx flo for reporting!)
Thilo Cestonaro <thilo@cestona.ro>
parents: 42
diff changeset
   459
		e.stanza.attr.from = session.username .. "@" .. session.host;
39
b84b2b026eb4 mod_privacy: never block communications from one of a user's resources to another.
Thilo Cestonaro <thilo@cestona.ro>
parents: 27
diff changeset
   460
		if session.resource ~= nil then
45
3f5bbd7c90d4 mod_privacy: it says "from" not "form" (thx flo for reporting!)
Thilo Cestonaro <thilo@cestona.ro>
parents: 42
diff changeset
   461
		 	e.stanza.attr.from = e.stanza.attr.from .. "/" .. session.resource;
18
2df11ec081fe mod_privacy: make finding the right session working
Thilo Cestonaro <thilo@cestona.ro>
parents: 17
diff changeset
   462
		end
11
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   463
	end
39
b84b2b026eb4 mod_privacy: never block communications from one of a user's resources to another.
Thilo Cestonaro <thilo@cestona.ro>
parents: 27
diff changeset
   464
	return checkIfNeedToBeBlocked(e, session);
11
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   465
end
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   466
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   467
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   468
module:hook("pre-message/full", preCheckOutgoing, 500);
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   469
module:hook("pre-message/bare", preCheckOutgoing, 500);
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   470
module:hook("pre-message/host", preCheckOutgoing, 500);
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   471
module:hook("pre-iq/full", preCheckOutgoing, 500);
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   472
module:hook("pre-iq/bare", preCheckOutgoing, 500);
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   473
module:hook("pre-iq/host", preCheckOutgoing, 500);
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   474
module:hook("pre-presence/full", preCheckOutgoing, 500);
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   475
module:hook("pre-presence/bare", preCheckOutgoing, 500);
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   476
module:hook("pre-presence/host", preCheckOutgoing, 500);
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   477
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   478
module:hook("message/full", preCheckIncoming, 500);
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   479
module:hook("message/bare", preCheckIncoming, 500);
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   480
module:hook("message/host", preCheckIncoming, 500);
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   481
module:hook("iq/full", preCheckIncoming, 500);
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   482
module:hook("iq/bare", preCheckIncoming, 500);
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   483
module:hook("iq/host", preCheckIncoming, 500);
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   484
module:hook("presence/full", preCheckIncoming, 500);
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   485
module:hook("presence/bare", preCheckIncoming, 500);
529819205379 do the first real blocking actions
Thilo Cestonaro <thilo@cestona.ro>
parents: 10
diff changeset
   486
module:hook("presence/host", preCheckIncoming, 500);
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   487
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   488
module:log("info", "mod_privacy loaded ...");