aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/muc/subject.lib.lua
diff options
context:
space:
mode:
authorMatthew Wild <mwild1@gmail.com>2014-05-08 18:09:59 +0100
committerMatthew Wild <mwild1@gmail.com>2014-05-08 18:09:59 +0100
commit21b4f0514bfd35fb7ec732d49d15a6cdff95a2ca (patch)
treee489fa9811c3626353c6428617f626282a6e319d /plugins/muc/subject.lib.lua
parent95eb3d5a0d01ac9e6670c9f5dc44752f1e1b7388 (diff)
parent09fa17a7efd4502c7fdf21ec2492946bb59239e3 (diff)
downloadprosody-21b4f0514bfd35fb7ec732d49d15a6cdff95a2ca.tar.gz
prosody-21b4f0514bfd35fb7ec732d49d15a6cdff95a2ca.zip
Merge with Prosody trunk
Diffstat (limited to 'plugins/muc/subject.lib.lua')
-rw-r--r--plugins/muc/subject.lib.lua94
1 files changed, 94 insertions, 0 deletions
diff --git a/plugins/muc/subject.lib.lua b/plugins/muc/subject.lib.lua
new file mode 100644
index 00000000..44fc915c
--- /dev/null
+++ b/plugins/muc/subject.lib.lua
@@ -0,0 +1,94 @@
+-- Prosody IM
+-- Copyright (C) 2008-2010 Matthew Wild
+-- Copyright (C) 2008-2010 Waqas Hussain
+-- Copyright (C) 2014 Daurnimator
+--
+-- This project is MIT/X11 licensed. Please see the
+-- COPYING file in the source package for more information.
+--
+
+local st = require "util.stanza";
+
+local function create_subject_message(from, subject)
+ return st.message({from = from; type = "groupchat"})
+ :tag("subject"):text(subject):up();
+end
+
+local function get_changesubject(room)
+ return room._data.changesubject;
+end
+
+local function set_changesubject(room, changesubject)
+ changesubject = changesubject and true or nil;
+ if get_changesubject(room) == changesubject then return false; end
+ room._data.changesubject = changesubject;
+ if room.save then room:save(true); end
+ return true;
+end
+
+module:hook("muc-config-form", function(event)
+ table.insert(event.form, {
+ name = "muc#roomconfig_changesubject";
+ type = "boolean";
+ label = "Allow Occupants to Change Subject?";
+ value = get_changesubject(event.room);
+ });
+end);
+
+module:hook("muc-config-submitted", function(event)
+ local new = event.fields["muc#roomconfig_changesubject"];
+ if new ~= nil and set_changesubject(event.room, new) then
+ event.status_codes["104"] = true;
+ end
+end);
+
+local function get_subject(room)
+ -- a <message/> stanza from the room JID (or from the occupant JID of the entity that set the subject)
+ return room._data.subject_from or room.jid, room._data.subject;
+end
+
+local function send_subject(room, to)
+ local msg = create_subject_message(get_subject(room));
+ msg.attr.to = to;
+ room:route_stanza(msg);
+end
+
+local function set_subject(room, from, subject)
+ if subject == "" then subject = nil; end
+ local old_from, old_subject = get_subject(room);
+ if old_subject == subject and old_from == from then return false; end
+ room._data.subject_from = from;
+ room._data.subject = subject;
+ if room.save then room:save(); end
+ local msg = create_subject_message(from, subject);
+ room:broadcast_message(msg);
+ return true;
+end
+
+-- Send subject to joining user
+module:hook("muc-occupant-joined", function(event)
+ send_subject(event.room, event.stanza.attr.from);
+end, 20);
+
+-- Role check for subject changes
+module:hook("muc-subject-change", function(event)
+ local room, stanza = event.room, event.stanza;
+ local occupant = room:get_occupant_by_real_jid(stanza.attr.from);
+ if occupant.role == "moderator" or
+ ( occupant.role == "participant" and get_changesubject(room) ) then -- and participant
+ local subject = stanza:get_child_text("subject");
+ set_subject(room, occupant.nick, subject);
+ return true;
+ else
+ event.origin.send(st.error_reply(stanza, "auth", "forbidden"));
+ return true;
+ end
+end);
+
+return {
+ get_changesubject = get_changesubject;
+ set_changesubject = set_changesubject;
+ get = get_subject;
+ set = set_subject;
+ send = send_subject;
+};