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