Comparison

plugins/mod_flags.lua @ 13583:e77ef9a4604f

mod_flags: New module to view and manage flags on user accounts via shell/API This will be useful for server operators to easily identify flagged accounts, etc.
author Matthew Wild <mwild1@gmail.com>
date Fri, 03 Jan 2025 11:45:48 +0000
comparison
equal deleted inserted replaced
13582:67c9fc643873 13583:e77ef9a4604f
1 local jid_node = require "prosody.util.jid".node;
2
3 local flags = module:open_store("account_flags", "keyval+");
4
5 -- API
6
7 function add_flag(username, flag, comment)
8 local flag_data = {
9 when = os.time();
10 comment = comment;
11 };
12
13 local ok, err = flags:set_key(username, flag, flag_data);
14 if not ok then
15 return nil, err;
16 end
17
18 module:fire_event("user-flag-added/"..flag, {
19 user = username;
20 flag = flag;
21 data = flag_data;
22 });
23
24 return true;
25 end
26
27 function remove_flag(username, flag)
28 local ok, err = flags:set_key(username, flag, nil);
29 if not ok then
30 return nil, err;
31 end
32
33 module:fire_event("user-flag-removed/"..flag, {
34 user = username;
35 flag = flag;
36 });
37
38 return true;
39 end
40
41 function has_flag(username, flag) -- luacheck: ignore 131/has_flag
42 local ok, err = flags:get_key(username, flag);
43 if not ok and err then
44 error("Failed to check flags for user: "..err);
45 end
46 return not not ok;
47 end
48
49 function get_flag_info(username, flag) -- luacheck: ignore 131/get_flag_info
50 return flags:get_key(username, flag);
51 end
52
53 -- Shell commands
54
55 local function get_username(jid)
56 return (assert(jid_node(jid), "please supply a valid user JID"));
57 end
58
59 module:add_item("shell-command", {
60 section = "flags";
61 section_desc = "View and manage flags on user accounts";
62 name = "list";
63 desc = "List flags for the given user account";
64 args = {
65 { name = "jid", type = "string" };
66 };
67 host_selector = "jid";
68 handler = function(self, jid) --luacheck: ignore 212/self
69 local c = 0;
70
71 local user_flags, err = flags:get(get_username(jid));
72
73 if not user_flags and err then
74 return false, "Unable to list flags: "..err;
75 end
76
77 if user_flags then
78 local print = self.session.print;
79
80 for flag_name, flag_data in pairs(user_flags) do
81 print(flag_name, os.date("%Y-%m-%d %R", flag_data.when), flag_data.comment);
82 c = c + 1;
83 end
84 end
85
86 return true, ("%d flags listed"):format(c);
87 end;
88 });
89
90 module:add_item("shell-command", {
91 section = "flags";
92 section_desc = "View and manage flags on user accounts";
93 name = "add";
94 desc = "Add a flag to the given user account, with optional comment";
95 args = {
96 { name = "jid", type = "string" };
97 { name = "flag", type = "string" };
98 { name = "comment", type = "string" };
99 };
100 host_selector = "jid";
101 handler = function(self, jid, flag, comment) --luacheck: ignore 212/self
102 local username = get_username(jid);
103
104 local ok, err = add_flag(username, flag, comment);
105 if not ok then
106 return false, "Failed to add flag: "..err;
107 end
108
109 return true, "Flag added";
110 end;
111 });
112
113 module:add_item("shell-command", {
114 section = "flags";
115 section_desc = "View and manage flags on user accounts";
116 name = "remove";
117 desc = "Remove a flag from the given user account";
118 args = {
119 { name = "jid", type = "string" };
120 { name = "flag", type = "string" };
121 };
122 host_selector = "jid";
123 handler = function(self, jid, flag) --luacheck: ignore 212/self
124 local username = get_username(jid);
125
126 local ok, err = remove_flag(username, flag);
127 if not ok then
128 return false, "Failed to remove flag: "..err;
129 end
130
131 return true, "Flag removed";
132 end;
133 });
134
135 module:add_item("shell-command", {
136 section = "flags";
137 section_desc = "View and manage flags on user accounts";
138 name = "find";
139 desc = "Find all user accounts with a given flag on the specified host";
140 args = {
141 { name = "host", type = "string" };
142 { name = "flag", type = "string" };
143 };
144 host_selector = "host";
145 handler = function(self, host, flag) --luacheck: ignore 212/self 212/host
146 local users_with_flag = flags:get_key_from_all(flag);
147
148 local print = self.session.print;
149 local c = 0;
150 for user, flag_data in pairs(users_with_flag) do
151 print(user, os.date("%Y-%m-%d %R", flag_data.when), flag_data.comment);
152 c = c + 1;
153 end
154
155 return true, ("%d accounts listed"):format(c);
156 end;
157 });