plugins/muc/muc.lib.lua
author Kim Alvefur <zash@zash.se>
Thu, 25 Mar 2021 13:18:23 +0100
changeset 11502 d61ec5e6ee16
parent 11250 ab189e707705
child 11717 7623767df468
permissions -rw-r--r--
MUC: Allow overriding occupant object from groupchat message event Use case: Enable module that provides a virtual occupant object for bots Before, if there is no occupant then either some other part of MUC would reject the message or `occupant.nick` would have caused an error.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
     1
-- Prosody IM
2923
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2864
diff changeset
     2
-- Copyright (C) 2008-2010 Matthew Wild
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2864
diff changeset
     3
-- Copyright (C) 2008-2010 Waqas Hussain
6102
385772166289 plugins/muc/muc: Add copyright for daurnimator
daurnimator <quae@daurnimator.com>
parents: 6101
diff changeset
     4
-- Copyright (C) 2014 Daurnimator
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5681
diff changeset
     5
--
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
     6
-- This project is MIT/X11 licensed. Please see the
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
     7
-- COPYING file in the source package for more information.
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
     8
--
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
     9
3281
fd6ab269ecc2 MUC: A little modification to improve code analysis.
Waqas Hussain <waqas20@gmail.com>
parents: 3280
diff changeset
    10
local select = select;
7092
890f4b2cc444 MUC: Add luacheck annotations, remove unused vars, rename conflicting vars etc
Kim Alvefur <zash@zash.se>
parents: 7016
diff changeset
    11
local pairs = pairs;
6180
35388114439f plugins/muc/muc.lib: non-function changes (reordering, semicolons and comments)
daurnimator <quae@daurnimator.com>
parents: 6179
diff changeset
    12
local next = next;
35388114439f plugins/muc/muc.lib: non-function changes (reordering, semicolons and comments)
daurnimator <quae@daurnimator.com>
parents: 6179
diff changeset
    13
local setmetatable = setmetatable;
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    14
3517
530f7de1d265 MUC: Use util.dataforms to generate forms
Kim Alvefur <zash@zash.se>
parents: 3516
diff changeset
    15
local dataform = require "util.dataforms";
6217
657c707d9229 plugins/muc/muc.lib: Use util.iterators to count occupants
daurnimator <quae@daurnimator.com>
parents: 6216
diff changeset
    16
local iterators = require "util.iterators";
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    17
local jid_split = require "util.jid".split;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    18
local jid_bare = require "util.jid".bare;
1862
115f274dd17f MUC: Prep given JID when changing affiliation.
Waqas Hussain <waqas20@gmail.com>
parents: 1826
diff changeset
    19
local jid_prep = require "util.jid".prep;
6479
fb8a9873728b plugins/muc/muc.lib: Kick users from outcast hosts
daurnimator <quae@daurnimator.com>
parents: 6478
diff changeset
    20
local jid_join = require "util.jid".join;
8859
538bb632a2b6 MUC: Use util.jid.resource
Kim Alvefur <zash@zash.se>
parents: 8856
diff changeset
    21
local jid_resource = require "util.jid".resource;
9266
4285d1efaf03 MUC: Allow admins to include a registered nick when setting affiliation
Matthew Wild <mwild1@gmail.com>
parents: 9242
diff changeset
    22
local resourceprep = require "util.encodings".stringprep.resourceprep;
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    23
local st = require "util.stanza";
1778
f4213d84ba8a MUC: Correct routing of vCard requests to bare JID.
Waqas Hussain <waqas20@gmail.com>
parents: 1769
diff changeset
    24
local base64 = require "util.encodings".base64;
f4213d84ba8a MUC: Correct routing of vCard requests to bare JID.
Waqas Hussain <waqas20@gmail.com>
parents: 1769
diff changeset
    25
local md5 = require "util.hashes".md5;
9770
f40b9649e929 MUC: Rename import to avoid name clash [luacheck]
Kim Alvefur <zash@zash.se>
parents: 9765
diff changeset
    26
local new_id = require "util.id".medium;
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    27
7273
df22e314946c MUC: Use already initialized logger
Kim Alvefur <zash@zash.se>
parents: 7123
diff changeset
    28
local log = module._log;
5612
5404832d6f7a mod_muc: Use stanza:maptags() instead of custom filtering functions, 7 insertions, 19 deletions!
Matthew Wild <mwild1@gmail.com>
parents: 5611
diff changeset
    29
6179
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
    30
local occupant_lib = module:require "muc/occupant"
6218
bf11910bad5a plugins/muc: Move valid_roles, valid_affiliations and is_kickable_error to new muc/util module
daurnimator <quae@daurnimator.com>
parents: 6217
diff changeset
    31
local muc_util = module:require "muc/util";
bf11910bad5a plugins/muc: Move valid_roles, valid_affiliations and is_kickable_error to new muc/util module
daurnimator <quae@daurnimator.com>
parents: 6217
diff changeset
    32
local is_kickable_error = muc_util.is_kickable_error;
bf11910bad5a plugins/muc: Move valid_roles, valid_affiliations and is_kickable_error to new muc/util module
daurnimator <quae@daurnimator.com>
parents: 6217
diff changeset
    33
local valid_roles, valid_affiliations = muc_util.valid_roles, muc_util.valid_affiliations;
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    34
1735
81406277279e MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 1734
diff changeset
    35
local room_mt = {};
1737
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
    36
room_mt.__index = room_mt;
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
    37
5519
06e188268df1 MUC: add __tostring on room metatable
Matthew Wild <mwild1@gmail.com>
parents: 5397
diff changeset
    38
function room_mt:__tostring()
06e188268df1 MUC: add __tostring on room metatable
Matthew Wild <mwild1@gmail.com>
parents: 5397
diff changeset
    39
	return "MUC room ("..self.jid..")";
06e188268df1 MUC: add __tostring on room metatable
Matthew Wild <mwild1@gmail.com>
parents: 5397
diff changeset
    40
end
06e188268df1 MUC: add __tostring on room metatable
Matthew Wild <mwild1@gmail.com>
parents: 5397
diff changeset
    41
7355
50b24b3476e6 MUC: Provide a noop stub room:save() method
Kim Alvefur <zash@zash.se>
parents: 7350
diff changeset
    42
function room_mt.save()
9619
66c6edf56397 MUC: Fix spelling in comments
Kim Alvefur <zash@zash.se>
parents: 9617
diff changeset
    43
	-- overridden by mod_muc.lua
7355
50b24b3476e6 MUC: Provide a noop stub room:save() method
Kim Alvefur <zash@zash.se>
parents: 7350
diff changeset
    44
end
50b24b3476e6 MUC: Provide a noop stub room:save() method
Kim Alvefur <zash@zash.se>
parents: 7350
diff changeset
    45
6119
c13f5d6b9b16 plugins/muc/muc.lib: Use `get_occupant_jid` method instead of indexing _jid_nick
daurnimator <quae@daurnimator.com>
parents: 6118
diff changeset
    46
function room_mt:get_occupant_jid(real_jid)
c13f5d6b9b16 plugins/muc/muc.lib: Use `get_occupant_jid` method instead of indexing _jid_nick
daurnimator <quae@daurnimator.com>
parents: 6118
diff changeset
    47
	return self._jid_nick[real_jid]
c13f5d6b9b16 plugins/muc/muc.lib: Use `get_occupant_jid` method instead of indexing _jid_nick
daurnimator <quae@daurnimator.com>
parents: 6118
diff changeset
    48
end
c13f5d6b9b16 plugins/muc/muc.lib: Use `get_occupant_jid` method instead of indexing _jid_nick
daurnimator <quae@daurnimator.com>
parents: 6118
diff changeset
    49
1737
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
    50
function room_mt:get_default_role(affiliation)
6220
4e7d205f49f7 plugins/muc/muc.lib: Turn get_default_role into an event
daurnimator <quae@daurnimator.com>
parents: 6219
diff changeset
    51
	local role = module:fire_event("muc-get-default-role", {
4e7d205f49f7 plugins/muc/muc.lib: Turn get_default_role into an event
daurnimator <quae@daurnimator.com>
parents: 6219
diff changeset
    52
		room = self;
4e7d205f49f7 plugins/muc/muc.lib: Turn get_default_role into an event
daurnimator <quae@daurnimator.com>
parents: 6219
diff changeset
    53
		affiliation = affiliation;
4e7d205f49f7 plugins/muc/muc.lib: Turn get_default_role into an event
daurnimator <quae@daurnimator.com>
parents: 6219
diff changeset
    54
		affiliation_rank = valid_affiliations[affiliation or "none"];
4e7d205f49f7 plugins/muc/muc.lib: Turn get_default_role into an event
daurnimator <quae@daurnimator.com>
parents: 6219
diff changeset
    55
	});
8918
2502be210a85 MUC: Normalize role value, fixes removal on loss of membership (thanks mimi89999)
Kim Alvefur <zash@zash.se>
parents: 8917
diff changeset
    56
	role = role ~= "none" and role or nil; -- coerces `role == false` to `nil`
6220
4e7d205f49f7 plugins/muc/muc.lib: Turn get_default_role into an event
daurnimator <quae@daurnimator.com>
parents: 6219
diff changeset
    57
	return role, valid_roles[role or "none"];
4e7d205f49f7 plugins/muc/muc.lib: Turn get_default_role into an event
daurnimator <quae@daurnimator.com>
parents: 6219
diff changeset
    58
end
4e7d205f49f7 plugins/muc/muc.lib: Turn get_default_role into an event
daurnimator <quae@daurnimator.com>
parents: 6219
diff changeset
    59
module:hook("muc-get-default-role", function(event)
4e7d205f49f7 plugins/muc/muc.lib: Turn get_default_role into an event
daurnimator <quae@daurnimator.com>
parents: 6219
diff changeset
    60
	if event.affiliation_rank >= valid_affiliations.admin then
1737
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
    61
		return "moderator";
6226
7582deb85812 plugins/muc: Move 'moderated' code to seperate file; changes default "muc-get-default-role" behaviour
daurnimator <quae@daurnimator.com>
parents: 6225
diff changeset
    62
	elseif event.affiliation_rank >= valid_affiliations.none then
1737
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
    63
		return "participant";
6220
4e7d205f49f7 plugins/muc/muc.lib: Turn get_default_role into an event
daurnimator <quae@daurnimator.com>
parents: 6219
diff changeset
    64
	end
9720
5281a795d6df MUC: Adjust priorities of muc-get-default-role handlers (fixes #1272)
Matthew Wild <mwild1@gmail.com>
parents: 9644
diff changeset
    65
end, -1);
1735
81406277279e MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 1734
diff changeset
    66
6179
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
    67
--- Occupant functions
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
    68
function room_mt:new_occupant(bare_real_jid, nick)
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
    69
	local occupant = occupant_lib.new(bare_real_jid, nick);
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
    70
	local affiliation = self:get_affiliation(bare_real_jid);
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
    71
	occupant.role = self:get_default_role(affiliation);
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
    72
	return occupant;
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
    73
end
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
    74
9242
03e37f7d6c97 MUC: Add some comments for clarity
Matthew Wild <mwild1@gmail.com>
parents: 9241
diff changeset
    75
-- nick is in the form of an in-room JID
6179
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
    76
function room_mt:get_occupant_by_nick(nick)
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
    77
	local occupant = self._occupants[nick];
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
    78
	if occupant == nil then return nil end
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
    79
	return occupant_lib.copy(occupant);
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
    80
end
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
    81
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
    82
do
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
    83
	local function next_copied_occupant(occupants, occupant_jid)
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
    84
		local next_occupant_jid, raw_occupant = next(occupants, occupant_jid);
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
    85
		if next_occupant_jid == nil then return nil end
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
    86
		return next_occupant_jid, occupant_lib.copy(raw_occupant);
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
    87
	end
7092
890f4b2cc444 MUC: Add luacheck annotations, remove unused vars, rename conflicting vars etc
Kim Alvefur <zash@zash.se>
parents: 7016
diff changeset
    88
	-- FIXME Explain what 'read_only' is supposed to be
890f4b2cc444 MUC: Add luacheck annotations, remove unused vars, rename conflicting vars etc
Kim Alvefur <zash@zash.se>
parents: 7016
diff changeset
    89
	function room_mt:each_occupant(read_only) -- luacheck: ignore 212
6179
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
    90
		return next_copied_occupant, self._occupants, nil;
1737
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
    91
	end
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
    92
end
1735
81406277279e MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 1734
diff changeset
    93
6237
a58685df9d16 plugins/muc: Add room:has_occupant() method
daurnimator <quae@daurnimator.com>
parents: 6231
diff changeset
    94
function room_mt:has_occupant()
a58685df9d16 plugins/muc: Add room:has_occupant() method
daurnimator <quae@daurnimator.com>
parents: 6231
diff changeset
    95
	return next(self._occupants, nil) ~= nil
a58685df9d16 plugins/muc: Add room:has_occupant() method
daurnimator <quae@daurnimator.com>
parents: 6231
diff changeset
    96
end
a58685df9d16 plugins/muc: Add room:has_occupant() method
daurnimator <quae@daurnimator.com>
parents: 6231
diff changeset
    97
6179
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
    98
function room_mt:get_occupant_by_real_jid(real_jid)
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
    99
	local occupant_jid = self:get_occupant_jid(real_jid);
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   100
	if occupant_jid == nil then return nil end
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   101
	return self:get_occupant_by_nick(occupant_jid);
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   102
end
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   103
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   104
function room_mt:save_occupant(occupant)
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   105
	occupant = occupant_lib.copy(occupant); -- So that occupant can be modified more
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   106
	local id = occupant.nick
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   107
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   108
	-- Need to maintain _jid_nick secondary index
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   109
	local old_occupant = self._occupants[id];
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   110
	if old_occupant then
6454
385932a83013 plugins/muc/muc.lib: Use occupants iterator instead of assuming pairs
daurnimator <quae@daurnimator.com>
parents: 6434
diff changeset
   111
		for real_jid in old_occupant:each_session() do
6179
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   112
			self._jid_nick[real_jid] = nil;
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   113
		end
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   114
	end
6250
454ef19e7925 plugins/muc/muc: Better check for live sessions
daurnimator <quae@daurnimator.com>
parents: 6249
diff changeset
   115
454ef19e7925 plugins/muc/muc: Better check for live sessions
daurnimator <quae@daurnimator.com>
parents: 6249
diff changeset
   116
	local has_live_session = false
454ef19e7925 plugins/muc/muc: Better check for live sessions
daurnimator <quae@daurnimator.com>
parents: 6249
diff changeset
   117
	if occupant.role ~= nil then
6179
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   118
		for real_jid, presence in occupant:each_session() do
6250
454ef19e7925 plugins/muc/muc: Better check for live sessions
daurnimator <quae@daurnimator.com>
parents: 6249
diff changeset
   119
			if presence.attr.type == nil then
454ef19e7925 plugins/muc/muc: Better check for live sessions
daurnimator <quae@daurnimator.com>
parents: 6249
diff changeset
   120
				has_live_session = true
454ef19e7925 plugins/muc/muc: Better check for live sessions
daurnimator <quae@daurnimator.com>
parents: 6249
diff changeset
   121
				self._jid_nick[real_jid] = occupant.nick;
454ef19e7925 plugins/muc/muc: Better check for live sessions
daurnimator <quae@daurnimator.com>
parents: 6249
diff changeset
   122
			end
6179
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   123
		end
6250
454ef19e7925 plugins/muc/muc: Better check for live sessions
daurnimator <quae@daurnimator.com>
parents: 6249
diff changeset
   124
		if not has_live_session then
454ef19e7925 plugins/muc/muc: Better check for live sessions
daurnimator <quae@daurnimator.com>
parents: 6249
diff changeset
   125
			-- Has no live sessions left; they have left the room.
454ef19e7925 plugins/muc/muc: Better check for live sessions
daurnimator <quae@daurnimator.com>
parents: 6249
diff changeset
   126
			occupant.role = nil
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   127
		end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   128
	end
6250
454ef19e7925 plugins/muc/muc: Better check for live sessions
daurnimator <quae@daurnimator.com>
parents: 6249
diff changeset
   129
	if not has_live_session then
6179
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   130
		occupant = nil
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   131
	end
6179
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   132
	self._occupants[id] = occupant
9005
1fcddb4a4d16 MUC: Return new occupant object from :save_occupant(), which can include useful info
Matthew Wild <mwild1@gmail.com>
parents: 8934
diff changeset
   133
	return occupant
6179
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   134
end
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   135
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   136
function room_mt:route_to_occupant(occupant, stanza)
6130
c95d9132592a plugins/muc/muc.lib: Add route_to_occupant function to send a stanza to all occupant sessions
daurnimator <quae@daurnimator.com>
parents: 6129
diff changeset
   137
	local to = stanza.attr.to;
7092
890f4b2cc444 MUC: Add luacheck annotations, remove unused vars, rename conflicting vars etc
Kim Alvefur <zash@zash.se>
parents: 7016
diff changeset
   138
	for jid in occupant:each_session() do
6253
2df9e7a67e56 plugins/muc/muc.lib: Even unavailable session need to be routed to sometimes (e.g. their own leave)
daurnimator <quae@daurnimator.com>
parents: 6252
diff changeset
   139
		stanza.attr.to = jid;
2df9e7a67e56 plugins/muc/muc.lib: Even unavailable session need to be routed to sometimes (e.g. their own leave)
daurnimator <quae@daurnimator.com>
parents: 6252
diff changeset
   140
		self:route_stanza(stanza);
6130
c95d9132592a plugins/muc/muc.lib: Add route_to_occupant function to send a stanza to all occupant sessions
daurnimator <quae@daurnimator.com>
parents: 6129
diff changeset
   141
	end
2172
84dd0fada45b MUC: Improved handling of incoming groupchat messages (state preserved for possible later use).
Waqas Hussain <waqas20@gmail.com>
parents: 2064
diff changeset
   142
	stanza.attr.to = to;
6130
c95d9132592a plugins/muc/muc.lib: Add route_to_occupant function to send a stanza to all occupant sessions
daurnimator <quae@daurnimator.com>
parents: 6129
diff changeset
   143
end
c95d9132592a plugins/muc/muc.lib: Add route_to_occupant function to send a stanza to all occupant sessions
daurnimator <quae@daurnimator.com>
parents: 6129
diff changeset
   144
6179
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   145
-- actor is the attribute table
6515
ad159be9e9d7 plugins/muc/muc.lib: Don't expose actor jids to users when anonymous
daurnimator <quae@daurnimator.com>
parents: 6481
diff changeset
   146
local function add_item(x, affiliation, role, jid, nick, actor_nick, actor_jid, reason)
10757
925081396c59 MUC: Always include 'affiliation'/'role' attributes, defaulting to 'none' if nil
Matthew Wild <mwild1@gmail.com>
parents: 10694
diff changeset
   147
	x:tag("item", {affiliation = affiliation or "none"; role = role; jid = jid; nick = nick;})
6515
ad159be9e9d7 plugins/muc/muc.lib: Don't expose actor jids to users when anonymous
daurnimator <quae@daurnimator.com>
parents: 6481
diff changeset
   148
	if actor_nick or actor_jid then
ad159be9e9d7 plugins/muc/muc.lib: Don't expose actor jids to users when anonymous
daurnimator <quae@daurnimator.com>
parents: 6481
diff changeset
   149
		x:tag("actor", {nick = actor_nick; jid = actor_jid;}):up()
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   150
	end
6179
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   151
	if reason then
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   152
		x:tag("reason"):text(reason):up()
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   153
	end
6179
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   154
	x:up();
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   155
	return x
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   156
end
6213
95bddf0142f4 plugins/muc/muc.lib: Remember to coerce nil role to "none"
daurnimator <quae@daurnimator.com>
parents: 6212
diff changeset
   157
6179
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   158
-- actor is (real) jid
6515
ad159be9e9d7 plugins/muc/muc.lib: Don't expose actor jids to users when anonymous
daurnimator <quae@daurnimator.com>
parents: 6481
diff changeset
   159
function room_mt:build_item_list(occupant, x, is_anonymous, nick, actor_nick, actor_jid, reason)
6213
95bddf0142f4 plugins/muc/muc.lib: Remember to coerce nil role to "none"
daurnimator <quae@daurnimator.com>
parents: 6212
diff changeset
   160
	local affiliation = self:get_affiliation(occupant.bare_jid) or "none";
95bddf0142f4 plugins/muc/muc.lib: Remember to coerce nil role to "none"
daurnimator <quae@daurnimator.com>
parents: 6212
diff changeset
   161
	local role = occupant.role or "none";
6179
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   162
	if is_anonymous then
6515
ad159be9e9d7 plugins/muc/muc.lib: Don't expose actor jids to users when anonymous
daurnimator <quae@daurnimator.com>
parents: 6481
diff changeset
   163
		add_item(x, affiliation, role, nil, nick, actor_nick, actor_jid, reason);
6179
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   164
	else
7092
890f4b2cc444 MUC: Add luacheck annotations, remove unused vars, rename conflicting vars etc
Kim Alvefur <zash@zash.se>
parents: 7016
diff changeset
   165
		for real_jid in occupant:each_session() do
6515
ad159be9e9d7 plugins/muc/muc.lib: Don't expose actor jids to users when anonymous
daurnimator <quae@daurnimator.com>
parents: 6481
diff changeset
   166
			add_item(x, affiliation, role, real_jid, nick, actor_nick, actor_jid, reason);
6179
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   167
		end
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   168
	end
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   169
	return x
5982
2d5685c6262f MUC: Split saving to history into a separate method
Kim Alvefur <zash@zash.se>
parents: 5854
diff changeset
   170
end
6179
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   171
6215
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents: 6214
diff changeset
   172
function room_mt:broadcast_message(stanza)
8484
d546815e555e MUC: Move delayed delivery check into an event handler
Kim Alvefur <zash@zash.se>
parents: 8483
diff changeset
   173
	if module:fire_event("muc-broadcast-message", {room = self, stanza = stanza}) then
d546815e555e MUC: Move delayed delivery check into an event handler
Kim Alvefur <zash@zash.se>
parents: 8483
diff changeset
   174
		return true;
d546815e555e MUC: Move delayed delivery check into an event handler
Kim Alvefur <zash@zash.se>
parents: 8483
diff changeset
   175
	end
d546815e555e MUC: Move delayed delivery check into an event handler
Kim Alvefur <zash@zash.se>
parents: 8483
diff changeset
   176
	self:broadcast(stanza);
d546815e555e MUC: Move delayed delivery check into an event handler
Kim Alvefur <zash@zash.se>
parents: 8483
diff changeset
   177
	return true;
d546815e555e MUC: Move delayed delivery check into an event handler
Kim Alvefur <zash@zash.se>
parents: 8483
diff changeset
   178
end
d546815e555e MUC: Move delayed delivery check into an event handler
Kim Alvefur <zash@zash.se>
parents: 8483
diff changeset
   179
d546815e555e MUC: Move delayed delivery check into an event handler
Kim Alvefur <zash@zash.se>
parents: 8483
diff changeset
   180
-- Strip delay tags claiming to be from us
8489
c50101b527bf MUC: Hook the correct event
Kim Alvefur <zash@zash.se>
parents: 8485
diff changeset
   181
module:hook("muc-occupant-groupchat", function (event)
8484
d546815e555e MUC: Move delayed delivery check into an event handler
Kim Alvefur <zash@zash.se>
parents: 8483
diff changeset
   182
	local stanza = event.stanza;
8489
c50101b527bf MUC: Hook the correct event
Kim Alvefur <zash@zash.se>
parents: 8485
diff changeset
   183
	local room = event.room;
c50101b527bf MUC: Hook the correct event
Kim Alvefur <zash@zash.se>
parents: 8485
diff changeset
   184
	local room_jid = room.jid;
8478
eb85b10e1fea MUC: Ensure that <delay/> elements which match our from are stripped (fixes #1055)
Jonas Wielicki <jonas@wielicki.name>
parents: 8477
diff changeset
   185
eb85b10e1fea MUC: Ensure that <delay/> elements which match our from are stripped (fixes #1055)
Jonas Wielicki <jonas@wielicki.name>
parents: 8477
diff changeset
   186
	stanza:maptags(function (child)
eb85b10e1fea MUC: Ensure that <delay/> elements which match our from are stripped (fixes #1055)
Jonas Wielicki <jonas@wielicki.name>
parents: 8477
diff changeset
   187
		if child.name == "delay" and child.attr["xmlns"] == "urn:xmpp:delay" then
8479
082d12728645 MUC: Rename variable to make it clearer that it is the room JID and not the MUC host
Kim Alvefur <zash@zash.se>
parents: 8478
diff changeset
   188
			if child.attr["from"] == room_jid then
8478
eb85b10e1fea MUC: Ensure that <delay/> elements which match our from are stripped (fixes #1055)
Jonas Wielicki <jonas@wielicki.name>
parents: 8477
diff changeset
   189
				return nil;
eb85b10e1fea MUC: Ensure that <delay/> elements which match our from are stripped (fixes #1055)
Jonas Wielicki <jonas@wielicki.name>
parents: 8477
diff changeset
   190
			end
eb85b10e1fea MUC: Ensure that <delay/> elements which match our from are stripped (fixes #1055)
Jonas Wielicki <jonas@wielicki.name>
parents: 8477
diff changeset
   191
		end
eb85b10e1fea MUC: Ensure that <delay/> elements which match our from are stripped (fixes #1055)
Jonas Wielicki <jonas@wielicki.name>
parents: 8477
diff changeset
   192
		if child.name == "x" and child.attr["xmlns"] == "jabber:x:delay" then
8479
082d12728645 MUC: Rename variable to make it clearer that it is the room JID and not the MUC host
Kim Alvefur <zash@zash.se>
parents: 8478
diff changeset
   193
			if child.attr["from"] == room_jid then
8478
eb85b10e1fea MUC: Ensure that <delay/> elements which match our from are stripped (fixes #1055)
Jonas Wielicki <jonas@wielicki.name>
parents: 8477
diff changeset
   194
				return nil;
eb85b10e1fea MUC: Ensure that <delay/> elements which match our from are stripped (fixes #1055)
Jonas Wielicki <jonas@wielicki.name>
parents: 8477
diff changeset
   195
			end
eb85b10e1fea MUC: Ensure that <delay/> elements which match our from are stripped (fixes #1055)
Jonas Wielicki <jonas@wielicki.name>
parents: 8477
diff changeset
   196
		end
eb85b10e1fea MUC: Ensure that <delay/> elements which match our from are stripped (fixes #1055)
Jonas Wielicki <jonas@wielicki.name>
parents: 8477
diff changeset
   197
		return child;
eb85b10e1fea MUC: Ensure that <delay/> elements which match our from are stripped (fixes #1055)
Jonas Wielicki <jonas@wielicki.name>
parents: 8477
diff changeset
   198
	end)
8484
d546815e555e MUC: Move delayed delivery check into an event handler
Kim Alvefur <zash@zash.se>
parents: 8483
diff changeset
   199
end);
8478
eb85b10e1fea MUC: Ensure that <delay/> elements which match our from are stripped (fixes #1055)
Jonas Wielicki <jonas@wielicki.name>
parents: 8477
diff changeset
   200
6140
e4cdb3e5d7d0 plugins/muc/muc.lib: Add :broadcast method; use it from :broadcast_except_nick and :broadcast_message
daurnimator <quae@daurnimator.com>
parents: 6139
diff changeset
   201
-- Broadcast a stanza to all occupants in the room.
6179
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   202
-- optionally checks conditional called with (nick, occupant)
6140
e4cdb3e5d7d0 plugins/muc/muc.lib: Add :broadcast method; use it from :broadcast_except_nick and :broadcast_message
daurnimator <quae@daurnimator.com>
parents: 6139
diff changeset
   203
function room_mt:broadcast(stanza, cond_func)
6179
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   204
	for nick, occupant in self:each_occupant() do
6140
e4cdb3e5d7d0 plugins/muc/muc.lib: Add :broadcast method; use it from :broadcast_except_nick and :broadcast_message
daurnimator <quae@daurnimator.com>
parents: 6139
diff changeset
   205
		if cond_func == nil or cond_func(nick, occupant) then
6133
5d8949bb15b0 plugins/muc/muc.lib: Additional `route_to_occupant` usage
daurnimator <quae@daurnimator.com>
parents: 6132
diff changeset
   206
			self:route_to_occupant(occupant, stanza)
1742
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
   207
		end
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
   208
	end
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
   209
end
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   210
6211
a4d26656df04 plugins/muc/muc.lib: Clean up whois handling
daurnimator <quae@daurnimator.com>
parents: 6209
diff changeset
   211
local function can_see_real_jids(whois, occupant)
a4d26656df04 plugins/muc/muc.lib: Clean up whois handling
daurnimator <quae@daurnimator.com>
parents: 6209
diff changeset
   212
	if whois == "anyone" then
a4d26656df04 plugins/muc/muc.lib: Clean up whois handling
daurnimator <quae@daurnimator.com>
parents: 6209
diff changeset
   213
		return true;
a4d26656df04 plugins/muc/muc.lib: Clean up whois handling
daurnimator <quae@daurnimator.com>
parents: 6209
diff changeset
   214
	elseif whois == "moderators" then
a4d26656df04 plugins/muc/muc.lib: Clean up whois handling
daurnimator <quae@daurnimator.com>
parents: 6209
diff changeset
   215
		return valid_roles[occupant.role or "none"] >= valid_roles.moderator;
6179
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   216
	end
6211
a4d26656df04 plugins/muc/muc.lib: Clean up whois handling
daurnimator <quae@daurnimator.com>
parents: 6209
diff changeset
   217
end
6179
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   218
11249
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   219
6278
fcc3ef191293 plugins/muc/muc: Fire broadcast presences event before creating full/anon presences
daurnimator <quae@daurnimator.com>
parents: 6277
diff changeset
   220
-- Broadcasts an occupant's presence to the whole room
fcc3ef191293 plugins/muc/muc: Fire broadcast presences event before creating full/anon presences
daurnimator <quae@daurnimator.com>
parents: 6277
diff changeset
   221
-- Takes the x element that goes into the stanzas
11249
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   222
function room_mt:publicise_occupant_status(occupant, x, nick, actor, reason, prev_role, force_unavailable, recipient)
7708
0a29a5e64f1d mod_muc/muc.lib: Allow passing different <x> elements to be passed to :publicise_occupant_status()
Kim Alvefur <zash@zash.se>
parents: 7689
diff changeset
   223
	local base_x = x.base or x;
6278
fcc3ef191293 plugins/muc/muc: Fire broadcast presences event before creating full/anon presences
daurnimator <quae@daurnimator.com>
parents: 6277
diff changeset
   224
	-- Build real jid and (optionally) occupant jid template presences
6804
5032d4817a30 plugins/muc/muc.lib: Fix issue #505 where unavailable presences were never used
daurnimator <quae@daurnimator.com>
parents: 6779
diff changeset
   225
	local base_presence do
6179
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   226
		-- Try to use main jid's presence
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   227
		local pr = occupant:get_presence();
10357
7b602e13c3b6 MUC: Add controls for whose presence is broadcast (closes #1335)
Lance Stout <lancestout@gmail.com>
parents: 10298
diff changeset
   228
		if pr and (occupant.role ~= nil or pr.attr.type == "unavailable") and not force_unavailable then
6278
fcc3ef191293 plugins/muc/muc: Fire broadcast presences event before creating full/anon presences
daurnimator <quae@daurnimator.com>
parents: 6277
diff changeset
   229
			base_presence = st.clone(pr);
6804
5032d4817a30 plugins/muc/muc.lib: Fix issue #505 where unavailable presences were never used
daurnimator <quae@daurnimator.com>
parents: 6779
diff changeset
   230
		else -- user is leaving but didn't send a leave presence. make one for them
5032d4817a30 plugins/muc/muc.lib: Fix issue #505 where unavailable presences were never used
daurnimator <quae@daurnimator.com>
parents: 6779
diff changeset
   231
			base_presence = st.presence {from = occupant.nick; type = "unavailable";};
6179
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   232
		end
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   233
	end
6211
a4d26656df04 plugins/muc/muc.lib: Clean up whois handling
daurnimator <quae@daurnimator.com>
parents: 6209
diff changeset
   234
6278
fcc3ef191293 plugins/muc/muc: Fire broadcast presences event before creating full/anon presences
daurnimator <quae@daurnimator.com>
parents: 6277
diff changeset
   235
	-- Fire event (before full_p and anon_p are created)
6516
3bbd59c03aeb plugins/muc/muc.lib: Allow muc-broadcast-presence event listeners to modify nick, actor, reason
daurnimator <quae@daurnimator.com>
parents: 6515
diff changeset
   236
	local event = {
6317
ec57067c1e0d mod_muc/muc.lib: Pass all info to muc-broadcast-presence handlers that would be required to use room:build_item_list() - useful for plugins
Matthew Wild <mwild1@gmail.com>
parents: 6278
diff changeset
   237
		room = self; stanza = base_presence; x = base_x;
6318
e5e7a789ef55 mod_muc/muc.lib: Remove is_anonymous from event (fix for ec57067c1e0d)
Matthew Wild <mwild1@gmail.com>
parents: 6317
diff changeset
   238
		occupant = occupant; nick = nick; actor = actor;
e5e7a789ef55 mod_muc/muc.lib: Remove is_anonymous from event (fix for ec57067c1e0d)
Matthew Wild <mwild1@gmail.com>
parents: 6317
diff changeset
   239
		reason = reason;
6516
3bbd59c03aeb plugins/muc/muc.lib: Allow muc-broadcast-presence event listeners to modify nick, actor, reason
daurnimator <quae@daurnimator.com>
parents: 6515
diff changeset
   240
	}
10716
dc24a8783c5d MUC: Add new event 'muc-build-occupant-presence' for plugins to extend occupant presence
Matthew Wild <mwild1@gmail.com>
parents: 10695
diff changeset
   241
	module:fire_event("muc-build-occupant-presence", event);
11249
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   242
	if not recipient then
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   243
		module:fire_event("muc-broadcast-presence", event);
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   244
	end
6516
3bbd59c03aeb plugins/muc/muc.lib: Allow muc-broadcast-presence event listeners to modify nick, actor, reason
daurnimator <quae@daurnimator.com>
parents: 6515
diff changeset
   245
3bbd59c03aeb plugins/muc/muc.lib: Allow muc-broadcast-presence event listeners to modify nick, actor, reason
daurnimator <quae@daurnimator.com>
parents: 6515
diff changeset
   246
	-- Allow muc-broadcast-presence listeners to change things
3bbd59c03aeb plugins/muc/muc.lib: Allow muc-broadcast-presence event listeners to modify nick, actor, reason
daurnimator <quae@daurnimator.com>
parents: 6515
diff changeset
   247
	nick = event.nick;
3bbd59c03aeb plugins/muc/muc.lib: Allow muc-broadcast-presence event listeners to modify nick, actor, reason
daurnimator <quae@daurnimator.com>
parents: 6515
diff changeset
   248
	actor = event.actor;
3bbd59c03aeb plugins/muc/muc.lib: Allow muc-broadcast-presence event listeners to modify nick, actor, reason
daurnimator <quae@daurnimator.com>
parents: 6515
diff changeset
   249
	reason = event.reason;
6278
fcc3ef191293 plugins/muc/muc: Fire broadcast presences event before creating full/anon presences
daurnimator <quae@daurnimator.com>
parents: 6277
diff changeset
   250
6515
ad159be9e9d7 plugins/muc/muc.lib: Don't expose actor jids to users when anonymous
daurnimator <quae@daurnimator.com>
parents: 6481
diff changeset
   251
	local whois = self:get_whois();
6516
3bbd59c03aeb plugins/muc/muc.lib: Allow muc-broadcast-presence event listeners to modify nick, actor, reason
daurnimator <quae@daurnimator.com>
parents: 6515
diff changeset
   252
6515
ad159be9e9d7 plugins/muc/muc.lib: Don't expose actor jids to users when anonymous
daurnimator <quae@daurnimator.com>
parents: 6481
diff changeset
   253
	local actor_nick;
ad159be9e9d7 plugins/muc/muc.lib: Don't expose actor jids to users when anonymous
daurnimator <quae@daurnimator.com>
parents: 6481
diff changeset
   254
	if actor then
8859
538bb632a2b6 MUC: Use util.jid.resource
Kim Alvefur <zash@zash.se>
parents: 8856
diff changeset
   255
		actor_nick = jid_resource(self:get_occupant_jid(actor));
6179
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   256
	end
6278
fcc3ef191293 plugins/muc/muc: Fire broadcast presences event before creating full/anon presences
daurnimator <quae@daurnimator.com>
parents: 6277
diff changeset
   257
6515
ad159be9e9d7 plugins/muc/muc.lib: Don't expose actor jids to users when anonymous
daurnimator <quae@daurnimator.com>
parents: 6481
diff changeset
   258
	local full_p, full_x;
ad159be9e9d7 plugins/muc/muc.lib: Don't expose actor jids to users when anonymous
daurnimator <quae@daurnimator.com>
parents: 6481
diff changeset
   259
	local function get_full_p()
ad159be9e9d7 plugins/muc/muc.lib: Don't expose actor jids to users when anonymous
daurnimator <quae@daurnimator.com>
parents: 6481
diff changeset
   260
		if full_p == nil then
7708
0a29a5e64f1d mod_muc/muc.lib: Allow passing different <x> elements to be passed to :publicise_occupant_status()
Kim Alvefur <zash@zash.se>
parents: 7689
diff changeset
   261
			full_x = st.clone(x.full or base_x);
6515
ad159be9e9d7 plugins/muc/muc.lib: Don't expose actor jids to users when anonymous
daurnimator <quae@daurnimator.com>
parents: 6481
diff changeset
   262
			self:build_item_list(occupant, full_x, false, nick, actor_nick, actor, reason);
ad159be9e9d7 plugins/muc/muc.lib: Don't expose actor jids to users when anonymous
daurnimator <quae@daurnimator.com>
parents: 6481
diff changeset
   263
			full_p = st.clone(base_presence):add_child(full_x);
ad159be9e9d7 plugins/muc/muc.lib: Don't expose actor jids to users when anonymous
daurnimator <quae@daurnimator.com>
parents: 6481
diff changeset
   264
		end
ad159be9e9d7 plugins/muc/muc.lib: Don't expose actor jids to users when anonymous
daurnimator <quae@daurnimator.com>
parents: 6481
diff changeset
   265
		return full_p, full_x;
ad159be9e9d7 plugins/muc/muc.lib: Don't expose actor jids to users when anonymous
daurnimator <quae@daurnimator.com>
parents: 6481
diff changeset
   266
	end
6266
cabeff5fc687 plugins/muc/muc.lib: Fire event for presence broadcast
daurnimator <quae@daurnimator.com>
parents: 6253
diff changeset
   267
6211
a4d26656df04 plugins/muc/muc.lib: Clean up whois handling
daurnimator <quae@daurnimator.com>
parents: 6209
diff changeset
   268
	local anon_p, anon_x;
a4d26656df04 plugins/muc/muc.lib: Clean up whois handling
daurnimator <quae@daurnimator.com>
parents: 6209
diff changeset
   269
	local function get_anon_p()
a4d26656df04 plugins/muc/muc.lib: Clean up whois handling
daurnimator <quae@daurnimator.com>
parents: 6209
diff changeset
   270
		if anon_p == nil then
7708
0a29a5e64f1d mod_muc/muc.lib: Allow passing different <x> elements to be passed to :publicise_occupant_status()
Kim Alvefur <zash@zash.se>
parents: 7689
diff changeset
   271
			anon_x = st.clone(x.anon or base_x);
6515
ad159be9e9d7 plugins/muc/muc.lib: Don't expose actor jids to users when anonymous
daurnimator <quae@daurnimator.com>
parents: 6481
diff changeset
   272
			self:build_item_list(occupant, anon_x, true, nick, actor_nick, nil, reason);
ad159be9e9d7 plugins/muc/muc.lib: Don't expose actor jids to users when anonymous
daurnimator <quae@daurnimator.com>
parents: 6481
diff changeset
   273
			anon_p = st.clone(base_presence):add_child(anon_x);
6211
a4d26656df04 plugins/muc/muc.lib: Clean up whois handling
daurnimator <quae@daurnimator.com>
parents: 6209
diff changeset
   274
		end
a4d26656df04 plugins/muc/muc.lib: Clean up whois handling
daurnimator <quae@daurnimator.com>
parents: 6209
diff changeset
   275
		return anon_p, anon_x;
a4d26656df04 plugins/muc/muc.lib: Clean up whois handling
daurnimator <quae@daurnimator.com>
parents: 6209
diff changeset
   276
	end
6179
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   277
6515
ad159be9e9d7 plugins/muc/muc.lib: Don't expose actor jids to users when anonymous
daurnimator <quae@daurnimator.com>
parents: 6481
diff changeset
   278
	local self_p, self_x;
9026
ce461a67d2cc MUC: Fix to send status code 100 *only* to the joining (fixes #680)
Kim Alvefur <zash@zash.se>
parents: 9025
diff changeset
   279
	do
6515
ad159be9e9d7 plugins/muc/muc.lib: Don't expose actor jids to users when anonymous
daurnimator <quae@daurnimator.com>
parents: 6481
diff changeset
   280
		-- Can always see your own full jids
ad159be9e9d7 plugins/muc/muc.lib: Don't expose actor jids to users when anonymous
daurnimator <quae@daurnimator.com>
parents: 6481
diff changeset
   281
		-- But not allowed to see actor's
7708
0a29a5e64f1d mod_muc/muc.lib: Allow passing different <x> elements to be passed to :publicise_occupant_status()
Kim Alvefur <zash@zash.se>
parents: 7689
diff changeset
   282
		self_x = st.clone(x.self or base_x);
6515
ad159be9e9d7 plugins/muc/muc.lib: Don't expose actor jids to users when anonymous
daurnimator <quae@daurnimator.com>
parents: 6481
diff changeset
   283
		self:build_item_list(occupant, self_x, false, nick, actor_nick, nil, reason);
ad159be9e9d7 plugins/muc/muc.lib: Don't expose actor jids to users when anonymous
daurnimator <quae@daurnimator.com>
parents: 6481
diff changeset
   284
		self_p = st.clone(base_presence):add_child(self_x);
ad159be9e9d7 plugins/muc/muc.lib: Don't expose actor jids to users when anonymous
daurnimator <quae@daurnimator.com>
parents: 6481
diff changeset
   285
	end
6211
a4d26656df04 plugins/muc/muc.lib: Clean up whois handling
daurnimator <quae@daurnimator.com>
parents: 6209
diff changeset
   286
11249
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   287
	local function get_p(rec_occupant)
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   288
		local pr;
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   289
		if can_see_real_jids(whois, rec_occupant) then
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   290
			pr = get_full_p();
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   291
		elseif occupant.bare_jid == rec_occupant.bare_jid then
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   292
			pr = self_p;
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   293
		else
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   294
			pr = get_anon_p();
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   295
		end
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   296
		return pr
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   297
	end
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   298
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   299
	if recipient then
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   300
		return self:route_to_occupant(recipient, get_p(recipient));
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   301
	end
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   302
10357
7b602e13c3b6 MUC: Add controls for whose presence is broadcast (closes #1335)
Lance Stout <lancestout@gmail.com>
parents: 10298
diff changeset
   303
	local broadcast_roles = self:get_presence_broadcast();
9619
66c6edf56397 MUC: Fix spelling in comments
Kim Alvefur <zash@zash.se>
parents: 9617
diff changeset
   304
	-- General populace
7092
890f4b2cc444 MUC: Add luacheck annotations, remove unused vars, rename conflicting vars etc
Kim Alvefur <zash@zash.se>
parents: 7016
diff changeset
   305
	for occupant_nick, n_occupant in self:each_occupant() do
890f4b2cc444 MUC: Add luacheck annotations, remove unused vars, rename conflicting vars etc
Kim Alvefur <zash@zash.se>
parents: 7016
diff changeset
   306
		if occupant_nick ~= occupant.nick then
11249
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   307
			local pr = get_p(n_occupant);
10357
7b602e13c3b6 MUC: Add controls for whose presence is broadcast (closes #1335)
Lance Stout <lancestout@gmail.com>
parents: 10298
diff changeset
   308
			if broadcast_roles[occupant.role or "none"] or force_unavailable then
7b602e13c3b6 MUC: Add controls for whose presence is broadcast (closes #1335)
Lance Stout <lancestout@gmail.com>
parents: 10298
diff changeset
   309
				self:route_to_occupant(n_occupant, pr);
7b602e13c3b6 MUC: Add controls for whose presence is broadcast (closes #1335)
Lance Stout <lancestout@gmail.com>
parents: 10298
diff changeset
   310
			elseif prev_role and broadcast_roles[prev_role] then
7b602e13c3b6 MUC: Add controls for whose presence is broadcast (closes #1335)
Lance Stout <lancestout@gmail.com>
parents: 10298
diff changeset
   311
				pr.attr.type = 'unavailable';
7b602e13c3b6 MUC: Add controls for whose presence is broadcast (closes #1335)
Lance Stout <lancestout@gmail.com>
parents: 10298
diff changeset
   312
				self:route_to_occupant(n_occupant, pr);
7b602e13c3b6 MUC: Add controls for whose presence is broadcast (closes #1335)
Lance Stout <lancestout@gmail.com>
parents: 10298
diff changeset
   313
			end
7b602e13c3b6 MUC: Add controls for whose presence is broadcast (closes #1335)
Lance Stout <lancestout@gmail.com>
parents: 10298
diff changeset
   314
6179
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   315
		end
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   316
	end
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   317
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   318
	-- Presences for occupant itself
6515
ad159be9e9d7 plugins/muc/muc.lib: Don't expose actor jids to users when anonymous
daurnimator <quae@daurnimator.com>
parents: 6481
diff changeset
   319
	self_x:tag("status", {code = "110";}):up();
6179
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   320
	if occupant.role == nil then
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   321
		-- They get an unavailable
6515
ad159be9e9d7 plugins/muc/muc.lib: Don't expose actor jids to users when anonymous
daurnimator <quae@daurnimator.com>
parents: 6481
diff changeset
   322
		self:route_to_occupant(occupant, self_p);
6179
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   323
	else
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   324
		-- use their own presences as templates
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   325
		for full_jid, pr in occupant:each_session() do
6212
b82523f92b06 plugins/muc/muc.lib: Refactor of change-nick presence handling
daurnimator <quae@daurnimator.com>
parents: 6211
diff changeset
   326
			pr = st.clone(pr);
10808
4dc57789f51b Fixes #1533 Hats don't get sent out to own MUC user
JC Brand <jc@opkode.com>
parents: 10761
diff changeset
   327
			module:fire_event("muc-build-occupant-presence", { room = self, occupant = occupant, stanza = pr });
6212
b82523f92b06 plugins/muc/muc.lib: Refactor of change-nick presence handling
daurnimator <quae@daurnimator.com>
parents: 6211
diff changeset
   328
			pr.attr.to = full_jid;
6515
ad159be9e9d7 plugins/muc/muc.lib: Don't expose actor jids to users when anonymous
daurnimator <quae@daurnimator.com>
parents: 6481
diff changeset
   329
			pr:add_child(self_x);
6212
b82523f92b06 plugins/muc/muc.lib: Refactor of change-nick presence handling
daurnimator <quae@daurnimator.com>
parents: 6211
diff changeset
   330
			self:route_stanza(pr);
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   331
		end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   332
	end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   333
end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5681
diff changeset
   334
6179
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   335
function room_mt:send_occupant_list(to, filter)
6185
d4a8840e72f9 plugins/muc/muc.lib: Fix anonymous check in `send_occupant_list`
daurnimator <quae@daurnimator.com>
parents: 6184
diff changeset
   336
	local to_bare = jid_bare(to);
10357
7b602e13c3b6 MUC: Add controls for whose presence is broadcast (closes #1335)
Lance Stout <lancestout@gmail.com>
parents: 10298
diff changeset
   337
	local broadcast_roles = self:get_presence_broadcast();
11249
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   338
	local is_anonymous = self:is_anonymous_for(to);
10691
8c2c5b4fde32 MUC: Support for broadcasting unavailable presence for affiliated offline users
Matthew Wild <mwild1@gmail.com>
parents: 10690
diff changeset
   339
	local broadcast_bare_jids = {}; -- Track which bare JIDs we have sent presence for
6179
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   340
	for occupant_jid, occupant in self:each_occupant() do
10691
8c2c5b4fde32 MUC: Support for broadcasting unavailable presence for affiliated offline users
Matthew Wild <mwild1@gmail.com>
parents: 10690
diff changeset
   341
		broadcast_bare_jids[occupant.bare_jid] = true;
6184
2bfc4b12ec8f plugins/muc/muc.lib: Allow `:send_occupant_list` to have no filter
daurnimator <quae@daurnimator.com>
parents: 6183
diff changeset
   342
		if filter == nil or filter(occupant_jid, occupant) then
6179
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   343
			local x = st.stanza("x", {xmlns='http://jabber.org/protocol/muc#user'});
6185
d4a8840e72f9 plugins/muc/muc.lib: Fix anonymous check in `send_occupant_list`
daurnimator <quae@daurnimator.com>
parents: 6184
diff changeset
   344
			self:build_item_list(occupant, x, is_anonymous and to_bare ~= occupant.bare_jid); -- can always see your own jids
6179
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   345
			local pres = st.clone(occupant:get_presence());
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   346
			pres.attr.to = to;
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   347
			pres:add_child(x);
10716
dc24a8783c5d MUC: Add new event 'muc-build-occupant-presence' for plugins to extend occupant presence
Matthew Wild <mwild1@gmail.com>
parents: 10695
diff changeset
   348
			module:fire_event("muc-build-occupant-presence", { room = self, occupant = occupant, stanza = pres });
10357
7b602e13c3b6 MUC: Add controls for whose presence is broadcast (closes #1335)
Lance Stout <lancestout@gmail.com>
parents: 10298
diff changeset
   349
			if to_bare == occupant.bare_jid or broadcast_roles[occupant.role or "none"] then
7b602e13c3b6 MUC: Add controls for whose presence is broadcast (closes #1335)
Lance Stout <lancestout@gmail.com>
parents: 10298
diff changeset
   350
				self:route_stanza(pres);
7b602e13c3b6 MUC: Add controls for whose presence is broadcast (closes #1335)
Lance Stout <lancestout@gmail.com>
parents: 10298
diff changeset
   351
			end
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   352
		end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   353
	end
10691
8c2c5b4fde32 MUC: Support for broadcasting unavailable presence for affiliated offline users
Matthew Wild <mwild1@gmail.com>
parents: 10690
diff changeset
   354
	if broadcast_roles.none then
8c2c5b4fde32 MUC: Support for broadcasting unavailable presence for affiliated offline users
Matthew Wild <mwild1@gmail.com>
parents: 10690
diff changeset
   355
		-- Broadcast stanzas for affiliated users not currently in the MUC
8c2c5b4fde32 MUC: Support for broadcasting unavailable presence for affiliated offline users
Matthew Wild <mwild1@gmail.com>
parents: 10690
diff changeset
   356
		for affiliated_jid, affiliation, affiliation_data in self:each_affiliation() do
8c2c5b4fde32 MUC: Support for broadcasting unavailable presence for affiliated offline users
Matthew Wild <mwild1@gmail.com>
parents: 10690
diff changeset
   357
			local nick = affiliation_data and affiliation_data.reserved_nickname;
8c2c5b4fde32 MUC: Support for broadcasting unavailable presence for affiliated offline users
Matthew Wild <mwild1@gmail.com>
parents: 10690
diff changeset
   358
			if (nick or not is_anonymous) and not broadcast_bare_jids[affiliated_jid]
8c2c5b4fde32 MUC: Support for broadcasting unavailable presence for affiliated offline users
Matthew Wild <mwild1@gmail.com>
parents: 10690
diff changeset
   359
			and (filter == nil or filter(affiliated_jid, nil)) then
8c2c5b4fde32 MUC: Support for broadcasting unavailable presence for affiliated offline users
Matthew Wild <mwild1@gmail.com>
parents: 10690
diff changeset
   360
				local from = nick and (self.jid.."/"..nick) or self.jid;
8c2c5b4fde32 MUC: Support for broadcasting unavailable presence for affiliated offline users
Matthew Wild <mwild1@gmail.com>
parents: 10690
diff changeset
   361
				local pres = st.presence({ to = to, from = from, type = "unavailable" })
8c2c5b4fde32 MUC: Support for broadcasting unavailable presence for affiliated offline users
Matthew Wild <mwild1@gmail.com>
parents: 10690
diff changeset
   362
					:tag("x", { xmlns = 'http://jabber.org/protocol/muc#user' })
8c2c5b4fde32 MUC: Support for broadcasting unavailable presence for affiliated offline users
Matthew Wild <mwild1@gmail.com>
parents: 10690
diff changeset
   363
						:tag("item", {
8c2c5b4fde32 MUC: Support for broadcasting unavailable presence for affiliated offline users
Matthew Wild <mwild1@gmail.com>
parents: 10690
diff changeset
   364
							affiliation = affiliation;
8c2c5b4fde32 MUC: Support for broadcasting unavailable presence for affiliated offline users
Matthew Wild <mwild1@gmail.com>
parents: 10690
diff changeset
   365
							role = "none";
8c2c5b4fde32 MUC: Support for broadcasting unavailable presence for affiliated offline users
Matthew Wild <mwild1@gmail.com>
parents: 10690
diff changeset
   366
							nick = nick;
8c2c5b4fde32 MUC: Support for broadcasting unavailable presence for affiliated offline users
Matthew Wild <mwild1@gmail.com>
parents: 10690
diff changeset
   367
							jid = not is_anonymous and affiliated_jid or nil }):up()
8c2c5b4fde32 MUC: Support for broadcasting unavailable presence for affiliated offline users
Matthew Wild <mwild1@gmail.com>
parents: 10690
diff changeset
   368
						:up();
8c2c5b4fde32 MUC: Support for broadcasting unavailable presence for affiliated offline users
Matthew Wild <mwild1@gmail.com>
parents: 10690
diff changeset
   369
				self:route_stanza(pres);
8c2c5b4fde32 MUC: Support for broadcasting unavailable presence for affiliated offline users
Matthew Wild <mwild1@gmail.com>
parents: 10690
diff changeset
   370
			end
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   371
		end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   372
	end
5983
930109558aa2 MUC: Split out sending of the topic into method separate from sending history
Kim Alvefur <zash@zash.se>
parents: 5982
diff changeset
   373
end
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   374
2503
bb6b0bd7f2cf MUC: Converted some local functions into methods.
Waqas Hussain <waqas20@gmail.com>
parents: 2416
diff changeset
   375
function room_mt:get_disco_info(stanza)
11146
552cafd30eb2 MUC: Preserve disco 'node' attribute (or lack thereof) in response (fix #1595) (thanks lessthan3)
Kim Alvefur <zash@zash.se>
parents: 10757
diff changeset
   376
	local node = stanza.tags[1].attr.node;
9302
2466b533f63d MUC: Fix to correctly return 'node' in disco#info responses (thanks jc)
Matthew Wild <mwild1@gmail.com>
parents: 9268
diff changeset
   377
	local reply = st.reply(stanza):tag("query", { xmlns = "http://jabber.org/protocol/disco#info", node = node });
9241
0c1a1172d942 MUC: Add support for separate events for disco#info queries with a 'node'
Matthew Wild <mwild1@gmail.com>
parents: 9240
diff changeset
   378
	local event_name = "muc-disco#info";
0c1a1172d942 MUC: Add support for separate events for disco#info queries with a 'node'
Matthew Wild <mwild1@gmail.com>
parents: 9240
diff changeset
   379
	local event_data = { room = self, reply = reply, stanza = stanza };
0c1a1172d942 MUC: Add support for separate events for disco#info queries with a 'node'
Matthew Wild <mwild1@gmail.com>
parents: 9240
diff changeset
   380
11146
552cafd30eb2 MUC: Preserve disco 'node' attribute (or lack thereof) in response (fix #1595) (thanks lessthan3)
Kim Alvefur <zash@zash.se>
parents: 10757
diff changeset
   381
	if node and node ~= "" then
9241
0c1a1172d942 MUC: Add support for separate events for disco#info queries with a 'node'
Matthew Wild <mwild1@gmail.com>
parents: 9240
diff changeset
   382
		event_name = event_name.."/"..node;
0c1a1172d942 MUC: Add support for separate events for disco#info queries with a 'node'
Matthew Wild <mwild1@gmail.com>
parents: 9240
diff changeset
   383
	else
0c1a1172d942 MUC: Add support for separate events for disco#info queries with a 'node'
Matthew Wild <mwild1@gmail.com>
parents: 9240
diff changeset
   384
		event_data.form = dataform.new {
0c1a1172d942 MUC: Add support for separate events for disco#info queries with a 'node'
Matthew Wild <mwild1@gmail.com>
parents: 9240
diff changeset
   385
			{ name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/muc#roominfo" };
0c1a1172d942 MUC: Add support for separate events for disco#info queries with a 'node'
Matthew Wild <mwild1@gmail.com>
parents: 9240
diff changeset
   386
		};
0c1a1172d942 MUC: Add support for separate events for disco#info queries with a 'node'
Matthew Wild <mwild1@gmail.com>
parents: 9240
diff changeset
   387
		event_data.formdata = {};
0c1a1172d942 MUC: Add support for separate events for disco#info queries with a 'node'
Matthew Wild <mwild1@gmail.com>
parents: 9240
diff changeset
   388
	end
0c1a1172d942 MUC: Add support for separate events for disco#info queries with a 'node'
Matthew Wild <mwild1@gmail.com>
parents: 9240
diff changeset
   389
	module:fire_event(event_name, event_data);
0c1a1172d942 MUC: Add support for separate events for disco#info queries with a 'node'
Matthew Wild <mwild1@gmail.com>
parents: 9240
diff changeset
   390
	if event_data.form then
0c1a1172d942 MUC: Add support for separate events for disco#info queries with a 'node'
Matthew Wild <mwild1@gmail.com>
parents: 9240
diff changeset
   391
		reply:add_child(event_data.form:form(event_data.formdata, "result"));
0c1a1172d942 MUC: Add support for separate events for disco#info queries with a 'node'
Matthew Wild <mwild1@gmail.com>
parents: 9240
diff changeset
   392
	end
7121
dacc07833b86 MUC: Fire a muc-disco#info event like in trunk so modules can extend the reply
Kim Alvefur <zash@zash.se>
parents: 7015
diff changeset
   393
	return reply;
1756
b2291156a9c2 MUC: Added service discovery replies for rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1755
diff changeset
   394
end
6200
57bc52f67564 plugins/muc/muc.lib: Split up get_disco_info into events
daurnimator <quae@daurnimator.com>
parents: 6199
diff changeset
   395
module:hook("muc-disco#info", function(event)
57bc52f67564 plugins/muc/muc.lib: Split up get_disco_info into events
daurnimator <quae@daurnimator.com>
parents: 6199
diff changeset
   396
	event.reply:tag("feature", {var = "http://jabber.org/protocol/muc"}):up();
8574
44217f40ff4a MUC: send muc#stanza_id feature as per XEP-0045 v1.31 (fixes #1097)
Jonas Wielicki <jonas@wielicki.name>
parents: 8530
diff changeset
   397
	event.reply:tag("feature", {var = "http://jabber.org/protocol/muc#stable_id"}):up();
10215
224e681c4db2 MUC: Advertise XEP-0410 support
Kim Alvefur <zash@zash.se>
parents: 9823
diff changeset
   398
	event.reply:tag("feature", {var = "http://jabber.org/protocol/muc#self-ping-optimization"}):up();
6200
57bc52f67564 plugins/muc/muc.lib: Split up get_disco_info into events
daurnimator <quae@daurnimator.com>
parents: 6199
diff changeset
   399
end);
57bc52f67564 plugins/muc/muc.lib: Split up get_disco_info into events
daurnimator <quae@daurnimator.com>
parents: 6199
diff changeset
   400
module:hook("muc-disco#info", function(event)
7122
50b9a7e86de9 MUC: Separate form data from form in disco#info event
Kim Alvefur <zash@zash.se>
parents: 7092
diff changeset
   401
	table.insert(event.form, { name = "muc#roominfo_occupants", label = "Number of occupants" });
50b9a7e86de9 MUC: Separate form data from form in disco#info event
Kim Alvefur <zash@zash.se>
parents: 7092
diff changeset
   402
	event.formdata["muc#roominfo_occupants"] = tostring(iterators.count(event.room:each_occupant()));
6200
57bc52f67564 plugins/muc/muc.lib: Split up get_disco_info into events
daurnimator <quae@daurnimator.com>
parents: 6199
diff changeset
   403
end);
57bc52f67564 plugins/muc/muc.lib: Split up get_disco_info into events
daurnimator <quae@daurnimator.com>
parents: 6199
diff changeset
   404
8894
d9b7db6f140f MUC: Ignore unused 'self' [luacheck]
Kim Alvefur <zash@zash.se>
parents: 8891
diff changeset
   405
function room_mt:get_disco_items(stanza) -- luacheck: ignore 212
8890
c47f220580fd Backed out changeset b8c3dbf76a2e (fixes #1162)
Kim Alvefur <zash@zash.se>
parents: 8845
diff changeset
   406
	return st.reply(stanza):query("http://jabber.org/protocol/disco#items");
1756
b2291156a9c2 MUC: Added service discovery replies for rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1755
diff changeset
   407
end
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   408
7092
890f4b2cc444 MUC: Add luacheck annotations, remove unused vars, rename conflicting vars etc
Kim Alvefur <zash@zash.se>
parents: 7016
diff changeset
   409
function room_mt:handle_kickable(origin, stanza) -- luacheck: ignore 212
6179
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   410
	local real_jid = stanza.attr.from;
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   411
	local occupant = self:get_occupant_by_real_jid(real_jid);
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   412
	if occupant == nil then return nil; end
10554
0566b45da987 MUC: Remove some unused variables [luacheck]
Kim Alvefur <zash@zash.se>
parents: 10455
diff changeset
   413
	local _, condition, text = stanza:get_error();
3506
0f46acca11cc MUC: Fixed traceback on presence errors lacking a condition.
Waqas Hussain <waqas20@gmail.com>
parents: 3446
diff changeset
   414
	local error_message = "Kicked: "..(condition and condition:gsub("%-", " ") or "presence error");
9150
e2bf4cd6d7a3 mod_muc: Fix incorrect variable usage [luacheck]
Matthew Wild <mwild1@gmail.com>
parents: 9148
diff changeset
   415
	if text and self:get_whois() == "anyone" then
2529
7968e8b3ecf9 MUC: Fixes and refactoring for the previous commit to work in all cases, text of error stanzas is now broadcast
Matthew Wild <mwild1@gmail.com>
parents: 2528
diff changeset
   416
		error_message = error_message..": "..text;
7968e8b3ecf9 MUC: Fixes and refactoring for the previous commit to work in all cases, text of error stanzas is now broadcast
Matthew Wild <mwild1@gmail.com>
parents: 2528
diff changeset
   417
	end
6179
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   418
	occupant:set_session(real_jid, st.presence({type="unavailable"})
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   419
		:tag('status'):text(error_message));
10688
de607875d4bd MUC: Pass previous role to :publicise_occupant_status() whenever possible
Matthew Wild <mwild1@gmail.com>
parents: 10666
diff changeset
   420
	local orig_role = occupant.role;
9006
a971023e9b6e MUC: Bring handling of presence errors to the room more in line with unavailable presence
Matthew Wild <mwild1@gmail.com>
parents: 9005
diff changeset
   421
	local is_last_session = occupant.jid == real_jid;
a971023e9b6e MUC: Bring handling of presence errors to the room more in line with unavailable presence
Matthew Wild <mwild1@gmail.com>
parents: 9005
diff changeset
   422
	if is_last_session then
a971023e9b6e MUC: Bring handling of presence errors to the room more in line with unavailable presence
Matthew Wild <mwild1@gmail.com>
parents: 9005
diff changeset
   423
		occupant.role = nil;
a971023e9b6e MUC: Bring handling of presence errors to the room more in line with unavailable presence
Matthew Wild <mwild1@gmail.com>
parents: 9005
diff changeset
   424
	end
a971023e9b6e MUC: Bring handling of presence errors to the room more in line with unavailable presence
Matthew Wild <mwild1@gmail.com>
parents: 9005
diff changeset
   425
	local new_occupant = self:save_occupant(occupant);
a971023e9b6e MUC: Bring handling of presence errors to the room more in line with unavailable presence
Matthew Wild <mwild1@gmail.com>
parents: 9005
diff changeset
   426
	local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user";});
a971023e9b6e MUC: Bring handling of presence errors to the room more in line with unavailable presence
Matthew Wild <mwild1@gmail.com>
parents: 9005
diff changeset
   427
	if is_last_session then
9151
a474c94d0b0a MUC: Remove 307 status from error-kicks (fixes #939)
Matthew Wild <mwild1@gmail.com>
parents: 9150
diff changeset
   428
		x:tag("status", {code = "333"});
9006
a971023e9b6e MUC: Bring handling of presence errors to the room more in line with unavailable presence
Matthew Wild <mwild1@gmail.com>
parents: 9005
diff changeset
   429
	end
10688
de607875d4bd MUC: Pass previous role to :publicise_occupant_status() whenever possible
Matthew Wild <mwild1@gmail.com>
parents: 10666
diff changeset
   430
	self:publicise_occupant_status(new_occupant or occupant, x, nil, nil, nil, orig_role);
9006
a971023e9b6e MUC: Bring handling of presence errors to the room more in line with unavailable presence
Matthew Wild <mwild1@gmail.com>
parents: 9005
diff changeset
   431
	if is_last_session then
10052
e5532fbdd163 MUC: Reflow event tables to improve readability
Kim Alvefur <zash@zash.se>
parents: 9866
diff changeset
   432
		module:fire_event("muc-occupant-left", {
e5532fbdd163 MUC: Reflow event tables to improve readability
Kim Alvefur <zash@zash.se>
parents: 9866
diff changeset
   433
				room = self;
e5532fbdd163 MUC: Reflow event tables to improve readability
Kim Alvefur <zash@zash.se>
parents: 9866
diff changeset
   434
				nick = occupant.nick;
e5532fbdd163 MUC: Reflow event tables to improve readability
Kim Alvefur <zash@zash.se>
parents: 9866
diff changeset
   435
				occupant = occupant;
e5532fbdd163 MUC: Reflow event tables to improve readability
Kim Alvefur <zash@zash.se>
parents: 9866
diff changeset
   436
			});
6457
6842b07fc7bc plugins/muc/muc.lib: Fire muc-occupant-left from other places an occupant may leave the room
daurnimator <quae@daurnimator.com>
parents: 6456
diff changeset
   437
	end
6100
c78ba94d3261 plugins/muc/muc.lib: Move all kick code into one place
daurnimator <quae@daurnimator.com>
parents: 6099
diff changeset
   438
	return true;
2529
7968e8b3ecf9 MUC: Fixes and refactoring for the previous commit to work in all cases, text of error stanzas is now broadcast
Matthew Wild <mwild1@gmail.com>
parents: 2528
diff changeset
   439
end
7968e8b3ecf9 MUC: Fixes and refactoring for the previous commit to work in all cases, text of error stanzas is now broadcast
Matthew Wild <mwild1@gmail.com>
parents: 2528
diff changeset
   440
6179
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   441
-- Give the room creator owner affiliation
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   442
module:hook("muc-room-pre-create", function(event)
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   443
	event.room:set_affiliation(true, jid_bare(event.stanza.attr.from), "owner");
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   444
end, -1);
6094
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   445
6180
35388114439f plugins/muc/muc.lib: non-function changes (reordering, semicolons and comments)
daurnimator <quae@daurnimator.com>
parents: 6179
diff changeset
   446
-- check if user is banned
6193
b9074126639f plugins/muc/muc.lib: Remove top level pre-join event. Assign event priorities for other handlers
daurnimator <quae@daurnimator.com>
parents: 6192
diff changeset
   447
module:hook("muc-occupant-pre-join", function(event)
6179
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   448
	local room, stanza = event.room, event.stanza;
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   449
	local affiliation = room:get_affiliation(stanza.attr.from);
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   450
	if affiliation == "outcast" then
10453
2e36a54906e4 MUC: Indicate that the room is the origin of various errors where 'from' is an occupant JID
Kim Alvefur <zash@zash.se>
parents: 10438
diff changeset
   451
		local reply = st.error_reply(stanza, "auth", "forbidden", nil, room.jid):up();
6135
6b061f8c6e11 plugins/muc/muc.lib: Add muc-occupant-prejoin events; Use it for banned, members-only, password, nick-conflict and lock checks
daurnimator <quae@daurnimator.com>
parents: 6134
diff changeset
   452
		reply.tags[1].attr.code = "403";
6b061f8c6e11 plugins/muc/muc.lib: Add muc-occupant-prejoin events; Use it for banned, members-only, password, nick-conflict and lock checks
daurnimator <quae@daurnimator.com>
parents: 6134
diff changeset
   453
		event.origin.send(reply:tag("x", {xmlns = "http://jabber.org/protocol/muc"}));
6b061f8c6e11 plugins/muc/muc.lib: Add muc-occupant-prejoin events; Use it for banned, members-only, password, nick-conflict and lock checks
daurnimator <quae@daurnimator.com>
parents: 6134
diff changeset
   454
		return true;
3254
a01c6411fdfb MUC: Added room:set_members_only(boolean) and room:is_members_only().
Waqas Hussain <waqas20@gmail.com>
parents: 3253
diff changeset
   455
	end
6193
b9074126639f plugins/muc/muc.lib: Remove top level pre-join event. Assign event priorities for other handlers
daurnimator <quae@daurnimator.com>
parents: 6192
diff changeset
   456
end, -10);
6094
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   457
9027
43241e74bb19 MUC: Reject invisible nicknames (fixes #979)
Kim Alvefur <zash@zash.se>
parents: 9026
diff changeset
   458
module:hook("muc-occupant-pre-join", function(event)
10453
2e36a54906e4 MUC: Indicate that the room is the origin of various errors where 'from' is an occupant JID
Kim Alvefur <zash@zash.se>
parents: 10438
diff changeset
   459
	local room = event.room;
9029
91b5a5667016 MUC: Use nickname from occupant object
Kim Alvefur <zash@zash.se>
parents: 9028
diff changeset
   460
	local nick = jid_resource(event.occupant.nick);
9027
43241e74bb19 MUC: Reject invisible nicknames (fixes #979)
Kim Alvefur <zash@zash.se>
parents: 9026
diff changeset
   461
	if not nick:find("%S") then
10453
2e36a54906e4 MUC: Indicate that the room is the origin of various errors where 'from' is an occupant JID
Kim Alvefur <zash@zash.se>
parents: 10438
diff changeset
   462
		event.origin.send(st.error_reply(event.stanza, "modify", "not-allowed", "Invisible Nicknames are forbidden", room.jid));
9027
43241e74bb19 MUC: Reject invisible nicknames (fixes #979)
Kim Alvefur <zash@zash.se>
parents: 9026
diff changeset
   463
		return true;
43241e74bb19 MUC: Reject invisible nicknames (fixes #979)
Kim Alvefur <zash@zash.se>
parents: 9026
diff changeset
   464
	end
43241e74bb19 MUC: Reject invisible nicknames (fixes #979)
Kim Alvefur <zash@zash.se>
parents: 9026
diff changeset
   465
end, 1);
43241e74bb19 MUC: Reject invisible nicknames (fixes #979)
Kim Alvefur <zash@zash.se>
parents: 9026
diff changeset
   466
9030
4028eb4a9f7f MUC: Also prevent changing to an invisible nickname
Kim Alvefur <zash@zash.se>
parents: 9029
diff changeset
   467
module:hook("muc-occupant-pre-change", function(event)
10453
2e36a54906e4 MUC: Indicate that the room is the origin of various errors where 'from' is an occupant JID
Kim Alvefur <zash@zash.se>
parents: 10438
diff changeset
   468
	local room = event.room;
9030
4028eb4a9f7f MUC: Also prevent changing to an invisible nickname
Kim Alvefur <zash@zash.se>
parents: 9029
diff changeset
   469
	if not jid_resource(event.dest_occupant.nick):find("%S") then
10453
2e36a54906e4 MUC: Indicate that the room is the origin of various errors where 'from' is an occupant JID
Kim Alvefur <zash@zash.se>
parents: 10438
diff changeset
   470
		event.origin.send(st.error_reply(event.stanza, "modify", "not-allowed", "Invisible Nicknames are forbidden", room.jid));
9030
4028eb4a9f7f MUC: Also prevent changing to an invisible nickname
Kim Alvefur <zash@zash.se>
parents: 9029
diff changeset
   471
		return true;
4028eb4a9f7f MUC: Also prevent changing to an invisible nickname
Kim Alvefur <zash@zash.se>
parents: 9029
diff changeset
   472
	end
4028eb4a9f7f MUC: Also prevent changing to an invisible nickname
Kim Alvefur <zash@zash.se>
parents: 9029
diff changeset
   473
end, 1);
4028eb4a9f7f MUC: Also prevent changing to an invisible nickname
Kim Alvefur <zash@zash.se>
parents: 9029
diff changeset
   474
10365
6e051bfca12d MUC: Enforce strict resourceprep on nicknames (bye bye robot face)
Kim Alvefur <zash@zash.se>
parents: 10357
diff changeset
   475
module:hook("muc-occupant-pre-join", function(event)
10453
2e36a54906e4 MUC: Indicate that the room is the origin of various errors where 'from' is an occupant JID
Kim Alvefur <zash@zash.se>
parents: 10438
diff changeset
   476
	local room = event.room;
10365
6e051bfca12d MUC: Enforce strict resourceprep on nicknames (bye bye robot face)
Kim Alvefur <zash@zash.se>
parents: 10357
diff changeset
   477
	local nick = jid_resource(event.occupant.nick);
6e051bfca12d MUC: Enforce strict resourceprep on nicknames (bye bye robot face)
Kim Alvefur <zash@zash.se>
parents: 10357
diff changeset
   478
	if not resourceprep(nick, true) then -- strict
10453
2e36a54906e4 MUC: Indicate that the room is the origin of various errors where 'from' is an occupant JID
Kim Alvefur <zash@zash.se>
parents: 10438
diff changeset
   479
		event.origin.send(st.error_reply(event.stanza, "modify", "jid-malformed", "Nickname must pass strict validation", room.jid));
8916
560419b759c8 MUC: Remove support for GC 1.0 during room creation
Kim Alvefur <zash@zash.se>
parents: 8915
diff changeset
   480
		return true;
560419b759c8 MUC: Remove support for GC 1.0 during room creation
Kim Alvefur <zash@zash.se>
parents: 8915
diff changeset
   481
	end
10365
6e051bfca12d MUC: Enforce strict resourceprep on nicknames (bye bye robot face)
Kim Alvefur <zash@zash.se>
parents: 10357
diff changeset
   482
end, 2);
8916
560419b759c8 MUC: Remove support for GC 1.0 during room creation
Kim Alvefur <zash@zash.se>
parents: 8915
diff changeset
   483
10365
6e051bfca12d MUC: Enforce strict resourceprep on nicknames (bye bye robot face)
Kim Alvefur <zash@zash.se>
parents: 10357
diff changeset
   484
module:hook("muc-occupant-pre-change", function(event)
10455
347d16d70280 MUC: Add missing reference to room (thanks buildbot) [luacheck]
Kim Alvefur <zash@zash.se>
parents: 10453
diff changeset
   485
	local room = event.room;
10365
6e051bfca12d MUC: Enforce strict resourceprep on nicknames (bye bye robot face)
Kim Alvefur <zash@zash.se>
parents: 10357
diff changeset
   486
	local nick = jid_resource(event.dest_occupant.nick);
6e051bfca12d MUC: Enforce strict resourceprep on nicknames (bye bye robot face)
Kim Alvefur <zash@zash.se>
parents: 10357
diff changeset
   487
	if not resourceprep(nick, true) then -- strict
10453
2e36a54906e4 MUC: Indicate that the room is the origin of various errors where 'from' is an occupant JID
Kim Alvefur <zash@zash.se>
parents: 10438
diff changeset
   488
		event.origin.send(st.error_reply(event.stanza, "modify", "jid-malformed", "Nickname must pass strict validation", room.jid));
10365
6e051bfca12d MUC: Enforce strict resourceprep on nicknames (bye bye robot face)
Kim Alvefur <zash@zash.se>
parents: 10357
diff changeset
   489
		return true;
6e051bfca12d MUC: Enforce strict resourceprep on nicknames (bye bye robot face)
Kim Alvefur <zash@zash.se>
parents: 10357
diff changeset
   490
	end
6e051bfca12d MUC: Enforce strict resourceprep on nicknames (bye bye robot face)
Kim Alvefur <zash@zash.se>
parents: 10357
diff changeset
   491
end, 2);
6e051bfca12d MUC: Enforce strict resourceprep on nicknames (bye bye robot face)
Kim Alvefur <zash@zash.se>
parents: 10357
diff changeset
   492
7413
45f543c82893 MUC: Split out handling of the room-creating presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7412
diff changeset
   493
function room_mt:handle_first_presence(origin, stanza)
45f543c82893 MUC: Split out handling of the room-creating presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7412
diff changeset
   494
	local real_jid = stanza.attr.from;
45f543c82893 MUC: Split out handling of the room-creating presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7412
diff changeset
   495
	local dest_jid = stanza.attr.to;
45f543c82893 MUC: Split out handling of the room-creating presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7412
diff changeset
   496
	local bare_jid = jid_bare(real_jid);
45f543c82893 MUC: Split out handling of the room-creating presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7412
diff changeset
   497
	if module:fire_event("muc-room-pre-create", {
45f543c82893 MUC: Split out handling of the room-creating presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7412
diff changeset
   498
			room = self;
45f543c82893 MUC: Split out handling of the room-creating presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7412
diff changeset
   499
			origin = origin;
45f543c82893 MUC: Split out handling of the room-creating presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7412
diff changeset
   500
			stanza = stanza;
45f543c82893 MUC: Split out handling of the room-creating presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7412
diff changeset
   501
		}) then return true; end
45f543c82893 MUC: Split out handling of the room-creating presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7412
diff changeset
   502
	local is_first_dest_session = true;
45f543c82893 MUC: Split out handling of the room-creating presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7412
diff changeset
   503
	local dest_occupant = self:new_occupant(bare_jid, dest_jid);
45f543c82893 MUC: Split out handling of the room-creating presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7412
diff changeset
   504
7709
d92e186c2a1c MUC: Include the appropriate status code if nickname is changed during join process
Kim Alvefur <zash@zash.se>
parents: 7708
diff changeset
   505
	local orig_nick = dest_occupant.nick;
7413
45f543c82893 MUC: Split out handling of the room-creating presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7412
diff changeset
   506
	if module:fire_event("muc-occupant-pre-join", {
45f543c82893 MUC: Split out handling of the room-creating presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7412
diff changeset
   507
		room = self;
45f543c82893 MUC: Split out handling of the room-creating presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7412
diff changeset
   508
		origin = origin;
45f543c82893 MUC: Split out handling of the room-creating presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7412
diff changeset
   509
		stanza = stanza;
45f543c82893 MUC: Split out handling of the room-creating presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7412
diff changeset
   510
		is_first_session = is_first_dest_session;
45f543c82893 MUC: Split out handling of the room-creating presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7412
diff changeset
   511
		is_new_room = true;
45f543c82893 MUC: Split out handling of the room-creating presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7412
diff changeset
   512
		occupant = dest_occupant;
45f543c82893 MUC: Split out handling of the room-creating presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7412
diff changeset
   513
	}) then return true; end
7709
d92e186c2a1c MUC: Include the appropriate status code if nickname is changed during join process
Kim Alvefur <zash@zash.se>
parents: 7708
diff changeset
   514
	local nick_changed = orig_nick ~= dest_occupant.nick;
7413
45f543c82893 MUC: Split out handling of the room-creating presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7412
diff changeset
   515
45f543c82893 MUC: Split out handling of the room-creating presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7412
diff changeset
   516
	dest_occupant:set_session(real_jid, stanza);
45f543c82893 MUC: Split out handling of the room-creating presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7412
diff changeset
   517
	local dest_x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user";});
45f543c82893 MUC: Split out handling of the room-creating presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7412
diff changeset
   518
	dest_x:tag("status", {code = "201"}):up();
45f543c82893 MUC: Split out handling of the room-creating presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7412
diff changeset
   519
	if self:get_whois() == "anyone" then
45f543c82893 MUC: Split out handling of the room-creating presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7412
diff changeset
   520
		dest_x:tag("status", {code = "100"}):up();
45f543c82893 MUC: Split out handling of the room-creating presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7412
diff changeset
   521
	end
7709
d92e186c2a1c MUC: Include the appropriate status code if nickname is changed during join process
Kim Alvefur <zash@zash.se>
parents: 7708
diff changeset
   522
	if nick_changed then
9025
293ebfed71f7 MUC: Simplify creation of <{muc}x> for room creation
Kim Alvefur <zash@zash.se>
parents: 9020
diff changeset
   523
		dest_x:tag("status", {code = "210"}):up();
7709
d92e186c2a1c MUC: Include the appropriate status code if nickname is changed during join process
Kim Alvefur <zash@zash.se>
parents: 7708
diff changeset
   524
	end
7413
45f543c82893 MUC: Split out handling of the room-creating presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7412
diff changeset
   525
	self:save_occupant(dest_occupant);
45f543c82893 MUC: Split out handling of the room-creating presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7412
diff changeset
   526
9025
293ebfed71f7 MUC: Simplify creation of <{muc}x> for room creation
Kim Alvefur <zash@zash.se>
parents: 9020
diff changeset
   527
	self:publicise_occupant_status(dest_occupant, dest_x);
7413
45f543c82893 MUC: Split out handling of the room-creating presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7412
diff changeset
   528
45f543c82893 MUC: Split out handling of the room-creating presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7412
diff changeset
   529
	module:fire_event("muc-occupant-joined", {
45f543c82893 MUC: Split out handling of the room-creating presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7412
diff changeset
   530
		room = self;
45f543c82893 MUC: Split out handling of the room-creating presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7412
diff changeset
   531
		nick = dest_occupant.nick;
45f543c82893 MUC: Split out handling of the room-creating presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7412
diff changeset
   532
		occupant = dest_occupant;
45f543c82893 MUC: Split out handling of the room-creating presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7412
diff changeset
   533
		stanza = stanza;
45f543c82893 MUC: Split out handling of the room-creating presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7412
diff changeset
   534
		origin = origin;
45f543c82893 MUC: Split out handling of the room-creating presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7412
diff changeset
   535
	});
45f543c82893 MUC: Split out handling of the room-creating presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7412
diff changeset
   536
	module:fire_event("muc-occupant-session-new", {
45f543c82893 MUC: Split out handling of the room-creating presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7412
diff changeset
   537
		room = self;
45f543c82893 MUC: Split out handling of the room-creating presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7412
diff changeset
   538
		nick = dest_occupant.nick;
45f543c82893 MUC: Split out handling of the room-creating presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7412
diff changeset
   539
		occupant = dest_occupant;
45f543c82893 MUC: Split out handling of the room-creating presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7412
diff changeset
   540
		stanza = stanza;
45f543c82893 MUC: Split out handling of the room-creating presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7412
diff changeset
   541
		origin = origin;
45f543c82893 MUC: Split out handling of the room-creating presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7412
diff changeset
   542
		jid = real_jid;
45f543c82893 MUC: Split out handling of the room-creating presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7412
diff changeset
   543
	});
7414
f385cd6127b2 MUC: Add event for when room is done being created
Kim Alvefur <zash@zash.se>
parents: 7413
diff changeset
   544
	module:fire_event("muc-room-created", {
f385cd6127b2 MUC: Add event for when room is done being created
Kim Alvefur <zash@zash.se>
parents: 7413
diff changeset
   545
		room = self;
f385cd6127b2 MUC: Add event for when room is done being created
Kim Alvefur <zash@zash.se>
parents: 7413
diff changeset
   546
		creator = dest_occupant;
f385cd6127b2 MUC: Add event for when room is done being created
Kim Alvefur <zash@zash.se>
parents: 7413
diff changeset
   547
		stanza = stanza;
f385cd6127b2 MUC: Add event for when room is done being created
Kim Alvefur <zash@zash.se>
parents: 7413
diff changeset
   548
		origin = origin;
f385cd6127b2 MUC: Add event for when room is done being created
Kim Alvefur <zash@zash.se>
parents: 7413
diff changeset
   549
	});
7413
45f543c82893 MUC: Split out handling of the room-creating presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7412
diff changeset
   550
	return true;
2529
7968e8b3ecf9 MUC: Fixes and refactoring for the previous commit to work in all cases, text of error stanzas is now broadcast
Matthew Wild <mwild1@gmail.com>
parents: 2528
diff changeset
   551
end
7968e8b3ecf9 MUC: Fixes and refactoring for the previous commit to work in all cases, text of error stanzas is now broadcast
Matthew Wild <mwild1@gmail.com>
parents: 2528
diff changeset
   552
11249
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   553
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   554
function room_mt:is_anonymous_for(jid)
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   555
	local is_anonymous = false;
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   556
	local whois = self:get_whois();
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   557
	if whois ~= "anyone" then
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   558
		local affiliation = self:get_affiliation(jid);
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   559
		if affiliation ~= "admin" and affiliation ~= "owner" then
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   560
			local occupant = self:get_occupant_by_real_jid(jid);
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   561
			if not (occupant and can_see_real_jids(whois, occupant)) then
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   562
				is_anonymous = true;
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   563
			end
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   564
		end
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   565
	end
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   566
	return is_anonymous;
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   567
end
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   568
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   569
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   570
function room_mt:build_unavailable_presence(from_muc_jid, to_jid)
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   571
	local nick = jid_resource(from_muc_jid);
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   572
	local from_jid = self:get_registered_jid(nick);
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   573
	if (not from_jid) then
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   574
		module:log("debug", "Received presence probe for unavailable nickname that's not registered");
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   575
		return;
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   576
	end
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   577
	local is_anonymous = self:is_anonymous_for(to_jid);
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   578
	local affiliation = self:get_affiliation(from_jid) or "none";
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   579
	local pr = st.presence({ to = to_jid, from = from_muc_jid, type = "unavailable" })
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   580
		:tag("x", { xmlns = 'http://jabber.org/protocol/muc#user' })
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   581
			:tag("item", {
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   582
				affiliation = affiliation;
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   583
				role = "none";
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   584
				nick = nick;
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   585
				jid = not is_anonymous and from_jid or nil }):up()
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   586
			:up();
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   587
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   588
	local x = pr:get_child("x", "http://jabber.org/protocol/muc");
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   589
	local event = {
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   590
		room = self; stanza = pr; x = x;
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   591
		bare_jid = from_jid;
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   592
		nick = nick;
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   593
	}
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   594
	module:fire_event("muc-build-occupant-presence", event);
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   595
	return event.stanza;
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   596
end
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   597
11250
ab189e707705 MUC: Reject probes from non-occupants
JC Brand <jc@opkode.com>
parents: 11249
diff changeset
   598
function room_mt:respond_to_probe(origin, stanza, probing_occupant)
ab189e707705 MUC: Reject probes from non-occupants
JC Brand <jc@opkode.com>
parents: 11249
diff changeset
   599
	if probing_occupant == nil then
ab189e707705 MUC: Reject probes from non-occupants
JC Brand <jc@opkode.com>
parents: 11249
diff changeset
   600
		origin.send(st.error_reply(stanza, "cancel", "not-acceptable", "You are not currently connected to this chat", self.jid));
ab189e707705 MUC: Reject probes from non-occupants
JC Brand <jc@opkode.com>
parents: 11249
diff changeset
   601
		return;
ab189e707705 MUC: Reject probes from non-occupants
JC Brand <jc@opkode.com>
parents: 11249
diff changeset
   602
	end
ab189e707705 MUC: Reject probes from non-occupants
JC Brand <jc@opkode.com>
parents: 11249
diff changeset
   603
ab189e707705 MUC: Reject probes from non-occupants
JC Brand <jc@opkode.com>
parents: 11249
diff changeset
   604
	local from_muc_jid = stanza.attr.to;
ab189e707705 MUC: Reject probes from non-occupants
JC Brand <jc@opkode.com>
parents: 11249
diff changeset
   605
	local probed_occupant = self:get_occupant_by_nick(from_muc_jid);
ab189e707705 MUC: Reject probes from non-occupants
JC Brand <jc@opkode.com>
parents: 11249
diff changeset
   606
	if probed_occupant == nil then
ab189e707705 MUC: Reject probes from non-occupants
JC Brand <jc@opkode.com>
parents: 11249
diff changeset
   607
		local to_jid = stanza.attr.from;
ab189e707705 MUC: Reject probes from non-occupants
JC Brand <jc@opkode.com>
parents: 11249
diff changeset
   608
		local pr = self:build_unavailable_presence(from_muc_jid, to_jid);
ab189e707705 MUC: Reject probes from non-occupants
JC Brand <jc@opkode.com>
parents: 11249
diff changeset
   609
		if pr then
ab189e707705 MUC: Reject probes from non-occupants
JC Brand <jc@opkode.com>
parents: 11249
diff changeset
   610
			self:route_stanza(pr);
ab189e707705 MUC: Reject probes from non-occupants
JC Brand <jc@opkode.com>
parents: 11249
diff changeset
   611
		end
ab189e707705 MUC: Reject probes from non-occupants
JC Brand <jc@opkode.com>
parents: 11249
diff changeset
   612
		return;
ab189e707705 MUC: Reject probes from non-occupants
JC Brand <jc@opkode.com>
parents: 11249
diff changeset
   613
	end
ab189e707705 MUC: Reject probes from non-occupants
JC Brand <jc@opkode.com>
parents: 11249
diff changeset
   614
	local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user"});
ab189e707705 MUC: Reject probes from non-occupants
JC Brand <jc@opkode.com>
parents: 11249
diff changeset
   615
	self:publicise_occupant_status(probed_occupant, x, nil, nil, nil, nil, false, probing_occupant);
ab189e707705 MUC: Reject probes from non-occupants
JC Brand <jc@opkode.com>
parents: 11249
diff changeset
   616
end
ab189e707705 MUC: Reject probes from non-occupants
JC Brand <jc@opkode.com>
parents: 11249
diff changeset
   617
11249
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   618
7412
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   619
function room_mt:handle_normal_presence(origin, stanza)
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   620
	local type = stanza.attr.type;
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   621
	local real_jid = stanza.attr.from;
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   622
	local bare_jid = jid_bare(real_jid);
7413
45f543c82893 MUC: Split out handling of the room-creating presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7412
diff changeset
   623
	local orig_occupant = self:get_occupant_by_real_jid(real_jid);
8915
43806beda970 MUC: Move extraction of <{muc}x> earlier, to be used later to differentiate between join and presence update
Kim Alvefur <zash@zash.se>
parents: 8894
diff changeset
   624
	local muc_x = stanza:get_child("x", "http://jabber.org/protocol/muc");
8917
e9acb928c637 MUC: Remove support for GC 1.0 for joining
Kim Alvefur <zash@zash.se>
parents: 8916
diff changeset
   625
8934
ae84911c1441 MUC: Avoid sending error for unavailable presence in GC 1.0 check
Kim Alvefur <zash@zash.se>
parents: 8918
diff changeset
   626
	if orig_occupant == nil and not muc_x and stanza.attr.type == nil then
8917
e9acb928c637 MUC: Remove support for GC 1.0 for joining
Kim Alvefur <zash@zash.se>
parents: 8916
diff changeset
   627
		module:log("debug", "Attempted join without <x>, possibly desynced");
9084
ce57c69a20e2 MUC: Split long lines [luacheck strict]
Kim Alvefur <zash@zash.se>
parents: 9060
diff changeset
   628
		origin.send(st.error_reply(stanza, "cancel", "item-not-found",
10453
2e36a54906e4 MUC: Indicate that the room is the origin of various errors where 'from' is an occupant JID
Kim Alvefur <zash@zash.se>
parents: 10438
diff changeset
   629
			"You are not currently connected to this chat", self.jid));
8917
e9acb928c637 MUC: Remove support for GC 1.0 for joining
Kim Alvefur <zash@zash.se>
parents: 8916
diff changeset
   630
		return true;
e9acb928c637 MUC: Remove support for GC 1.0 for joining
Kim Alvefur <zash@zash.se>
parents: 8916
diff changeset
   631
	end
e9acb928c637 MUC: Remove support for GC 1.0 for joining
Kim Alvefur <zash@zash.se>
parents: 8916
diff changeset
   632
7412
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   633
	local is_first_dest_session;
7413
45f543c82893 MUC: Split out handling of the room-creating presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7412
diff changeset
   634
	local dest_occupant;
8757
ca40f9a5751a MUC: Move something into empty if branch
Kim Alvefur <zash@zash.se>
parents: 8731
diff changeset
   635
	if type == "unavailable" then
ca40f9a5751a MUC: Move something into empty if branch
Kim Alvefur <zash@zash.se>
parents: 8731
diff changeset
   636
		if orig_occupant == nil then return true; end -- Unavailable from someone not in the room
7412
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   637
		-- dest_occupant = nil
11249
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   638
	elseif type == "probe" then
11250
ab189e707705 MUC: Reject probes from non-occupants
JC Brand <jc@opkode.com>
parents: 11249
diff changeset
   639
		self:respond_to_probe(origin, stanza, orig_occupant)
11249
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   640
		return true;
7429
3a90e9732204 Backed out changeset 63141a85beea, broke multi-session nicks
Kim Alvefur <zash@zash.se>
parents: 7426
diff changeset
   641
	elseif orig_occupant and orig_occupant.nick == stanza.attr.to then -- Just a presence update
7412
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   642
		log("debug", "presence update for %s from session %s", orig_occupant.nick, real_jid);
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   643
		dest_occupant = orig_occupant;
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   644
	else
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   645
		local dest_jid = stanza.attr.to;
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   646
		dest_occupant = self:get_occupant_by_nick(dest_jid);
7429
3a90e9732204 Backed out changeset 63141a85beea, broke multi-session nicks
Kim Alvefur <zash@zash.se>
parents: 7426
diff changeset
   647
		if dest_occupant == nil then
7412
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   648
			log("debug", "no occupant found for %s; creating new occupant object for %s", dest_jid, real_jid);
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   649
			is_first_dest_session = true;
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   650
			dest_occupant = self:new_occupant(bare_jid, dest_jid);
10435
3db8372e203c MUC: Keep role across nickname change (fixes #1466)
Kim Alvefur <zash@zash.se>
parents: 10297
diff changeset
   651
			if orig_occupant then
3db8372e203c MUC: Keep role across nickname change (fixes #1466)
Kim Alvefur <zash@zash.se>
parents: 10297
diff changeset
   652
				dest_occupant.role = orig_occupant.role;
3db8372e203c MUC: Keep role across nickname change (fixes #1466)
Kim Alvefur <zash@zash.se>
parents: 10297
diff changeset
   653
			end
7412
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   654
		else
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   655
			is_first_dest_session = false;
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   656
		end
3507
b639042bb0d5 MUC: Added a 'Name' property (muc#roomconfig_roomname)
Kim Alvefur <zash@zash.se>
parents: 3446
diff changeset
   657
	end
7412
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   658
	local is_last_orig_session;
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   659
	if orig_occupant ~= nil then
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   660
		-- Is there are least 2 sessions?
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   661
		local iter, ob, last = orig_occupant:each_session();
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   662
		is_last_orig_session = iter(ob, iter(ob, last)) == nil;
3508
9e4c2b048f9a MUC: Added a 'Description' property (muc#roomconfig_roomdesc)
Kim Alvefur <zash@zash.se>
parents: 3507
diff changeset
   663
	end
7412
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   664
7709
d92e186c2a1c MUC: Include the appropriate status code if nickname is changed during join process
Kim Alvefur <zash@zash.se>
parents: 7708
diff changeset
   665
	local orig_nick = dest_occupant and dest_occupant.nick;
d92e186c2a1c MUC: Include the appropriate status code if nickname is changed during join process
Kim Alvefur <zash@zash.se>
parents: 7708
diff changeset
   666
7412
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   667
	local event, event_name = {
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   668
		room = self;
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   669
		origin = origin;
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   670
		stanza = stanza;
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   671
		is_first_session = is_first_dest_session;
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   672
		is_last_session = is_last_orig_session;
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   673
	};
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   674
	if orig_occupant == nil then
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   675
		event_name = "muc-occupant-pre-join";
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   676
		event.occupant = dest_occupant;
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   677
	elseif dest_occupant == nil then
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   678
		event_name = "muc-occupant-pre-leave";
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   679
		event.occupant = orig_occupant;
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   680
	else
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   681
		event_name = "muc-occupant-pre-change";
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   682
		event.orig_occupant = orig_occupant;
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   683
		event.dest_occupant = dest_occupant;
3250
38402e874b45 MUC: Added room:set_moderated(boolean) and room:is_moderated().
Waqas Hussain <waqas20@gmail.com>
parents: 3249
diff changeset
   684
	end
7412
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   685
	if module:fire_event(event_name, event) then return true; end
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   686
7709
d92e186c2a1c MUC: Include the appropriate status code if nickname is changed during join process
Kim Alvefur <zash@zash.se>
parents: 7708
diff changeset
   687
	local nick_changed = dest_occupant and orig_nick ~= dest_occupant.nick;
d92e186c2a1c MUC: Include the appropriate status code if nickname is changed during join process
Kim Alvefur <zash@zash.se>
parents: 7708
diff changeset
   688
7412
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   689
	-- Check for nick conflicts
7961
47cb54a08336 MUC: Split long line [luacheck]
Kim Alvefur <zash@zash.se>
parents: 7709
diff changeset
   690
	if dest_occupant ~= nil and not is_first_dest_session
47cb54a08336 MUC: Split long line [luacheck]
Kim Alvefur <zash@zash.se>
parents: 7709
diff changeset
   691
		and bare_jid ~= jid_bare(dest_occupant.bare_jid) then
47cb54a08336 MUC: Split long line [luacheck]
Kim Alvefur <zash@zash.se>
parents: 7709
diff changeset
   692
		-- new nick or has different bare real jid
7412
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   693
		log("debug", "%s couldn't join due to nick conflict: %s", real_jid, dest_occupant.nick);
10453
2e36a54906e4 MUC: Indicate that the room is the origin of various errors where 'from' is an occupant JID
Kim Alvefur <zash@zash.se>
parents: 10438
diff changeset
   694
		local reply = st.error_reply(stanza, "cancel", "conflict", nil, self.jid):up();
7412
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   695
		reply.tags[1].attr.code = "409";
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   696
		origin.send(reply:tag("x", {xmlns = "http://jabber.org/protocol/muc"}));
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   697
		return true;
3254
a01c6411fdfb MUC: Added room:set_members_only(boolean) and room:is_members_only().
Waqas Hussain <waqas20@gmail.com>
parents: 3253
diff changeset
   698
	end
7412
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   699
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   700
	-- Send presence stanza about original occupant
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   701
	if orig_occupant ~= nil and orig_occupant ~= dest_occupant then
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   702
		local orig_x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user";});
10688
de607875d4bd MUC: Pass previous role to :publicise_occupant_status() whenever possible
Matthew Wild <mwild1@gmail.com>
parents: 10666
diff changeset
   703
		local orig_role = orig_occupant.role;
7412
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   704
		local dest_nick;
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   705
		if dest_occupant == nil then -- Session is leaving
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   706
			log("debug", "session %s is leaving occupant %s", real_jid, orig_occupant.nick);
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   707
			if is_last_orig_session then
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   708
				orig_occupant.role = nil;
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   709
			end
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   710
			orig_occupant:set_session(real_jid, stanza);
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   711
		else
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   712
			log("debug", "session %s is changing from occupant %s to %s", real_jid, orig_occupant.nick, dest_occupant.nick);
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   713
			local generated_unavail = st.presence {from = orig_occupant.nick, to = real_jid, type = "unavailable"};
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   714
			orig_occupant:set_session(real_jid, generated_unavail);
8859
538bb632a2b6 MUC: Use util.jid.resource
Kim Alvefur <zash@zash.se>
parents: 8856
diff changeset
   715
			dest_nick = jid_resource(dest_occupant.nick);
7412
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   716
			if not is_first_dest_session then -- User is swapping into another pre-existing session
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   717
				log("debug", "session %s is swapping into multisession %s, showing it leave.", real_jid, dest_occupant.nick);
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   718
				-- Show the other session leaving
7421
e9f7043b86b5 MUC: Move status text out of <x> element (thanks Tobias)
Kim Alvefur <zash@zash.se>
parents: 7419
diff changeset
   719
				local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user";});
7412
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   720
				add_item(x, self:get_affiliation(bare_jid), "none");
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   721
				local pr = st.presence{from = dest_occupant.nick, to = real_jid, type = "unavailable"}
7421
e9f7043b86b5 MUC: Move status text out of <x> element (thanks Tobias)
Kim Alvefur <zash@zash.se>
parents: 7419
diff changeset
   722
					:tag("status"):text("you are joining pre-existing session " .. dest_nick):up()
7412
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   723
					:add_child(x);
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   724
				self:route_stanza(pr);
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   725
			end
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   726
			if is_first_dest_session and is_last_orig_session then -- Normal nick change
8731
41c959c5c84b Fix spelling throughout the codebase [codespell]
Kim Alvefur <zash@zash.se>
parents: 8594
diff changeset
   727
				log("debug", "no sessions in %s left; publicly marking as nick change", orig_occupant.nick);
7412
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   728
				orig_x:tag("status", {code = "303";}):up();
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   729
			else -- The session itself always needs to see a nick change
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   730
				-- don't want to get our old nick's available presence,
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   731
				-- so remove our session from there, and manually generate an unavailable
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   732
				orig_occupant:remove_session(real_jid);
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   733
				log("debug", "generating nick change for %s", real_jid);
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   734
				local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user";});
9084
ce57c69a20e2 MUC: Split long lines [luacheck strict]
Kim Alvefur <zash@zash.se>
parents: 9060
diff changeset
   735
				-- COMPAT: clients get confused if they see other items besides their own
ce57c69a20e2 MUC: Split long lines [luacheck strict]
Kim Alvefur <zash@zash.se>
parents: 9060
diff changeset
   736
				-- self:build_item_list(orig_occupant, x, false, dest_nick);
7412
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   737
				add_item(x, self:get_affiliation(bare_jid), orig_occupant.role, real_jid, dest_nick);
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   738
				x:tag("status", {code = "303";}):up();
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   739
				x:tag("status", {code = "110";}):up();
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   740
				self:route_stanza(generated_unavail:add_child(x));
9619
66c6edf56397 MUC: Fix spelling in comments
Kim Alvefur <zash@zash.se>
parents: 9617
diff changeset
   741
				dest_nick = nil; -- set dest_nick to nil; so general populace doesn't see it for whole orig_occupant
7412
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   742
			end
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   743
		end
7670
5523880760b3 MUC: Insert the appropriate status code (210) if the nickname is overridden
Kim Alvefur <zash@zash.se>
parents: 7664
diff changeset
   744
7412
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   745
		self:save_occupant(orig_occupant);
10688
de607875d4bd MUC: Pass previous role to :publicise_occupant_status() whenever possible
Matthew Wild <mwild1@gmail.com>
parents: 10666
diff changeset
   746
		self:publicise_occupant_status(orig_occupant, orig_x, dest_nick, nil, nil, orig_role);
7412
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   747
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   748
		if is_last_orig_session then
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   749
			module:fire_event("muc-occupant-left", {
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   750
				room = self;
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   751
				nick = orig_occupant.nick;
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   752
				occupant = orig_occupant;
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   753
				origin = origin;
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   754
				stanza = stanza;
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   755
			});
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   756
		end
3261
fe1c93296abd MUC: Added room:set_hidden(boolean) and room:is_hidden().
Waqas Hussain <waqas20@gmail.com>
parents: 3260
diff changeset
   757
	end
7412
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   758
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   759
	if dest_occupant ~= nil then
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   760
		dest_occupant:set_session(real_jid, stanza);
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   761
		self:save_occupant(dest_occupant);
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   762
7430
bf43a08e5a74 MUC: Send participant list and subject on explicit joins (thanks daurnimator)
Kim Alvefur <zash@zash.se>
parents: 7429
diff changeset
   763
		if orig_occupant == nil or muc_x then
8917
e9acb928c637 MUC: Remove support for GC 1.0 for joining
Kim Alvefur <zash@zash.se>
parents: 8916
diff changeset
   764
			-- Send occupant list to newly joined or desynced user
7412
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   765
			self:send_occupant_list(real_jid, function(nick, occupant) -- luacheck: ignore 212
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   766
				-- Don't include self
10691
8c2c5b4fde32 MUC: Support for broadcasting unavailable presence for affiliated offline users
Matthew Wild <mwild1@gmail.com>
parents: 10690
diff changeset
   767
				return (not occupant) or occupant:get_presence(real_jid) == nil;
7412
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   768
			end)
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   769
		end
9026
ce461a67d2cc MUC: Fix to send status code 100 *only* to the joining (fixes #680)
Kim Alvefur <zash@zash.se>
parents: 9025
diff changeset
   770
		local dest_x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user";});
ce461a67d2cc MUC: Fix to send status code 100 *only* to the joining (fixes #680)
Kim Alvefur <zash@zash.se>
parents: 9025
diff changeset
   771
		local self_x = st.clone(dest_x);
ce461a67d2cc MUC: Fix to send status code 100 *only* to the joining (fixes #680)
Kim Alvefur <zash@zash.se>
parents: 9025
diff changeset
   772
		if orig_occupant == nil and self:get_whois() == "anyone" then
ce461a67d2cc MUC: Fix to send status code 100 *only* to the joining (fixes #680)
Kim Alvefur <zash@zash.se>
parents: 9025
diff changeset
   773
			self_x:tag("status", {code = "100"}):up();
ce461a67d2cc MUC: Fix to send status code 100 *only* to the joining (fixes #680)
Kim Alvefur <zash@zash.se>
parents: 9025
diff changeset
   774
		end
7709
d92e186c2a1c MUC: Include the appropriate status code if nickname is changed during join process
Kim Alvefur <zash@zash.se>
parents: 7708
diff changeset
   775
		if nick_changed then
d92e186c2a1c MUC: Include the appropriate status code if nickname is changed during join process
Kim Alvefur <zash@zash.se>
parents: 7708
diff changeset
   776
			self_x:tag("status", {code="210"}):up();
d92e186c2a1c MUC: Include the appropriate status code if nickname is changed during join process
Kim Alvefur <zash@zash.se>
parents: 7708
diff changeset
   777
		end
10688
de607875d4bd MUC: Pass previous role to :publicise_occupant_status() whenever possible
Matthew Wild <mwild1@gmail.com>
parents: 10666
diff changeset
   778
		self:publicise_occupant_status(dest_occupant, {base=dest_x,self=self_x}, nil, nil, nil, orig_occupant and orig_occupant.role or nil);
7412
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   779
9084
ce57c69a20e2 MUC: Split long lines [luacheck strict]
Kim Alvefur <zash@zash.se>
parents: 9060
diff changeset
   780
		if orig_occupant ~= nil and orig_occupant ~= dest_occupant and not is_last_orig_session then
ce57c69a20e2 MUC: Split long lines [luacheck strict]
Kim Alvefur <zash@zash.se>
parents: 9060
diff changeset
   781
			-- If user is swapping and wasn't last original session
7412
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   782
			log("debug", "session %s split nicks; showing %s rejoining", real_jid, orig_occupant.nick);
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   783
			-- Show the original nick joining again
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   784
			local pr = st.clone(orig_occupant:get_presence());
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   785
			pr.attr.to = real_jid;
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   786
			local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user";});
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   787
			self:build_item_list(orig_occupant, x, false);
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   788
			-- TODO: new status code to inform client this was the multi-session it left?
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   789
			pr:add_child(x);
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   790
			self:route_stanza(pr);
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   791
		end
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   792
7430
bf43a08e5a74 MUC: Send participant list and subject on explicit joins (thanks daurnimator)
Kim Alvefur <zash@zash.se>
parents: 7429
diff changeset
   793
		if orig_occupant == nil or muc_x then
7412
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   794
			if is_first_dest_session then
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   795
				module:fire_event("muc-occupant-joined", {
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   796
					room = self;
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   797
					nick = dest_occupant.nick;
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   798
					occupant = dest_occupant;
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   799
					stanza = stanza;
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   800
					origin = origin;
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   801
				});
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   802
			end
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   803
			module:fire_event("muc-occupant-session-new", {
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   804
				room = self;
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   805
				nick = dest_occupant.nick;
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   806
				occupant = dest_occupant;
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   807
				stanza = stanza;
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   808
				origin = origin;
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   809
				jid = real_jid;
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   810
			});
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   811
		end
4119
813adb81d7da MUC: Add option to allow participants to change the subject.
Kim Alvefur <zash@zash.se>
parents: 3989
diff changeset
   812
	end
7412
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   813
	return true;
4528
875b90d5ce0f muc - implement per channel history limits
Markus Kötter <koetter@rrzn-hiwi.uni-hannover.de>
parents: 4419
diff changeset
   814
end
875b90d5ce0f muc - implement per channel history limits
Markus Kötter <koetter@rrzn-hiwi.uni-hannover.de>
parents: 4419
diff changeset
   815
6094
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   816
function room_mt:handle_presence_to_occupant(origin, stanza)
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   817
	local type = stanza.attr.type;
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   818
	if type == "error" then -- error, kick em out!
6100
c78ba94d3261 plugins/muc/muc.lib: Move all kick code into one place
daurnimator <quae@daurnimator.com>
parents: 6099
diff changeset
   819
		return self:handle_kickable(origin, stanza)
11249
43b43e7156b8 MUC: Add support for presence probes (fixes #1535)
JC Brand <jc@opkode.com>
parents: 11240
diff changeset
   820
	elseif type == nil or type == "unavailable" or type == "probe" then
7412
9a3ce6da3256 MUC: Split out handling of normal (un)available presence into its own method
Kim Alvefur <zash@zash.se>
parents: 7409
diff changeset
   821
		return self:handle_normal_presence(origin, stanza);
6094
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   822
	elseif type ~= 'result' then -- bad type
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   823
		if type ~= 'visible' and type ~= 'invisible' then -- COMPAT ejabberd can broadcast or forward XEP-0018 presences
10453
2e36a54906e4 MUC: Indicate that the room is the origin of various errors where 'from' is an occupant JID
Kim Alvefur <zash@zash.se>
parents: 10438
diff changeset
   824
			origin.send(st.error_reply(stanza, "modify", "bad-request", nil, self.jid)); -- FIXME correct error?
6094
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   825
		end
5600
1b326a1e4da6 mod_muc: Add getter/setter for 'whois' (fixes traceback)
Matthew Wild <mwild1@gmail.com>
parents: 5580
diff changeset
   826
	end
6094
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   827
	return true;
5600
1b326a1e4da6 mod_muc: Add getter/setter for 'whois' (fixes traceback)
Matthew Wild <mwild1@gmail.com>
parents: 5580
diff changeset
   828
end
1b326a1e4da6 mod_muc: Add getter/setter for 'whois' (fixes traceback)
Matthew Wild <mwild1@gmail.com>
parents: 5580
diff changeset
   829
6094
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   830
function room_mt:handle_iq_to_occupant(origin, stanza)
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   831
	local from, to = stanza.attr.from, stanza.attr.to;
6096
84f9123637d4 plugins/muc/muc.lib: Refactor _to_occupant handlers
daurnimator <quae@daurnimator.com>
parents: 6095
diff changeset
   832
	local type = stanza.attr.type;
84f9123637d4 plugins/muc/muc.lib: Refactor _to_occupant handlers
daurnimator <quae@daurnimator.com>
parents: 6095
diff changeset
   833
	local id = stanza.attr.id;
6179
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   834
	local occupant = self:get_occupant_by_nick(to);
6096
84f9123637d4 plugins/muc/muc.lib: Refactor _to_occupant handlers
daurnimator <quae@daurnimator.com>
parents: 6095
diff changeset
   835
	if (type == "error" or type == "result") then
6097
538cdc3d8225 plugins/muc/muc.lib: Move (de)construct_stanza_id into `handle_iq_to_occupant`
daurnimator <quae@daurnimator.com>
parents: 6096
diff changeset
   836
		do -- deconstruct_stanza_id
6216
50b8b7caf200 plugins/muc/muc.lib: Move occupancy check to later in `deconstruct_stanza_id`: As vcards are from the bare jid, you need to use the `from_jid` out of the encoded `id`
daurnimator <quae@daurnimator.com>
parents: 6215
diff changeset
   837
			if not occupant then return nil; end
7092
890f4b2cc444 MUC: Add luacheck annotations, remove unused vars, rename conflicting vars etc
Kim Alvefur <zash@zash.se>
parents: 7016
diff changeset
   838
			local from_jid, orig_id, to_jid_hash = (base64.decode(id) or ""):match("^(%Z+)%z(%Z*)%z(.+)$");
6097
538cdc3d8225 plugins/muc/muc.lib: Move (de)construct_stanza_id into `handle_iq_to_occupant`
daurnimator <quae@daurnimator.com>
parents: 6096
diff changeset
   839
			if not(from == from_jid or from == jid_bare(from_jid)) then return nil; end
6216
50b8b7caf200 plugins/muc/muc.lib: Move occupancy check to later in `deconstruct_stanza_id`: As vcards are from the bare jid, you need to use the `from_jid` out of the encoded `id`
daurnimator <quae@daurnimator.com>
parents: 6215
diff changeset
   840
			local from_occupant_jid = self:get_occupant_jid(from_jid);
50b8b7caf200 plugins/muc/muc.lib: Move occupancy check to later in `deconstruct_stanza_id`: As vcards are from the bare jid, you need to use the `from_jid` out of the encoded `id`
daurnimator <quae@daurnimator.com>
parents: 6215
diff changeset
   841
			if from_occupant_jid == nil then return nil; end
6097
538cdc3d8225 plugins/muc/muc.lib: Move (de)construct_stanza_id into `handle_iq_to_occupant`
daurnimator <quae@daurnimator.com>
parents: 6096
diff changeset
   842
			local session_jid
6179
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   843
			for to_jid in occupant:each_session() do
6097
538cdc3d8225 plugins/muc/muc.lib: Move (de)construct_stanza_id into `handle_iq_to_occupant`
daurnimator <quae@daurnimator.com>
parents: 6096
diff changeset
   844
				if md5(to_jid) == to_jid_hash then
538cdc3d8225 plugins/muc/muc.lib: Move (de)construct_stanza_id into `handle_iq_to_occupant`
daurnimator <quae@daurnimator.com>
parents: 6096
diff changeset
   845
					session_jid = to_jid;
538cdc3d8225 plugins/muc/muc.lib: Move (de)construct_stanza_id into `handle_iq_to_occupant`
daurnimator <quae@daurnimator.com>
parents: 6096
diff changeset
   846
					break;
538cdc3d8225 plugins/muc/muc.lib: Move (de)construct_stanza_id into `handle_iq_to_occupant`
daurnimator <quae@daurnimator.com>
parents: 6096
diff changeset
   847
				end
538cdc3d8225 plugins/muc/muc.lib: Move (de)construct_stanza_id into `handle_iq_to_occupant`
daurnimator <quae@daurnimator.com>
parents: 6096
diff changeset
   848
			end
538cdc3d8225 plugins/muc/muc.lib: Move (de)construct_stanza_id into `handle_iq_to_occupant`
daurnimator <quae@daurnimator.com>
parents: 6096
diff changeset
   849
			if session_jid == nil then return nil; end
7092
890f4b2cc444 MUC: Add luacheck annotations, remove unused vars, rename conflicting vars etc
Kim Alvefur <zash@zash.se>
parents: 7016
diff changeset
   850
			stanza.attr.from, stanza.attr.to, stanza.attr.id = from_occupant_jid, session_jid, orig_id;
5061
186f34d88073 MUC: Fix private IQ and message routing.
Waqas Hussain <waqas20@gmail.com>
parents: 4999
diff changeset
   851
		end
6096
84f9123637d4 plugins/muc/muc.lib: Refactor _to_occupant handlers
daurnimator <quae@daurnimator.com>
parents: 6095
diff changeset
   852
		log("debug", "%s sent private iq stanza to %s (%s)", from, to, stanza.attr.to);
6179
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   853
		self:route_stanza(stanza);
6096
84f9123637d4 plugins/muc/muc.lib: Refactor _to_occupant handlers
daurnimator <quae@daurnimator.com>
parents: 6095
diff changeset
   854
		stanza.attr.from, stanza.attr.to, stanza.attr.id = from, to, id;
84f9123637d4 plugins/muc/muc.lib: Refactor _to_occupant handlers
daurnimator <quae@daurnimator.com>
parents: 6095
diff changeset
   855
		return true;
84f9123637d4 plugins/muc/muc.lib: Refactor _to_occupant handlers
daurnimator <quae@daurnimator.com>
parents: 6095
diff changeset
   856
	else -- Type is "get" or "set"
6216
50b8b7caf200 plugins/muc/muc.lib: Move occupancy check to later in `deconstruct_stanza_id`: As vcards are from the bare jid, you need to use the `from_jid` out of the encoded `id`
daurnimator <quae@daurnimator.com>
parents: 6215
diff changeset
   857
		local current_nick = self:get_occupant_jid(from);
6096
84f9123637d4 plugins/muc/muc.lib: Refactor _to_occupant handlers
daurnimator <quae@daurnimator.com>
parents: 6095
diff changeset
   858
		if not current_nick then
10453
2e36a54906e4 MUC: Indicate that the room is the origin of various errors where 'from' is an occupant JID
Kim Alvefur <zash@zash.se>
parents: 10438
diff changeset
   859
			origin.send(st.error_reply(stanza, "cancel", "not-acceptable", "You are not currently connected to this chat", self.jid));
6096
84f9123637d4 plugins/muc/muc.lib: Refactor _to_occupant handlers
daurnimator <quae@daurnimator.com>
parents: 6095
diff changeset
   860
			return true;
6094
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   861
		end
6179
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   862
		if not occupant then -- recipient not in room
10453
2e36a54906e4 MUC: Indicate that the room is the origin of various errors where 'from' is an occupant JID
Kim Alvefur <zash@zash.se>
parents: 10438
diff changeset
   863
			origin.send(st.error_reply(stanza, "cancel", "item-not-found", "Recipient not in room", self.jid));
6096
84f9123637d4 plugins/muc/muc.lib: Refactor _to_occupant handlers
daurnimator <quae@daurnimator.com>
parents: 6095
diff changeset
   864
			return true;
84f9123637d4 plugins/muc/muc.lib: Refactor _to_occupant handlers
daurnimator <quae@daurnimator.com>
parents: 6095
diff changeset
   865
		end
9447
7c1cdf5f9f83 MUC: Respond to ping per XEP-0410: MUC Self-Ping server optimization (closes #1220)
Kim Alvefur <zash@zash.se>
parents: 9318
diff changeset
   866
		-- XEP-0410 MUC Self-Ping #1220
7c1cdf5f9f83 MUC: Respond to ping per XEP-0410: MUC Self-Ping server optimization (closes #1220)
Kim Alvefur <zash@zash.se>
parents: 9318
diff changeset
   867
		if to == current_nick and stanza.attr.type == "get" and stanza:get_child("ping", "urn:xmpp:ping") then
7c1cdf5f9f83 MUC: Respond to ping per XEP-0410: MUC Self-Ping server optimization (closes #1220)
Kim Alvefur <zash@zash.se>
parents: 9318
diff changeset
   868
			self:route_stanza(st.reply(stanza));
7c1cdf5f9f83 MUC: Respond to ping per XEP-0410: MUC Self-Ping server optimization (closes #1220)
Kim Alvefur <zash@zash.se>
parents: 9318
diff changeset
   869
			return true;
7c1cdf5f9f83 MUC: Respond to ping per XEP-0410: MUC Self-Ping server optimization (closes #1220)
Kim Alvefur <zash@zash.se>
parents: 9318
diff changeset
   870
		end
6097
538cdc3d8225 plugins/muc/muc.lib: Move (de)construct_stanza_id into `handle_iq_to_occupant`
daurnimator <quae@daurnimator.com>
parents: 6096
diff changeset
   871
		do -- construct_stanza_id
6179
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   872
			stanza.attr.id = base64.encode(occupant.jid.."\0"..stanza.attr.id.."\0"..md5(from));
6097
538cdc3d8225 plugins/muc/muc.lib: Move (de)construct_stanza_id into `handle_iq_to_occupant`
daurnimator <quae@daurnimator.com>
parents: 6096
diff changeset
   873
		end
6179
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   874
		stanza.attr.from, stanza.attr.to = current_nick, occupant.jid;
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   875
		log("debug", "%s sent private iq stanza to %s (%s)", from, to, occupant.jid);
9197
11a0b32fef24 MUC: Direct PubSub queries to occupants to their real bare JID
Kim Alvefur <zash@zash.se>
parents: 9151
diff changeset
   876
		local iq_ns = stanza.tags[1].attr.xmlns;
9267
f13517b63e6c MUC: Allow vCard4 requests trough
Kim Alvefur <zash@zash.se>
parents: 9266
diff changeset
   877
		if iq_ns == 'vcard-temp' or iq_ns == "http://jabber.org/protocol/pubsub" or iq_ns == "urn:ietf:params:xml:ns:vcard-4.0" then
6096
84f9123637d4 plugins/muc/muc.lib: Refactor _to_occupant handlers
daurnimator <quae@daurnimator.com>
parents: 6095
diff changeset
   878
			stanza.attr.to = jid_bare(stanza.attr.to);
84f9123637d4 plugins/muc/muc.lib: Refactor _to_occupant handlers
daurnimator <quae@daurnimator.com>
parents: 6095
diff changeset
   879
		end
6179
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   880
		self:route_stanza(stanza);
6096
84f9123637d4 plugins/muc/muc.lib: Refactor _to_occupant handlers
daurnimator <quae@daurnimator.com>
parents: 6095
diff changeset
   881
		stanza.attr.from, stanza.attr.to, stanza.attr.id = from, to, id;
6094
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   882
		return true;
5061
186f34d88073 MUC: Fix private IQ and message routing.
Waqas Hussain <waqas20@gmail.com>
parents: 4999
diff changeset
   883
	end
186f34d88073 MUC: Fix private IQ and message routing.
Waqas Hussain <waqas20@gmail.com>
parents: 4999
diff changeset
   884
end
186f34d88073 MUC: Fix private IQ and message routing.
Waqas Hussain <waqas20@gmail.com>
parents: 4999
diff changeset
   885
6094
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   886
function room_mt:handle_message_to_occupant(origin, stanza)
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   887
	local from, to = stanza.attr.from, stanza.attr.to;
6119
c13f5d6b9b16 plugins/muc/muc.lib: Use `get_occupant_jid` method instead of indexing _jid_nick
daurnimator <quae@daurnimator.com>
parents: 6118
diff changeset
   888
	local current_nick = self:get_occupant_jid(from);
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   889
	local type = stanza.attr.type;
6094
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   890
	if not current_nick then -- not in room
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   891
		if type ~= "error" then
10453
2e36a54906e4 MUC: Indicate that the room is the origin of various errors where 'from' is an occupant JID
Kim Alvefur <zash@zash.se>
parents: 10438
diff changeset
   892
			origin.send(st.error_reply(stanza, "cancel", "not-acceptable", "You are not currently connected to this chat", self.jid));
1778
f4213d84ba8a MUC: Correct routing of vCard requests to bare JID.
Waqas Hussain <waqas20@gmail.com>
parents: 1769
diff changeset
   893
		end
6094
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   894
		return true;
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   895
	end
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   896
	if type == "groupchat" then -- groupchat messages not allowed in PM
10453
2e36a54906e4 MUC: Indicate that the room is the origin of various errors where 'from' is an occupant JID
Kim Alvefur <zash@zash.se>
parents: 10438
diff changeset
   897
		origin.send(st.error_reply(stanza, "modify", "bad-request", nil, self.jid));
6094
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   898
		return true;
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   899
	elseif type == "error" and is_kickable_error(stanza) then
1996
3e6b36c6d7b7 MUC: Kick occupants on sending error messages to other occupants.
Waqas Hussain <waqas20@gmail.com>
parents: 1989
diff changeset
   900
		log("debug", "%s kicked from %s for sending an error message", current_nick, self.jid);
6100
c78ba94d3261 plugins/muc/muc.lib: Move all kick code into one place
daurnimator <quae@daurnimator.com>
parents: 6099
diff changeset
   901
		return self:handle_kickable(origin, stanza); -- send unavailable
6096
84f9123637d4 plugins/muc/muc.lib: Refactor _to_occupant handlers
daurnimator <quae@daurnimator.com>
parents: 6095
diff changeset
   902
	end
84f9123637d4 plugins/muc/muc.lib: Refactor _to_occupant handlers
daurnimator <quae@daurnimator.com>
parents: 6095
diff changeset
   903
6179
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
   904
	local o_data = self:get_occupant_by_nick(to);
6096
84f9123637d4 plugins/muc/muc.lib: Refactor _to_occupant handlers
daurnimator <quae@daurnimator.com>
parents: 6095
diff changeset
   905
	if not o_data then
10453
2e36a54906e4 MUC: Indicate that the room is the origin of various errors where 'from' is an occupant JID
Kim Alvefur <zash@zash.se>
parents: 10438
diff changeset
   906
		origin.send(st.error_reply(stanza, "cancel", "item-not-found", "Recipient not in room", self.jid));
6096
84f9123637d4 plugins/muc/muc.lib: Refactor _to_occupant handlers
daurnimator <quae@daurnimator.com>
parents: 6095
diff changeset
   907
		return true;
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   908
	end
6096
84f9123637d4 plugins/muc/muc.lib: Refactor _to_occupant handlers
daurnimator <quae@daurnimator.com>
parents: 6095
diff changeset
   909
	log("debug", "%s sent private message stanza to %s (%s)", from, to, o_data.jid);
10297
f9301d93de72 MUC: Strip tags with MUC-related namespaces from private messages (fixes #1427)
Kim Alvefur <zash@zash.se>
parents: 10215
diff changeset
   910
	stanza = muc_util.filter_muc_x(st.clone(stanza));
6096
84f9123637d4 plugins/muc/muc.lib: Refactor _to_occupant handlers
daurnimator <quae@daurnimator.com>
parents: 6095
diff changeset
   911
	stanza:tag("x", { xmlns = "http://jabber.org/protocol/muc#user" }):up();
84f9123637d4 plugins/muc/muc.lib: Refactor _to_occupant handlers
daurnimator <quae@daurnimator.com>
parents: 6095
diff changeset
   912
	stanza.attr.from = current_nick;
10666
46373b97e648 mod_muc: add muc-private-message event
Maxime “pep” Buquet <pep@bouah.net>
parents: 10557
diff changeset
   913
	if module:fire_event("muc-private-message", { room = self, origin = origin, stanza = stanza }) ~= false then
46373b97e648 mod_muc: add muc-private-message event
Maxime “pep” Buquet <pep@bouah.net>
parents: 10557
diff changeset
   914
		self:route_to_occupant(o_data, stanza)
46373b97e648 mod_muc: add muc-private-message event
Maxime “pep” Buquet <pep@bouah.net>
parents: 10557
diff changeset
   915
	end
6180
35388114439f plugins/muc/muc.lib: non-function changes (reordering, semicolons and comments)
daurnimator <quae@daurnimator.com>
parents: 6179
diff changeset
   916
	-- TODO: Remove x tag?
6130
c95d9132592a plugins/muc/muc.lib: Add route_to_occupant function to send a stanza to all occupant sessions
daurnimator <quae@daurnimator.com>
parents: 6129
diff changeset
   917
	stanza.attr.from = from;
6096
84f9123637d4 plugins/muc/muc.lib: Refactor _to_occupant handlers
daurnimator <quae@daurnimator.com>
parents: 6095
diff changeset
   918
	return true;
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   919
end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   920
2216
dbbb5ed41365 MUC: Slightly refactored form processing.
Waqas Hussain <waqas20@gmail.com>
parents: 2174
diff changeset
   921
function room_mt:send_form(origin, stanza)
3591
dff4a77ee285 MUC: Parse submitted form with util.dataforms
Kim Alvefur <zash@zash.se>
parents: 3590
diff changeset
   922
	origin.send(st.reply(stanza):query("http://jabber.org/protocol/muc#owner")
5601
f55ab5fa939f mod_muc: Pass actor (requesting JID) when generating the config form, and to the muc-config-form event handler
Matthew Wild <mwild1@gmail.com>
parents: 5600
diff changeset
   923
		:add_child(self:get_form_layout(stanza.attr.from):form())
3591
dff4a77ee285 MUC: Parse submitted form with util.dataforms
Kim Alvefur <zash@zash.se>
parents: 3590
diff changeset
   924
	);
dff4a77ee285 MUC: Parse submitted form with util.dataforms
Kim Alvefur <zash@zash.se>
parents: 3590
diff changeset
   925
end
dff4a77ee285 MUC: Parse submitted form with util.dataforms
Kim Alvefur <zash@zash.se>
parents: 3590
diff changeset
   926
5601
f55ab5fa939f mod_muc: Pass actor (requesting JID) when generating the config form, and to the muc-config-form event handler
Matthew Wild <mwild1@gmail.com>
parents: 5600
diff changeset
   927
function room_mt:get_form_layout(actor)
5541
1997671d5e46 MUC: Allow plugins to add and handle options in the MUC config form
Matthew Wild <mwild1@gmail.com>
parents: 5519
diff changeset
   928
	local form = dataform.new({
1997671d5e46 MUC: Allow plugins to add and handle options in the MUC config form
Matthew Wild <mwild1@gmail.com>
parents: 5519
diff changeset
   929
		title = "Configuration for "..self.jid,
1997671d5e46 MUC: Allow plugins to add and handle options in the MUC config form
Matthew Wild <mwild1@gmail.com>
parents: 5519
diff changeset
   930
		instructions = "Complete and submit this form to configure the room.",
3517
530f7de1d265 MUC: Use util.dataforms to generate forms
Kim Alvefur <zash@zash.se>
parents: 3516
diff changeset
   931
		{
530f7de1d265 MUC: Use util.dataforms to generate forms
Kim Alvefur <zash@zash.se>
parents: 3516
diff changeset
   932
			name = 'FORM_TYPE',
530f7de1d265 MUC: Use util.dataforms to generate forms
Kim Alvefur <zash@zash.se>
parents: 3516
diff changeset
   933
			type = 'hidden',
530f7de1d265 MUC: Use util.dataforms to generate forms
Kim Alvefur <zash@zash.se>
parents: 3516
diff changeset
   934
			value = 'http://jabber.org/protocol/muc#roomconfig'
530f7de1d265 MUC: Use util.dataforms to generate forms
Kim Alvefur <zash@zash.se>
parents: 3516
diff changeset
   935
		}
3591
dff4a77ee285 MUC: Parse submitted form with util.dataforms
Kim Alvefur <zash@zash.se>
parents: 3590
diff changeset
   936
	});
5601
f55ab5fa939f mod_muc: Pass actor (requesting JID) when generating the config form, and to the muc-config-form event handler
Matthew Wild <mwild1@gmail.com>
parents: 5600
diff changeset
   937
	return module:fire_event("muc-config-form", { room = self, actor = actor, form = form }) or form;
2216
dbbb5ed41365 MUC: Slightly refactored form processing.
Waqas Hussain <waqas20@gmail.com>
parents: 2174
diff changeset
   938
end
dbbb5ed41365 MUC: Slightly refactored form processing.
Waqas Hussain <waqas20@gmail.com>
parents: 2174
diff changeset
   939
dbbb5ed41365 MUC: Slightly refactored form processing.
Waqas Hussain <waqas20@gmail.com>
parents: 2174
diff changeset
   940
function room_mt:process_form(origin, stanza)
6202
64ed7f538f81 plugins/muc/muc.lib: Refactor out process_form into hooks
daurnimator <quae@daurnimator.com>
parents: 6201
diff changeset
   941
	local form = stanza.tags[1]:get_child("x", "jabber:x:data");
64ed7f538f81 plugins/muc/muc.lib: Refactor out process_form into hooks
daurnimator <quae@daurnimator.com>
parents: 6201
diff changeset
   942
	if form.attr.type == "cancel" then
7386
69827ee1f951 MUC: Accept missing form as "instant room" request (fixes #377)
Kim Alvefur <zash@zash.se>
parents: 7014
diff changeset
   943
		origin.send(st.reply(stanza));
6202
64ed7f538f81 plugins/muc/muc.lib: Refactor out process_form into hooks
daurnimator <quae@daurnimator.com>
parents: 6201
diff changeset
   944
	elseif form.attr.type == "submit" then
10557
4d8119ffd433 MUC: Make note to handle configuration form errors [luacheck]
Kim Alvefur <zash@zash.se>
parents: 10554
diff changeset
   945
		-- luacheck: ignore 231/errors
6993
f476e2497568 MUC: Fire per-field events for the config form, including those with value == nil (no <value/> element in form)
Matthew Wild <mwild1@gmail.com>
parents: 6991
diff changeset
   946
		local fields, errors, present;
6391
4d334d00c635 plugins/muc/muc.lib: Add instant room support
daurnimator <quae@daurnimator.com>
parents: 6318
diff changeset
   947
		if form.tags[1] == nil then -- Instant room
6993
f476e2497568 MUC: Fire per-field events for the config form, including those with value == nil (no <value/> element in form)
Matthew Wild <mwild1@gmail.com>
parents: 6991
diff changeset
   948
			fields, present = {}, {};
6391
4d334d00c635 plugins/muc/muc.lib: Add instant room support
daurnimator <quae@daurnimator.com>
parents: 6318
diff changeset
   949
		else
10557
4d8119ffd433 MUC: Make note to handle configuration form errors [luacheck]
Kim Alvefur <zash@zash.se>
parents: 10554
diff changeset
   950
			-- FIXME handle form errors
6993
f476e2497568 MUC: Fire per-field events for the config form, including those with value == nil (no <value/> element in form)
Matthew Wild <mwild1@gmail.com>
parents: 6991
diff changeset
   951
			fields, errors, present = self:get_form_layout(stanza.attr.from):data(form);
6391
4d334d00c635 plugins/muc/muc.lib: Add instant room support
daurnimator <quae@daurnimator.com>
parents: 6318
diff changeset
   952
			if fields.FORM_TYPE ~= "http://jabber.org/protocol/muc#roomconfig" then
4d334d00c635 plugins/muc/muc.lib: Add instant room support
daurnimator <quae@daurnimator.com>
parents: 6318
diff changeset
   953
				origin.send(st.error_reply(stanza, "cancel", "bad-request", "Form is not of type room configuration"));
4d334d00c635 plugins/muc/muc.lib: Add instant room support
daurnimator <quae@daurnimator.com>
parents: 6318
diff changeset
   954
				return true;
4d334d00c635 plugins/muc/muc.lib: Add instant room support
daurnimator <quae@daurnimator.com>
parents: 6318
diff changeset
   955
			end
6202
64ed7f538f81 plugins/muc/muc.lib: Refactor out process_form into hooks
daurnimator <quae@daurnimator.com>
parents: 6201
diff changeset
   956
		end
3508
9e4c2b048f9a MUC: Added a 'Description' property (muc#roomconfig_roomdesc)
Kim Alvefur <zash@zash.se>
parents: 3507
diff changeset
   957
9055
5017e43ccc39 MUC: Add 'actor' field in muc-config-submitted event
Matthew Wild <mwild1@gmail.com>
parents: 9030
diff changeset
   958
		local event = {
5017e43ccc39 MUC: Add 'actor' field in muc-config-submitted event
Matthew Wild <mwild1@gmail.com>
parents: 9030
diff changeset
   959
			room = self;
5017e43ccc39 MUC: Add 'actor' field in muc-config-submitted event
Matthew Wild <mwild1@gmail.com>
parents: 9030
diff changeset
   960
			origin = origin;
5017e43ccc39 MUC: Add 'actor' field in muc-config-submitted event
Matthew Wild <mwild1@gmail.com>
parents: 9030
diff changeset
   961
			stanza = stanza;
5017e43ccc39 MUC: Add 'actor' field in muc-config-submitted event
Matthew Wild <mwild1@gmail.com>
parents: 9030
diff changeset
   962
			fields = fields;
5017e43ccc39 MUC: Add 'actor' field in muc-config-submitted event
Matthew Wild <mwild1@gmail.com>
parents: 9030
diff changeset
   963
			status_codes = {};
5017e43ccc39 MUC: Add 'actor' field in muc-config-submitted event
Matthew Wild <mwild1@gmail.com>
parents: 9030
diff changeset
   964
			actor = stanza.attr.from;
5017e43ccc39 MUC: Add 'actor' field in muc-config-submitted event
Matthew Wild <mwild1@gmail.com>
parents: 9030
diff changeset
   965
		};
6203
b6ffce01e6cf plugins/muc/muc.lib: Modify muc-config-submitted to keep a list of status codes instead of fields changed
daurnimator <quae@daurnimator.com>
parents: 6202
diff changeset
   966
		function event.update_option(name, field, allowed)
6202
64ed7f538f81 plugins/muc/muc.lib: Refactor out process_form into hooks
daurnimator <quae@daurnimator.com>
parents: 6201
diff changeset
   967
			local new = fields[field];
64ed7f538f81 plugins/muc/muc.lib: Refactor out process_form into hooks
daurnimator <quae@daurnimator.com>
parents: 6201
diff changeset
   968
			if new == nil then return; end
64ed7f538f81 plugins/muc/muc.lib: Refactor out process_form into hooks
daurnimator <quae@daurnimator.com>
parents: 6201
diff changeset
   969
			if allowed and not allowed[new] then return; end
64ed7f538f81 plugins/muc/muc.lib: Refactor out process_form into hooks
daurnimator <quae@daurnimator.com>
parents: 6201
diff changeset
   970
			if new == self["get_"..name](self) then return; end
6203
b6ffce01e6cf plugins/muc/muc.lib: Modify muc-config-submitted to keep a list of status codes instead of fields changed
daurnimator <quae@daurnimator.com>
parents: 6202
diff changeset
   971
			event.status_codes["104"] = true;
6202
64ed7f538f81 plugins/muc/muc.lib: Refactor out process_form into hooks
daurnimator <quae@daurnimator.com>
parents: 6201
diff changeset
   972
			self["set_"..name](self, new);
6203
b6ffce01e6cf plugins/muc/muc.lib: Modify muc-config-submitted to keep a list of status codes instead of fields changed
daurnimator <quae@daurnimator.com>
parents: 6202
diff changeset
   973
			return true;
6202
64ed7f538f81 plugins/muc/muc.lib: Refactor out process_form into hooks
daurnimator <quae@daurnimator.com>
parents: 6201
diff changeset
   974
		end
64ed7f538f81 plugins/muc/muc.lib: Refactor out process_form into hooks
daurnimator <quae@daurnimator.com>
parents: 6201
diff changeset
   975
		module:fire_event("muc-config-submitted", event);
6993
f476e2497568 MUC: Fire per-field events for the config form, including those with value == nil (no <value/> element in form)
Matthew Wild <mwild1@gmail.com>
parents: 6991
diff changeset
   976
		for submitted_field in pairs(present) do
f476e2497568 MUC: Fire per-field events for the config form, including those with value == nil (no <value/> element in form)
Matthew Wild <mwild1@gmail.com>
parents: 6991
diff changeset
   977
			event.field, event.value = submitted_field, fields[submitted_field];
f476e2497568 MUC: Fire per-field events for the config form, including those with value == nil (no <value/> element in form)
Matthew Wild <mwild1@gmail.com>
parents: 6991
diff changeset
   978
			module:fire_event("muc-config-submitted/"..submitted_field, event);
f476e2497568 MUC: Fire per-field events for the config form, including those with value == nil (no <value/> element in form)
Matthew Wild <mwild1@gmail.com>
parents: 6991
diff changeset
   979
		end
f476e2497568 MUC: Fire per-field events for the config form, including those with value == nil (no <value/> element in form)
Matthew Wild <mwild1@gmail.com>
parents: 6991
diff changeset
   980
		event.field, event.value = nil, nil;
4119
813adb81d7da MUC: Add option to allow participants to change the subject.
Kim Alvefur <zash@zash.se>
parents: 3989
diff changeset
   981
7417
1b62c89014c4 MUC: Separate force-save parameter from save-entire-state flag
Kim Alvefur <zash@zash.se>
parents: 7416
diff changeset
   982
		self:save(true);
6202
64ed7f538f81 plugins/muc/muc.lib: Refactor out process_form into hooks
daurnimator <quae@daurnimator.com>
parents: 6201
diff changeset
   983
		origin.send(st.reply(stanza));
3248
f8d14ea3ad0e MUC: Added a password field to the room config dialog.
Waqas Hussain <waqas20@gmail.com>
parents: 3247
diff changeset
   984
6203
b6ffce01e6cf plugins/muc/muc.lib: Modify muc-config-submitted to keep a list of status codes instead of fields changed
daurnimator <quae@daurnimator.com>
parents: 6202
diff changeset
   985
		if next(event.status_codes) then
6202
64ed7f538f81 plugins/muc/muc.lib: Refactor out process_form into hooks
daurnimator <quae@daurnimator.com>
parents: 6201
diff changeset
   986
			local msg = st.message({type='groupchat', from=self.jid})
64ed7f538f81 plugins/muc/muc.lib: Refactor out process_form into hooks
daurnimator <quae@daurnimator.com>
parents: 6201
diff changeset
   987
				:tag('x', {xmlns='http://jabber.org/protocol/muc#user'})
6203
b6ffce01e6cf plugins/muc/muc.lib: Modify muc-config-submitted to keep a list of status codes instead of fields changed
daurnimator <quae@daurnimator.com>
parents: 6202
diff changeset
   988
			for code in pairs(event.status_codes) do
b6ffce01e6cf plugins/muc/muc.lib: Modify muc-config-submitted to keep a list of status codes instead of fields changed
daurnimator <quae@daurnimator.com>
parents: 6202
diff changeset
   989
				msg:tag("status", {code = code;}):up();
6202
64ed7f538f81 plugins/muc/muc.lib: Refactor out process_form into hooks
daurnimator <quae@daurnimator.com>
parents: 6201
diff changeset
   990
			end
6203
b6ffce01e6cf plugins/muc/muc.lib: Modify muc-config-submitted to keep a list of status codes instead of fields changed
daurnimator <quae@daurnimator.com>
parents: 6202
diff changeset
   991
			msg:up();
6215
1dd09dc04945 plugins/muc: Move history to an external module
daurnimator <quae@daurnimator.com>
parents: 6214
diff changeset
   992
			self:broadcast_message(msg);
6202
64ed7f538f81 plugins/muc/muc.lib: Refactor out process_form into hooks
daurnimator <quae@daurnimator.com>
parents: 6201
diff changeset
   993
		end
64ed7f538f81 plugins/muc/muc.lib: Refactor out process_form into hooks
daurnimator <quae@daurnimator.com>
parents: 6201
diff changeset
   994
	else
64ed7f538f81 plugins/muc/muc.lib: Refactor out process_form into hooks
daurnimator <quae@daurnimator.com>
parents: 6201
diff changeset
   995
		origin.send(st.error_reply(stanza, "cancel", "bad-request", "Not a submitted form"));
5808
026367992a0f mod_muc: Support for locking newly-created rooms until they are configured (enabled with muc_room_locking = true)
Matthew Wild <mwild1@gmail.com>
parents: 5776
diff changeset
   996
	end
6202
64ed7f538f81 plugins/muc/muc.lib: Refactor out process_form into hooks
daurnimator <quae@daurnimator.com>
parents: 6201
diff changeset
   997
	return true;
1754
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1753
diff changeset
   998
end
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1753
diff changeset
   999
6179
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
  1000
-- Removes everyone from the room
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
  1001
function room_mt:clear(x)
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
  1002
	x = x or st.stanza("x", {xmlns='http://jabber.org/protocol/muc#user'});
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
  1003
	local occupants_updated = {};
7092
890f4b2cc444 MUC: Add luacheck annotations, remove unused vars, rename conflicting vars etc
Kim Alvefur <zash@zash.se>
parents: 7016
diff changeset
  1004
	for nick, occupant in self:each_occupant() do -- luacheck: ignore 213
10690
ac3ec4f2b124 MUC: Pass previous role to :publicise_occupant_status() when destroying a MUC
Matthew Wild <mwild1@gmail.com>
parents: 10688
diff changeset
  1005
		local prev_role = occupant.role;
6179
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
  1006
		occupant.role = nil;
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
  1007
		self:save_occupant(occupant);
10690
ac3ec4f2b124 MUC: Pass previous role to :publicise_occupant_status() when destroying a MUC
Matthew Wild <mwild1@gmail.com>
parents: 10688
diff changeset
  1008
		occupants_updated[occupant] = prev_role;
6179
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
  1009
	end
10690
ac3ec4f2b124 MUC: Pass previous role to :publicise_occupant_status() when destroying a MUC
Matthew Wild <mwild1@gmail.com>
parents: 10688
diff changeset
  1010
	for occupant, prev_role in pairs(occupants_updated) do
ac3ec4f2b124 MUC: Pass previous role to :publicise_occupant_status() when destroying a MUC
Matthew Wild <mwild1@gmail.com>
parents: 10688
diff changeset
  1011
		self:publicise_occupant_status(occupant, x, nil, nil, nil, prev_role);
10052
e5532fbdd163 MUC: Reflow event tables to improve readability
Kim Alvefur <zash@zash.se>
parents: 9866
diff changeset
  1012
		module:fire_event("muc-occupant-left", {
e5532fbdd163 MUC: Reflow event tables to improve readability
Kim Alvefur <zash@zash.se>
parents: 9866
diff changeset
  1013
				room = self;
e5532fbdd163 MUC: Reflow event tables to improve readability
Kim Alvefur <zash@zash.se>
parents: 9866
diff changeset
  1014
				nick = occupant.nick;
e5532fbdd163 MUC: Reflow event tables to improve readability
Kim Alvefur <zash@zash.se>
parents: 9866
diff changeset
  1015
				occupant = occupant;
e5532fbdd163 MUC: Reflow event tables to improve readability
Kim Alvefur <zash@zash.se>
parents: 9866
diff changeset
  1016
			});
2412
e243b7c81de6 Added notification of configuration changes for MUCs
Rob Hoelz <rob@hoelzro.net>
parents: 2411
diff changeset
  1017
	end
1754
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1753
diff changeset
  1018
end
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1753
diff changeset
  1019
2217
838f6d546177 MUC: Added support for the room-destroy owner use case.
Waqas Hussain <waqas20@gmail.com>
parents: 2216
diff changeset
  1020
function room_mt:destroy(newjid, reason, password)
6179
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
  1021
	local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user"})
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
  1022
		:tag("destroy", {jid=newjid});
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
  1023
	if reason then x:tag("reason"):text(reason):up(); end
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
  1024
	if password then x:tag("password"):text(password):up(); end
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
  1025
	x:up();
9010
695904638cfa MUC: Flag rooms being destroyed (fixes #887)
Kim Alvefur <zash@zash.se>
parents: 9006
diff changeset
  1026
	self.destroying = reason or true;
6179
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
  1027
	self:clear(x);
9014
ce8e5206aeba MUC: Include destruction reason and other info in destroyed event
Kim Alvefur <zash@zash.se>
parents: 9010
diff changeset
  1028
	module:fire_event("muc-room-destroyed", { room = self, reason = reason, newjid = newjid, password = password });
7687
65ba769d9f05 MUC: Return 'true' from room:destroy() so that use from the telnet console says 'OK'
Kim Alvefur <zash@zash.se>
parents: 7446
diff changeset
  1029
	return true;
1754
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1753
diff changeset
  1030
end
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1753
diff changeset
  1031
6101
a861dc18e08d plugins/muc/muc.lib: Add disco iq handlers with compatible argument signature
daurnimator <quae@daurnimator.com>
parents: 6100
diff changeset
  1032
function room_mt:handle_disco_info_get_query(origin, stanza)
a861dc18e08d plugins/muc/muc.lib: Add disco iq handlers with compatible argument signature
daurnimator <quae@daurnimator.com>
parents: 6100
diff changeset
  1033
	origin.send(self:get_disco_info(stanza));
a861dc18e08d plugins/muc/muc.lib: Add disco iq handlers with compatible argument signature
daurnimator <quae@daurnimator.com>
parents: 6100
diff changeset
  1034
	return true;
a861dc18e08d plugins/muc/muc.lib: Add disco iq handlers with compatible argument signature
daurnimator <quae@daurnimator.com>
parents: 6100
diff changeset
  1035
end
8794
8da11142fabf muc: Allow clients to change multiple affiliations or roles at once (#345)
Lennart Sauerbeck <devel@lennart.sauerbeck.org>
parents: 8593
diff changeset
  1036
6101
a861dc18e08d plugins/muc/muc.lib: Add disco iq handlers with compatible argument signature
daurnimator <quae@daurnimator.com>
parents: 6100
diff changeset
  1037
function room_mt:handle_disco_items_get_query(origin, stanza)
a861dc18e08d plugins/muc/muc.lib: Add disco iq handlers with compatible argument signature
daurnimator <quae@daurnimator.com>
parents: 6100
diff changeset
  1038
	origin.send(self:get_disco_items(stanza));
a861dc18e08d plugins/muc/muc.lib: Add disco iq handlers with compatible argument signature
daurnimator <quae@daurnimator.com>
parents: 6100
diff changeset
  1039
	return true;
a861dc18e08d plugins/muc/muc.lib: Add disco iq handlers with compatible argument signature
daurnimator <quae@daurnimator.com>
parents: 6100
diff changeset
  1040
end
8795
c2b99fa134b3 MUC: Import revised, more comprehensive patch for 8da11142fabf (#345)
Matthew Wild <mwild1@gmail.com>
parents: 8794
diff changeset
  1041
6141
bf6de8ef66c2 plugins/muc: Rename admin query hook
daurnimator <quae@daurnimator.com>
parents: 6140
diff changeset
  1042
function room_mt:handle_admin_query_set_command(origin, stanza)
6095
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
  1043
	local item = stanza.tags[1].tags[1];
6912
494938dec5d8 MUC: Reject muc:admin query with missing <item> child
Kim Alvefur <zash@zash.se>
parents: 6838
diff changeset
  1044
	if not item then
494938dec5d8 MUC: Reject muc:admin query with missing <item> child
Kim Alvefur <zash@zash.se>
parents: 6838
diff changeset
  1045
		origin.send(st.error_reply(stanza, "cancel", "bad-request"));
9644
28d4b9d5a432 MUC: Fix traceback on muc#admin query with missing <item> child (#1242)
Kim Alvefur <zash@zash.se>
parents: 9617
diff changeset
  1046
		return true;
6912
494938dec5d8 MUC: Reject muc:admin query with missing <item> child
Kim Alvefur <zash@zash.se>
parents: 6838
diff changeset
  1047
	end
6095
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
  1048
	if item.attr.jid then -- Validate provided JID
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
  1049
		item.attr.jid = jid_prep(item.attr.jid);
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
  1050
		if not item.attr.jid then
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
  1051
			origin.send(st.error_reply(stanza, "modify", "jid-malformed"));
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
  1052
			return true;
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
  1053
		end
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
  1054
	end
9266
4285d1efaf03 MUC: Allow admins to include a registered nick when setting affiliation
Matthew Wild <mwild1@gmail.com>
parents: 9242
diff changeset
  1055
	if item.attr.nick then -- Validate provided nick
4285d1efaf03 MUC: Allow admins to include a registered nick when setting affiliation
Matthew Wild <mwild1@gmail.com>
parents: 9242
diff changeset
  1056
		item.attr.nick = resourceprep(item.attr.nick);
4285d1efaf03 MUC: Allow admins to include a registered nick when setting affiliation
Matthew Wild <mwild1@gmail.com>
parents: 9242
diff changeset
  1057
		if not item.attr.nick then
4285d1efaf03 MUC: Allow admins to include a registered nick when setting affiliation
Matthew Wild <mwild1@gmail.com>
parents: 9242
diff changeset
  1058
			origin.send(st.error_reply(stanza, "modify", "jid-malformed", "invalid nickname"));
4285d1efaf03 MUC: Allow admins to include a registered nick when setting affiliation
Matthew Wild <mwild1@gmail.com>
parents: 9242
diff changeset
  1059
			return true;
4285d1efaf03 MUC: Allow admins to include a registered nick when setting affiliation
Matthew Wild <mwild1@gmail.com>
parents: 9242
diff changeset
  1060
		end
4285d1efaf03 MUC: Allow admins to include a registered nick when setting affiliation
Matthew Wild <mwild1@gmail.com>
parents: 9242
diff changeset
  1061
	end
9084
ce57c69a20e2 MUC: Split long lines [luacheck strict]
Kim Alvefur <zash@zash.se>
parents: 9060
diff changeset
  1062
	if not item.attr.jid and item.attr.nick then
ce57c69a20e2 MUC: Split long lines [luacheck strict]
Kim Alvefur <zash@zash.se>
parents: 9060
diff changeset
  1063
		-- COMPAT Workaround for Miranda sending 'nick' instead of 'jid' when changing affiliation
6179
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
  1064
		local occupant = self:get_occupant_by_nick(self.jid.."/"..item.attr.nick);
9533
3bc5c22e2ca4 MUC: Use the bare JID when performing a lookup for COMPAT with clients that don't set it (fixes #1224)
Matthew Wild <mwild1@gmail.com>
parents: 9447
diff changeset
  1065
		if occupant then item.attr.jid = occupant.bare_jid; end
9266
4285d1efaf03 MUC: Allow admins to include a registered nick when setting affiliation
Matthew Wild <mwild1@gmail.com>
parents: 9242
diff changeset
  1066
	elseif item.attr.role and not item.attr.nick and item.attr.jid then
4285d1efaf03 MUC: Allow admins to include a registered nick when setting affiliation
Matthew Wild <mwild1@gmail.com>
parents: 9242
diff changeset
  1067
		-- Role changes should use nick, but we have a JID so pull the nick from that
6119
c13f5d6b9b16 plugins/muc/muc.lib: Use `get_occupant_jid` method instead of indexing _jid_nick
daurnimator <quae@daurnimator.com>
parents: 6118
diff changeset
  1068
		local nick = self:get_occupant_jid(item.attr.jid);
8859
538bb632a2b6 MUC: Use util.jid.resource
Kim Alvefur <zash@zash.se>
parents: 8856
diff changeset
  1069
		if nick then item.attr.nick = jid_resource(nick); end
6095
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
  1070
	end
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
  1071
	local actor = stanza.attr.from;
6112
819e00a86239 plugins/muc/muc.lib: Use more modern stanza methods
daurnimator <quae@daurnimator.com>
parents: 6111
diff changeset
  1072
	local reason = item:get_child_text("reason");
6181
6baa9a59aa03 plugins/muc/muc.lib: Remove callback parameter from set_role and set_affiliation
daurnimator <quae@daurnimator.com>
parents: 6180
diff changeset
  1073
	local success, errtype, err
6095
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
  1074
	if item.attr.affiliation and item.attr.jid and not item.attr.role then
9266
4285d1efaf03 MUC: Allow admins to include a registered nick when setting affiliation
Matthew Wild <mwild1@gmail.com>
parents: 9242
diff changeset
  1075
		local registration_data;
4285d1efaf03 MUC: Allow admins to include a registered nick when setting affiliation
Matthew Wild <mwild1@gmail.com>
parents: 9242
diff changeset
  1076
		if item.attr.nick then
4285d1efaf03 MUC: Allow admins to include a registered nick when setting affiliation
Matthew Wild <mwild1@gmail.com>
parents: 9242
diff changeset
  1077
			local room_nick = self.jid.."/"..item.attr.nick;
9318
a47bba3b35f3 MUC: Don't kick user of a reserved nick if it's theirs (thanks pep.)
Matthew Wild <mwild1@gmail.com>
parents: 9315
diff changeset
  1078
			local existing_occupant = self:get_occupant_by_nick(room_nick);
a47bba3b35f3 MUC: Don't kick user of a reserved nick if it's theirs (thanks pep.)
Matthew Wild <mwild1@gmail.com>
parents: 9315
diff changeset
  1079
			if existing_occupant and existing_occupant.bare_jid ~= item.attr.jid then
a47bba3b35f3 MUC: Don't kick user of a reserved nick if it's theirs (thanks pep.)
Matthew Wild <mwild1@gmail.com>
parents: 9315
diff changeset
  1080
				module:log("debug", "Existing occupant for %s: %s does not match %s", room_nick, existing_occupant.bare_jid, item.attr.jid);
9266
4285d1efaf03 MUC: Allow admins to include a registered nick when setting affiliation
Matthew Wild <mwild1@gmail.com>
parents: 9242
diff changeset
  1081
				self:set_role(true, room_nick, nil, "This nickname is reserved");
4285d1efaf03 MUC: Allow admins to include a registered nick when setting affiliation
Matthew Wild <mwild1@gmail.com>
parents: 9242
diff changeset
  1082
			end
9318
a47bba3b35f3 MUC: Don't kick user of a reserved nick if it's theirs (thanks pep.)
Matthew Wild <mwild1@gmail.com>
parents: 9315
diff changeset
  1083
			module:log("debug", "Reserving %s for %s (%s)", item.attr.nick, item.attr.jid, item.attr.affiliation);
9266
4285d1efaf03 MUC: Allow admins to include a registered nick when setting affiliation
Matthew Wild <mwild1@gmail.com>
parents: 9242
diff changeset
  1084
			registration_data = { reserved_nickname = item.attr.nick };
4285d1efaf03 MUC: Allow admins to include a registered nick when setting affiliation
Matthew Wild <mwild1@gmail.com>
parents: 9242
diff changeset
  1085
		end
4285d1efaf03 MUC: Allow admins to include a registered nick when setting affiliation
Matthew Wild <mwild1@gmail.com>
parents: 9242
diff changeset
  1086
		success, errtype, err = self:set_affiliation(actor, item.attr.jid, item.attr.affiliation, reason, registration_data);
6095
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
  1087
	elseif item.attr.role and item.attr.nick and not item.attr.affiliation then
6181
6baa9a59aa03 plugins/muc/muc.lib: Remove callback parameter from set_role and set_affiliation
daurnimator <quae@daurnimator.com>
parents: 6180
diff changeset
  1088
		success, errtype, err = self:set_role(actor, self.jid.."/"..item.attr.nick, item.attr.role, reason);
6095
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
  1089
	else
6181
6baa9a59aa03 plugins/muc/muc.lib: Remove callback parameter from set_role and set_affiliation
daurnimator <quae@daurnimator.com>
parents: 6180
diff changeset
  1090
		success, errtype, err = nil, "cancel", "bad-request";
6095
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
  1091
	end
7417
1b62c89014c4 MUC: Separate force-save parameter from save-entire-state flag
Kim Alvefur <zash@zash.se>
parents: 7416
diff changeset
  1092
	self:save(true);
6831
9019bc4c9a5a MUC: Prevent double replies when MUC affiliation/role change requests fail.
Lance Stout <lancestout@gmail.com>
parents: 6830
diff changeset
  1093
	if not success then
9019bc4c9a5a MUC: Prevent double replies when MUC affiliation/role change requests fail.
Lance Stout <lancestout@gmail.com>
parents: 6830
diff changeset
  1094
		origin.send(st.error_reply(stanza, errtype, err));
9019bc4c9a5a MUC: Prevent double replies when MUC affiliation/role change requests fail.
Lance Stout <lancestout@gmail.com>
parents: 6830
diff changeset
  1095
	else
9019bc4c9a5a MUC: Prevent double replies when MUC affiliation/role change requests fail.
Lance Stout <lancestout@gmail.com>
parents: 6830
diff changeset
  1096
		origin.send(st.reply(stanza));
9019bc4c9a5a MUC: Prevent double replies when MUC affiliation/role change requests fail.
Lance Stout <lancestout@gmail.com>
parents: 6830
diff changeset
  1097
	end
6181
6baa9a59aa03 plugins/muc/muc.lib: Remove callback parameter from set_role and set_affiliation
daurnimator <quae@daurnimator.com>
parents: 6180
diff changeset
  1098
	return true;
6095
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
  1099
end
8795
c2b99fa134b3 MUC: Import revised, more comprehensive patch for 8da11142fabf (#345)
Matthew Wild <mwild1@gmail.com>
parents: 8794
diff changeset
  1100
6141
bf6de8ef66c2 plugins/muc: Rename admin query hook
daurnimator <quae@daurnimator.com>
parents: 6140
diff changeset
  1101
function room_mt:handle_admin_query_get_command(origin, stanza)
6095
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
  1102
	local actor = stanza.attr.from;
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
  1103
	local affiliation = self:get_affiliation(actor);
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
  1104
	local item = stanza.tags[1].tags[1];
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
  1105
	local _aff = item.attr.affiliation;
6456
14b62ad88d8a plugins/muc/muc.lib: Validate affiliations and roles to admin query get
daurnimator <quae@daurnimator.com>
parents: 6455
diff changeset
  1106
	local _aff_rank = valid_affiliations[_aff or "none"];
6095
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
  1107
	local _rol = item.attr.role;
6456
14b62ad88d8a plugins/muc/muc.lib: Validate affiliations and roles to admin query get
daurnimator <quae@daurnimator.com>
parents: 6455
diff changeset
  1108
	if _aff and _aff_rank and not _rol then
9619
66c6edf56397 MUC: Fix spelling in comments
Kim Alvefur <zash@zash.se>
parents: 9617
diff changeset
  1109
		-- You need to be at least an admin, and be requesting info about your affiliation or lower
6456
14b62ad88d8a plugins/muc/muc.lib: Validate affiliations and roles to admin query get
daurnimator <quae@daurnimator.com>
parents: 6455
diff changeset
  1110
		-- e.g. an admin can't ask for a list of owners
6830
19c4532946b3 MUC: Fix traceback in request for MUC affiliation lists by non-affiliated (thanks Lance)
Kim Alvefur <zash@zash.se>
parents: 6804
diff changeset
  1111
		local affiliation_rank = valid_affiliations[affiliation or "none"];
9616
abf9bacf77d4 MUC: Clarify condition with parenthesis
Kim Alvefur <zash@zash.se>
parents: 9603
diff changeset
  1112
		if (affiliation_rank >= valid_affiliations.admin and affiliation_rank >= _aff_rank)
9617
4d7b925652d9 MUC: Allow anyone read access to all affiliation lists in non-anonymous rooms (fixes #1230)
Kim Alvefur <zash@zash.se>
parents: 9616
diff changeset
  1113
		or (self:get_whois() == "anyone") then
6095
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
  1114
			local reply = st.reply(stanza):query("http://jabber.org/protocol/muc#admin");
6481
413923bbd1a0 plugins/muc/muc.lib: Add :each_affiliation() iterator
daurnimator <quae@daurnimator.com>
parents: 6479
diff changeset
  1115
			for jid in self:each_affiliation(_aff or "none") do
9315
6cab07323274 MUC: Include 'nick' attribute in affiliation lists (thanks jc)
Matthew Wild <mwild1@gmail.com>
parents: 9302
diff changeset
  1116
				local nick = self:get_registered_nick(jid);
6cab07323274 MUC: Include 'nick' attribute in affiliation lists (thanks jc)
Matthew Wild <mwild1@gmail.com>
parents: 9302
diff changeset
  1117
				reply:tag("item", {affiliation = _aff, jid = jid, nick = nick }):up();
1742
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
  1118
			end
6455
e692ea8c09a0 plugins/muc/muc.lib: Add missing :up()
daurnimator <quae@daurnimator.com>
parents: 6454
diff changeset
  1119
			origin.send(reply:up());
6095
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
  1120
			return true;
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
  1121
		else
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
  1122
			origin.send(st.error_reply(stanza, "auth", "forbidden"));
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
  1123
			return true;
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
  1124
		end
6456
14b62ad88d8a plugins/muc/muc.lib: Validate affiliations and roles to admin query get
daurnimator <quae@daurnimator.com>
parents: 6455
diff changeset
  1125
	elseif _rol and valid_roles[_rol or "none"] and not _aff then
6126
122e0f26e8f6 plugins/muc/muc.lib: Use `get_role` in `handle_admin_item_get_command`. Removed a TODO that's already done
daurnimator <quae@daurnimator.com>
parents: 6125
diff changeset
  1126
		local role = self:get_role(self:get_occupant_jid(actor)) or self:get_default_role(affiliation);
6219
a90159cfa530 plugins/muc/muc.lib: Fix getting a list of occupants by role (it was sending presences instead of items inside an iq)
daurnimator <quae@daurnimator.com>
parents: 6218
diff changeset
  1127
		if valid_roles[role or "none"] >= valid_roles.moderator then
6095
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
  1128
			if _rol == "none" then _rol = nil; end
6219
a90159cfa530 plugins/muc/muc.lib: Fix getting a list of occupants by role (it was sending presences instead of items inside an iq)
daurnimator <quae@daurnimator.com>
parents: 6218
diff changeset
  1129
			local reply = st.reply(stanza):query("http://jabber.org/protocol/muc#admin");
a90159cfa530 plugins/muc/muc.lib: Fix getting a list of occupants by role (it was sending presences instead of items inside an iq)
daurnimator <quae@daurnimator.com>
parents: 6218
diff changeset
  1130
			-- TODO: whois check here? (though fully anonymous rooms are not supported)
a90159cfa530 plugins/muc/muc.lib: Fix getting a list of occupants by role (it was sending presences instead of items inside an iq)
daurnimator <quae@daurnimator.com>
parents: 6218
diff changeset
  1131
			for occupant_jid, occupant in self:each_occupant() do
a90159cfa530 plugins/muc/muc.lib: Fix getting a list of occupants by role (it was sending presences instead of items inside an iq)
daurnimator <quae@daurnimator.com>
parents: 6218
diff changeset
  1132
				if occupant.role == _rol then
8859
538bb632a2b6 MUC: Use util.jid.resource
Kim Alvefur <zash@zash.se>
parents: 8856
diff changeset
  1133
					local nick = jid_resource(occupant_jid);
6219
a90159cfa530 plugins/muc/muc.lib: Fix getting a list of occupants by role (it was sending presences instead of items inside an iq)
daurnimator <quae@daurnimator.com>
parents: 6218
diff changeset
  1134
					self:build_item_list(occupant, reply, false, nick);
2217
838f6d546177 MUC: Added support for the room-destroy owner use case.
Waqas Hussain <waqas20@gmail.com>
parents: 2216
diff changeset
  1135
				end
2216
dbbb5ed41365 MUC: Slightly refactored form processing.
Waqas Hussain <waqas20@gmail.com>
parents: 2174
diff changeset
  1136
			end
6219
a90159cfa530 plugins/muc/muc.lib: Fix getting a list of occupants by role (it was sending presences instead of items inside an iq)
daurnimator <quae@daurnimator.com>
parents: 6218
diff changeset
  1137
			origin.send(reply:up());
6095
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
  1138
			return true;
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
  1139
		else
6095
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
  1140
			origin.send(st.error_reply(stanza, "auth", "forbidden"));
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
  1141
			return true;
2005
478ba7e85862 MUC: Rewrote code for mediated invites to be more robust, and to support legacy clients.
Waqas Hussain <waqas20@gmail.com>
parents: 1999
diff changeset
  1142
		end
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
  1143
	else
6095
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
  1144
		origin.send(st.error_reply(stanza, "cancel", "bad-request"));
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
  1145
		return true;
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
  1146
	end
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
  1147
end
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
  1148
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
  1149
function room_mt:handle_owner_query_get_to_room(origin, stanza)
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
  1150
	if self:get_affiliation(stanza.attr.from) ~= "owner" then
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
  1151
		origin.send(st.error_reply(stanza, "auth", "forbidden", "Only owners can configure rooms"));
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
  1152
		return true;
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
  1153
	end
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
  1154
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
  1155
	self:send_form(origin, stanza);
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
  1156
	return true;
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
  1157
end
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
  1158
function room_mt:handle_owner_query_set_to_room(origin, stanza)
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
  1159
	if self:get_affiliation(stanza.attr.from) ~= "owner" then
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
  1160
		origin.send(st.error_reply(stanza, "auth", "forbidden", "Only owners can configure rooms"));
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
  1161
		return true;
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
  1162
	end
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
  1163
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
  1164
	local child = stanza.tags[1].tags[1];
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
  1165
	if not child then
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
  1166
		origin.send(st.error_reply(stanza, "modify", "bad-request"));
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
  1167
		return true;
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
  1168
	elseif child.name == "destroy" then
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
  1169
		local newjid = child.attr.jid;
6112
819e00a86239 plugins/muc/muc.lib: Use more modern stanza methods
daurnimator <quae@daurnimator.com>
parents: 6111
diff changeset
  1170
		local reason = child:get_child_text("reason");
819e00a86239 plugins/muc/muc.lib: Use more modern stanza methods
daurnimator <quae@daurnimator.com>
parents: 6111
diff changeset
  1171
		local password = child:get_child_text("password");
6095
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
  1172
		self:destroy(newjid, reason, password);
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
  1173
		origin.send(st.reply(stanza));
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
  1174
		return true;
6202
64ed7f538f81 plugins/muc/muc.lib: Refactor out process_form into hooks
daurnimator <quae@daurnimator.com>
parents: 6201
diff changeset
  1175
	elseif child.name == "x" and child.attr.xmlns == "jabber:x:data" then
64ed7f538f81 plugins/muc/muc.lib: Refactor out process_form into hooks
daurnimator <quae@daurnimator.com>
parents: 6201
diff changeset
  1176
		return self:process_form(origin, stanza);
6095
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
  1177
	else
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
  1178
		origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
6095
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
  1179
		return true;
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
  1180
	end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
  1181
end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
  1182
6093
9a7eaf0a35b6 plugins/muc/muc.lib: Split up `handle_to_room` into smaller handlers (thanks sysko)
daurnimator <quae@daurnimator.com>
parents: 6092
diff changeset
  1183
function room_mt:handle_groupchat_to_room(origin, stanza)
9a7eaf0a35b6 plugins/muc/muc.lib: Split up `handle_to_room` into smaller handlers (thanks sysko)
daurnimator <quae@daurnimator.com>
parents: 6092
diff changeset
  1184
	local from = stanza.attr.from;
6179
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
  1185
	local occupant = self:get_occupant_by_real_jid(from);
9765
8e83f90bf96b MUC: add ID to message if no ID is present
Jonas Wielicki <jonas@wielicki.name>
parents: 9723
diff changeset
  1186
	if not stanza.attr.id then
9770
f40b9649e929 MUC: Rename import to avoid name clash [luacheck]
Kim Alvefur <zash@zash.se>
parents: 9765
diff changeset
  1187
		stanza.attr.id = new_id()
9765
8e83f90bf96b MUC: add ID to message if no ID is present
Jonas Wielicki <jonas@wielicki.name>
parents: 9723
diff changeset
  1188
	end
11502
d61ec5e6ee16 MUC: Allow overriding occupant object from groupchat message event
Kim Alvefur <zash@zash.se>
parents: 11250
diff changeset
  1189
	local event_data = {room = self; origin = origin; stanza = stanza; from = from; occupant = occupant};
d61ec5e6ee16 MUC: Allow overriding occupant object from groupchat message event
Kim Alvefur <zash@zash.se>
parents: 11250
diff changeset
  1190
	if module:fire_event("muc-occupant-groupchat", event_data) then
d61ec5e6ee16 MUC: Allow overriding occupant object from groupchat message event
Kim Alvefur <zash@zash.se>
parents: 11250
diff changeset
  1191
		return true;
d61ec5e6ee16 MUC: Allow overriding occupant object from groupchat message event
Kim Alvefur <zash@zash.se>
parents: 11250
diff changeset
  1192
	end
d61ec5e6ee16 MUC: Allow overriding occupant object from groupchat message event
Kim Alvefur <zash@zash.se>
parents: 11250
diff changeset
  1193
	if event_data.occupant then
d61ec5e6ee16 MUC: Allow overriding occupant object from groupchat message event
Kim Alvefur <zash@zash.se>
parents: 11250
diff changeset
  1194
		stanza.attr.from = event_data.occupant.nick;
d61ec5e6ee16 MUC: Allow overriding occupant object from groupchat message event
Kim Alvefur <zash@zash.se>
parents: 11250
diff changeset
  1195
	else
d61ec5e6ee16 MUC: Allow overriding occupant object from groupchat message event
Kim Alvefur <zash@zash.se>
parents: 11250
diff changeset
  1196
		stanza.attr.from = self.jid;
d61ec5e6ee16 MUC: Allow overriding occupant object from groupchat message event
Kim Alvefur <zash@zash.se>
parents: 11250
diff changeset
  1197
	end
6223
2a7ce69844ca plugins/muc/muc.lib: Refactor subject logic; fix bug of mixed up subject/author
daurnimator <quae@daurnimator.com>
parents: 6222
diff changeset
  1198
	self:broadcast_message(stanza);
2a7ce69844ca plugins/muc/muc.lib: Refactor subject logic; fix bug of mixed up subject/author
daurnimator <quae@daurnimator.com>
parents: 6222
diff changeset
  1199
	stanza.attr.from = from;
2a7ce69844ca plugins/muc/muc.lib: Refactor subject logic; fix bug of mixed up subject/author
daurnimator <quae@daurnimator.com>
parents: 6222
diff changeset
  1200
	return true;
6093
9a7eaf0a35b6 plugins/muc/muc.lib: Split up `handle_to_room` into smaller handlers (thanks sysko)
daurnimator <quae@daurnimator.com>
parents: 6092
diff changeset
  1201
end
9a7eaf0a35b6 plugins/muc/muc.lib: Split up `handle_to_room` into smaller handlers (thanks sysko)
daurnimator <quae@daurnimator.com>
parents: 6092
diff changeset
  1202
6432
675aea867574 plugins/muc: Add muc-occupant-groupchat event
daurnimator <quae@daurnimator.com>
parents: 6422
diff changeset
  1203
-- Role check
675aea867574 plugins/muc: Add muc-occupant-groupchat event
daurnimator <quae@daurnimator.com>
parents: 6422
diff changeset
  1204
module:hook("muc-occupant-groupchat", function(event)
675aea867574 plugins/muc: Add muc-occupant-groupchat event
daurnimator <quae@daurnimator.com>
parents: 6422
diff changeset
  1205
	local role_rank = valid_roles[event.occupant and event.occupant.role or "none"];
675aea867574 plugins/muc: Add muc-occupant-groupchat event
daurnimator <quae@daurnimator.com>
parents: 6422
diff changeset
  1206
	if role_rank <= valid_roles.none then
9823
11671a2e07a9 MUC: Add error message to error bounces when not joined to room
Matthew Wild <mwild1@gmail.com>
parents: 9722
diff changeset
  1207
		event.origin.send(st.error_reply(event.stanza, "cancel", "not-acceptable", "You are not currently connected to this chat"));
6432
675aea867574 plugins/muc: Add muc-occupant-groupchat event
daurnimator <quae@daurnimator.com>
parents: 6422
diff changeset
  1208
		return true;
675aea867574 plugins/muc: Add muc-occupant-groupchat event
daurnimator <quae@daurnimator.com>
parents: 6422
diff changeset
  1209
	elseif role_rank <= valid_roles.visitor then
675aea867574 plugins/muc: Add muc-occupant-groupchat event
daurnimator <quae@daurnimator.com>
parents: 6422
diff changeset
  1210
		event.origin.send(st.error_reply(event.stanza, "auth", "forbidden"));
675aea867574 plugins/muc: Add muc-occupant-groupchat event
daurnimator <quae@daurnimator.com>
parents: 6422
diff changeset
  1211
		return true;
675aea867574 plugins/muc: Add muc-occupant-groupchat event
daurnimator <quae@daurnimator.com>
parents: 6422
diff changeset
  1212
	end
675aea867574 plugins/muc: Add muc-occupant-groupchat event
daurnimator <quae@daurnimator.com>
parents: 6422
diff changeset
  1213
end, 50);
675aea867574 plugins/muc: Add muc-occupant-groupchat event
daurnimator <quae@daurnimator.com>
parents: 6422
diff changeset
  1214
6093
9a7eaf0a35b6 plugins/muc/muc.lib: Split up `handle_to_room` into smaller handlers (thanks sysko)
daurnimator <quae@daurnimator.com>
parents: 6092
diff changeset
  1215
-- hack - some buggy clients send presence updates to the room rather than their nick
9a7eaf0a35b6 plugins/muc/muc.lib: Split up `handle_to_room` into smaller handlers (thanks sysko)
daurnimator <quae@daurnimator.com>
parents: 6092
diff changeset
  1216
function room_mt:handle_presence_to_room(origin, stanza)
6119
c13f5d6b9b16 plugins/muc/muc.lib: Use `get_occupant_jid` method instead of indexing _jid_nick
daurnimator <quae@daurnimator.com>
parents: 6118
diff changeset
  1217
	local current_nick = self:get_occupant_jid(stanza.attr.from);
6100
c78ba94d3261 plugins/muc/muc.lib: Move all kick code into one place
daurnimator <quae@daurnimator.com>
parents: 6099
diff changeset
  1218
	local handled
6093
9a7eaf0a35b6 plugins/muc/muc.lib: Split up `handle_to_room` into smaller handlers (thanks sysko)
daurnimator <quae@daurnimator.com>
parents: 6092
diff changeset
  1219
	if current_nick then
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
  1220
		local to = stanza.attr.to;
6093
9a7eaf0a35b6 plugins/muc/muc.lib: Split up `handle_to_room` into smaller handlers (thanks sysko)
daurnimator <quae@daurnimator.com>
parents: 6092
diff changeset
  1221
		stanza.attr.to = current_nick;
6100
c78ba94d3261 plugins/muc/muc.lib: Move all kick code into one place
daurnimator <quae@daurnimator.com>
parents: 6099
diff changeset
  1222
		handled = self:handle_presence_to_occupant(origin, stanza);
6093
9a7eaf0a35b6 plugins/muc/muc.lib: Split up `handle_to_room` into smaller handlers (thanks sysko)
daurnimator <quae@daurnimator.com>
parents: 6092
diff changeset
  1223
		stanza.attr.to = to;
9a7eaf0a35b6 plugins/muc/muc.lib: Split up `handle_to_room` into smaller handlers (thanks sysko)
daurnimator <quae@daurnimator.com>
parents: 6092
diff changeset
  1224
	end
6100
c78ba94d3261 plugins/muc/muc.lib: Move all kick code into one place
daurnimator <quae@daurnimator.com>
parents: 6099
diff changeset
  1225
	return handled;
6093
9a7eaf0a35b6 plugins/muc/muc.lib: Split up `handle_to_room` into smaller handlers (thanks sysko)
daurnimator <quae@daurnimator.com>
parents: 6092
diff changeset
  1226
end
9a7eaf0a35b6 plugins/muc/muc.lib: Split up `handle_to_room` into smaller handlers (thanks sysko)
daurnimator <quae@daurnimator.com>
parents: 6092
diff changeset
  1227
6195
9f6a003baf2e plugins/muc/muc.lib: Add pre-invite event. Move role check to it
daurnimator <quae@daurnimator.com>
parents: 6194
diff changeset
  1228
-- Need visitor role or higher to invite
9f6a003baf2e plugins/muc/muc.lib: Add pre-invite event. Move role check to it
daurnimator <quae@daurnimator.com>
parents: 6194
diff changeset
  1229
module:hook("muc-pre-invite", function(event)
9f6a003baf2e plugins/muc/muc.lib: Add pre-invite event. Move role check to it
daurnimator <quae@daurnimator.com>
parents: 6194
diff changeset
  1230
	local room, stanza = event.room, event.stanza;
7092
890f4b2cc444 MUC: Add luacheck annotations, remove unused vars, rename conflicting vars etc
Kim Alvefur <zash@zash.se>
parents: 7016
diff changeset
  1231
	local _from = stanza.attr.from;
6195
9f6a003baf2e plugins/muc/muc.lib: Add pre-invite event. Move role check to it
daurnimator <quae@daurnimator.com>
parents: 6194
diff changeset
  1232
	local inviter = room:get_occupant_by_real_jid(_from);
9f6a003baf2e plugins/muc/muc.lib: Add pre-invite event. Move role check to it
daurnimator <quae@daurnimator.com>
parents: 6194
diff changeset
  1233
	local role = inviter and inviter.role or room:get_default_role(room:get_affiliation(_from));
9f6a003baf2e plugins/muc/muc.lib: Add pre-invite event. Move role check to it
daurnimator <quae@daurnimator.com>
parents: 6194
diff changeset
  1234
	if valid_roles[role or "none"] <= valid_roles.visitor then
9f6a003baf2e plugins/muc/muc.lib: Add pre-invite event. Move role check to it
daurnimator <quae@daurnimator.com>
parents: 6194
diff changeset
  1235
		event.origin.send(st.error_reply(stanza, "auth", "forbidden"));
6103
25ba4e2b31b3 plugins/muc/muc: Check for mediated invites in a smarter way
daurnimator <quae@daurnimator.com>
parents: 6102
diff changeset
  1236
		return true;
25ba4e2b31b3 plugins/muc/muc: Check for mediated invites in a smarter way
daurnimator <quae@daurnimator.com>
parents: 6102
diff changeset
  1237
	end
6195
9f6a003baf2e plugins/muc/muc.lib: Add pre-invite event. Move role check to it
daurnimator <quae@daurnimator.com>
parents: 6194
diff changeset
  1238
end);
9f6a003baf2e plugins/muc/muc.lib: Add pre-invite event. Move role check to it
daurnimator <quae@daurnimator.com>
parents: 6194
diff changeset
  1239
9f6a003baf2e plugins/muc/muc.lib: Add pre-invite event. Move role check to it
daurnimator <quae@daurnimator.com>
parents: 6194
diff changeset
  1240
function room_mt:handle_mediated_invite(origin, stanza)
9f6a003baf2e plugins/muc/muc.lib: Add pre-invite event. Move role check to it
daurnimator <quae@daurnimator.com>
parents: 6194
diff changeset
  1241
	local payload = stanza:get_child("x", "http://jabber.org/protocol/muc#user"):get_child("invite");
9f6a003baf2e plugins/muc/muc.lib: Add pre-invite event. Move role check to it
daurnimator <quae@daurnimator.com>
parents: 6194
diff changeset
  1242
	local invitee = jid_prep(payload.attr.to);
9f6a003baf2e plugins/muc/muc.lib: Add pre-invite event. Move role check to it
daurnimator <quae@daurnimator.com>
parents: 6194
diff changeset
  1243
	if not invitee then
6093
9a7eaf0a35b6 plugins/muc/muc.lib: Split up `handle_to_room` into smaller handlers (thanks sysko)
daurnimator <quae@daurnimator.com>
parents: 6092
diff changeset
  1244
		origin.send(st.error_reply(stanza, "cancel", "jid-malformed"));
6098
1d7e5d091980 plugins/muc/muc.lib: Add some missing return values
daurnimator <quae@daurnimator.com>
parents: 6097
diff changeset
  1245
		return true;
6229
8aa59b73f801 plugins/muc/muc.lib: Remove reversed conditionals when firing pre- events
daurnimator <quae@daurnimator.com>
parents: 6227
diff changeset
  1246
	elseif module:fire_event("muc-pre-invite", {room = self, origin = origin, stanza = stanza}) then
6195
9f6a003baf2e plugins/muc/muc.lib: Add pre-invite event. Move role check to it
daurnimator <quae@daurnimator.com>
parents: 6194
diff changeset
  1247
		return true;
6093
9a7eaf0a35b6 plugins/muc/muc.lib: Split up `handle_to_room` into smaller handlers (thanks sysko)
daurnimator <quae@daurnimator.com>
parents: 6092
diff changeset
  1248
	end
6272
90054f21d1af plugins/muc/muc: When forwarding mediated invites; use filtered version of original invite instead of new object
daurnimator <quae@daurnimator.com>
parents: 6269
diff changeset
  1249
	local invite = muc_util.filter_muc_x(st.clone(stanza));
90054f21d1af plugins/muc/muc: When forwarding mediated invites; use filtered version of original invite instead of new object
daurnimator <quae@daurnimator.com>
parents: 6269
diff changeset
  1250
	invite.attr.from = self.jid;
90054f21d1af plugins/muc/muc: When forwarding mediated invites; use filtered version of original invite instead of new object
daurnimator <quae@daurnimator.com>
parents: 6269
diff changeset
  1251
	invite.attr.to = invitee;
90054f21d1af plugins/muc/muc: When forwarding mediated invites; use filtered version of original invite instead of new object
daurnimator <quae@daurnimator.com>
parents: 6269
diff changeset
  1252
	invite:tag('x', {xmlns='http://jabber.org/protocol/muc#user'})
6195
9f6a003baf2e plugins/muc/muc.lib: Add pre-invite event. Move role check to it
daurnimator <quae@daurnimator.com>
parents: 6194
diff changeset
  1253
			:tag('invite', {from = stanza.attr.from;})
9f6a003baf2e plugins/muc/muc.lib: Add pre-invite event. Move role check to it
daurnimator <quae@daurnimator.com>
parents: 6194
diff changeset
  1254
				:tag('reason'):text(payload:get_child_text("reason")):up()
9f6a003baf2e plugins/muc/muc.lib: Add pre-invite event. Move role check to it
daurnimator <quae@daurnimator.com>
parents: 6194
diff changeset
  1255
			:up()
9f6a003baf2e plugins/muc/muc.lib: Add pre-invite event. Move role check to it
daurnimator <quae@daurnimator.com>
parents: 6194
diff changeset
  1256
		:up();
9f6a003baf2e plugins/muc/muc.lib: Add pre-invite event. Move role check to it
daurnimator <quae@daurnimator.com>
parents: 6194
diff changeset
  1257
	if not module:fire_event("muc-invite", {room = self, stanza = invite, origin = origin, incoming = stanza}) then
9f6a003baf2e plugins/muc/muc.lib: Add pre-invite event. Move role check to it
daurnimator <quae@daurnimator.com>
parents: 6194
diff changeset
  1258
		self:route_stanza(invite);
9f6a003baf2e plugins/muc/muc.lib: Add pre-invite event. Move role check to it
daurnimator <quae@daurnimator.com>
parents: 6194
diff changeset
  1259
	end
9f6a003baf2e plugins/muc/muc.lib: Add pre-invite event. Move role check to it
daurnimator <quae@daurnimator.com>
parents: 6194
diff changeset
  1260
	return true;
6093
9a7eaf0a35b6 plugins/muc/muc.lib: Split up `handle_to_room` into smaller handlers (thanks sysko)
daurnimator <quae@daurnimator.com>
parents: 6092
diff changeset
  1261
end
9a7eaf0a35b6 plugins/muc/muc.lib: Split up `handle_to_room` into smaller handlers (thanks sysko)
daurnimator <quae@daurnimator.com>
parents: 6092
diff changeset
  1262
6194
9b6c2d89f143 plugins/muc/muc.lib: Tidy up muc-invite event.
daurnimator <quae@daurnimator.com>
parents: 6193
diff changeset
  1263
-- COMPAT: Some older clients expect this
9b6c2d89f143 plugins/muc/muc.lib: Tidy up muc-invite event.
daurnimator <quae@daurnimator.com>
parents: 6193
diff changeset
  1264
module:hook("muc-invite", function(event)
9b6c2d89f143 plugins/muc/muc.lib: Tidy up muc-invite event.
daurnimator <quae@daurnimator.com>
parents: 6193
diff changeset
  1265
	local room, stanza = event.room, event.stanza;
9b6c2d89f143 plugins/muc/muc.lib: Tidy up muc-invite event.
daurnimator <quae@daurnimator.com>
parents: 6193
diff changeset
  1266
	local invite = stanza:get_child("x", "http://jabber.org/protocol/muc#user"):get_child("invite");
9b6c2d89f143 plugins/muc/muc.lib: Tidy up muc-invite event.
daurnimator <quae@daurnimator.com>
parents: 6193
diff changeset
  1267
	local reason = invite:get_child_text("reason");
9b6c2d89f143 plugins/muc/muc.lib: Tidy up muc-invite event.
daurnimator <quae@daurnimator.com>
parents: 6193
diff changeset
  1268
	stanza:tag('x', {xmlns = "jabber:x:conference"; jid = room.jid;})
9b6c2d89f143 plugins/muc/muc.lib: Tidy up muc-invite event.
daurnimator <quae@daurnimator.com>
parents: 6193
diff changeset
  1269
		:text(reason or "")
9b6c2d89f143 plugins/muc/muc.lib: Tidy up muc-invite event.
daurnimator <quae@daurnimator.com>
parents: 6193
diff changeset
  1270
	:up();
9b6c2d89f143 plugins/muc/muc.lib: Tidy up muc-invite event.
daurnimator <quae@daurnimator.com>
parents: 6193
diff changeset
  1271
end);
9b6c2d89f143 plugins/muc/muc.lib: Tidy up muc-invite event.
daurnimator <quae@daurnimator.com>
parents: 6193
diff changeset
  1272
9b6c2d89f143 plugins/muc/muc.lib: Tidy up muc-invite event.
daurnimator <quae@daurnimator.com>
parents: 6193
diff changeset
  1273
-- Add a plain message for clients which don't support invites
9b6c2d89f143 plugins/muc/muc.lib: Tidy up muc-invite event.
daurnimator <quae@daurnimator.com>
parents: 6193
diff changeset
  1274
module:hook("muc-invite", function(event)
9b6c2d89f143 plugins/muc/muc.lib: Tidy up muc-invite event.
daurnimator <quae@daurnimator.com>
parents: 6193
diff changeset
  1275
	local room, stanza = event.room, event.stanza;
6274
77bdbaec3b7f plugins/muc/muc.lib: Don't add invite/decline bodies if they already have one
daurnimator <quae@daurnimator.com>
parents: 6273
diff changeset
  1276
	if not stanza:get_child("body") then
77bdbaec3b7f plugins/muc/muc.lib: Don't add invite/decline bodies if they already have one
daurnimator <quae@daurnimator.com>
parents: 6273
diff changeset
  1277
		local invite = stanza:get_child("x", "http://jabber.org/protocol/muc#user"):get_child("invite");
77bdbaec3b7f plugins/muc/muc.lib: Don't add invite/decline bodies if they already have one
daurnimator <quae@daurnimator.com>
parents: 6273
diff changeset
  1278
		local reason = invite:get_child_text("reason") or "";
77bdbaec3b7f plugins/muc/muc.lib: Don't add invite/decline bodies if they already have one
daurnimator <quae@daurnimator.com>
parents: 6273
diff changeset
  1279
		stanza:tag("body")
9059
37e287113a8d MUC: Fix inverted logic ()
Matthew Wild <mwild1@gmail.com>
parents: 9058
diff changeset
  1280
			:text(invite.attr.from.." invited you to the room "..room.jid..(reason ~= "" and (" ("..reason..")") or ""))
6274
77bdbaec3b7f plugins/muc/muc.lib: Don't add invite/decline bodies if they already have one
daurnimator <quae@daurnimator.com>
parents: 6273
diff changeset
  1281
		:up();
77bdbaec3b7f plugins/muc/muc.lib: Don't add invite/decline bodies if they already have one
daurnimator <quae@daurnimator.com>
parents: 6273
diff changeset
  1282
	end
6194
9b6c2d89f143 plugins/muc/muc.lib: Tidy up muc-invite event.
daurnimator <quae@daurnimator.com>
parents: 6193
diff changeset
  1283
end);
9b6c2d89f143 plugins/muc/muc.lib: Tidy up muc-invite event.
daurnimator <quae@daurnimator.com>
parents: 6193
diff changeset
  1284
6123
7f82bbd249fe plugins/muc/muc.lib: Remove `payload` argument from `handle_mediated_*`; extract it from inside.
daurnimator <quae@daurnimator.com>
parents: 6122
diff changeset
  1285
function room_mt:handle_mediated_decline(origin, stanza)
6196
e73bb1568d87 plugins/muc/muc.lib: Update declines to be more like invites
daurnimator <quae@daurnimator.com>
parents: 6195
diff changeset
  1286
	local payload = stanza:get_child("x", "http://jabber.org/protocol/muc#user"):get_child("decline");
6105
68f53b9a186e plugins/muc/muc: Support mediated declines
daurnimator <quae@daurnimator.com>
parents: 6104
diff changeset
  1287
	local declinee = jid_prep(payload.attr.to);
6196
e73bb1568d87 plugins/muc/muc.lib: Update declines to be more like invites
daurnimator <quae@daurnimator.com>
parents: 6195
diff changeset
  1288
	if not declinee then
6105
68f53b9a186e plugins/muc/muc: Support mediated declines
daurnimator <quae@daurnimator.com>
parents: 6104
diff changeset
  1289
		origin.send(st.error_reply(stanza, "cancel", "jid-malformed"));
68f53b9a186e plugins/muc/muc: Support mediated declines
daurnimator <quae@daurnimator.com>
parents: 6104
diff changeset
  1290
		return true;
6229
8aa59b73f801 plugins/muc/muc.lib: Remove reversed conditionals when firing pre- events
daurnimator <quae@daurnimator.com>
parents: 6227
diff changeset
  1291
	elseif module:fire_event("muc-pre-decline", {room = self, origin = origin, stanza = stanza}) then
6196
e73bb1568d87 plugins/muc/muc.lib: Update declines to be more like invites
daurnimator <quae@daurnimator.com>
parents: 6195
diff changeset
  1292
		return true;
6105
68f53b9a186e plugins/muc/muc: Support mediated declines
daurnimator <quae@daurnimator.com>
parents: 6104
diff changeset
  1293
	end
6273
7ef064101994 plugins/muc/muc.lib: Use original decline as template for medated decline
daurnimator <quae@daurnimator.com>
parents: 6272
diff changeset
  1294
	local decline = muc_util.filter_muc_x(st.clone(stanza));
7ef064101994 plugins/muc/muc.lib: Use original decline as template for medated decline
daurnimator <quae@daurnimator.com>
parents: 6272
diff changeset
  1295
	decline.attr.from = self.jid;
7ef064101994 plugins/muc/muc.lib: Use original decline as template for medated decline
daurnimator <quae@daurnimator.com>
parents: 6272
diff changeset
  1296
	decline.attr.to = declinee;
7ef064101994 plugins/muc/muc.lib: Use original decline as template for medated decline
daurnimator <quae@daurnimator.com>
parents: 6272
diff changeset
  1297
	decline:tag("x", {xmlns = "http://jabber.org/protocol/muc#user"})
6196
e73bb1568d87 plugins/muc/muc.lib: Update declines to be more like invites
daurnimator <quae@daurnimator.com>
parents: 6195
diff changeset
  1298
			:tag("decline", {from = stanza.attr.from})
e73bb1568d87 plugins/muc/muc.lib: Update declines to be more like invites
daurnimator <quae@daurnimator.com>
parents: 6195
diff changeset
  1299
				:tag("reason"):text(payload:get_child_text("reason")):up()
e73bb1568d87 plugins/muc/muc.lib: Update declines to be more like invites
daurnimator <quae@daurnimator.com>
parents: 6195
diff changeset
  1300
			:up()
e73bb1568d87 plugins/muc/muc.lib: Update declines to be more like invites
daurnimator <quae@daurnimator.com>
parents: 6195
diff changeset
  1301
		:up();
e73bb1568d87 plugins/muc/muc.lib: Update declines to be more like invites
daurnimator <quae@daurnimator.com>
parents: 6195
diff changeset
  1302
	if not module:fire_event("muc-decline", {room = self, stanza = decline, origin = origin, incoming = stanza}) then
7092
890f4b2cc444 MUC: Add luacheck annotations, remove unused vars, rename conflicting vars etc
Kim Alvefur <zash@zash.se>
parents: 7016
diff changeset
  1303
		declinee = decline.attr.to; -- re-fetch, in case event modified it
6275
d891fa02e5e5 plugins/muc/muc.lib: Deliver declines to in-room jids correctly
daurnimator <quae@daurnimator.com>
parents: 6274
diff changeset
  1304
		local occupant
d891fa02e5e5 plugins/muc/muc.lib: Deliver declines to in-room jids correctly
daurnimator <quae@daurnimator.com>
parents: 6274
diff changeset
  1305
		if jid_bare(declinee) == self.jid then -- declinee jid is already an in-room jid
d891fa02e5e5 plugins/muc/muc.lib: Deliver declines to in-room jids correctly
daurnimator <quae@daurnimator.com>
parents: 6274
diff changeset
  1306
			occupant = self:get_occupant_by_nick(declinee);
d891fa02e5e5 plugins/muc/muc.lib: Deliver declines to in-room jids correctly
daurnimator <quae@daurnimator.com>
parents: 6274
diff changeset
  1307
		end
6196
e73bb1568d87 plugins/muc/muc.lib: Update declines to be more like invites
daurnimator <quae@daurnimator.com>
parents: 6195
diff changeset
  1308
		if occupant then
e73bb1568d87 plugins/muc/muc.lib: Update declines to be more like invites
daurnimator <quae@daurnimator.com>
parents: 6195
diff changeset
  1309
			self:route_to_occupant(occupant, decline);
e73bb1568d87 plugins/muc/muc.lib: Update declines to be more like invites
daurnimator <quae@daurnimator.com>
parents: 6195
diff changeset
  1310
		else
e73bb1568d87 plugins/muc/muc.lib: Update declines to be more like invites
daurnimator <quae@daurnimator.com>
parents: 6195
diff changeset
  1311
			self:route_stanza(decline);
e73bb1568d87 plugins/muc/muc.lib: Update declines to be more like invites
daurnimator <quae@daurnimator.com>
parents: 6195
diff changeset
  1312
		end
e73bb1568d87 plugins/muc/muc.lib: Update declines to be more like invites
daurnimator <quae@daurnimator.com>
parents: 6195
diff changeset
  1313
	end
e73bb1568d87 plugins/muc/muc.lib: Update declines to be more like invites
daurnimator <quae@daurnimator.com>
parents: 6195
diff changeset
  1314
	return true;
6105
68f53b9a186e plugins/muc/muc: Support mediated declines
daurnimator <quae@daurnimator.com>
parents: 6104
diff changeset
  1315
end
68f53b9a186e plugins/muc/muc: Support mediated declines
daurnimator <quae@daurnimator.com>
parents: 6104
diff changeset
  1316
6196
e73bb1568d87 plugins/muc/muc.lib: Update declines to be more like invites
daurnimator <quae@daurnimator.com>
parents: 6195
diff changeset
  1317
-- Add a plain message for clients which don't support declines
6131
8dd0c6145603 plugins/muc/muc.lib: Add decline event for parity with invite
daurnimator <quae@daurnimator.com>
parents: 6130
diff changeset
  1318
module:hook("muc-decline", function(event)
6196
e73bb1568d87 plugins/muc/muc.lib: Update declines to be more like invites
daurnimator <quae@daurnimator.com>
parents: 6195
diff changeset
  1319
	local room, stanza = event.room, event.stanza;
6274
77bdbaec3b7f plugins/muc/muc.lib: Don't add invite/decline bodies if they already have one
daurnimator <quae@daurnimator.com>
parents: 6273
diff changeset
  1320
	if not stanza:get_child("body") then
77bdbaec3b7f plugins/muc/muc.lib: Don't add invite/decline bodies if they already have one
daurnimator <quae@daurnimator.com>
parents: 6273
diff changeset
  1321
		local decline = stanza:get_child("x", "http://jabber.org/protocol/muc#user"):get_child("decline");
77bdbaec3b7f plugins/muc/muc.lib: Don't add invite/decline bodies if they already have one
daurnimator <quae@daurnimator.com>
parents: 6273
diff changeset
  1322
		local reason = decline:get_child_text("reason") or "";
9084
ce57c69a20e2 MUC: Split long lines [luacheck strict]
Kim Alvefur <zash@zash.se>
parents: 9060
diff changeset
  1323
		stanza:body(decline.attr.from.." declined your invite to the room "
ce57c69a20e2 MUC: Split long lines [luacheck strict]
Kim Alvefur <zash@zash.se>
parents: 9060
diff changeset
  1324
			..room.jid..(reason ~= "" and (" ("..reason..")") or ""));
6274
77bdbaec3b7f plugins/muc/muc.lib: Don't add invite/decline bodies if they already have one
daurnimator <quae@daurnimator.com>
parents: 6273
diff changeset
  1325
	end
6196
e73bb1568d87 plugins/muc/muc.lib: Update declines to be more like invites
daurnimator <quae@daurnimator.com>
parents: 6195
diff changeset
  1326
end);
6131
8dd0c6145603 plugins/muc/muc.lib: Add decline event for parity with invite
daurnimator <quae@daurnimator.com>
parents: 6130
diff changeset
  1327
6093
9a7eaf0a35b6 plugins/muc/muc.lib: Split up `handle_to_room` into smaller handlers (thanks sysko)
daurnimator <quae@daurnimator.com>
parents: 6092
diff changeset
  1328
function room_mt:handle_message_to_room(origin, stanza)
9a7eaf0a35b6 plugins/muc/muc.lib: Split up `handle_to_room` into smaller handlers (thanks sysko)
daurnimator <quae@daurnimator.com>
parents: 6092
diff changeset
  1329
	local type = stanza.attr.type;
9a7eaf0a35b6 plugins/muc/muc.lib: Split up `handle_to_room` into smaller handlers (thanks sysko)
daurnimator <quae@daurnimator.com>
parents: 6092
diff changeset
  1330
	if type == "groupchat" then
9a7eaf0a35b6 plugins/muc/muc.lib: Split up `handle_to_room` into smaller handlers (thanks sysko)
daurnimator <quae@daurnimator.com>
parents: 6092
diff changeset
  1331
		return self:handle_groupchat_to_room(origin, stanza)
9a7eaf0a35b6 plugins/muc/muc.lib: Split up `handle_to_room` into smaller handlers (thanks sysko)
daurnimator <quae@daurnimator.com>
parents: 6092
diff changeset
  1332
	elseif type == "error" and is_kickable_error(stanza) then
6100
c78ba94d3261 plugins/muc/muc.lib: Move all kick code into one place
daurnimator <quae@daurnimator.com>
parents: 6099
diff changeset
  1333
		return self:handle_kickable(origin, stanza)
8529
f1a46eef9df1 MUC: Treat missing type and type=normal the same
Kim Alvefur <zash@zash.se>
parents: 8522
diff changeset
  1334
	elseif type == nil or type == "normal" then
6103
25ba4e2b31b3 plugins/muc/muc: Check for mediated invites in a smarter way
daurnimator <quae@daurnimator.com>
parents: 6102
diff changeset
  1335
		local x = stanza:get_child("x", "http://jabber.org/protocol/muc#user");
25ba4e2b31b3 plugins/muc/muc: Check for mediated invites in a smarter way
daurnimator <quae@daurnimator.com>
parents: 6102
diff changeset
  1336
		if x then
25ba4e2b31b3 plugins/muc/muc: Check for mediated invites in a smarter way
daurnimator <quae@daurnimator.com>
parents: 6102
diff changeset
  1337
			local payload = x.tags[1];
7092
890f4b2cc444 MUC: Add luacheck annotations, remove unused vars, rename conflicting vars etc
Kim Alvefur <zash@zash.se>
parents: 7016
diff changeset
  1338
			if payload == nil then --luacheck: ignore 542
6104
260a18062cb2 plugins/muc/muc: Rename `handle_invite_to_room` to `handle_mediated_invite`; clean up logic
daurnimator <quae@daurnimator.com>
parents: 6103
diff changeset
  1339
				-- fallthrough
260a18062cb2 plugins/muc/muc: Rename `handle_invite_to_room` to `handle_mediated_invite`; clean up logic
daurnimator <quae@daurnimator.com>
parents: 6103
diff changeset
  1340
			elseif payload.name == "invite" and payload.attr.to then
6123
7f82bbd249fe plugins/muc/muc.lib: Remove `payload` argument from `handle_mediated_*`; extract it from inside.
daurnimator <quae@daurnimator.com>
parents: 6122
diff changeset
  1341
				return self:handle_mediated_invite(origin, stanza)
6105
68f53b9a186e plugins/muc/muc: Support mediated declines
daurnimator <quae@daurnimator.com>
parents: 6104
diff changeset
  1342
			elseif payload.name == "decline" and payload.attr.to then
6123
7f82bbd249fe plugins/muc/muc.lib: Remove `payload` argument from `handle_mediated_*`; extract it from inside.
daurnimator <quae@daurnimator.com>
parents: 6122
diff changeset
  1343
				return self:handle_mediated_decline(origin, stanza)
6103
25ba4e2b31b3 plugins/muc/muc: Check for mediated invites in a smarter way
daurnimator <quae@daurnimator.com>
parents: 6102
diff changeset
  1344
			end
2005
478ba7e85862 MUC: Rewrote code for mediated invites to be more robust, and to support legacy clients.
Waqas Hussain <waqas20@gmail.com>
parents: 1999
diff changeset
  1345
			origin.send(st.error_reply(stanza, "cancel", "bad-request"));
6098
1d7e5d091980 plugins/muc/muc.lib: Add some missing return values
daurnimator <quae@daurnimator.com>
parents: 6097
diff changeset
  1346
			return true;
2005
478ba7e85862 MUC: Rewrote code for mediated invites to be more robust, and to support legacy clients.
Waqas Hussain <waqas20@gmail.com>
parents: 1999
diff changeset
  1347
		end
8854
ab5f678f1376 MUC: Support MUC voice requests and approvals in moderated rooms (closes #655) (thanks to Lance Stout)
Kim Alvefur <zash@zash.se>
parents: 8846
diff changeset
  1348
ab5f678f1376 MUC: Support MUC voice requests and approvals in moderated rooms (closes #655) (thanks to Lance Stout)
Kim Alvefur <zash@zash.se>
parents: 8846
diff changeset
  1349
		local form = stanza:get_child("x", "jabber:x:data");
8868
2a8bbfcb6868 MUC: Move voice request into its own lib
Kim Alvefur <zash@zash.se>
parents: 8864
diff changeset
  1350
		local form_type = dataform.get_type(form);
2a8bbfcb6868 MUC: Move voice request into its own lib
Kim Alvefur <zash@zash.se>
parents: 8864
diff changeset
  1351
		if form_type == "http://jabber.org/protocol/muc#request" then
2a8bbfcb6868 MUC: Move voice request into its own lib
Kim Alvefur <zash@zash.se>
parents: 8864
diff changeset
  1352
			self:handle_role_request(origin, stanza, form);
2a8bbfcb6868 MUC: Move voice request into its own lib
Kim Alvefur <zash@zash.se>
parents: 8864
diff changeset
  1353
			return true;
8854
ab5f678f1376 MUC: Support MUC voice requests and approvals in moderated rooms (closes #655) (thanks to Lance Stout)
Kim Alvefur <zash@zash.se>
parents: 8846
diff changeset
  1354
		end
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
  1355
	end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
  1356
end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
  1357
7092
890f4b2cc444 MUC: Add luacheck annotations, remove unused vars, rename conflicting vars etc
Kim Alvefur <zash@zash.se>
parents: 7016
diff changeset
  1358
function room_mt:route_stanza(stanza) -- luacheck: ignore 212
6180
35388114439f plugins/muc/muc.lib: non-function changes (reordering, semicolons and comments)
daurnimator <quae@daurnimator.com>
parents: 6179
diff changeset
  1359
	module:send(stanza);
6111
f8b94903be52 plugins/muc: Provide a reasonable default `route_stanza`
daurnimator <quae@daurnimator.com>
parents: 6108
diff changeset
  1360
end
1735
81406277279e MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 1734
diff changeset
  1361
1737
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
  1362
function room_mt:get_affiliation(jid)
10554
0566b45da987 MUC: Remove some unused variables [luacheck]
Kim Alvefur <zash@zash.se>
parents: 10455
diff changeset
  1363
	local node, host = jid_split(jid);
9084
ce57c69a20e2 MUC: Split long lines [luacheck strict]
Kim Alvefur <zash@zash.se>
parents: 9060
diff changeset
  1364
	-- Affiliations are granted, revoked, and maintained based on the user's bare JID.
1737
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
  1365
	local bare = node and node.."@"..host or host;
9084
ce57c69a20e2 MUC: Split long lines [luacheck strict]
Kim Alvefur <zash@zash.se>
parents: 9060
diff changeset
  1366
	local result = self._affiliations[bare];
1737
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
  1367
	if not result and self._affiliations[host] == "outcast" then result = "outcast"; end -- host banned
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
  1368
	return result;
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
  1369
end
6186
85f7cd91dc31 plugins/muc/muc.lib: Smarter validation in set_affiliation
daurnimator <quae@daurnimator.com>
parents: 6185
diff changeset
  1370
6481
413923bbd1a0 plugins/muc/muc.lib: Add :each_affiliation() iterator
daurnimator <quae@daurnimator.com>
parents: 6479
diff changeset
  1371
-- Iterates over jid, affiliation pairs
413923bbd1a0 plugins/muc/muc.lib: Add :each_affiliation() iterator
daurnimator <quae@daurnimator.com>
parents: 6479
diff changeset
  1372
function room_mt:each_affiliation(with_affiliation)
9534
f2d70dc13700 MUC: Include affiliation data when iterating over affiliations with a room
Matthew Wild <mwild1@gmail.com>
parents: 9533
diff changeset
  1373
	local _affiliations, _affiliation_data = self._affiliations, self._affiliation_data;
f2d70dc13700 MUC: Include affiliation data when iterating over affiliations with a room
Matthew Wild <mwild1@gmail.com>
parents: 9533
diff changeset
  1374
	return function(_, jid)
f2d70dc13700 MUC: Include affiliation data when iterating over affiliations with a room
Matthew Wild <mwild1@gmail.com>
parents: 9533
diff changeset
  1375
		local affiliation;
f2d70dc13700 MUC: Include affiliation data when iterating over affiliations with a room
Matthew Wild <mwild1@gmail.com>
parents: 9533
diff changeset
  1376
		repeat -- Iterate until we get a match
f2d70dc13700 MUC: Include affiliation data when iterating over affiliations with a room
Matthew Wild <mwild1@gmail.com>
parents: 9533
diff changeset
  1377
			jid, affiliation = next(_affiliations, jid);
f2d70dc13700 MUC: Include affiliation data when iterating over affiliations with a room
Matthew Wild <mwild1@gmail.com>
parents: 9533
diff changeset
  1378
		until with_affiliation == nil or jid == nil or affiliation == with_affiliation
f2d70dc13700 MUC: Include affiliation data when iterating over affiliations with a room
Matthew Wild <mwild1@gmail.com>
parents: 9533
diff changeset
  1379
		return jid, affiliation, _affiliation_data[jid];
f2d70dc13700 MUC: Include affiliation data when iterating over affiliations with a room
Matthew Wild <mwild1@gmail.com>
parents: 9533
diff changeset
  1380
	end, nil, nil;
6481
413923bbd1a0 plugins/muc/muc.lib: Add :each_affiliation() iterator
daurnimator <quae@daurnimator.com>
parents: 6479
diff changeset
  1381
end
8795
c2b99fa134b3 MUC: Import revised, more comprehensive patch for 8da11142fabf (#345)
Matthew Wild <mwild1@gmail.com>
parents: 8794
diff changeset
  1382
9240
b86c2e135797 MUC: Add support for storing additional data with MUC affiliations
Matthew Wild <mwild1@gmail.com>
parents: 9197
diff changeset
  1383
function room_mt:set_affiliation(actor, jid, affiliation, reason, data)
6186
85f7cd91dc31 plugins/muc/muc.lib: Smarter validation in set_affiliation
daurnimator <quae@daurnimator.com>
parents: 6185
diff changeset
  1384
	if not actor then return nil, "modify", "not-acceptable"; end;
8795
c2b99fa134b3 MUC: Import revised, more comprehensive patch for 8da11142fabf (#345)
Matthew Wild <mwild1@gmail.com>
parents: 8794
diff changeset
  1385
10554
0566b45da987 MUC: Remove some unused variables [luacheck]
Kim Alvefur <zash@zash.se>
parents: 10455
diff changeset
  1386
	local node, host = jid_split(jid);
6479
fb8a9873728b plugins/muc/muc.lib: Kick users from outcast hosts
daurnimator <quae@daurnimator.com>
parents: 6478
diff changeset
  1387
	if not host then return nil, "modify", "not-acceptable"; end
fb8a9873728b plugins/muc/muc.lib: Kick users from outcast hosts
daurnimator <quae@daurnimator.com>
parents: 6478
diff changeset
  1388
	jid = jid_join(node, host); -- Bare
fb8a9873728b plugins/muc/muc.lib: Kick users from outcast hosts
daurnimator <quae@daurnimator.com>
parents: 6478
diff changeset
  1389
	local is_host_only = node == nil;
6186
85f7cd91dc31 plugins/muc/muc.lib: Smarter validation in set_affiliation
daurnimator <quae@daurnimator.com>
parents: 6185
diff changeset
  1390
85f7cd91dc31 plugins/muc/muc.lib: Smarter validation in set_affiliation
daurnimator <quae@daurnimator.com>
parents: 6185
diff changeset
  1391
	if valid_affiliations[affiliation or "none"] == nil then
8845
463505cc75d5 MUC: Revert unstable MUC commits since 0.10.1
Matthew Wild <mwild1@gmail.com>
parents: 8842
diff changeset
  1392
		return nil, "modify", "not-acceptable";
1742
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
  1393
	end
6186
85f7cd91dc31 plugins/muc/muc.lib: Smarter validation in set_affiliation
daurnimator <quae@daurnimator.com>
parents: 6185
diff changeset
  1394
	affiliation = affiliation ~= "none" and affiliation or nil; -- coerces `affiliation == false` to `nil`
85f7cd91dc31 plugins/muc/muc.lib: Smarter validation in set_affiliation
daurnimator <quae@daurnimator.com>
parents: 6185
diff changeset
  1395
6187
c0b4b5d41e55 plugins/muc/muc.lib: Improve set affiliation logic;
daurnimator <quae@daurnimator.com>
parents: 6186
diff changeset
  1396
	local target_affiliation = self._affiliations[jid]; -- Raw; don't want to check against host
c0b4b5d41e55 plugins/muc/muc.lib: Improve set affiliation logic;
daurnimator <quae@daurnimator.com>
parents: 6186
diff changeset
  1397
	local is_downgrade = valid_affiliations[target_affiliation or "none"] > valid_affiliations[affiliation or "none"];
8795
c2b99fa134b3 MUC: Import revised, more comprehensive patch for 8da11142fabf (#345)
Matthew Wild <mwild1@gmail.com>
parents: 8794
diff changeset
  1398
6478
3c815a64042b plugins/muc/muc.lib: Fix passing actor along as a boolean (thanks fippo)
daurnimator <quae@daurnimator.com>
parents: 6457
diff changeset
  1399
	if actor == true then
3c815a64042b plugins/muc/muc.lib: Fix passing actor along as a boolean (thanks fippo)
daurnimator <quae@daurnimator.com>
parents: 6457
diff changeset
  1400
		actor = nil -- So we can pass it safely to 'publicise_occupant_status' below
3c815a64042b plugins/muc/muc.lib: Fix passing actor along as a boolean (thanks fippo)
daurnimator <quae@daurnimator.com>
parents: 6457
diff changeset
  1401
	else
8845
463505cc75d5 MUC: Revert unstable MUC commits since 0.10.1
Matthew Wild <mwild1@gmail.com>
parents: 8842
diff changeset
  1402
		local actor_affiliation = self:get_affiliation(actor);
6187
c0b4b5d41e55 plugins/muc/muc.lib: Improve set affiliation logic;
daurnimator <quae@daurnimator.com>
parents: 6186
diff changeset
  1403
		if actor_affiliation == "owner" then
9722
81ef96368bff MUC: Allow changing data attached to an only owner (fixes #1273)
Kim Alvefur <zash@zash.se>
parents: 9720
diff changeset
  1404
			if jid_bare(actor) == jid and is_downgrade then -- self change
6187
c0b4b5d41e55 plugins/muc/muc.lib: Improve set affiliation logic;
daurnimator <quae@daurnimator.com>
parents: 6186
diff changeset
  1405
				-- need at least one owner
c0b4b5d41e55 plugins/muc/muc.lib: Improve set affiliation logic;
daurnimator <quae@daurnimator.com>
parents: 6186
diff changeset
  1406
				local is_last = true;
6481
413923bbd1a0 plugins/muc/muc.lib: Add :each_affiliation() iterator
daurnimator <quae@daurnimator.com>
parents: 6479
diff changeset
  1407
				for j in self:each_affiliation("owner") do
413923bbd1a0 plugins/muc/muc.lib: Add :each_affiliation() iterator
daurnimator <quae@daurnimator.com>
parents: 6479
diff changeset
  1408
					if j ~= jid then is_last = false; break; end
8795
c2b99fa134b3 MUC: Import revised, more comprehensive patch for 8da11142fabf (#345)
Matthew Wild <mwild1@gmail.com>
parents: 8794
diff changeset
  1409
				end
c2b99fa134b3 MUC: Import revised, more comprehensive patch for 8da11142fabf (#345)
Matthew Wild <mwild1@gmail.com>
parents: 8794
diff changeset
  1410
				if is_last then
6187
c0b4b5d41e55 plugins/muc/muc.lib: Improve set affiliation logic;
daurnimator <quae@daurnimator.com>
parents: 6186
diff changeset
  1411
					return nil, "cancel", "conflict";
8795
c2b99fa134b3 MUC: Import revised, more comprehensive patch for 8da11142fabf (#345)
Matthew Wild <mwild1@gmail.com>
parents: 8794
diff changeset
  1412
				end
4357
d6928b78c548 MUC: Allow affiliation change when argument actor==true in room:set_affiliation().
Waqas Hussain <waqas20@gmail.com>
parents: 4326
diff changeset
  1413
			end
6187
c0b4b5d41e55 plugins/muc/muc.lib: Improve set affiliation logic;
daurnimator <quae@daurnimator.com>
parents: 6186
diff changeset
  1414
			-- owners can do anything else
c0b4b5d41e55 plugins/muc/muc.lib: Improve set affiliation logic;
daurnimator <quae@daurnimator.com>
parents: 6186
diff changeset
  1415
		elseif affiliation == "owner" or affiliation == "admin"
c0b4b5d41e55 plugins/muc/muc.lib: Improve set affiliation logic;
daurnimator <quae@daurnimator.com>
parents: 6186
diff changeset
  1416
			or actor_affiliation ~= "admin"
c0b4b5d41e55 plugins/muc/muc.lib: Improve set affiliation logic;
daurnimator <quae@daurnimator.com>
parents: 6186
diff changeset
  1417
			or target_affiliation == "owner" or target_affiliation == "admin" then
c0b4b5d41e55 plugins/muc/muc.lib: Improve set affiliation logic;
daurnimator <quae@daurnimator.com>
parents: 6186
diff changeset
  1418
			-- Can't demote owners or other admins
c0b4b5d41e55 plugins/muc/muc.lib: Improve set affiliation logic;
daurnimator <quae@daurnimator.com>
parents: 6186
diff changeset
  1419
			return nil, "cancel", "not-allowed";
8839
564e897f0790 MUC: reject non-bare JIDs in set_affiliation requests with not-acceptable
Jonas Wielicki <jonas@wielicki.name>
parents: 8838
diff changeset
  1420
		end
4202
dff7df4a191b MUC: Don't limit affiliation changes to owners, and allow owners to remove themselves if they are not the last owner.
Waqas Hussain <waqas20@gmail.com>
parents: 4201
diff changeset
  1421
	end
8795
c2b99fa134b3 MUC: Import revised, more comprehensive patch for 8da11142fabf (#345)
Matthew Wild <mwild1@gmail.com>
parents: 8794
diff changeset
  1422
6187
c0b4b5d41e55 plugins/muc/muc.lib: Improve set affiliation logic;
daurnimator <quae@daurnimator.com>
parents: 6186
diff changeset
  1423
	-- Set in 'database'
c0b4b5d41e55 plugins/muc/muc.lib: Improve set affiliation logic;
daurnimator <quae@daurnimator.com>
parents: 6186
diff changeset
  1424
	self._affiliations[jid] = affiliation;
9240
b86c2e135797 MUC: Add support for storing additional data with MUC affiliations
Matthew Wild <mwild1@gmail.com>
parents: 9197
diff changeset
  1425
	if not affiliation or data == false or (data ~= nil and next(data) == nil) then
b86c2e135797 MUC: Add support for storing additional data with MUC affiliations
Matthew Wild <mwild1@gmail.com>
parents: 9197
diff changeset
  1426
		module:log("debug", "Clearing affiliation data for %s", jid);
b86c2e135797 MUC: Add support for storing additional data with MUC affiliations
Matthew Wild <mwild1@gmail.com>
parents: 9197
diff changeset
  1427
		self._affiliation_data[jid] = nil;
b86c2e135797 MUC: Add support for storing additional data with MUC affiliations
Matthew Wild <mwild1@gmail.com>
parents: 9197
diff changeset
  1428
	elseif data then
b86c2e135797 MUC: Add support for storing additional data with MUC affiliations
Matthew Wild <mwild1@gmail.com>
parents: 9197
diff changeset
  1429
		module:log("debug", "Updating affiliation data for %s", jid);
b86c2e135797 MUC: Add support for storing additional data with MUC affiliations
Matthew Wild <mwild1@gmail.com>
parents: 9197
diff changeset
  1430
		self._affiliation_data[jid] = data;
b86c2e135797 MUC: Add support for storing additional data with MUC affiliations
Matthew Wild <mwild1@gmail.com>
parents: 9197
diff changeset
  1431
	end
8795
c2b99fa134b3 MUC: Import revised, more comprehensive patch for 8da11142fabf (#345)
Matthew Wild <mwild1@gmail.com>
parents: 8794
diff changeset
  1432
6187
c0b4b5d41e55 plugins/muc/muc.lib: Improve set affiliation logic;
daurnimator <quae@daurnimator.com>
parents: 6186
diff changeset
  1433
	-- Update roles
c0b4b5d41e55 plugins/muc/muc.lib: Improve set affiliation logic;
daurnimator <quae@daurnimator.com>
parents: 6186
diff changeset
  1434
	local role = self:get_default_role(affiliation);
c0b4b5d41e55 plugins/muc/muc.lib: Improve set affiliation logic;
daurnimator <quae@daurnimator.com>
parents: 6186
diff changeset
  1435
	local role_rank = valid_roles[role or "none"];
c0b4b5d41e55 plugins/muc/muc.lib: Improve set affiliation logic;
daurnimator <quae@daurnimator.com>
parents: 6186
diff changeset
  1436
	local occupants_updated = {}; -- Filled with old roles
7092
890f4b2cc444 MUC: Add luacheck annotations, remove unused vars, rename conflicting vars etc
Kim Alvefur <zash@zash.se>
parents: 7016
diff changeset
  1437
	for nick, occupant in self:each_occupant() do -- luacheck: ignore 213
6479
fb8a9873728b plugins/muc/muc.lib: Kick users from outcast hosts
daurnimator <quae@daurnimator.com>
parents: 6478
diff changeset
  1438
		if occupant.bare_jid == jid or (
fb8a9873728b plugins/muc/muc.lib: Kick users from outcast hosts
daurnimator <quae@daurnimator.com>
parents: 6478
diff changeset
  1439
			-- Outcast can be by host.
fb8a9873728b plugins/muc/muc.lib: Kick users from outcast hosts
daurnimator <quae@daurnimator.com>
parents: 6478
diff changeset
  1440
			is_host_only and affiliation == "outcast" and select(2, jid_split(occupant.bare_jid)) == host
fb8a9873728b plugins/muc/muc.lib: Kick users from outcast hosts
daurnimator <quae@daurnimator.com>
parents: 6478
diff changeset
  1441
		) then
9619
66c6edf56397 MUC: Fix spelling in comments
Kim Alvefur <zash@zash.se>
parents: 9617
diff changeset
  1442
			-- need to publicize in all cases; as affiliation in <item/> has changed.
6187
c0b4b5d41e55 plugins/muc/muc.lib: Improve set affiliation logic;
daurnimator <quae@daurnimator.com>
parents: 6186
diff changeset
  1443
			occupants_updated[occupant] = occupant.role;
c0b4b5d41e55 plugins/muc/muc.lib: Improve set affiliation logic;
daurnimator <quae@daurnimator.com>
parents: 6186
diff changeset
  1444
			if occupant.role ~= role and (
c0b4b5d41e55 plugins/muc/muc.lib: Improve set affiliation logic;
daurnimator <quae@daurnimator.com>
parents: 6186
diff changeset
  1445
				is_downgrade or
c0b4b5d41e55 plugins/muc/muc.lib: Improve set affiliation logic;
daurnimator <quae@daurnimator.com>
parents: 6186
diff changeset
  1446
				valid_roles[occupant.role or "none"] < role_rank -- upgrade
c0b4b5d41e55 plugins/muc/muc.lib: Improve set affiliation logic;
daurnimator <quae@daurnimator.com>
parents: 6186
diff changeset
  1447
			) then
c0b4b5d41e55 plugins/muc/muc.lib: Improve set affiliation logic;
daurnimator <quae@daurnimator.com>
parents: 6186
diff changeset
  1448
				occupant.role = role;
c0b4b5d41e55 plugins/muc/muc.lib: Improve set affiliation logic;
daurnimator <quae@daurnimator.com>
parents: 6186
diff changeset
  1449
				self:save_occupant(occupant);
4357
d6928b78c548 MUC: Allow affiliation change when argument actor==true in room:set_affiliation().
Waqas Hussain <waqas20@gmail.com>
parents: 4326
diff changeset
  1450
			end
4202
dff7df4a191b MUC: Don't limit affiliation changes to owners, and allow owners to remove themselves if they are not the last owner.
Waqas Hussain <waqas20@gmail.com>
parents: 4201
diff changeset
  1451
		end
dff7df4a191b MUC: Don't limit affiliation changes to owners, and allow owners to remove themselves if they are not the last owner.
Waqas Hussain <waqas20@gmail.com>
parents: 4201
diff changeset
  1452
	end
8795
c2b99fa134b3 MUC: Import revised, more comprehensive patch for 8da11142fabf (#345)
Matthew Wild <mwild1@gmail.com>
parents: 8794
diff changeset
  1453
6187
c0b4b5d41e55 plugins/muc/muc.lib: Improve set affiliation logic;
daurnimator <quae@daurnimator.com>
parents: 6186
diff changeset
  1454
	-- Tell the room of the new occupant affiliations+roles
6179
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
  1455
	local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user"});
8845
463505cc75d5 MUC: Revert unstable MUC commits since 0.10.1
Matthew Wild <mwild1@gmail.com>
parents: 8842
diff changeset
  1456
	if not role then -- getting kicked
463505cc75d5 MUC: Revert unstable MUC commits since 0.10.1
Matthew Wild <mwild1@gmail.com>
parents: 8842
diff changeset
  1457
		if affiliation == "outcast" then
463505cc75d5 MUC: Revert unstable MUC commits since 0.10.1
Matthew Wild <mwild1@gmail.com>
parents: 8842
diff changeset
  1458
			x:tag("status", {code="301"}):up(); -- banned
463505cc75d5 MUC: Revert unstable MUC commits since 0.10.1
Matthew Wild <mwild1@gmail.com>
parents: 8842
diff changeset
  1459
		else
463505cc75d5 MUC: Revert unstable MUC commits since 0.10.1
Matthew Wild <mwild1@gmail.com>
parents: 8842
diff changeset
  1460
			x:tag("status", {code="321"}):up(); -- affiliation change
463505cc75d5 MUC: Revert unstable MUC commits since 0.10.1
Matthew Wild <mwild1@gmail.com>
parents: 8842
diff changeset
  1461
		end
1742
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
  1462
	end
6187
c0b4b5d41e55 plugins/muc/muc.lib: Improve set affiliation logic;
daurnimator <quae@daurnimator.com>
parents: 6186
diff changeset
  1463
	local is_semi_anonymous = self:get_whois() == "moderators";
9603
5a2135964ed3 MUC: Announce affiliation changes for JIDs that are not in the room
Matthew Wild <mwild1@gmail.com>
parents: 9550
diff changeset
  1464
5a2135964ed3 MUC: Announce affiliation changes for JIDs that are not in the room
Matthew Wild <mwild1@gmail.com>
parents: 9550
diff changeset
  1465
	if next(occupants_updated) ~= nil then
5a2135964ed3 MUC: Announce affiliation changes for JIDs that are not in the room
Matthew Wild <mwild1@gmail.com>
parents: 9550
diff changeset
  1466
		for occupant, old_role in pairs(occupants_updated) do
10688
de607875d4bd MUC: Pass previous role to :publicise_occupant_status() whenever possible
Matthew Wild <mwild1@gmail.com>
parents: 10666
diff changeset
  1467
			self:publicise_occupant_status(occupant, x, nil, actor, reason, old_role);
9603
5a2135964ed3 MUC: Announce affiliation changes for JIDs that are not in the room
Matthew Wild <mwild1@gmail.com>
parents: 9550
diff changeset
  1468
			if occupant.role == nil then
10052
e5532fbdd163 MUC: Reflow event tables to improve readability
Kim Alvefur <zash@zash.se>
parents: 9866
diff changeset
  1469
				module:fire_event("muc-occupant-left", {
e5532fbdd163 MUC: Reflow event tables to improve readability
Kim Alvefur <zash@zash.se>
parents: 9866
diff changeset
  1470
						room = self;
e5532fbdd163 MUC: Reflow event tables to improve readability
Kim Alvefur <zash@zash.se>
parents: 9866
diff changeset
  1471
						nick = occupant.nick;
e5532fbdd163 MUC: Reflow event tables to improve readability
Kim Alvefur <zash@zash.se>
parents: 9866
diff changeset
  1472
						occupant = occupant;
e5532fbdd163 MUC: Reflow event tables to improve readability
Kim Alvefur <zash@zash.se>
parents: 9866
diff changeset
  1473
					});
9603
5a2135964ed3 MUC: Announce affiliation changes for JIDs that are not in the room
Matthew Wild <mwild1@gmail.com>
parents: 9550
diff changeset
  1474
			elseif is_semi_anonymous and
11239
1dba335eacea MUC: Fix logic bug causing unnecessary presence to be sent, fixes #1615 (thanks damencho)
Matthew Wild <mwild1@gmail.com>
parents: 11146
diff changeset
  1475
				((old_role == "moderator" and occupant.role ~= "moderator") or
1dba335eacea MUC: Fix logic bug causing unnecessary presence to be sent, fixes #1615 (thanks damencho)
Matthew Wild <mwild1@gmail.com>
parents: 11146
diff changeset
  1476
				(old_role ~= "moderator" and occupant.role == "moderator")) then -- Has gained or lost moderator status
9603
5a2135964ed3 MUC: Announce affiliation changes for JIDs that are not in the room
Matthew Wild <mwild1@gmail.com>
parents: 9550
diff changeset
  1477
				-- Send everyone else's presences (as jid visibility has changed)
5a2135964ed3 MUC: Announce affiliation changes for JIDs that are not in the room
Matthew Wild <mwild1@gmail.com>
parents: 9550
diff changeset
  1478
				for real_jid in occupant:each_session() do
5a2135964ed3 MUC: Announce affiliation changes for JIDs that are not in the room
Matthew Wild <mwild1@gmail.com>
parents: 9550
diff changeset
  1479
					self:send_occupant_list(real_jid, function(occupant_jid, occupant) --luacheck: ignore 212 433
10691
8c2c5b4fde32 MUC: Support for broadcasting unavailable presence for affiliated offline users
Matthew Wild <mwild1@gmail.com>
parents: 10690
diff changeset
  1480
						return (not occupant) or occupant.bare_jid ~= jid;
9603
5a2135964ed3 MUC: Announce affiliation changes for JIDs that are not in the room
Matthew Wild <mwild1@gmail.com>
parents: 9550
diff changeset
  1481
					end);
5a2135964ed3 MUC: Announce affiliation changes for JIDs that are not in the room
Matthew Wild <mwild1@gmail.com>
parents: 9550
diff changeset
  1482
				end
1742
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
  1483
			end
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
  1484
		end
9603
5a2135964ed3 MUC: Announce affiliation changes for JIDs that are not in the room
Matthew Wild <mwild1@gmail.com>
parents: 9550
diff changeset
  1485
	else
5a2135964ed3 MUC: Announce affiliation changes for JIDs that are not in the room
Matthew Wild <mwild1@gmail.com>
parents: 9550
diff changeset
  1486
		-- Announce affiliation change for a user that is not currently in the room,
5a2135964ed3 MUC: Announce affiliation changes for JIDs that are not in the room
Matthew Wild <mwild1@gmail.com>
parents: 9550
diff changeset
  1487
		-- XEP-0045 (v1.31.2) example 195
5a2135964ed3 MUC: Announce affiliation changes for JIDs that are not in the room
Matthew Wild <mwild1@gmail.com>
parents: 9550
diff changeset
  1488
		-- add_item(x, affiliation, role, jid, nick, actor_nick, actor_jid, reason)
5a2135964ed3 MUC: Announce affiliation changes for JIDs that are not in the room
Matthew Wild <mwild1@gmail.com>
parents: 9550
diff changeset
  1489
		local announce_msg = st.message({ from = self.jid })
5a2135964ed3 MUC: Announce affiliation changes for JIDs that are not in the room
Matthew Wild <mwild1@gmail.com>
parents: 9550
diff changeset
  1490
			:add_child(add_item(st.clone(x), affiliation, nil, jid, nil, nil, nil, reason));
5a2135964ed3 MUC: Announce affiliation changes for JIDs that are not in the room
Matthew Wild <mwild1@gmail.com>
parents: 9550
diff changeset
  1491
		local min_role = is_semi_anonymous and "moderator" or "none";
5a2135964ed3 MUC: Announce affiliation changes for JIDs that are not in the room
Matthew Wild <mwild1@gmail.com>
parents: 9550
diff changeset
  1492
		self:broadcast(announce_msg, muc_util.only_with_min_role(min_role));
1742
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
  1493
	end
6187
c0b4b5d41e55 plugins/muc/muc.lib: Improve set affiliation logic;
daurnimator <quae@daurnimator.com>
parents: 6186
diff changeset
  1494
7417
1b62c89014c4 MUC: Separate force-save parameter from save-entire-state flag
Kim Alvefur <zash@zash.se>
parents: 7416
diff changeset
  1495
	self:save(true);
8795
c2b99fa134b3 MUC: Import revised, more comprehensive patch for 8da11142fabf (#345)
Matthew Wild <mwild1@gmail.com>
parents: 8794
diff changeset
  1496
6393
fff6ca13cb0f plugins/muc/muc.lib: Add muc-set-affiliation event
daurnimator <quae@daurnimator.com>
parents: 6392
diff changeset
  1497
	module:fire_event("muc-set-affiliation", {
fff6ca13cb0f plugins/muc/muc.lib: Add muc-set-affiliation event
daurnimator <quae@daurnimator.com>
parents: 6392
diff changeset
  1498
		room = self;
fff6ca13cb0f plugins/muc/muc.lib: Add muc-set-affiliation event
daurnimator <quae@daurnimator.com>
parents: 6392
diff changeset
  1499
		actor = actor;
fff6ca13cb0f plugins/muc/muc.lib: Add muc-set-affiliation event
daurnimator <quae@daurnimator.com>
parents: 6392
diff changeset
  1500
		jid = jid;
fff6ca13cb0f plugins/muc/muc.lib: Add muc-set-affiliation event
daurnimator <quae@daurnimator.com>
parents: 6392
diff changeset
  1501
		affiliation = affiliation or "none";
fff6ca13cb0f plugins/muc/muc.lib: Add muc-set-affiliation event
daurnimator <quae@daurnimator.com>
parents: 6392
diff changeset
  1502
		reason = reason;
fff6ca13cb0f plugins/muc/muc.lib: Add muc-set-affiliation event
daurnimator <quae@daurnimator.com>
parents: 6392
diff changeset
  1503
		previous_affiliation = target_affiliation;
9240
b86c2e135797 MUC: Add support for storing additional data with MUC affiliations
Matthew Wild <mwild1@gmail.com>
parents: 9197
diff changeset
  1504
		data = data and data or nil; -- coerce false to nil
6393
fff6ca13cb0f plugins/muc/muc.lib: Add muc-set-affiliation event
daurnimator <quae@daurnimator.com>
parents: 6392
diff changeset
  1505
		in_room = next(occupants_updated) ~= nil;
fff6ca13cb0f plugins/muc/muc.lib: Add muc-set-affiliation event
daurnimator <quae@daurnimator.com>
parents: 6392
diff changeset
  1506
	});
8795
c2b99fa134b3 MUC: Import revised, more comprehensive patch for 8da11142fabf (#345)
Matthew Wild <mwild1@gmail.com>
parents: 8794
diff changeset
  1507
1742
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
  1508
	return true;
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
  1509
end
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
  1510
9535
9bb33edd7255 MUC: Add function to retrieve affiliation data for a given JID
Matthew Wild <mwild1@gmail.com>
parents: 9534
diff changeset
  1511
function room_mt:get_affiliation_data(jid, key)
9bb33edd7255 MUC: Add function to retrieve affiliation data for a given JID
Matthew Wild <mwild1@gmail.com>
parents: 9534
diff changeset
  1512
	local data = self._affiliation_data[jid];
9bb33edd7255 MUC: Add function to retrieve affiliation data for a given JID
Matthew Wild <mwild1@gmail.com>
parents: 9534
diff changeset
  1513
	if not data then return nil; end
9bb33edd7255 MUC: Add function to retrieve affiliation data for a given JID
Matthew Wild <mwild1@gmail.com>
parents: 9534
diff changeset
  1514
	if key then
9bb33edd7255 MUC: Add function to retrieve affiliation data for a given JID
Matthew Wild <mwild1@gmail.com>
parents: 9534
diff changeset
  1515
		return data[key];
9bb33edd7255 MUC: Add function to retrieve affiliation data for a given JID
Matthew Wild <mwild1@gmail.com>
parents: 9534
diff changeset
  1516
	end
9bb33edd7255 MUC: Add function to retrieve affiliation data for a given JID
Matthew Wild <mwild1@gmail.com>
parents: 9534
diff changeset
  1517
	return data;
9bb33edd7255 MUC: Add function to retrieve affiliation data for a given JID
Matthew Wild <mwild1@gmail.com>
parents: 9534
diff changeset
  1518
end
9bb33edd7255 MUC: Add function to retrieve affiliation data for a given JID
Matthew Wild <mwild1@gmail.com>
parents: 9534
diff changeset
  1519
1742
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
  1520
function room_mt:get_role(nick)
6179
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
  1521
	local occupant = self:get_occupant_by_nick(nick);
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
  1522
	return occupant and occupant.role or nil;
1742
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
  1523
end
6182
dbf0b09664cd plugins/muc/muc.lib: Clean up :set_role. Removes :can_set_role
daurnimator <quae@daurnimator.com>
parents: 6181
diff changeset
  1524
9834
d935a0f0de24 MUC: Factor out role change permission check into its own method
Kim Alvefur <zash@zash.se>
parents: 9824
diff changeset
  1525
function room_mt:may_set_role(actor, occupant, role)
9835
43f81e85cec2 MUC: Fire an event to allow affecting decision of whether to allow a role change
Kim Alvefur <zash@zash.se>
parents: 9834
diff changeset
  1526
	local event = {
43f81e85cec2 MUC: Fire an event to allow affecting decision of whether to allow a role change
Kim Alvefur <zash@zash.se>
parents: 9834
diff changeset
  1527
		room = self,
43f81e85cec2 MUC: Fire an event to allow affecting decision of whether to allow a role change
Kim Alvefur <zash@zash.se>
parents: 9834
diff changeset
  1528
		actor = actor,
43f81e85cec2 MUC: Fire an event to allow affecting decision of whether to allow a role change
Kim Alvefur <zash@zash.se>
parents: 9834
diff changeset
  1529
		occupant = occupant,
43f81e85cec2 MUC: Fire an event to allow affecting decision of whether to allow a role change
Kim Alvefur <zash@zash.se>
parents: 9834
diff changeset
  1530
		role = role,
43f81e85cec2 MUC: Fire an event to allow affecting decision of whether to allow a role change
Kim Alvefur <zash@zash.se>
parents: 9834
diff changeset
  1531
	};
43f81e85cec2 MUC: Fire an event to allow affecting decision of whether to allow a role change
Kim Alvefur <zash@zash.se>
parents: 9834
diff changeset
  1532
43f81e85cec2 MUC: Fire an event to allow affecting decision of whether to allow a role change
Kim Alvefur <zash@zash.se>
parents: 9834
diff changeset
  1533
	module:fire_event("muc-pre-set-role", event);
43f81e85cec2 MUC: Fire an event to allow affecting decision of whether to allow a role change
Kim Alvefur <zash@zash.se>
parents: 9834
diff changeset
  1534
	if event.allowed ~= nil then
43f81e85cec2 MUC: Fire an event to allow affecting decision of whether to allow a role change
Kim Alvefur <zash@zash.se>
parents: 9834
diff changeset
  1535
		return event.allowed, event.error, event.condition;
43f81e85cec2 MUC: Fire an event to allow affecting decision of whether to allow a role change
Kim Alvefur <zash@zash.se>
parents: 9834
diff changeset
  1536
	end
43f81e85cec2 MUC: Fire an event to allow affecting decision of whether to allow a role change
Kim Alvefur <zash@zash.se>
parents: 9834
diff changeset
  1537
9834
d935a0f0de24 MUC: Factor out role change permission check into its own method
Kim Alvefur <zash@zash.se>
parents: 9824
diff changeset
  1538
	-- Can't do anything to other owners or admins
d935a0f0de24 MUC: Factor out role change permission check into its own method
Kim Alvefur <zash@zash.se>
parents: 9824
diff changeset
  1539
	local occupant_affiliation = self:get_affiliation(occupant.bare_jid);
d935a0f0de24 MUC: Factor out role change permission check into its own method
Kim Alvefur <zash@zash.se>
parents: 9824
diff changeset
  1540
	if occupant_affiliation == "owner" or occupant_affiliation == "admin" then
d935a0f0de24 MUC: Factor out role change permission check into its own method
Kim Alvefur <zash@zash.se>
parents: 9824
diff changeset
  1541
		return nil, "cancel", "not-allowed";
d935a0f0de24 MUC: Factor out role change permission check into its own method
Kim Alvefur <zash@zash.se>
parents: 9824
diff changeset
  1542
	end
d935a0f0de24 MUC: Factor out role change permission check into its own method
Kim Alvefur <zash@zash.se>
parents: 9824
diff changeset
  1543
d935a0f0de24 MUC: Factor out role change permission check into its own method
Kim Alvefur <zash@zash.se>
parents: 9824
diff changeset
  1544
	-- If you are trying to give or take moderator role you need to be an owner or admin
d935a0f0de24 MUC: Factor out role change permission check into its own method
Kim Alvefur <zash@zash.se>
parents: 9824
diff changeset
  1545
	if occupant.role == "moderator" or role == "moderator" then
d935a0f0de24 MUC: Factor out role change permission check into its own method
Kim Alvefur <zash@zash.se>
parents: 9824
diff changeset
  1546
		local actor_affiliation = self:get_affiliation(actor);
d935a0f0de24 MUC: Factor out role change permission check into its own method
Kim Alvefur <zash@zash.se>
parents: 9824
diff changeset
  1547
		if actor_affiliation ~= "owner" and actor_affiliation ~= "admin" then
d935a0f0de24 MUC: Factor out role change permission check into its own method
Kim Alvefur <zash@zash.se>
parents: 9824
diff changeset
  1548
			return nil, "cancel", "not-allowed";
d935a0f0de24 MUC: Factor out role change permission check into its own method
Kim Alvefur <zash@zash.se>
parents: 9824
diff changeset
  1549
		end
d935a0f0de24 MUC: Factor out role change permission check into its own method
Kim Alvefur <zash@zash.se>
parents: 9824
diff changeset
  1550
	end
d935a0f0de24 MUC: Factor out role change permission check into its own method
Kim Alvefur <zash@zash.se>
parents: 9824
diff changeset
  1551
d935a0f0de24 MUC: Factor out role change permission check into its own method
Kim Alvefur <zash@zash.se>
parents: 9824
diff changeset
  1552
	-- Need to be in the room and a moderator
d935a0f0de24 MUC: Factor out role change permission check into its own method
Kim Alvefur <zash@zash.se>
parents: 9824
diff changeset
  1553
	local actor_occupant = self:get_occupant_by_real_jid(actor);
d935a0f0de24 MUC: Factor out role change permission check into its own method
Kim Alvefur <zash@zash.se>
parents: 9824
diff changeset
  1554
	if not actor_occupant or actor_occupant.role ~= "moderator" then
d935a0f0de24 MUC: Factor out role change permission check into its own method
Kim Alvefur <zash@zash.se>
parents: 9824
diff changeset
  1555
		return nil, "cancel", "not-allowed";
d935a0f0de24 MUC: Factor out role change permission check into its own method
Kim Alvefur <zash@zash.se>
parents: 9824
diff changeset
  1556
	end
d935a0f0de24 MUC: Factor out role change permission check into its own method
Kim Alvefur <zash@zash.se>
parents: 9824
diff changeset
  1557
d935a0f0de24 MUC: Factor out role change permission check into its own method
Kim Alvefur <zash@zash.se>
parents: 9824
diff changeset
  1558
	return true;
d935a0f0de24 MUC: Factor out role change permission check into its own method
Kim Alvefur <zash@zash.se>
parents: 9824
diff changeset
  1559
end
d935a0f0de24 MUC: Factor out role change permission check into its own method
Kim Alvefur <zash@zash.se>
parents: 9824
diff changeset
  1560
6182
dbf0b09664cd plugins/muc/muc.lib: Clean up :set_role. Removes :can_set_role
daurnimator <quae@daurnimator.com>
parents: 6181
diff changeset
  1561
function room_mt:set_role(actor, occupant_jid, role, reason)
dbf0b09664cd plugins/muc/muc.lib: Clean up :set_role. Removes :can_set_role
daurnimator <quae@daurnimator.com>
parents: 6181
diff changeset
  1562
	if not actor then return nil, "modify", "not-acceptable"; end
3279
8b0a4a7d2c6e MUC: Added room:can_set_role().
Waqas Hussain <waqas20@gmail.com>
parents: 3264
diff changeset
  1563
6179
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
  1564
	local occupant = self:get_occupant_by_nick(occupant_jid);
7399
71a7140200fc MUC: Return item-not-found as error when attempting to change role of non-existant occupant
Kim Alvefur <zash@zash.se>
parents: 7389
diff changeset
  1565
	if not occupant then return nil, "modify", "item-not-found"; end
5542
329ebdfb39a2 MUC: Allow actor == true to set roles (like affiliations)
Matthew Wild <mwild1@gmail.com>
parents: 5541
diff changeset
  1566
6182
dbf0b09664cd plugins/muc/muc.lib: Clean up :set_role. Removes :can_set_role
daurnimator <quae@daurnimator.com>
parents: 6181
diff changeset
  1567
	if valid_roles[role or "none"] == nil then
dbf0b09664cd plugins/muc/muc.lib: Clean up :set_role. Removes :can_set_role
daurnimator <quae@daurnimator.com>
parents: 6181
diff changeset
  1568
		return nil, "modify", "not-acceptable";
dbf0b09664cd plugins/muc/muc.lib: Clean up :set_role. Removes :can_set_role
daurnimator <quae@daurnimator.com>
parents: 6181
diff changeset
  1569
	end
dbf0b09664cd plugins/muc/muc.lib: Clean up :set_role. Removes :can_set_role
daurnimator <quae@daurnimator.com>
parents: 6181
diff changeset
  1570
	role = role ~= "none" and role or nil; -- coerces `role == false` to `nil`
5542
329ebdfb39a2 MUC: Allow actor == true to set roles (like affiliations)
Matthew Wild <mwild1@gmail.com>
parents: 5541
diff changeset
  1571
6478
3c815a64042b plugins/muc/muc.lib: Fix passing actor along as a boolean (thanks fippo)
daurnimator <quae@daurnimator.com>
parents: 6457
diff changeset
  1572
	if actor == true then
3c815a64042b plugins/muc/muc.lib: Fix passing actor along as a boolean (thanks fippo)
daurnimator <quae@daurnimator.com>
parents: 6457
diff changeset
  1573
		actor = nil -- So we can pass it safely to 'publicise_occupant_status' below
3c815a64042b plugins/muc/muc.lib: Fix passing actor along as a boolean (thanks fippo)
daurnimator <quae@daurnimator.com>
parents: 6457
diff changeset
  1574
	else
9834
d935a0f0de24 MUC: Factor out role change permission check into its own method
Kim Alvefur <zash@zash.se>
parents: 9824
diff changeset
  1575
		local allowed, err, condition = self:may_set_role(actor, occupant, role)
d935a0f0de24 MUC: Factor out role change permission check into its own method
Kim Alvefur <zash@zash.se>
parents: 9824
diff changeset
  1576
		if not allowed then
d935a0f0de24 MUC: Factor out role change permission check into its own method
Kim Alvefur <zash@zash.se>
parents: 9824
diff changeset
  1577
			return allowed, err, condition;
8795
c2b99fa134b3 MUC: Import revised, more comprehensive patch for 8da11142fabf (#345)
Matthew Wild <mwild1@gmail.com>
parents: 8794
diff changeset
  1578
		end
3279
8b0a4a7d2c6e MUC: Added room:can_set_role().
Waqas Hussain <waqas20@gmail.com>
parents: 3264
diff changeset
  1579
	end
8795
c2b99fa134b3 MUC: Import revised, more comprehensive patch for 8da11142fabf (#345)
Matthew Wild <mwild1@gmail.com>
parents: 8794
diff changeset
  1580
6179
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
  1581
	local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user"});
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
  1582
	if not role then
8845
463505cc75d5 MUC: Revert unstable MUC commits since 0.10.1
Matthew Wild <mwild1@gmail.com>
parents: 8842
diff changeset
  1583
		x:tag("status", {code = "307"}):up();
463505cc75d5 MUC: Revert unstable MUC commits since 0.10.1
Matthew Wild <mwild1@gmail.com>
parents: 8842
diff changeset
  1584
	end
10357
7b602e13c3b6 MUC: Add controls for whose presence is broadcast (closes #1335)
Lance Stout <lancestout@gmail.com>
parents: 10298
diff changeset
  1585
7b602e13c3b6 MUC: Add controls for whose presence is broadcast (closes #1335)
Lance Stout <lancestout@gmail.com>
parents: 10298
diff changeset
  1586
	local prev_role = occupant.role;
6179
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
  1587
	occupant.role = role;
e488a90195bc plugins/muc: Massive refactor
daurnimator <quae@daurnimator.com>
parents: 6143
diff changeset
  1588
	self:save_occupant(occupant);
10357
7b602e13c3b6 MUC: Add controls for whose presence is broadcast (closes #1335)
Lance Stout <lancestout@gmail.com>
parents: 10298
diff changeset
  1589
	self:publicise_occupant_status(occupant, x, nil, actor, reason, prev_role);
6457
6842b07fc7bc plugins/muc/muc.lib: Fire muc-occupant-left from other places an occupant may leave the room
daurnimator <quae@daurnimator.com>
parents: 6456
diff changeset
  1590
	if role == nil then
10052
e5532fbdd163 MUC: Reflow event tables to improve readability
Kim Alvefur <zash@zash.se>
parents: 9866
diff changeset
  1591
		module:fire_event("muc-occupant-left", {
e5532fbdd163 MUC: Reflow event tables to improve readability
Kim Alvefur <zash@zash.se>
parents: 9866
diff changeset
  1592
				room = self;
e5532fbdd163 MUC: Reflow event tables to improve readability
Kim Alvefur <zash@zash.se>
parents: 9866
diff changeset
  1593
				nick = occupant.nick;
e5532fbdd163 MUC: Reflow event tables to improve readability
Kim Alvefur <zash@zash.se>
parents: 9866
diff changeset
  1594
				occupant = occupant;
e5532fbdd163 MUC: Reflow event tables to improve readability
Kim Alvefur <zash@zash.se>
parents: 9866
diff changeset
  1595
			});
3632
d82189efecc0 MUC: Include the user's current presence contents when broadcasting a role change.
Waqas Hussain <waqas20@gmail.com>
parents: 3631
diff changeset
  1596
	end
1737
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
  1597
	return true;
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
  1598
end
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
  1599
6214
9813c74ce006 plugins/muc: Move `whois` code to seperate file
daurnimator <quae@daurnimator.com>
parents: 6213
diff changeset
  1600
local whois = module:require "muc/whois";
9813c74ce006 plugins/muc: Move `whois` code to seperate file
daurnimator <quae@daurnimator.com>
parents: 6213
diff changeset
  1601
room_mt.get_whois = whois.get;
9813c74ce006 plugins/muc: Move `whois` code to seperate file
daurnimator <quae@daurnimator.com>
parents: 6213
diff changeset
  1602
room_mt.set_whois = whois.set;
2064
1ee862fd1afe MUC: Include occupants' real JIDs in their presence (semi-anonymous rooms).
Waqas Hussain <waqas20@gmail.com>
parents: 2053
diff changeset
  1603
1737
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
  1604
local _M = {}; -- module "muc"
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
  1605
3330
bdc325ce9fbc MUC: Make number of stored history messages configurable with option max_history_messages (thanks michal and others who requested)
Matthew Wild <mwild1@gmail.com>
parents: 3281
diff changeset
  1606
function _M.new_room(jid, config)
1735
81406277279e MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 1734
diff changeset
  1607
	return setmetatable({
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
  1608
		jid = jid;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
  1609
		_jid_nick = {};
1739
393abf245322 MUC: Renamed _participants table to _occupants
Waqas Hussain <waqas20@gmail.com>
parents: 1737
diff changeset
  1610
		_occupants = {};
7365
032fcb7b80a1 MUC: Use config passed to rew_roow()
Kim Alvefur <zash@zash.se>
parents: 7363
diff changeset
  1611
		_data = config or {};
1737
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
  1612
		_affiliations = {};
9240
b86c2e135797 MUC: Add support for storing additional data with MUC affiliations
Matthew Wild <mwild1@gmail.com>
parents: 9197
diff changeset
  1613
		_affiliation_data = {};
1735
81406277279e MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 1734
diff changeset
  1614
	}, room_mt);
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
  1615
end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
  1616
10692
83668e16b9a3 MUC: Switch to new storage format by default
Matthew Wild <mwild1@gmail.com>
parents: 10691
diff changeset
  1617
local new_format = module:get_option_boolean("new_muc_storage_format", true);
7491
cdabf8199903 MUC: Hide new MUC room storage format behind an off-by-default option
Kim Alvefur <zash@zash.se>
parents: 7447
diff changeset
  1618
7372
c5cae59831d7 MUC: Add support for serializing live rooms, including occupants and their presence
Kim Alvefur <zash@zash.se>
parents: 7371
diff changeset
  1619
function room_mt:freeze(live)
7491
cdabf8199903 MUC: Hide new MUC room storage format behind an off-by-default option
Kim Alvefur <zash@zash.se>
parents: 7447
diff changeset
  1620
	local frozen, state;
cdabf8199903 MUC: Hide new MUC room storage format behind an off-by-default option
Kim Alvefur <zash@zash.se>
parents: 7447
diff changeset
  1621
	if new_format then
cdabf8199903 MUC: Hide new MUC room storage format behind an off-by-default option
Kim Alvefur <zash@zash.se>
parents: 7447
diff changeset
  1622
		frozen = {
cdabf8199903 MUC: Hide new MUC room storage format behind an off-by-default option
Kim Alvefur <zash@zash.se>
parents: 7447
diff changeset
  1623
			_jid = self.jid;
cdabf8199903 MUC: Hide new MUC room storage format behind an off-by-default option
Kim Alvefur <zash@zash.se>
parents: 7447
diff changeset
  1624
			_data = self._data;
10694
27d15097c235 MUC: Persist affiliation_data in new MUC format!
Matthew Wild <mwild1@gmail.com>
parents: 10435
diff changeset
  1625
			_affiliation_data = self._affiliation_data;
7491
cdabf8199903 MUC: Hide new MUC room storage format behind an off-by-default option
Kim Alvefur <zash@zash.se>
parents: 7447
diff changeset
  1626
		};
cdabf8199903 MUC: Hide new MUC room storage format behind an off-by-default option
Kim Alvefur <zash@zash.se>
parents: 7447
diff changeset
  1627
		for user, affiliation in pairs(self._affiliations) do
cdabf8199903 MUC: Hide new MUC room storage format behind an off-by-default option
Kim Alvefur <zash@zash.se>
parents: 7447
diff changeset
  1628
			frozen[user] = affiliation;
cdabf8199903 MUC: Hide new MUC room storage format behind an off-by-default option
Kim Alvefur <zash@zash.se>
parents: 7447
diff changeset
  1629
		end
cdabf8199903 MUC: Hide new MUC room storage format behind an off-by-default option
Kim Alvefur <zash@zash.se>
parents: 7447
diff changeset
  1630
	else
cdabf8199903 MUC: Hide new MUC room storage format behind an off-by-default option
Kim Alvefur <zash@zash.se>
parents: 7447
diff changeset
  1631
		frozen = {
cdabf8199903 MUC: Hide new MUC room storage format behind an off-by-default option
Kim Alvefur <zash@zash.se>
parents: 7447
diff changeset
  1632
			jid = self.jid;
cdabf8199903 MUC: Hide new MUC room storage format behind an off-by-default option
Kim Alvefur <zash@zash.se>
parents: 7447
diff changeset
  1633
			_data = self._data;
cdabf8199903 MUC: Hide new MUC room storage format behind an off-by-default option
Kim Alvefur <zash@zash.se>
parents: 7447
diff changeset
  1634
			_affiliations = self._affiliations;
9240
b86c2e135797 MUC: Add support for storing additional data with MUC affiliations
Matthew Wild <mwild1@gmail.com>
parents: 9197
diff changeset
  1635
			_affiliation_data = self._affiliation_data;
7491
cdabf8199903 MUC: Hide new MUC room storage format behind an off-by-default option
Kim Alvefur <zash@zash.se>
parents: 7447
diff changeset
  1636
		};
7371
4e24aff1e4df MUC: Flatten format of serialized rooms
Kim Alvefur <zash@zash.se>
parents: 7370
diff changeset
  1637
	end
7372
c5cae59831d7 MUC: Add support for serializing live rooms, including occupants and their presence
Kim Alvefur <zash@zash.se>
parents: 7371
diff changeset
  1638
	if live then
7418
cbb05b454c13 MUC: Separate config from live state
Kim Alvefur <zash@zash.se>
parents: 7417
diff changeset
  1639
		state = {};
7372
c5cae59831d7 MUC: Add support for serializing live rooms, including occupants and their presence
Kim Alvefur <zash@zash.se>
parents: 7371
diff changeset
  1640
		for nick, occupant in self:each_occupant() do
7418
cbb05b454c13 MUC: Separate config from live state
Kim Alvefur <zash@zash.se>
parents: 7417
diff changeset
  1641
			state[nick] = {
7372
c5cae59831d7 MUC: Add support for serializing live rooms, including occupants and their presence
Kim Alvefur <zash@zash.se>
parents: 7371
diff changeset
  1642
				bare_jid = occupant.bare_jid;
c5cae59831d7 MUC: Add support for serializing live rooms, including occupants and their presence
Kim Alvefur <zash@zash.se>
parents: 7371
diff changeset
  1643
				role = occupant.role;
c5cae59831d7 MUC: Add support for serializing live rooms, including occupants and their presence
Kim Alvefur <zash@zash.se>
parents: 7371
diff changeset
  1644
				jid = occupant.jid;
c5cae59831d7 MUC: Add support for serializing live rooms, including occupants and their presence
Kim Alvefur <zash@zash.se>
parents: 7371
diff changeset
  1645
			}
c5cae59831d7 MUC: Add support for serializing live rooms, including occupants and their presence
Kim Alvefur <zash@zash.se>
parents: 7371
diff changeset
  1646
			for jid, presence in occupant:each_session() do
7418
cbb05b454c13 MUC: Separate config from live state
Kim Alvefur <zash@zash.se>
parents: 7417
diff changeset
  1647
				state[jid] = st.preserialize(presence);
7372
c5cae59831d7 MUC: Add support for serializing live rooms, including occupants and their presence
Kim Alvefur <zash@zash.se>
parents: 7371
diff changeset
  1648
			end
c5cae59831d7 MUC: Add support for serializing live rooms, including occupants and their presence
Kim Alvefur <zash@zash.se>
parents: 7371
diff changeset
  1649
		end
7415
f9744effae04 MUC: Include the very last message in serialized form to keep it across eviction and restore
Kim Alvefur <zash@zash.se>
parents: 7414
diff changeset
  1650
		local history = self._history;
8522
e7808f8d7d11 MUC: Prevent traceback in case of no history items to serialize (fixes #1083)
Kim Alvefur <zash@zash.se>
parents: 8489
diff changeset
  1651
		if history and history[1] ~= nil then
7418
cbb05b454c13 MUC: Separate config from live state
Kim Alvefur <zash@zash.se>
parents: 7417
diff changeset
  1652
			state._last_message = st.preserialize(history[#history].stanza);
cbb05b454c13 MUC: Separate config from live state
Kim Alvefur <zash@zash.se>
parents: 7417
diff changeset
  1653
			state._last_message_at = history[#history].timestamp;
7415
f9744effae04 MUC: Include the very last message in serialized form to keep it across eviction and restore
Kim Alvefur <zash@zash.se>
parents: 7414
diff changeset
  1654
		end
7372
c5cae59831d7 MUC: Add support for serializing live rooms, including occupants and their presence
Kim Alvefur <zash@zash.se>
parents: 7371
diff changeset
  1655
	end
7418
cbb05b454c13 MUC: Separate config from live state
Kim Alvefur <zash@zash.se>
parents: 7417
diff changeset
  1656
	return frozen, state;
7363
7a37fade5380 MUC: Move 'preserialization' step to muc.lib
Kim Alvefur <zash@zash.se>
parents: 7356
diff changeset
  1657
end
7a37fade5380 MUC: Move 'preserialization' step to muc.lib
Kim Alvefur <zash@zash.se>
parents: 7356
diff changeset
  1658
7418
cbb05b454c13 MUC: Separate config from live state
Kim Alvefur <zash@zash.se>
parents: 7417
diff changeset
  1659
function _M.restore_room(frozen, state)
7634
f50c039d6bb1 MUC: Fix compatibility with new and old storage format
Kim Alvefur <zash@zash.se>
parents: 7491
diff changeset
  1660
	local room_jid = frozen._jid or frozen.jid;
7370
2aef5e8b69e9 MUC: Move room deserialization to muc.lib
Kim Alvefur <zash@zash.se>
parents: 7369
diff changeset
  1661
	local room = _M.new_room(room_jid, frozen._data);
7371
4e24aff1e4df MUC: Flatten format of serialized rooms
Kim Alvefur <zash@zash.se>
parents: 7370
diff changeset
  1662
7419
c33a1d6da016 MUC: Restore last message from state, not room config (missing change from cbb05b454c13)
Kim Alvefur <zash@zash.se>
parents: 7418
diff changeset
  1663
	if state and state._last_message and state._last_message_at then
7415
f9744effae04 MUC: Include the very last message in serialized form to keep it across eviction and restore
Kim Alvefur <zash@zash.se>
parents: 7414
diff changeset
  1664
		room._history = {
7419
c33a1d6da016 MUC: Restore last message from state, not room config (missing change from cbb05b454c13)
Kim Alvefur <zash@zash.se>
parents: 7418
diff changeset
  1665
			{ stanza = st.deserialize(state._last_message),
c33a1d6da016 MUC: Restore last message from state, not room config (missing change from cbb05b454c13)
Kim Alvefur <zash@zash.se>
parents: 7418
diff changeset
  1666
			  timestamp = state._last_message_at, },
7415
f9744effae04 MUC: Include the very last message in serialized form to keep it across eviction and restore
Kim Alvefur <zash@zash.se>
parents: 7414
diff changeset
  1667
		};
f9744effae04 MUC: Include the very last message in serialized form to keep it across eviction and restore
Kim Alvefur <zash@zash.se>
parents: 7414
diff changeset
  1668
	end
f9744effae04 MUC: Include the very last message in serialized form to keep it across eviction and restore
Kim Alvefur <zash@zash.se>
parents: 7414
diff changeset
  1669
7372
c5cae59831d7 MUC: Add support for serializing live rooms, including occupants and their presence
Kim Alvefur <zash@zash.se>
parents: 7371
diff changeset
  1670
	local occupants = {};
c5cae59831d7 MUC: Add support for serializing live rooms, including occupants and their presence
Kim Alvefur <zash@zash.se>
parents: 7371
diff changeset
  1671
	local room_name, room_host = jid_split(room_jid);
7634
f50c039d6bb1 MUC: Fix compatibility with new and old storage format
Kim Alvefur <zash@zash.se>
parents: 7491
diff changeset
  1672
9268
585ef5c1b226 MUC: Initialize room with empty affiliation_data if none stored
Matthew Wild <mwild1@gmail.com>
parents: 9267
diff changeset
  1673
	room._affiliation_data = frozen._affiliation_data or {};
9240
b86c2e135797 MUC: Add support for storing additional data with MUC affiliations
Matthew Wild <mwild1@gmail.com>
parents: 9197
diff changeset
  1674
7634
f50c039d6bb1 MUC: Fix compatibility with new and old storage format
Kim Alvefur <zash@zash.se>
parents: 7491
diff changeset
  1675
	if frozen.jid and frozen._affiliations then
9242
03e37f7d6c97 MUC: Add some comments for clarity
Matthew Wild <mwild1@gmail.com>
parents: 9241
diff changeset
  1676
		-- Old storage format
7634
f50c039d6bb1 MUC: Fix compatibility with new and old storage format
Kim Alvefur <zash@zash.se>
parents: 7491
diff changeset
  1677
		room._affiliations = frozen._affiliations;
f50c039d6bb1 MUC: Fix compatibility with new and old storage format
Kim Alvefur <zash@zash.se>
parents: 7491
diff changeset
  1678
	else
9242
03e37f7d6c97 MUC: Add some comments for clarity
Matthew Wild <mwild1@gmail.com>
parents: 9241
diff changeset
  1679
		-- New storage format
7634
f50c039d6bb1 MUC: Fix compatibility with new and old storage format
Kim Alvefur <zash@zash.se>
parents: 7491
diff changeset
  1680
		for jid, data in pairs(frozen) do
10554
0566b45da987 MUC: Remove some unused variables [luacheck]
Kim Alvefur <zash@zash.se>
parents: 10455
diff changeset
  1681
			local _, host, resource = jid_split(jid);
7634
f50c039d6bb1 MUC: Fix compatibility with new and old storage format
Kim Alvefur <zash@zash.se>
parents: 7491
diff changeset
  1682
			if host:sub(1,1) ~= "_" and not resource and type(data) == "string" then
f50c039d6bb1 MUC: Fix compatibility with new and old storage format
Kim Alvefur <zash@zash.se>
parents: 7491
diff changeset
  1683
				-- bare jid: affiliation
f50c039d6bb1 MUC: Fix compatibility with new and old storage format
Kim Alvefur <zash@zash.se>
parents: 7491
diff changeset
  1684
				room._affiliations[jid] = data;
f50c039d6bb1 MUC: Fix compatibility with new and old storage format
Kim Alvefur <zash@zash.se>
parents: 7491
diff changeset
  1685
			end
7418
cbb05b454c13 MUC: Separate config from live state
Kim Alvefur <zash@zash.se>
parents: 7417
diff changeset
  1686
		end
cbb05b454c13 MUC: Separate config from live state
Kim Alvefur <zash@zash.se>
parents: 7417
diff changeset
  1687
	end
cbb05b454c13 MUC: Separate config from live state
Kim Alvefur <zash@zash.se>
parents: 7417
diff changeset
  1688
	for jid, data in pairs(state or frozen) do
cbb05b454c13 MUC: Separate config from live state
Kim Alvefur <zash@zash.se>
parents: 7417
diff changeset
  1689
		local node, host, resource = jid_split(jid);
7371
4e24aff1e4df MUC: Flatten format of serialized rooms
Kim Alvefur <zash@zash.se>
parents: 7370
diff changeset
  1690
		if node or host:sub(1,1) ~= "_" then
7418
cbb05b454c13 MUC: Separate config from live state
Kim Alvefur <zash@zash.se>
parents: 7417
diff changeset
  1691
			if host == room_host and node == room_name and resource and type(data) == "table" then
7372
c5cae59831d7 MUC: Add support for serializing live rooms, including occupants and their presence
Kim Alvefur <zash@zash.se>
parents: 7371
diff changeset
  1692
				-- full room jid: bare real jid and role
7664
37ab6c6326fe MUC: Fix conflict when restoring room where the same bare JID has joined as multiple participants
Kim Alvefur <zash@zash.se>
parents: 7634
diff changeset
  1693
				local nick = jid;
37ab6c6326fe MUC: Fix conflict when restoring room where the same bare JID has joined as multiple participants
Kim Alvefur <zash@zash.se>
parents: 7634
diff changeset
  1694
				local occupant = occupants[nick] or occupant_lib.new(data.bare_jid, nick);
37ab6c6326fe MUC: Fix conflict when restoring room where the same bare JID has joined as multiple participants
Kim Alvefur <zash@zash.se>
parents: 7634
diff changeset
  1695
				occupant.bare_jid = data.bare_jid;
7372
c5cae59831d7 MUC: Add support for serializing live rooms, including occupants and their presence
Kim Alvefur <zash@zash.se>
parents: 7371
diff changeset
  1696
				occupant.role = data.role;
7664
37ab6c6326fe MUC: Fix conflict when restoring room where the same bare JID has joined as multiple participants
Kim Alvefur <zash@zash.se>
parents: 7634
diff changeset
  1697
				occupant.jid = data.jid; -- Primary session JID
37ab6c6326fe MUC: Fix conflict when restoring room where the same bare JID has joined as multiple participants
Kim Alvefur <zash@zash.se>
parents: 7634
diff changeset
  1698
				occupants[nick] = occupant;
37ab6c6326fe MUC: Fix conflict when restoring room where the same bare JID has joined as multiple participants
Kim Alvefur <zash@zash.se>
parents: 7634
diff changeset
  1699
			elseif type(data) == "table" and data.name == "presence" then
7372
c5cae59831d7 MUC: Add support for serializing live rooms, including occupants and their presence
Kim Alvefur <zash@zash.se>
parents: 7371
diff changeset
  1700
				-- full user jid: presence
7664
37ab6c6326fe MUC: Fix conflict when restoring room where the same bare JID has joined as multiple participants
Kim Alvefur <zash@zash.se>
parents: 7634
diff changeset
  1701
				local nick = data.attr.from;
37ab6c6326fe MUC: Fix conflict when restoring room where the same bare JID has joined as multiple participants
Kim Alvefur <zash@zash.se>
parents: 7634
diff changeset
  1702
				local occupant = occupants[nick] or occupant_lib.new(nil, nick);
7372
c5cae59831d7 MUC: Add support for serializing live rooms, including occupants and their presence
Kim Alvefur <zash@zash.se>
parents: 7371
diff changeset
  1703
				local presence = st.deserialize(data);
7664
37ab6c6326fe MUC: Fix conflict when restoring room where the same bare JID has joined as multiple participants
Kim Alvefur <zash@zash.se>
parents: 7634
diff changeset
  1704
				occupant:set_session(jid, presence);
37ab6c6326fe MUC: Fix conflict when restoring room where the same bare JID has joined as multiple participants
Kim Alvefur <zash@zash.se>
parents: 7634
diff changeset
  1705
				occupants[nick] = occupant;
7372
c5cae59831d7 MUC: Add support for serializing live rooms, including occupants and their presence
Kim Alvefur <zash@zash.se>
parents: 7371
diff changeset
  1706
			end
7371
4e24aff1e4df MUC: Flatten format of serialized rooms
Kim Alvefur <zash@zash.se>
parents: 7370
diff changeset
  1707
		end
4e24aff1e4df MUC: Flatten format of serialized rooms
Kim Alvefur <zash@zash.se>
parents: 7370
diff changeset
  1708
	end
7372
c5cae59831d7 MUC: Add support for serializing live rooms, including occupants and their presence
Kim Alvefur <zash@zash.se>
parents: 7371
diff changeset
  1709
c5cae59831d7 MUC: Add support for serializing live rooms, including occupants and their presence
Kim Alvefur <zash@zash.se>
parents: 7371
diff changeset
  1710
	for _, occupant in pairs(occupants) do
c5cae59831d7 MUC: Add support for serializing live rooms, including occupants and their presence
Kim Alvefur <zash@zash.se>
parents: 7371
diff changeset
  1711
		room:save_occupant(occupant);
c5cae59831d7 MUC: Add support for serializing live rooms, including occupants and their presence
Kim Alvefur <zash@zash.se>
parents: 7371
diff changeset
  1712
	end
c5cae59831d7 MUC: Add support for serializing live rooms, including occupants and their presence
Kim Alvefur <zash@zash.se>
parents: 7371
diff changeset
  1713
7370
2aef5e8b69e9 MUC: Move room deserialization to muc.lib
Kim Alvefur <zash@zash.se>
parents: 7369
diff changeset
  1714
	return room;
5195
ce5d7538ac48 muc: Make max_history_messages simply a service-wide config option, and don't store it per-room (rooms still have their own history_message, but this is a global limit)
Matthew Wild <mwild1@gmail.com>
parents: 5144
diff changeset
  1715
end
ce5d7538ac48 muc: Make max_history_messages simply a service-wide config option, and don't store it per-room (rooms still have their own history_message, but this is a global limit)
Matthew Wild <mwild1@gmail.com>
parents: 5144
diff changeset
  1716
5063
4bc202a7b351 MUC: Expose room metatable in the MUC lib.
Waqas Hussain <waqas20@gmail.com>
parents: 5061
diff changeset
  1717
_M.room_mt = room_mt;
4bc202a7b351 MUC: Expose room metatable in the MUC lib.
Waqas Hussain <waqas20@gmail.com>
parents: 5061
diff changeset
  1718
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
  1719
return _M;