Merge 0.10->trunk
[prosody.git] / plugins / muc / mod_muc.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 --
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9 if module:get_host_type() ~= "component" then
10         error("MUC should be loaded as a component, please see http://prosody.im/doc/components", 0);
11 end
12
13 local muclib = module:require "muc";
14 room_mt = muclib.room_mt; -- Yes, global.
15
16 local affiliation_notify = module:require "muc/affiliation_notify";
17
18 local name = module:require "muc/name";
19 room_mt.get_name = name.get;
20 room_mt.set_name = name.set;
21
22 local description = module:require "muc/description";
23 room_mt.get_description = description.get;
24 room_mt.set_description = description.set;
25
26 local hidden = module:require "muc/hidden";
27 room_mt.get_hidden = hidden.get;
28 room_mt.set_hidden = hidden.set;
29 function room_mt:get_public()
30         return not self:get_hidden();
31 end
32 function room_mt:set_public(public)
33         return self:set_hidden(not public);
34 end
35
36 local password = module:require "muc/password";
37 room_mt.get_password = password.get;
38 room_mt.set_password = password.set;
39
40 local members_only = module:require "muc/members_only";
41 room_mt.get_members_only = members_only.get;
42 room_mt.set_members_only = members_only.set;
43
44 local moderated = module:require "muc/moderated";
45 room_mt.get_moderated = moderated.get;
46 room_mt.set_moderated = moderated.set;
47
48 local persistent = module:require "muc/persistent";
49 room_mt.get_persistent = persistent.get;
50 room_mt.set_persistent = persistent.set;
51
52 local subject = module:require "muc/subject";
53 room_mt.get_changesubject = subject.get_changesubject;
54 room_mt.set_changesubject = subject.set_changesubject;
55 room_mt.get_subject = subject.get;
56 room_mt.set_subject = subject.set;
57 room_mt.send_subject = subject.send;
58
59 local history = module:require "muc/history";
60 room_mt.send_history = history.send;
61 room_mt.get_historylength = history.get_length;
62 room_mt.set_historylength = history.set_length;
63
64 local iterators = require "util.iterators";
65 local jid_split = require "util.jid".split;
66 local jid_bare = require "util.jid".bare;
67 local st = require "util.stanza";
68 local um_is_admin = require "core.usermanager".is_admin;
69
70 local rooms = module:shared "rooms";
71
72 module:depends("disco");
73 module:add_identity("conference", "text", module:get_option_string("name", "Prosody Chatrooms"));
74 module:add_feature("http://jabber.org/protocol/muc");
75 module:depends "muc_unique"
76 module:require "muc/lock";
77
78 local function is_admin(jid)
79         return um_is_admin(jid, module.host);
80 end
81
82 do -- Monkey patch to make server admins room owners
83         local _get_affiliation = room_mt.get_affiliation;
84         function room_mt:get_affiliation(jid)
85                 if is_admin(jid) then return "owner"; end
86                 return _get_affiliation(self, jid);
87         end
88
89         local _set_affiliation = room_mt.set_affiliation;
90         function room_mt:set_affiliation(actor, jid, affiliation, reason)
91                 if affiliation ~= "owner" and is_admin(jid) then return nil, "modify", "not-acceptable"; end
92                 return _set_affiliation(self, actor, jid, affiliation, reason);
93         end
94 end
95
96 local persistent = module:require "muc/persistent";
97 local persistent_rooms_storage = module:open_store("persistent");
98 local persistent_rooms = module:open_store("persistent", "map");
99 local room_configs = module:open_store("config");
100
101 local function room_save(room, forced)
102         local node = jid_split(room.jid);
103         local is_persistent = persistent.get(room);
104         persistent_rooms:set(nil, room.jid, is_persistent);
105         if is_persistent then
106                 local history = room._data.history;
107                 room._data.history = nil;
108                 local data = {
109                         jid = room.jid;
110                         _data = room._data;
111                         _affiliations = room._affiliations;
112                 };
113                 room_configs:set(node, data);
114                 room._data.history = history;
115         elseif forced then
116                 room_configs:set(node, nil);
117                 if not next(room._occupants) then -- Room empty
118                         rooms[room.jid] = nil;
119                 end
120         end
121 end
122
123 -- Automatically destroy empty non-persistent rooms
124 module:hook("muc-occupant-left",function(event)
125         local room = event.room
126         if not room:has_occupant() and not persistent.get(room) then -- empty, non-persistent room
127                 module:fire_event("muc-room-destroyed", { room = room });
128         end
129 end);
130
131 function track_room(room)
132         rooms[room.jid] = room;
133         -- When room is created, over-ride 'save' method
134         room.save = room_save;
135 end
136
137 local function restore_room(jid)
138         local node = jid_split(jid);
139         local data = room_configs:get(node);
140         if data then
141                 local room = muclib.new_room(jid);
142                 room._data = data._data;
143                 room._affiliations = data._affiliations;
144                 track_room(room);
145                 return room;
146         end
147 end
148
149 function forget_room(room)
150         local room_jid = room.jid;
151         local node = jid_split(room.jid);
152         rooms[room_jid] = nil;
153         room_configs:set(node, nil);
154         if persistent.get(room) then
155                 persistent_rooms:set(nil, room_jid, nil);
156         end
157 end
158
159 function get_room_from_jid(room_jid)
160         local room = rooms[room_jid];
161         if room == nil then
162                 -- Check if in persistent storage
163                 if persistent_rooms:get(nil, room_jid) then
164                         room = restore_room(room_jid);
165                         if room == nil then
166                                 module:log("error", "Missing data for room '%s', removing from persistent room list", room_jid);
167                                 persistent_rooms:set(nil, room_jid, nil);
168                         end
169                 end
170         end
171         return room
172 end
173
174 function each_room(local_only)
175         if not local_only then
176                 for room_jid in pairs(persistent_rooms_storage:get(nil) or {}) do
177                         if rooms[room_jid] == nil then -- Don't restore rooms that already exist
178                                 local room = restore_room(room_jid);
179                                 if room == nil then
180                                         module:log("error", "Missing data for room '%s', omitting from iteration", room_jid);
181                                 end
182                         end
183                 end
184         end
185         return iterators.values(rooms);
186 end
187
188 module:hook("host-disco-items", function(event)
189         local reply = event.reply;
190         module:log("debug", "host-disco-items called");
191         for room in each_room() do
192                 if not room:get_hidden() then
193                         reply:tag("item", {jid=room.jid, name=room:get_name()}):up();
194                 end
195         end
196 end);
197
198 module:hook("muc-room-pre-create", function(event)
199         track_room(event.room);
200 end, -1000);
201
202 module:hook("muc-room-destroyed",function(event)
203         return forget_room(event.room);
204 end)
205
206 do
207         local restrict_room_creation = module:get_option("restrict_room_creation");
208         if restrict_room_creation == true then
209                 restrict_room_creation = "admin";
210         end
211         if restrict_room_creation then
212                 local host_suffix = module.host:gsub("^[^%.]+%.", "");
213                 module:hook("muc-room-pre-create", function(event)
214                         local origin, stanza = event.origin, event.stanza;
215                         local user_jid = stanza.attr.from;
216                         if not is_admin(user_jid) and not (
217                                 restrict_room_creation == "local" and
218                                 select(2, jid_split(user_jid)) == host_suffix
219                         ) then
220                                 origin.send(st.error_reply(stanza, "cancel", "not-allowed"));
221                                 return true;
222                         end
223                 end);
224         end
225 end
226
227 for event_name, method in pairs {
228         -- Normal room interactions
229         ["iq-get/bare/http://jabber.org/protocol/disco#info:query"] = "handle_disco_info_get_query" ;
230         ["iq-get/bare/http://jabber.org/protocol/disco#items:query"] = "handle_disco_items_get_query" ;
231         ["iq-set/bare/http://jabber.org/protocol/muc#admin:query"] = "handle_admin_query_set_command" ;
232         ["iq-get/bare/http://jabber.org/protocol/muc#admin:query"] = "handle_admin_query_get_command" ;
233         ["iq-set/bare/http://jabber.org/protocol/muc#owner:query"] = "handle_owner_query_set_to_room" ;
234         ["iq-get/bare/http://jabber.org/protocol/muc#owner:query"] = "handle_owner_query_get_to_room" ;
235         ["message/bare"] = "handle_message_to_room" ;
236         ["presence/bare"] = "handle_presence_to_room" ;
237         -- Host room
238         ["iq-get/host/http://jabber.org/protocol/disco#info:query"] = "handle_disco_info_get_query" ;
239         ["iq-get/host/http://jabber.org/protocol/disco#items:query"] = "handle_disco_items_get_query" ;
240         ["iq-set/host/http://jabber.org/protocol/muc#admin:query"] = "handle_admin_query_set_command" ;
241         ["iq-get/host/http://jabber.org/protocol/muc#admin:query"] = "handle_admin_query_get_command" ;
242         ["iq-set/host/http://jabber.org/protocol/muc#owner:query"] = "handle_owner_query_set_to_room" ;
243         ["iq-get/host/http://jabber.org/protocol/muc#owner:query"] = "handle_owner_query_get_to_room" ;
244         ["message/host"] = "handle_message_to_room" ;
245         ["presence/host"] = "handle_presence_to_room" ;
246         -- Direct to occupant (normal rooms and host room)
247         ["presence/full"] = "handle_presence_to_occupant" ;
248         ["iq/full"] = "handle_iq_to_occupant" ;
249         ["message/full"] = "handle_message_to_occupant" ;
250 } do
251         module:hook(event_name, function (event)
252                 local origin, stanza = event.origin, event.stanza;
253                 local room_jid = jid_bare(stanza.attr.to);
254                 local room = get_room_from_jid(room_jid);
255                 if room == nil then
256                         -- Watch presence to create rooms
257                         if stanza.attr.type == nil and stanza.name == "presence" then
258                                 room = muclib.new_room(room_jid);
259                         else
260                                 origin.send(st.error_reply(stanza, "cancel", "not-allowed"));
261                                 return true;
262                         end
263                 end
264                 return room[method](room, origin, stanza);
265         end, -2)
266 end
267
268 function shutdown_component()
269         local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user"})
270                 :tag("status", { code = "332"}):up();
271         for room in each_room(true) do
272                 room:clear(x);
273         end
274 end
275 module:hook_global("server-stopping", shutdown_component);
276
277 do -- Ad-hoc commands
278         module:depends "adhoc";
279         local t_concat = table.concat;
280         local adhoc_new = module:require "adhoc".new;
281         local adhoc_initial = require "util.adhoc".new_initial_data_form;
282         local array = require "util.array";
283         local dataforms_new = require "util.dataforms".new;
284
285         local destroy_rooms_layout = dataforms_new {
286                 title = "Destroy rooms";
287                 instructions = "Select the rooms to destroy";
288
289                 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/muc#destroy" };
290                 { name = "rooms", type = "list-multi", required = true, label = "Rooms to destroy:"};
291         };
292
293         local destroy_rooms_handler = adhoc_initial(destroy_rooms_layout, function()
294                 return { rooms = array.collect(each_room()):pluck("jid"):sort(); };
295         end, function(fields, errors)
296                 if errors then
297                         local errmsg = {};
298                         for name, err in pairs(errors) do
299                                 errmsg[#errmsg + 1] = name .. ": " .. err;
300                         end
301                         return { status = "completed", error = { message = t_concat(errmsg, "\n") } };
302                 end
303                 for _, room in ipairs(fields.rooms) do
304                         get_room_from_jid(room):destroy();
305                 end
306                 return { status = "completed", info = "The following rooms were destroyed:\n"..t_concat(fields.rooms, "\n") };
307         end);
308         local destroy_rooms_desc = adhoc_new("Destroy Rooms", "http://prosody.im/protocol/muc#destroy", destroy_rooms_handler, "admin");
309
310         module:provides("adhoc", destroy_rooms_desc);
311 end