832f160b5f2d03d6250d2333558abe318f64111b
[prosody.git] / plugins / muc / subject.lib.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 -- Copyright (C) 2014 Daurnimator
5 --
6 -- This project is MIT/X11 licensed. Please see the
7 -- COPYING file in the source package for more information.
8 --
9
10 local st = require "util.stanza";
11
12 local muc_util = module:require "muc/util";
13 local valid_roles = muc_util.valid_roles;
14
15 local function create_subject_message(from, subject)
16         return st.message({from = from; type = "groupchat"})
17                 :tag("subject"):text(subject):up();
18 end
19
20 local function get_changesubject(room)
21         return room._data.changesubject;
22 end
23
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;
28         return true;
29 end
30
31 module:hook("muc-config-form", function(event)
32         table.insert(event.form, {
33                 name = "muc#roomconfig_changesubject";
34                 type = "boolean";
35                 label = "Allow Occupants to Change Subject?";
36                 value = get_changesubject(event.room);
37         });
38 end);
39
40 module:hook("muc-config-submitted/muc#roomconfig_changesubject", function(event)
41         if set_changesubject(event.room, event.value) then
42                 event.status_codes["104"] = true;
43         end
44 end);
45
46 local function get_subject(room)
47         -- a <message/> stanza from the room JID (or from the occupant JID of the entity that set the subject)
48         return room._data.subject_from or room.jid, room._data.subject;
49 end
50
51 local function send_subject(room, to)
52         local msg = create_subject_message(get_subject(room));
53         msg.attr.to = to;
54         room:route_stanza(msg);
55 end
56
57 local function set_subject(room, from, subject)
58         if subject == "" then subject = nil; end
59         local old_from, old_subject = get_subject(room);
60         if old_subject == subject and old_from == from then return false; end
61         room._data.subject_from = from;
62         room._data.subject = subject;
63         local msg = create_subject_message(from, subject);
64         room:broadcast_message(msg);
65         return true;
66 end
67
68 -- Send subject to joining user
69 module:hook("muc-occupant-session-new", function(event)
70         send_subject(event.room, event.stanza.attr.from);
71 end, 20);
72
73 -- Prosody has made the decision that messages with <subject/> are exclusively subject changes
74 -- e.g. body will be ignored; even if the subject change was not allowed
75 module:hook("muc-occupant-groupchat", function(event)
76         local stanza = event.stanza;
77         local subject = stanza:get_child("subject");
78         if subject then
79                 local room = event.room;
80                 local occupant = event.occupant;
81                 -- Role check for subject changes
82                 local role_rank = valid_roles[occupant and occupant.role or "none"];
83                 if role_rank >= valid_roles.moderator or
84                         ( role_rank >= valid_roles.participant and get_changesubject(room) ) then -- and participant
85                         set_subject(room, occupant.nick, subject:get_text());
86                         room:save();
87                         return true;
88                 else
89                         event.origin.send(st.error_reply(stanza, "auth", "forbidden"));
90                         return true;
91                 end
92         end
93 end, 20);
94
95 return {
96         get_changesubject = get_changesubject;
97         set_changesubject = set_changesubject;
98         get = get_subject;
99         set = set_subject;
100         send = send_subject;
101 };