MUC: Added config option 'restrict_room_creation' to allow restricting room creation...
[prosody.git] / plugins / muc / mod_muc.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 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 and restrict_room_creation ~= true then restrict_room_creation = nil; end
19 local history_length = 20;
20
21 local muc_new_room = module:require "muc".new_room;
22 local register_component = require "core.componentmanager".register_component;
23 local deregister_component = require "core.componentmanager".deregister_component;
24 local jid_split = require "util.jid".split;
25 local jid_bare = require "util.jid".bare;
26 local st = require "util.stanza";
27 local uuid_gen = require "util.uuid".generate;
28 local datamanager = require "util.datamanager";
29 local um_is_admin = require "core.usermanager".is_admin;
30
31 local rooms = {};
32 local persistent_rooms = datamanager.load(nil, muc_host, "persistent") or {};
33 local component;
34
35 local function is_admin(jid)
36         return um_is_admin(jid) or um_is_admin(jid, module.host);
37 end
38
39 local function room_route_stanza(room, stanza) core_post_stanza(component, stanza); end
40 local function room_save(room, forced)
41         local node = jid_split(room.jid);
42         persistent_rooms[room.jid] = room._data.persistent;
43         if room._data.persistent then
44                 local history = room._data.history;
45                 room._data.history = nil;
46                 local data = {
47                         jid = room.jid;
48                         _data = room._data;
49                         _affiliations = room._affiliations;
50                 };
51                 datamanager.store(node, muc_host, "config", data);
52                 room._data.history = history;
53         elseif forced then
54                 datamanager.store(node, muc_host, "config", nil);
55         end
56         if forced then datamanager.store(nil, muc_host, "persistent", persistent_rooms); end
57 end
58
59 for jid in pairs(persistent_rooms) do
60         local node = jid_split(jid);
61         local data = datamanager.load(node, muc_host, "config") or {};
62         local room = muc_new_room(jid);
63         room._data = data._data;
64         room._affiliations = data._affiliations;
65         room.route_stanza = room_route_stanza;
66         room.save = room_save;
67         rooms[jid] = room;
68 end
69
70 local host_room = muc_new_room(muc_host);
71 host_room.route_stanza = room_route_stanza;
72 host_room.save = room_save;
73
74 local function get_disco_info(stanza)
75         return st.iq({type='result', id=stanza.attr.id, from=muc_host, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#info")
76                 :tag("identity", {category='conference', type='text', name=muc_name}):up()
77                 :tag("feature", {var="http://jabber.org/protocol/muc"}); -- TODO cache disco reply
78 end
79 local function get_disco_items(stanza)
80         local reply = st.iq({type='result', id=stanza.attr.id, from=muc_host, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#items");
81         for jid, room in pairs(rooms) do
82                 if not room._data.hidden then
83                         reply:tag("item", {jid=jid, name=jid}):up();
84                 end
85         end
86         return reply; -- TODO cache disco reply
87 end
88
89 local function handle_to_domain(origin, stanza)
90         local type = stanza.attr.type;
91         if type == "error" or type == "result" then return; end
92         if stanza.name == "iq" and type == "get" then
93                 local xmlns = stanza.tags[1].attr.xmlns;
94                 if xmlns == "http://jabber.org/protocol/disco#info" then
95                         origin.send(get_disco_info(stanza));
96                 elseif xmlns == "http://jabber.org/protocol/disco#items" then
97                         origin.send(get_disco_items(stanza));
98                 elseif xmlns == "http://jabber.org/protocol/muc#unique" then
99                         origin.send(st.reply(stanza):tag("unique", {xmlns = xmlns}):text(uuid_gen())); -- FIXME Random UUIDs can theoretically have collisions
100                 else
101                         origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- TODO disco/etc
102                 end
103         else
104                 host_room:handle_stanza(origin, stanza);
105                 --origin.send(st.error_reply(stanza, "cancel", "service-unavailable", "The muc server doesn't deal with messages and presence directed at it"));
106         end
107 end
108
109 component = register_component(muc_host, function(origin, stanza)
110         local to_node, to_host, to_resource = jid_split(stanza.attr.to);
111         if to_node then
112                 local bare = to_node.."@"..to_host;
113                 if to_host == muc_host or bare == muc_host then
114                         local room = rooms[bare];
115                         if not room then
116                                 if not(restrict_room_creation) or is_admin(stanza.attr.from) then
117                                         room = muc_new_room(bare);
118                                         room.route_stanza = room_route_stanza;
119                                         room.save = room_save;
120                                         rooms[bare] = room;
121                                 end
122                         end
123                         if room then
124                                 room:handle_stanza(origin, stanza);
125                                 if not next(room._occupants) and not persistent_rooms[room.jid] then -- empty, non-persistent room
126                                         rooms[bare] = nil; -- discard room
127                                 end
128                         else
129                                 origin.send(st.error_reply(stanza, "cancel", "not-allowed"));
130                         end
131                 else --[[not for us?]] end
132                 return;
133         end
134         -- to the main muc domain
135         handle_to_domain(origin, stanza);
136 end);
137 function component.send(stanza) -- FIXME do a generic fix
138         if stanza.attr.type == "result" or stanza.attr.type == "error" then
139                 core_post_stanza(component, stanza);
140         else error("component.send only supports result and error stanzas at the moment"); end
141 end
142
143 prosody.hosts[module:get_host()].muc = { rooms = rooms };
144
145 module.unload = function()
146         deregister_component(muc_host);
147 end
148 module.save = function()
149         return {rooms = rooms};
150 end
151 module.restore = function(data)
152         rooms = {};
153         for jid, oldroom in pairs(data.rooms or {}) do
154                 local room = muc_new_room(jid);
155                 room._jid_nick = oldroom._jid_nick;
156                 room._occupants = oldroom._occupants;
157                 room._data = oldroom._data;
158                 room._affiliations = oldroom._affiliations;
159                 room.route_stanza = room_route_stanza;
160                 room.save = room_save;
161                 rooms[jid] = room;
162         end
163         prosody.hosts[module:get_host()].muc = { rooms = rooms };
164 end