Comparison

plugins/muc/mod_muc.lua @ 6244:dfaacf042cfe

plugins/muc/mod_muc: Remove attempt_room_creation and create_room function. Instead have a 'track_room' function called from the end of the pre-create hook, and just create an un-tracked room object when we get a presence
author daurnimator <quae@daurnimator.com>
date Tue, 29 Apr 2014 19:35:25 -0400
parent 6243:b7c95e9c13de
child 6245:8ec4ff630eb4
comparison
equal deleted inserted replaced
6243:b7c95e9c13de 6244:dfaacf042cfe
11 if module:get_host_type() ~= "component" then 11 if module:get_host_type() ~= "component" then
12 error("MUC should be loaded as a component, please see http://prosody.im/doc/components", 0); 12 error("MUC should be loaded as a component, please see http://prosody.im/doc/components", 0);
13 end 13 end
14 14
15 local muclib = module:require "muc"; 15 local muclib = module:require "muc";
16 local muc_new_room = muclib.new_room;
17 room_mt = muclib.room_mt; -- Yes, global. 16 room_mt = muclib.room_mt; -- Yes, global.
18 local jid_split = require "util.jid".split; 17 local jid_split = require "util.jid".split;
19 local jid_bare = require "util.jid".bare; 18 local jid_bare = require "util.jid".bare;
20 local st = require "util.stanza"; 19 local st = require "util.stanza";
21 local um_is_admin = require "core.usermanager".is_admin; 20 local um_is_admin = require "core.usermanager".is_admin;
46 if is_admin(jid) then return nil, "modify", "not-acceptable"; end 45 if is_admin(jid) then return nil, "modify", "not-acceptable"; end
47 return _set_affiliation(self, actor, jid, ...); 46 return _set_affiliation(self, actor, jid, ...);
48 end 47 end
49 end 48 end
50 49
51 function create_room(jid) 50 function track_room(room)
52 local room = muc_new_room(jid); 51 rooms[room.jid] = room;
53 rooms[jid] = room;
54 module:fire_event("muc-room-created", { room = room });
55 return room;
56 end 52 end
57 53
58 function forget_room(jid) 54 function forget_room(jid)
59 rooms[jid] = nil; 55 rooms[jid] = nil;
60 end 56 end
91 end 87 end
92 if forced then persistent_rooms_storage:set(nil, persistent_rooms); end 88 if forced then persistent_rooms_storage:set(nil, persistent_rooms); end
93 end 89 end
94 90
95 -- When room is created, over-ride 'save' method 91 -- When room is created, over-ride 'save' method
96 module:hook("muc-room-created", function(event) 92 module:hook("muc-occupant-pre-create", function(event)
97 event.room.save = room_save; 93 event.room.save = room_save;
98 end); 94 end, 1000);
99 95
100 -- Automatically destroy empty non-persistent rooms 96 -- Automatically destroy empty non-persistent rooms
101 module:hook("muc-occupant-left",function(event) 97 module:hook("muc-occupant-left",function(event)
102 local room = event.room 98 local room = event.room
103 if not room:has_occupant() and not persistent.get(room) then -- empty, non-persistent room 99 if not room:has_occupant() and not persistent.get(room) then -- empty, non-persistent room
108 local persistent_errors = false; 104 local persistent_errors = false;
109 for jid in pairs(persistent_rooms) do 105 for jid in pairs(persistent_rooms) do
110 local node = jid_split(jid); 106 local node = jid_split(jid);
111 local data = room_configs:get(node); 107 local data = room_configs:get(node);
112 if data then 108 if data then
113 local room = create_room(jid); 109 local room = muclib.new_room(jid);
114 room._data = data._data; 110 room._data = data._data;
115 room._affiliations = data._affiliations; 111 room._affiliations = data._affiliations;
112 track_room(room);
116 else -- missing room data 113 else -- missing room data
117 persistent_rooms[jid] = nil; 114 persistent_rooms[jid] = nil;
118 module:log("error", "Missing data for room '%s', removing from persistent room list", jid); 115 module:log("error", "Missing data for room '%s', removing from persistent room list", jid);
119 persistent_errors = true; 116 persistent_errors = true;
120 end 117 end
129 if not room:get_hidden() then 126 if not room:get_hidden() then
130 reply:tag("item", {jid=jid, name=room:get_name()}):up(); 127 reply:tag("item", {jid=jid, name=room:get_name()}):up();
131 end 128 end
132 end 129 end
133 end); 130 end);
131
132 module:hook("muc-room-pre-create", function(event)
133 track_room(event.room);
134 end, -1000);
134 135
135 module:hook("muc-room-destroyed",function(event) 136 module:hook("muc-room-destroyed",function(event)
136 local room = event.room 137 local room = event.room
137 forget_room(room.jid) 138 forget_room(room.jid)
138 end) 139 end)
154 return true; 155 return true;
155 end 156 end
156 end); 157 end);
157 end 158 end
158 end 159 end
159
160 -- Watch presence to create rooms
161 local function attempt_room_creation(event)
162 local origin, stanza = event.origin, event.stanza;
163 local room_jid = jid_bare(stanza.attr.to);
164 if stanza.attr.type == nil and get_room_from_jid(room_jid) == nil then
165 create_room(room_jid);
166 end
167 end
168 module:hook("presence/full", attempt_room_creation, -1)
169 module:hook("presence/bare", attempt_room_creation, -1)
170 module:hook("presence/host", attempt_room_creation, -1)
171 160
172 for event_name, method in pairs { 161 for event_name, method in pairs {
173 -- Normal room interactions 162 -- Normal room interactions
174 ["iq-get/bare/http://jabber.org/protocol/disco#info:query"] = "handle_disco_info_get_query" ; 163 ["iq-get/bare/http://jabber.org/protocol/disco#info:query"] = "handle_disco_info_get_query" ;
175 ["iq-get/bare/http://jabber.org/protocol/disco#items:query"] = "handle_disco_items_get_query" ; 164 ["iq-get/bare/http://jabber.org/protocol/disco#items:query"] = "handle_disco_items_get_query" ;
193 ["iq/full"] = "handle_iq_to_occupant" ; 182 ["iq/full"] = "handle_iq_to_occupant" ;
194 ["message/full"] = "handle_message_to_occupant" ; 183 ["message/full"] = "handle_message_to_occupant" ;
195 } do 184 } do
196 module:hook(event_name, function (event) 185 module:hook(event_name, function (event)
197 local origin, stanza = event.origin, event.stanza; 186 local origin, stanza = event.origin, event.stanza;
198 local room = get_room_from_jid(jid_bare(stanza.attr.to)) 187 local room_jid = jid_bare(stanza.attr.to);
188 local room = get_room_from_jid(room_jid);
199 if room == nil then 189 if room == nil then
200 origin.send(st.error_reply(stanza, "cancel", "not-allowed")); 190 -- Watch presence to create rooms
201 return true; 191 if stanza.attr.type == nil and stanza.name == "presence" then
192 room = muclib.new_room(room_jid);
193 else
194 origin.send(st.error_reply(stanza, "cancel", "not-allowed"));
195 return true;
196 end
202 end 197 end
203 return room[method](room, origin, stanza); 198 return room[method](room, origin, stanza);
204 end, -2) 199 end, -2)
205 end 200 end
206 201