mod_privacy/mod_privacy.lua
author Thilo Cestonaro <thilo@cestona.ro>
Fri, 25 Sep 2009 21:50:01 +0200
changeset 10 7d70faba234c
parent 8 10502594a49b
child 11 529819205379
child 14 0892941186f2
permissions -rw-r--r--
some error reporting during list editing
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
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    11
local prosody = prosody;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    12
local helpers = require "util/helpers";
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    13
local st = require "util.stanza";
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    14
local datamanager = require "util.datamanager";
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    15
local bare_sessions = bare_sessions;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    16
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    17
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
    18
function findNamedList (privacy_lists, name)
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    19
	local ret = nil
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    20
	if privacy_lists.lists == nil then return nil; end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    21
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    22
	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
    23
		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
    24
			ret = i;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    25
			break;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    26
		end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    27
	end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    28
	return ret;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    29
end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    30
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
    31
function declineList (privacy_lists, origin, stanza, which)
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    32
	module:log("info", "User requests to decline the use of privacy list: %s", which);
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    33
	privacy_lists[which] = nil;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    34
	origin.send(st.reply(stanza));
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    35
	return true;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    36
end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    37
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
    38
function activateList (privacy_lists, origin, stanza, which, name)
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    39
	module:log("info", "User requests to change the privacy list: %s, to be list named %s", which, name);
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    40
	local ret = false;
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
    41
	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
    42
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    43
	if privacy_lists[which] == nil then
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    44
		privacy_lists[which] = "";
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    45
	end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    46
	
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    47
	if privacy_lists[which] ~= name and idx ~= nil then
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    48
		privacy_lists[which] = name;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    49
		origin.send(st.reply(stanza));
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    50
		ret = true;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    51
	end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    52
	return ret;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    53
end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    54
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
    55
function deleteList (privacy_lists, origin, stanza, name)
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    56
	module:log("info", "User requests to delete privacy list: %s", name);
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    57
	local ret = false;
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
    58
	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
    59
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    60
	if idx ~= nil then
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    61
		table.remove(privacy_lists.lists, idx);
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    62
		origin.send(st.reply(stanza));
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    63
		ret = true;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    64
	end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    65
	return ret;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    66
end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    67
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
    68
local function sortByOrder(a, b)
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
    69
	if a.order < b.order then
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
    70
		return true;
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
    71
	end
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
    72
	return false;
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
    73
end
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
    74
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
    75
function createOrReplaceList (privacy_lists, origin, stanza, name, entries)
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    76
	module:log("info", "User requests to create / replace list named %s, item count: %d", name, #entries);
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    77
	local ret = true;
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
    78
	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
    79
	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
    80
	
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    81
	if privacy_lists.lists == nil then
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    82
		privacy_lists.lists = {};
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    83
	end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    84
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    85
	if idx == nil then
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    86
		idx = #privacy_lists.lists + 1;
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
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    89
	local list = {};
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    90
	list.name = name;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    91
	list.items = {};
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    92
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    93
	for _,item in ipairs(entries) do
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    94
		tmp = {};
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    95
		tmp["type"] = item.attr.type;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    96
		tmp["value"] = item.attr.value;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    97
		tmp["action"] = item.attr.action;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    98
		tmp["order"] = item.attr.order;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
    99
		tmp["presence-in"] = false;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   100
		tmp["presence-out"] = false;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   101
		tmp["message"] = false;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   102
		tmp["iq"] = false;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   103
		
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   104
		if #item.tags > 0 then
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   105
			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
   106
				tmp[tag.name] = true;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   107
			end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   108
		end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   109
		list.items[#list.items + 1] = tmp;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   110
	end
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   111
	
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   112
	table.sort(list, sortByOrder);
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   113
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   114
	privacy_lists.lists[idx] = list;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   115
	origin.send(st.reply(stanza));
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   116
	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
   117
		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
   118
		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
   119
		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
   120
		iq:up();
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   121
		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
   122
			iq.attr.to = bare_jid.."/"..resource
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   123
			session.send(iq);
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   124
		end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   125
	end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   126
	return true;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   127
end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   128
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   129
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
   130
	module:log("info", "User requests list named: %s", name or "nil");
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   131
	local ret = false;
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   132
	local reply = st.reply(stanza);
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   133
	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
   134
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   135
	if name == nil then
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   136
		reply:tag("active", {name=privacy_lists.active or ""}):up();
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   137
		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
   138
		if privacy_lists.lists then
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   139
			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
   140
				reply:tag("list", {name=list.name}):up();
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   141
			end
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   142
			ret = true;	
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   143
		end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   144
	else
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   145
		local idx = findNamedList(privacy_lists, name);
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   146
		log("debug", "list idx: %d", idx or -1);
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   147
		if idx ~= nil then
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   148
			list = privacy_lists.lists[idx];
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   149
			reply = reply:tag("list", {name=list.name});
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   150
			for _,item in ipairs(list.items) do
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   151
				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
   152
				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
   153
				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
   154
				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
   155
				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
   156
				reply:up();
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   157
			end
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   158
			ret = true;
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   159
		end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   160
	end
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   161
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   162
	if ret then
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   163
		origin.send(reply);
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   164
	end
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   165
	return ret;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   166
end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   167
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   168
--          "[tagname]/[target-type]/[payload-namespace]:[payload-tagname]"
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   169
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
   170
	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
   171
	
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   172
	if stanza.attr.to == nil then -- only service requests to own bare JID
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   173
		local err_reply = nil;
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   174
		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
   175
		local valid = false;
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   176
		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
   177
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   178
		if stanza.attr.type == "set" then
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   179
			if #query.tags >= 1 then
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   180
				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
   181
					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
   182
						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
   183
							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
   184
						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
   185
							valid = activateList(privacy_lists, origin, stanza, tag.name, tag.attr.name);
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   186
							err_reply = st.error_reply(stanza, "cancel", "item-not-found");
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   187
						end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   188
					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
   189
						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
   190
							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
   191
						else -- Client edits a privacy list
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   192
							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
   193
						end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   194
					end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   195
				end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   196
			end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   197
		elseif stanza.attr.type == "get" then
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   198
			local name = nil;
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   199
			local listsToRetrieve = 0;
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   200
			if #query.tags >= 1 then
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   201
				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
   202
					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
   203
						name = tag.attr.name;
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   204
						listsToRetrieve = listsToRetrieve + 1;
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   205
					end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   206
				end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   207
			end
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   208
			if listsToRetrieve == 0 or listsToRetrieve == 1 then
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   209
				valid = getList(privacy_lists, origin, stanza, name);
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   210
				err_reply = st.error_reply(stanza, "cancel", "item-not-found");
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   211
			end
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   212
		end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   213
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   214
		if valid == false then
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   215
			if err_reply == nil then
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   216
				err_reply = st.error_reply(stanza, "modify", "bad-request");
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   217
			end
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   218
			origin.send(err_reply);
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   219
		else
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   220
			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
   221
		end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   222
		return true;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   223
	end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   224
	return false;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   225
end, 500);
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   226
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   227
function checkIfNeedToBeBlocked(e)
10
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   228
	local origin, stanza = e.origin, e.stanza;
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   229
	local privacy_lists = datamanager.load(origin.username, origin.host, "privacy") or {};
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   230
	if privacy_lists.lists ~= nil then
7d70faba234c some error reporting during list editing
Thilo Cestonaro <thilo@cestona.ro>
parents: 8
diff changeset
   231
	end
8
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   232
	return false;
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   233
end
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   234
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   235
module:hook("pre-message/full", checkIfNeedToBeBlocked, 500);
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   236
module:hook("pre-iq/bare", checkIfNeedToBeBlocked, 500);
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   237
module:hook("pre-presence/bare", checkIfNeedToBeBlocked, 500);
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   238
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   239
-- helpers.log_events(hosts["albastru.de"].events, "albastru.de");
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   240
-- helpers.log_events(prosody.events, "*");
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   241
10502594a49b adds mod_privacy; lists creating, editing and deletion working.
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
   242
module:log("info", "mod_privacy loaded ...");