Software /
code /
prosody
Comparison
plugins/muc/subject.lib.lua @ 6224:2a9aff163545
plugins/muc: Move subject code to seperate module
author | daurnimator <quae@daurnimator.com> |
---|---|
date | Wed, 16 Apr 2014 13:54:51 -0400 |
child | 6228:4968d18e2c1e |
comparison
equal
deleted
inserted
replaced
6223:2a7ce69844ca | 6224:2a9aff163545 |
---|---|
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 return room._data.subject_from, room._data.subject; | |
47 end | |
48 | |
49 local function send_subject(room, to) | |
50 local msg = create_subject_message(get_subject(room)); | |
51 msg.attr.to = to; | |
52 room:route_stanza(msg); | |
53 end | |
54 | |
55 local function set_subject(room, from, subject) | |
56 if subject == "" then subject = nil; end | |
57 local old_from, old_subject = get_subject(room); | |
58 if old_subject == subject and old_from == from then return false; end | |
59 room._data.subject_from = from; | |
60 room._data.subject = subject; | |
61 if room.save then room:save(); end | |
62 local msg = create_subject_message(from, subject); | |
63 room:broadcast_message(msg); | |
64 return true; | |
65 end | |
66 | |
67 -- Send subject to joining user | |
68 module:hook("muc-occupant-joined", function(event) | |
69 event.room:send_subject(event.stanza.attr.from); | |
70 end, 20); | |
71 | |
72 -- Role check for subject changes | |
73 module:hook("muc-subject-change", function(event) | |
74 local room, stanza = event.room, event.stanza; | |
75 local occupant = room:get_occupant_by_real_jid(stanza.attr.from); | |
76 if occupant.role == "moderator" or | |
77 ( occupant.role == "participant" and get_changesubject(room) ) then -- and participant | |
78 local subject = stanza:get_child_text("subject"); | |
79 set_subject(room, occupant.nick, subject); | |
80 return true; | |
81 else | |
82 event.origin.send(st.error_reply(stanza, "auth", "forbidden")); | |
83 return true; | |
84 end | |
85 end); | |
86 | |
87 return { | |
88 get_changesubject = get_changesubject; | |
89 set_changesubject = set_changesubject; | |
90 get = get_subject; | |
91 set = set_subject; | |
92 send = send_subject; | |
93 }; |