Merge 0.9->0.10
[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 local array = require "util.array";
10
11 if module:get_host_type() ~= "component" then
12         error("MUC should be loaded as a component, please see http://prosody.im/doc/components", 0);
13 end
14
15 local muc_host = module:get_host();
16 local muc_name = module:get_option("name");
17 if type(muc_name) ~= "string" then muc_name = "Prosody Chatrooms"; end
18 local restrict_room_creation = module:get_option("restrict_room_creation");
19 if restrict_room_creation then
20         if restrict_room_creation == true then
21                 restrict_room_creation = "admin";
22         elseif restrict_room_creation ~= "admin" and restrict_room_creation ~= "local" then
23                 restrict_room_creation = nil;
24         end
25 end
26 local lock_rooms = module:get_option_boolean("muc_room_locking", false);
27 local lock_room_timeout = module:get_option_number("muc_room_lock_timeout", 300);
28
29 local muclib = module:require "muc";
30 local muc_new_room = muclib.new_room;
31 local jid_split = require "util.jid".split;
32 local jid_bare = require "util.jid".bare;
33 local st = require "util.stanza";
34 local uuid_gen = require "util.uuid".generate;
35 local um_is_admin = require "core.usermanager".is_admin;
36 local hosts = prosody.hosts;
37
38 rooms = {};
39 local rooms = rooms;
40 local persistent_rooms_storage = module:open_store("persistent");
41 local persistent_rooms = persistent_rooms_storage:get() or {};
42 local room_configs = module:open_store("config");
43
44 -- Configurable options
45 muclib.set_max_history_length(module:get_option_number("max_history_messages"));
46
47 module:depends("disco");
48 module:add_identity("conference", "text", muc_name);
49 module:add_feature("http://jabber.org/protocol/muc");
50
51 local function is_admin(jid)
52         return um_is_admin(jid, module.host);
53 end
54
55 room_mt = muclib.room_mt; -- Yes, global.
56 local _set_affiliation = room_mt.set_affiliation;
57 local _get_affiliation = room_mt.get_affiliation;
58 function muclib.room_mt:get_affiliation(jid)
59         if is_admin(jid) then return "owner"; end
60         return _get_affiliation(self, jid);
61 end
62 function muclib.room_mt:set_affiliation(actor, jid, affiliation, callback, reason)
63         if affiliation ~= "owner" and is_admin(jid) then return nil, "modify", "not-acceptable"; end
64         return _set_affiliation(self, actor, jid, affiliation, callback, reason);
65 end
66
67 local function room_route_stanza(room, stanza) module:send(stanza); end
68 local function room_save(room, forced)
69         local node = jid_split(room.jid);
70         persistent_rooms[room.jid] = room._data.persistent;
71         if room._data.persistent then
72                 local history = room._data.history;
73                 room._data.history = nil;
74                 local data = {
75                         jid = room.jid;
76                         _data = room._data;
77                         _affiliations = room._affiliations;
78                 };
79                 room_configs:set(node, data);
80                 room._data.history = history;
81         elseif forced then
82                 room_configs:set(node, nil);
83                 if not next(room._occupants) then -- Room empty
84                         rooms[room.jid] = nil;
85                 end
86         end
87         if forced then persistent_rooms_storage:set(nil, persistent_rooms); end
88 end
89
90 function create_room(jid)
91         local room = muc_new_room(jid);
92         room.route_stanza = room_route_stanza;
93         room.save = room_save;
94         rooms[jid] = room;
95         if lock_rooms then
96                 room.locked = true;
97                 if lock_room_timeout and lock_room_timeout > 0 then
98                         module:add_timer(lock_room_timeout, function ()
99                                 if room.locked then
100                                         room:destroy(); -- Not unlocked in time
101                                 end
102                         end);
103                 end
104         end
105         module:fire_event("muc-room-created", { room = room });
106         return room;
107 end
108
109 local persistent_errors = false;
110 for jid in pairs(persistent_rooms) do
111         local node = jid_split(jid);
112         local data = room_configs:get(node);
113         if data then
114                 local room = create_room(jid);
115                 room._data = data._data;
116                 room._affiliations = data._affiliations;
117         else -- missing room data
118                 persistent_rooms[jid] = nil;
119                 module:log("error", "Missing data for room '%s', removing from persistent room list", jid);
120                 persistent_errors = true;
121         end
122 end
123 if persistent_errors then persistent_rooms_storage:set(nil, persistent_rooms); end
124
125 local host_room = muc_new_room(muc_host);
126 host_room.route_stanza = room_route_stanza;
127 host_room.save = room_save;
128
129 module:hook("host-disco-items", function(event)
130         local reply = event.reply;
131         module:log("debug", "host-disco-items called");
132         for jid, room in pairs(rooms) do
133                 if not room:get_hidden() then
134                         reply:tag("item", {jid=jid, name=room:get_name()}):up();
135                 end
136         end
137 end);
138
139 local function handle_to_domain(event)
140         local origin, stanza = event.origin, event.stanza;
141         local type = stanza.attr.type;
142         if type == "error" or type == "result" then return; end
143         if stanza.name == "iq" and type == "get" then
144                 local xmlns = stanza.tags[1].attr.xmlns;
145                 local node = stanza.tags[1].attr.node;
146                 if xmlns == "http://jabber.org/protocol/muc#unique" then
147                         origin.send(st.reply(stanza):tag("unique", {xmlns = xmlns}):text(uuid_gen())); -- FIXME Random UUIDs can theoretically have collisions
148                 else
149                         origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- TODO disco/etc
150                 end
151         else
152                 host_room:handle_stanza(origin, stanza);
153                 --origin.send(st.error_reply(stanza, "cancel", "service-unavailable", "The muc server doesn't deal with messages and presence directed at it"));
154         end
155         return true;
156 end
157
158 function stanza_handler(event)
159         local origin, stanza = event.origin, event.stanza;
160         local bare = jid_bare(stanza.attr.to);
161         local room = rooms[bare];
162         if not room then
163                 if stanza.name ~= "presence" then
164                         origin.send(st.error_reply(stanza, "cancel", "item-not-found"));
165                         return true;
166                 end
167                 if not(restrict_room_creation) or
168                   is_admin(stanza.attr.from) or
169                   (restrict_room_creation == "local" and select(2, jid_split(stanza.attr.from)) == module.host:gsub("^[^%.]+%.", "")) then
170                         room = create_room(bare);
171                 end
172         end
173         if room then
174                 room:handle_stanza(origin, stanza);
175                 if not next(room._occupants) and not persistent_rooms[room.jid] then -- empty, non-persistent room
176                         module:fire_event("muc-room-destroyed", { room = room });
177                         rooms[bare] = nil; -- discard room
178                 end
179         else
180                 origin.send(st.error_reply(stanza, "cancel", "not-allowed"));
181         end
182         return true;
183 end
184 module:hook("iq/bare", stanza_handler, -1);
185 module:hook("message/bare", stanza_handler, -1);
186 module:hook("presence/bare", stanza_handler, -1);
187 module:hook("iq/full", stanza_handler, -1);
188 module:hook("message/full", stanza_handler, -1);
189 module:hook("presence/full", stanza_handler, -1);
190 module:hook("iq/host", handle_to_domain, -1);
191 module:hook("message/host", handle_to_domain, -1);
192 module:hook("presence/host", handle_to_domain, -1);
193
194 hosts[module.host].send = function(stanza) -- FIXME do a generic fix
195         if stanza.attr.type == "result" or stanza.attr.type == "error" then
196                 module:send(stanza);
197         else error("component.send only supports result and error stanzas at the moment"); end
198 end
199
200 hosts[module:get_host()].muc = { rooms = rooms };
201
202 local saved = false;
203 module.save = function()
204         saved = true;
205         return {rooms = rooms};
206 end
207 module.restore = function(data)
208         for jid, oldroom in pairs(data.rooms or {}) do
209                 local room = create_room(jid);
210                 room._jid_nick = oldroom._jid_nick;
211                 room._occupants = oldroom._occupants;
212                 room._data = oldroom._data;
213                 room._affiliations = oldroom._affiliations;
214         end
215         hosts[module:get_host()].muc = { rooms = rooms };
216 end
217
218 function shutdown_room(room, stanza)
219         for nick, occupant in pairs(room._occupants) do
220                 stanza.attr.from = nick;
221                 for jid in pairs(occupant.sessions) do
222                         stanza.attr.to = jid;
223                         room:_route_stanza(stanza);
224                         room._jid_nick[jid] = nil;
225                 end
226                 room._occupants[nick] = nil;
227         end
228 end
229 function shutdown_component()
230         if not saved then
231                 local stanza = st.presence({type = "unavailable"})
232                         :tag("x", {xmlns = "http://jabber.org/protocol/muc#user"})
233                                 :tag("item", { affiliation='none', role='none' }):up()
234                                 :tag("status", { code = "332"}):up();
235                 for roomjid, room in pairs(rooms) do
236                         shutdown_room(room, stanza);
237                 end
238                 shutdown_room(host_room, stanza);
239         end
240 end
241 module.unload = shutdown_component;
242 module:hook_global("server-stopping", shutdown_component);
243
244 -- Ad-hoc commands
245 module:depends("adhoc")
246 local t_concat = table.concat;
247 local keys = require "util.iterators".keys;
248 local adhoc_new = module:require "adhoc".new;
249 local adhoc_initial = require "util.adhoc".new_initial_data_form;
250 local dataforms_new = require "util.dataforms".new;
251
252 local destroy_rooms_layout = dataforms_new {
253         title = "Destroy rooms";
254         instructions = "Select the rooms to destroy";
255
256         { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/muc#destroy" };
257         { name = "rooms", type = "list-multi", required = true, label = "Rooms to destroy:"};
258 };
259
260 local destroy_rooms_handler = adhoc_initial(destroy_rooms_layout, function()
261         return { rooms = array.collect(keys(rooms)):sort() };
262 end, function(fields, errors)
263         if errors then
264                 local errmsg = {};
265                 for name, err in pairs(errors) do
266                         errmsg[#errmsg + 1] = name .. ": " .. err;
267                 end
268                 return { status = "completed", error = { message = t_concat(errmsg, "\n") } };
269         end
270         for _, room in ipairs(fields.rooms) do
271                 rooms[room]:destroy();
272                 rooms[room] = nil;
273         end
274         return { status = "completed", info = "The following rooms were destroyed:\n"..t_concat(fields.rooms, "\n") };
275 end);
276 local destroy_rooms_desc = adhoc_new("Destroy Rooms", "http://prosody.im/protocol/muc#destroy", destroy_rooms_handler, "admin");
277
278 module:provides("adhoc", destroy_rooms_desc);