Mercurial > prosody-hg
comparison plugins/muc/subject.lib.lua @ 6609:d2faaaca695d
Merge 0.10->trunk
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Fri, 27 Mar 2015 22:24:57 +0000 |
| parents | 675aea867574 |
| children | 84e01dbb739e |
comparison
equal
deleted
inserted
replaced
| 6608:b6e558febb7a | 6609:d2faaaca695d |
|---|---|
| 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 if room.save then room:save(true); end | |
| 29 return true; | |
| 30 end | |
| 31 | |
| 32 module:hook("muc-config-form", function(event) | |
| 33 table.insert(event.form, { | |
| 34 name = "muc#roomconfig_changesubject"; | |
| 35 type = "boolean"; | |
| 36 label = "Allow Occupants to Change Subject?"; | |
| 37 value = get_changesubject(event.room); | |
| 38 }); | |
| 39 end); | |
| 40 | |
| 41 module:hook("muc-config-submitted", function(event) | |
| 42 local new = event.fields["muc#roomconfig_changesubject"]; | |
| 43 if new ~= nil and set_changesubject(event.room, new) then | |
| 44 event.status_codes["104"] = true; | |
| 45 end | |
| 46 end); | |
| 47 | |
| 48 local function get_subject(room) | |
| 49 -- a <message/> stanza from the room JID (or from the occupant JID of the entity that set the subject) | |
| 50 return room._data.subject_from or room.jid, room._data.subject; | |
| 51 end | |
| 52 | |
| 53 local function send_subject(room, to) | |
| 54 local msg = create_subject_message(get_subject(room)); | |
| 55 msg.attr.to = to; | |
| 56 room:route_stanza(msg); | |
| 57 end | |
| 58 | |
| 59 local function set_subject(room, from, subject) | |
| 60 if subject == "" then subject = nil; end | |
| 61 local old_from, old_subject = get_subject(room); | |
| 62 if old_subject == subject and old_from == from then return false; end | |
| 63 room._data.subject_from = from; | |
| 64 room._data.subject = subject; | |
| 65 if room.save then room:save(); end | |
| 66 local msg = create_subject_message(from, subject); | |
| 67 room:broadcast_message(msg); | |
| 68 return true; | |
| 69 end | |
| 70 | |
| 71 -- Send subject to joining user | |
| 72 module:hook("muc-occupant-session-new", function(event) | |
| 73 send_subject(event.room, event.stanza.attr.from); | |
| 74 end, 20); | |
| 75 | |
| 76 -- Prosody has made the decision that messages with <subject/> are exclusively subject changes | |
| 77 -- e.g. body will be ignored; even if the subject change was not allowed | |
| 78 module:hook("muc-occupant-groupchat", function(event) | |
| 79 local stanza = event.stanza; | |
| 80 local subject = stanza:get_child("subject"); | |
| 81 if subject then | |
| 82 local occupant = event.occupant; | |
| 83 -- Role check for subject changes | |
| 84 local role_rank = valid_roles[occupant and occupant.role or "none"]; | |
| 85 if role_rank >= valid_roles.moderator or | |
| 86 ( role_rank >= valid_roles.participant and get_changesubject(event.room) ) then -- and participant | |
| 87 set_subject(event.room, occupant.nick, subject:get_text()); | |
| 88 return true; | |
| 89 else | |
| 90 event.origin.send(st.error_reply(stanza, "auth", "forbidden")); | |
| 91 return true; | |
| 92 end | |
| 93 end | |
| 94 end, 20); | |
| 95 | |
| 96 return { | |
| 97 get_changesubject = get_changesubject; | |
| 98 set_changesubject = set_changesubject; | |
| 99 get = get_subject; | |
| 100 set = set_subject; | |
| 101 send = send_subject; | |
| 102 }; |
