plugins/muc: Add room:has_occupant() method
[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 restrict_room_creation = module:get_option("restrict_room_creation");
16 if restrict_room_creation then
17         if restrict_room_creation == true then
18                 restrict_room_creation = "admin";
19         elseif restrict_room_creation ~= "admin" and restrict_room_creation ~= "local" then
20                 restrict_room_creation = nil;
21         end
22 end
23
24 local muclib = module:require "muc";
25 local muc_new_room = muclib.new_room;
26 room_mt = muclib.room_mt; -- Yes, global.
27 local persistent = module:require "muc/persistent";
28 local jid_split = require "util.jid".split;
29 local jid_bare = require "util.jid".bare;
30 local st = require "util.stanza";
31 local um_is_admin = require "core.usermanager".is_admin;
32 local hosts = prosody.hosts;
33
34 rooms = {};
35 local rooms = rooms;
36 local persistent_rooms_storage = module:open_store("persistent");
37 local persistent_rooms = persistent_rooms_storage:get() or {};
38 local room_configs = module:open_store("config");
39
40 -- Configurable options
41 muclib.set_max_history_length(module:get_option_number("max_history_messages"));
42
43 module:depends("disco");
44 module:add_identity("conference", "text", module:get_option_string("name", "Prosody Chatrooms"));
45 module:add_feature("http://jabber.org/protocol/muc");
46 module:depends "muc_unique"
47 module:require "muc/lock";
48
49 local function is_admin(jid)
50         return um_is_admin(jid, module.host);
51 end
52
53 do -- Monkey patch to make server admins room owners
54         local _get_affiliation = room_mt.get_affiliation;
55         function room_mt:get_affiliation(jid)
56                 if is_admin(jid) then return "owner"; end
57                 return _get_affiliation(self, jid);
58         end
59
60         local _set_affiliation = room_mt.set_affiliation;
61         function room_mt:set_affiliation(actor, jid, ...)
62                 if is_admin(jid) then return nil, "modify", "not-acceptable"; end
63                 return _set_affiliation(self, actor, jid, ...);
64         end
65 end
66
67 local function room_save(room, forced)
68         local node = jid_split(room.jid);
69         local is_persistent = persistent.get(room);
70         persistent_rooms[room.jid] = is_persistent;
71         if is_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.save = room_save;
93         rooms[jid] = room;
94         module:fire_event("muc-room-created", { room = room });
95         return room;
96 end
97
98 function forget_room(jid)
99         rooms[jid] = nil;
100 end
101
102 function get_room_from_jid(room_jid)
103         return rooms[room_jid]
104 end
105
106 local persistent_errors = false;
107 for jid in pairs(persistent_rooms) do
108         local node = jid_split(jid);
109         local data = room_configs:get(node);
110         if data then
111                 local room = create_room(jid);
112                 room._data = data._data;
113                 room._affiliations = data._affiliations;
114         else -- missing room data
115                 persistent_rooms[jid] = nil;
116                 module:log("error", "Missing data for room '%s', removing from persistent room list", jid);
117                 persistent_errors = true;
118         end
119 end
120 if persistent_errors then persistent_rooms_storage:set(nil, persistent_rooms); end
121
122 module:hook("host-disco-items", function(event)
123         local reply = event.reply;
124         module:log("debug", "host-disco-items called");
125         for jid, room in pairs(rooms) do
126                 if not room:get_hidden() then
127                         reply:tag("item", {jid=jid, name=room:get_name()}):up();
128                 end
129         end
130 end);
131
132 module:hook("muc-room-destroyed",function(event)
133         local room = event.room
134         forget_room(room.jid)
135 end)
136
137 module:hook("muc-occupant-left",function(event)
138         local room = event.room
139         if not room:has_occupant() and not persistent.get(room) then -- empty, non-persistent room
140                 module:fire_event("muc-room-destroyed", { room = room });
141         end
142 end);
143
144 -- Watch presence to create rooms
145 local function attempt_room_creation(event)
146         local origin, stanza = event.origin, event.stanza;
147         local room_jid = jid_bare(stanza.attr.to);
148         if stanza.attr.type == nil and
149                 get_room_from_jid(room_jid) == nil and
150                 (
151                         not(restrict_room_creation) or
152                         is_admin(stanza.attr.from) or
153                         (
154                                 restrict_room_creation == "local" and
155                                 select(2, jid_split(stanza.attr.from)) == module.host:gsub("^[^%.]+%.", "")
156                         )
157                 ) then
158                 create_room(room_jid);
159         end
160 end
161 module:hook("presence/full", attempt_room_creation, -1)
162 module:hook("presence/bare", attempt_room_creation, -1)
163 module:hook("presence/host", attempt_room_creation, -1)
164
165 for event_name, method in pairs {
166         -- Normal room interactions
167         ["iq-get/bare/http://jabber.org/protocol/disco#info:query"] = "handle_disco_info_get_query" ;
168         ["iq-get/bare/http://jabber.org/protocol/disco#items:query"] = "handle_disco_items_get_query" ;
169         ["iq-set/bare/http://jabber.org/protocol/muc#admin:query"] = "handle_admin_query_set_command" ;
170         ["iq-get/bare/http://jabber.org/protocol/muc#admin:query"] = "handle_admin_query_get_command" ;
171         ["iq-set/bare/http://jabber.org/protocol/muc#owner:query"] = "handle_owner_query_set_to_room" ;
172         ["iq-get/bare/http://jabber.org/protocol/muc#owner:query"] = "handle_owner_query_get_to_room" ;
173         ["message/bare"] = "handle_message_to_room" ;
174         ["presence/bare"] = "handle_presence_to_room" ;
175         -- Host room
176         ["iq-get/host/http://jabber.org/protocol/disco#info:query"] = "handle_disco_info_get_query" ;
177         ["iq-get/host/http://jabber.org/protocol/disco#items:query"] = "handle_disco_items_get_query" ;
178         ["iq-set/host/http://jabber.org/protocol/muc#admin:query"] = "handle_admin_query_set_command" ;
179         ["iq-get/host/http://jabber.org/protocol/muc#admin:query"] = "handle_admin_query_get_command" ;
180         ["iq-set/host/http://jabber.org/protocol/muc#owner:query"] = "handle_owner_query_set_to_room" ;
181         ["iq-get/host/http://jabber.org/protocol/muc#owner:query"] = "handle_owner_query_get_to_room" ;
182         ["message/host"] = "handle_message_to_room" ;
183         ["presence/host"] = "handle_presence_to_room" ;
184         -- Direct to occupant (normal rooms and host room)
185         ["presence/full"] = "handle_presence_to_occupant" ;
186         ["iq/full"] = "handle_iq_to_occupant" ;
187         ["message/full"] = "handle_message_to_occupant" ;
188 } do
189         module:hook(event_name, function (event)
190                 local origin, stanza = event.origin, event.stanza;
191                 local room = get_room_from_jid(jid_bare(stanza.attr.to))
192                 if room == nil then
193                         origin.send(st.error_reply(stanza, "cancel", "not-allowed"));
194                         return true;
195                 end
196                 return room[method](room, origin, stanza);
197         end, -2)
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_component()
219         if not saved then
220                 local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user"})
221                                 :tag("status", { code = "332"}):up();
222                 for roomjid, room in pairs(rooms) do
223                         room:clear(x);
224                 end
225         end
226 end
227 module.unload = shutdown_component;
228 module:hook_global("server-stopping", shutdown_component);
229
230 -- Ad-hoc commands
231 module:depends("adhoc")
232 local t_concat = table.concat;
233 local keys = require "util.iterators".keys;
234 local adhoc_new = module:require "adhoc".new;
235 local adhoc_initial = require "util.adhoc".new_initial_data_form;
236 local dataforms_new = require "util.dataforms".new;
237
238 local destroy_rooms_layout = dataforms_new {
239         title = "Destroy rooms";
240         instructions = "Select the rooms to destroy";
241
242         { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/muc#destroy" };
243         { name = "rooms", type = "list-multi", required = true, label = "Rooms to destroy:"};
244 };
245
246 local destroy_rooms_handler = adhoc_initial(destroy_rooms_layout, function()
247         return { rooms = array.collect(keys(rooms)):sort() };
248 end, function(fields, errors)
249         if errors then
250                 local errmsg = {};
251                 for name, err in pairs(errors) do
252                         errmsg[#errmsg + 1] = name .. ": " .. err;
253                 end
254                 return { status = "completed", error = { message = t_concat(errmsg, "\n") } };
255         end
256         for _, room in ipairs(fields.rooms) do
257                 rooms[room]:destroy();
258                 rooms[room] = nil;
259         end
260         return { status = "completed", info = "The following rooms were destroyed:\n"..t_concat(fields.rooms, "\n") };
261 end);
262 local destroy_rooms_desc = adhoc_new("Destroy Rooms", "http://prosody.im/protocol/muc#destroy", destroy_rooms_handler, "admin");
263
264 module:provides("adhoc", destroy_rooms_desc);