plugins/muc/muc.lib.lua
author daurnimator <quae@daurnimator.com>
Thu, 20 Mar 2014 16:19:13 -0400
changeset 6133 5d8949bb15b0
parent 6132 96a1aa23ae0d
child 6134 48b6ef993888
permissions -rw-r--r--
plugins/muc/muc.lib: Additional `route_to_occupant` usage
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;
fd6ab269ecc2 MUC: A little modification to improve code analysis.
Waqas Hussain <waqas20@gmail.com>
parents: 3280
diff changeset
    11
local pairs, ipairs = pairs, ipairs;
fd6ab269ecc2 MUC: A little modification to improve code analysis.
Waqas Hussain <waqas20@gmail.com>
parents: 3280
diff changeset
    12
6092
16d5049fe842 plugins/muc/muc.lib: Split out `send_history` into `parse_history` and `get_history`
daurnimator <quae@daurnimator.com>
parents: 6004
diff changeset
    13
local gettime = os.time;
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    14
local datetime = require "util.datetime";
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    15
3517
530f7de1d265 MUC: Use util.dataforms to generate forms
Kim Alvefur <zash@zash.se>
parents: 3516
diff changeset
    16
local dataform = require "util.dataforms";
530f7de1d265 MUC: Use util.dataforms to generate forms
Kim Alvefur <zash@zash.se>
parents: 3516
diff changeset
    17
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    18
local jid_split = require "util.jid".split;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    19
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
    20
local jid_prep = require "util.jid".prep;
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    21
local st = require "util.stanza";
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    22
local log = require "util.logger".init("mod_muc");
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    23
local t_insert, t_remove = table.insert, table.remove;
1735
81406277279e MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 1734
diff changeset
    24
local setmetatable = setmetatable;
1778
f4213d84ba8a MUC: Correct routing of vCard requests to bare JID.
Waqas Hussain <waqas20@gmail.com>
parents: 1769
diff changeset
    25
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
    26
local md5 = require "util.hashes".md5;
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    27
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
    28
local default_history_length, max_history_length = 20, math.huge;
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    29
6116
9147e566fde0 plugins/muc/muc.lib: Tidy up `get_filtered_presence`
daurnimator <quae@daurnimator.com>
parents: 6115
diff changeset
    30
local get_filtered_presence do
9147e566fde0 plugins/muc/muc.lib: Tidy up `get_filtered_presence`
daurnimator <quae@daurnimator.com>
parents: 6115
diff changeset
    31
	local presence_filters = {
9147e566fde0 plugins/muc/muc.lib: Tidy up `get_filtered_presence`
daurnimator <quae@daurnimator.com>
parents: 6115
diff changeset
    32
		["http://jabber.org/protocol/muc"] = true;
9147e566fde0 plugins/muc/muc.lib: Tidy up `get_filtered_presence`
daurnimator <quae@daurnimator.com>
parents: 6115
diff changeset
    33
		["http://jabber.org/protocol/muc#user"] = true;
9147e566fde0 plugins/muc/muc.lib: Tidy up `get_filtered_presence`
daurnimator <quae@daurnimator.com>
parents: 6115
diff changeset
    34
	}
9147e566fde0 plugins/muc/muc.lib: Tidy up `get_filtered_presence`
daurnimator <quae@daurnimator.com>
parents: 6115
diff changeset
    35
	local function presence_filter(tag)
9147e566fde0 plugins/muc/muc.lib: Tidy up `get_filtered_presence`
daurnimator <quae@daurnimator.com>
parents: 6115
diff changeset
    36
		if presence_filters[tag.attr.xmlns] then
9147e566fde0 plugins/muc/muc.lib: Tidy up `get_filtered_presence`
daurnimator <quae@daurnimator.com>
parents: 6115
diff changeset
    37
			return nil;
9147e566fde0 plugins/muc/muc.lib: Tidy up `get_filtered_presence`
daurnimator <quae@daurnimator.com>
parents: 6115
diff changeset
    38
		end
9147e566fde0 plugins/muc/muc.lib: Tidy up `get_filtered_presence`
daurnimator <quae@daurnimator.com>
parents: 6115
diff changeset
    39
		return tag;
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    40
	end
6116
9147e566fde0 plugins/muc/muc.lib: Tidy up `get_filtered_presence`
daurnimator <quae@daurnimator.com>
parents: 6115
diff changeset
    41
	function get_filtered_presence(stanza)
9147e566fde0 plugins/muc/muc.lib: Tidy up `get_filtered_presence`
daurnimator <quae@daurnimator.com>
parents: 6115
diff changeset
    42
		return st.clone(stanza):maptags(presence_filter);
9147e566fde0 plugins/muc/muc.lib: Tidy up `get_filtered_presence`
daurnimator <quae@daurnimator.com>
parents: 6115
diff changeset
    43
	end
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    44
end
2527
3fe3dbb27b6f MUC: Have get_error_condition() use the new stanza:get_error() (muc.lib.lua 11 lines shorter \o/)
Matthew Wild <mwild1@gmail.com>
parents: 2504
diff changeset
    45
6115
b8b68d09c9d8 plugins/muc/muc.lib: Tidy up is_kickable_error: it didn't need to return the condition.
daurnimator <quae@daurnimator.com>
parents: 6114
diff changeset
    46
local is_kickable_error do
b8b68d09c9d8 plugins/muc/muc.lib: Tidy up is_kickable_error: it didn't need to return the condition.
daurnimator <quae@daurnimator.com>
parents: 6114
diff changeset
    47
	local kickable_error_conditions = {
b8b68d09c9d8 plugins/muc/muc.lib: Tidy up is_kickable_error: it didn't need to return the condition.
daurnimator <quae@daurnimator.com>
parents: 6114
diff changeset
    48
		["gone"] = true;
b8b68d09c9d8 plugins/muc/muc.lib: Tidy up is_kickable_error: it didn't need to return the condition.
daurnimator <quae@daurnimator.com>
parents: 6114
diff changeset
    49
		["internal-server-error"] = true;
b8b68d09c9d8 plugins/muc/muc.lib: Tidy up is_kickable_error: it didn't need to return the condition.
daurnimator <quae@daurnimator.com>
parents: 6114
diff changeset
    50
		["item-not-found"] = true;
b8b68d09c9d8 plugins/muc/muc.lib: Tidy up is_kickable_error: it didn't need to return the condition.
daurnimator <quae@daurnimator.com>
parents: 6114
diff changeset
    51
		["jid-malformed"] = true;
b8b68d09c9d8 plugins/muc/muc.lib: Tidy up is_kickable_error: it didn't need to return the condition.
daurnimator <quae@daurnimator.com>
parents: 6114
diff changeset
    52
		["recipient-unavailable"] = true;
b8b68d09c9d8 plugins/muc/muc.lib: Tidy up is_kickable_error: it didn't need to return the condition.
daurnimator <quae@daurnimator.com>
parents: 6114
diff changeset
    53
		["redirect"] = true;
b8b68d09c9d8 plugins/muc/muc.lib: Tidy up is_kickable_error: it didn't need to return the condition.
daurnimator <quae@daurnimator.com>
parents: 6114
diff changeset
    54
		["remote-server-not-found"] = true;
b8b68d09c9d8 plugins/muc/muc.lib: Tidy up is_kickable_error: it didn't need to return the condition.
daurnimator <quae@daurnimator.com>
parents: 6114
diff changeset
    55
		["remote-server-timeout"] = true;
b8b68d09c9d8 plugins/muc/muc.lib: Tidy up is_kickable_error: it didn't need to return the condition.
daurnimator <quae@daurnimator.com>
parents: 6114
diff changeset
    56
		["service-unavailable"] = true;
b8b68d09c9d8 plugins/muc/muc.lib: Tidy up is_kickable_error: it didn't need to return the condition.
daurnimator <quae@daurnimator.com>
parents: 6114
diff changeset
    57
		["malformed error"] = true;
b8b68d09c9d8 plugins/muc/muc.lib: Tidy up is_kickable_error: it didn't need to return the condition.
daurnimator <quae@daurnimator.com>
parents: 6114
diff changeset
    58
	};
b8b68d09c9d8 plugins/muc/muc.lib: Tidy up is_kickable_error: it didn't need to return the condition.
daurnimator <quae@daurnimator.com>
parents: 6114
diff changeset
    59
	function is_kickable_error(stanza)
b8b68d09c9d8 plugins/muc/muc.lib: Tidy up is_kickable_error: it didn't need to return the condition.
daurnimator <quae@daurnimator.com>
parents: 6114
diff changeset
    60
		local cond = select(2, stanza:get_error()) or "malformed error";
b8b68d09c9d8 plugins/muc/muc.lib: Tidy up is_kickable_error: it didn't need to return the condition.
daurnimator <quae@daurnimator.com>
parents: 6114
diff changeset
    61
		return kickable_error_conditions[cond];
b8b68d09c9d8 plugins/muc/muc.lib: Tidy up is_kickable_error: it didn't need to return the condition.
daurnimator <quae@daurnimator.com>
parents: 6114
diff changeset
    62
	end
1999
05054e360d89 MUC: Improved handling of error stanzas and made error messages concise.
Waqas Hussain <waqas20@gmail.com>
parents: 1998
diff changeset
    63
end
2527
3fe3dbb27b6f MUC: Have get_error_condition() use the new stanza:get_error() (muc.lib.lua 11 lines shorter \o/)
Matthew Wild <mwild1@gmail.com>
parents: 2504
diff changeset
    64
1735
81406277279e MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 1734
diff changeset
    65
local room_mt = {};
1737
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
    66
room_mt.__index = room_mt;
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
    67
5519
06e188268df1 MUC: add __tostring on room metatable
Matthew Wild <mwild1@gmail.com>
parents: 5397
diff changeset
    68
function room_mt:__tostring()
06e188268df1 MUC: add __tostring on room metatable
Matthew Wild <mwild1@gmail.com>
parents: 5397
diff changeset
    69
	return "MUC room ("..self.jid..")";
06e188268df1 MUC: add __tostring on room metatable
Matthew Wild <mwild1@gmail.com>
parents: 5397
diff changeset
    70
end
06e188268df1 MUC: add __tostring on room metatable
Matthew Wild <mwild1@gmail.com>
parents: 5397
diff changeset
    71
6119
c13f5d6b9b16 plugins/muc/muc.lib: Use `get_occupant_jid` method instead of indexing _jid_nick
daurnimator <quae@daurnimator.com>
parents: 6118
diff changeset
    72
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
    73
	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
    74
end
c13f5d6b9b16 plugins/muc/muc.lib: Use `get_occupant_jid` method instead of indexing _jid_nick
daurnimator <quae@daurnimator.com>
parents: 6118
diff changeset
    75
1737
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
    76
function room_mt:get_default_role(affiliation)
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
    77
	if affiliation == "owner" or affiliation == "admin" then
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
    78
		return "moderator";
3251
f2f9fe088f6e MUC: Updated room:get_default_role() to assign unaffiliated occupants a "visitor" role in moderated rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 3250
diff changeset
    79
	elseif affiliation == "member" then
1737
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
    80
		return "participant";
3251
f2f9fe088f6e MUC: Updated room:get_default_role() to assign unaffiliated occupants a "visitor" role in moderated rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 3250
diff changeset
    81
	elseif not affiliation then
5580
db5d1a350cc7 mod_muc: Refactor config form handling, and allow for clients to submit incomplete forms. Fixes #246
Matthew Wild <mwild1@gmail.com>
parents: 5577
diff changeset
    82
		if not self:get_members_only() then
db5d1a350cc7 mod_muc: Refactor config form handling, and allow for clients to submit incomplete forms. Fixes #246
Matthew Wild <mwild1@gmail.com>
parents: 5577
diff changeset
    83
			return self:get_moderated() and "visitor" or "participant";
3255
6bffb5c63131 MUC: Updated room:get_default_role() to not assign unaffiliated occupants a role in members-only rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 3254
diff changeset
    84
		end
1737
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
    85
	end
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
    86
end
1735
81406277279e MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 1734
diff changeset
    87
6129
6c66571ab0f9 plugins/muc: Have utility methods for locking the room
daurnimator <quae@daurnimator.com>
parents: 6128
diff changeset
    88
function room_mt:lock()
6c66571ab0f9 plugins/muc: Have utility methods for locking the room
daurnimator <quae@daurnimator.com>
parents: 6128
diff changeset
    89
	self.locked = true
6c66571ab0f9 plugins/muc: Have utility methods for locking the room
daurnimator <quae@daurnimator.com>
parents: 6128
diff changeset
    90
end
6c66571ab0f9 plugins/muc: Have utility methods for locking the room
daurnimator <quae@daurnimator.com>
parents: 6128
diff changeset
    91
function room_mt:unlock()
6c66571ab0f9 plugins/muc: Have utility methods for locking the room
daurnimator <quae@daurnimator.com>
parents: 6128
diff changeset
    92
	module:fire_event("muc-room-unlocked", { room = self });
6c66571ab0f9 plugins/muc: Have utility methods for locking the room
daurnimator <quae@daurnimator.com>
parents: 6128
diff changeset
    93
	self.locked = nil
6c66571ab0f9 plugins/muc: Have utility methods for locking the room
daurnimator <quae@daurnimator.com>
parents: 6128
diff changeset
    94
end
6c66571ab0f9 plugins/muc: Have utility methods for locking the room
daurnimator <quae@daurnimator.com>
parents: 6128
diff changeset
    95
function room_mt:is_locked()
6c66571ab0f9 plugins/muc: Have utility methods for locking the room
daurnimator <quae@daurnimator.com>
parents: 6128
diff changeset
    96
	return not not self.locked
6c66571ab0f9 plugins/muc: Have utility methods for locking the room
daurnimator <quae@daurnimator.com>
parents: 6128
diff changeset
    97
end
6c66571ab0f9 plugins/muc: Have utility methods for locking the room
daurnimator <quae@daurnimator.com>
parents: 6128
diff changeset
    98
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
    99
function room_mt:route_to_occupant(o_data, stanza)
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
   100
	local to = stanza.attr.to;
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
   101
	for jid in pairs(o_data.sessions) do
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
   102
		stanza.attr.to = jid;
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
   103
		self:_route_stanza(stanza);
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
   104
	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
   105
	stanza.attr.to = to;
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
   106
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
   107
1989
97c3236cc4ac MUC: Multiple sessions per nick.
Waqas Hussain <waqas20@gmail.com>
parents: 1862
diff changeset
   108
function room_mt:broadcast_presence(stanza, sid, code, nick)
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   109
	stanza = get_filtered_presence(stanza);
1825
f67e4bfc62f1 MUC: Renamed a variable name.
Waqas Hussain <waqas20@gmail.com>
parents: 1824
diff changeset
   110
	local occupant = self._occupants[stanza.attr.from];
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   111
	stanza:tag("x", {xmlns='http://jabber.org/protocol/muc#user'})
2053
f5a198127dd3 MUC: Fixed: affiliation='none' was omitted from some presence broadcasts.
Waqas Hussain <waqas20@gmail.com>
parents: 2051
diff changeset
   112
		:tag("item", {affiliation=occupant.affiliation or "none", role=occupant.role or "none", nick=nick}):up();
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   113
	if code then
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   114
		stanza:tag("status", {code=code}):up();
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   115
	end
1824
8e66c9d09f81 MUC: Refactored to remove some duplicate code.
Waqas Hussain <waqas20@gmail.com>
parents: 1819
diff changeset
   116
	self:broadcast_except_nick(stanza, stanza.attr.from);
6132
96a1aa23ae0d plugins/muc/muc.lib: Remove duplicate variable; it can never be nil.
daurnimator <quae@daurnimator.com>
parents: 6131
diff changeset
   117
	stanza:tag("status", {code='110'}):up();
96a1aa23ae0d plugins/muc/muc.lib: Remove duplicate variable; it can never be nil.
daurnimator <quae@daurnimator.com>
parents: 6131
diff changeset
   118
	stanza.attr.to = sid;
96a1aa23ae0d plugins/muc/muc.lib: Remove duplicate variable; it can never be nil.
daurnimator <quae@daurnimator.com>
parents: 6131
diff changeset
   119
	self:_route_stanza(stanza);
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   120
end
1736
98f833669d7f MUC: Fixed function declarations.
Waqas Hussain <waqas20@gmail.com>
parents: 1735
diff changeset
   121
function room_mt:broadcast_message(stanza, historic)
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
   122
	for occupant_jid, o_data in pairs(self._occupants) do
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
   123
		self:route_to_occupant(o_data, stanza)
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   124
	end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   125
	if historic then -- add to history
5982
2d5685c6262f MUC: Split saving to history into a separate method
Kim Alvefur <zash@zash.se>
parents: 5854
diff changeset
   126
		return self:save_to_history(stanza)
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
5982
2d5685c6262f MUC: Split saving to history into a separate method
Kim Alvefur <zash@zash.se>
parents: 5854
diff changeset
   129
function room_mt:save_to_history(stanza)
2d5685c6262f MUC: Split saving to history into a separate method
Kim Alvefur <zash@zash.se>
parents: 5854
diff changeset
   130
	local history = self._data['history'];
2d5685c6262f MUC: Split saving to history into a separate method
Kim Alvefur <zash@zash.se>
parents: 5854
diff changeset
   131
	if not history then history = {}; self._data['history'] = history; end
2d5685c6262f MUC: Split saving to history into a separate method
Kim Alvefur <zash@zash.se>
parents: 5854
diff changeset
   132
	stanza = st.clone(stanza);
2d5685c6262f MUC: Split saving to history into a separate method
Kim Alvefur <zash@zash.se>
parents: 5854
diff changeset
   133
	stanza.attr.to = "";
2d5685c6262f MUC: Split saving to history into a separate method
Kim Alvefur <zash@zash.se>
parents: 5854
diff changeset
   134
	local stamp = datetime.datetime();
6114
055efbb6d10c plugins/muc/muc.lib: Use module.host where `muc_domain` was previously
daurnimator <quae@daurnimator.com>
parents: 6113
diff changeset
   135
	stanza:tag("delay", {xmlns = "urn:xmpp:delay", from = module.host, stamp = stamp}):up(); -- XEP-0203
055efbb6d10c plugins/muc/muc.lib: Use module.host where `muc_domain` was previously
daurnimator <quae@daurnimator.com>
parents: 6113
diff changeset
   136
	stanza:tag("x", {xmlns = "jabber:x:delay", from = module.host, stamp = datetime.legacy()}):up(); -- XEP-0091 (deprecated)
5982
2d5685c6262f MUC: Split saving to history into a separate method
Kim Alvefur <zash@zash.se>
parents: 5854
diff changeset
   137
	local entry = { stanza = stanza, stamp = stamp };
2d5685c6262f MUC: Split saving to history into a separate method
Kim Alvefur <zash@zash.se>
parents: 5854
diff changeset
   138
	t_insert(history, entry);
2d5685c6262f MUC: Split saving to history into a separate method
Kim Alvefur <zash@zash.se>
parents: 5854
diff changeset
   139
	while #history > (self._data.history_length or default_history_length) do t_remove(history, 1) end
2d5685c6262f MUC: Split saving to history into a separate method
Kim Alvefur <zash@zash.se>
parents: 5854
diff changeset
   140
end
1742
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
   141
function room_mt:broadcast_except_nick(stanza, nick)
1751
55ee6e792e3e MUC: Fixed a variable scoping bug causing problems with presence routing on affiliation/role change.
Waqas Hussain <waqas20@gmail.com>
parents: 1750
diff changeset
   142
	for rnick, occupant in pairs(self._occupants) do
55ee6e792e3e MUC: Fixed a variable scoping bug causing problems with presence routing on affiliation/role change.
Waqas Hussain <waqas20@gmail.com>
parents: 1750
diff changeset
   143
		if rnick ~= nick then
6133
5d8949bb15b0 plugins/muc/muc.lib: Additional `route_to_occupant` usage
daurnimator <quae@daurnimator.com>
parents: 6132
diff changeset
   144
			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
   145
		end
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
   146
	end
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
   147
end
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   148
1735
81406277279e MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 1734
diff changeset
   149
function room_mt:send_occupant_list(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
   150
	local current_nick = self:get_occupant_jid(to);
1739
393abf245322 MUC: Renamed _participants table to _occupants
Waqas Hussain <waqas20@gmail.com>
parents: 1737
diff changeset
   151
	for occupant, o_data in pairs(self._occupants) do
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   152
		if occupant ~= current_nick then
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   153
			local pres = get_filtered_presence(o_data.sessions[o_data.jid]);
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   154
			pres.attr.to, pres.attr.from = to, occupant;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   155
			pres:tag("x", {xmlns='http://jabber.org/protocol/muc#user'})
2053
f5a198127dd3 MUC: Fixed: affiliation='none' was omitted from some presence broadcasts.
Waqas Hussain <waqas20@gmail.com>
parents: 2051
diff changeset
   156
				:tag("item", {affiliation=o_data.affiliation or "none", role=o_data.role or "none"}):up();
2064
1ee862fd1afe MUC: Include occupants' real JIDs in their presence (semi-anonymous rooms).
Waqas Hussain <waqas20@gmail.com>
parents: 2053
diff changeset
   157
			self:_route_stanza(pres);
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   158
		end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   159
	end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   160
end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5681
diff changeset
   161
6092
16d5049fe842 plugins/muc/muc.lib: Split out `send_history` into `parse_history` and `get_history`
daurnimator <quae@daurnimator.com>
parents: 6004
diff changeset
   162
local function parse_history(stanza)
16d5049fe842 plugins/muc/muc.lib: Split out `send_history` into `parse_history` and `get_history`
daurnimator <quae@daurnimator.com>
parents: 6004
diff changeset
   163
	local x_tag = stanza:get_child("x", "http://jabber.org/protocol/muc");
16d5049fe842 plugins/muc/muc.lib: Split out `send_history` into `parse_history` and `get_history`
daurnimator <quae@daurnimator.com>
parents: 6004
diff changeset
   164
	local history_tag = x_tag and x_tag:get_child("history", "http://jabber.org/protocol/muc");
16d5049fe842 plugins/muc/muc.lib: Split out `send_history` into `parse_history` and `get_history`
daurnimator <quae@daurnimator.com>
parents: 6004
diff changeset
   165
	if not history_tag then
16d5049fe842 plugins/muc/muc.lib: Split out `send_history` into `parse_history` and `get_history`
daurnimator <quae@daurnimator.com>
parents: 6004
diff changeset
   166
		return nil, 20, nil
16d5049fe842 plugins/muc/muc.lib: Split out `send_history` into `parse_history` and `get_history`
daurnimator <quae@daurnimator.com>
parents: 6004
diff changeset
   167
	end
16d5049fe842 plugins/muc/muc.lib: Split out `send_history` into `parse_history` and `get_history`
daurnimator <quae@daurnimator.com>
parents: 6004
diff changeset
   168
16d5049fe842 plugins/muc/muc.lib: Split out `send_history` into `parse_history` and `get_history`
daurnimator <quae@daurnimator.com>
parents: 6004
diff changeset
   169
	local maxchars = tonumber(history_tag.attr.maxchars);
16d5049fe842 plugins/muc/muc.lib: Split out `send_history` into `parse_history` and `get_history`
daurnimator <quae@daurnimator.com>
parents: 6004
diff changeset
   170
16d5049fe842 plugins/muc/muc.lib: Split out `send_history` into `parse_history` and `get_history`
daurnimator <quae@daurnimator.com>
parents: 6004
diff changeset
   171
	local maxstanzas = tonumber(history_tag.attr.maxstanzas);
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5681
diff changeset
   172
6092
16d5049fe842 plugins/muc/muc.lib: Split out `send_history` into `parse_history` and `get_history`
daurnimator <quae@daurnimator.com>
parents: 6004
diff changeset
   173
	-- messages received since the UTC datetime specified
16d5049fe842 plugins/muc/muc.lib: Split out `send_history` into `parse_history` and `get_history`
daurnimator <quae@daurnimator.com>
parents: 6004
diff changeset
   174
	local since = history_tag.attr.since;
16d5049fe842 plugins/muc/muc.lib: Split out `send_history` into `parse_history` and `get_history`
daurnimator <quae@daurnimator.com>
parents: 6004
diff changeset
   175
	if since then
16d5049fe842 plugins/muc/muc.lib: Split out `send_history` into `parse_history` and `get_history`
daurnimator <quae@daurnimator.com>
parents: 6004
diff changeset
   176
		since = datetime.parse(since);
16d5049fe842 plugins/muc/muc.lib: Split out `send_history` into `parse_history` and `get_history`
daurnimator <quae@daurnimator.com>
parents: 6004
diff changeset
   177
	end
2880
a3f6cc3417f2 MUC: Added support for letting clients manage discussion history.
Waqas Hussain <waqas20@gmail.com>
parents: 2658
diff changeset
   178
6092
16d5049fe842 plugins/muc/muc.lib: Split out `send_history` into `parse_history` and `get_history`
daurnimator <quae@daurnimator.com>
parents: 6004
diff changeset
   179
	-- messages received in the last "X" seconds.
16d5049fe842 plugins/muc/muc.lib: Split out `send_history` into `parse_history` and `get_history`
daurnimator <quae@daurnimator.com>
parents: 6004
diff changeset
   180
	local seconds = tonumber(history_tag.attr.seconds);
16d5049fe842 plugins/muc/muc.lib: Split out `send_history` into `parse_history` and `get_history`
daurnimator <quae@daurnimator.com>
parents: 6004
diff changeset
   181
	if seconds then
16d5049fe842 plugins/muc/muc.lib: Split out `send_history` into `parse_history` and `get_history`
daurnimator <quae@daurnimator.com>
parents: 6004
diff changeset
   182
		seconds = gettime() - seconds
16d5049fe842 plugins/muc/muc.lib: Split out `send_history` into `parse_history` and `get_history`
daurnimator <quae@daurnimator.com>
parents: 6004
diff changeset
   183
		if since then
16d5049fe842 plugins/muc/muc.lib: Split out `send_history` into `parse_history` and `get_history`
daurnimator <quae@daurnimator.com>
parents: 6004
diff changeset
   184
			since = math.max(since, seconds);
16d5049fe842 plugins/muc/muc.lib: Split out `send_history` into `parse_history` and `get_history`
daurnimator <quae@daurnimator.com>
parents: 6004
diff changeset
   185
		else
16d5049fe842 plugins/muc/muc.lib: Split out `send_history` into `parse_history` and `get_history`
daurnimator <quae@daurnimator.com>
parents: 6004
diff changeset
   186
			since = seconds;
16d5049fe842 plugins/muc/muc.lib: Split out `send_history` into `parse_history` and `get_history`
daurnimator <quae@daurnimator.com>
parents: 6004
diff changeset
   187
		end
16d5049fe842 plugins/muc/muc.lib: Split out `send_history` into `parse_history` and `get_history`
daurnimator <quae@daurnimator.com>
parents: 6004
diff changeset
   188
	end
2880
a3f6cc3417f2 MUC: Added support for letting clients manage discussion history.
Waqas Hussain <waqas20@gmail.com>
parents: 2658
diff changeset
   189
6092
16d5049fe842 plugins/muc/muc.lib: Split out `send_history` into `parse_history` and `get_history`
daurnimator <quae@daurnimator.com>
parents: 6004
diff changeset
   190
	return maxchars, maxstanzas, since
16d5049fe842 plugins/muc/muc.lib: Split out `send_history` into `parse_history` and `get_history`
daurnimator <quae@daurnimator.com>
parents: 6004
diff changeset
   191
end
16d5049fe842 plugins/muc/muc.lib: Split out `send_history` into `parse_history` and `get_history`
daurnimator <quae@daurnimator.com>
parents: 6004
diff changeset
   192
-- Get history for 'to'
16d5049fe842 plugins/muc/muc.lib: Split out `send_history` into `parse_history` and `get_history`
daurnimator <quae@daurnimator.com>
parents: 6004
diff changeset
   193
function room_mt:get_history(to, maxchars, maxstanzas, since)
16d5049fe842 plugins/muc/muc.lib: Split out `send_history` into `parse_history` and `get_history`
daurnimator <quae@daurnimator.com>
parents: 6004
diff changeset
   194
	local history = self._data['history']; -- send discussion history
6106
4b15cfae2d11 plugins/muc/muc: When there's no history; return an empty iterator
daurnimator <quae@daurnimator.com>
parents: 6105
diff changeset
   195
	if not history then return function() end end
6092
16d5049fe842 plugins/muc/muc.lib: Split out `send_history` into `parse_history` and `get_history`
daurnimator <quae@daurnimator.com>
parents: 6004
diff changeset
   196
	local history_len = #history
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5681
diff changeset
   197
6092
16d5049fe842 plugins/muc/muc.lib: Split out `send_history` into `parse_history` and `get_history`
daurnimator <quae@daurnimator.com>
parents: 6004
diff changeset
   198
	maxstanzas = maxstanzas or history_len
16d5049fe842 plugins/muc/muc.lib: Split out `send_history` into `parse_history` and `get_history`
daurnimator <quae@daurnimator.com>
parents: 6004
diff changeset
   199
	local n = 0;
16d5049fe842 plugins/muc/muc.lib: Split out `send_history` into `parse_history` and `get_history`
daurnimator <quae@daurnimator.com>
parents: 6004
diff changeset
   200
	local charcount = 0;
16d5049fe842 plugins/muc/muc.lib: Split out `send_history` into `parse_history` and `get_history`
daurnimator <quae@daurnimator.com>
parents: 6004
diff changeset
   201
	for i=history_len,1,-1 do
16d5049fe842 plugins/muc/muc.lib: Split out `send_history` into `parse_history` and `get_history`
daurnimator <quae@daurnimator.com>
parents: 6004
diff changeset
   202
		local entry = history[i];
16d5049fe842 plugins/muc/muc.lib: Split out `send_history` into `parse_history` and `get_history`
daurnimator <quae@daurnimator.com>
parents: 6004
diff changeset
   203
		if maxchars then
16d5049fe842 plugins/muc/muc.lib: Split out `send_history` into `parse_history` and `get_history`
daurnimator <quae@daurnimator.com>
parents: 6004
diff changeset
   204
			if not entry.chars then
16d5049fe842 plugins/muc/muc.lib: Split out `send_history` into `parse_history` and `get_history`
daurnimator <quae@daurnimator.com>
parents: 6004
diff changeset
   205
				entry.stanza.attr.to = "";
16d5049fe842 plugins/muc/muc.lib: Split out `send_history` into `parse_history` and `get_history`
daurnimator <quae@daurnimator.com>
parents: 6004
diff changeset
   206
				entry.chars = #tostring(entry.stanza);
2880
a3f6cc3417f2 MUC: Added support for letting clients manage discussion history.
Waqas Hussain <waqas20@gmail.com>
parents: 2658
diff changeset
   207
			end
6092
16d5049fe842 plugins/muc/muc.lib: Split out `send_history` into `parse_history` and `get_history`
daurnimator <quae@daurnimator.com>
parents: 6004
diff changeset
   208
			charcount = charcount + entry.chars + #to;
16d5049fe842 plugins/muc/muc.lib: Split out `send_history` into `parse_history` and `get_history`
daurnimator <quae@daurnimator.com>
parents: 6004
diff changeset
   209
			if charcount > maxchars then break; end
2880
a3f6cc3417f2 MUC: Added support for letting clients manage discussion history.
Waqas Hussain <waqas20@gmail.com>
parents: 2658
diff changeset
   210
		end
6092
16d5049fe842 plugins/muc/muc.lib: Split out `send_history` into `parse_history` and `get_history`
daurnimator <quae@daurnimator.com>
parents: 6004
diff changeset
   211
		if since and since > entry.stamp then break; end
16d5049fe842 plugins/muc/muc.lib: Split out `send_history` into `parse_history` and `get_history`
daurnimator <quae@daurnimator.com>
parents: 6004
diff changeset
   212
		if n + 1 > maxstanzas then break; end
16d5049fe842 plugins/muc/muc.lib: Split out `send_history` into `parse_history` and `get_history`
daurnimator <quae@daurnimator.com>
parents: 6004
diff changeset
   213
		n = n + 1;
16d5049fe842 plugins/muc/muc.lib: Split out `send_history` into `parse_history` and `get_history`
daurnimator <quae@daurnimator.com>
parents: 6004
diff changeset
   214
	end
16d5049fe842 plugins/muc/muc.lib: Split out `send_history` into `parse_history` and `get_history`
daurnimator <quae@daurnimator.com>
parents: 6004
diff changeset
   215
16d5049fe842 plugins/muc/muc.lib: Split out `send_history` into `parse_history` and `get_history`
daurnimator <quae@daurnimator.com>
parents: 6004
diff changeset
   216
	local i = history_len-n+1
16d5049fe842 plugins/muc/muc.lib: Split out `send_history` into `parse_history` and `get_history`
daurnimator <quae@daurnimator.com>
parents: 6004
diff changeset
   217
	return function()
16d5049fe842 plugins/muc/muc.lib: Split out `send_history` into `parse_history` and `get_history`
daurnimator <quae@daurnimator.com>
parents: 6004
diff changeset
   218
		if i > history_len then return nil end
16d5049fe842 plugins/muc/muc.lib: Split out `send_history` into `parse_history` and `get_history`
daurnimator <quae@daurnimator.com>
parents: 6004
diff changeset
   219
		local entry = history[i]
16d5049fe842 plugins/muc/muc.lib: Split out `send_history` into `parse_history` and `get_history`
daurnimator <quae@daurnimator.com>
parents: 6004
diff changeset
   220
		local msg = entry.stanza
16d5049fe842 plugins/muc/muc.lib: Split out `send_history` into `parse_history` and `get_history`
daurnimator <quae@daurnimator.com>
parents: 6004
diff changeset
   221
		msg.attr.to = to;
16d5049fe842 plugins/muc/muc.lib: Split out `send_history` into `parse_history` and `get_history`
daurnimator <quae@daurnimator.com>
parents: 6004
diff changeset
   222
		i = i + 1
16d5049fe842 plugins/muc/muc.lib: Split out `send_history` into `parse_history` and `get_history`
daurnimator <quae@daurnimator.com>
parents: 6004
diff changeset
   223
		return msg
16d5049fe842 plugins/muc/muc.lib: Split out `send_history` into `parse_history` and `get_history`
daurnimator <quae@daurnimator.com>
parents: 6004
diff changeset
   224
	end
16d5049fe842 plugins/muc/muc.lib: Split out `send_history` into `parse_history` and `get_history`
daurnimator <quae@daurnimator.com>
parents: 6004
diff changeset
   225
end
16d5049fe842 plugins/muc/muc.lib: Split out `send_history` into `parse_history` and `get_history`
daurnimator <quae@daurnimator.com>
parents: 6004
diff changeset
   226
function room_mt:send_history(to, stanza)
16d5049fe842 plugins/muc/muc.lib: Split out `send_history` into `parse_history` and `get_history`
daurnimator <quae@daurnimator.com>
parents: 6004
diff changeset
   227
	local maxchars, maxstanzas, since = parse_history(stanza)
16d5049fe842 plugins/muc/muc.lib: Split out `send_history` into `parse_history` and `get_history`
daurnimator <quae@daurnimator.com>
parents: 6004
diff changeset
   228
	for msg in self:get_history(to, maxchars, maxstanzas, since) do
16d5049fe842 plugins/muc/muc.lib: Split out `send_history` into `parse_history` and `get_history`
daurnimator <quae@daurnimator.com>
parents: 6004
diff changeset
   229
		self:_route_stanza(msg);
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   230
	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
   231
end
930109558aa2 MUC: Split out sending of the topic into method separate from sending history
Kim Alvefur <zash@zash.se>
parents: 5982
diff changeset
   232
function room_mt:send_subject(to)
1735
81406277279e MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 1734
diff changeset
   233
	if self._data['subject'] then
3393
5b8de0731c4d MUC: Store the nick (full room JID) which set the subject, and send subject to occupants from that JID.
Waqas Hussain <waqas20@gmail.com>
parents: 3361
diff changeset
   234
		self:_route_stanza(st.message({type='groupchat', from=self._data['subject_from'] or self.jid, to=to}):tag("subject"):text(self._data['subject']));
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   235
	end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   236
end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   237
2503
bb6b0bd7f2cf MUC: Converted some local functions into methods.
Waqas Hussain <waqas20@gmail.com>
parents: 2416
diff changeset
   238
function room_mt:get_disco_info(stanza)
4266
513485a11b85 MUC: Include occupant count in room disco#info response.
Waqas Hussain <waqas20@gmail.com>
parents: 4202
diff changeset
   239
	local count = 0; for _ in pairs(self._occupants) do count = count + 1; end
1808
e164fdb2d18f MUC: Added MUC feature to the disco#info replies of rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1778
diff changeset
   240
	return st.reply(stanza):query("http://jabber.org/protocol/disco#info")
3507
b639042bb0d5 MUC: Added a 'Name' property (muc#roomconfig_roomname)
Kim Alvefur <zash@zash.se>
parents: 3446
diff changeset
   241
		:tag("identity", {category="conference", type="text", name=self:get_name()}):up()
3246
3371419eb0e1 MUC: Added disco#info features to advertise room's password protection (muc_passwordprotected or muc_unsecured, depending on whether a password is set).
Waqas Hussain <waqas20@gmail.com>
parents: 3245
diff changeset
   242
		:tag("feature", {var="http://jabber.org/protocol/muc"}):up()
3371419eb0e1 MUC: Added disco#info features to advertise room's password protection (muc_passwordprotected or muc_unsecured, depending on whether a password is set).
Waqas Hussain <waqas20@gmail.com>
parents: 3245
diff changeset
   243
		:tag("feature", {var=self:get_password() and "muc_passwordprotected" or "muc_unsecured"}):up()
5580
db5d1a350cc7 mod_muc: Refactor config form handling, and allow for clients to submit incomplete forms. Fixes #246
Matthew Wild <mwild1@gmail.com>
parents: 5577
diff changeset
   244
		:tag("feature", {var=self:get_moderated() and "muc_moderated" or "muc_unmoderated"}):up()
db5d1a350cc7 mod_muc: Refactor config form handling, and allow for clients to submit incomplete forms. Fixes #246
Matthew Wild <mwild1@gmail.com>
parents: 5577
diff changeset
   245
		:tag("feature", {var=self:get_members_only() and "muc_membersonly" or "muc_open"}):up()
db5d1a350cc7 mod_muc: Refactor config form handling, and allow for clients to submit incomplete forms. Fixes #246
Matthew Wild <mwild1@gmail.com>
parents: 5577
diff changeset
   246
		:tag("feature", {var=self:get_persistent() and "muc_persistent" or "muc_temporary"}):up()
db5d1a350cc7 mod_muc: Refactor config form handling, and allow for clients to submit incomplete forms. Fixes #246
Matthew Wild <mwild1@gmail.com>
parents: 5577
diff changeset
   247
		:tag("feature", {var=self:get_hidden() and "muc_hidden" or "muc_public"}):up()
6118
aae3d6daa50d plugins/muc/muc.lib: Fetch config via accessors instead of using `_data`
daurnimator <quae@daurnimator.com>
parents: 6117
diff changeset
   248
		:tag("feature", {var=self:get_whois() ~= "anyone" and "muc_semianonymous" or "muc_nonanonymous"}):up()
3517
530f7de1d265 MUC: Use util.dataforms to generate forms
Kim Alvefur <zash@zash.se>
parents: 3516
diff changeset
   249
		:add_child(dataform.new({
530f7de1d265 MUC: Use util.dataforms to generate forms
Kim Alvefur <zash@zash.se>
parents: 3516
diff changeset
   250
			{ name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/muc#roominfo" },
5853
3ee3d79db18c muc.lib.lua: Fix Spark jabber client not displaying conference room lists, seemingly due to a missing value tag for the room description if the description has not been set
Paul <paul@quakenet.org>
parents: 5680
diff changeset
   251
			{ name = "muc#roominfo_description", label = "Description", value = "" },
4266
513485a11b85 MUC: Include occupant count in room disco#info response.
Waqas Hussain <waqas20@gmail.com>
parents: 4202
diff changeset
   252
			{ name = "muc#roominfo_occupants", label = "Number of occupants", value = tostring(count) }
3517
530f7de1d265 MUC: Use util.dataforms to generate forms
Kim Alvefur <zash@zash.se>
parents: 3516
diff changeset
   253
		}):form({["muc#roominfo_description"] = self:get_description()}, 'result'))
3246
3371419eb0e1 MUC: Added disco#info features to advertise room's password protection (muc_passwordprotected or muc_unsecured, depending on whether a password is set).
Waqas Hussain <waqas20@gmail.com>
parents: 3245
diff changeset
   254
	;
1756
b2291156a9c2 MUC: Added service discovery replies for rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1755
diff changeset
   255
end
2503
bb6b0bd7f2cf MUC: Converted some local functions into methods.
Waqas Hussain <waqas20@gmail.com>
parents: 2416
diff changeset
   256
function room_mt:get_disco_items(stanza)
2035
b8c3dbf76a2e MUC: List occupants in a room's disco#items response.
Waqas Hussain <waqas20@gmail.com>
parents: 2008
diff changeset
   257
	local reply = st.reply(stanza):query("http://jabber.org/protocol/disco#items");
b8c3dbf76a2e MUC: List occupants in a room's disco#items response.
Waqas Hussain <waqas20@gmail.com>
parents: 2008
diff changeset
   258
	for room_jid in pairs(self._occupants) do
b8c3dbf76a2e MUC: List occupants in a room's disco#items response.
Waqas Hussain <waqas20@gmail.com>
parents: 2008
diff changeset
   259
		reply:tag("item", {jid = room_jid, name = room_jid:match("/(.*)")}):up();
b8c3dbf76a2e MUC: List occupants in a room's disco#items response.
Waqas Hussain <waqas20@gmail.com>
parents: 2008
diff changeset
   260
	end
b8c3dbf76a2e MUC: List occupants in a room's disco#items response.
Waqas Hussain <waqas20@gmail.com>
parents: 2008
diff changeset
   261
	return reply;
1756
b2291156a9c2 MUC: Added service discovery replies for rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1755
diff changeset
   262
end
1735
81406277279e MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 1734
diff changeset
   263
function room_mt:set_subject(current_nick, subject)
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   264
	if subject == "" then subject = nil; end
1735
81406277279e MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 1734
diff changeset
   265
	self._data['subject'] = subject;
3393
5b8de0731c4d MUC: Store the nick (full room JID) which set the subject, and send subject to occupants from that JID.
Waqas Hussain <waqas20@gmail.com>
parents: 3361
diff changeset
   266
	self._data['subject_from'] = current_nick;
1754
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1753
diff changeset
   267
	if self.save then self:save(); end
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   268
	local msg = st.message({type='groupchat', from=current_nick})
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   269
		:tag('subject'):text(subject):up();
1735
81406277279e MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 1734
diff changeset
   270
	self:broadcast_message(msg, false);
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   271
	return true;
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   272
end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   273
6100
c78ba94d3261 plugins/muc/muc.lib: Move all kick code into one place
daurnimator <quae@daurnimator.com>
parents: 6099
diff changeset
   274
function room_mt:handle_kickable(origin, stanza)
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
   275
	local type, 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
   276
	local error_message = "Kicked: "..(condition and condition:gsub("%-", " ") or "presence error");
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
   277
	if text then
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
   278
		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
   279
	end
6100
c78ba94d3261 plugins/muc/muc.lib: Move all kick code into one place
daurnimator <quae@daurnimator.com>
parents: 6099
diff changeset
   280
	local kick_stanza = st.presence({type='unavailable', from=stanza.attr.from, to=stanza.attr.to})
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
   281
		:tag('status'):text(error_message);
6100
c78ba94d3261 plugins/muc/muc.lib: Move all kick code into one place
daurnimator <quae@daurnimator.com>
parents: 6099
diff changeset
   282
	self:handle_unavailable_to_occupant(origin, kick_stanza); -- send unavailable
c78ba94d3261 plugins/muc/muc.lib: Move all kick code into one place
daurnimator <quae@daurnimator.com>
parents: 6099
diff changeset
   283
	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
   284
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
   285
3507
b639042bb0d5 MUC: Added a 'Name' property (muc#roomconfig_roomname)
Kim Alvefur <zash@zash.se>
parents: 3446
diff changeset
   286
function room_mt:set_name(name)
3510
711eb5bf94b4 MUC: Make the room node be the default room name (thanks Zash).
Waqas Hussain <waqas20@gmail.com>
parents: 3509
diff changeset
   287
	if name == "" or type(name) ~= "string" or name == (jid_split(self.jid)) then name = nil; end
3507
b639042bb0d5 MUC: Added a 'Name' property (muc#roomconfig_roomname)
Kim Alvefur <zash@zash.se>
parents: 3446
diff changeset
   288
	if self._data.name ~= name then
b639042bb0d5 MUC: Added a 'Name' property (muc#roomconfig_roomname)
Kim Alvefur <zash@zash.se>
parents: 3446
diff changeset
   289
		self._data.name = name;
b639042bb0d5 MUC: Added a 'Name' property (muc#roomconfig_roomname)
Kim Alvefur <zash@zash.se>
parents: 3446
diff changeset
   290
		if self.save then self:save(true); end
b639042bb0d5 MUC: Added a 'Name' property (muc#roomconfig_roomname)
Kim Alvefur <zash@zash.se>
parents: 3446
diff changeset
   291
	end
b639042bb0d5 MUC: Added a 'Name' property (muc#roomconfig_roomname)
Kim Alvefur <zash@zash.se>
parents: 3446
diff changeset
   292
end
b639042bb0d5 MUC: Added a 'Name' property (muc#roomconfig_roomname)
Kim Alvefur <zash@zash.se>
parents: 3446
diff changeset
   293
function room_mt:get_name()
3510
711eb5bf94b4 MUC: Make the room node be the default room name (thanks Zash).
Waqas Hussain <waqas20@gmail.com>
parents: 3509
diff changeset
   294
	return self._data.name or jid_split(self.jid);
3507
b639042bb0d5 MUC: Added a 'Name' property (muc#roomconfig_roomname)
Kim Alvefur <zash@zash.se>
parents: 3446
diff changeset
   295
end
3508
9e4c2b048f9a MUC: Added a 'Description' property (muc#roomconfig_roomdesc)
Kim Alvefur <zash@zash.se>
parents: 3507
diff changeset
   296
function room_mt:set_description(description)
9e4c2b048f9a MUC: Added a 'Description' property (muc#roomconfig_roomdesc)
Kim Alvefur <zash@zash.se>
parents: 3507
diff changeset
   297
	if description == "" or type(description) ~= "string" then description = nil; end
9e4c2b048f9a MUC: Added a 'Description' property (muc#roomconfig_roomdesc)
Kim Alvefur <zash@zash.se>
parents: 3507
diff changeset
   298
	if self._data.description ~= description then
9e4c2b048f9a MUC: Added a 'Description' property (muc#roomconfig_roomdesc)
Kim Alvefur <zash@zash.se>
parents: 3507
diff changeset
   299
		self._data.description = description;
9e4c2b048f9a MUC: Added a 'Description' property (muc#roomconfig_roomdesc)
Kim Alvefur <zash@zash.se>
parents: 3507
diff changeset
   300
		if self.save then self:save(true); end
9e4c2b048f9a MUC: Added a 'Description' property (muc#roomconfig_roomdesc)
Kim Alvefur <zash@zash.se>
parents: 3507
diff changeset
   301
	end
9e4c2b048f9a MUC: Added a 'Description' property (muc#roomconfig_roomdesc)
Kim Alvefur <zash@zash.se>
parents: 3507
diff changeset
   302
end
9e4c2b048f9a MUC: Added a 'Description' property (muc#roomconfig_roomdesc)
Kim Alvefur <zash@zash.se>
parents: 3507
diff changeset
   303
function room_mt:get_description()
9e4c2b048f9a MUC: Added a 'Description' property (muc#roomconfig_roomdesc)
Kim Alvefur <zash@zash.se>
parents: 3507
diff changeset
   304
	return self._data.description;
9e4c2b048f9a MUC: Added a 'Description' property (muc#roomconfig_roomdesc)
Kim Alvefur <zash@zash.se>
parents: 3507
diff changeset
   305
end
3244
616a3bb2bad9 MUC: Added room:get_password() and room:set_password().
Waqas Hussain <waqas20@gmail.com>
parents: 2985
diff changeset
   306
function room_mt:set_password(password)
616a3bb2bad9 MUC: Added room:get_password() and room:set_password().
Waqas Hussain <waqas20@gmail.com>
parents: 2985
diff changeset
   307
	if password == "" or type(password) ~= "string" then password = nil; end
3249
95daf6398dbb MUC: Persist data in room:set_password() when called programmatically.
Waqas Hussain <waqas20@gmail.com>
parents: 3248
diff changeset
   308
	if self._data.password ~= password then
95daf6398dbb MUC: Persist data in room:set_password() when called programmatically.
Waqas Hussain <waqas20@gmail.com>
parents: 3248
diff changeset
   309
		self._data.password = password;
95daf6398dbb MUC: Persist data in room:set_password() when called programmatically.
Waqas Hussain <waqas20@gmail.com>
parents: 3248
diff changeset
   310
		if self.save then self:save(true); end
95daf6398dbb MUC: Persist data in room:set_password() when called programmatically.
Waqas Hussain <waqas20@gmail.com>
parents: 3248
diff changeset
   311
	end
3244
616a3bb2bad9 MUC: Added room:get_password() and room:set_password().
Waqas Hussain <waqas20@gmail.com>
parents: 2985
diff changeset
   312
end
616a3bb2bad9 MUC: Added room:get_password() and room:set_password().
Waqas Hussain <waqas20@gmail.com>
parents: 2985
diff changeset
   313
function room_mt:get_password()
616a3bb2bad9 MUC: Added room:get_password() and room:set_password().
Waqas Hussain <waqas20@gmail.com>
parents: 2985
diff changeset
   314
	return self._data.password;
616a3bb2bad9 MUC: Added room:get_password() and room:set_password().
Waqas Hussain <waqas20@gmail.com>
parents: 2985
diff changeset
   315
end
3250
38402e874b45 MUC: Added room:set_moderated(boolean) and room:is_moderated().
Waqas Hussain <waqas20@gmail.com>
parents: 3249
diff changeset
   316
function room_mt:set_moderated(moderated)
38402e874b45 MUC: Added room:set_moderated(boolean) and room:is_moderated().
Waqas Hussain <waqas20@gmail.com>
parents: 3249
diff changeset
   317
	moderated = moderated and true or nil;
38402e874b45 MUC: Added room:set_moderated(boolean) and room:is_moderated().
Waqas Hussain <waqas20@gmail.com>
parents: 3249
diff changeset
   318
	if self._data.moderated ~= moderated then
3252
22062c50eabe MUC: Added a 'Make Room Moderated?' field to the room config dialog.
Waqas Hussain <waqas20@gmail.com>
parents: 3251
diff changeset
   319
		self._data.moderated = moderated;
3250
38402e874b45 MUC: Added room:set_moderated(boolean) and room:is_moderated().
Waqas Hussain <waqas20@gmail.com>
parents: 3249
diff changeset
   320
		if self.save then self:save(true); end
38402e874b45 MUC: Added room:set_moderated(boolean) and room:is_moderated().
Waqas Hussain <waqas20@gmail.com>
parents: 3249
diff changeset
   321
	end
38402e874b45 MUC: Added room:set_moderated(boolean) and room:is_moderated().
Waqas Hussain <waqas20@gmail.com>
parents: 3249
diff changeset
   322
end
5580
db5d1a350cc7 mod_muc: Refactor config form handling, and allow for clients to submit incomplete forms. Fixes #246
Matthew Wild <mwild1@gmail.com>
parents: 5577
diff changeset
   323
function room_mt:get_moderated()
3250
38402e874b45 MUC: Added room:set_moderated(boolean) and room:is_moderated().
Waqas Hussain <waqas20@gmail.com>
parents: 3249
diff changeset
   324
	return self._data.moderated;
38402e874b45 MUC: Added room:set_moderated(boolean) and room:is_moderated().
Waqas Hussain <waqas20@gmail.com>
parents: 3249
diff changeset
   325
end
3254
a01c6411fdfb MUC: Added room:set_members_only(boolean) and room:is_members_only().
Waqas Hussain <waqas20@gmail.com>
parents: 3253
diff changeset
   326
function room_mt:set_members_only(members_only)
a01c6411fdfb MUC: Added room:set_members_only(boolean) and room:is_members_only().
Waqas Hussain <waqas20@gmail.com>
parents: 3253
diff changeset
   327
	members_only = members_only and true or nil;
a01c6411fdfb MUC: Added room:set_members_only(boolean) and room:is_members_only().
Waqas Hussain <waqas20@gmail.com>
parents: 3253
diff changeset
   328
	if self._data.members_only ~= members_only then
a01c6411fdfb MUC: Added room:set_members_only(boolean) and room:is_members_only().
Waqas Hussain <waqas20@gmail.com>
parents: 3253
diff changeset
   329
		self._data.members_only = members_only;
a01c6411fdfb MUC: Added room:set_members_only(boolean) and room:is_members_only().
Waqas Hussain <waqas20@gmail.com>
parents: 3253
diff changeset
   330
		if self.save then self:save(true); end
a01c6411fdfb MUC: Added room:set_members_only(boolean) and room:is_members_only().
Waqas Hussain <waqas20@gmail.com>
parents: 3253
diff changeset
   331
	end
a01c6411fdfb MUC: Added room:set_members_only(boolean) and room:is_members_only().
Waqas Hussain <waqas20@gmail.com>
parents: 3253
diff changeset
   332
end
5580
db5d1a350cc7 mod_muc: Refactor config form handling, and allow for clients to submit incomplete forms. Fixes #246
Matthew Wild <mwild1@gmail.com>
parents: 5577
diff changeset
   333
function room_mt:get_members_only()
3254
a01c6411fdfb MUC: Added room:set_members_only(boolean) and room:is_members_only().
Waqas Hussain <waqas20@gmail.com>
parents: 3253
diff changeset
   334
	return self._data.members_only;
a01c6411fdfb MUC: Added room:set_members_only(boolean) and room:is_members_only().
Waqas Hussain <waqas20@gmail.com>
parents: 3253
diff changeset
   335
end
3258
bc07564bec6d MUC: Added room:set_persistent(boolean) and room:is_persistent().
Waqas Hussain <waqas20@gmail.com>
parents: 3257
diff changeset
   336
function room_mt:set_persistent(persistent)
bc07564bec6d MUC: Added room:set_persistent(boolean) and room:is_persistent().
Waqas Hussain <waqas20@gmail.com>
parents: 3257
diff changeset
   337
	persistent = persistent and true or nil;
bc07564bec6d MUC: Added room:set_persistent(boolean) and room:is_persistent().
Waqas Hussain <waqas20@gmail.com>
parents: 3257
diff changeset
   338
	if self._data.persistent ~= persistent then
bc07564bec6d MUC: Added room:set_persistent(boolean) and room:is_persistent().
Waqas Hussain <waqas20@gmail.com>
parents: 3257
diff changeset
   339
		self._data.persistent = persistent;
bc07564bec6d MUC: Added room:set_persistent(boolean) and room:is_persistent().
Waqas Hussain <waqas20@gmail.com>
parents: 3257
diff changeset
   340
		if self.save then self:save(true); end
bc07564bec6d MUC: Added room:set_persistent(boolean) and room:is_persistent().
Waqas Hussain <waqas20@gmail.com>
parents: 3257
diff changeset
   341
	end
bc07564bec6d MUC: Added room:set_persistent(boolean) and room:is_persistent().
Waqas Hussain <waqas20@gmail.com>
parents: 3257
diff changeset
   342
end
5580
db5d1a350cc7 mod_muc: Refactor config form handling, and allow for clients to submit incomplete forms. Fixes #246
Matthew Wild <mwild1@gmail.com>
parents: 5577
diff changeset
   343
function room_mt:get_persistent()
3258
bc07564bec6d MUC: Added room:set_persistent(boolean) and room:is_persistent().
Waqas Hussain <waqas20@gmail.com>
parents: 3257
diff changeset
   344
	return self._data.persistent;
bc07564bec6d MUC: Added room:set_persistent(boolean) and room:is_persistent().
Waqas Hussain <waqas20@gmail.com>
parents: 3257
diff changeset
   345
end
3261
fe1c93296abd MUC: Added room:set_hidden(boolean) and room:is_hidden().
Waqas Hussain <waqas20@gmail.com>
parents: 3260
diff changeset
   346
function room_mt:set_hidden(hidden)
fe1c93296abd MUC: Added room:set_hidden(boolean) and room:is_hidden().
Waqas Hussain <waqas20@gmail.com>
parents: 3260
diff changeset
   347
	hidden = hidden and true or nil;
fe1c93296abd MUC: Added room:set_hidden(boolean) and room:is_hidden().
Waqas Hussain <waqas20@gmail.com>
parents: 3260
diff changeset
   348
	if self._data.hidden ~= hidden then
fe1c93296abd MUC: Added room:set_hidden(boolean) and room:is_hidden().
Waqas Hussain <waqas20@gmail.com>
parents: 3260
diff changeset
   349
		self._data.hidden = hidden;
fe1c93296abd MUC: Added room:set_hidden(boolean) and room:is_hidden().
Waqas Hussain <waqas20@gmail.com>
parents: 3260
diff changeset
   350
		if self.save then self:save(true); end
fe1c93296abd MUC: Added room:set_hidden(boolean) and room:is_hidden().
Waqas Hussain <waqas20@gmail.com>
parents: 3260
diff changeset
   351
	end
fe1c93296abd MUC: Added room:set_hidden(boolean) and room:is_hidden().
Waqas Hussain <waqas20@gmail.com>
parents: 3260
diff changeset
   352
end
5580
db5d1a350cc7 mod_muc: Refactor config form handling, and allow for clients to submit incomplete forms. Fixes #246
Matthew Wild <mwild1@gmail.com>
parents: 5577
diff changeset
   353
function room_mt:get_hidden()
3261
fe1c93296abd MUC: Added room:set_hidden(boolean) and room:is_hidden().
Waqas Hussain <waqas20@gmail.com>
parents: 3260
diff changeset
   354
	return self._data.hidden;
fe1c93296abd MUC: Added room:set_hidden(boolean) and room:is_hidden().
Waqas Hussain <waqas20@gmail.com>
parents: 3260
diff changeset
   355
end
5580
db5d1a350cc7 mod_muc: Refactor config form handling, and allow for clients to submit incomplete forms. Fixes #246
Matthew Wild <mwild1@gmail.com>
parents: 5577
diff changeset
   356
function room_mt:get_public()
db5d1a350cc7 mod_muc: Refactor config form handling, and allow for clients to submit incomplete forms. Fixes #246
Matthew Wild <mwild1@gmail.com>
parents: 5577
diff changeset
   357
	return not self:get_hidden();
db5d1a350cc7 mod_muc: Refactor config form handling, and allow for clients to submit incomplete forms. Fixes #246
Matthew Wild <mwild1@gmail.com>
parents: 5577
diff changeset
   358
end
db5d1a350cc7 mod_muc: Refactor config form handling, and allow for clients to submit incomplete forms. Fixes #246
Matthew Wild <mwild1@gmail.com>
parents: 5577
diff changeset
   359
function room_mt:set_public(public)
db5d1a350cc7 mod_muc: Refactor config form handling, and allow for clients to submit incomplete forms. Fixes #246
Matthew Wild <mwild1@gmail.com>
parents: 5577
diff changeset
   360
	return self:set_hidden(not public);
db5d1a350cc7 mod_muc: Refactor config form handling, and allow for clients to submit incomplete forms. Fixes #246
Matthew Wild <mwild1@gmail.com>
parents: 5577
diff changeset
   361
end
4119
813adb81d7da MUC: Add option to allow participants to change the subject.
Kim Alvefur <zash@zash.se>
parents: 3989
diff changeset
   362
function room_mt:set_changesubject(changesubject)
813adb81d7da MUC: Add option to allow participants to change the subject.
Kim Alvefur <zash@zash.se>
parents: 3989
diff changeset
   363
	changesubject = changesubject and true or nil;
813adb81d7da MUC: Add option to allow participants to change the subject.
Kim Alvefur <zash@zash.se>
parents: 3989
diff changeset
   364
	if self._data.changesubject ~= changesubject then
813adb81d7da MUC: Add option to allow participants to change the subject.
Kim Alvefur <zash@zash.se>
parents: 3989
diff changeset
   365
		self._data.changesubject = changesubject;
813adb81d7da MUC: Add option to allow participants to change the subject.
Kim Alvefur <zash@zash.se>
parents: 3989
diff changeset
   366
		if self.save then self:save(true); end
813adb81d7da MUC: Add option to allow participants to change the subject.
Kim Alvefur <zash@zash.se>
parents: 3989
diff changeset
   367
	end
813adb81d7da MUC: Add option to allow participants to change the subject.
Kim Alvefur <zash@zash.se>
parents: 3989
diff changeset
   368
end
813adb81d7da MUC: Add option to allow participants to change the subject.
Kim Alvefur <zash@zash.se>
parents: 3989
diff changeset
   369
function room_mt:get_changesubject()
813adb81d7da MUC: Add option to allow participants to change the subject.
Kim Alvefur <zash@zash.se>
parents: 3989
diff changeset
   370
	return self._data.changesubject;
813adb81d7da MUC: Add option to allow participants to change the subject.
Kim Alvefur <zash@zash.se>
parents: 3989
diff changeset
   371
end
4528
875b90d5ce0f muc - implement per channel history limits
Markus Kötter <koetter@rrzn-hiwi.uni-hannover.de>
parents: 4419
diff changeset
   372
function room_mt:get_historylength()
4785
36234dc4b177 mod_muc/muc.lib: Fall back to default_history_length if no length in config
Matthew Wild <mwild1@gmail.com>
parents: 4766
diff changeset
   373
	return self._data.history_length or default_history_length;
4528
875b90d5ce0f muc - implement per channel history limits
Markus Kötter <koetter@rrzn-hiwi.uni-hannover.de>
parents: 4419
diff changeset
   374
end
875b90d5ce0f muc - implement per channel history limits
Markus Kötter <koetter@rrzn-hiwi.uni-hannover.de>
parents: 4419
diff changeset
   375
function room_mt:set_historylength(length)
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
   376
	length = math.min(tonumber(length) or default_history_length, max_history_length or math.huge);
4876
fa41d05ee7ef muc.lib: room:set_historylength(): Condense code, and don't store length when equal to default
Matthew Wild <mwild1@gmail.com>
parents: 4875
diff changeset
   377
	if length == default_history_length then
fa41d05ee7ef muc.lib: room:set_historylength(): Condense code, and don't store length when equal to default
Matthew Wild <mwild1@gmail.com>
parents: 4875
diff changeset
   378
		length = nil;
4528
875b90d5ce0f muc - implement per channel history limits
Markus Kötter <koetter@rrzn-hiwi.uni-hannover.de>
parents: 4419
diff changeset
   379
	end
875b90d5ce0f muc - implement per channel history limits
Markus Kötter <koetter@rrzn-hiwi.uni-hannover.de>
parents: 4419
diff changeset
   380
	self._data.history_length = length;
875b90d5ce0f muc - implement per channel history limits
Markus Kötter <koetter@rrzn-hiwi.uni-hannover.de>
parents: 4419
diff changeset
   381
end
875b90d5ce0f muc - implement per channel history limits
Markus Kötter <koetter@rrzn-hiwi.uni-hannover.de>
parents: 4419
diff changeset
   382
3244
616a3bb2bad9 MUC: Added room:get_password() and room:set_password().
Waqas Hussain <waqas20@gmail.com>
parents: 2985
diff changeset
   383
5600
1b326a1e4da6 mod_muc: Add getter/setter for 'whois' (fixes traceback)
Matthew Wild <mwild1@gmail.com>
parents: 5580
diff changeset
   384
local valid_whois = { moderators = true, anyone = true };
1b326a1e4da6 mod_muc: Add getter/setter for 'whois' (fixes traceback)
Matthew Wild <mwild1@gmail.com>
parents: 5580
diff changeset
   385
1b326a1e4da6 mod_muc: Add getter/setter for 'whois' (fixes traceback)
Matthew Wild <mwild1@gmail.com>
parents: 5580
diff changeset
   386
function room_mt:set_whois(whois)
1b326a1e4da6 mod_muc: Add getter/setter for 'whois' (fixes traceback)
Matthew Wild <mwild1@gmail.com>
parents: 5580
diff changeset
   387
	if valid_whois[whois] and self._data.whois ~= whois then
1b326a1e4da6 mod_muc: Add getter/setter for 'whois' (fixes traceback)
Matthew Wild <mwild1@gmail.com>
parents: 5580
diff changeset
   388
		self._data.whois = whois;
1b326a1e4da6 mod_muc: Add getter/setter for 'whois' (fixes traceback)
Matthew Wild <mwild1@gmail.com>
parents: 5580
diff changeset
   389
		if self.save then self:save(true); end
1b326a1e4da6 mod_muc: Add getter/setter for 'whois' (fixes traceback)
Matthew Wild <mwild1@gmail.com>
parents: 5580
diff changeset
   390
	end
1b326a1e4da6 mod_muc: Add getter/setter for 'whois' (fixes traceback)
Matthew Wild <mwild1@gmail.com>
parents: 5580
diff changeset
   391
end
1b326a1e4da6 mod_muc: Add getter/setter for 'whois' (fixes traceback)
Matthew Wild <mwild1@gmail.com>
parents: 5580
diff changeset
   392
1b326a1e4da6 mod_muc: Add getter/setter for 'whois' (fixes traceback)
Matthew Wild <mwild1@gmail.com>
parents: 5580
diff changeset
   393
function room_mt:get_whois()
1b326a1e4da6 mod_muc: Add getter/setter for 'whois' (fixes traceback)
Matthew Wild <mwild1@gmail.com>
parents: 5580
diff changeset
   394
	return self._data.whois;
1b326a1e4da6 mod_muc: Add getter/setter for 'whois' (fixes traceback)
Matthew Wild <mwild1@gmail.com>
parents: 5580
diff changeset
   395
end
1b326a1e4da6 mod_muc: Add getter/setter for 'whois' (fixes traceback)
Matthew Wild <mwild1@gmail.com>
parents: 5580
diff changeset
   396
6094
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   397
function room_mt:handle_unavailable_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
   398
	local from = stanza.attr.from;
6119
c13f5d6b9b16 plugins/muc/muc.lib: Use `get_occupant_jid` method instead of indexing _jid_nick
daurnimator <quae@daurnimator.com>
parents: 6118
diff changeset
   399
	local current_nick = self:get_occupant_jid(from);
6094
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   400
	if not current_nick then
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   401
		return true; -- discard
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   402
	end
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   403
	local pr = get_filtered_presence(stanza);
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   404
	pr.attr.from = current_nick;
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   405
	log("debug", "%s leaving %s", current_nick, self.jid);
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   406
	self._jid_nick[from] = nil;
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   407
	local occupant = self._occupants[current_nick];
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   408
	local new_jid = next(occupant.sessions);
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   409
	if new_jid == from then new_jid = next(occupant.sessions, new_jid); end
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   410
	if new_jid then
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   411
		local jid = occupant.jid;
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   412
		occupant.jid = new_jid;
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   413
		occupant.sessions[from] = nil;
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   414
		pr.attr.to = from;
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   415
		pr:tag("x", {xmlns='http://jabber.org/protocol/muc#user'})
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   416
			:tag("item", {affiliation=occupant.affiliation or "none", role='none'}):up()
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   417
			:tag("status", {code='110'}):up();
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   418
		self:_route_stanza(pr);
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   419
		if jid ~= new_jid then
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   420
			pr = st.clone(occupant.sessions[new_jid])
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   421
				:tag("x", {xmlns='http://jabber.org/protocol/muc#user'})
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   422
				:tag("item", {affiliation=occupant.affiliation or "none", role=occupant.role or "none"});
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   423
			pr.attr.from = current_nick;
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   424
			self:broadcast_except_nick(pr, current_nick);
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   425
		end
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   426
	else
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   427
		occupant.role = 'none';
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   428
		self:broadcast_presence(pr, from);
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   429
		self._occupants[current_nick] = nil;
6108
aae7bc9d6e93 plugins/muc/muc: Add 'muc-occupant-left' event
daurnimator <quae@daurnimator.com>
parents: 6107
diff changeset
   430
		module:fire_event("muc-occupant-left", { room = self; nick = current_nick; });
6094
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   431
	end
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   432
	return true;
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   433
end
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   434
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   435
function room_mt:handle_occupant_presence(origin, stanza)
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   436
	local from = stanza.attr.from;
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   437
	local pr = get_filtered_presence(stanza);
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   438
	local current_nick = stanza.attr.to
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   439
	pr.attr.from = current_nick;
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   440
	log("debug", "%s broadcasted presence", current_nick);
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   441
	self._occupants[current_nick].sessions[from] = pr;
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   442
	self:broadcast_presence(pr, from);
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   443
	return true;
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   444
end
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   445
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   446
function room_mt:handle_change_nick(origin, stanza, current_nick, to)
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   447
	local from = stanza.attr.from;
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   448
	local occupant = self._occupants[current_nick];
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   449
	local is_multisession = next(occupant.sessions, next(occupant.sessions));
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   450
	if self._occupants[to] or is_multisession then
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   451
		log("debug", "%s couldn't change nick", current_nick);
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   452
		local reply = st.error_reply(stanza, "cancel", "conflict"):up();
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   453
		reply.tags[1].attr.code = "409";
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   454
		origin.send(reply:tag("x", {xmlns = "http://jabber.org/protocol/muc"}));
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   455
		return true;
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   456
	else
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   457
		local to_nick = select(3, jid_split(to));
6120
4520439227fc plugins/muc/muc.lib: Don't get same variable twice.....
daurnimator <quae@daurnimator.com>
parents: 6119
diff changeset
   458
		log("debug", "%s (%s) changing nick to %s", current_nick, occupant.jid, to);
6094
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   459
		local p = st.presence({type='unavailable', from=current_nick});
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   460
		self:broadcast_presence(p, from, '303', to_nick);
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   461
		self._occupants[current_nick] = nil;
6120
4520439227fc plugins/muc/muc.lib: Don't get same variable twice.....
daurnimator <quae@daurnimator.com>
parents: 6119
diff changeset
   462
		self._occupants[to] = occupant;
6094
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   463
		self._jid_nick[from] = to;
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   464
		local pr = get_filtered_presence(stanza);
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   465
		pr.attr.from = to;
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   466
		self._occupants[to].sessions[from] = pr;
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   467
		self:broadcast_presence(pr, from);
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   468
		return true;
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   469
	end
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   470
end
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   471
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   472
function room_mt:handle_join(origin, stanza)
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   473
	local from, to = stanza.attr.from, stanza.attr.to;
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   474
	log("debug", "%s joining as %s", from, to);
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   475
	if not next(self._affiliations) then -- new room, no owners
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   476
		self._affiliations[jid_bare(from)] = "owner";
6129
6c66571ab0f9 plugins/muc: Have utility methods for locking the room
daurnimator <quae@daurnimator.com>
parents: 6128
diff changeset
   477
		if self:is_locked() and not stanza:get_child("x", "http://jabber.org/protocol/muc") then
6c66571ab0f9 plugins/muc: Have utility methods for locking the room
daurnimator <quae@daurnimator.com>
parents: 6128
diff changeset
   478
			self:unlock(); -- Older groupchat protocol doesn't lock
6094
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   479
		end
6129
6c66571ab0f9 plugins/muc: Have utility methods for locking the room
daurnimator <quae@daurnimator.com>
parents: 6128
diff changeset
   480
	elseif self:is_locked() then -- Deny entry
6094
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   481
		origin.send(st.error_reply(stanza, "cancel", "item-not-found"));
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   482
		return true;
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   483
	end
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   484
	local affiliation = self:get_affiliation(from);
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   485
	local role = self:get_default_role(affiliation)
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   486
	if role then -- new occupant
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   487
		local is_merge = not not self._occupants[to]
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   488
		if not is_merge then
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   489
			self._occupants[to] = {affiliation=affiliation, role=role, jid=from, sessions={[from]=get_filtered_presence(stanza)}};
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   490
		else
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   491
			self._occupants[to].sessions[from] = get_filtered_presence(stanza);
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   492
		end
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   493
		self._jid_nick[from] = to;
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   494
		self:send_occupant_list(from);
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   495
		local pr = get_filtered_presence(stanza);
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   496
		pr.attr.from = to;
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   497
		pr:tag("x", {xmlns='http://jabber.org/protocol/muc#user'})
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   498
			:tag("item", {affiliation=affiliation or "none", role=role or "none"}):up();
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   499
		if not is_merge then
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   500
			self:broadcast_except_nick(pr, to);
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   501
		end
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   502
		pr:tag("status", {code='110'}):up();
6118
aae3d6daa50d plugins/muc/muc.lib: Fetch config via accessors instead of using `_data`
daurnimator <quae@daurnimator.com>
parents: 6117
diff changeset
   503
		if self:get_whois() == 'anyone' then
6094
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   504
			pr:tag("status", {code='100'}):up();
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   505
		end
6129
6c66571ab0f9 plugins/muc: Have utility methods for locking the room
daurnimator <quae@daurnimator.com>
parents: 6128
diff changeset
   506
		if self:is_locked() then
6094
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   507
			pr:tag("status", {code='201'}):up();
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   508
		end
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   509
		pr.attr.to = from;
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   510
		self:_route_stanza(pr);
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   511
		self:send_history(from, stanza);
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   512
		self:send_subject(from);
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   513
		return true;
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   514
	elseif not affiliation then -- registration required for entering members-only room
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   515
		local reply = st.error_reply(stanza, "auth", "registration-required"):up();
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   516
		reply.tags[1].attr.code = "407";
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   517
		origin.send(reply:tag("x", {xmlns = "http://jabber.org/protocol/muc"}));
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   518
		return true;
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   519
	else -- banned
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   520
		local reply = st.error_reply(stanza, "auth", "forbidden"):up();
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   521
		reply.tags[1].attr.code = "403";
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   522
		origin.send(reply:tag("x", {xmlns = "http://jabber.org/protocol/muc"}));
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   523
		return true;
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   524
	end
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   525
end
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   526
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   527
function room_mt:handle_available_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
   528
	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
   529
	local current_nick = self:get_occupant_jid(from);
6094
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   530
	if current_nick then
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   531
		--if #pr == #stanza or current_nick ~= to then -- commented because google keeps resending directed presence
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   532
			if current_nick == to then -- simple presence
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   533
				return self:handle_occupant_presence(origin, stanza)
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   534
			else -- change nick
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   535
				return self:handle_change_nick(origin, stanza, current_nick, to)
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   536
			end
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   537
		--else -- possible rejoin
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   538
		--	log("debug", "%s had connection replaced", current_nick);
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   539
		--	self:handle_to_occupant(origin, st.presence({type='unavailable', from=from, to=to})
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   540
		--		:tag('status'):text('Replaced by new connection'):up()); -- send unavailable
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   541
		--	self:handle_to_occupant(origin, stanza); -- resend available
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   542
		--end
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   543
	else -- enter room
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   544
		local new_nick = to;
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   545
		if self._occupants[to] then
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   546
			if jid_bare(from) ~= jid_bare(self._occupants[to].jid) then
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   547
				new_nick = nil;
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   548
			end
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   549
		end
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   550
		local password = stanza:get_child("x", "http://jabber.org/protocol/muc");
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   551
		password = password and password:get_child("password", "http://jabber.org/protocol/muc");
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   552
		password = password and password[1] ~= "" and password[1];
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   553
		if self:get_password() and self:get_password() ~= password then
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   554
			log("debug", "%s couldn't join due to invalid password: %s", from, to);
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   555
			local reply = st.error_reply(stanza, "auth", "not-authorized"):up();
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   556
			reply.tags[1].attr.code = "401";
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   557
			origin.send(reply:tag("x", {xmlns = "http://jabber.org/protocol/muc"}));
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   558
			return true;
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   559
		elseif not new_nick then
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   560
			log("debug", "%s couldn't join due to nick conflict: %s", from, to);
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   561
			local reply = st.error_reply(stanza, "cancel", "conflict"):up();
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   562
			reply.tags[1].attr.code = "409";
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   563
			origin.send(reply:tag("x", {xmlns = "http://jabber.org/protocol/muc"}));
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   564
			return true;
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   565
		else
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   566
			return self:handle_join(origin, stanza)
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   567
		end
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   568
	end
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   569
end
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   570
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   571
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
   572
	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
   573
	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
   574
		return self:handle_kickable(origin, stanza)
6094
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   575
	elseif type == "unavailable" then -- unavailable
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   576
		return self:handle_unavailable_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
   577
	elseif not type then -- available
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   578
		return self:handle_available_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
   579
	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
   580
		if type ~= 'visible' and type ~= 'invisible' then -- COMPAT ejabberd can broadcast or forward XEP-0018 presences
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   581
			origin.send(st.error_reply(stanza, "modify", "bad-request")); -- FIXME correct error?
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   582
		end
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   583
	end
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   584
	return true;
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   585
end
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   586
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   587
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
   588
	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
   589
	local type = stanza.attr.type;
84f9123637d4 plugins/muc/muc.lib: Refactor _to_occupant handlers
daurnimator <quae@daurnimator.com>
parents: 6095
diff changeset
   590
	local id = stanza.attr.id;
6119
c13f5d6b9b16 plugins/muc/muc.lib: Use `get_occupant_jid` method instead of indexing _jid_nick
daurnimator <quae@daurnimator.com>
parents: 6118
diff changeset
   591
	local current_nick = self:get_occupant_jid(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
   592
	local o_data = self._occupants[to];
6096
84f9123637d4 plugins/muc/muc.lib: Refactor _to_occupant handlers
daurnimator <quae@daurnimator.com>
parents: 6095
diff changeset
   593
	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
   594
		do -- deconstruct_stanza_id
538cdc3d8225 plugins/muc/muc.lib: Move (de)construct_stanza_id into `handle_iq_to_occupant`
daurnimator <quae@daurnimator.com>
parents: 6096
diff changeset
   595
			if not current_nick or not o_data then return nil; end
538cdc3d8225 plugins/muc/muc.lib: Move (de)construct_stanza_id into `handle_iq_to_occupant`
daurnimator <quae@daurnimator.com>
parents: 6096
diff changeset
   596
			local from_jid, id, to_jid_hash = (base64.decode(stanza.attr.id) or ""):match("^(.+)%z(.*)%z(.+)$");
538cdc3d8225 plugins/muc/muc.lib: Move (de)construct_stanza_id into `handle_iq_to_occupant`
daurnimator <quae@daurnimator.com>
parents: 6096
diff changeset
   597
			if not(from == from_jid or from == jid_bare(from_jid)) then return nil; end
538cdc3d8225 plugins/muc/muc.lib: Move (de)construct_stanza_id into `handle_iq_to_occupant`
daurnimator <quae@daurnimator.com>
parents: 6096
diff changeset
   598
			local session_jid
538cdc3d8225 plugins/muc/muc.lib: Move (de)construct_stanza_id into `handle_iq_to_occupant`
daurnimator <quae@daurnimator.com>
parents: 6096
diff changeset
   599
			for to_jid in pairs(o_data.sessions) do
538cdc3d8225 plugins/muc/muc.lib: Move (de)construct_stanza_id into `handle_iq_to_occupant`
daurnimator <quae@daurnimator.com>
parents: 6096
diff changeset
   600
				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
   601
					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
   602
					break;
538cdc3d8225 plugins/muc/muc.lib: Move (de)construct_stanza_id into `handle_iq_to_occupant`
daurnimator <quae@daurnimator.com>
parents: 6096
diff changeset
   603
				end
538cdc3d8225 plugins/muc/muc.lib: Move (de)construct_stanza_id into `handle_iq_to_occupant`
daurnimator <quae@daurnimator.com>
parents: 6096
diff changeset
   604
			end
538cdc3d8225 plugins/muc/muc.lib: Move (de)construct_stanza_id into `handle_iq_to_occupant`
daurnimator <quae@daurnimator.com>
parents: 6096
diff changeset
   605
			if session_jid == nil then return nil; end
538cdc3d8225 plugins/muc/muc.lib: Move (de)construct_stanza_id into `handle_iq_to_occupant`
daurnimator <quae@daurnimator.com>
parents: 6096
diff changeset
   606
			stanza.attr.from, stanza.attr.to, stanza.attr.id = current_nick, session_jid, id
538cdc3d8225 plugins/muc/muc.lib: Move (de)construct_stanza_id into `handle_iq_to_occupant`
daurnimator <quae@daurnimator.com>
parents: 6096
diff changeset
   607
		end
6096
84f9123637d4 plugins/muc/muc.lib: Refactor _to_occupant handlers
daurnimator <quae@daurnimator.com>
parents: 6095
diff changeset
   608
		log("debug", "%s sent private iq stanza to %s (%s)", from, to, stanza.attr.to);
6097
538cdc3d8225 plugins/muc/muc.lib: Move (de)construct_stanza_id into `handle_iq_to_occupant`
daurnimator <quae@daurnimator.com>
parents: 6096
diff changeset
   609
		self:_route_stanza(stanza);
6096
84f9123637d4 plugins/muc/muc.lib: Refactor _to_occupant handlers
daurnimator <quae@daurnimator.com>
parents: 6095
diff changeset
   610
		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
   611
		return true;
84f9123637d4 plugins/muc/muc.lib: Refactor _to_occupant handlers
daurnimator <quae@daurnimator.com>
parents: 6095
diff changeset
   612
	else -- Type is "get" or "set"
84f9123637d4 plugins/muc/muc.lib: Refactor _to_occupant handlers
daurnimator <quae@daurnimator.com>
parents: 6095
diff changeset
   613
		if not current_nick then
6094
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   614
			origin.send(st.error_reply(stanza, "cancel", "not-acceptable"));
6096
84f9123637d4 plugins/muc/muc.lib: Refactor _to_occupant handlers
daurnimator <quae@daurnimator.com>
parents: 6095
diff changeset
   615
			return true;
6094
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   616
		end
6096
84f9123637d4 plugins/muc/muc.lib: Refactor _to_occupant handlers
daurnimator <quae@daurnimator.com>
parents: 6095
diff changeset
   617
		if not o_data then -- recipient not in room
84f9123637d4 plugins/muc/muc.lib: Refactor _to_occupant handlers
daurnimator <quae@daurnimator.com>
parents: 6095
diff changeset
   618
			origin.send(st.error_reply(stanza, "cancel", "item-not-found", "Recipient not in room"));
84f9123637d4 plugins/muc/muc.lib: Refactor _to_occupant handlers
daurnimator <quae@daurnimator.com>
parents: 6095
diff changeset
   619
			return true;
84f9123637d4 plugins/muc/muc.lib: Refactor _to_occupant handlers
daurnimator <quae@daurnimator.com>
parents: 6095
diff changeset
   620
		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
   621
		do -- construct_stanza_id
6122
44a846744814 plugins/muc/muc.lib: Fix wrong variable in `construct_stanza_id` block
daurnimator <quae@daurnimator.com>
parents: 6121
diff changeset
   622
			stanza.attr.id = base64.encode(o_data.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
   623
		end
538cdc3d8225 plugins/muc/muc.lib: Move (de)construct_stanza_id into `handle_iq_to_occupant`
daurnimator <quae@daurnimator.com>
parents: 6096
diff changeset
   624
		stanza.attr.from, stanza.attr.to = current_nick, o_data.jid;
6096
84f9123637d4 plugins/muc/muc.lib: Refactor _to_occupant handlers
daurnimator <quae@daurnimator.com>
parents: 6095
diff changeset
   625
		log("debug", "%s sent private iq stanza to %s (%s)", from, to, o_data.jid);
84f9123637d4 plugins/muc/muc.lib: Refactor _to_occupant handlers
daurnimator <quae@daurnimator.com>
parents: 6095
diff changeset
   626
		if stanza.tags[1].attr.xmlns == 'vcard-temp' then
84f9123637d4 plugins/muc/muc.lib: Refactor _to_occupant handlers
daurnimator <quae@daurnimator.com>
parents: 6095
diff changeset
   627
			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
   628
		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
   629
		self:_route_stanza(stanza);
6096
84f9123637d4 plugins/muc/muc.lib: Refactor _to_occupant handlers
daurnimator <quae@daurnimator.com>
parents: 6095
diff changeset
   630
		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
   631
		return true;
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   632
	end
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   633
end
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   634
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   635
function room_mt:handle_message_to_occupant(origin, stanza)
6096
84f9123637d4 plugins/muc/muc.lib: Refactor _to_occupant handlers
daurnimator <quae@daurnimator.com>
parents: 6095
diff changeset
   636
	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
   637
	local current_nick = self:get_occupant_jid(from);
6094
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   638
	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
   639
	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
   640
		if type ~= "error" then
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   641
			origin.send(st.error_reply(stanza, "cancel", "not-acceptable"));
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   642
		end
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   643
		return true;
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   644
	end
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   645
	if type == "groupchat" then -- groupchat messages not allowed in PM
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   646
		origin.send(st.error_reply(stanza, "modify", "bad-request"));
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   647
		return true;
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   648
	elseif type == "error" and is_kickable_error(stanza) then
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   649
		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
   650
		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
   651
	end
84f9123637d4 plugins/muc/muc.lib: Refactor _to_occupant handlers
daurnimator <quae@daurnimator.com>
parents: 6095
diff changeset
   652
84f9123637d4 plugins/muc/muc.lib: Refactor _to_occupant handlers
daurnimator <quae@daurnimator.com>
parents: 6095
diff changeset
   653
	local o_data = self._occupants[to];
84f9123637d4 plugins/muc/muc.lib: Refactor _to_occupant handlers
daurnimator <quae@daurnimator.com>
parents: 6095
diff changeset
   654
	if not o_data then
84f9123637d4 plugins/muc/muc.lib: Refactor _to_occupant handlers
daurnimator <quae@daurnimator.com>
parents: 6095
diff changeset
   655
		origin.send(st.error_reply(stanza, "cancel", "item-not-found", "Recipient not in room"));
84f9123637d4 plugins/muc/muc.lib: Refactor _to_occupant handlers
daurnimator <quae@daurnimator.com>
parents: 6095
diff changeset
   656
		return true;
6094
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   657
	end
6096
84f9123637d4 plugins/muc/muc.lib: Refactor _to_occupant handlers
daurnimator <quae@daurnimator.com>
parents: 6095
diff changeset
   658
	log("debug", "%s sent private message stanza to %s (%s)", from, to, o_data.jid);
84f9123637d4 plugins/muc/muc.lib: Refactor _to_occupant handlers
daurnimator <quae@daurnimator.com>
parents: 6095
diff changeset
   659
	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
   660
	stanza.attr.from = current_nick;
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
   661
	self:route_to_occupant(o_data, stanza)
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
   662
	stanza.attr.from = from;
6096
84f9123637d4 plugins/muc/muc.lib: Refactor _to_occupant handlers
daurnimator <quae@daurnimator.com>
parents: 6095
diff changeset
   663
	return true;
6094
db2faeb151b6 plugins/muc/muc.lib: Factor `handle_to_occupant` out into many functions
daurnimator <quae@daurnimator.com>
parents: 6093
diff changeset
   664
end
5061
186f34d88073 MUC: Fix private IQ and message routing.
Waqas Hussain <waqas20@gmail.com>
parents: 4999
diff changeset
   665
2216
dbbb5ed41365 MUC: Slightly refactored form processing.
Waqas Hussain <waqas20@gmail.com>
parents: 2174
diff changeset
   666
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
   667
	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
   668
		: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
   669
	);
dff4a77ee285 MUC: Parse submitted form with util.dataforms
Kim Alvefur <zash@zash.se>
parents: 3590
diff changeset
   670
end
dff4a77ee285 MUC: Parse submitted form with util.dataforms
Kim Alvefur <zash@zash.se>
parents: 3590
diff changeset
   671
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
   672
function room_mt:get_form_layout(actor)
6118
aae3d6daa50d plugins/muc/muc.lib: Fetch config via accessors instead of using `_data`
daurnimator <quae@daurnimator.com>
parents: 6117
diff changeset
   673
	local whois = self:get_whois()
5541
1997671d5e46 MUC: Allow plugins to add and handle options in the MUC config form
Matthew Wild <mwild1@gmail.com>
parents: 5519
diff changeset
   674
	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
   675
		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
   676
		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
   677
		{
530f7de1d265 MUC: Use util.dataforms to generate forms
Kim Alvefur <zash@zash.se>
parents: 3516
diff changeset
   678
			name = 'FORM_TYPE',
530f7de1d265 MUC: Use util.dataforms to generate forms
Kim Alvefur <zash@zash.se>
parents: 3516
diff changeset
   679
			type = 'hidden',
530f7de1d265 MUC: Use util.dataforms to generate forms
Kim Alvefur <zash@zash.se>
parents: 3516
diff changeset
   680
			value = 'http://jabber.org/protocol/muc#roomconfig'
530f7de1d265 MUC: Use util.dataforms to generate forms
Kim Alvefur <zash@zash.se>
parents: 3516
diff changeset
   681
		},
530f7de1d265 MUC: Use util.dataforms to generate forms
Kim Alvefur <zash@zash.se>
parents: 3516
diff changeset
   682
		{
530f7de1d265 MUC: Use util.dataforms to generate forms
Kim Alvefur <zash@zash.se>
parents: 3516
diff changeset
   683
			name = 'muc#roomconfig_roomname',
530f7de1d265 MUC: Use util.dataforms to generate forms
Kim Alvefur <zash@zash.se>
parents: 3516
diff changeset
   684
			type = 'text-single',
530f7de1d265 MUC: Use util.dataforms to generate forms
Kim Alvefur <zash@zash.se>
parents: 3516
diff changeset
   685
			label = 'Name',
530f7de1d265 MUC: Use util.dataforms to generate forms
Kim Alvefur <zash@zash.se>
parents: 3516
diff changeset
   686
			value = self:get_name() or "",
530f7de1d265 MUC: Use util.dataforms to generate forms
Kim Alvefur <zash@zash.se>
parents: 3516
diff changeset
   687
		},
530f7de1d265 MUC: Use util.dataforms to generate forms
Kim Alvefur <zash@zash.se>
parents: 3516
diff changeset
   688
		{
530f7de1d265 MUC: Use util.dataforms to generate forms
Kim Alvefur <zash@zash.se>
parents: 3516
diff changeset
   689
			name = 'muc#roomconfig_roomdesc',
530f7de1d265 MUC: Use util.dataforms to generate forms
Kim Alvefur <zash@zash.se>
parents: 3516
diff changeset
   690
			type = 'text-single',
530f7de1d265 MUC: Use util.dataforms to generate forms
Kim Alvefur <zash@zash.se>
parents: 3516
diff changeset
   691
			label = 'Description',
530f7de1d265 MUC: Use util.dataforms to generate forms
Kim Alvefur <zash@zash.se>
parents: 3516
diff changeset
   692
			value = self:get_description() or "",
530f7de1d265 MUC: Use util.dataforms to generate forms
Kim Alvefur <zash@zash.se>
parents: 3516
diff changeset
   693
		},
530f7de1d265 MUC: Use util.dataforms to generate forms
Kim Alvefur <zash@zash.se>
parents: 3516
diff changeset
   694
		{
530f7de1d265 MUC: Use util.dataforms to generate forms
Kim Alvefur <zash@zash.se>
parents: 3516
diff changeset
   695
			name = 'muc#roomconfig_persistentroom',
530f7de1d265 MUC: Use util.dataforms to generate forms
Kim Alvefur <zash@zash.se>
parents: 3516
diff changeset
   696
			type = 'boolean',
530f7de1d265 MUC: Use util.dataforms to generate forms
Kim Alvefur <zash@zash.se>
parents: 3516
diff changeset
   697
			label = 'Make Room Persistent?',
5580
db5d1a350cc7 mod_muc: Refactor config form handling, and allow for clients to submit incomplete forms. Fixes #246
Matthew Wild <mwild1@gmail.com>
parents: 5577
diff changeset
   698
			value = self:get_persistent()
3517
530f7de1d265 MUC: Use util.dataforms to generate forms
Kim Alvefur <zash@zash.se>
parents: 3516
diff changeset
   699
		},
530f7de1d265 MUC: Use util.dataforms to generate forms
Kim Alvefur <zash@zash.se>
parents: 3516
diff changeset
   700
		{
530f7de1d265 MUC: Use util.dataforms to generate forms
Kim Alvefur <zash@zash.se>
parents: 3516
diff changeset
   701
			name = 'muc#roomconfig_publicroom',
530f7de1d265 MUC: Use util.dataforms to generate forms
Kim Alvefur <zash@zash.se>
parents: 3516
diff changeset
   702
			type = 'boolean',
530f7de1d265 MUC: Use util.dataforms to generate forms
Kim Alvefur <zash@zash.se>
parents: 3516
diff changeset
   703
			label = 'Make Room Publicly Searchable?',
5580
db5d1a350cc7 mod_muc: Refactor config form handling, and allow for clients to submit incomplete forms. Fixes #246
Matthew Wild <mwild1@gmail.com>
parents: 5577
diff changeset
   704
			value = not self:get_hidden()
3517
530f7de1d265 MUC: Use util.dataforms to generate forms
Kim Alvefur <zash@zash.se>
parents: 3516
diff changeset
   705
		},
530f7de1d265 MUC: Use util.dataforms to generate forms
Kim Alvefur <zash@zash.se>
parents: 3516
diff changeset
   706
		{
4119
813adb81d7da MUC: Add option to allow participants to change the subject.
Kim Alvefur <zash@zash.se>
parents: 3989
diff changeset
   707
			name = 'muc#roomconfig_changesubject',
813adb81d7da MUC: Add option to allow participants to change the subject.
Kim Alvefur <zash@zash.se>
parents: 3989
diff changeset
   708
			type = 'boolean',
813adb81d7da MUC: Add option to allow participants to change the subject.
Kim Alvefur <zash@zash.se>
parents: 3989
diff changeset
   709
			label = 'Allow Occupants to Change Subject?',
813adb81d7da MUC: Add option to allow participants to change the subject.
Kim Alvefur <zash@zash.se>
parents: 3989
diff changeset
   710
			value = self:get_changesubject()
813adb81d7da MUC: Add option to allow participants to change the subject.
Kim Alvefur <zash@zash.se>
parents: 3989
diff changeset
   711
		},
813adb81d7da MUC: Add option to allow participants to change the subject.
Kim Alvefur <zash@zash.se>
parents: 3989
diff changeset
   712
		{
3517
530f7de1d265 MUC: Use util.dataforms to generate forms
Kim Alvefur <zash@zash.se>
parents: 3516
diff changeset
   713
			name = 'muc#roomconfig_whois',
530f7de1d265 MUC: Use util.dataforms to generate forms
Kim Alvefur <zash@zash.se>
parents: 3516
diff changeset
   714
			type = 'list-single',
530f7de1d265 MUC: Use util.dataforms to generate forms
Kim Alvefur <zash@zash.se>
parents: 3516
diff changeset
   715
			label = 'Who May Discover Real JIDs?',
530f7de1d265 MUC: Use util.dataforms to generate forms
Kim Alvefur <zash@zash.se>
parents: 3516
diff changeset
   716
			value = {
6118
aae3d6daa50d plugins/muc/muc.lib: Fetch config via accessors instead of using `_data`
daurnimator <quae@daurnimator.com>
parents: 6117
diff changeset
   717
				{ value = 'moderators', label = 'Moderators Only', default = whois == 'moderators' },
aae3d6daa50d plugins/muc/muc.lib: Fetch config via accessors instead of using `_data`
daurnimator <quae@daurnimator.com>
parents: 6117
diff changeset
   718
				{ value = 'anyone',     label = 'Anyone',          default = whois == 'anyone' }
3517
530f7de1d265 MUC: Use util.dataforms to generate forms
Kim Alvefur <zash@zash.se>
parents: 3516
diff changeset
   719
			}
530f7de1d265 MUC: Use util.dataforms to generate forms
Kim Alvefur <zash@zash.se>
parents: 3516
diff changeset
   720
		},
530f7de1d265 MUC: Use util.dataforms to generate forms
Kim Alvefur <zash@zash.se>
parents: 3516
diff changeset
   721
		{
530f7de1d265 MUC: Use util.dataforms to generate forms
Kim Alvefur <zash@zash.se>
parents: 3516
diff changeset
   722
			name = 'muc#roomconfig_roomsecret',
530f7de1d265 MUC: Use util.dataforms to generate forms
Kim Alvefur <zash@zash.se>
parents: 3516
diff changeset
   723
			type = 'text-private',
530f7de1d265 MUC: Use util.dataforms to generate forms
Kim Alvefur <zash@zash.se>
parents: 3516
diff changeset
   724
			label = 'Password',
530f7de1d265 MUC: Use util.dataforms to generate forms
Kim Alvefur <zash@zash.se>
parents: 3516
diff changeset
   725
			value = self:get_password() or "",
530f7de1d265 MUC: Use util.dataforms to generate forms
Kim Alvefur <zash@zash.se>
parents: 3516
diff changeset
   726
		},
530f7de1d265 MUC: Use util.dataforms to generate forms
Kim Alvefur <zash@zash.se>
parents: 3516
diff changeset
   727
		{
530f7de1d265 MUC: Use util.dataforms to generate forms
Kim Alvefur <zash@zash.se>
parents: 3516
diff changeset
   728
			name = 'muc#roomconfig_moderatedroom',
530f7de1d265 MUC: Use util.dataforms to generate forms
Kim Alvefur <zash@zash.se>
parents: 3516
diff changeset
   729
			type = 'boolean',
530f7de1d265 MUC: Use util.dataforms to generate forms
Kim Alvefur <zash@zash.se>
parents: 3516
diff changeset
   730
			label = 'Make Room Moderated?',
5580
db5d1a350cc7 mod_muc: Refactor config form handling, and allow for clients to submit incomplete forms. Fixes #246
Matthew Wild <mwild1@gmail.com>
parents: 5577
diff changeset
   731
			value = self:get_moderated()
3517
530f7de1d265 MUC: Use util.dataforms to generate forms
Kim Alvefur <zash@zash.se>
parents: 3516
diff changeset
   732
		},
530f7de1d265 MUC: Use util.dataforms to generate forms
Kim Alvefur <zash@zash.se>
parents: 3516
diff changeset
   733
		{
530f7de1d265 MUC: Use util.dataforms to generate forms
Kim Alvefur <zash@zash.se>
parents: 3516
diff changeset
   734
			name = 'muc#roomconfig_membersonly',
530f7de1d265 MUC: Use util.dataforms to generate forms
Kim Alvefur <zash@zash.se>
parents: 3516
diff changeset
   735
			type = 'boolean',
530f7de1d265 MUC: Use util.dataforms to generate forms
Kim Alvefur <zash@zash.se>
parents: 3516
diff changeset
   736
			label = 'Make Room Members-Only?',
5580
db5d1a350cc7 mod_muc: Refactor config form handling, and allow for clients to submit incomplete forms. Fixes #246
Matthew Wild <mwild1@gmail.com>
parents: 5577
diff changeset
   737
			value = self:get_members_only()
4528
875b90d5ce0f muc - implement per channel history limits
Markus Kötter <koetter@rrzn-hiwi.uni-hannover.de>
parents: 4419
diff changeset
   738
		},
875b90d5ce0f muc - implement per channel history limits
Markus Kötter <koetter@rrzn-hiwi.uni-hannover.de>
parents: 4419
diff changeset
   739
		{
875b90d5ce0f muc - implement per channel history limits
Markus Kötter <koetter@rrzn-hiwi.uni-hannover.de>
parents: 4419
diff changeset
   740
			name = 'muc#roomconfig_historylength',
875b90d5ce0f muc - implement per channel history limits
Markus Kötter <koetter@rrzn-hiwi.uni-hannover.de>
parents: 4419
diff changeset
   741
			type = 'text-single',
875b90d5ce0f muc - implement per channel history limits
Markus Kötter <koetter@rrzn-hiwi.uni-hannover.de>
parents: 4419
diff changeset
   742
			label = 'Maximum Number of History Messages Returned by Room',
875b90d5ce0f muc - implement per channel history limits
Markus Kötter <koetter@rrzn-hiwi.uni-hannover.de>
parents: 4419
diff changeset
   743
			value = tostring(self:get_historylength())
3517
530f7de1d265 MUC: Use util.dataforms to generate forms
Kim Alvefur <zash@zash.se>
parents: 3516
diff changeset
   744
		}
3591
dff4a77ee285 MUC: Parse submitted form with util.dataforms
Kim Alvefur <zash@zash.se>
parents: 3590
diff changeset
   745
	});
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
   746
	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
   747
end
dbbb5ed41365 MUC: Slightly refactored form processing.
Waqas Hussain <waqas20@gmail.com>
parents: 2174
diff changeset
   748
dbbb5ed41365 MUC: Slightly refactored form processing.
Waqas Hussain <waqas20@gmail.com>
parents: 2174
diff changeset
   749
function room_mt:process_form(origin, stanza)
dbbb5ed41365 MUC: Slightly refactored form processing.
Waqas Hussain <waqas20@gmail.com>
parents: 2174
diff changeset
   750
	local query = stanza.tags[1];
6112
819e00a86239 plugins/muc/muc.lib: Use more modern stanza methods
daurnimator <quae@daurnimator.com>
parents: 6111
diff changeset
   751
	local form = query:get_child("x", "jabber:x:data")
2216
dbbb5ed41365 MUC: Slightly refactored form processing.
Waqas Hussain <waqas20@gmail.com>
parents: 2174
diff changeset
   752
	if not form then origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); return; end
dbbb5ed41365 MUC: Slightly refactored form processing.
Waqas Hussain <waqas20@gmail.com>
parents: 2174
diff changeset
   753
	if form.attr.type == "cancel" then origin.send(st.reply(stanza)); return; end
3591
dff4a77ee285 MUC: Parse submitted form with util.dataforms
Kim Alvefur <zash@zash.se>
parents: 3590
diff changeset
   754
	if form.attr.type ~= "submit" then origin.send(st.error_reply(stanza, "cancel", "bad-request", "Not a submitted form")); return; end
dff4a77ee285 MUC: Parse submitted form with util.dataforms
Kim Alvefur <zash@zash.se>
parents: 3590
diff changeset
   755
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
   756
	local fields = self:get_form_layout(stanza.attr.from):data(form);
3591
dff4a77ee285 MUC: Parse submitted form with util.dataforms
Kim Alvefur <zash@zash.se>
parents: 3590
diff changeset
   757
	if fields.FORM_TYPE ~= "http://jabber.org/protocol/muc#roomconfig" then origin.send(st.error_reply(stanza, "cancel", "bad-request", "Form is not of type room configuration")); return; end
1754
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1753
diff changeset
   758
2412
e243b7c81de6 Added notification of configuration changes for MUCs
Rob Hoelz <rob@hoelzro.net>
parents: 2411
diff changeset
   759
5580
db5d1a350cc7 mod_muc: Refactor config form handling, and allow for clients to submit incomplete forms. Fixes #246
Matthew Wild <mwild1@gmail.com>
parents: 5577
diff changeset
   760
	local changed = {};
5541
1997671d5e46 MUC: Allow plugins to add and handle options in the MUC config form
Matthew Wild <mwild1@gmail.com>
parents: 5519
diff changeset
   761
5580
db5d1a350cc7 mod_muc: Refactor config form handling, and allow for clients to submit incomplete forms. Fixes #246
Matthew Wild <mwild1@gmail.com>
parents: 5577
diff changeset
   762
	local function handle_option(name, field, allowed)
db5d1a350cc7 mod_muc: Refactor config form handling, and allow for clients to submit incomplete forms. Fixes #246
Matthew Wild <mwild1@gmail.com>
parents: 5577
diff changeset
   763
		local new = fields[field];
db5d1a350cc7 mod_muc: Refactor config form handling, and allow for clients to submit incomplete forms. Fixes #246
Matthew Wild <mwild1@gmail.com>
parents: 5577
diff changeset
   764
		if new == nil then return; end
db5d1a350cc7 mod_muc: Refactor config form handling, and allow for clients to submit incomplete forms. Fixes #246
Matthew Wild <mwild1@gmail.com>
parents: 5577
diff changeset
   765
		if allowed and not allowed[new] then return; end
db5d1a350cc7 mod_muc: Refactor config form handling, and allow for clients to submit incomplete forms. Fixes #246
Matthew Wild <mwild1@gmail.com>
parents: 5577
diff changeset
   766
		if new == self["get_"..name](self) then return; end
db5d1a350cc7 mod_muc: Refactor config form handling, and allow for clients to submit incomplete forms. Fixes #246
Matthew Wild <mwild1@gmail.com>
parents: 5577
diff changeset
   767
		changed[name] = true;
db5d1a350cc7 mod_muc: Refactor config form handling, and allow for clients to submit incomplete forms. Fixes #246
Matthew Wild <mwild1@gmail.com>
parents: 5577
diff changeset
   768
		self["set_"..name](self, new);
3508
9e4c2b048f9a MUC: Added a 'Description' property (muc#roomconfig_roomdesc)
Kim Alvefur <zash@zash.se>
parents: 3507
diff changeset
   769
	end
9e4c2b048f9a MUC: Added a 'Description' property (muc#roomconfig_roomdesc)
Kim Alvefur <zash@zash.se>
parents: 3507
diff changeset
   770
5580
db5d1a350cc7 mod_muc: Refactor config form handling, and allow for clients to submit incomplete forms. Fixes #246
Matthew Wild <mwild1@gmail.com>
parents: 5577
diff changeset
   771
	local event = { room = self, fields = fields, changed = changed, stanza = stanza, origin = origin, update_option = handle_option };
db5d1a350cc7 mod_muc: Refactor config form handling, and allow for clients to submit incomplete forms. Fixes #246
Matthew Wild <mwild1@gmail.com>
parents: 5577
diff changeset
   772
	module:fire_event("muc-config-submitted", event);
4119
813adb81d7da MUC: Add option to allow participants to change the subject.
Kim Alvefur <zash@zash.se>
parents: 3989
diff changeset
   773
5580
db5d1a350cc7 mod_muc: Refactor config form handling, and allow for clients to submit incomplete forms. Fixes #246
Matthew Wild <mwild1@gmail.com>
parents: 5577
diff changeset
   774
	handle_option("name", "muc#roomconfig_roomname");
db5d1a350cc7 mod_muc: Refactor config form handling, and allow for clients to submit incomplete forms. Fixes #246
Matthew Wild <mwild1@gmail.com>
parents: 5577
diff changeset
   775
	handle_option("description", "muc#roomconfig_roomdesc");
db5d1a350cc7 mod_muc: Refactor config form handling, and allow for clients to submit incomplete forms. Fixes #246
Matthew Wild <mwild1@gmail.com>
parents: 5577
diff changeset
   776
	handle_option("persistent", "muc#roomconfig_persistentroom");
db5d1a350cc7 mod_muc: Refactor config form handling, and allow for clients to submit incomplete forms. Fixes #246
Matthew Wild <mwild1@gmail.com>
parents: 5577
diff changeset
   777
	handle_option("moderated", "muc#roomconfig_moderatedroom");
db5d1a350cc7 mod_muc: Refactor config form handling, and allow for clients to submit incomplete forms. Fixes #246
Matthew Wild <mwild1@gmail.com>
parents: 5577
diff changeset
   778
	handle_option("members_only", "muc#roomconfig_membersonly");
db5d1a350cc7 mod_muc: Refactor config form handling, and allow for clients to submit incomplete forms. Fixes #246
Matthew Wild <mwild1@gmail.com>
parents: 5577
diff changeset
   779
	handle_option("public", "muc#roomconfig_publicroom");
db5d1a350cc7 mod_muc: Refactor config form handling, and allow for clients to submit incomplete forms. Fixes #246
Matthew Wild <mwild1@gmail.com>
parents: 5577
diff changeset
   780
	handle_option("changesubject", "muc#roomconfig_changesubject");
db5d1a350cc7 mod_muc: Refactor config form handling, and allow for clients to submit incomplete forms. Fixes #246
Matthew Wild <mwild1@gmail.com>
parents: 5577
diff changeset
   781
	handle_option("historylength", "muc#roomconfig_historylength");
db5d1a350cc7 mod_muc: Refactor config form handling, and allow for clients to submit incomplete forms. Fixes #246
Matthew Wild <mwild1@gmail.com>
parents: 5577
diff changeset
   782
	handle_option("whois", "muc#roomconfig_whois", valid_whois);
db5d1a350cc7 mod_muc: Refactor config form handling, and allow for clients to submit incomplete forms. Fixes #246
Matthew Wild <mwild1@gmail.com>
parents: 5577
diff changeset
   783
	handle_option("password", "muc#roomconfig_roomsecret");
3248
f8d14ea3ad0e MUC: Added a password field to the room config dialog.
Waqas Hussain <waqas20@gmail.com>
parents: 3247
diff changeset
   784
2216
dbbb5ed41365 MUC: Slightly refactored form processing.
Waqas Hussain <waqas20@gmail.com>
parents: 2174
diff changeset
   785
	if self.save then self:save(true); end
6129
6c66571ab0f9 plugins/muc: Have utility methods for locking the room
daurnimator <quae@daurnimator.com>
parents: 6128
diff changeset
   786
	if self:is_locked() then
6c66571ab0f9 plugins/muc: Have utility methods for locking the room
daurnimator <quae@daurnimator.com>
parents: 6128
diff changeset
   787
		self:unlock();
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
   788
	end
2216
dbbb5ed41365 MUC: Slightly refactored form processing.
Waqas Hussain <waqas20@gmail.com>
parents: 2174
diff changeset
   789
	origin.send(st.reply(stanza));
1754
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1753
diff changeset
   790
5580
db5d1a350cc7 mod_muc: Refactor config form handling, and allow for clients to submit incomplete forms. Fixes #246
Matthew Wild <mwild1@gmail.com>
parents: 5577
diff changeset
   791
	if next(changed) then
3592
3adac5780c5a MUC: Added some more missing :up()s to the stanza building for presence broadcasts (thanks again Zash).
Waqas Hussain <waqas20@gmail.com>
parents: 3591
diff changeset
   792
		local msg = st.message({type='groupchat', from=self.jid})
3adac5780c5a MUC: Added some more missing :up()s to the stanza building for presence broadcasts (thanks again Zash).
Waqas Hussain <waqas20@gmail.com>
parents: 3591
diff changeset
   793
			:tag('x', {xmlns='http://jabber.org/protocol/muc#user'}):up()
5580
db5d1a350cc7 mod_muc: Refactor config form handling, and allow for clients to submit incomplete forms. Fixes #246
Matthew Wild <mwild1@gmail.com>
parents: 5577
diff changeset
   794
				:tag('status', {code = '104'}):up();
db5d1a350cc7 mod_muc: Refactor config form handling, and allow for clients to submit incomplete forms. Fixes #246
Matthew Wild <mwild1@gmail.com>
parents: 5577
diff changeset
   795
		if changed.whois then
5600
1b326a1e4da6 mod_muc: Add getter/setter for 'whois' (fixes traceback)
Matthew Wild <mwild1@gmail.com>
parents: 5580
diff changeset
   796
			local code = (self:get_whois() == 'moderators') and "173" or "172";
3592
3adac5780c5a MUC: Added some more missing :up()s to the stanza building for presence broadcasts (thanks again Zash).
Waqas Hussain <waqas20@gmail.com>
parents: 3591
diff changeset
   797
			msg.tags[1]:tag('status', {code = code}):up();
3adac5780c5a MUC: Added some more missing :up()s to the stanza building for presence broadcasts (thanks again Zash).
Waqas Hussain <waqas20@gmail.com>
parents: 3591
diff changeset
   798
		end
3adac5780c5a MUC: Added some more missing :up()s to the stanza building for presence broadcasts (thanks again Zash).
Waqas Hussain <waqas20@gmail.com>
parents: 3591
diff changeset
   799
		self:broadcast_message(msg, false)
2412
e243b7c81de6 Added notification of configuration changes for MUCs
Rob Hoelz <rob@hoelzro.net>
parents: 2411
diff changeset
   800
	end
1754
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1753
diff changeset
   801
end
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1753
diff changeset
   802
2217
838f6d546177 MUC: Added support for the room-destroy owner use case.
Waqas Hussain <waqas20@gmail.com>
parents: 2216
diff changeset
   803
function room_mt:destroy(newjid, reason, password)
838f6d546177 MUC: Added support for the room-destroy owner use case.
Waqas Hussain <waqas20@gmail.com>
parents: 2216
diff changeset
   804
	local pr = st.presence({type = "unavailable"})
838f6d546177 MUC: Added support for the room-destroy owner use case.
Waqas Hussain <waqas20@gmail.com>
parents: 2216
diff changeset
   805
		:tag("x", {xmlns = "http://jabber.org/protocol/muc#user"})
838f6d546177 MUC: Added support for the room-destroy owner use case.
Waqas Hussain <waqas20@gmail.com>
parents: 2216
diff changeset
   806
			:tag("item", { affiliation='none', role='none' }):up()
838f6d546177 MUC: Added support for the room-destroy owner use case.
Waqas Hussain <waqas20@gmail.com>
parents: 2216
diff changeset
   807
			:tag("destroy", {jid=newjid})
838f6d546177 MUC: Added support for the room-destroy owner use case.
Waqas Hussain <waqas20@gmail.com>
parents: 2216
diff changeset
   808
	if reason then pr:tag("reason"):text(reason):up(); end
838f6d546177 MUC: Added support for the room-destroy owner use case.
Waqas Hussain <waqas20@gmail.com>
parents: 2216
diff changeset
   809
	if password then pr:tag("password"):text(password):up(); end
838f6d546177 MUC: Added support for the room-destroy owner use case.
Waqas Hussain <waqas20@gmail.com>
parents: 2216
diff changeset
   810
	for nick, occupant in pairs(self._occupants) do
838f6d546177 MUC: Added support for the room-destroy owner use case.
Waqas Hussain <waqas20@gmail.com>
parents: 2216
diff changeset
   811
		pr.attr.from = nick;
838f6d546177 MUC: Added support for the room-destroy owner use case.
Waqas Hussain <waqas20@gmail.com>
parents: 2216
diff changeset
   812
		for jid in pairs(occupant.sessions) do
838f6d546177 MUC: Added support for the room-destroy owner use case.
Waqas Hussain <waqas20@gmail.com>
parents: 2216
diff changeset
   813
			pr.attr.to = jid;
838f6d546177 MUC: Added support for the room-destroy owner use case.
Waqas Hussain <waqas20@gmail.com>
parents: 2216
diff changeset
   814
			self:_route_stanza(pr);
838f6d546177 MUC: Added support for the room-destroy owner use case.
Waqas Hussain <waqas20@gmail.com>
parents: 2216
diff changeset
   815
			self._jid_nick[jid] = nil;
838f6d546177 MUC: Added support for the room-destroy owner use case.
Waqas Hussain <waqas20@gmail.com>
parents: 2216
diff changeset
   816
		end
838f6d546177 MUC: Added support for the room-destroy owner use case.
Waqas Hussain <waqas20@gmail.com>
parents: 2216
diff changeset
   817
		self._occupants[nick] = nil;
6108
aae7bc9d6e93 plugins/muc/muc: Add 'muc-occupant-left' event
daurnimator <quae@daurnimator.com>
parents: 6107
diff changeset
   818
		module:fire_event("muc-occupant-left", { room = self; nick = nick; });
1754
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1753
diff changeset
   819
	end
3259
a5b9209efb23 MUC: Replaced direct access of room's internal persistence state with :set_persistent(boolean) and :is_persistent() in various functions.
Waqas Hussain <waqas20@gmail.com>
parents: 3258
diff changeset
   820
	self:set_persistent(false);
5577
8b09b0d068d4 mod_muc: Fire muc-room-created and muc-room-destroyed events (thanks nik)
Matthew Wild <mwild1@gmail.com>
parents: 5542
diff changeset
   821
	module:fire_event("muc-room-destroyed", { room = self });
1754
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1753
diff changeset
   822
end
67b66eec9777 MUC: Added support for room configuration forms, persistence and hidden rooms.
Waqas Hussain <waqas20@gmail.com>
parents: 1753
diff changeset
   823
6101
a861dc18e08d plugins/muc/muc.lib: Add disco iq handlers with compatible argument signature
daurnimator <quae@daurnimator.com>
parents: 6100
diff changeset
   824
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
   825
	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
   826
	return true;
a861dc18e08d plugins/muc/muc.lib: Add disco iq handlers with compatible argument signature
daurnimator <quae@daurnimator.com>
parents: 6100
diff changeset
   827
end
a861dc18e08d plugins/muc/muc.lib: Add disco iq handlers with compatible argument signature
daurnimator <quae@daurnimator.com>
parents: 6100
diff changeset
   828
a861dc18e08d plugins/muc/muc.lib: Add disco iq handlers with compatible argument signature
daurnimator <quae@daurnimator.com>
parents: 6100
diff changeset
   829
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
   830
	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
   831
	return true;
a861dc18e08d plugins/muc/muc.lib: Add disco iq handlers with compatible argument signature
daurnimator <quae@daurnimator.com>
parents: 6100
diff changeset
   832
end
a861dc18e08d plugins/muc/muc.lib: Add disco iq handlers with compatible argument signature
daurnimator <quae@daurnimator.com>
parents: 6100
diff changeset
   833
6095
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   834
function room_mt:handle_admin_item_set_command(origin, stanza)
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   835
	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
   836
	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
   837
		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
   838
		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
   839
			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
   840
			return true;
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   841
		end
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   842
	end
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   843
	if not item.attr.jid and item.attr.nick then -- COMPAT Workaround for Miranda sending 'nick' instead of 'jid' when changing affiliation
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   844
		local occupant = self._occupants[self.jid.."/"..item.attr.nick];
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   845
		if occupant then item.attr.jid = occupant.jid; end
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   846
	elseif not item.attr.nick and item.attr.jid then
6119
c13f5d6b9b16 plugins/muc/muc.lib: Use `get_occupant_jid` method instead of indexing _jid_nick
daurnimator <quae@daurnimator.com>
parents: 6118
diff changeset
   847
		local nick = self:get_occupant_jid(item.attr.jid);
6095
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   848
		if nick then item.attr.nick = select(3, jid_split(nick)); end
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   849
	end
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   850
	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
   851
	local callback = function() origin.send(st.reply(stanza)); end
6112
819e00a86239 plugins/muc/muc.lib: Use more modern stanza methods
daurnimator <quae@daurnimator.com>
parents: 6111
diff changeset
   852
	local reason = item:get_child_text("reason");
6095
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   853
	if item.attr.affiliation and item.attr.jid and not item.attr.role then
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   854
		local success, errtype, err = self:set_affiliation(actor, item.attr.jid, item.attr.affiliation, callback, reason);
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   855
		if not success then origin.send(st.error_reply(stanza, errtype, err)); end
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   856
		return true;
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   857
	elseif item.attr.role and item.attr.nick and not item.attr.affiliation then
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   858
		local success, errtype, err = self:set_role(actor, self.jid.."/"..item.attr.nick, item.attr.role, callback, reason);
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   859
		if not success then origin.send(st.error_reply(stanza, errtype, err)); end
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   860
		return true;
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   861
	else
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   862
		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
   863
		return true;
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   864
	end
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   865
end
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   866
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   867
function room_mt:handle_admin_item_get_command(origin, stanza)
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   868
	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
   869
	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
   870
	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
   871
	local _aff = item.attr.affiliation;
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   872
	local _rol = item.attr.role;
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   873
	if _aff and not _rol then
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   874
		if affiliation == "owner" or (affiliation == "admin" and _aff ~= "owner" and _aff ~= "admin") then
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   875
			local reply = st.reply(stanza):query("http://jabber.org/protocol/muc#admin");
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   876
			for jid, affiliation in pairs(self._affiliations) do
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   877
				if affiliation == _aff then
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   878
					reply:tag("item", {affiliation = _aff, jid = jid}):up();
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   879
				end
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   880
			end
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   881
			origin.send(reply);
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   882
			return true;
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   883
		else
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   884
			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
   885
			return true;
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   886
		end
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   887
	elseif _rol 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
   888
		local role = self:get_role(self:get_occupant_jid(actor)) or self:get_default_role(affiliation);
6095
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   889
		if role == "moderator" then
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   890
			if _rol == "none" then _rol = nil; end
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   891
			local reply = st.reply(stanza):query("http://jabber.org/protocol/muc#admin");
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   892
			for occupant_jid, occupant in pairs(self._occupants) do
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   893
				if occupant.role == _rol then
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   894
					reply:tag("item", {
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   895
						nick = select(3, jid_split(occupant_jid)),
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   896
						role = _rol or "none",
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   897
						affiliation = occupant.affiliation or "none",
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   898
						jid = occupant.jid
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   899
						}):up();
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   900
				end
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   901
			end
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   902
			origin.send(reply);
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   903
			return true;
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   904
		else
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   905
			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
   906
			return true;
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   907
		end
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   908
	else
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   909
		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
   910
		return true;
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   911
	end
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   912
end
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   913
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   914
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
   915
	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
   916
		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
   917
		return true;
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   918
	end
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   919
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   920
	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
   921
	return true;
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   922
end
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   923
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
   924
	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
   925
		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
   926
		return true;
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   927
	end
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   928
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   929
	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
   930
	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
   931
		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
   932
		return true;
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   933
	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
   934
		local newjid = child.attr.jid;
6112
819e00a86239 plugins/muc/muc.lib: Use more modern stanza methods
daurnimator <quae@daurnimator.com>
parents: 6111
diff changeset
   935
		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
   936
		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
   937
		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
   938
		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
   939
		return true;
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   940
	else
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   941
		self:process_form(origin, stanza);
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   942
		return true;
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   943
	end
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   944
end
7900ebc544ce plugins/muc/muc.lib: Split out the room iq handler into functions
daurnimator <quae@daurnimator.com>
parents: 6094
diff changeset
   945
6093
9a7eaf0a35b6 plugins/muc/muc.lib: Split up `handle_to_room` into smaller handlers (thanks sysko)
daurnimator <quae@daurnimator.com>
parents: 6092
diff changeset
   946
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
   947
	local from = stanza.attr.from;
6119
c13f5d6b9b16 plugins/muc/muc.lib: Use `get_occupant_jid` method instead of indexing _jid_nick
daurnimator <quae@daurnimator.com>
parents: 6118
diff changeset
   948
	local current_nick = self:get_occupant_jid(from);
6093
9a7eaf0a35b6 plugins/muc/muc.lib: Split up `handle_to_room` into smaller handlers (thanks sysko)
daurnimator <quae@daurnimator.com>
parents: 6092
diff changeset
   949
	local occupant = self._occupants[current_nick];
9a7eaf0a35b6 plugins/muc/muc.lib: Split up `handle_to_room` into smaller handlers (thanks sysko)
daurnimator <quae@daurnimator.com>
parents: 6092
diff changeset
   950
	if not occupant then -- not in room
9a7eaf0a35b6 plugins/muc/muc.lib: Split up `handle_to_room` into smaller handlers (thanks sysko)
daurnimator <quae@daurnimator.com>
parents: 6092
diff changeset
   951
		origin.send(st.error_reply(stanza, "cancel", "not-acceptable"));
6098
1d7e5d091980 plugins/muc/muc.lib: Add some missing return values
daurnimator <quae@daurnimator.com>
parents: 6097
diff changeset
   952
		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
   953
	elseif occupant.role == "visitor" then
9a7eaf0a35b6 plugins/muc/muc.lib: Split up `handle_to_room` into smaller handlers (thanks sysko)
daurnimator <quae@daurnimator.com>
parents: 6092
diff changeset
   954
		origin.send(st.error_reply(stanza, "auth", "forbidden"));
6098
1d7e5d091980 plugins/muc/muc.lib: Add some missing return values
daurnimator <quae@daurnimator.com>
parents: 6097
diff changeset
   955
		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
   956
	else
5613
f3166adab512 mod_muc: Remove unused variable
Matthew Wild <mwild1@gmail.com>
parents: 5612
diff changeset
   957
		local from = stanza.attr.from;
6093
9a7eaf0a35b6 plugins/muc/muc.lib: Split up `handle_to_room` into smaller handlers (thanks sysko)
daurnimator <quae@daurnimator.com>
parents: 6092
diff changeset
   958
		stanza.attr.from = current_nick;
9a7eaf0a35b6 plugins/muc/muc.lib: Split up `handle_to_room` into smaller handlers (thanks sysko)
daurnimator <quae@daurnimator.com>
parents: 6092
diff changeset
   959
		local subject = stanza:get_child_text("subject");
9a7eaf0a35b6 plugins/muc/muc.lib: Split up `handle_to_room` into smaller handlers (thanks sysko)
daurnimator <quae@daurnimator.com>
parents: 6092
diff changeset
   960
		if subject then
9a7eaf0a35b6 plugins/muc/muc.lib: Split up `handle_to_room` into smaller handlers (thanks sysko)
daurnimator <quae@daurnimator.com>
parents: 6092
diff changeset
   961
			if occupant.role == "moderator" or
6118
aae3d6daa50d plugins/muc/muc.lib: Fetch config via accessors instead of using `_data`
daurnimator <quae@daurnimator.com>
parents: 6117
diff changeset
   962
				( self:get_changesubject() and occupant.role == "participant" ) then -- and participant
6093
9a7eaf0a35b6 plugins/muc/muc.lib: Split up `handle_to_room` into smaller handlers (thanks sysko)
daurnimator <quae@daurnimator.com>
parents: 6092
diff changeset
   963
				self:set_subject(current_nick, subject);
9a7eaf0a35b6 plugins/muc/muc.lib: Split up `handle_to_room` into smaller handlers (thanks sysko)
daurnimator <quae@daurnimator.com>
parents: 6092
diff changeset
   964
			else
9a7eaf0a35b6 plugins/muc/muc.lib: Split up `handle_to_room` into smaller handlers (thanks sysko)
daurnimator <quae@daurnimator.com>
parents: 6092
diff changeset
   965
				stanza.attr.from = from;
9a7eaf0a35b6 plugins/muc/muc.lib: Split up `handle_to_room` into smaller handlers (thanks sysko)
daurnimator <quae@daurnimator.com>
parents: 6092
diff changeset
   966
				origin.send(st.error_reply(stanza, "auth", "forbidden"));
9a7eaf0a35b6 plugins/muc/muc.lib: Split up `handle_to_room` into smaller handlers (thanks sysko)
daurnimator <quae@daurnimator.com>
parents: 6092
diff changeset
   967
			end
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   968
		else
6093
9a7eaf0a35b6 plugins/muc/muc.lib: Split up `handle_to_room` into smaller handlers (thanks sysko)
daurnimator <quae@daurnimator.com>
parents: 6092
diff changeset
   969
			self:broadcast_message(stanza, self:get_historylength() > 0 and stanza:get_child("body"));
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   970
		end
6093
9a7eaf0a35b6 plugins/muc/muc.lib: Split up `handle_to_room` into smaller handlers (thanks sysko)
daurnimator <quae@daurnimator.com>
parents: 6092
diff changeset
   971
		stanza.attr.from = from;
6098
1d7e5d091980 plugins/muc/muc.lib: Add some missing return values
daurnimator <quae@daurnimator.com>
parents: 6097
diff changeset
   972
		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
   973
	end
9a7eaf0a35b6 plugins/muc/muc.lib: Split up `handle_to_room` into smaller handlers (thanks sysko)
daurnimator <quae@daurnimator.com>
parents: 6092
diff changeset
   974
end
9a7eaf0a35b6 plugins/muc/muc.lib: Split up `handle_to_room` into smaller handlers (thanks sysko)
daurnimator <quae@daurnimator.com>
parents: 6092
diff changeset
   975
9a7eaf0a35b6 plugins/muc/muc.lib: Split up `handle_to_room` into smaller handlers (thanks sysko)
daurnimator <quae@daurnimator.com>
parents: 6092
diff changeset
   976
-- 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
   977
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
   978
	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
   979
	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
   980
	if current_nick then
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   981
		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
   982
		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
   983
		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
   984
		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
   985
	end
6100
c78ba94d3261 plugins/muc/muc.lib: Move all kick code into one place
daurnimator <quae@daurnimator.com>
parents: 6099
diff changeset
   986
	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
   987
end
9a7eaf0a35b6 plugins/muc/muc.lib: Split up `handle_to_room` into smaller handlers (thanks sysko)
daurnimator <quae@daurnimator.com>
parents: 6092
diff changeset
   988
6123
7f82bbd249fe plugins/muc/muc.lib: Remove `payload` argument from `handle_mediated_*`; extract it from inside.
daurnimator <quae@daurnimator.com>
parents: 6122
diff changeset
   989
function room_mt:handle_mediated_invite(origin, stanza)
7f82bbd249fe plugins/muc/muc.lib: Remove `payload` argument from `handle_mediated_*`; extract it from inside.
daurnimator <quae@daurnimator.com>
parents: 6122
diff changeset
   990
	local payload = stanza:get_child("x", "http://jabber.org/protocol/muc#user"):get_child("invite")
6093
9a7eaf0a35b6 plugins/muc/muc.lib: Split up `handle_to_room` into smaller handlers (thanks sysko)
daurnimator <quae@daurnimator.com>
parents: 6092
diff changeset
   991
	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
   992
	local current_nick = self:get_occupant_jid(_from)
6124
203f2b4933b0 plugins/muc/muc.lib: Check role instead of current_nick
daurnimator <quae@daurnimator.com>
parents: 6123
diff changeset
   993
	-- Need visitor role or higher to invite
6127
a66ebc5d0ab5 plugins/muc/muc.lib: Allow users with affiliations to invite while not in room themselves
daurnimator <quae@daurnimator.com>
parents: 6126
diff changeset
   994
	if not self:get_role(current_nick) or not self:get_default_role(self:get_affiliation(_from)) then
6103
25ba4e2b31b3 plugins/muc/muc: Check for mediated invites in a smarter way
daurnimator <quae@daurnimator.com>
parents: 6102
diff changeset
   995
		origin.send(st.error_reply(stanza, "auth", "forbidden"));
25ba4e2b31b3 plugins/muc/muc: Check for mediated invites in a smarter way
daurnimator <quae@daurnimator.com>
parents: 6102
diff changeset
   996
		return true;
25ba4e2b31b3 plugins/muc/muc: Check for mediated invites in a smarter way
daurnimator <quae@daurnimator.com>
parents: 6102
diff changeset
   997
	end
6093
9a7eaf0a35b6 plugins/muc/muc.lib: Split up `handle_to_room` into smaller handlers (thanks sysko)
daurnimator <quae@daurnimator.com>
parents: 6092
diff changeset
   998
	local _invitee = jid_prep(payload.attr.to);
9a7eaf0a35b6 plugins/muc/muc.lib: Split up `handle_to_room` into smaller handlers (thanks sysko)
daurnimator <quae@daurnimator.com>
parents: 6092
diff changeset
   999
	if _invitee then
6128
8a71a1c34202 plugins/muc/muc.lib: Use occupant jids when whois == "moderators"
daurnimator <quae@daurnimator.com>
parents: 6127
diff changeset
  1000
		if self:get_whois() == "moderators" then
8a71a1c34202 plugins/muc/muc.lib: Use occupant jids when whois == "moderators"
daurnimator <quae@daurnimator.com>
parents: 6127
diff changeset
  1001
			_from = current_nick;
8a71a1c34202 plugins/muc/muc.lib: Use occupant jids when whois == "moderators"
daurnimator <quae@daurnimator.com>
parents: 6127
diff changeset
  1002
		end
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
  1003
		local _reason = payload:get_child_text("reason")
6093
9a7eaf0a35b6 plugins/muc/muc.lib: Split up `handle_to_room` into smaller handlers (thanks sysko)
daurnimator <quae@daurnimator.com>
parents: 6092
diff changeset
  1004
		local invite = st.message({from = _to, to = _invitee, id = stanza.attr.id})
9a7eaf0a35b6 plugins/muc/muc.lib: Split up `handle_to_room` into smaller handlers (thanks sysko)
daurnimator <quae@daurnimator.com>
parents: 6092
diff changeset
  1005
			:tag('x', {xmlns='http://jabber.org/protocol/muc#user'})
9a7eaf0a35b6 plugins/muc/muc.lib: Split up `handle_to_room` into smaller handlers (thanks sysko)
daurnimator <quae@daurnimator.com>
parents: 6092
diff changeset
  1006
				:tag('invite', {from=_from})
9a7eaf0a35b6 plugins/muc/muc.lib: Split up `handle_to_room` into smaller handlers (thanks sysko)
daurnimator <quae@daurnimator.com>
parents: 6092
diff changeset
  1007
					:tag('reason'):text(_reason or ""):up()
9a7eaf0a35b6 plugins/muc/muc.lib: Split up `handle_to_room` into smaller handlers (thanks sysko)
daurnimator <quae@daurnimator.com>
parents: 6092
diff changeset
  1008
				:up();
6107
5491be05b84c plugins/muc/muc: Only call get_password once in invite creation
daurnimator <quae@daurnimator.com>
parents: 6106
diff changeset
  1009
		local password = self:get_password()
5491be05b84c plugins/muc/muc: Only call get_password once in invite creation
daurnimator <quae@daurnimator.com>
parents: 6106
diff changeset
  1010
		if password then
5491be05b84c plugins/muc/muc: Only call get_password once in invite creation
daurnimator <quae@daurnimator.com>
parents: 6106
diff changeset
  1011
			invite:tag("password"):text(password):up();
5491be05b84c plugins/muc/muc: Only call get_password once in invite creation
daurnimator <quae@daurnimator.com>
parents: 6106
diff changeset
  1012
		end
6093
9a7eaf0a35b6 plugins/muc/muc.lib: Split up `handle_to_room` into smaller handlers (thanks sysko)
daurnimator <quae@daurnimator.com>
parents: 6092
diff changeset
  1013
			invite:up()
9a7eaf0a35b6 plugins/muc/muc.lib: Split up `handle_to_room` into smaller handlers (thanks sysko)
daurnimator <quae@daurnimator.com>
parents: 6092
diff changeset
  1014
			:tag('x', {xmlns="jabber:x:conference", jid=_to}) -- COMPAT: Some older clients expect this
9a7eaf0a35b6 plugins/muc/muc.lib: Split up `handle_to_room` into smaller handlers (thanks sysko)
daurnimator <quae@daurnimator.com>
parents: 6092
diff changeset
  1015
				:text(_reason or "")
9a7eaf0a35b6 plugins/muc/muc.lib: Split up `handle_to_room` into smaller handlers (thanks sysko)
daurnimator <quae@daurnimator.com>
parents: 6092
diff changeset
  1016
			:up()
9a7eaf0a35b6 plugins/muc/muc.lib: Split up `handle_to_room` into smaller handlers (thanks sysko)
daurnimator <quae@daurnimator.com>
parents: 6092
diff changeset
  1017
			:tag('body') -- Add a plain message for clients which don't support invites
9a7eaf0a35b6 plugins/muc/muc.lib: Split up `handle_to_room` into smaller handlers (thanks sysko)
daurnimator <quae@daurnimator.com>
parents: 6092
diff changeset
  1018
				:text(_from..' invited you to the room '.._to..(_reason and (' ('.._reason..')') or ""))
9a7eaf0a35b6 plugins/muc/muc.lib: Split up `handle_to_room` into smaller handlers (thanks sysko)
daurnimator <quae@daurnimator.com>
parents: 6092
diff changeset
  1019
			:up();
6125
4a35a0281d8a plugins/muc/muc.lib: Send invite out from event: removes '-prepared' from event name
daurnimator <quae@daurnimator.com>
parents: 6124
diff changeset
  1020
		module:fire_event("muc-invite", { room = self, stanza = invite, origin = origin, incoming = stanza });
6098
1d7e5d091980 plugins/muc/muc.lib: Add some missing return values
daurnimator <quae@daurnimator.com>
parents: 6097
diff changeset
  1021
		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
  1022
	else
9a7eaf0a35b6 plugins/muc/muc.lib: Split up `handle_to_room` into smaller handlers (thanks sysko)
daurnimator <quae@daurnimator.com>
parents: 6092
diff changeset
  1023
		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
  1024
		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
  1025
	end
9a7eaf0a35b6 plugins/muc/muc.lib: Split up `handle_to_room` into smaller handlers (thanks sysko)
daurnimator <quae@daurnimator.com>
parents: 6092
diff changeset
  1026
end
9a7eaf0a35b6 plugins/muc/muc.lib: Split up `handle_to_room` into smaller handlers (thanks sysko)
daurnimator <quae@daurnimator.com>
parents: 6092
diff changeset
  1027
6125
4a35a0281d8a plugins/muc/muc.lib: Send invite out from event: removes '-prepared' from event name
daurnimator <quae@daurnimator.com>
parents: 6124
diff changeset
  1028
module:hook("muc-invite", function(event)
4a35a0281d8a plugins/muc/muc.lib: Send invite out from event: removes '-prepared' from event name
daurnimator <quae@daurnimator.com>
parents: 6124
diff changeset
  1029
	event.room:_route_stanza(event.stanza);
4a35a0281d8a plugins/muc/muc.lib: Send invite out from event: removes '-prepared' from event name
daurnimator <quae@daurnimator.com>
parents: 6124
diff changeset
  1030
	return true;
4a35a0281d8a plugins/muc/muc.lib: Send invite out from event: removes '-prepared' from event name
daurnimator <quae@daurnimator.com>
parents: 6124
diff changeset
  1031
end, -1)
4a35a0281d8a plugins/muc/muc.lib: Send invite out from event: removes '-prepared' from event name
daurnimator <quae@daurnimator.com>
parents: 6124
diff changeset
  1032
6121
74bbcef3978e plugins/muc/muc.lib: Add muc-invite-prepared event; Use it for granting affiliations in members only rooms
daurnimator <quae@daurnimator.com>
parents: 6120
diff changeset
  1033
-- When an invite is sent; add an affiliation for the invitee
6125
4a35a0281d8a plugins/muc/muc.lib: Send invite out from event: removes '-prepared' from event name
daurnimator <quae@daurnimator.com>
parents: 6124
diff changeset
  1034
module:hook("muc-invite", function(event)
6121
74bbcef3978e plugins/muc/muc.lib: Add muc-invite-prepared event; Use it for granting affiliations in members only rooms
daurnimator <quae@daurnimator.com>
parents: 6120
diff changeset
  1035
	local room, stanza = event.room, event.stanza
74bbcef3978e plugins/muc/muc.lib: Add muc-invite-prepared event; Use it for granting affiliations in members only rooms
daurnimator <quae@daurnimator.com>
parents: 6120
diff changeset
  1036
	local invitee = stanza.attr.to
74bbcef3978e plugins/muc/muc.lib: Add muc-invite-prepared event; Use it for granting affiliations in members only rooms
daurnimator <quae@daurnimator.com>
parents: 6120
diff changeset
  1037
	if room:get_members_only() and not room:get_affiliation(invitee) then
74bbcef3978e plugins/muc/muc.lib: Add muc-invite-prepared event; Use it for granting affiliations in members only rooms
daurnimator <quae@daurnimator.com>
parents: 6120
diff changeset
  1038
		local from = stanza:get_child("x", "http://jabber.org/protocol/muc#user"):get_child("invite").attr.from
74bbcef3978e plugins/muc/muc.lib: Add muc-invite-prepared event; Use it for granting affiliations in members only rooms
daurnimator <quae@daurnimator.com>
parents: 6120
diff changeset
  1039
		local current_nick = room:get_occupant_jid(from)
74bbcef3978e plugins/muc/muc.lib: Add muc-invite-prepared event; Use it for granting affiliations in members only rooms
daurnimator <quae@daurnimator.com>
parents: 6120
diff changeset
  1040
		log("debug", "%s invited %s into members only room %s, granting membership", from, invitee, room.jid);
74bbcef3978e plugins/muc/muc.lib: Add muc-invite-prepared event; Use it for granting affiliations in members only rooms
daurnimator <quae@daurnimator.com>
parents: 6120
diff changeset
  1041
		room:set_affiliation(from, invitee, "member", nil, "Invited by " .. current_nick)
74bbcef3978e plugins/muc/muc.lib: Add muc-invite-prepared event; Use it for granting affiliations in members only rooms
daurnimator <quae@daurnimator.com>
parents: 6120
diff changeset
  1042
	end
74bbcef3978e plugins/muc/muc.lib: Add muc-invite-prepared event; Use it for granting affiliations in members only rooms
daurnimator <quae@daurnimator.com>
parents: 6120
diff changeset
  1043
end)
74bbcef3978e plugins/muc/muc.lib: Add muc-invite-prepared event; Use it for granting affiliations in members only rooms
daurnimator <quae@daurnimator.com>
parents: 6120
diff changeset
  1044
6123
7f82bbd249fe plugins/muc/muc.lib: Remove `payload` argument from `handle_mediated_*`; extract it from inside.
daurnimator <quae@daurnimator.com>
parents: 6122
diff changeset
  1045
function room_mt:handle_mediated_decline(origin, stanza)
7f82bbd249fe plugins/muc/muc.lib: Remove `payload` argument from `handle_mediated_*`; extract it from inside.
daurnimator <quae@daurnimator.com>
parents: 6122
diff changeset
  1046
	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
  1047
	local declinee = jid_prep(payload.attr.to);
68f53b9a186e plugins/muc/muc: Support mediated declines
daurnimator <quae@daurnimator.com>
parents: 6104
diff changeset
  1048
	if declinee then
68f53b9a186e plugins/muc/muc: Support mediated declines
daurnimator <quae@daurnimator.com>
parents: 6104
diff changeset
  1049
		local from, to = stanza.attr.from, stanza.attr.to;
68f53b9a186e plugins/muc/muc: Support mediated declines
daurnimator <quae@daurnimator.com>
parents: 6104
diff changeset
  1050
		-- TODO: Validate declinee
68f53b9a186e plugins/muc/muc: Support mediated declines
daurnimator <quae@daurnimator.com>
parents: 6104
diff changeset
  1051
		local reason = payload:get_child_text("reason")
68f53b9a186e plugins/muc/muc: Support mediated declines
daurnimator <quae@daurnimator.com>
parents: 6104
diff changeset
  1052
		local decline = st.message({from = to, to = declinee, id = stanza.attr.id})
68f53b9a186e plugins/muc/muc: Support mediated declines
daurnimator <quae@daurnimator.com>
parents: 6104
diff changeset
  1053
			:tag('x', {xmlns='http://jabber.org/protocol/muc#user'})
68f53b9a186e plugins/muc/muc: Support mediated declines
daurnimator <quae@daurnimator.com>
parents: 6104
diff changeset
  1054
				:tag('decline', {from=from})
68f53b9a186e plugins/muc/muc: Support mediated declines
daurnimator <quae@daurnimator.com>
parents: 6104
diff changeset
  1055
					:tag('reason'):text(reason or ""):up()
68f53b9a186e plugins/muc/muc: Support mediated declines
daurnimator <quae@daurnimator.com>
parents: 6104
diff changeset
  1056
				:up()
68f53b9a186e plugins/muc/muc: Support mediated declines
daurnimator <quae@daurnimator.com>
parents: 6104
diff changeset
  1057
			:up()
68f53b9a186e plugins/muc/muc: Support mediated declines
daurnimator <quae@daurnimator.com>
parents: 6104
diff changeset
  1058
			:tag('body') -- Add a plain message for clients which don't support declines
68f53b9a186e plugins/muc/muc: Support mediated declines
daurnimator <quae@daurnimator.com>
parents: 6104
diff changeset
  1059
				:text(from..' declined your invite to the room '..to..(reason and (' ('..reason..')') or ""))
68f53b9a186e plugins/muc/muc: Support mediated declines
daurnimator <quae@daurnimator.com>
parents: 6104
diff changeset
  1060
			:up();
6131
8dd0c6145603 plugins/muc/muc.lib: Add decline event for parity with invite
daurnimator <quae@daurnimator.com>
parents: 6130
diff changeset
  1061
		module:fire_event("muc-decline", { room = self, stanza = decline, origin = origin, incoming = stanza });
6105
68f53b9a186e plugins/muc/muc: Support mediated declines
daurnimator <quae@daurnimator.com>
parents: 6104
diff changeset
  1062
		return true;
68f53b9a186e plugins/muc/muc: Support mediated declines
daurnimator <quae@daurnimator.com>
parents: 6104
diff changeset
  1063
	else
68f53b9a186e plugins/muc/muc: Support mediated declines
daurnimator <quae@daurnimator.com>
parents: 6104
diff changeset
  1064
		origin.send(st.error_reply(stanza, "cancel", "jid-malformed"));
68f53b9a186e plugins/muc/muc: Support mediated declines
daurnimator <quae@daurnimator.com>
parents: 6104
diff changeset
  1065
		return true;
68f53b9a186e plugins/muc/muc: Support mediated declines
daurnimator <quae@daurnimator.com>
parents: 6104
diff changeset
  1066
	end
68f53b9a186e plugins/muc/muc: Support mediated declines
daurnimator <quae@daurnimator.com>
parents: 6104
diff changeset
  1067
end
68f53b9a186e plugins/muc/muc: Support mediated declines
daurnimator <quae@daurnimator.com>
parents: 6104
diff changeset
  1068
6131
8dd0c6145603 plugins/muc/muc.lib: Add decline event for parity with invite
daurnimator <quae@daurnimator.com>
parents: 6130
diff changeset
  1069
module:hook("muc-decline", function(event)
8dd0c6145603 plugins/muc/muc.lib: Add decline event for parity with invite
daurnimator <quae@daurnimator.com>
parents: 6130
diff changeset
  1070
	event.room:_route_stanza(event.stanza);
8dd0c6145603 plugins/muc/muc.lib: Add decline event for parity with invite
daurnimator <quae@daurnimator.com>
parents: 6130
diff changeset
  1071
	return true;
8dd0c6145603 plugins/muc/muc.lib: Add decline event for parity with invite
daurnimator <quae@daurnimator.com>
parents: 6130
diff changeset
  1072
end, -1)
8dd0c6145603 plugins/muc/muc.lib: Add decline event for parity with invite
daurnimator <quae@daurnimator.com>
parents: 6130
diff changeset
  1073
6093
9a7eaf0a35b6 plugins/muc/muc.lib: Split up `handle_to_room` into smaller handlers (thanks sysko)
daurnimator <quae@daurnimator.com>
parents: 6092
diff changeset
  1074
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
  1075
	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
  1076
	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
  1077
		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
  1078
	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
  1079
		return self:handle_kickable(origin, stanza)
6103
25ba4e2b31b3 plugins/muc/muc: Check for mediated invites in a smarter way
daurnimator <quae@daurnimator.com>
parents: 6102
diff changeset
  1080
	elseif type == nil then
25ba4e2b31b3 plugins/muc/muc: Check for mediated invites in a smarter way
daurnimator <quae@daurnimator.com>
parents: 6102
diff changeset
  1081
		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
  1082
		if x then
25ba4e2b31b3 plugins/muc/muc: Check for mediated invites in a smarter way
daurnimator <quae@daurnimator.com>
parents: 6102
diff changeset
  1083
			local payload = x.tags[1];
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
  1084
			if payload == nil then
260a18062cb2 plugins/muc/muc: Rename `handle_invite_to_room` to `handle_mediated_invite`; clean up logic
daurnimator <quae@daurnimator.com>
parents: 6103
diff changeset
  1085
				-- 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
  1086
			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
  1087
				return self:handle_mediated_invite(origin, stanza)
6105
68f53b9a186e plugins/muc/muc: Support mediated declines
daurnimator <quae@daurnimator.com>
parents: 6104
diff changeset
  1088
			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
  1089
				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
  1090
			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
  1091
			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
  1092
			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
  1093
		end
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
  1094
	else
6099
c8a749298d51 plugins/muc/muc.lib: Make use of return values to send service-unavailable errors
daurnimator <quae@daurnimator.com>
parents: 6098
diff changeset
  1095
		return nil;
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
  1096
	end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
  1097
end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
  1098
6111
f8b94903be52 plugins/muc: Provide a reasonable default `route_stanza`
daurnimator <quae@daurnimator.com>
parents: 6108
diff changeset
  1099
function room_mt:route_stanza(stanza)
f8b94903be52 plugins/muc: Provide a reasonable default `route_stanza`
daurnimator <quae@daurnimator.com>
parents: 6108
diff changeset
  1100
	module:send(stanza)
f8b94903be52 plugins/muc: Provide a reasonable default `route_stanza`
daurnimator <quae@daurnimator.com>
parents: 6108
diff changeset
  1101
end
1735
81406277279e MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 1734
diff changeset
  1102
1737
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
  1103
function room_mt:get_affiliation(jid)
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
  1104
	local node, host, resource = jid_split(jid);
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
  1105
	local bare = node and node.."@"..host or host;
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
  1106
	local result = self._affiliations[bare]; -- Affiliations are granted, revoked, and maintained based on the user's bare JID.
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
  1107
	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
  1108
	return result;
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
  1109
end
2006
0c62bed9d338 MUC: Added support for reason messages in role and affiliation changes (e.g., reason for kick, etc).
Waqas Hussain <waqas20@gmail.com>
parents: 2005
diff changeset
  1110
function room_mt:set_affiliation(actor, jid, affiliation, callback, reason)
1742
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
  1111
	jid = jid_bare(jid);
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
  1112
	if affiliation == "none" then affiliation = nil; end
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
  1113
	if affiliation and affiliation ~= "outcast" and affiliation ~= "owner" and affiliation ~= "admin" and affiliation ~= "member" then
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
  1114
		return nil, "modify", "not-acceptable";
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
  1115
	end
4357
d6928b78c548 MUC: Allow affiliation change when argument actor==true in room:set_affiliation().
Waqas Hussain <waqas20@gmail.com>
parents: 4326
diff changeset
  1116
	if actor ~= true then
d6928b78c548 MUC: Allow affiliation change when argument actor==true in room:set_affiliation().
Waqas Hussain <waqas20@gmail.com>
parents: 4326
diff changeset
  1117
		local actor_affiliation = self:get_affiliation(actor);
d6928b78c548 MUC: Allow affiliation change when argument actor==true in room:set_affiliation().
Waqas Hussain <waqas20@gmail.com>
parents: 4326
diff changeset
  1118
		local target_affiliation = self:get_affiliation(jid);
d6928b78c548 MUC: Allow affiliation change when argument actor==true in room:set_affiliation().
Waqas Hussain <waqas20@gmail.com>
parents: 4326
diff changeset
  1119
		if target_affiliation == affiliation then -- no change, shortcut
d6928b78c548 MUC: Allow affiliation change when argument actor==true in room:set_affiliation().
Waqas Hussain <waqas20@gmail.com>
parents: 4326
diff changeset
  1120
			if callback then callback(); end
d6928b78c548 MUC: Allow affiliation change when argument actor==true in room:set_affiliation().
Waqas Hussain <waqas20@gmail.com>
parents: 4326
diff changeset
  1121
			return true;
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
  1122
		end
4357
d6928b78c548 MUC: Allow affiliation change when argument actor==true in room:set_affiliation().
Waqas Hussain <waqas20@gmail.com>
parents: 4326
diff changeset
  1123
		if actor_affiliation ~= "owner" then
5334
da7857891eb8 MUC: Fix affiliation check for admins, and bring it in line with the spec (thanks Maranda).
Waqas Hussain <waqas20@gmail.com>
parents: 5294
diff changeset
  1124
			if affiliation == "owner" or affiliation == "admin" or actor_affiliation ~= "admin" or target_affiliation == "owner" or target_affiliation == "admin" then
4357
d6928b78c548 MUC: Allow affiliation change when argument actor==true in room:set_affiliation().
Waqas Hussain <waqas20@gmail.com>
parents: 4326
diff changeset
  1125
				return nil, "cancel", "not-allowed";
d6928b78c548 MUC: Allow affiliation change when argument actor==true in room:set_affiliation().
Waqas Hussain <waqas20@gmail.com>
parents: 4326
diff changeset
  1126
			end
d6928b78c548 MUC: Allow affiliation change when argument actor==true in room:set_affiliation().
Waqas Hussain <waqas20@gmail.com>
parents: 4326
diff changeset
  1127
		elseif target_affiliation == "owner" and jid_bare(actor) == jid then -- self change
d6928b78c548 MUC: Allow affiliation change when argument actor==true in room:set_affiliation().
Waqas Hussain <waqas20@gmail.com>
parents: 4326
diff changeset
  1128
			local is_last = true;
d6928b78c548 MUC: Allow affiliation change when argument actor==true in room:set_affiliation().
Waqas Hussain <waqas20@gmail.com>
parents: 4326
diff changeset
  1129
			for j, aff in pairs(self._affiliations) do if j ~= jid and aff == "owner" then is_last = false; break; end end
d6928b78c548 MUC: Allow affiliation change when argument actor==true in room:set_affiliation().
Waqas Hussain <waqas20@gmail.com>
parents: 4326
diff changeset
  1130
			if is_last then
d6928b78c548 MUC: Allow affiliation change when argument actor==true in room:set_affiliation().
Waqas Hussain <waqas20@gmail.com>
parents: 4326
diff changeset
  1131
				return nil, "cancel", "conflict";
d6928b78c548 MUC: Allow affiliation change when argument actor==true in room:set_affiliation().
Waqas Hussain <waqas20@gmail.com>
parents: 4326
diff changeset
  1132
			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
  1133
		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
  1134
	end
1742
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
  1135
	self._affiliations[jid] = affiliation;
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
  1136
	local role = self:get_default_role(affiliation);
3631
138f385e8246 MUC: Include the user's current presence contents when broadcasting an affiliation change.
Waqas Hussain <waqas20@gmail.com>
parents: 3629
diff changeset
  1137
	local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user"})
2006
0c62bed9d338 MUC: Added support for reason messages in role and affiliation changes (e.g., reason for kick, etc).
Waqas Hussain <waqas20@gmail.com>
parents: 2005
diff changeset
  1138
			:tag("item", {affiliation=affiliation or "none", role=role or "none"})
0c62bed9d338 MUC: Added support for reason messages in role and affiliation changes (e.g., reason for kick, etc).
Waqas Hussain <waqas20@gmail.com>
parents: 2005
diff changeset
  1139
				:tag("reason"):text(reason or ""):up()
0c62bed9d338 MUC: Added support for reason messages in role and affiliation changes (e.g., reason for kick, etc).
Waqas Hussain <waqas20@gmail.com>
parents: 2005
diff changeset
  1140
			:up();
3631
138f385e8246 MUC: Include the user's current presence contents when broadcasting an affiliation change.
Waqas Hussain <waqas20@gmail.com>
parents: 3629
diff changeset
  1141
	local presence_type = nil;
1742
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
  1142
	if not role then -- getting kicked
3631
138f385e8246 MUC: Include the user's current presence contents when broadcasting an affiliation change.
Waqas Hussain <waqas20@gmail.com>
parents: 3629
diff changeset
  1143
		presence_type = "unavailable";
1742
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
  1144
		if affiliation == "outcast" then
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
  1145
			x:tag("status", {code="301"}):up(); -- banned
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
  1146
		else
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
  1147
			x:tag("status", {code="321"}):up(); -- affiliation change
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
  1148
		end
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
  1149
	end
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
  1150
	local modified_nicks = {};
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
  1151
	for nick, occupant in pairs(self._occupants) do
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
  1152
		if jid_bare(occupant.jid) == jid then
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
  1153
			if not role then -- getting kicked
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
  1154
				self._occupants[nick] = nil;
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
  1155
			else
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
  1156
				occupant.affiliation, occupant.role = affiliation, role;
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
  1157
			end
3631
138f385e8246 MUC: Include the user's current presence contents when broadcasting an affiliation change.
Waqas Hussain <waqas20@gmail.com>
parents: 3629
diff changeset
  1158
			for jid,pres in pairs(occupant.sessions) do -- remove for all sessions of the nick
1742
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
  1159
				if not role then self._jid_nick[jid] = nil; end
3631
138f385e8246 MUC: Include the user's current presence contents when broadcasting an affiliation change.
Waqas Hussain <waqas20@gmail.com>
parents: 3629
diff changeset
  1160
				local p = st.clone(pres);
138f385e8246 MUC: Include the user's current presence contents when broadcasting an affiliation change.
Waqas Hussain <waqas20@gmail.com>
parents: 3629
diff changeset
  1161
				p.attr.from = nick;
138f385e8246 MUC: Include the user's current presence contents when broadcasting an affiliation change.
Waqas Hussain <waqas20@gmail.com>
parents: 3629
diff changeset
  1162
				p.attr.type = presence_type;
1742
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
  1163
				p.attr.to = jid;
3631
138f385e8246 MUC: Include the user's current presence contents when broadcasting an affiliation change.
Waqas Hussain <waqas20@gmail.com>
parents: 3629
diff changeset
  1164
				p:add_child(x);
2064
1ee862fd1afe MUC: Include occupants' real JIDs in their presence (semi-anonymous rooms).
Waqas Hussain <waqas20@gmail.com>
parents: 2053
diff changeset
  1165
				self:_route_stanza(p);
3631
138f385e8246 MUC: Include the user's current presence contents when broadcasting an affiliation change.
Waqas Hussain <waqas20@gmail.com>
parents: 3629
diff changeset
  1166
				if occupant.jid == jid then
138f385e8246 MUC: Include the user's current presence contents when broadcasting an affiliation change.
Waqas Hussain <waqas20@gmail.com>
parents: 3629
diff changeset
  1167
					modified_nicks[nick] = p;
138f385e8246 MUC: Include the user's current presence contents when broadcasting an affiliation change.
Waqas Hussain <waqas20@gmail.com>
parents: 3629
diff changeset
  1168
				end
1742
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
  1169
			end
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
  1170
		end
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
  1171
	end
1755
1614e8e62ad5 MUC: Fixed an undefined global access.
Waqas Hussain <waqas20@gmail.com>
parents: 1754
diff changeset
  1172
	if self.save then self:save(); end
1742
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
  1173
	if callback then callback(); end
3631
138f385e8246 MUC: Include the user's current presence contents when broadcasting an affiliation change.
Waqas Hussain <waqas20@gmail.com>
parents: 3629
diff changeset
  1174
	for nick,p in pairs(modified_nicks) do
1742
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
  1175
		p.attr.from = nick;
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
  1176
		self:broadcast_except_nick(p, nick);
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
  1177
	end
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
  1178
	return true;
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
  1179
end
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
  1180
1742
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
  1181
function room_mt:get_role(nick)
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
  1182
	local session = self._occupants[nick];
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
  1183
	return session and session.role or nil;
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
  1184
end
3279
8b0a4a7d2c6e MUC: Added room:can_set_role().
Waqas Hussain <waqas20@gmail.com>
parents: 3264
diff changeset
  1185
function room_mt:can_set_role(actor_jid, occupant_jid, role)
8b0a4a7d2c6e MUC: Added room:can_set_role().
Waqas Hussain <waqas20@gmail.com>
parents: 3264
diff changeset
  1186
	local occupant = self._occupants[occupant_jid];
5680
24b044f6e06d mod_muc: Fix incorrect variable name
Matthew Wild <mwild1@gmail.com>
parents: 5577
diff changeset
  1187
	if not occupant or not actor_jid then return nil, "modify", "not-acceptable"; end
3279
8b0a4a7d2c6e MUC: Added room:can_set_role().
Waqas Hussain <waqas20@gmail.com>
parents: 3264
diff changeset
  1188
5542
329ebdfb39a2 MUC: Allow actor == true to set roles (like affiliations)
Matthew Wild <mwild1@gmail.com>
parents: 5541
diff changeset
  1189
	if actor_jid == true then return true; end
329ebdfb39a2 MUC: Allow actor == true to set roles (like affiliations)
Matthew Wild <mwild1@gmail.com>
parents: 5541
diff changeset
  1190
6119
c13f5d6b9b16 plugins/muc/muc.lib: Use `get_occupant_jid` method instead of indexing _jid_nick
daurnimator <quae@daurnimator.com>
parents: 6118
diff changeset
  1191
	local actor = self._occupants[self:get_occupant_jid(actor_jid)];
3279
8b0a4a7d2c6e MUC: Added room:can_set_role().
Waqas Hussain <waqas20@gmail.com>
parents: 3264
diff changeset
  1192
	if actor.role == "moderator" then
8b0a4a7d2c6e MUC: Added room:can_set_role().
Waqas Hussain <waqas20@gmail.com>
parents: 3264
diff changeset
  1193
		if occupant.affiliation ~= "owner" and occupant.affiliation ~= "admin" then
8b0a4a7d2c6e MUC: Added room:can_set_role().
Waqas Hussain <waqas20@gmail.com>
parents: 3264
diff changeset
  1194
			if actor.affiliation == "owner" or actor.affiliation == "admin" then
8b0a4a7d2c6e MUC: Added room:can_set_role().
Waqas Hussain <waqas20@gmail.com>
parents: 3264
diff changeset
  1195
				return true;
8b0a4a7d2c6e MUC: Added room:can_set_role().
Waqas Hussain <waqas20@gmail.com>
parents: 3264
diff changeset
  1196
			elseif occupant.role ~= "moderator" and role ~= "moderator" then
8b0a4a7d2c6e MUC: Added room:can_set_role().
Waqas Hussain <waqas20@gmail.com>
parents: 3264
diff changeset
  1197
				return true;
8b0a4a7d2c6e MUC: Added room:can_set_role().
Waqas Hussain <waqas20@gmail.com>
parents: 3264
diff changeset
  1198
			end
8b0a4a7d2c6e MUC: Added room:can_set_role().
Waqas Hussain <waqas20@gmail.com>
parents: 3264
diff changeset
  1199
		end
8b0a4a7d2c6e MUC: Added room:can_set_role().
Waqas Hussain <waqas20@gmail.com>
parents: 3264
diff changeset
  1200
	end
8b0a4a7d2c6e MUC: Added room:can_set_role().
Waqas Hussain <waqas20@gmail.com>
parents: 3264
diff changeset
  1201
	return nil, "cancel", "not-allowed";
8b0a4a7d2c6e MUC: Added room:can_set_role().
Waqas Hussain <waqas20@gmail.com>
parents: 3264
diff changeset
  1202
end
2845
f76139aa7cd5 MUC: muc.lib.lua: Fix the sending of the occupant JID instead of the nick in role lists and presence broadcasts after role changes (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents: 2174
diff changeset
  1203
function room_mt:set_role(actor, occupant_jid, role, callback, reason)
1752
4db786919805 MUC: Added kicking support.
Waqas Hussain <waqas20@gmail.com>
parents: 1751
diff changeset
  1204
	if role == "none" then role = nil; end
1742
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
  1205
	if role and role ~= "moderator" and role ~= "participant" and role ~= "visitor" then return nil, "modify", "not-acceptable"; end
3280
eef4f31f2d7b MUC: Updated room:set_role() to use room:can_set_role().
Waqas Hussain <waqas20@gmail.com>
parents: 3279
diff changeset
  1206
	local allowed, err_type, err_condition = self:can_set_role(actor, occupant_jid, role);
eef4f31f2d7b MUC: Updated room:set_role() to use room:can_set_role().
Waqas Hussain <waqas20@gmail.com>
parents: 3279
diff changeset
  1207
	if not allowed then return allowed, err_type, err_condition; end
2845
f76139aa7cd5 MUC: muc.lib.lua: Fix the sending of the occupant JID instead of the nick in role lists and presence broadcasts after role changes (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents: 2174
diff changeset
  1208
	local occupant = self._occupants[occupant_jid];
3632
d82189efecc0 MUC: Include the user's current presence contents when broadcasting a role change.
Waqas Hussain <waqas20@gmail.com>
parents: 3631
diff changeset
  1209
	local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user"})
2845
f76139aa7cd5 MUC: muc.lib.lua: Fix the sending of the occupant JID instead of the nick in role lists and presence broadcasts after role changes (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents: 2174
diff changeset
  1210
			:tag("item", {affiliation=occupant.affiliation or "none", nick=select(3, jid_split(occupant_jid)), role=role or "none"})
2006
0c62bed9d338 MUC: Added support for reason messages in role and affiliation changes (e.g., reason for kick, etc).
Waqas Hussain <waqas20@gmail.com>
parents: 2005
diff changeset
  1211
				:tag("reason"):text(reason or ""):up()
0c62bed9d338 MUC: Added support for reason messages in role and affiliation changes (e.g., reason for kick, etc).
Waqas Hussain <waqas20@gmail.com>
parents: 2005
diff changeset
  1212
			:up();
3632
d82189efecc0 MUC: Include the user's current presence contents when broadcasting a role change.
Waqas Hussain <waqas20@gmail.com>
parents: 3631
diff changeset
  1213
	local presence_type = nil;
1742
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
  1214
	if not role then -- kick
3632
d82189efecc0 MUC: Include the user's current presence contents when broadcasting a role change.
Waqas Hussain <waqas20@gmail.com>
parents: 3631
diff changeset
  1215
		presence_type = "unavailable";
2845
f76139aa7cd5 MUC: muc.lib.lua: Fix the sending of the occupant JID instead of the nick in role lists and presence broadcasts after role changes (thanks teo)
Matthew Wild <mwild1@gmail.com>
parents: 2174
diff changeset
  1216
		self._occupants[occupant_jid] = nil;
1742
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
  1217
		for jid in pairs(occupant.sessions) do -- remove for all sessions of the nick
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
  1218
			self._jid_nick[jid] = nil;
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
  1219
		end
3632
d82189efecc0 MUC: Include the user's current presence contents when broadcasting a role change.
Waqas Hussain <waqas20@gmail.com>
parents: 3631
diff changeset
  1220
		x:tag("status", {code = "307"}):up();
1742
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
  1221
	else
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
  1222
		occupant.role = role;
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
  1223
	end
3632
d82189efecc0 MUC: Include the user's current presence contents when broadcasting a role change.
Waqas Hussain <waqas20@gmail.com>
parents: 3631
diff changeset
  1224
	local bp;
d82189efecc0 MUC: Include the user's current presence contents when broadcasting a role change.
Waqas Hussain <waqas20@gmail.com>
parents: 3631
diff changeset
  1225
	for jid,pres in pairs(occupant.sessions) do -- send to all sessions of the nick
d82189efecc0 MUC: Include the user's current presence contents when broadcasting a role change.
Waqas Hussain <waqas20@gmail.com>
parents: 3631
diff changeset
  1226
		local p = st.clone(pres);
d82189efecc0 MUC: Include the user's current presence contents when broadcasting a role change.
Waqas Hussain <waqas20@gmail.com>
parents: 3631
diff changeset
  1227
		p.attr.from = occupant_jid;
d82189efecc0 MUC: Include the user's current presence contents when broadcasting a role change.
Waqas Hussain <waqas20@gmail.com>
parents: 3631
diff changeset
  1228
		p.attr.type = presence_type;
1742
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
  1229
		p.attr.to = jid;
3632
d82189efecc0 MUC: Include the user's current presence contents when broadcasting a role change.
Waqas Hussain <waqas20@gmail.com>
parents: 3631
diff changeset
  1230
		p:add_child(x);
2064
1ee862fd1afe MUC: Include occupants' real JIDs in their presence (semi-anonymous rooms).
Waqas Hussain <waqas20@gmail.com>
parents: 2053
diff changeset
  1231
		self:_route_stanza(p);
3632
d82189efecc0 MUC: Include the user's current presence contents when broadcasting a role change.
Waqas Hussain <waqas20@gmail.com>
parents: 3631
diff changeset
  1232
		if occupant.jid == jid then
d82189efecc0 MUC: Include the user's current presence contents when broadcasting a role change.
Waqas Hussain <waqas20@gmail.com>
parents: 3631
diff changeset
  1233
			bp = p;
d82189efecc0 MUC: Include the user's current presence contents when broadcasting a role change.
Waqas Hussain <waqas20@gmail.com>
parents: 3631
diff changeset
  1234
		end
1742
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
  1235
	end
1483a62d69bb MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1740
diff changeset
  1236
	if callback then callback(); end
3632
d82189efecc0 MUC: Include the user's current presence contents when broadcasting a role change.
Waqas Hussain <waqas20@gmail.com>
parents: 3631
diff changeset
  1237
	if bp then
d82189efecc0 MUC: Include the user's current presence contents when broadcasting a role change.
Waqas Hussain <waqas20@gmail.com>
parents: 3631
diff changeset
  1238
		self:broadcast_except_nick(bp, occupant_jid);
d82189efecc0 MUC: Include the user's current presence contents when broadcasting a role change.
Waqas Hussain <waqas20@gmail.com>
parents: 3631
diff changeset
  1239
	end
1737
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
  1240
	return true;
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
  1241
end
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
  1242
2064
1ee862fd1afe MUC: Include occupants' real JIDs in their presence (semi-anonymous rooms).
Waqas Hussain <waqas20@gmail.com>
parents: 2053
diff changeset
  1243
function room_mt:_route_stanza(stanza)
1ee862fd1afe MUC: Include occupants' real JIDs in their presence (semi-anonymous rooms).
Waqas Hussain <waqas20@gmail.com>
parents: 2053
diff changeset
  1244
	local muc_child;
1ee862fd1afe MUC: Include occupants' real JIDs in their presence (semi-anonymous rooms).
Waqas Hussain <waqas20@gmail.com>
parents: 2053
diff changeset
  1245
	if stanza.name == "presence" then
6119
c13f5d6b9b16 plugins/muc/muc.lib: Use `get_occupant_jid` method instead of indexing _jid_nick
daurnimator <quae@daurnimator.com>
parents: 6118
diff changeset
  1246
		local to_occupant = self._occupants[self:get_occupant_jid(stanza.attr.to)];
6117
d7ade0b00ec5 plugins/muc/muc.lib: In `_route_stanza` access occupant data less often
daurnimator <quae@daurnimator.com>
parents: 6116
diff changeset
  1247
		local from_occupant = self._occupants[stanza.attr.from];
2064
1ee862fd1afe MUC: Include occupants' real JIDs in their presence (semi-anonymous rooms).
Waqas Hussain <waqas20@gmail.com>
parents: 2053
diff changeset
  1248
		if to_occupant and from_occupant then
6118
aae3d6daa50d plugins/muc/muc.lib: Fetch config via accessors instead of using `_data`
daurnimator <quae@daurnimator.com>
parents: 6117
diff changeset
  1249
			if self:get_whois() == 'anyone' then
2416
89be536aae25 mod_muc/muc.lib: Use stanza:get_child() to locate MUC child element
Matthew Wild <mwild1@gmail.com>
parents: 2412
diff changeset
  1250
			    muc_child = stanza:get_child("x", "http://jabber.org/protocol/muc#user");
2411
c2b6c55201af Add support for non-anonymous MUC rooms
Rob Hoelz <rob@hoelzro.net>
parents: 2217
diff changeset
  1251
			else
c2b6c55201af Add support for non-anonymous MUC rooms
Rob Hoelz <rob@hoelzro.net>
parents: 2217
diff changeset
  1252
				if to_occupant.role == "moderator" or jid_bare(to_occupant.jid) == jid_bare(from_occupant.jid) then
2416
89be536aae25 mod_muc/muc.lib: Use stanza:get_child() to locate MUC child element
Matthew Wild <mwild1@gmail.com>
parents: 2412
diff changeset
  1253
					muc_child = stanza:get_child("x", "http://jabber.org/protocol/muc#user");
2064
1ee862fd1afe MUC: Include occupants' real JIDs in their presence (semi-anonymous rooms).
Waqas Hussain <waqas20@gmail.com>
parents: 2053
diff changeset
  1254
				end
1ee862fd1afe MUC: Include occupants' real JIDs in their presence (semi-anonymous rooms).
Waqas Hussain <waqas20@gmail.com>
parents: 2053
diff changeset
  1255
			end
1ee862fd1afe MUC: Include occupants' real JIDs in their presence (semi-anonymous rooms).
Waqas Hussain <waqas20@gmail.com>
parents: 2053
diff changeset
  1256
		end
6117
d7ade0b00ec5 plugins/muc/muc.lib: In `_route_stanza` access occupant data less often
daurnimator <quae@daurnimator.com>
parents: 6116
diff changeset
  1257
		if muc_child then
d7ade0b00ec5 plugins/muc/muc.lib: In `_route_stanza` access occupant data less often
daurnimator <quae@daurnimator.com>
parents: 6116
diff changeset
  1258
			for item in muc_child:childtags("item") do
d7ade0b00ec5 plugins/muc/muc.lib: In `_route_stanza` access occupant data less often
daurnimator <quae@daurnimator.com>
parents: 6116
diff changeset
  1259
				if from_occupant == to_occupant then
d7ade0b00ec5 plugins/muc/muc.lib: In `_route_stanza` access occupant data less often
daurnimator <quae@daurnimator.com>
parents: 6116
diff changeset
  1260
					item.attr.jid = stanza.attr.to;
d7ade0b00ec5 plugins/muc/muc.lib: In `_route_stanza` access occupant data less often
daurnimator <quae@daurnimator.com>
parents: 6116
diff changeset
  1261
				else
d7ade0b00ec5 plugins/muc/muc.lib: In `_route_stanza` access occupant data less often
daurnimator <quae@daurnimator.com>
parents: 6116
diff changeset
  1262
					item.attr.jid = from_occupant.jid;
d7ade0b00ec5 plugins/muc/muc.lib: In `_route_stanza` access occupant data less often
daurnimator <quae@daurnimator.com>
parents: 6116
diff changeset
  1263
				end
2064
1ee862fd1afe MUC: Include occupants' real JIDs in their presence (semi-anonymous rooms).
Waqas Hussain <waqas20@gmail.com>
parents: 2053
diff changeset
  1264
			end
1ee862fd1afe MUC: Include occupants' real JIDs in their presence (semi-anonymous rooms).
Waqas Hussain <waqas20@gmail.com>
parents: 2053
diff changeset
  1265
		end
1ee862fd1afe MUC: Include occupants' real JIDs in their presence (semi-anonymous rooms).
Waqas Hussain <waqas20@gmail.com>
parents: 2053
diff changeset
  1266
	end
1ee862fd1afe MUC: Include occupants' real JIDs in their presence (semi-anonymous rooms).
Waqas Hussain <waqas20@gmail.com>
parents: 2053
diff changeset
  1267
	self:route_stanza(stanza);
1ee862fd1afe MUC: Include occupants' real JIDs in their presence (semi-anonymous rooms).
Waqas Hussain <waqas20@gmail.com>
parents: 2053
diff changeset
  1268
	if muc_child then
6112
819e00a86239 plugins/muc/muc.lib: Use more modern stanza methods
daurnimator <quae@daurnimator.com>
parents: 6111
diff changeset
  1269
		for item in muc_child:childtags("item") do
819e00a86239 plugins/muc/muc.lib: Use more modern stanza methods
daurnimator <quae@daurnimator.com>
parents: 6111
diff changeset
  1270
			item.attr.jid = nil;
2064
1ee862fd1afe MUC: Include occupants' real JIDs in their presence (semi-anonymous rooms).
Waqas Hussain <waqas20@gmail.com>
parents: 2053
diff changeset
  1271
		end
1ee862fd1afe MUC: Include occupants' real JIDs in their presence (semi-anonymous rooms).
Waqas Hussain <waqas20@gmail.com>
parents: 2053
diff changeset
  1272
	end
1ee862fd1afe MUC: Include occupants' real JIDs in their presence (semi-anonymous rooms).
Waqas Hussain <waqas20@gmail.com>
parents: 2053
diff changeset
  1273
end
1ee862fd1afe MUC: Include occupants' real JIDs in their presence (semi-anonymous rooms).
Waqas Hussain <waqas20@gmail.com>
parents: 2053
diff changeset
  1274
1737
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
  1275
local _M = {}; -- module "muc"
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
  1276
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
  1277
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
  1278
	return setmetatable({
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
  1279
		jid = jid;
6129
6c66571ab0f9 plugins/muc: Have utility methods for locking the room
daurnimator <quae@daurnimator.com>
parents: 6128
diff changeset
  1280
		locked = nil;
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
  1281
		_jid_nick = {};
1739
393abf245322 MUC: Renamed _participants table to _occupants
Waqas Hussain <waqas20@gmail.com>
parents: 1737
diff changeset
  1282
		_occupants = {};
2411
c2b6c55201af Add support for non-anonymous MUC rooms
Rob Hoelz <rob@hoelzro.net>
parents: 2217
diff changeset
  1283
		_data = {
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
  1284
		    whois = 'moderators';
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
  1285
		    history_length = math.min((config and config.history_length)
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
  1286
		    	or default_history_length, max_history_length);
2411
c2b6c55201af Add support for non-anonymous MUC rooms
Rob Hoelz <rob@hoelzro.net>
parents: 2217
diff changeset
  1287
		};
1737
31c3eb5797c7 MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents: 1736
diff changeset
  1288
		_affiliations = {};
1735
81406277279e MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 1734
diff changeset
  1289
	}, room_mt);
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
  1290
end
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
  1291
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
  1292
function _M.set_max_history_length(_max_history_length)
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
  1293
	max_history_length = _max_history_length or math.huge;
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
  1294
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
  1295
5063
4bc202a7b351 MUC: Expose room metatable in the MUC lib.
Waqas Hussain <waqas20@gmail.com>
parents: 5061
diff changeset
  1296
_M.room_mt = room_mt;
4bc202a7b351 MUC: Expose room metatable in the MUC lib.
Waqas Hussain <waqas20@gmail.com>
parents: 5061
diff changeset
  1297
1734
34ac9ba0aad6 MUC: Added initial MUC lib
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
  1298
return _M;