Comparison

plugins/mod_blocklist.lua @ 6629:42aeb882b3e1

mod_blocklist: Some cleanup [luacheck]
author Kim Alvefur <zash@zash.se>
date Sat, 25 Apr 2015 14:57:52 +0200
parent 6531:18f4973849b1
child 6833:aeb088bb1a20
comparison
equal deleted inserted replaced
6628:8495734da243 6629:42aeb882b3e1
11 11
12 local user_exists = require"core.usermanager".user_exists; 12 local user_exists = require"core.usermanager".user_exists;
13 local is_contact_subscribed = require"core.rostermanager".is_contact_subscribed; 13 local is_contact_subscribed = require"core.rostermanager".is_contact_subscribed;
14 local st = require"util.stanza"; 14 local st = require"util.stanza";
15 local st_error_reply = st.error_reply; 15 local st_error_reply = st.error_reply;
16 local jid_prep, jid_split = import("util.jid", "prep", "split"); 16 local jid_prep = require"util.jid".prep;
17 17 local jid_split = require"util.jid".split;
18 local host = module.host; 18
19 local storage = module:open_store(); 19 local storage = module:open_store();
20 local sessions = prosody.hosts[host].sessions; 20 local sessions = prosody.hosts[module.host].sessions;
21 21
22 -- Cache of blocklists used since module was loaded 22 -- Cache of blocklists used since module was loaded
23 local cache = {}; 23 local cache = {};
24 if module:get_option_boolean("blocklist_weak_cache") then 24 if module:get_option_boolean("blocklist_weak_cache") then
25 -- Lower memory usage, more IO and latency 25 -- Lower memory usage, more IO and latency
70 end 70 end
71 71
72 local function get_blocklist(username) 72 local function get_blocklist(username)
73 local blocklist = cache[username]; 73 local blocklist = cache[username];
74 if not blocklist then 74 if not blocklist then
75 if not user_exists(username, host) then 75 if not user_exists(username, module.host) then
76 return null_blocklist; 76 return null_blocklist;
77 end 77 end
78 blocklist = storage:get(username); 78 blocklist = storage:get(username);
79 if not blocklist then 79 if not blocklist then
80 blocklist = migrate_privacy_list(username); 80 blocklist = migrate_privacy_list(username);
104 local origin, stanza = event.origin, event.stanza; 104 local origin, stanza = event.origin, event.stanza;
105 local username = origin.username; 105 local username = origin.username;
106 local action = stanza.tags[1]; 106 local action = stanza.tags[1];
107 local new = {}; 107 local new = {};
108 108
109 local jid;
110 for item in action:childtags("item") do 109 for item in action:childtags("item") do
111 jid = jid_prep(item.attr.jid); 110 local jid = jid_prep(item.attr.jid);
112 if not jid then 111 if not jid then
113 return origin.send(st_error_reply(stanza, "modify", "jid-malformed")); 112 return origin.send(st_error_reply(stanza, "modify", "jid-malformed"));
114 end 113 end
115 item.attr.jid = jid; -- echo back prepped 114 item.attr.jid = jid; -- echo back prepped
116 new[jid] = is_contact_subscribed(username, host, jid) or false; 115 new[jid] = is_contact_subscribed(username, module.host, jid) or false;
117 end 116 end
118 117
119 local mode = action.name == "block" or nil; 118 local mode = action.name == "block" or nil;
120 119
121 if mode and not next(new) then 120 if mode and not next(new) then
174 module:hook("iq-set/self/urn:xmpp:blocking:block", edit_blocklist); 173 module:hook("iq-set/self/urn:xmpp:blocking:block", edit_blocklist);
175 module:hook("iq-set/self/urn:xmpp:blocking:unblock", edit_blocklist); 174 module:hook("iq-set/self/urn:xmpp:blocking:unblock", edit_blocklist);
176 175
177 -- Cache invalidation, solved! 176 -- Cache invalidation, solved!
178 module:hook_global("user-deleted", function (event) 177 module:hook_global("user-deleted", function (event)
179 if event.host == host then 178 if event.host == module.host then
180 cache[event.username] = nil; 179 cache[event.username] = nil;
181 end 180 end
182 end); 181 end);
183 182
184 -- Buggy clients 183 -- Buggy clients
185 module:hook("iq-error/self/blocklist-push", function (event) 184 module:hook("iq-error/self/blocklist-push", function (event)
186 local type, condition, text = event.stanza:get_error(); 185 local _, condition, text = event.stanza:get_error();
187 (event.origin.log or module._log)("warn", "Client returned an error in response to notification from mod_%s: %s%s%s", module.name, condition, text and ": " or "", text or ""); 186 (event.origin.log or module._log)("warn", "Client returned an error in response to notification from mod_%s: %s%s%s", module.name, condition, text and ": " or "", text or "");
188 return true; 187 return true;
189 end); 188 end);
190 189
191 local function is_blocked(user, jid) 190 local function is_blocked(user, jid)