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 function create_subject_message(from, subject)
13 return st.message({from = from; type = "groupchat"})
14 :tag("subject"):text(subject):up();
17 local function get_changesubject(room)
18 return room._data.changesubject;
21 local function set_changesubject(room, changesubject)
22 changesubject = changesubject and true or nil;
23 if get_changesubject(room) == changesubject then return false; end
24 room._data.changesubject = changesubject;
25 if room.save then room:save(true); end
29 module:hook("muc-config-form", function(event)
30 table.insert(event.form, {
31 name = "muc#roomconfig_changesubject";
33 label = "Allow Occupants to Change Subject?";
34 value = get_changesubject(event.room);
38 module:hook("muc-config-submitted", function(event)
39 local new = event.fields["muc#roomconfig_changesubject"];
40 if new ~= nil and set_changesubject(event.room, new) then
41 event.status_codes["104"] = true;
45 local function get_subject(room)
46 -- a <message/> stanza from the room JID (or from the occupant JID of the entity that set the subject)
47 return room._data.subject_from or room.jid, room._data.subject;
50 local function send_subject(room, to)
51 local msg = create_subject_message(get_subject(room));
53 room:route_stanza(msg);
56 local function set_subject(room, from, subject)
57 if subject == "" then subject = nil; end
58 local old_from, old_subject = get_subject(room);
59 if old_subject == subject and old_from == from then return false; end
60 room._data.subject_from = from;
61 room._data.subject = subject;
62 if room.save then room:save(); end
63 local msg = create_subject_message(from, subject);
64 room:broadcast_message(msg);
68 -- Send subject to joining user
69 module:hook("muc-occupant-session-new", function(event)
70 send_subject(event.room, event.stanza.attr.from);
73 -- Role check for subject changes
74 module:hook("muc-subject-change", function(event)
75 local room, stanza = event.room, event.stanza;
76 local occupant = room:get_occupant_by_real_jid(stanza.attr.from);
77 if occupant.role == "moderator" or
78 ( occupant.role == "participant" and get_changesubject(room) ) then -- and participant
79 local subject = stanza:get_child_text("subject");
80 set_subject(room, occupant.nick, subject);
83 event.origin.send(st.error_reply(stanza, "auth", "forbidden"));
89 get_changesubject = get_changesubject;
90 set_changesubject = set_changesubject;