2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 -- Copyright (C) 2014 Daurnimator
6 -- This project is MIT/X11 licensed. Please see the
7 -- COPYING file in the source package for more information.
10 local st = require "util.stanza";
12 local muc_util = module:require "muc/util";
13 local valid_roles = muc_util.valid_roles;
15 local function create_subject_message(from, subject)
16 return st.message({from = from; type = "groupchat"})
17 :tag("subject"):text(subject):up();
20 local function get_changesubject(room)
21 return room._data.changesubject;
24 local function set_changesubject(room, changesubject)
25 changesubject = changesubject and true or nil;
26 if get_changesubject(room) == changesubject then return false; end
27 room._data.changesubject = changesubject;
32 module:hook("muc-config-form", function(event)
33 table.insert(event.form, {
34 name = "muc#roomconfig_changesubject";
36 label = "Allow Occupants to Change Subject?";
37 value = get_changesubject(event.room);
41 module:hook("muc-config-submitted/muc#roomconfig_changesubject", function(event)
42 if set_changesubject(event.room, event.value) then
43 event.status_codes["104"] = true;
47 local function get_subject(room)
48 -- a <message/> stanza from the room JID (or from the occupant JID of the entity that set the subject)
49 return room._data.subject_from or room.jid, room._data.subject;
52 local function send_subject(room, to)
53 local msg = create_subject_message(get_subject(room));
55 room:route_stanza(msg);
58 local function set_subject(room, from, subject)
59 if subject == "" then subject = nil; end
60 local old_from, old_subject = get_subject(room);
61 if old_subject == subject and old_from == from then return false; end
62 room._data.subject_from = from;
63 room._data.subject = subject;
65 local msg = create_subject_message(from, subject);
66 room:broadcast_message(msg);
70 -- Send subject to joining user
71 module:hook("muc-occupant-session-new", function(event)
72 send_subject(event.room, event.stanza.attr.from);
75 -- Prosody has made the decision that messages with <subject/> are exclusively subject changes
76 -- e.g. body will be ignored; even if the subject change was not allowed
77 module:hook("muc-occupant-groupchat", function(event)
78 local stanza = event.stanza;
79 local subject = stanza:get_child("subject");
81 local occupant = event.occupant;
82 -- Role check for subject changes
83 local role_rank = valid_roles[occupant and occupant.role or "none"];
84 if role_rank >= valid_roles.moderator or
85 ( role_rank >= valid_roles.participant and get_changesubject(event.room) ) then -- and participant
86 set_subject(event.room, occupant.nick, subject:get_text());
89 event.origin.send(st.error_reply(stanza, "auth", "forbidden"));
96 get_changesubject = get_changesubject;
97 set_changesubject = set_changesubject;