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