Comparison

mod_flags/mod_flags.lua @ 6135:c42419d73737

mod_flags: trunk version backported to 0.12
author Matthew Wild <mwild1@gmail.com>
date Sat, 04 Jan 2025 22:50:49 +0000
parent 6132:ffec70ddbffc
comparison
equal deleted inserted replaced
6134:00b55c7ef393 6135:c42419d73737
1 -- This module is only for 0.12, later versions have mod_flags bundled
2 --% conflicts: mod_flags
3
4 local flags_map;
5 if prosody.process_type ~= "prosodyctl" then
6 flags_map = module:open_store("account_flags", "map");
7 end
8
9 -- API
10
11 function add_flag(username, flag, comment) -- luacheck: ignore 131/add_flag
12 local flag_data = {
13 when = os.time();
14 comment = comment;
15 };
16
17 local ok, err = flags_map:set(username, flag, flag_data);
18 if not ok then
19 return nil, err;
20 end
21
22 module:fire_event("user-flag-added/"..flag, {
23 user = username;
24 flag = flag;
25 data = flag_data;
26 });
27
28 return true;
29 end
30
31 function remove_flag(username, flag) -- luacheck: ignore 131/remove_flag
32 local ok, err = flags_map:set(username, flag, nil);
33 if not ok then
34 return nil, err;
35 end
36
37 module:fire_event("user-flag-removed/"..flag, {
38 user = username;
39 flag = flag;
40 });
41
42 return true;
43 end
44
45 function has_flag(username, flag) -- luacheck: ignore 131/has_flag
46 local ok, err = flags_map:get(username, flag);
47 if not ok and err then
48 error("Failed to check flags for user: "..err);
49 end
50 return not not ok;
51 end
52
53 function get_flag_info(username, flag) -- luacheck: ignore 131/get_flag_info
54 return flags_map:get(username, flag);
55 end
56
57
58 -- Migration from mod_firewall marks
59
60 local function migrate_marks(host)
61 local usermanager = require "core.usermanager";
62
63 local flag_storage = module:open_store("account_flags");
64 local mark_storage = module:open_store("firewall_marks");
65
66 local migration_comment = "Migrated from mod_firewall marks at "..os.date("%Y-%m-%d %R");
67
68 local migrated, empty, errors = 0, 0, 0;
69 for username in usermanager.users(host) do
70 local marks, err = mark_storage:get(username);
71 if marks then
72 local flags = {};
73 for mark_name, mark_timestamp in pairs(marks) do
74 flags[mark_name] = {
75 when = mark_timestamp;
76 comment = migration_comment;
77 };
78 end
79 local saved_ok, saved_err = flag_storage:set(username, flags);
80 if saved_ok then
81 prosody.log("error", "Failed to save flags for %s: %s", username, saved_err);
82 migrated = migrated + 1;
83 else
84 errors = errors + 1;
85 end
86 elseif err then
87 prosody.log("error", "Failed to load marks for %s: %s", username, err);
88 errors = errors + 1;
89 else
90 empty = empty + 1;
91 end
92 end
93
94 print(("Finished - %d migrated, %d users with no marks, %d errors"):format(migrated, empty, errors));
95 end
96
97 function module.command(arg)
98 local storagemanager = require "core.storagemanager";
99 local usermanager = require "core.usermanager";
100 local jid = require "util.jid";
101 local warn = require"util.prosodyctl".show_warning;
102
103 local command = arg[1];
104 if not command then
105 warn("Valid subcommands: migrate_marks");
106 return 0;
107 end
108 table.remove(arg, 1);
109
110 local node, host = jid.prepped_split(arg[1]);
111 if not host then
112 warn("Please specify a host or JID after the command");
113 return 1;
114 elseif not prosody.hosts[host] then
115 warn("Unknown host: "..host);
116 return 1;
117 end
118
119 table.remove(arg, 1);
120
121 module.host = host; -- luacheck: ignore 122
122 storagemanager.initialize_host(host);
123 usermanager.initialize_host(host);
124
125 flags_map = module:open_store("account_flags", "map");
126
127 if command == "migrate_marks" then
128 migrate_marks(host);
129 return 0;
130 elseif command == "find" then
131 local flag = assert(arg[1], "expected argument: flag");
132 local flags = module:open_store("account_flags", "map");
133 local users_with_flag = flags:get_all(flag);
134
135 local c = 0;
136 for user, flag_data in pairs(users_with_flag) do
137 print(user, os.date("%Y-%m-%d %R", flag_data.when), flag_data.comment or "");
138 c = c + 1;
139 end
140
141 print(("%d accounts listed"):format(c));
142 return 1;
143 elseif command == "add" then
144 local username = assert(node, "expected a user JID, got "..host);
145 local flag = assert(arg[1], "expected argument: flag");
146 local comment = arg[2];
147
148 local ok, err = add_flag(username, flag, comment);
149 if not ok then
150 print("Failed to add flag: "..err);
151 return 1;
152 end
153
154 print("Flag added");
155 return 1;
156 elseif command == "remove" then
157 local username = assert(node, "expected a user JID, got "..host);
158 local flag = assert(arg[1], "expected argument: flag");
159
160 local ok, err = remove_flag(username, flag);
161 if not ok then
162 print("Failed to remove flag: "..err);
163 return 1;
164 end
165
166 print("Flag removed");
167 return 1;
168 elseif command == "list" then
169 local username = assert(node, "expected a user JID, got "..host);
170
171 local c = 0;
172
173 local flags = module:open_store("account_flags");
174 local user_flags, err = flags:get(username);
175
176 if not user_flags and err then
177 print("Unable to list flags: "..err);
178 return 1;
179 end
180
181 if user_flags then
182 for flag_name, flag_data in pairs(user_flags) do
183 print(flag_name, os.date("%Y-%m-%d %R", flag_data.when), flag_data.comment or "");
184 c = c + 1;
185 end
186 end
187
188 print(("%d flags listed"):format(c));
189 return 0;
190 else
191 warn("Unknown command: %s", command);
192 return 1;
193 end
194 end