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