Merge 0.10->trunk
[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 function create_subject_message(from, subject)
13         return st.message({from = from; type = "groupchat"})
14                 :tag("subject"):text(subject):up();
15 end
16
17 local function get_changesubject(room)
18         return room._data.changesubject;
19 end
20
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
26         return true;
27 end
28
29 module:hook("muc-config-form", function(event)
30         table.insert(event.form, {
31                 name = "muc#roomconfig_changesubject";
32                 type = "boolean";
33                 label = "Allow Occupants to Change Subject?";
34                 value = get_changesubject(event.room);
35         });
36 end);
37
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;
42         end
43 end);
44
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;
48 end
49
50 local function send_subject(room, to)
51         local msg = create_subject_message(get_subject(room));
52         msg.attr.to = to;
53         room:route_stanza(msg);
54 end
55
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);
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 -- 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);
81                 return true;
82         else
83                 event.origin.send(st.error_reply(stanza, "auth", "forbidden"));
84                 return true;
85         end
86 end);
87
88 return {
89         get_changesubject = get_changesubject;
90         set_changesubject = set_changesubject;
91         get = get_subject;
92         set = set_subject;
93         send = send_subject;
94 };