From 68f64d281702713a8a342807b6b4b51616f3d40d Mon Sep 17 00:00:00 2001 From: daurnimator Date: Wed, 16 Apr 2014 13:54:51 -0400 Subject: plugins/muc: Move subject code to seperate module --- plugins/muc/muc.lib.lua | 68 ++++----------------------------- plugins/muc/subject.lib.lua | 93 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+), 61 deletions(-) create mode 100644 plugins/muc/subject.lib.lua diff --git a/plugins/muc/muc.lib.lua b/plugins/muc/muc.lib.lua index 782d4fe2..26b2d335 100644 --- a/plugins/muc/muc.lib.lua +++ b/plugins/muc/muc.lib.lua @@ -296,28 +296,6 @@ function room_mt:get_disco_items(stanza) return reply; end -function room_mt:get_subject() - return self._data.subject_from, self._data.subject; -end -local function create_subject_message(from, subject) - return st.message({from = from; type = "groupchat"}) - :tag('subject'):text(subject):up(); -end -function room_mt:send_subject(to) - local msg = create_subject_message(self:get_subject()); - msg.attr.to = to; - self:route_stanza(msg); -end -function room_mt:set_subject(current_nick, subject) - if subject == "" then subject = nil; end - self._data['subject'] = subject; - self._data['subject_from'] = current_nick; - if self.save then self:save(); end - local msg = create_subject_message(current_nick, subject); - self:broadcast_message(msg); - return true; -end - function room_mt:handle_kickable(origin, stanza) local real_jid = stanza.attr.from; local occupant = self:get_occupant_by_real_jid(real_jid); @@ -362,16 +340,6 @@ end function room_mt:set_public(public) return self:set_hidden(not public); end -function room_mt:set_changesubject(changesubject) - changesubject = changesubject and true or nil; - if self._data.changesubject ~= changesubject then - self._data.changesubject = changesubject; - if self.save then self:save(true); end - end -end -function room_mt:get_changesubject() - return self._data.changesubject; -end -- Give the room creator owner affiliation module:hook("muc-room-pre-create", function(event) @@ -400,11 +368,6 @@ module:hook("muc-occupant-joined", function(event) end); end, 80); --- Send subject to joining user -module:hook("muc-occupant-joined", function(event) - event.room:send_subject(event.stanza.attr.from); -end, 20); - function room_mt:handle_presence_to_occupant(origin, stanza) local type = stanza.attr.type; if type == "error" then -- error, kick em out! @@ -666,14 +629,6 @@ module:hook("muc-config-form", function(event) value = not event.room:get_hidden() }); 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 = event.room:get_changesubject() - }); -end); module:hook("muc-config-form", function(event) table.insert(event.form, { name = 'muc#roomconfig_moderatedroom', @@ -729,9 +684,6 @@ end); module:hook("muc-config-submitted", function(event) event.update_option("public", "muc#roomconfig_publicroom"); end); -module:hook("muc-config-submitted", function(event) - event.update_option("changesubject", "muc#roomconfig_changesubject"); -end); -- Removes everyone from the room function room_mt:clear(x) @@ -878,19 +830,6 @@ function room_mt:handle_owner_query_set_to_room(origin, stanza) end end -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 room:get_changesubject() ) then -- and participant - local subject = stanza:get_child_text("subject"); - room:set_subject(occupant.nick, subject); - else - event.origin.send(st.error_reply(stanza, "auth", "forbidden")); - end - return true; -end); - function room_mt:handle_groupchat_to_room(origin, stanza) -- Prosody has made the decision that messages with are exclusively subject changes -- e.g. body will be ignored; even if the subject change was not allowed @@ -1204,6 +1143,13 @@ local persistent = module:require "muc/persistent"; room_mt.get_persistent = persistent.get; room_mt.set_persistent = persistent.set; +local subject = module:require "muc/subject"; +room_mt.get_changesubject = subject.get_changesubject; +room_mt.set_changesubject = subject.set_changesubject; +room_mt.get_subject = subject.get; +room_mt.set_subject = subject.set; +room_mt.send_subject = subject.send; + local history = module:require "muc/history"; room_mt.send_history = history.send; room_mt.get_historylength = history.get_length; diff --git a/plugins/muc/subject.lib.lua b/plugins/muc/subject.lib.lua new file mode 100644 index 00000000..a42a18c5 --- /dev/null +++ b/plugins/muc/subject.lib.lua @@ -0,0 +1,93 @@ +-- 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) + return room._data.subject_from, 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) + event.room:send_subject(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; +}; -- cgit v1.2.3