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 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 um_is_admin = require "core.usermanager".is_admin;
35 local hosts = prosody.hosts;
36
37 rooms = {};
38 local rooms = rooms;
39 local persistent_rooms_storage = module:open_store("persistent");
40 local persistent_rooms = persistent_rooms_storage:get() or {};
41 local room_configs = module:open_store("config");
42
43 -- Configurable options
44 muclib.set_max_history_length(module:get_option_number("max_history_messages"));
45
46 module:depends("disco");
47 module:add_identity("conference", "text", muc_name);
48 module:add_feature("http://jabber.org/protocol/muc");
49 module:depends "muc_unique"
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 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_save(room, forced)
68         local node = jid_split(room.jid);
69         persistent_rooms[room.jid] = room._data.persistent;
70         if room._data.persistent then
71                 local history = room._data.history;
72                 room._data.history = nil;
73                 local data = {
74                         jid = room.jid;
75                         _data = room._data;
76                         _affiliations = room._affiliations;
77                 };
78                 room_configs:set(node, data);
79                 room._data.history = history;
80         elseif forced then
81                 room_configs:set(node, nil);
82                 if not next(room._occupants) then -- Room empty
83                         rooms[room.jid] = nil;
84                 end
85         end
86         if forced then persistent_rooms_storage:set(nil, persistent_rooms); end
87 end
88
89 function create_room(jid)
90         local room = muc_new_room(jid);
91         room.save = room_save;
92         rooms[jid] = room;
93         if lock_rooms then
94                 room:lock();
95                 if lock_room_timeout and lock_room_timeout > 0 then
96                         module:add_timer(lock_room_timeout, function ()
97                                 if room:is_locked() then
98                                         room:destroy(); -- Not unlocked in time
99                                 end
100                         end);
101                 end
102         end
103         module:fire_event("muc-room-created", { room = room });
104         return room;
105 end
106
107 function forget_room(jid)
108         rooms[jid] = nil;
109 end
110
111 function get_room_from_jid(room_jid)
112         return rooms[room_jid]
113 end
114
115 local persistent_errors = false;
116 for jid in pairs(persistent_rooms) do
117         local node = jid_split(jid);
118         local data = room_configs:get(node);
119         if data then
120                 local room = create_room(jid);
121                 room._data = data._data;
122                 room._affiliations = data._affiliations;
123         else -- missing room data
124                 persistent_rooms[jid] = nil;
125                 module:log("error", "Missing data for room '%s', removing from persistent room list", jid);
126                 persistent_errors = true;
127         end
128 end
129 if persistent_errors then persistent_rooms_storage:set(nil, persistent_rooms); end
130
131 local host_room = muc_new_room(muc_host);
132 host_room.save = room_save;
133 rooms[muc_host] = host_room;
134
135 module:hook("host-disco-items", function(event)
136         local reply = event.reply;
137         module:log("debug", "host-disco-items called");
138         for jid, room in pairs(rooms) do
139                 if not room:get_hidden() then
140                         reply:tag("item", {jid=jid, name=room:get_name()}):up();
141                 end
142         end
143 end);
144
145 module:hook("muc-room-destroyed",function(event)
146         local room = event.room
147         forget_room(room.jid)
148 end)
149
150 module:hook("muc-occupant-left",function(event)
151         local room = event.room
152         if not next(room._occupants) and not persistent_rooms[room.jid] then -- empty, non-persistent room
153                 module:fire_event("muc-room-destroyed", { room = room });
154         end
155 end);
156
157 -- Watch presence to create rooms
158 local function attempt_room_creation(event)
159         local origin, stanza = event.origin, event.stanza;
160         local room_jid = jid_bare(stanza.attr.to);
161         if stanza.attr.type == nil and
162                 get_room_from_jid(room_jid) == nil and
163                 (
164                         not(restrict_room_creation) or
165                         is_admin(stanza.attr.from) or
166                         (
167                                 restrict_room_creation == "local" and
168                                 select(2, jid_split(stanza.attr.from)) == module.host:gsub("^[^%.]+%.", "")
169                         )
170                 ) then
171                 create_room(room_jid);
172         end
173 end
174 module:hook("presence/full", attempt_room_creation, -1)
175 module:hook("presence/bare", attempt_room_creation, -1)
176 module:hook("presence/host", attempt_room_creation, -1)
177
178 for event_name, method in pairs {
179         -- Normal room interactions
180         ["iq-get/bare/http://jabber.org/protocol/disco#info:query"] = "handle_disco_info_get_query" ;
181         ["iq-get/bare/http://jabber.org/protocol/disco#items:query"] = "handle_disco_items_get_query" ;
182         ["iq-set/bare/http://jabber.org/protocol/muc#admin:query"] = "handle_admin_query_set_command" ;
183         ["iq-get/bare/http://jabber.org/protocol/muc#admin:query"] = "handle_admin_query_get_command" ;
184         ["iq-set/bare/http://jabber.org/protocol/muc#owner:query"] = "handle_owner_query_set_to_room" ;
185         ["iq-get/bare/http://jabber.org/protocol/muc#owner:query"] = "handle_owner_query_get_to_room" ;
186         ["message/bare"] = "handle_message_to_room" ;
187         ["presence/bare"] = "handle_presence_to_room" ;
188         -- Host room
189         ["iq-get/host/http://jabber.org/protocol/disco#info:query"] = "handle_disco_info_get_query" ;
190         ["iq-get/host/http://jabber.org/protocol/disco#items:query"] = "handle_disco_items_get_query" ;
191         ["iq-set/host/http://jabber.org/protocol/muc#admin:query"] = "handle_admin_query_set_command" ;
192         ["iq-get/host/http://jabber.org/protocol/muc#admin:query"] = "handle_admin_query_get_command" ;
193         ["iq-set/host/http://jabber.org/protocol/muc#owner:query"] = "handle_owner_query_set_to_room" ;
194         ["iq-get/host/http://jabber.org/protocol/muc#owner:query"] = "handle_owner_query_get_to_room" ;
195         ["message/host"] = "handle_message_to_room" ;
196         ["presence/host"] = "handle_presence_to_room" ;
197         -- Direct to occupant (normal rooms and host room)
198         ["presence/full"] = "handle_presence_to_occupant" ;
199         ["iq/full"] = "handle_iq_to_occupant" ;
200         ["message/full"] = "handle_message_to_occupant" ;
201 } do
202         module:hook(event_name, function (event)
203                 local origin, stanza = event.origin, event.stanza;
204                 local room = get_room_from_jid(jid_bare(stanza.attr.to))
205                 if room == nil then
206                         origin.send(st.error_reply(stanza, "cancel", "not-allowed"));
207                         return true;
208                 end
209                 return room[method](room, origin, stanza);
210         end, -2)
211 end
212
213 hosts[module:get_host()].muc = { rooms = rooms };
214
215 local saved = false;
216 module.save = function()
217         saved = true;
218         return {rooms = rooms};
219 end
220 module.restore = function(data)
221         for jid, oldroom in pairs(data.rooms or {}) do
222                 local room = create_room(jid);
223                 room._jid_nick = oldroom._jid_nick;
224                 room._occupants = oldroom._occupants;
225                 room._data = oldroom._data;
226                 room._affiliations = oldroom._affiliations;
227         end
228         hosts[module:get_host()].muc = { rooms = rooms };
229 end
230
231 function shutdown_room(room, stanza)
232         for nick, occupant in pairs(room._occupants) do
233                 stanza.attr.from = nick;
234                 for jid in pairs(occupant.sessions) do
235                         stanza.attr.to = jid;
236                         room:_route_stanza(stanza);
237                         room._jid_nick[jid] = nil;
238                 end
239                 room._occupants[nick] = nil;
240         end
241 end
242 function shutdown_component()
243         if not saved then
244                 local stanza = st.presence({type = "unavailable"})
245                         :tag("x", {xmlns = "http://jabber.org/protocol/muc#user"})
246                                 :tag("item", { affiliation='none', role='none' }):up()
247                                 :tag("status", { code = "332"}):up();
248                 for roomjid, room in pairs(rooms) do
249                         shutdown_room(room, stanza);
250                 end
251                 shutdown_room(host_room, stanza);
252         end
253 end
254 module.unload = shutdown_component;
255 module:hook_global("server-stopping", shutdown_component);
256
257 -- Ad-hoc commands
258 module:depends("adhoc")
259 local t_concat = table.concat;
260 local keys = require "util.iterators".keys;
261 local adhoc_new = module:require "adhoc".new;
262 local adhoc_initial = require "util.adhoc".new_initial_data_form;
263 local dataforms_new = require "util.dataforms".new;
264
265 local destroy_rooms_layout = dataforms_new {
266         title = "Destroy rooms";
267         instructions = "Select the rooms to destroy";
268
269         { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/muc#destroy" };
270         { name = "rooms", type = "list-multi", required = true, label = "Rooms to destroy:"};
271 };
272
273 local destroy_rooms_handler = adhoc_initial(destroy_rooms_layout, function()
274         return { rooms = array.collect(keys(rooms)):sort() };
275 end, function(fields, errors)
276         if errors then
277                 local errmsg = {};
278                 for name, err in pairs(errors) do
279                         errmsg[#errmsg + 1] = name .. ": " .. err;
280                 end
281                 return { status = "completed", error = { message = t_concat(errmsg, "\n") } };
282         end
283         for _, room in ipairs(fields.rooms) do
284                 rooms[room]:destroy();
285                 rooms[room] = nil;
286         end
287         return { status = "completed", info = "The following rooms were destroyed:\n"..t_concat(fields.rooms, "\n") };
288 end);
289 local destroy_rooms_desc = adhoc_new("Destroy Rooms", "http://prosody.im/protocol/muc#destroy", destroy_rooms_handler, "admin");
290
291 module:provides("adhoc", destroy_rooms_desc);