Comparison

plugins/mod_privacy.lua @ 2588:741302471a23

mod_privacy: Don't store the privacy lists as an array, but as a map... faster and less code
author Matthew Wild <mwild1@gmail.com>
date Thu, 11 Feb 2010 12:10:06 +0000
parent 2579:4e0caff152d6
child 2589:39f5a068dc41
comparison
equal deleted inserted replaced
2587:c37f971f0fe6 2588:741302471a23
15 local jid_bare = util_Jid.bare; 15 local jid_bare = util_Jid.bare;
16 local jid_split = util_Jid.split; 16 local jid_split = util_Jid.split;
17 local load_roster = require "core.rostermanager".load_roster; 17 local load_roster = require "core.rostermanager".load_roster;
18 local to_number = tonumber; 18 local to_number = tonumber;
19 19
20 function findNamedList(privacy_lists, name)
21 if privacy_lists.lists then
22 for i=1,#privacy_lists.lists do
23 if privacy_lists.lists[i].name == name then
24 return i;
25 end
26 end
27 end
28 end
29
30 function isListUsed(origin, name, privacy_lists) 20 function isListUsed(origin, name, privacy_lists)
31 local user = bare_sessions[origin.username.."@"..origin.host]; 21 local user = bare_sessions[origin.username.."@"..origin.host];
32 if user then 22 if user then
33 for resource, session in pairs(user.sessions) do 23 for resource, session in pairs(user.sessions) do
34 if resource ~= origin.resource then 24 if resource ~= origin.resource then
124 end 114 end
125 return true; 115 return true;
126 end 116 end
127 117
128 function activateList(privacy_lists, origin, stanza, which, name) 118 function activateList(privacy_lists, origin, stanza, which, name)
129 local idx = findNamedList(privacy_lists, name); 119 local list = privacy_lists.lists[name];
130 120
131 if privacy_lists.default == nil then 121 if privacy_lists.default == nil then
132 privacy_lists.default = ""; 122 privacy_lists.default = "";
133 end 123 end
134 if origin.activePrivacyList == nil then 124 if origin.activePrivacyList == nil then
135 origin.activePrivacyList = ""; 125 origin.activePrivacyList = "";
136 end 126 end
137 127
138 if which == "default" and idx ~= nil then 128 if which == "default" and list then
139 if isAnotherSessionUsingDefaultList(origin) then 129 if isAnotherSessionUsingDefaultList(origin) then
140 return {"cancel", "conflict", "Another session is online and using the default list."}; 130 return {"cancel", "conflict", "Another session is online and using the default list."};
141 end 131 end
142 privacy_lists.default = name; 132 privacy_lists.default = name;
143 origin.send(st.reply(stanza)); 133 origin.send(st.reply(stanza));
144 --[[ 134 --[[
145 if origin.activePrivacyList == nil then 135 if origin.activePrivacyList == nil then
146 sendNeededUnavailablePersences(origin, name); 136 sendNeededUnavailablePersences(origin, name);
147 end 137 end
148 ]]-- 138 ]]--
149 elseif which == "active" and idx ~= nil then 139 elseif which == "active" and list then
150 origin.activePrivacyList = name; 140 origin.activePrivacyList = name;
151 origin.send(st.reply(stanza)); 141 origin.send(st.reply(stanza));
152 -- sendNeededUnavailablePersences(origin, name); 142 -- sendNeededUnavailablePersences(origin, name);
153 else 143 else
154 return {"modify", "bad-request", "Either not active or default given or unknown list name specified."}; 144 return {"modify", "bad-request", "Either not active or default given or unknown list name specified."};
155 end 145 end
156 return true; 146 return true;
157 end 147 end
158 148
159 function deleteList(privacy_lists, origin, stanza, name) 149 function deleteList(privacy_lists, origin, stanza, name)
160 local idx = findNamedList(privacy_lists, name); 150 local list = privacy_lists.lists[name];
161 151
162 if idx ~= nil then 152 if list then
163 if isListUsed(origin, name, privacy_lists) then 153 if isListUsed(origin, name, privacy_lists) then
164 return {"cancel", "conflict", "Another session is online and using the list which should be deleted."}; 154 return {"cancel", "conflict", "Another session is online and using the list which should be deleted."};
165 end 155 end
166 if privacy_lists.default == name then 156 if privacy_lists.default == name then
167 privacy_lists.default = ""; 157 privacy_lists.default = "";
168 end 158 end
169 if origin.activePrivacyList == name then 159 if origin.activePrivacyList == name then
170 origin.activePrivacyList = ""; 160 origin.activePrivacyList = "";
171 end 161 end
172 table.remove(privacy_lists.lists, idx); 162 privacy_lists.lists[name] = nil;
173 origin.send(st.reply(stanza)); 163 origin.send(st.reply(stanza));
174 return true; 164 return true;
175 end 165 end
176 return {"modify", "bad-request", "Not existing list specifed to be deleted."}; 166 return {"modify", "bad-request", "Not existing list specifed to be deleted."};
177 end 167 end
178 168
179 function createOrReplaceList (privacy_lists, origin, stanza, name, entries, roster) 169 function createOrReplaceList (privacy_lists, origin, stanza, name, entries, roster)
180 local idx = findNamedList(privacy_lists, name);
181 local bare_jid = origin.username.."@"..origin.host; 170 local bare_jid = origin.username.."@"..origin.host;
182 171
183 if privacy_lists.lists == nil then 172 if privacy_lists.lists == nil then
184 privacy_lists.lists = {}; 173 privacy_lists.lists = {};
185 end 174 end
186 175
187 if idx == nil then 176 local list = {};
188 idx = #privacy_lists.lists + 1; 177 privacy_lists.lists[name] = list;
189 end
190 178
191 local orderCheck = {}; 179 local orderCheck = {};
192 local list = {};
193 list.name = name; 180 list.name = name;
194 list.items = {}; 181 list.items = {};
195 182
196 for _,item in ipairs(entries) do 183 for _,item in ipairs(entries) do
197 if to_number(item.attr.order) == nil or to_number(item.attr.order) < 0 or orderCheck[item.attr.order] ~= nil then 184 if to_number(item.attr.order) == nil or to_number(item.attr.order) < 0 or orderCheck[item.attr.order] ~= nil then
262 list.items[#list.items + 1] = tmp; 249 list.items[#list.items + 1] = tmp;
263 end 250 end
264 251
265 table.sort(list, function(a, b) return a.order < b.order; end); 252 table.sort(list, function(a, b) return a.order < b.order; end);
266 253
267 privacy_lists.lists[idx] = list;
268 origin.send(st.reply(stanza)); 254 origin.send(st.reply(stanza));
269 if bare_sessions[bare_jid] ~= nil then 255 if bare_sessions[bare_jid] ~= nil then
270 local iq = st.iq ( { type = "set", id="push1" } ); 256 local iq = st.iq ( { type = "set", id="push1" } );
271 iq:tag ("query", { xmlns = "jabber:iq:privacy" } ); 257 iq:tag ("query", { xmlns = "jabber:iq:privacy" } );
272 iq:tag ("list", { name = list.name } ):up(); 258 iq:tag ("list", { name = list.name } ):up();
292 for _,list in ipairs(privacy_lists.lists) do 278 for _,list in ipairs(privacy_lists.lists) do
293 reply:tag("list", {name=list.name}):up(); 279 reply:tag("list", {name=list.name}):up();
294 end 280 end
295 end 281 end
296 else 282 else
297 local idx = findNamedList(privacy_lists, name); 283 local list = privacy_lists.lists[name];
298 if idx ~= nil then 284 if list then
299 local list = privacy_lists.lists[idx];
300 reply = reply:tag("list", {name=list.name}); 285 reply = reply:tag("list", {name=list.name});
301 for _,item in ipairs(list.items) do 286 for _,item in ipairs(list.items) do
302 reply:tag("item", {type=item.type, value=item.value, action=item.action, order=item.order}); 287 reply:tag("item", {type=item.type, value=item.value, action=item.action, order=item.order});
303 if item["message"] then reply:tag("message"):up(); end 288 if item["message"] then reply:tag("message"):up(); end
304 if item["iq"] then reply:tag("iq"):up(); end 289 if item["iq"] then reply:tag("iq"):up(); end
393 if is_from_user and is_to_user then 378 if is_from_user and is_to_user then
394 module:log("debug", "Not blocking communications between user's resources"); 379 module:log("debug", "Not blocking communications between user's resources");
395 return; -- from one of a user's resource to another => HANDS OFF! 380 return; -- from one of a user's resource to another => HANDS OFF!
396 end 381 end
397 382
398 local idx;
399 local list;
400 local item; 383 local item;
401 local listname = session.activePrivacyList; 384 local listname = session.activePrivacyList;
402 if listname == nil or listname == "" then 385 if listname == nil or listname == "" then
403 listname = privacy_lists.default; -- no active list selected, use default list 386 listname = privacy_lists.default; -- no active list selected, use default list
404 end 387 end
405 idx = findNamedList(privacy_lists, listname); 388 local list = privacy_lists.lists[listname];
406 if idx == nil then 389 if not list then
407 module:log("debug", "given privacy listname not found. name: %s", listname); 390 module:log("debug", "given privacy list not found. name: %s", listname);
408 return;
409 end
410 list = privacy_lists.lists[idx];
411 if list == nil then
412 module:log("debug", "privacy list index wrong. index: %d", idx);
413 return; 391 return;
414 end 392 end
415 for _,item in ipairs(list.items) do 393 for _,item in ipairs(list.items) do
416 local apply = false; 394 local apply = false;
417 local block = false; 395 local block = false;