0f27519650577dbf598426b6fb38d0f298398d4d
[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 muclib = module:require "muc";
26 local muc_new_room = muclib.new_room;
27 local jid_split = require "util.jid".split;
28 local jid_bare = require "util.jid".bare;
29 local st = require "util.stanza";
30 local uuid_gen = require "util.uuid".generate;
31 local datamanager = require "util.datamanager";
32 local um_is_admin = require "core.usermanager".is_admin;
33 local hosts = hosts;
34
35 rooms = {};
36 local rooms = rooms;
37 local persistent_rooms = datamanager.load(nil, muc_host, "persistent") or {};
38
39 -- Configurable options
40 local max_history_messages = module:get_option_number("max_history_messages");
41
42 local function is_admin(jid)
43         return um_is_admin(jid, module.host);
44 end
45
46 local _set_affiliation = muc_new_room.room_mt.set_affiliation;
47 local _get_affiliation = muc_new_room.room_mt.get_affiliation;
48 function muclib.room_mt:get_affiliation(jid)
49         if is_admin(jid) then return "owner"; end
50         return _get_affiliation(self, jid);
51 end
52 function muclib.room_mt:set_affiliation(actor, jid, affiliation, callback, reason)
53         if is_admin(jid) then return nil, "modify", "not-acceptable"; end
54         return _set_affiliation(self, actor, jid, affiliation, callback, reason);
55 end
56
57 local function room_route_stanza(room, stanza) module:send(stanza); end
58 local function room_save(room, forced)
59         local node = jid_split(room.jid);
60         persistent_rooms[room.jid] = room._data.persistent;
61         if room._data.persistent then
62                 local history = room._data.history;
63                 room._data.history = nil;
64                 local data = {
65                         jid = room.jid;
66                         _data = room._data;
67                         _affiliations = room._affiliations;
68                 };
69                 datamanager.store(node, muc_host, "config", data);
70                 room._data.history = history;
71         elseif forced then
72                 datamanager.store(node, muc_host, "config", nil);
73                 if not next(room._occupants) then -- Room empty
74                         rooms[room.jid] = nil;
75                 end
76         end
77         if forced then datamanager.store(nil, muc_host, "persistent", persistent_rooms); end
78 end
79
80 local persistent_errors = false;
81 for jid in pairs(persistent_rooms) do
82         local node = jid_split(jid);
83         local data = datamanager.load(node, muc_host, "config");
84         if data then
85                 local room = muc_new_room(jid, {
86                         max_history_length = max_history_messages;
87                 });
88                 room._data = data._data;
89                 room._data.max_history_length = max_history_messages; -- Overwrite old max_history_length in data with current settings
90                 room._affiliations = data._affiliations;
91                 room.route_stanza = room_route_stanza;
92                 room.save = room_save;
93                 rooms[jid] = room;
94         else -- missing room data
95                 persistent_rooms[jid] = nil;
96                 module:log("error", "Missing data for room '%s', removing from persistent room list", jid);
97                 persistent_errors = true;
98         end
99 end
100 if persistent_errors then datamanager.store(nil, muc_host, "persistent", persistent_rooms); end
101
102 local host_room = muc_new_room(muc_host, {
103         max_history_length = max_history_messages;
104 });
105 host_room.route_stanza = room_route_stanza;
106 host_room.save = room_save;
107
108 local function get_disco_info(stanza)
109         return st.iq({type='result', id=stanza.attr.id, from=muc_host, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#info")
110                 :tag("identity", {category='conference', type='text', name=muc_name}):up()
111                 :tag("feature", {var="http://jabber.org/protocol/muc"}); -- TODO cache disco reply
112 end
113 local function get_disco_items(stanza)
114         local reply = st.iq({type='result', id=stanza.attr.id, from=muc_host, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#items");
115         for jid, room in pairs(rooms) do
116                 if not room:is_hidden() then
117                         reply:tag("item", {jid=jid, name=room:get_name()}):up();
118                 end
119         end
120         return reply; -- TODO cache disco reply
121 end
122
123 local function handle_to_domain(event)
124         local origin, stanza = event.origin, event.stanza;
125         local type = stanza.attr.type;
126         if type == "error" or type == "result" then return; end
127         if stanza.name == "iq" and type == "get" then
128                 local xmlns = stanza.tags[1].attr.xmlns;
129                 if xmlns == "http://jabber.org/protocol/disco#info" then
130                         origin.send(get_disco_info(stanza));
131                 elseif xmlns == "http://jabber.org/protocol/disco#items" then
132                         origin.send(get_disco_items(stanza));
133                 elseif xmlns == "http://jabber.org/protocol/muc#unique" then
134                         origin.send(st.reply(stanza):tag("unique", {xmlns = xmlns}):text(uuid_gen())); -- FIXME Random UUIDs can theoretically have collisions
135                 else
136                         origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- TODO disco/etc
137                 end
138         else
139                 host_room:handle_stanza(origin, stanza);
140                 --origin.send(st.error_reply(stanza, "cancel", "service-unavailable", "The muc server doesn't deal with messages and presence directed at it"));
141         end
142         return true;
143 end
144
145 function stanza_handler(event)
146         local origin, stanza = event.origin, event.stanza;
147         local bare = jid_bare(stanza.attr.to);
148         local room = rooms[bare];
149         if not room then
150                 if stanza.name ~= "presence" then
151                         origin.send(st.error_reply(stanza, "cancel", "item-not-found"));
152                         return true;
153                 end
154                 if not(restrict_room_creation) or
155                   (restrict_room_creation == "admin" and is_admin(stanza.attr.from)) or
156                   (restrict_room_creation == "local" and select(2, jid_split(stanza.attr.from)) == module.host:gsub("^[^%.]+%.", "")) then
157                         room = muc_new_room(bare, {
158                                 max_history_length = max_history_messages;
159                         });
160                         room.route_stanza = room_route_stanza;
161                         room.save = room_save;
162                         rooms[bare] = room;
163                 end
164         end
165         if room then
166                 room:handle_stanza(origin, stanza);
167                 if not next(room._occupants) and not persistent_rooms[room.jid] then -- empty, non-persistent room
168                         rooms[bare] = nil; -- discard room
169                 end
170         else
171                 origin.send(st.error_reply(stanza, "cancel", "not-allowed"));
172         end
173         return true;
174 end
175 module:hook("iq/bare", stanza_handler, -1);
176 module:hook("message/bare", stanza_handler, -1);
177 module:hook("presence/bare", stanza_handler, -1);
178 module:hook("iq/full", stanza_handler, -1);
179 module:hook("message/full", stanza_handler, -1);
180 module:hook("presence/full", stanza_handler, -1);
181 module:hook("iq/host", handle_to_domain, -1);
182 module:hook("message/host", handle_to_domain, -1);
183 module:hook("presence/host", handle_to_domain, -1);
184
185 hosts[module.host].send = function(stanza) -- FIXME do a generic fix
186         if stanza.attr.type == "result" or stanza.attr.type == "error" then
187                 module:send(stanza);
188         else error("component.send only supports result and error stanzas at the moment"); end
189 end
190
191 hosts[module:get_host()].muc = { rooms = rooms };
192
193 local saved = false;
194 module.save = function()
195         saved = true;
196         return {rooms = rooms};
197 end
198 module.restore = function(data)
199         for jid, oldroom in pairs(data.rooms or {}) do
200                 local room = muc_new_room(jid);
201                 room._jid_nick = oldroom._jid_nick;
202                 room._occupants = oldroom._occupants;
203                 room._data = oldroom._data;
204                 room._affiliations = oldroom._affiliations;
205                 room.route_stanza = room_route_stanza;
206                 room.save = room_save;
207                 rooms[jid] = room;
208         end
209         hosts[module:get_host()].muc = { rooms = rooms };
210 end
211
212 function shutdown_room(room, stanza)
213         for nick, occupant in pairs(room._occupants) do
214                 stanza.attr.from = nick;
215                 for jid in pairs(occupant.sessions) do
216                         stanza.attr.to = jid;
217                         room:_route_stanza(stanza);
218                         room._jid_nick[jid] = nil;
219                 end
220                 room._occupants[nick] = nil;
221         end
222 end
223 function shutdown_component()
224         if not saved then
225                 local stanza = st.presence({type = "unavailable"})
226                         :tag("x", {xmlns = "http://jabber.org/protocol/muc#user"})
227                                 :tag("item", { affiliation='none', role='none' }):up();
228                 for roomjid, room in pairs(rooms) do
229                         shutdown_room(room, stanza);
230                 end
231                 shutdown_room(host_room, stanza);
232         end
233 end
234 module.unload = shutdown_component;
235 module:hook_global("server-stopping", shutdown_component);