Comparison

mod_audit/mod_audit.lua @ 5326:dc058fcc3fe3

mod_audit: Improve filtering options and add documentation to README
author Matthew Wild <mwild1@gmail.com>
date Fri, 07 Apr 2023 13:44:18 +0100
parent 5325:11b37063d80a
child 5327:7e3862a26e55
comparison
equal deleted inserted replaced
5325:11b37063d80a 5326:dc058fcc3fe3
123 function moduleapi.audit(module, user, event_type, extra) 123 function moduleapi.audit(module, user, event_type, extra)
124 audit(module.host, user, "mod_" .. module:get_name(), event_type, extra); 124 audit(module.host, user, "mod_" .. module:get_name(), event_type, extra);
125 end 125 end
126 126
127 function module.command(_arg) 127 function module.command(_arg)
128 local arg = require "util.argparse".parse(_arg, { value_params = { "limit", "user" } }); 128 local jid = require "util.jid";
129 local host = arg[1]; 129 local arg = require "util.argparse".parse(_arg, { value_params = { "limit" } });
130 local query_user, host = jid.prepped_split(arg[1]);
130 if not host then 131 if not host then
131 print("EE: Please supply the host for which you want to show events"); 132 print("EE: Please supply the host for which you want to show events");
132 return 1; 133 return 1;
133 elseif not prosody.hosts[host] then 134 elseif not prosody.hosts[host] then
134 print("EE: Unknown host: "..host); 135 print("EE: Unknown host: "..host);
137 138
138 require "core.storagemanager".initialize_host(host); 139 require "core.storagemanager".initialize_host(host);
139 local store = stores[host]; 140 local store = stores[host];
140 local c = 0; 141 local c = 0;
141 142
143 if arg.global then
144 if query_user then
145 print("WW: Specifying a user account is incompatible with --global. Showing only global events.");
146 end
147 query_user = "@";
148 end
149
142 local results, err = store:find(nil, { 150 local results, err = store:find(nil, {
143 with = arg.user; 151 with = query_user;
144 limit = arg.limit and tonumber(arg.limit) or nil; 152 limit = arg.limit and tonumber(arg.limit) or nil;
145 reverse = true; 153 reverse = true;
146 }) 154 })
147 if not results then 155 if not results then
148 print("EE: Failed to query audit log: "..tostring(err)); 156 print("EE: Failed to query audit log: "..tostring(err));
153 { title = "Date", key = "when", width = 19, mapper = function (when) return os.date("%Y-%m-%d %R:%S", when); end }; 161 { title = "Date", key = "when", width = 19, mapper = function (when) return os.date("%Y-%m-%d %R:%S", when); end };
154 { title = "Source", key = "source", width = "2p" }; 162 { title = "Source", key = "source", width = "2p" };
155 { title = "Event", key = "event_type", width = "2p" }; 163 { title = "Event", key = "event_type", width = "2p" };
156 }; 164 };
157 165
158 if arg.show_user ~= false and (not arg.global or arg.show_user) then 166 if arg.show_user ~= false and (not arg.global and not query_user) or arg.show_user then
159 table.insert(colspec, { 167 table.insert(colspec, {
160 title = "User", key = "username", width = "2p", 168 title = "User", key = "username", width = "2p",
161 mapper = function (user) 169 mapper = function (user)
162 if user == "@" then return ""; end 170 if user == "@" then return ""; end
163 if user:sub(-#host-1, -1) == ("@"..host) then 171 if user:sub(-#host-1, -1) == ("@"..host) then
164 return (user:gsub("@.+$", "")); 172 return (user:gsub("@.+$", ""));
165 end 173 end
166 end; 174 end;
167 }); 175 });
168 end 176 end
169 if arg.show_ip ~= false and (not arg.global and attach_ips or arg.show_ip) then 177 if arg.show_ip ~= false and (not arg.global and attach_ips) or arg.show_ip then
170 table.insert(colspec, { 178 table.insert(colspec, {
171 title = "IP", key = "ip", width = "2p"; 179 title = "IP", key = "ip", width = "2p";
172 }); 180 });
173 end 181 end
174 if arg.show_location ~= false and (not arg.global and attach_location or arg.show_location) then 182 if arg.show_location ~= false and (not arg.global and attach_location) or arg.show_location then
175 table.insert(colspec, { 183 table.insert(colspec, {
176 title = "Location", key = "country", width = 2; 184 title = "Location", key = "country", width = 2;
177 }); 185 });
178 end 186 end
179 187
181 189
182 print(string.rep("-", width)); 190 print(string.rep("-", width));
183 print(row()); 191 print(row());
184 print(string.rep("-", width)); 192 print(string.rep("-", width));
185 for _, entry, when, user in results do 193 for _, entry, when, user in results do
186 c = c + 1; 194 if arg.global ~= false or user ~= "@" then
187 print(row({ 195 c = c + 1;
188 when = when; 196 print(row({
189 source = entry.attr.source; 197 when = when;
190 event_type = entry.attr.type:gsub("%-", " "); 198 source = entry.attr.source;
191 username = user; 199 event_type = entry.attr.type:gsub("%-", " ");
192 ip = entry:get_child_text("remote-ip"); 200 username = user;
193 location = entry:find("location@country"); 201 ip = entry:get_child_text("remote-ip");
194 })); 202 location = entry:find("location@country");
203 }));
204 end
195 end 205 end
196 print(string.rep("-", width)); 206 print(string.rep("-", width));
197 print(("%d records displayed"):format(c)); 207 print(("%d records displayed"):format(c));
198 end 208 end