Comparison

plugins/muc/mod_muc.lua @ 6245:8ec4ff630eb4

plugins/muc/mod_muc.lua: Add "each_room" function to iterate over rooms (instead of accessing directly)
author daurnimator <quae@daurnimator.com>
date Wed, 30 Apr 2014 13:12:32 -0400
parent 6244:dfaacf042cfe
child 6246:45b8ad2b14c5
comparison
equal deleted inserted replaced
6244:dfaacf042cfe 6245:8ec4ff630eb4
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 room_mt = muclib.room_mt; -- Yes, global. 16 room_mt = muclib.room_mt; -- Yes, global.
17 local iterators = require "util.iterators";
17 local jid_split = require "util.jid".split; 18 local jid_split = require "util.jid".split;
18 local jid_bare = require "util.jid".bare; 19 local jid_bare = require "util.jid".bare;
19 local st = require "util.stanza"; 20 local st = require "util.stanza";
20 local um_is_admin = require "core.usermanager".is_admin; 21 local um_is_admin = require "core.usermanager".is_admin;
21 local hosts = prosody.hosts; 22 local hosts = prosody.hosts;
55 rooms[jid] = nil; 56 rooms[jid] = nil;
56 end 57 end
57 58
58 function get_room_from_jid(room_jid) 59 function get_room_from_jid(room_jid)
59 return rooms[room_jid] 60 return rooms[room_jid]
61 end
62
63 function each_room()
64 return iterators.values(rooms);
60 end 65 end
61 66
62 do -- Persistent rooms 67 do -- Persistent rooms
63 local persistent = module:require "muc/persistent"; 68 local persistent = module:require "muc/persistent";
64 local persistent_rooms_storage = module:open_store("persistent"); 69 local persistent_rooms_storage = module:open_store("persistent");
120 end 125 end
121 126
122 module:hook("host-disco-items", function(event) 127 module:hook("host-disco-items", function(event)
123 local reply = event.reply; 128 local reply = event.reply;
124 module:log("debug", "host-disco-items called"); 129 module:log("debug", "host-disco-items called");
125 for jid, room in pairs(rooms) do 130 for room in each_room() do
126 if not room:get_hidden() then 131 if not room:get_hidden() then
127 reply:tag("item", {jid=jid, name=room:get_name()}):up(); 132 reply:tag("item", {jid=room.jid, name=room:get_name()}):up();
128 end 133 end
129 end 134 end
130 end); 135 end);
131 136
132 module:hook("muc-room-pre-create", function(event) 137 module:hook("muc-room-pre-create", function(event)
200 end 205 end
201 206
202 function shutdown_component() 207 function shutdown_component()
203 local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user"}) 208 local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user"})
204 :tag("status", { code = "332"}):up(); 209 :tag("status", { code = "332"}):up();
205 for roomjid, room in pairs(rooms) do 210 for room in each_room() do
206 room:clear(x); 211 room:clear(x);
207 end 212 end
208 end 213 end
209 module:hook_global("server-stopping", shutdown_component); 214 module:hook_global("server-stopping", shutdown_component);
210 215
211 -- Ad-hoc commands 216 -- Ad-hoc commands
212 module:depends("adhoc") 217 module:depends("adhoc")
213 local t_concat = table.concat; 218 local t_concat = table.concat;
214 local keys = require "util.iterators".keys;
215 local adhoc_new = module:require "adhoc".new; 219 local adhoc_new = module:require "adhoc".new;
216 local adhoc_initial = require "util.adhoc".new_initial_data_form; 220 local adhoc_initial = require "util.adhoc".new_initial_data_form;
217 local dataforms_new = require "util.dataforms".new; 221 local dataforms_new = require "util.dataforms".new;
218 222
219 local destroy_rooms_layout = dataforms_new { 223 local destroy_rooms_layout = dataforms_new {
223 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/muc#destroy" }; 227 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/muc#destroy" };
224 { name = "rooms", type = "list-multi", required = true, label = "Rooms to destroy:"}; 228 { name = "rooms", type = "list-multi", required = true, label = "Rooms to destroy:"};
225 }; 229 };
226 230
227 local destroy_rooms_handler = adhoc_initial(destroy_rooms_layout, function() 231 local destroy_rooms_handler = adhoc_initial(destroy_rooms_layout, function()
228 return { rooms = array.collect(keys(rooms)):sort() }; 232 return { rooms = array.collect(each_room):pluck("jid"):sort(); };
229 end, function(fields, errors) 233 end, function(fields, errors)
230 if errors then 234 if errors then
231 local errmsg = {}; 235 local errmsg = {};
232 for name, err in pairs(errors) do 236 for name, err in pairs(errors) do
233 errmsg[#errmsg + 1] = name .. ": " .. err; 237 errmsg[#errmsg + 1] = name .. ": " .. err;