plugins/mod_privacy.lua
author Matthew Wild <mwild1@gmail.com>
Tue, 23 Apr 2013 15:14:22 +0100
changeset 5510 3758898cefdd
parent 5500 eeea0eb2602a
child 5776 bd0ff8ae98a8
child 6365 75bd55e84112
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1522
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1293
diff changeset
     1
-- Prosody IM
2925
692b3c6c5bd2 Merge 0.6->0.7
Matthew Wild <mwild1@gmail.com>
parents: 2616 2923
diff changeset
     2
-- Copyright (C) 2009-2010 Matthew Wild
692b3c6c5bd2 Merge 0.6->0.7
Matthew Wild <mwild1@gmail.com>
parents: 2616 2923
diff changeset
     3
-- Copyright (C) 2009-2010 Waqas Hussain
2496
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
     4
-- Copyright (C) 2009 Thilo Cestonaro
1522
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1293
diff changeset
     5
-- 
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1293
diff changeset
     6
-- This project is MIT/X11 licensed. Please see the
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1293
diff changeset
     7
-- COPYING file in the source package for more information.
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1293
diff changeset
     8
--
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1293
diff changeset
     9
3317
82b17b199d68 mod_privacy: Add service discovery feature.
Waqas Hussain <waqas20@gmail.com>
parents: 3313
diff changeset
    10
module:add_feature("jabber:iq:privacy");
82b17b199d68 mod_privacy: Add service discovery feature.
Waqas Hussain <waqas20@gmail.com>
parents: 3313
diff changeset
    11
1293
13dde33d5b87 mod_privacy: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    12
local st = require "util.stanza";
5370
7838acadb0fa mod_announce, mod_auth_anonymous, mod_c2s, mod_c2s, mod_component, mod_iq, mod_message, mod_presence, mod_tls: Access prosody.{hosts,bare_sessions,full_sessions} instead of the old globals
Kim Alvefur <zash@zash.se>
parents: 4232
diff changeset
    13
local bare_sessions, full_sessions = prosody.bare_sessions, prosody.full_sessions;
2496
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
    14
local util_Jid = require "util.jid";
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
    15
local jid_bare = util_Jid.bare;
3069
c3df9053e0a9 mod_privacy: Fix several possible tracebacks when either the contact is a host JID, or the contact isn't in your roster and you have roster group/subscription rules in place
Matthew Wild <mwild1@gmail.com>
parents: 3068
diff changeset
    16
local jid_split, jid_join = util_Jid.split, util_Jid.join;
2496
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
    17
local load_roster = require "core.rostermanager".load_roster;
2498
3d08a6cf57ea mod_privacy: Fixed global access.
Waqas Hussain <waqas20@gmail.com>
parents: 2496
diff changeset
    18
local to_number = tonumber;
2496
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
    19
5500
eeea0eb2602a mod_auth_internal_hashed, mod_auth_internal_plain, mod_privacy, mod_private, mod_register, mod_vcard, mod_muc: Use module:open_store()
Kim Alvefur <zash@zash.se>
parents: 5393
diff changeset
    20
local privacy_storage = module:open_store();
eeea0eb2602a mod_auth_internal_hashed, mod_auth_internal_plain, mod_privacy, mod_private, mod_register, mod_vcard, mod_muc: Use module:open_store()
Kim Alvefur <zash@zash.se>
parents: 5393
diff changeset
    21
2500
bffdaeb7ab5e mod_privacy: Cleaned up code.
Waqas Hussain <waqas20@gmail.com>
parents: 2499
diff changeset
    22
function isListUsed(origin, name, privacy_lists)
bffdaeb7ab5e mod_privacy: Cleaned up code.
Waqas Hussain <waqas20@gmail.com>
parents: 2499
diff changeset
    23
	local user = bare_sessions[origin.username.."@"..origin.host];
bffdaeb7ab5e mod_privacy: Cleaned up code.
Waqas Hussain <waqas20@gmail.com>
parents: 2499
diff changeset
    24
	if user then
bffdaeb7ab5e mod_privacy: Cleaned up code.
Waqas Hussain <waqas20@gmail.com>
parents: 2499
diff changeset
    25
		for resource, session in pairs(user.sessions) do
2496
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
    26
			if resource ~= origin.resource then
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
    27
				if session.activePrivacyList == name then
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
    28
					return true;
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
    29
				elseif session.activePrivacyList == nil and privacy_lists.default == name then
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
    30
					return true;
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
    31
				end
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
    32
			end
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
    33
		end
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
    34
	end
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
    35
end
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
    36
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
    37
function isAnotherSessionUsingDefaultList(origin)
2500
bffdaeb7ab5e mod_privacy: Cleaned up code.
Waqas Hussain <waqas20@gmail.com>
parents: 2499
diff changeset
    38
	local user = bare_sessions[origin.username.."@"..origin.host];
bffdaeb7ab5e mod_privacy: Cleaned up code.
Waqas Hussain <waqas20@gmail.com>
parents: 2499
diff changeset
    39
	if user then
bffdaeb7ab5e mod_privacy: Cleaned up code.
Waqas Hussain <waqas20@gmail.com>
parents: 2499
diff changeset
    40
		for resource, session in pairs(user.sessions) do
2496
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
    41
			if resource ~= origin.resource and session.activePrivacyList == nil then
2500
bffdaeb7ab5e mod_privacy: Cleaned up code.
Waqas Hussain <waqas20@gmail.com>
parents: 2499
diff changeset
    42
				return true;
2496
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
    43
			end
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
    44
		end
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
    45
	end
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
    46
end
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
    47
2500
bffdaeb7ab5e mod_privacy: Cleaned up code.
Waqas Hussain <waqas20@gmail.com>
parents: 2499
diff changeset
    48
function declineList(privacy_lists, origin, stanza, which)
2496
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
    49
	if which == "default" then
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
    50
		if isAnotherSessionUsingDefaultList(origin) then
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
    51
			return { "cancel", "conflict", "Another session is online and using the default list."};
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
    52
		end
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
    53
		privacy_lists.default = nil;
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
    54
		origin.send(st.reply(stanza));
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
    55
	elseif which == "active" then
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
    56
		origin.activePrivacyList = nil;
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
    57
		origin.send(st.reply(stanza));
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
    58
	else
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
    59
		return {"modify", "bad-request", "Neither default nor active list specifed to decline."};
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
    60
	end
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
    61
	return true;
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
    62
end
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
    63
2500
bffdaeb7ab5e mod_privacy: Cleaned up code.
Waqas Hussain <waqas20@gmail.com>
parents: 2499
diff changeset
    64
function activateList(privacy_lists, origin, stanza, which, name)
2588
741302471a23 mod_privacy: Don't store the privacy lists as an array, but as a map... faster and less code
Matthew Wild <mwild1@gmail.com>
parents: 2579
diff changeset
    65
	local list = privacy_lists.lists[name];
2496
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
    66
2588
741302471a23 mod_privacy: Don't store the privacy lists as an array, but as a map... faster and less code
Matthew Wild <mwild1@gmail.com>
parents: 2579
diff changeset
    67
	if which == "default" and list then
2496
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
    68
		if isAnotherSessionUsingDefaultList(origin) then
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
    69
			return {"cancel", "conflict", "Another session is online and using the default list."};
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
    70
		end
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
    71
		privacy_lists.default = name;
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
    72
		origin.send(st.reply(stanza));
2588
741302471a23 mod_privacy: Don't store the privacy lists as an array, but as a map... faster and less code
Matthew Wild <mwild1@gmail.com>
parents: 2579
diff changeset
    73
	elseif which == "active" and list then
2496
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
    74
		origin.activePrivacyList = name;
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
    75
		origin.send(st.reply(stanza));
3313
9bcf8d612a52 mod_privacy: Return the correct item-not-found instead of bad-request when a non-existent list is activated by the client (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents: 3300
diff changeset
    76
	elseif not list then
9bcf8d612a52 mod_privacy: Return the correct item-not-found instead of bad-request when a non-existent list is activated by the client (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents: 3300
diff changeset
    77
		return {"cancel", "item-not-found", "No such list: "..name};
2496
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
    78
	else
3313
9bcf8d612a52 mod_privacy: Return the correct item-not-found instead of bad-request when a non-existent list is activated by the client (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents: 3300
diff changeset
    79
		return {"modify", "bad-request", "No list chosen to be active or default."};
2496
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
    80
	end
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
    81
	return true;
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
    82
end
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
    83
2500
bffdaeb7ab5e mod_privacy: Cleaned up code.
Waqas Hussain <waqas20@gmail.com>
parents: 2499
diff changeset
    84
function deleteList(privacy_lists, origin, stanza, name)
2588
741302471a23 mod_privacy: Don't store the privacy lists as an array, but as a map... faster and less code
Matthew Wild <mwild1@gmail.com>
parents: 2579
diff changeset
    85
	local list = privacy_lists.lists[name];
2496
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
    86
2588
741302471a23 mod_privacy: Don't store the privacy lists as an array, but as a map... faster and less code
Matthew Wild <mwild1@gmail.com>
parents: 2579
diff changeset
    87
	if list then
2496
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
    88
		if isListUsed(origin, name, privacy_lists) then
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
    89
			return {"cancel", "conflict", "Another session is online and using the list which should be deleted."};
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
    90
		end
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
    91
		if privacy_lists.default == name then
2590
2d7e2a28d1d5 mod_privacy: Remove use of to identify unspecified privacy list
Matthew Wild <mwild1@gmail.com>
parents: 2589
diff changeset
    92
			privacy_lists.default = nil;
2496
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
    93
		end
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
    94
		if origin.activePrivacyList == name then
2590
2d7e2a28d1d5 mod_privacy: Remove use of to identify unspecified privacy list
Matthew Wild <mwild1@gmail.com>
parents: 2589
diff changeset
    95
			origin.activePrivacyList = nil;
2496
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
    96
		end
2588
741302471a23 mod_privacy: Don't store the privacy lists as an array, but as a map... faster and less code
Matthew Wild <mwild1@gmail.com>
parents: 2579
diff changeset
    97
		privacy_lists.lists[name] = nil;
2496
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
    98
		origin.send(st.reply(stanza));
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
    99
		return true;
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   100
	end
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   101
	return {"modify", "bad-request", "Not existing list specifed to be deleted."};
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   102
end
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   103
4232
582fe508214f mod_privacy: Remove several unused variable declarations
Matthew Wild <mwild1@gmail.com>
parents: 4231
diff changeset
   104
function createOrReplaceList (privacy_lists, origin, stanza, name, entries)
2496
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   105
	local bare_jid = origin.username.."@"..origin.host;
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   106
	
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   107
	if privacy_lists.lists == nil then
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   108
		privacy_lists.lists = {};
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   109
	end
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   110
2588
741302471a23 mod_privacy: Don't store the privacy lists as an array, but as a map... faster and less code
Matthew Wild <mwild1@gmail.com>
parents: 2579
diff changeset
   111
	local list = {};
741302471a23 mod_privacy: Don't store the privacy lists as an array, but as a map... faster and less code
Matthew Wild <mwild1@gmail.com>
parents: 2579
diff changeset
   112
	privacy_lists.lists[name] = list;
2496
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   113
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   114
	local orderCheck = {};
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   115
	list.name = name;
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   116
	list.items = {};
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   117
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   118
	for _,item in ipairs(entries) do
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   119
		if to_number(item.attr.order) == nil or to_number(item.attr.order) < 0 or orderCheck[item.attr.order] ~= nil then
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   120
			return {"modify", "bad-request", "Order attribute not valid."};
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   121
		end
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   122
		
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   123
		if item.attr.type ~= nil and item.attr.type ~= "jid" and item.attr.type ~= "subscription" and item.attr.type ~= "group" then
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   124
			return {"modify", "bad-request", "Type attribute not valid."};
2516
68a889016a46 mod_privacy: Trailing whitespace (I can't sleep)
Matthew Wild <mwild1@gmail.com>
parents: 2502
diff changeset
   125
		end
2496
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   126
		
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   127
		local tmp = {};
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   128
		orderCheck[item.attr.order] = true;
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   129
		
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   130
		tmp["type"] = item.attr.type;
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   131
		tmp["value"] = item.attr.value;
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   132
		tmp["action"] = item.attr.action;
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   133
		tmp["order"] = to_number(item.attr.order);
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   134
		tmp["presence-in"] = false;
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   135
		tmp["presence-out"] = false;
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   136
		tmp["message"] = false;
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   137
		tmp["iq"] = false;
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   138
		
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   139
		if #item.tags > 0 then
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   140
			for _,tag in ipairs(item.tags) do
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   141
				tmp[tag.name] = true;
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   142
			end
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   143
		end
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   144
		
3068
fc7c693e094a mod_privacy: Remove validation that checks a roster group you block actually exists - it would be time-consuming on large rosters, and isn't important (or correct?) anyway.
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
   145
		if tmp.type == "subscription" then
2496
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   146
			if	tmp.value ~= "both" and
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   147
				tmp.value ~= "to" and
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   148
				tmp.value ~= "from" and
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   149
				tmp.value ~= "none" then
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   150
				return {"cancel", "bad-request", "Subscription value must be both, to, from or none."};
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   151
			end
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   152
		end
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   153
		
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   154
		if tmp.action ~= "deny" and tmp.action ~= "allow" then
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   155
			return {"cancel", "bad-request", "Action must be either deny or allow."};
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   156
		end
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   157
		list.items[#list.items + 1] = tmp;
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   158
	end
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   159
	
2499
145d62abdbfc mod_privacy: Reduced a function.
Waqas Hussain <waqas20@gmail.com>
parents: 2498
diff changeset
   160
	table.sort(list, function(a, b) return a.order < b.order; end);
2496
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   161
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   162
	origin.send(st.reply(stanza));
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   163
	if bare_sessions[bare_jid] ~= nil then
2498
3d08a6cf57ea mod_privacy: Fixed global access.
Waqas Hussain <waqas20@gmail.com>
parents: 2496
diff changeset
   164
		local iq = st.iq ( { type = "set", id="push1" } );
2496
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   165
		iq:tag ("query", { xmlns = "jabber:iq:privacy" } );
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   166
		iq:tag ("list", { name = list.name } ):up();
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   167
		iq:up();
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   168
		for resource, session in pairs(bare_sessions[bare_jid].sessions) do
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   169
			iq.attr.to = bare_jid.."/"..resource
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   170
			session.send(iq);
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   171
		end
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   172
	else
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   173
		return {"cancel", "bad-request", "internal error."};
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   174
	end
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   175
	return true;
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   176
end
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   177
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   178
function getList(privacy_lists, origin, stanza, name)
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   179
	local reply = st.reply(stanza);
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   180
	reply:tag("query", {xmlns="jabber:iq:privacy"});
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   181
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   182
	if name == nil then
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   183
		if privacy_lists.lists then
3453
fba130c16818 mod_privacy: Fix typo causing <active/> to never be send
Florian Zeitz <florob@babelmonkeys.de>
parents: 3318
diff changeset
   184
			if origin.activePrivacyList then
2590
2d7e2a28d1d5 mod_privacy: Remove use of to identify unspecified privacy list
Matthew Wild <mwild1@gmail.com>
parents: 2589
diff changeset
   185
				reply:tag("active", {name=origin.activePrivacyList}):up();
2d7e2a28d1d5 mod_privacy: Remove use of to identify unspecified privacy list
Matthew Wild <mwild1@gmail.com>
parents: 2589
diff changeset
   186
			end
2d7e2a28d1d5 mod_privacy: Remove use of to identify unspecified privacy list
Matthew Wild <mwild1@gmail.com>
parents: 2589
diff changeset
   187
			if privacy_lists.default then
2d7e2a28d1d5 mod_privacy: Remove use of to identify unspecified privacy list
Matthew Wild <mwild1@gmail.com>
parents: 2589
diff changeset
   188
				reply:tag("default", {name=privacy_lists.default}):up();
2d7e2a28d1d5 mod_privacy: Remove use of to identify unspecified privacy list
Matthew Wild <mwild1@gmail.com>
parents: 2589
diff changeset
   189
			end
2615
c5f7c803fe7d mod_privacy: Fix to correctly iterate over lists stored in new format
Matthew Wild <mwild1@gmail.com>
parents: 2614
diff changeset
   190
			for name,list in pairs(privacy_lists.lists) do
c5f7c803fe7d mod_privacy: Fix to correctly iterate over lists stored in new format
Matthew Wild <mwild1@gmail.com>
parents: 2614
diff changeset
   191
				reply:tag("list", {name=name}):up();
2496
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   192
			end
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   193
		end
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   194
	else
2588
741302471a23 mod_privacy: Don't store the privacy lists as an array, but as a map... faster and less code
Matthew Wild <mwild1@gmail.com>
parents: 2579
diff changeset
   195
		local list = privacy_lists.lists[name];
741302471a23 mod_privacy: Don't store the privacy lists as an array, but as a map... faster and less code
Matthew Wild <mwild1@gmail.com>
parents: 2579
diff changeset
   196
		if list then
2496
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   197
			reply = reply:tag("list", {name=list.name});
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   198
			for _,item in ipairs(list.items) do
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   199
				reply:tag("item", {type=item.type, value=item.value, action=item.action, order=item.order});
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   200
				if item["message"] then reply:tag("message"):up(); end
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   201
				if item["iq"] then reply:tag("iq"):up(); end
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   202
				if item["presence-in"] then reply:tag("presence-in"):up(); end
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   203
				if item["presence-out"] then reply:tag("presence-out"):up(); end
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   204
				reply:up();
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   205
			end
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   206
		else
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   207
			return {"cancel", "item-not-found", "Unknown list specified."};
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   208
		end
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   209
	end
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   210
	
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   211
	origin.send(reply);
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   212
	return true;
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   213
end
1293
13dde33d5b87 mod_privacy: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   214
13dde33d5b87 mod_privacy: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   215
module:hook("iq/bare/jabber:iq:privacy:query", function(data)
13dde33d5b87 mod_privacy: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   216
	local origin, stanza = data.origin, data.stanza;
13dde33d5b87 mod_privacy: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   217
	
2496
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   218
	if stanza.attr.to == nil then -- only service requests to own bare JID
1293
13dde33d5b87 mod_privacy: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   219
		local query = stanza.tags[1]; -- the query element
2496
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   220
		local valid = false;
5500
eeea0eb2602a mod_auth_internal_hashed, mod_auth_internal_plain, mod_privacy, mod_private, mod_register, mod_vcard, mod_muc: Use module:open_store()
Kim Alvefur <zash@zash.se>
parents: 5393
diff changeset
   221
		local privacy_lists = privacy_storage:get(origin.username) or { lists = {} };
2614
7a589e9e2372 mod_privacy: Upgrade old privacy lists to new storage format, so they don't get lost
Matthew Wild <mwild1@gmail.com>
parents: 2590
diff changeset
   222
7a589e9e2372 mod_privacy: Upgrade old privacy lists to new storage format, so they don't get lost
Matthew Wild <mwild1@gmail.com>
parents: 2590
diff changeset
   223
		if privacy_lists.lists[1] then -- Code to migrate from old privacy lists format, remove in 0.8
7a589e9e2372 mod_privacy: Upgrade old privacy lists to new storage format, so they don't get lost
Matthew Wild <mwild1@gmail.com>
parents: 2590
diff changeset
   224
			module:log("info", "Upgrading format of stored privacy lists for %s@%s", origin.username, origin.host);
7a589e9e2372 mod_privacy: Upgrade old privacy lists to new storage format, so they don't get lost
Matthew Wild <mwild1@gmail.com>
parents: 2590
diff changeset
   225
			local lists = privacy_lists.lists;
7a589e9e2372 mod_privacy: Upgrade old privacy lists to new storage format, so they don't get lost
Matthew Wild <mwild1@gmail.com>
parents: 2590
diff changeset
   226
			for idx, list in ipairs(lists) do
7a589e9e2372 mod_privacy: Upgrade old privacy lists to new storage format, so they don't get lost
Matthew Wild <mwild1@gmail.com>
parents: 2590
diff changeset
   227
				lists[list.name] = list;
7a589e9e2372 mod_privacy: Upgrade old privacy lists to new storage format, so they don't get lost
Matthew Wild <mwild1@gmail.com>
parents: 2590
diff changeset
   228
				lists[idx] = nil;
7a589e9e2372 mod_privacy: Upgrade old privacy lists to new storage format, so they don't get lost
Matthew Wild <mwild1@gmail.com>
parents: 2590
diff changeset
   229
			end
7a589e9e2372 mod_privacy: Upgrade old privacy lists to new storage format, so they don't get lost
Matthew Wild <mwild1@gmail.com>
parents: 2590
diff changeset
   230
		end
2496
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   231
1293
13dde33d5b87 mod_privacy: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   232
		if stanza.attr.type == "set" then
2516
68a889016a46 mod_privacy: Trailing whitespace (I can't sleep)
Matthew Wild <mwild1@gmail.com>
parents: 2502
diff changeset
   233
			if #query.tags == 1 then --  the <query/> element MUST NOT include more than one child element
2496
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   234
				for _,tag in ipairs(query.tags) do
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   235
					if tag.name == "active" or tag.name == "default" then
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   236
						if tag.attr.name == nil then -- Client declines the use of active / default list
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   237
							valid = declineList(privacy_lists, origin, stanza, tag.name);
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   238
						else -- Client requests change of active / default list
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   239
							valid = activateList(privacy_lists, origin, stanza, tag.name, tag.attr.name);
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   240
						end
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   241
					elseif tag.name == "list" and tag.attr.name then -- Client adds / edits a privacy list
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   242
						if #tag.tags == 0 then -- Client removes a privacy list
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   243
							valid = deleteList(privacy_lists, origin, stanza, tag.attr.name);
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   244
						else -- Client edits a privacy list
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   245
							valid = createOrReplaceList(privacy_lists, origin, stanza, tag.attr.name, tag.tags);
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   246
						end
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   247
					end
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   248
				end
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   249
			end
1293
13dde33d5b87 mod_privacy: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   250
		elseif stanza.attr.type == "get" then
2496
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   251
			local name = nil;
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   252
			local listsToRetrieve = 0;
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   253
			if #query.tags >= 1 then
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   254
				for _,tag in ipairs(query.tags) do
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   255
					if tag.name == "list" then -- Client requests a privacy list from server
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   256
						name = tag.attr.name;
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   257
						listsToRetrieve = listsToRetrieve + 1;
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   258
					end
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   259
				end
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   260
			end
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   261
			if listsToRetrieve == 0 or listsToRetrieve == 1 then
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   262
				valid = getList(privacy_lists, origin, stanza, name);
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   263
			end
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   264
		end
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   265
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   266
		if valid ~= true then
2589
39f5a068dc41 mod_privacy: Fix potential traceback in stanza validation, and fix some zero-based indexing :)
Matthew Wild <mwild1@gmail.com>
parents: 2588
diff changeset
   267
			valid = valid or { "cancel", "bad-request", "Couldn't understand request" };
2496
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   268
			if valid[1] == nil then
2589
39f5a068dc41 mod_privacy: Fix potential traceback in stanza validation, and fix some zero-based indexing :)
Matthew Wild <mwild1@gmail.com>
parents: 2588
diff changeset
   269
				valid[1] = "cancel";
2496
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   270
			end
2589
39f5a068dc41 mod_privacy: Fix potential traceback in stanza validation, and fix some zero-based indexing :)
Matthew Wild <mwild1@gmail.com>
parents: 2588
diff changeset
   271
			if valid[2] == nil then
39f5a068dc41 mod_privacy: Fix potential traceback in stanza validation, and fix some zero-based indexing :)
Matthew Wild <mwild1@gmail.com>
parents: 2588
diff changeset
   272
				valid[2] = "bad-request";
39f5a068dc41 mod_privacy: Fix potential traceback in stanza validation, and fix some zero-based indexing :)
Matthew Wild <mwild1@gmail.com>
parents: 2588
diff changeset
   273
			end
39f5a068dc41 mod_privacy: Fix potential traceback in stanza validation, and fix some zero-based indexing :)
Matthew Wild <mwild1@gmail.com>
parents: 2588
diff changeset
   274
			origin.send(st.error_reply(stanza, valid[1], valid[2], valid[3]));
2496
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   275
		else
5500
eeea0eb2602a mod_auth_internal_hashed, mod_auth_internal_plain, mod_privacy, mod_private, mod_register, mod_vcard, mod_muc: Use module:open_store()
Kim Alvefur <zash@zash.se>
parents: 5393
diff changeset
   276
			privacy_storage:set(origin.username, privacy_lists);
2496
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   277
		end
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   278
		return true;
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   279
	end
2500
bffdaeb7ab5e mod_privacy: Cleaned up code.
Waqas Hussain <waqas20@gmail.com>
parents: 2499
diff changeset
   280
end);
2496
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   281
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   282
function checkIfNeedToBeBlocked(e, session)
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   283
	local origin, stanza = e.origin, e.stanza;
5500
eeea0eb2602a mod_auth_internal_hashed, mod_auth_internal_plain, mod_privacy, mod_private, mod_register, mod_vcard, mod_muc: Use module:open_store()
Kim Alvefur <zash@zash.se>
parents: 5393
diff changeset
   284
	local privacy_lists = privacy_storage:get(session.username) or {};
2496
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   285
	local bare_jid = session.username.."@"..session.host;
3300
b651f8290e27 mod_privacy: Treat stanzas with no 'to' address as going to the user's bare JID (thanks KSid)
Matthew Wild <mwild1@gmail.com>
parents: 3085
diff changeset
   286
	local to = stanza.attr.to or bare_jid;
2523
0c816b271208 mod_privacy: Remove the giant if/end block that was really causing problems - skipping stanzas with no to/from (like presence broadcast). Also optimised the code a *lot*, these changes untested at the moment though.
Matthew Wild <mwild1@gmail.com>
parents: 2522
diff changeset
   287
	local from = stanza.attr.from;
0c816b271208 mod_privacy: Remove the giant if/end block that was really causing problems - skipping stanzas with no to/from (like presence broadcast). Also optimised the code a *lot*, these changes untested at the moment though.
Matthew Wild <mwild1@gmail.com>
parents: 2522
diff changeset
   288
	
2579
4e0caff152d6 mod_privacy: Rename from_user/to_user flags to is_from_user/is_to_user for clarity
Matthew Wild <mwild1@gmail.com>
parents: 2524
diff changeset
   289
	local is_to_user = bare_jid == jid_bare(to);
4e0caff152d6 mod_privacy: Rename from_user/to_user flags to is_from_user/is_to_user for clarity
Matthew Wild <mwild1@gmail.com>
parents: 2524
diff changeset
   290
	local is_from_user = bare_jid == jid_bare(from);
2523
0c816b271208 mod_privacy: Remove the giant if/end block that was really causing problems - skipping stanzas with no to/from (like presence broadcast). Also optimised the code a *lot*, these changes untested at the moment though.
Matthew Wild <mwild1@gmail.com>
parents: 2522
diff changeset
   291
	
3085
cf01b0725272 mod_privacy: Improved logging.
Waqas Hussain <waqas20@gmail.com>
parents: 3069
diff changeset
   292
	--module:log("debug", "stanza: %s, to: %s, from: %s", tostring(stanza.name), tostring(to), tostring(from));
2523
0c816b271208 mod_privacy: Remove the giant if/end block that was really causing problems - skipping stanzas with no to/from (like presence broadcast). Also optimised the code a *lot*, these changes untested at the moment though.
Matthew Wild <mwild1@gmail.com>
parents: 2522
diff changeset
   293
	
0c816b271208 mod_privacy: Remove the giant if/end block that was really causing problems - skipping stanzas with no to/from (like presence broadcast). Also optimised the code a *lot*, these changes untested at the moment though.
Matthew Wild <mwild1@gmail.com>
parents: 2522
diff changeset
   294
	if privacy_lists.lists == nil or
2590
2d7e2a28d1d5 mod_privacy: Remove use of to identify unspecified privacy list
Matthew Wild <mwild1@gmail.com>
parents: 2589
diff changeset
   295
		not (session.activePrivacyList or privacy_lists.default)
2523
0c816b271208 mod_privacy: Remove the giant if/end block that was really causing problems - skipping stanzas with no to/from (like presence broadcast). Also optimised the code a *lot*, these changes untested at the moment though.
Matthew Wild <mwild1@gmail.com>
parents: 2522
diff changeset
   296
	then
0c816b271208 mod_privacy: Remove the giant if/end block that was really causing problems - skipping stanzas with no to/from (like presence broadcast). Also optimised the code a *lot*, these changes untested at the moment though.
Matthew Wild <mwild1@gmail.com>
parents: 2522
diff changeset
   297
		return; -- Nothing to block, default is Allow all
0c816b271208 mod_privacy: Remove the giant if/end block that was really causing problems - skipping stanzas with no to/from (like presence broadcast). Also optimised the code a *lot*, these changes untested at the moment though.
Matthew Wild <mwild1@gmail.com>
parents: 2522
diff changeset
   298
	end
2579
4e0caff152d6 mod_privacy: Rename from_user/to_user flags to is_from_user/is_to_user for clarity
Matthew Wild <mwild1@gmail.com>
parents: 2524
diff changeset
   299
	if is_from_user and is_to_user then
3085
cf01b0725272 mod_privacy: Improved logging.
Waqas Hussain <waqas20@gmail.com>
parents: 3069
diff changeset
   300
		--module:log("debug", "Not blocking communications between user's resources");
2523
0c816b271208 mod_privacy: Remove the giant if/end block that was really causing problems - skipping stanzas with no to/from (like presence broadcast). Also optimised the code a *lot*, these changes untested at the moment though.
Matthew Wild <mwild1@gmail.com>
parents: 2522
diff changeset
   301
		return; -- from one of a user's resource to another => HANDS OFF!
0c816b271208 mod_privacy: Remove the giant if/end block that was really causing problems - skipping stanzas with no to/from (like presence broadcast). Also optimised the code a *lot*, these changes untested at the moment though.
Matthew Wild <mwild1@gmail.com>
parents: 2522
diff changeset
   302
	end
2496
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   303
	
2523
0c816b271208 mod_privacy: Remove the giant if/end block that was really causing problems - skipping stanzas with no to/from (like presence broadcast). Also optimised the code a *lot*, these changes untested at the moment though.
Matthew Wild <mwild1@gmail.com>
parents: 2522
diff changeset
   304
	local listname = session.activePrivacyList;
2590
2d7e2a28d1d5 mod_privacy: Remove use of to identify unspecified privacy list
Matthew Wild <mwild1@gmail.com>
parents: 2589
diff changeset
   305
	if listname == nil then
2523
0c816b271208 mod_privacy: Remove the giant if/end block that was really causing problems - skipping stanzas with no to/from (like presence broadcast). Also optimised the code a *lot*, these changes untested at the moment though.
Matthew Wild <mwild1@gmail.com>
parents: 2522
diff changeset
   306
		listname = privacy_lists.default; -- no active list selected, use default list
0c816b271208 mod_privacy: Remove the giant if/end block that was really causing problems - skipping stanzas with no to/from (like presence broadcast). Also optimised the code a *lot*, these changes untested at the moment though.
Matthew Wild <mwild1@gmail.com>
parents: 2522
diff changeset
   307
	end
2588
741302471a23 mod_privacy: Don't store the privacy lists as an array, but as a map... faster and less code
Matthew Wild <mwild1@gmail.com>
parents: 2579
diff changeset
   308
	local list = privacy_lists.lists[listname];
3085
cf01b0725272 mod_privacy: Improved logging.
Waqas Hussain <waqas20@gmail.com>
parents: 3069
diff changeset
   309
	if not list then -- should never happen
cf01b0725272 mod_privacy: Improved logging.
Waqas Hussain <waqas20@gmail.com>
parents: 3069
diff changeset
   310
		module:log("warn", "given privacy list not found. name: %s for user %s", listname, bare_jid);
2523
0c816b271208 mod_privacy: Remove the giant if/end block that was really causing problems - skipping stanzas with no to/from (like presence broadcast). Also optimised the code a *lot*, these changes untested at the moment though.
Matthew Wild <mwild1@gmail.com>
parents: 2522
diff changeset
   311
		return;
0c816b271208 mod_privacy: Remove the giant if/end block that was really causing problems - skipping stanzas with no to/from (like presence broadcast). Also optimised the code a *lot*, these changes untested at the moment though.
Matthew Wild <mwild1@gmail.com>
parents: 2522
diff changeset
   312
	end
0c816b271208 mod_privacy: Remove the giant if/end block that was really causing problems - skipping stanzas with no to/from (like presence broadcast). Also optimised the code a *lot*, these changes untested at the moment though.
Matthew Wild <mwild1@gmail.com>
parents: 2522
diff changeset
   313
	for _,item in ipairs(list.items) do
0c816b271208 mod_privacy: Remove the giant if/end block that was really causing problems - skipping stanzas with no to/from (like presence broadcast). Also optimised the code a *lot*, these changes untested at the moment though.
Matthew Wild <mwild1@gmail.com>
parents: 2522
diff changeset
   314
		local apply = false;
0c816b271208 mod_privacy: Remove the giant if/end block that was really causing problems - skipping stanzas with no to/from (like presence broadcast). Also optimised the code a *lot*, these changes untested at the moment though.
Matthew Wild <mwild1@gmail.com>
parents: 2522
diff changeset
   315
		local block = false;
0c816b271208 mod_privacy: Remove the giant if/end block that was really causing problems - skipping stanzas with no to/from (like presence broadcast). Also optimised the code a *lot*, these changes untested at the moment though.
Matthew Wild <mwild1@gmail.com>
parents: 2522
diff changeset
   316
		if (
0c816b271208 mod_privacy: Remove the giant if/end block that was really causing problems - skipping stanzas with no to/from (like presence broadcast). Also optimised the code a *lot*, these changes untested at the moment though.
Matthew Wild <mwild1@gmail.com>
parents: 2522
diff changeset
   317
			(stanza.name == "message" and item.message) or
0c816b271208 mod_privacy: Remove the giant if/end block that was really causing problems - skipping stanzas with no to/from (like presence broadcast). Also optimised the code a *lot*, these changes untested at the moment though.
Matthew Wild <mwild1@gmail.com>
parents: 2522
diff changeset
   318
			(stanza.name == "iq" and item.iq) or
2579
4e0caff152d6 mod_privacy: Rename from_user/to_user flags to is_from_user/is_to_user for clarity
Matthew Wild <mwild1@gmail.com>
parents: 2524
diff changeset
   319
			(stanza.name == "presence" and is_to_user and item["presence-in"]) or
4e0caff152d6 mod_privacy: Rename from_user/to_user flags to is_from_user/is_to_user for clarity
Matthew Wild <mwild1@gmail.com>
parents: 2524
diff changeset
   320
			(stanza.name == "presence" and is_from_user and item["presence-out"]) or
2523
0c816b271208 mod_privacy: Remove the giant if/end block that was really causing problems - skipping stanzas with no to/from (like presence broadcast). Also optimised the code a *lot*, these changes untested at the moment though.
Matthew Wild <mwild1@gmail.com>
parents: 2522
diff changeset
   321
			(item.message == false and item.iq == false and item["presence-in"] == false and item["presence-out"] == false)
0c816b271208 mod_privacy: Remove the giant if/end block that was really causing problems - skipping stanzas with no to/from (like presence broadcast). Also optimised the code a *lot*, these changes untested at the moment though.
Matthew Wild <mwild1@gmail.com>
parents: 2522
diff changeset
   322
		) then
0c816b271208 mod_privacy: Remove the giant if/end block that was really causing problems - skipping stanzas with no to/from (like presence broadcast). Also optimised the code a *lot*, these changes untested at the moment though.
Matthew Wild <mwild1@gmail.com>
parents: 2522
diff changeset
   323
			apply = true;
2496
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   324
		end
2523
0c816b271208 mod_privacy: Remove the giant if/end block that was really causing problems - skipping stanzas with no to/from (like presence broadcast). Also optimised the code a *lot*, these changes untested at the moment though.
Matthew Wild <mwild1@gmail.com>
parents: 2522
diff changeset
   325
		if apply then
0c816b271208 mod_privacy: Remove the giant if/end block that was really causing problems - skipping stanzas with no to/from (like presence broadcast). Also optimised the code a *lot*, these changes untested at the moment though.
Matthew Wild <mwild1@gmail.com>
parents: 2522
diff changeset
   326
			local evilJid = {};
0c816b271208 mod_privacy: Remove the giant if/end block that was really causing problems - skipping stanzas with no to/from (like presence broadcast). Also optimised the code a *lot*, these changes untested at the moment though.
Matthew Wild <mwild1@gmail.com>
parents: 2522
diff changeset
   327
			apply = false;
2579
4e0caff152d6 mod_privacy: Rename from_user/to_user flags to is_from_user/is_to_user for clarity
Matthew Wild <mwild1@gmail.com>
parents: 2524
diff changeset
   328
			if is_to_user then
3085
cf01b0725272 mod_privacy: Improved logging.
Waqas Hussain <waqas20@gmail.com>
parents: 3069
diff changeset
   329
				--module:log("debug", "evil jid is (from): %s", from);
2523
0c816b271208 mod_privacy: Remove the giant if/end block that was really causing problems - skipping stanzas with no to/from (like presence broadcast). Also optimised the code a *lot*, these changes untested at the moment though.
Matthew Wild <mwild1@gmail.com>
parents: 2522
diff changeset
   330
				evilJid.node, evilJid.host, evilJid.resource = jid_split(from);
1293
13dde33d5b87 mod_privacy: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   331
			else
3085
cf01b0725272 mod_privacy: Improved logging.
Waqas Hussain <waqas20@gmail.com>
parents: 3069
diff changeset
   332
				--module:log("debug", "evil jid is (to): %s", to);
2523
0c816b271208 mod_privacy: Remove the giant if/end block that was really causing problems - skipping stanzas with no to/from (like presence broadcast). Also optimised the code a *lot*, these changes untested at the moment though.
Matthew Wild <mwild1@gmail.com>
parents: 2522
diff changeset
   333
				evilJid.node, evilJid.host, evilJid.resource = jid_split(to);
2496
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   334
			end
2523
0c816b271208 mod_privacy: Remove the giant if/end block that was really causing problems - skipping stanzas with no to/from (like presence broadcast). Also optimised the code a *lot*, these changes untested at the moment though.
Matthew Wild <mwild1@gmail.com>
parents: 2522
diff changeset
   335
			if	item.type == "jid" and
0c816b271208 mod_privacy: Remove the giant if/end block that was really causing problems - skipping stanzas with no to/from (like presence broadcast). Also optimised the code a *lot*, these changes untested at the moment though.
Matthew Wild <mwild1@gmail.com>
parents: 2522
diff changeset
   336
				(evilJid.node and evilJid.host and evilJid.resource and item.value == evilJid.node.."@"..evilJid.host.."/"..evilJid.resource) or
0c816b271208 mod_privacy: Remove the giant if/end block that was really causing problems - skipping stanzas with no to/from (like presence broadcast). Also optimised the code a *lot*, these changes untested at the moment though.
Matthew Wild <mwild1@gmail.com>
parents: 2522
diff changeset
   337
				(evilJid.node and evilJid.host and item.value == evilJid.node.."@"..evilJid.host) or
0c816b271208 mod_privacy: Remove the giant if/end block that was really causing problems - skipping stanzas with no to/from (like presence broadcast). Also optimised the code a *lot*, these changes untested at the moment though.
Matthew Wild <mwild1@gmail.com>
parents: 2522
diff changeset
   338
				(evilJid.host and evilJid.resource and item.value == evilJid.host.."/"..evilJid.resource) or
0c816b271208 mod_privacy: Remove the giant if/end block that was really causing problems - skipping stanzas with no to/from (like presence broadcast). Also optimised the code a *lot*, these changes untested at the moment though.
Matthew Wild <mwild1@gmail.com>
parents: 2522
diff changeset
   339
				(evilJid.host and item.value == evilJid.host) then
0c816b271208 mod_privacy: Remove the giant if/end block that was really causing problems - skipping stanzas with no to/from (like presence broadcast). Also optimised the code a *lot*, these changes untested at the moment though.
Matthew Wild <mwild1@gmail.com>
parents: 2522
diff changeset
   340
				apply = true;
0c816b271208 mod_privacy: Remove the giant if/end block that was really causing problems - skipping stanzas with no to/from (like presence broadcast). Also optimised the code a *lot*, these changes untested at the moment though.
Matthew Wild <mwild1@gmail.com>
parents: 2522
diff changeset
   341
				block = (item.action == "deny");
0c816b271208 mod_privacy: Remove the giant if/end block that was really causing problems - skipping stanzas with no to/from (like presence broadcast). Also optimised the code a *lot*, these changes untested at the moment though.
Matthew Wild <mwild1@gmail.com>
parents: 2522
diff changeset
   342
			elseif item.type == "group" then
0c816b271208 mod_privacy: Remove the giant if/end block that was really causing problems - skipping stanzas with no to/from (like presence broadcast). Also optimised the code a *lot*, these changes untested at the moment though.
Matthew Wild <mwild1@gmail.com>
parents: 2522
diff changeset
   343
				local roster = load_roster(session.username, session.host);
3069
c3df9053e0a9 mod_privacy: Fix several possible tracebacks when either the contact is a host JID, or the contact isn't in your roster and you have roster group/subscription rules in place
Matthew Wild <mwild1@gmail.com>
parents: 3068
diff changeset
   344
				local roster_entry = roster[jid_join(evilJid.node, evilJid.host)];
c3df9053e0a9 mod_privacy: Fix several possible tracebacks when either the contact is a host JID, or the contact isn't in your roster and you have roster group/subscription rules in place
Matthew Wild <mwild1@gmail.com>
parents: 3068
diff changeset
   345
				if roster_entry then
c3df9053e0a9 mod_privacy: Fix several possible tracebacks when either the contact is a host JID, or the contact isn't in your roster and you have roster group/subscription rules in place
Matthew Wild <mwild1@gmail.com>
parents: 3068
diff changeset
   346
					local groups = roster_entry.groups;
c3df9053e0a9 mod_privacy: Fix several possible tracebacks when either the contact is a host JID, or the contact isn't in your roster and you have roster group/subscription rules in place
Matthew Wild <mwild1@gmail.com>
parents: 3068
diff changeset
   347
					for group in pairs(groups) do
c3df9053e0a9 mod_privacy: Fix several possible tracebacks when either the contact is a host JID, or the contact isn't in your roster and you have roster group/subscription rules in place
Matthew Wild <mwild1@gmail.com>
parents: 3068
diff changeset
   348
						if group == item.value then
c3df9053e0a9 mod_privacy: Fix several possible tracebacks when either the contact is a host JID, or the contact isn't in your roster and you have roster group/subscription rules in place
Matthew Wild <mwild1@gmail.com>
parents: 3068
diff changeset
   349
							apply = true;
c3df9053e0a9 mod_privacy: Fix several possible tracebacks when either the contact is a host JID, or the contact isn't in your roster and you have roster group/subscription rules in place
Matthew Wild <mwild1@gmail.com>
parents: 3068
diff changeset
   350
							block = (item.action == "deny");
c3df9053e0a9 mod_privacy: Fix several possible tracebacks when either the contact is a host JID, or the contact isn't in your roster and you have roster group/subscription rules in place
Matthew Wild <mwild1@gmail.com>
parents: 3068
diff changeset
   351
							break;
c3df9053e0a9 mod_privacy: Fix several possible tracebacks when either the contact is a host JID, or the contact isn't in your roster and you have roster group/subscription rules in place
Matthew Wild <mwild1@gmail.com>
parents: 3068
diff changeset
   352
						end
2496
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   353
					end
2523
0c816b271208 mod_privacy: Remove the giant if/end block that was really causing problems - skipping stanzas with no to/from (like presence broadcast). Also optimised the code a *lot*, these changes untested at the moment though.
Matthew Wild <mwild1@gmail.com>
parents: 2522
diff changeset
   354
				end
3069
c3df9053e0a9 mod_privacy: Fix several possible tracebacks when either the contact is a host JID, or the contact isn't in your roster and you have roster group/subscription rules in place
Matthew Wild <mwild1@gmail.com>
parents: 3068
diff changeset
   355
			elseif item.type == "subscription" then -- we need a valid bare evil jid
2523
0c816b271208 mod_privacy: Remove the giant if/end block that was really causing problems - skipping stanzas with no to/from (like presence broadcast). Also optimised the code a *lot*, these changes untested at the moment though.
Matthew Wild <mwild1@gmail.com>
parents: 2522
diff changeset
   356
				local roster = load_roster(session.username, session.host);
3069
c3df9053e0a9 mod_privacy: Fix several possible tracebacks when either the contact is a host JID, or the contact isn't in your roster and you have roster group/subscription rules in place
Matthew Wild <mwild1@gmail.com>
parents: 3068
diff changeset
   357
				local roster_entry = roster[jid_join(evilJid.node, evilJid.host)];
c3df9053e0a9 mod_privacy: Fix several possible tracebacks when either the contact is a host JID, or the contact isn't in your roster and you have roster group/subscription rules in place
Matthew Wild <mwild1@gmail.com>
parents: 3068
diff changeset
   358
				if (not(roster_entry) and item.value == "none")
c3df9053e0a9 mod_privacy: Fix several possible tracebacks when either the contact is a host JID, or the contact isn't in your roster and you have roster group/subscription rules in place
Matthew Wild <mwild1@gmail.com>
parents: 3068
diff changeset
   359
				   or (roster_entry and roster_entry.subscription == item.value) then
2496
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   360
					apply = true;
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   361
					block = (item.action == "deny");
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   362
				end
2523
0c816b271208 mod_privacy: Remove the giant if/end block that was really causing problems - skipping stanzas with no to/from (like presence broadcast). Also optimised the code a *lot*, these changes untested at the moment though.
Matthew Wild <mwild1@gmail.com>
parents: 2522
diff changeset
   363
			elseif item.type == nil then
0c816b271208 mod_privacy: Remove the giant if/end block that was really causing problems - skipping stanzas with no to/from (like presence broadcast). Also optimised the code a *lot*, these changes untested at the moment though.
Matthew Wild <mwild1@gmail.com>
parents: 2522
diff changeset
   364
				apply = true;
0c816b271208 mod_privacy: Remove the giant if/end block that was really causing problems - skipping stanzas with no to/from (like presence broadcast). Also optimised the code a *lot*, these changes untested at the moment though.
Matthew Wild <mwild1@gmail.com>
parents: 2522
diff changeset
   365
				block = (item.action == "deny");
2496
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   366
			end
2523
0c816b271208 mod_privacy: Remove the giant if/end block that was really causing problems - skipping stanzas with no to/from (like presence broadcast). Also optimised the code a *lot*, these changes untested at the moment though.
Matthew Wild <mwild1@gmail.com>
parents: 2522
diff changeset
   367
		end
0c816b271208 mod_privacy: Remove the giant if/end block that was really causing problems - skipping stanzas with no to/from (like presence broadcast). Also optimised the code a *lot*, these changes untested at the moment though.
Matthew Wild <mwild1@gmail.com>
parents: 2522
diff changeset
   368
		if apply then
0c816b271208 mod_privacy: Remove the giant if/end block that was really causing problems - skipping stanzas with no to/from (like presence broadcast). Also optimised the code a *lot*, these changes untested at the moment though.
Matthew Wild <mwild1@gmail.com>
parents: 2522
diff changeset
   369
			if block then
5393
57c4964eff0b mod_privacy: Drop stanzas of type groupchat, so users aren't kicked from their chatrooms when blocking specific MUC occupants.
Tobias Markmann <tm@ayena.de>
parents: 5370
diff changeset
   370
				-- drop and not bounce groupchat messages, otherwise users will get kicked
57c4964eff0b mod_privacy: Drop stanzas of type groupchat, so users aren't kicked from their chatrooms when blocking specific MUC occupants.
Tobias Markmann <tm@ayena.de>
parents: 5370
diff changeset
   371
				if stanza.attr.type == "groupchat" then
57c4964eff0b mod_privacy: Drop stanzas of type groupchat, so users aren't kicked from their chatrooms when blocking specific MUC occupants.
Tobias Markmann <tm@ayena.de>
parents: 5370
diff changeset
   372
					return true;
57c4964eff0b mod_privacy: Drop stanzas of type groupchat, so users aren't kicked from their chatrooms when blocking specific MUC occupants.
Tobias Markmann <tm@ayena.de>
parents: 5370
diff changeset
   373
				end
2524
b427f5401ce7 mod_privacy: A handful of logging fixes
Matthew Wild <mwild1@gmail.com>
parents: 2523
diff changeset
   374
				module:log("debug", "stanza blocked: %s, to: %s, from: %s", tostring(stanza.name), tostring(to), tostring(from));
2523
0c816b271208 mod_privacy: Remove the giant if/end block that was really causing problems - skipping stanzas with no to/from (like presence broadcast). Also optimised the code a *lot*, these changes untested at the moment though.
Matthew Wild <mwild1@gmail.com>
parents: 2522
diff changeset
   375
				if stanza.name == "message" then
0c816b271208 mod_privacy: Remove the giant if/end block that was really causing problems - skipping stanzas with no to/from (like presence broadcast). Also optimised the code a *lot*, these changes untested at the moment though.
Matthew Wild <mwild1@gmail.com>
parents: 2522
diff changeset
   376
					origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
0c816b271208 mod_privacy: Remove the giant if/end block that was really causing problems - skipping stanzas with no to/from (like presence broadcast). Also optimised the code a *lot*, these changes untested at the moment though.
Matthew Wild <mwild1@gmail.com>
parents: 2522
diff changeset
   377
				elseif stanza.name == "iq" and (stanza.attr.type == "get" or stanza.attr.type == "set") then
0c816b271208 mod_privacy: Remove the giant if/end block that was really causing problems - skipping stanzas with no to/from (like presence broadcast). Also optimised the code a *lot*, these changes untested at the moment though.
Matthew Wild <mwild1@gmail.com>
parents: 2522
diff changeset
   378
					origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
2496
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   379
				end
2523
0c816b271208 mod_privacy: Remove the giant if/end block that was really causing problems - skipping stanzas with no to/from (like presence broadcast). Also optimised the code a *lot*, these changes untested at the moment though.
Matthew Wild <mwild1@gmail.com>
parents: 2522
diff changeset
   380
				return true; -- stanza blocked !
0c816b271208 mod_privacy: Remove the giant if/end block that was really causing problems - skipping stanzas with no to/from (like presence broadcast). Also optimised the code a *lot*, these changes untested at the moment though.
Matthew Wild <mwild1@gmail.com>
parents: 2522
diff changeset
   381
			else
3085
cf01b0725272 mod_privacy: Improved logging.
Waqas Hussain <waqas20@gmail.com>
parents: 3069
diff changeset
   382
				--module:log("debug", "stanza explicitly allowed!")
2523
0c816b271208 mod_privacy: Remove the giant if/end block that was really causing problems - skipping stanzas with no to/from (like presence broadcast). Also optimised the code a *lot*, these changes untested at the moment though.
Matthew Wild <mwild1@gmail.com>
parents: 2522
diff changeset
   383
				return;
1293
13dde33d5b87 mod_privacy: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   384
			end
13dde33d5b87 mod_privacy: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   385
		end
13dde33d5b87 mod_privacy: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   386
	end
2496
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   387
end
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   388
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   389
function preCheckIncoming(e)
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   390
	local session;
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   391
	if e.stanza.attr.to ~= nil then
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   392
		local node, host, resource = jid_split(e.stanza.attr.to);
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   393
		if node == nil or host == nil then
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   394
			return;
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   395
		end
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   396
		if resource == nil then
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   397
			local prio = 0;
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   398
			if bare_sessions[node.."@"..host] ~= nil then
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   399
				for resource, session_ in pairs(bare_sessions[node.."@"..host].sessions) do
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   400
					if session_.priority ~= nil and session_.priority > prio then
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   401
						session = session_;
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   402
						prio = session_.priority;
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   403
					end
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   404
				end
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   405
			end
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   406
		else
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   407
			session = full_sessions[node.."@"..host.."/"..resource];
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   408
		end
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   409
		if session ~= nil then
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   410
			return checkIfNeedToBeBlocked(e, session);
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   411
		else
3085
cf01b0725272 mod_privacy: Improved logging.
Waqas Hussain <waqas20@gmail.com>
parents: 3069
diff changeset
   412
			--module:log("debug", "preCheckIncoming: Couldn't get session for jid: %s@%s/%s", tostring(node), tostring(host), tostring(resource));
2496
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   413
		end
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   414
	end
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   415
end
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   416
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   417
function preCheckOutgoing(e)
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   418
	local session = e.origin;
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   419
	if e.stanza.attr.from == nil then
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   420
		e.stanza.attr.from = session.username .. "@" .. session.host;
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   421
		if session.resource ~= nil then
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   422
		 	e.stanza.attr.from = e.stanza.attr.from .. "/" .. session.resource;
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   423
		end
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   424
	end
3221
01da55cffcfb mod_privacy: Workaround for a traceback.
Waqas Hussain <waqas20@gmail.com>
parents: 3085
diff changeset
   425
	if session.username then -- FIXME do properly
01da55cffcfb mod_privacy: Workaround for a traceback.
Waqas Hussain <waqas20@gmail.com>
parents: 3085
diff changeset
   426
		return checkIfNeedToBeBlocked(e, session);
01da55cffcfb mod_privacy: Workaround for a traceback.
Waqas Hussain <waqas20@gmail.com>
parents: 3085
diff changeset
   427
	end
2496
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   428
end
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   429
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   430
module:hook("pre-message/full", preCheckOutgoing, 500);
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   431
module:hook("pre-message/bare", preCheckOutgoing, 500);
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   432
module:hook("pre-message/host", preCheckOutgoing, 500);
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   433
module:hook("pre-iq/full", preCheckOutgoing, 500);
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   434
module:hook("pre-iq/bare", preCheckOutgoing, 500);
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   435
module:hook("pre-iq/host", preCheckOutgoing, 500);
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   436
module:hook("pre-presence/full", preCheckOutgoing, 500);
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   437
module:hook("pre-presence/bare", preCheckOutgoing, 500);
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   438
module:hook("pre-presence/host", preCheckOutgoing, 500);
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   439
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   440
module:hook("message/full", preCheckIncoming, 500);
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   441
module:hook("message/bare", preCheckIncoming, 500);
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   442
module:hook("message/host", preCheckIncoming, 500);
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   443
module:hook("iq/full", preCheckIncoming, 500);
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   444
module:hook("iq/bare", preCheckIncoming, 500);
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   445
module:hook("iq/host", preCheckIncoming, 500);
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   446
module:hook("presence/full", preCheckIncoming, 500);
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   447
module:hook("presence/bare", preCheckIncoming, 500);
f18b882af1d1 mod_privacy: Imported from prosody-modules.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
   448
module:hook("presence/host", preCheckIncoming, 500);