plugins/muc/subject.lib.lua
author Kim Alvefur <zash@zash.se>
Thu, 14 Apr 2016 21:23:09 +0200
changeset 7355 50b24b3476e6
parent 6994 84e01dbb739e
child 7356 ca31d3271cf8
permissions -rw-r--r--
MUC: Provide a noop stub room:save() method
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6224
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
     1
-- Prosody IM
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
     2
-- Copyright (C) 2008-2010 Matthew Wild
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
     3
-- Copyright (C) 2008-2010 Waqas Hussain
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
     4
-- Copyright (C) 2014 Daurnimator
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
     5
--
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
     6
-- This project is MIT/X11 licensed. Please see the
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
     7
-- COPYING file in the source package for more information.
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
     8
--
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
     9
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
    10
local st = require "util.stanza";
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
    11
6432
675aea867574 plugins/muc: Add muc-occupant-groupchat event
daurnimator <quae@daurnimator.com>
parents: 6277
diff changeset
    12
local muc_util = module:require "muc/util";
675aea867574 plugins/muc: Add muc-occupant-groupchat event
daurnimator <quae@daurnimator.com>
parents: 6277
diff changeset
    13
local valid_roles = muc_util.valid_roles;
675aea867574 plugins/muc: Add muc-occupant-groupchat event
daurnimator <quae@daurnimator.com>
parents: 6277
diff changeset
    14
6224
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
    15
local function create_subject_message(from, subject)
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
    16
	return st.message({from = from; type = "groupchat"})
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
    17
		:tag("subject"):text(subject):up();
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
    18
end
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
    19
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
    20
local function get_changesubject(room)
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
    21
	return room._data.changesubject;
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
    22
end
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
    23
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
    24
local function set_changesubject(room, changesubject)
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
    25
	changesubject = changesubject and true or nil;
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
    26
	if get_changesubject(room) == changesubject then return false; end
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
    27
	room._data.changesubject = changesubject;
7355
50b24b3476e6 MUC: Provide a noop stub room:save() method
Kim Alvefur <zash@zash.se>
parents: 6994
diff changeset
    28
	room:save(true);
6224
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
    29
	return true;
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
    30
end
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
    31
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
    32
module:hook("muc-config-form", function(event)
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
    33
	table.insert(event.form, {
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
    34
		name = "muc#roomconfig_changesubject";
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
    35
		type = "boolean";
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
    36
		label = "Allow Occupants to Change Subject?";
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
    37
		value = get_changesubject(event.room);
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
    38
	});
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
    39
end);
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
    40
6994
84e01dbb739e MUC: Update all config form handlers to take advantage of the new per-option events
Matthew Wild <mwild1@gmail.com>
parents: 6432
diff changeset
    41
module:hook("muc-config-submitted/muc#roomconfig_changesubject", function(event)
84e01dbb739e MUC: Update all config form handlers to take advantage of the new per-option events
Matthew Wild <mwild1@gmail.com>
parents: 6432
diff changeset
    42
	if set_changesubject(event.room, event.value) then
6224
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
    43
		event.status_codes["104"] = true;
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
    44
	end
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
    45
end);
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
    46
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
    47
local function get_subject(room)
6228
4968d18e2c1e plugins/muc/subject.lib: If subject is not set by an occupant, it should come from room jid itself
daurnimator <quae@daurnimator.com>
parents: 6224
diff changeset
    48
	-- a <message/> stanza from the room JID (or from the occupant JID of the entity that set the subject)
4968d18e2c1e plugins/muc/subject.lib: If subject is not set by an occupant, it should come from room jid itself
daurnimator <quae@daurnimator.com>
parents: 6224
diff changeset
    49
	return room._data.subject_from or room.jid, room._data.subject;
6224
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
    50
end
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
    51
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
    52
local function send_subject(room, to)
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
    53
	local msg = create_subject_message(get_subject(room));
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
    54
	msg.attr.to = to;
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
    55
	room:route_stanza(msg);
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
    56
end
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
    57
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
    58
local function set_subject(room, from, subject)
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
    59
	if subject == "" then subject = nil; end
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
    60
	local old_from, old_subject = get_subject(room);
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
    61
	if old_subject == subject and old_from == from then return false; end
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
    62
	room._data.subject_from = from;
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
    63
	room._data.subject = subject;
7355
50b24b3476e6 MUC: Provide a noop stub room:save() method
Kim Alvefur <zash@zash.se>
parents: 6994
diff changeset
    64
	room:save();
6224
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
    65
	local msg = create_subject_message(from, subject);
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
    66
	room:broadcast_message(msg);
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
    67
	return true;
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
    68
end
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
    69
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
    70
-- Send subject to joining user
6277
f2c9c36979b3 plugins/muc: Fix use of incorrect event on occupant join
daurnimator <quae@daurnimator.com>
parents: 6228
diff changeset
    71
module:hook("muc-occupant-session-new", function(event)
6228
4968d18e2c1e plugins/muc/subject.lib: If subject is not set by an occupant, it should come from room jid itself
daurnimator <quae@daurnimator.com>
parents: 6224
diff changeset
    72
	send_subject(event.room, event.stanza.attr.from);
6224
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
    73
end, 20);
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
    74
6432
675aea867574 plugins/muc: Add muc-occupant-groupchat event
daurnimator <quae@daurnimator.com>
parents: 6277
diff changeset
    75
-- Prosody has made the decision that messages with <subject/> are exclusively subject changes
675aea867574 plugins/muc: Add muc-occupant-groupchat event
daurnimator <quae@daurnimator.com>
parents: 6277
diff changeset
    76
-- e.g. body will be ignored; even if the subject change was not allowed
675aea867574 plugins/muc: Add muc-occupant-groupchat event
daurnimator <quae@daurnimator.com>
parents: 6277
diff changeset
    77
module:hook("muc-occupant-groupchat", function(event)
675aea867574 plugins/muc: Add muc-occupant-groupchat event
daurnimator <quae@daurnimator.com>
parents: 6277
diff changeset
    78
	local stanza = event.stanza;
675aea867574 plugins/muc: Add muc-occupant-groupchat event
daurnimator <quae@daurnimator.com>
parents: 6277
diff changeset
    79
	local subject = stanza:get_child("subject");
675aea867574 plugins/muc: Add muc-occupant-groupchat event
daurnimator <quae@daurnimator.com>
parents: 6277
diff changeset
    80
	if subject then
675aea867574 plugins/muc: Add muc-occupant-groupchat event
daurnimator <quae@daurnimator.com>
parents: 6277
diff changeset
    81
		local occupant = event.occupant;
675aea867574 plugins/muc: Add muc-occupant-groupchat event
daurnimator <quae@daurnimator.com>
parents: 6277
diff changeset
    82
		-- Role check for subject changes
675aea867574 plugins/muc: Add muc-occupant-groupchat event
daurnimator <quae@daurnimator.com>
parents: 6277
diff changeset
    83
		local role_rank = valid_roles[occupant and occupant.role or "none"];
675aea867574 plugins/muc: Add muc-occupant-groupchat event
daurnimator <quae@daurnimator.com>
parents: 6277
diff changeset
    84
		if role_rank >= valid_roles.moderator or
675aea867574 plugins/muc: Add muc-occupant-groupchat event
daurnimator <quae@daurnimator.com>
parents: 6277
diff changeset
    85
			( role_rank >= valid_roles.participant and get_changesubject(event.room) ) then -- and participant
675aea867574 plugins/muc: Add muc-occupant-groupchat event
daurnimator <quae@daurnimator.com>
parents: 6277
diff changeset
    86
			set_subject(event.room, occupant.nick, subject:get_text());
675aea867574 plugins/muc: Add muc-occupant-groupchat event
daurnimator <quae@daurnimator.com>
parents: 6277
diff changeset
    87
			return true;
675aea867574 plugins/muc: Add muc-occupant-groupchat event
daurnimator <quae@daurnimator.com>
parents: 6277
diff changeset
    88
		else
675aea867574 plugins/muc: Add muc-occupant-groupchat event
daurnimator <quae@daurnimator.com>
parents: 6277
diff changeset
    89
			event.origin.send(st.error_reply(stanza, "auth", "forbidden"));
675aea867574 plugins/muc: Add muc-occupant-groupchat event
daurnimator <quae@daurnimator.com>
parents: 6277
diff changeset
    90
			return true;
675aea867574 plugins/muc: Add muc-occupant-groupchat event
daurnimator <quae@daurnimator.com>
parents: 6277
diff changeset
    91
		end
6224
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
    92
	end
6432
675aea867574 plugins/muc: Add muc-occupant-groupchat event
daurnimator <quae@daurnimator.com>
parents: 6277
diff changeset
    93
end, 20);
6224
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
    94
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
    95
return {
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
    96
	get_changesubject = get_changesubject;
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
    97
	set_changesubject = set_changesubject;
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
    98
	get = get_subject;
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
    99
	set = set_subject;
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
   100
	send = send_subject;
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
   101
};