Annotate

plugins/mod_flags.lua @ 13652:a08065207ef0

net.server_epoll: Call :shutdown() on TLS sockets when supported Comment from Matthew: This fixes a potential issue where the Prosody process gets blocked on sockets waiting for them to close. Unlike non-TLS sockets, closing a TLS socket sends layer 7 data, and this can cause problems for sockets which are in the process of being cleaned up. This depends on LuaSec changes which are not yet upstream. From Martijn's original email: So first my analysis of luasec. in ssl.c the socket is put into blocking mode right before calling SSL_shutdown() inside meth_destroy(). My best guess to why this is is because meth_destroy is linked to the __close and __gc methods, which can't exactly be called multiple times and luasec does want to make sure that a tls session is shutdown as clean as possible. I can't say I disagree with this reasoning and don't want to change this behaviour. My solution to this without changing the current behaviour is to introduce a shutdown() method. I am aware that this overlaps in a conflicting way with tcp's shutdown method, but it stays close to the OpenSSL name. This method calls SSL_shutdown() in the current (non)blocking mode of the underlying socket and returns a boolean whether or not the shutdown is completed (matching SSL_shutdown()'s 0 or 1 return values), and returns the familiar ssl_ioerror() strings on error with a false for completion. This error can then be used to determine if we have wantread/wantwrite to finalize things. Once meth_shutdown() has been called once a shutdown flag will be set, which indicates to meth_destroy() that the SSL_shutdown() has been handled by the application and it shouldn't be needed to set the socket to blocking mode. I've left the SSL_shutdown() call in the LSEC_STATE_CONNECTED to prevent TOCTOU if the application reaches a timeout for the shutdown code, which might allow SSL_shutdown() to clean up anyway at the last possible moment. Another thing I've changed to luasec is the call to socket_setblocking() right before calling close(2) in socket_destroy() in usocket.c. According to the latest POSIX[0]: Note that the requirement for close() on a socket to block for up to the current linger interval is not conditional on the O_NONBLOCK setting. Which I read to mean that removing O_NONBLOCK on the socket before close doesn't impact the behaviour and only causes noise in system call tracers. I didn't touch the windows bits of this, since I don't do windows. For the prosody side of things I've made the TLS shutdown bits resemble interface:onwritable(), and put it under a combined guard of self._tls and self.conn.shutdown. The self._tls bit is there to prevent getting stuck on this condition, and self.conn.shutdown is there to prevent the code being called by instances where the patched luasec isn't deployed. The destroy() method can be called from various places and is read by me as the "we give up" error path. To accommodate for these unexpected entrypoints I've added a single call to self.conn:shutdown() to prevent the socket being put into blocking mode. I have no expectations that there is any other use here. Same as previous, the self.conn.shutdown check is there to make sure it's not called on unpatched luasec deployments and self._tls is there to make sure we don't call shutdown() on tcp sockets. I wouldn't recommend logging of the conn:shutdown() error inside close(), since a lot of clients simply close the connection before SSL_shutdown() is done.
author Martijn van Duren <martijn@openbsd.org>
date Thu, 06 Feb 2025 15:04:38 +0000
parent 13583:e77ef9a4604f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
13583
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 local jid_node = require "prosody.util.jid".node;
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 local flags = module:open_store("account_flags", "keyval+");
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 -- API
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 function add_flag(username, flag, comment)
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 local flag_data = {
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 when = os.time();
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 comment = comment;
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 };
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 local ok, err = flags:set_key(username, flag, flag_data);
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 if not ok then
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 return nil, err;
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 end
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 module:fire_event("user-flag-added/"..flag, {
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 user = username;
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 flag = flag;
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 data = flag_data;
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 });
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 return true;
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 end
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 function remove_flag(username, flag)
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 local ok, err = flags:set_key(username, flag, nil);
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 if not ok then
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 return nil, err;
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 end
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 module:fire_event("user-flag-removed/"..flag, {
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 user = username;
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 flag = flag;
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 });
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 return true;
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 end
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 function has_flag(username, flag) -- luacheck: ignore 131/has_flag
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 local ok, err = flags:get_key(username, flag);
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 if not ok and err then
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 error("Failed to check flags for user: "..err);
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 end
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 return not not ok;
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 end
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 function get_flag_info(username, flag) -- luacheck: ignore 131/get_flag_info
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 return flags:get_key(username, flag);
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 end
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 -- Shell commands
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 local function get_username(jid)
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 return (assert(jid_node(jid), "please supply a valid user JID"));
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 end
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 module:add_item("shell-command", {
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 section = "flags";
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 section_desc = "View and manage flags on user accounts";
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 name = "list";
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 desc = "List flags for the given user account";
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 args = {
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 { name = "jid", type = "string" };
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 };
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 host_selector = "jid";
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 handler = function(self, jid) --luacheck: ignore 212/self
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 local c = 0;
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 local user_flags, err = flags:get(get_username(jid));
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 if not user_flags and err then
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 return false, "Unable to list flags: "..err;
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 end
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 if user_flags then
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 local print = self.session.print;
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 for flag_name, flag_data in pairs(user_flags) do
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 print(flag_name, os.date("%Y-%m-%d %R", flag_data.when), flag_data.comment);
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 c = c + 1;
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 end
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 end
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 return true, ("%d flags listed"):format(c);
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 end;
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 });
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 module:add_item("shell-command", {
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 section = "flags";
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 section_desc = "View and manage flags on user accounts";
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 name = "add";
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 desc = "Add a flag to the given user account, with optional comment";
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 args = {
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 { name = "jid", type = "string" };
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 { name = "flag", type = "string" };
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 { name = "comment", type = "string" };
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 };
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 host_selector = "jid";
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 handler = function(self, jid, flag, comment) --luacheck: ignore 212/self
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 local username = get_username(jid);
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 local ok, err = add_flag(username, flag, comment);
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 if not ok then
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 return false, "Failed to add flag: "..err;
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 end
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 return true, "Flag added";
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 end;
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 });
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 module:add_item("shell-command", {
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 section = "flags";
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 section_desc = "View and manage flags on user accounts";
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 name = "remove";
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117 desc = "Remove a flag from the given user account";
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 args = {
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 { name = "jid", type = "string" };
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 { name = "flag", type = "string" };
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 };
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 host_selector = "jid";
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 handler = function(self, jid, flag) --luacheck: ignore 212/self
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 local username = get_username(jid);
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 local ok, err = remove_flag(username, flag);
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 if not ok then
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128 return false, "Failed to remove flag: "..err;
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129 end
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131 return true, "Flag removed";
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132 end;
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 });
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135 module:add_item("shell-command", {
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136 section = "flags";
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137 section_desc = "View and manage flags on user accounts";
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138 name = "find";
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139 desc = "Find all user accounts with a given flag on the specified host";
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140 args = {
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141 { name = "host", type = "string" };
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142 { name = "flag", type = "string" };
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143 };
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
144 host_selector = "host";
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145 handler = function(self, host, flag) --luacheck: ignore 212/self 212/host
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146 local users_with_flag = flags:get_key_from_all(flag);
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
147
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
148 local print = self.session.print;
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
149 local c = 0;
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150 for user, flag_data in pairs(users_with_flag) do
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
151 print(user, os.date("%Y-%m-%d %R", flag_data.when), flag_data.comment);
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
152 c = c + 1;
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
153 end
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
154
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
155 return true, ("%d accounts listed"):format(c);
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
156 end;
e77ef9a4604f mod_flags: New module to view and manage flags on user accounts via shell/API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
157 });