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