Mercurial > prosody-modules
comparison mod_firewall/marks.lib.lua @ 6550:f9d93d7cd6d5
mod_firewall: marks: Add commands for managing marks
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Sun, 24 May 2026 18:50:41 +0100 |
| parents | d2e50b587721 |
| children | 2d916ba3253c |
comparison
equal
deleted
inserted
replaced
| 6549:d2e50b587721 | 6550:f9d93d7cd6d5 |
|---|---|
| 43 if not ok then | 43 if not ok then |
| 44 module:log("error", "Failed to unmark user %q with %q: %s", event.username, event.mark, err); | 44 module:log("error", "Failed to unmark user %q with %q: %s", event.username, event.mark, err); |
| 45 end | 45 end |
| 46 return true; | 46 return true; |
| 47 end, -1); | 47 end, -1); |
| 48 | |
| 49 module:add_item("shell-command", { | |
| 50 section = "firewall"; | |
| 51 section_desc = "mod_firewall commands"; | |
| 52 name = "mark"; | |
| 53 desc = "Add a firewall mark to a user account"; | |
| 54 args = { | |
| 55 { name = "user_jid", type = "string" }; | |
| 56 { name = "mark_name", type = "string" }; | |
| 57 }; | |
| 58 host_selector = "user_jid"; | |
| 59 handler = function(self, user_jid, mark_name) --luacheck: ignore 212/self 212/host | |
| 60 local username, host = jid.split(user_jid); | |
| 61 if not username or not hosts[host] then | |
| 62 return nil, "Invalid JID supplied"; | |
| 63 elseif not idsafe(mark_name) then | |
| 64 return nil, "Invalid characters in mark name"; | |
| 65 end | |
| 66 if not module:fire_event("firewall/marked/user", { | |
| 67 username = session.username; | |
| 68 mark = mark_name; | |
| 69 timestamp = os.time(); | |
| 70 }) then | |
| 71 return nil, "Failed to set mark"; | |
| 72 end | |
| 73 return true, "Marked"; | |
| 74 end; | |
| 75 }); | |
| 76 | |
| 77 module:add_item("shell-command", { | |
| 78 section = "firewall"; | |
| 79 section_desc = "mod_firewall commands"; | |
| 80 name = "unmark"; | |
| 81 desc = "Remove a firewall mark from a user account"; | |
| 82 args = { | |
| 83 { name = "user_jid", type = "string" }; | |
| 84 { name = "mark_name", type = "string" }; | |
| 85 }; | |
| 86 host_selector = "user_jid"; | |
| 87 handler = function(self, user_jid, mark_name) --luacheck: ignore 212/self 212/host | |
| 88 local username, host = jid.split(user_jid); | |
| 89 if not username or not hosts[host] then | |
| 90 return nil, "Invalid JID supplied"; | |
| 91 elseif not idsafe(mark_name) then | |
| 92 return nil, "Invalid characters in mark name"; | |
| 93 end | |
| 94 if not module:fire_event("firewall/unmarked/user", { | |
| 95 username = session.username; | |
| 96 mark = mark_name; | |
| 97 timestamp = os.time(); | |
| 98 }) then | |
| 99 return nil, "Failed to remove mark"; | |
| 100 end | |
| 101 return true, "Unmarked"; | |
| 102 end; | |
| 103 }); | |
| 104 | |
| 105 module:add_item("shell-command", { | |
| 106 section = "firewall"; | |
| 107 section_desc = "mod_firewall commands"; | |
| 108 name = "marked"; | |
| 109 desc = "List all user accounts with a specific firewall mark"; | |
| 110 args = { | |
| 111 { name = "host", type = "string" }; | |
| 112 { name = "mark_name", type = "string" }; | |
| 113 }; | |
| 114 host_selector = "host"; | |
| 115 handler = function(self, host, mark_name) --luacheck: ignore 212/self 212/host | |
| 116 if not idsafe(mark_name) then | |
| 117 return nil, "Invalid characters in mark name"; | |
| 118 end | |
| 119 local users = mark_storage:get_key_from_all(mark_name); | |
| 120 local usernames = {}; | |
| 121 for username in it.keys(users) do | |
| 122 table.insert(usernames, username); | |
| 123 end | |
| 124 table.sort(usernames, function (a, b) | |
| 125 return users[a] < users[b]; | |
| 126 end); | |
| 127 for _, username in ipairs(usernames) do | |
| 128 self.session.print(username, os.date("%Y-%m-%d %R:%s", users[username]); | |
| 129 end | |
| 130 return true, ("Showing %d users"):format(#usernames); | |
| 131 end; | |
| 132 }); | |
| 133 |
