comparison mod_firewall/marks.lib.lua @ 5653:62c6e17a5e9d

Merge
author Stephen Paul Weber <singpolyma@singpolyma.net>
date Mon, 18 Sep 2023 08:24:19 -0500
parents 048284447643
children
comparison
equal deleted inserted replaced
5652:eade7ff9f52c 5653:62c6e17a5e9d
1 local mark_storage = module:open_store("firewall_marks"); 1 local mark_storage = module:open_store("firewall_marks");
2 local mark_map_storage = module:open_store("firewall_marks", "map");
2 3
3 local user_sessions = prosody.hosts[module.host].sessions; 4 local user_sessions = prosody.hosts[module.host].sessions;
4 5
5 module:hook("resource-bind", function (event) 6 module:hook("firewall/marked/user", function (event)
6 local session = event.session; 7 local user = user_sessions[event.username];
7 local username = session.username; 8 local marks = user and user.firewall_marks;
8 local user = user_sessions[username]; 9 if user and not marks then
9 local marks = user.firewall_marks; 10 -- Load marks from storage to cache on the user object
10 if not marks then 11 marks = mark_storage:get(event.username) or {};
11 marks = mark_storage:get(username) or {}; 12 user.firewall_marks = marks; --luacheck: ignore 122
12 user.firewall_marks = marks; -- luacheck: ignore 122
13 end 13 end
14 session.firewall_marks = marks; 14 if marks then
15 end); 15 marks[event.mark] = event.timestamp;
16 end
17 local ok, err = mark_map_storage:set(event.username, event.mark, event.timestamp);
18 if not ok then
19 module:log("error", "Failed to mark user %q with %q: %s", event.username, event.mark, err);
20 end
21 return true;
22 end, -1);
16 23
17 module:hook("resource-unbind", function (event) 24 module:hook("firewall/unmarked/user", function (event)
18 local session = event.session; 25 local user = user_sessions[event.username];
19 local username = session.username; 26 local marks = user and user.firewall_marks;
20 local marks = session.firewall_marks; 27 if marks then
21 mark_storage:set(username, marks); 28 marks[event.mark] = nil;
22 end); 29 end
23 30 local ok, err = mark_map_storage:set(event.username, event.mark, nil);
31 if not ok then
32 module:log("error", "Failed to unmark user %q with %q: %s", event.username, event.mark, err);
33 end
34 return true;
35 end, -1);