Annotate

plugins/muc/subject.lib.lua @ 6228:4968d18e2c1e

plugins/muc/subject.lib: If subject is not set by an occupant, it should come from room jid itself
author daurnimator <quae@daurnimator.com>
date Fri, 18 Apr 2014 12:20:07 -0400
parent 6224:2a9aff163545
child 6277:f2c9c36979b3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6224
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
1 -- Prosody IM
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
2 -- Copyright (C) 2008-2010 Matthew Wild
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
3 -- Copyright (C) 2008-2010 Waqas Hussain
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
4 -- Copyright (C) 2014 Daurnimator
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
5 --
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
6 -- This project is MIT/X11 licensed. Please see the
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
7 -- COPYING file in the source package for more information.
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
8 --
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
9
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
10 local st = require "util.stanza";
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
11
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
12 local function create_subject_message(from, subject)
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
13 return st.message({from = from; type = "groupchat"})
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
14 :tag("subject"):text(subject):up();
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
15 end
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
16
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
17 local function get_changesubject(room)
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
18 return room._data.changesubject;
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
19 end
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
20
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
21 local function set_changesubject(room, changesubject)
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
22 changesubject = changesubject and true or nil;
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
23 if get_changesubject(room) == changesubject then return false; end
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
24 room._data.changesubject = changesubject;
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
25 if room.save then room:save(true); end
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
26 return true;
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
27 end
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
28
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
29 module:hook("muc-config-form", function(event)
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
30 table.insert(event.form, {
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
31 name = "muc#roomconfig_changesubject";
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
32 type = "boolean";
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
33 label = "Allow Occupants to Change Subject?";
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
34 value = get_changesubject(event.room);
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
35 });
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
36 end);
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
37
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
38 module:hook("muc-config-submitted", function(event)
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
39 local new = event.fields["muc#roomconfig_changesubject"];
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
40 if new ~= nil and set_changesubject(event.room, new) then
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
41 event.status_codes["104"] = true;
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
42 end
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
43 end);
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
44
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
45 local function get_subject(room)
6228
4968d18e2c1e plugins/muc/subject.lib: If subject is not set by an occupant, it should come from room jid itself
daurnimator <quae@daurnimator.com>
parents: 6224
diff changeset
46 -- a <message/> stanza from the room JID (or from the occupant JID of the entity that set the subject)
4968d18e2c1e plugins/muc/subject.lib: If subject is not set by an occupant, it should come from room jid itself
daurnimator <quae@daurnimator.com>
parents: 6224
diff changeset
47 return room._data.subject_from or room.jid, room._data.subject;
6224
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
48 end
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
49
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
50 local function send_subject(room, to)
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
51 local msg = create_subject_message(get_subject(room));
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
52 msg.attr.to = to;
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
53 room:route_stanza(msg);
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
54 end
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
55
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
56 local function set_subject(room, from, subject)
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
57 if subject == "" then subject = nil; end
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
58 local old_from, old_subject = get_subject(room);
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
59 if old_subject == subject and old_from == from then return false; end
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
60 room._data.subject_from = from;
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
61 room._data.subject = subject;
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
62 if room.save then room:save(); end
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
63 local msg = create_subject_message(from, subject);
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
64 room:broadcast_message(msg);
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
65 return true;
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
66 end
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
67
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
68 -- Send subject to joining user
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
69 module:hook("muc-occupant-joined", function(event)
6228
4968d18e2c1e plugins/muc/subject.lib: If subject is not set by an occupant, it should come from room jid itself
daurnimator <quae@daurnimator.com>
parents: 6224
diff changeset
70 send_subject(event.room, event.stanza.attr.from);
6224
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
71 end, 20);
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
72
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
73 -- Role check for subject changes
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
74 module:hook("muc-subject-change", function(event)
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
75 local room, stanza = event.room, event.stanza;
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
76 local occupant = room:get_occupant_by_real_jid(stanza.attr.from);
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
77 if occupant.role == "moderator" or
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
78 ( occupant.role == "participant" and get_changesubject(room) ) then -- and participant
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
79 local subject = stanza:get_child_text("subject");
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
80 set_subject(room, occupant.nick, subject);
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
81 return true;
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
82 else
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
83 event.origin.send(st.error_reply(stanza, "auth", "forbidden"));
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
84 return true;
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
85 end
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
86 end);
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
87
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
88 return {
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
89 get_changesubject = get_changesubject;
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
90 set_changesubject = set_changesubject;
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
91 get = get_subject;
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
92 set = set_subject;
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
93 send = send_subject;
2a9aff163545 plugins/muc: Move subject code to seperate module
daurnimator <quae@daurnimator.com>
parents:
diff changeset
94 };