Software /
code /
prosody-modules
Comparison
mod_blocking/mod_blocking.lua @ 176:26bb69a57749
mod_blocking: Ensure that a JID can be in the blocklist at most once, and have helper functions return true/false on success/error
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Mon, 14 Jun 2010 12:25:53 +0100 |
parent | 175:92a72435721a |
child | 177:bcd7dc51a5e3 |
comparison
equal
deleted
inserted
replaced
175:92a72435721a | 176:26bb69a57749 |
---|---|
19 privacy_lists.lists[default_list_name] = default_list; | 19 privacy_lists.lists[default_list_name] = default_list; |
20 end | 20 end |
21 local items = default_list.items; | 21 local items = default_list.items; |
22 local order = items[1] and items[1].order or 0; -- Must come first | 22 local order = items[1] and items[1].order or 0; -- Must come first |
23 for i=1,#items do -- order must be unique | 23 for i=1,#items do -- order must be unique |
24 items[i].order = items[i].order + 1; | 24 local item = items[i]; |
25 item.order = item.order + 1; | |
26 if item.type == "jid" and item.action == "deny" and item.value == jid then | |
27 return false; | |
28 end | |
25 end | 29 end |
26 table.insert(items, 1, { type = "jid" | 30 table.insert(items, 1, { type = "jid" |
27 , action = "deny" | 31 , action = "deny" |
28 , value = jid | 32 , value = jid |
29 , message = false | 33 , message = false |
31 , ["presence-in"] = false | 35 , ["presence-in"] = false |
32 , iq = false | 36 , iq = false |
33 , order = order | 37 , order = order |
34 }); | 38 }); |
35 datamanager.store(username, host, "privacy", privacy_lists); | 39 datamanager.store(username, host, "privacy", privacy_lists); |
40 return true; | |
36 end | 41 end |
37 | 42 |
38 -- Remove JID from default privacy list | 43 -- Remove JID from default privacy list |
39 function remove_blocked_jid(username, host, jid) | 44 function remove_blocked_jid(username, host, jid) |
40 local privacy_lists = datamanager.load(username, host, "privacy") or {}; | 45 local privacy_lists = datamanager.load(username, host, "privacy") or {}; |
41 local default_list_name = privacy_lists.default; | 46 local default_list_name = privacy_lists.default; |
42 if not default_list_name then return; end | 47 if not default_list_name then return; end |
43 local default_list = privacy_lists.lists[default_list_name]; | 48 local default_list = privacy_lists.lists[default_list_name]; |
44 if not default_list then return; end | 49 if not default_list then return; end |
45 local items = default_list.items; | 50 local items = default_list.items; |
46 local item; | 51 local item, removed = nil, false; |
47 for i=1,#items do -- order must be unique | 52 for i=1,#items do -- order must be unique |
48 item = items[i]; | 53 item = items[i]; |
49 if item.type == "jid" and item.action == "deny" and item.value == jid then | 54 if item.type == "jid" and item.action == "deny" and item.value == jid then |
50 table.remove(items, i); | 55 table.remove(items, i); |
51 return true; | 56 removed = true; |
57 break; | |
52 end | 58 end |
53 end | 59 end |
54 datamanager.store(username, host, "privacy", privacy_lists); | 60 if removed then |
61 datamanager.store(username, host, "privacy", privacy_lists); | |
62 end | |
63 return removed; | |
55 end | 64 end |
56 | 65 |
57 function remove_all_blocked_jids(username, host) | 66 function remove_all_blocked_jids(username, host) |
58 local privacy_lists = datamanager.load(username, host, "privacy") or {}; | 67 local privacy_lists = datamanager.load(username, host, "privacy") or {}; |
59 local default_list_name = privacy_lists.default; | 68 local default_list_name = privacy_lists.default; |
67 if item.type == "jid" and item.action == "deny" then | 76 if item.type == "jid" and item.action == "deny" then |
68 table.remove(items, i); | 77 table.remove(items, i); |
69 end | 78 end |
70 end | 79 end |
71 datamanager.store(username, host, "privacy", privacy_lists); | 80 datamanager.store(username, host, "privacy", privacy_lists); |
81 return true; | |
72 end | 82 end |
73 | 83 |
74 function get_blocked_jids(username, host) | 84 function get_blocked_jids(username, host) |
75 -- Return array of blocked JIDs in default privacy list | 85 -- Return array of blocked JIDs in default privacy list |
76 local privacy_lists = datamanager.load(username, host, "privacy") or {}; | 86 local privacy_lists = datamanager.load(username, host, "privacy") or {}; |