MUC: Return <item-not-found/> on message and iq to non-existent rooms (thanks Maranda).
[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
10 if module:get_host_type() ~= "component" then
11         error("MUC should be loaded as a component, please see http://prosody.im/doc/components", 0);
12 end
13
14 local muc_host = module:get_host();
15 local muc_name = module:get_option("name");
16 if type(muc_name) ~= "string" then muc_name = "Prosody Chatrooms"; end
17 local restrict_room_creation = module:get_option("restrict_room_creation");
18 if restrict_room_creation then
19         if restrict_room_creation == true then 
20                 restrict_room_creation = "admin";
21         elseif restrict_room_creation ~= "admin" and restrict_room_creation ~= "local" then
22                 restrict_room_creation = nil;
23         end
24 end
25 local muc_new_room = module:require "muc".new_room;
26 local jid_split = require "util.jid".split;
27 local jid_bare = require "util.jid".bare;
28 local st = require "util.stanza";
29 local uuid_gen = require "util.uuid".generate;
30 local datamanager = require "util.datamanager";
31 local um_is_admin = require "core.usermanager".is_admin;
32 local hosts = hosts;
33
34 rooms = {};
35 local rooms = rooms;
36 local persistent_rooms = datamanager.load(nil, muc_host, "persistent") or {};
37
38 -- Configurable options
39 local max_history_messages = module:get_option_number("max_history_messages");
40
41 local function is_admin(jid)
42         return um_is_admin(jid, module.host);
43 end
44
45 local function room_route_stanza(room, stanza) module:send(stanza); end
46 local function room_save(room, forced)
47         local node = jid_split(room.jid);
48         persistent_rooms[room.jid] = room._data.persistent;
49         if room._data.persistent then
50                 local history = room._data.history;
51                 room._data.history = nil;
52                 local data = {
53                         jid = room.jid;
54                         _data = room._data;
55                         _affiliations = room._affiliations;
56                 };
57                 datamanager.store(node, muc_host, "config", data);
58                 room._data.history = history;
59         elseif forced then
60                 datamanager.store(node, muc_host, "config", nil);
61                 if not next(room._occupants) then -- Room empty
62                         rooms[room.jid] = nil;
63                 end
64         end
65         if forced then datamanager.store(nil, muc_host, "persistent", persistent_rooms); end
66 end
67
68 local persistent_errors = false;
69 for jid in pairs(persistent_rooms) do
70         local node = jid_split(jid);
71         local data = datamanager.load(node, muc_host, "config");
72         if data then
73                 local room = muc_new_room(jid, {
74                         max_history_length = max_history_messages;
75                 });
76                 room._data = data._data;
77                 room._data.max_history_length = max_history_messages; -- Overwrite old max_history_length in data with current settings
78                 room._affiliations = data._affiliations;
79                 room.route_stanza = room_route_stanza;
80                 room.save = room_save;
81                 rooms[jid] = room;
82         else -- missing room data
83                 persistent_rooms[jid] = nil;
84                 module:log("error", "Missing data for room '%s', removing from persistent room list", jid);
85                 persistent_errors = true;
86         end
87 end
88 if persistent_errors then datamanager.store(nil, muc_host, "persistent", persistent_rooms); end
89
90 local host_room = muc_new_room(muc_host, {
91         max_history_length = max_history_messages;
92 });
93 host_room.route_stanza = room_route_stanza;
94 host_room.save = room_save;
95
96 local function get_disco_info(stanza)
97         return st.iq({type='result', id=stanza.attr.id, from=muc_host, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#info")
98                 :tag("identity", {category='conference', type='text', name=muc_name}):up()
99                 :tag("feature", {var="http://jabber.org/protocol/muc"}); -- TODO cache disco reply
100 end
101 local function get_disco_items(stanza)
102         local reply = st.iq({type='result', id=stanza.attr.id, from=muc_host, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#items");
103         for jid, room in pairs(rooms) do
104                 if not room:is_hidden() then
105                         reply:tag("item", {jid=jid, name=room:get_name()}):up();
106                 end
107         end
108         return reply; -- TODO cache disco reply
109 end
110
111 local function handle_to_domain(event)
112         local origin, stanza = event.origin, event.stanza;
113         local type = stanza.attr.type;
114         if type == "error" or type == "result" then return; end
115         if stanza.name == "iq" and type == "get" then
116                 local xmlns = stanza.tags[1].attr.xmlns;
117                 if xmlns == "http://jabber.org/protocol/disco#info" then
118                         origin.send(get_disco_info(stanza));
119                 elseif xmlns == "http://jabber.org/protocol/disco#items" then
120                         origin.send(get_disco_items(stanza));
121                 elseif xmlns == "http://jabber.org/protocol/muc#unique" then
122                         origin.send(st.reply(stanza):tag("unique", {xmlns = xmlns}):text(uuid_gen())); -- FIXME Random UUIDs can theoretically have collisions
123                 else
124                         origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- TODO disco/etc
125                 end
126         else
127                 host_room:handle_stanza(origin, stanza);
128                 --origin.send(st.error_reply(stanza, "cancel", "service-unavailable", "The muc server doesn't deal with messages and presence directed at it"));
129         end
130         return true;
131 end
132
133 function stanza_handler(event)
134         local origin, stanza = event.origin, event.stanza;
135         local bare = jid_bare(stanza.attr.to);
136         local room = rooms[bare];
137         if not room then
138                 if stanza.name ~= "presence" then
139                         origin.send(st.error_reply(stanza, "cancel", "item-not-found"));
140                         return true;
141                 end
142                 if not(restrict_room_creation) or
143                   (restrict_room_creation == "admin" and is_admin(stanza.attr.from)) or
144                   (restrict_room_creation == "local" and select(2, jid_split(stanza.attr.from)) == module.host:gsub("^[^%.]+%.", "")) then
145                         room = muc_new_room(bare, {
146                                 max_history_length = max_history_messages;
147                         });
148                         room.route_stanza = room_route_stanza;
149                         room.save = room_save;
150                         rooms[bare] = room;
151                 end
152         end
153         if room then
154                 room:handle_stanza(origin, stanza);
155                 if not next(room._occupants) and not persistent_rooms[room.jid] then -- empty, non-persistent room
156                         rooms[bare] = nil; -- discard room
157                 end
158         else
159                 origin.send(st.error_reply(stanza, "cancel", "not-allowed"));
160         end
161         return true;
162 end
163 module:hook("iq/bare", stanza_handler, -1);
164 module:hook("message/bare", stanza_handler, -1);
165 module:hook("presence/bare", stanza_handler, -1);
166 module:hook("iq/full", stanza_handler, -1);
167 module:hook("message/full", stanza_handler, -1);
168 module:hook("presence/full", stanza_handler, -1);
169 module:hook("iq/host", handle_to_domain, -1);
170 module:hook("message/host", handle_to_domain, -1);
171 module:hook("presence/host", handle_to_domain, -1);
172
173 hosts[module.host].send = function(stanza) -- FIXME do a generic fix
174         if stanza.attr.type == "result" or stanza.attr.type == "error" then
175                 module:send(stanza);
176         else error("component.send only supports result and error stanzas at the moment"); end
177 end
178
179 hosts[module:get_host()].muc = { rooms = rooms };
180
181 module.save = function()
182         return {rooms = rooms};
183 end
184 module.restore = function(data)
185         for jid, oldroom in pairs(data.rooms or {}) do
186                 local room = muc_new_room(jid);
187                 room._jid_nick = oldroom._jid_nick;
188                 room._occupants = oldroom._occupants;
189                 room._data = oldroom._data;
190                 room._affiliations = oldroom._affiliations;
191                 room.route_stanza = room_route_stanza;
192                 room.save = room_save;
193                 rooms[jid] = room;
194         end
195         hosts[module:get_host()].muc = { rooms = rooms };
196 end