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 local iterators = require "util.iterators";
16 local jid_split = require "util.jid".split;
17 local jid_bare = require "util.jid".bare;
18 local st = require "util.stanza";
19 local um_is_admin = require "core.usermanager".is_admin;
20
21 local rooms = module:shared "rooms";
22
23 module:depends("disco");
24 module:add_identity("conference", "text", module:get_option_string("name", "Prosody Chatrooms"));
25 module:add_feature("http://jabber.org/protocol/muc");
26 module:depends "muc_unique"
27 module:require "muc/lock";
28
29 local function is_admin(jid)
30         return um_is_admin(jid, module.host);
31 end
32
33 do -- Monkey patch to make server admins room owners
34         local _get_affiliation = room_mt.get_affiliation;
35         function room_mt:get_affiliation(jid)
36                 if is_admin(jid) then return "owner"; end
37                 return _get_affiliation(self, jid);
38         end
39
40         local _set_affiliation = room_mt.set_affiliation;
41         function room_mt:set_affiliation(actor, jid, ...)
42                 if is_admin(jid) then return nil, "modify", "not-acceptable"; end
43                 return _set_affiliation(self, actor, jid, ...);
44         end
45 end
46
47 local persistent = module:require "muc/persistent";
48 local persistent_rooms_storage = module:open_store("persistent");
49 local persistent_rooms = module:open_store("persistent", "map");
50 local room_configs = module:open_store("config");
51
52 local function room_save(room, forced)
53         local node = jid_split(room.jid);
54         local is_persistent = persistent.get(room);
55         persistent_rooms:set(nil, room.jid, is_persistent);
56         if is_persistent then
57                 local history = room._data.history;
58                 room._data.history = nil;
59                 local data = {
60                         jid = room.jid;
61                         _data = room._data;
62                         _affiliations = room._affiliations;
63                 };
64                 room_configs:set(node, data);
65                 room._data.history = history;
66         elseif forced then
67                 room_configs:set(node, nil);
68                 if not next(room._occupants) then -- Room empty
69                         rooms[room.jid] = nil;
70                 end
71         end
72 end
73
74 -- Automatically destroy empty non-persistent rooms
75 module:hook("muc-occupant-left",function(event)
76         local room = event.room
77         if not room:has_occupant() and not persistent.get(room) then -- empty, non-persistent room
78                 module:fire_event("muc-room-destroyed", { room = room });
79         end
80 end);
81
82 function track_room(room)
83         rooms[room.jid] = room;
84         -- When room is created, over-ride 'save' method
85         room.save = room_save;
86 end
87
88 local function restore_room(jid)
89         local node = jid_split(jid);
90         local data = room_configs:get(node);
91         if data then
92                 local room = muclib.new_room(jid);
93                 room._data = data._data;
94                 room._affiliations = data._affiliations;
95                 track_room(room);
96                 return room;
97         end
98 end
99
100 function forget_room(room)
101         local room_jid = room.jid;
102         local node = jid_split(room.jid);
103         rooms[room_jid] = nil;
104         room_configs:set(node, nil);
105         if persistent.get(room) then
106                 persistent_rooms:set(nil, room_jid, nil);
107         end
108 end
109
110 function get_room_from_jid(room_jid)
111         local room = rooms[room_jid];
112         if room == nil then
113                 -- Check if in persistent storage
114                 if persistent_rooms:get(nil, room_jid) then
115                         room = restore_room(room_jid);
116                         if room == nil then
117                                 module:log("error", "Missing data for room '%s', removing from persistent room list", room_jid);
118                                 persistent_rooms:set(nil, room_jid, nil);
119                         end
120                 end
121         end
122         return room
123 end
124
125 function each_room(local_only)
126         if not local_only then
127                 for room_jid in pairs(persistent_rooms_storage:get(nil) or {}) do
128                         if rooms[room_jid] == nil then -- Don't restore rooms that already exist
129                                 local room = restore_room(room_jid);
130                                 if room == nil then
131                                         module:log("error", "Missing data for room '%s', omitting from iteration", room_jid);
132                                 end
133                         end
134                 end
135         end
136         return iterators.values(rooms);
137 end
138
139 module:hook("host-disco-items", function(event)
140         local reply = event.reply;
141         module:log("debug", "host-disco-items called");
142         for room in each_room() do
143                 if not room:get_hidden() then
144                         reply:tag("item", {jid=room.jid, name=room:get_name()}):up();
145                 end
146         end
147 end);
148
149 module:hook("muc-room-pre-create", function(event)
150         track_room(event.room);
151 end, -1000);
152
153 module:hook("muc-room-destroyed",function(event)
154         return forget_room(event.room);
155 end)
156
157 do
158         local restrict_room_creation = module:get_option("restrict_room_creation");
159         if restrict_room_creation == true then
160                 restrict_room_creation = "admin";
161         end
162         if restrict_room_creation then
163                 local host_suffix = module.host:gsub("^[^%.]+%.", "");
164                 module:hook("muc-room-pre-create", function(event)
165                         local origin, stanza = event.origin, event.stanza;
166                         local user_jid = stanza.attr.from;
167                         if not is_admin(user_jid) and not (
168                                 restrict_room_creation == "local" and
169                                 select(2, jid_split(user_jid)) == host_suffix
170                         ) then
171                                 origin.send(st.error_reply(stanza, "cancel", "not-allowed"));
172                                 return true;
173                         end
174                 end);
175         end
176 end
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_jid = jid_bare(stanza.attr.to);
205                 local room = get_room_from_jid(room_jid);
206                 if room == nil then
207                         -- Watch presence to create rooms
208                         if stanza.attr.type == nil and stanza.name == "presence" then
209                                 room = muclib.new_room(room_jid);
210                         else
211                                 origin.send(st.error_reply(stanza, "cancel", "not-allowed"));
212                                 return true;
213                         end
214                 end
215                 return room[method](room, origin, stanza);
216         end, -2)
217 end
218
219 function shutdown_component()
220         local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user"})
221                 :tag("status", { code = "332"}):up();
222         for room in each_room(true) do
223                 room:clear(x);
224         end
225 end
226 module:hook_global("server-stopping", shutdown_component);
227
228 do -- Ad-hoc commands
229         module:depends "adhoc";
230         local t_concat = table.concat;
231         local adhoc_new = module:require "adhoc".new;
232         local adhoc_initial = require "util.adhoc".new_initial_data_form;
233         local array = require "util.array";
234         local dataforms_new = require "util.dataforms".new;
235
236         local destroy_rooms_layout = dataforms_new {
237                 title = "Destroy rooms";
238                 instructions = "Select the rooms to destroy";
239
240                 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/muc#destroy" };
241                 { name = "rooms", type = "list-multi", required = true, label = "Rooms to destroy:"};
242         };
243
244         local destroy_rooms_handler = adhoc_initial(destroy_rooms_layout, function()
245                 return { rooms = array.collect(each_room()):pluck("jid"):sort(); };
246         end, function(fields, errors)
247                 if errors then
248                         local errmsg = {};
249                         for name, err in pairs(errors) do
250                                 errmsg[#errmsg + 1] = name .. ": " .. err;
251                         end
252                         return { status = "completed", error = { message = t_concat(errmsg, "\n") } };
253                 end
254                 for _, room in ipairs(fields.rooms) do
255                         get_room_from_jid(room):destroy();
256                 end
257                 return { status = "completed", info = "The following rooms were destroyed:\n"..t_concat(fields.rooms, "\n") };
258         end);
259         local destroy_rooms_desc = adhoc_new("Destroy Rooms", "http://prosody.im/protocol/muc#destroy", destroy_rooms_handler, "admin");
260
261         module:provides("adhoc", destroy_rooms_desc);
262 end