bin/anyevent-mcp

filter: Statementstmt 95.2% 80 / 84 Branchbran 82.8% 53 / 64 Conditioncond 69.7% 30 / 43 MC/DCmcdc 37.5% 9 / 24 Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 80.6% 179 / 222 CC 27 SCAR 31.4
CC
27
Cov
85%
change_user
27.9
BEGIN
1.0
BEGIN
1.0
CRAP 23.0
« bin/mcp-vim ↑ Summary lib/AnyEvent/MCP/IMAP/OAuth2.pm »
1 #!/opt/bin/perl
2  
3 =head1 NAME
4  
5 anyevent-mcp - initialize MCP configuration and manage users and permissions
6  
7 =head1 SYNOPSIS
8  
9 anyevent-mcp init
10 anyevent-mcp manage-rules fs-default
11 anyevent-mcp create-user chatgpt
12 anyevent-mcp revoke-user chatgpt
13 anyevent-mcp edit-rules-file /path/to/rules.ini
14  
15 =head1 DESCRIPTION
16  
17 C<anyevent-mcp> sets up the shared AnyEvent::MCP configuration and manages
18 permission rules saved by its servers. Use it to review permissions you have
19 allowed or denied permanently, remove a rule, or change the order in which
20 rules are checked.
21  
22 C<manage-rules> opens a graphical rule manager; C<edit-rules> opens the
23 rule file in your text editor. Both take a rule profile name. For example,
24 C<anyevent-mcp manage-rules fs-default> manages the default filesystem
25 server's rules. The corresponding C<*-file> commands accept a pathname
26 when an application stores its rules elsewhere.
27  
28 C<init> creates the private global configuration and its master secret,
29 from which servers can derive their connection credentials. You normally
30 do not need to run it: a server creates a missing secret when it loads its
31 configuration. Use C<init> when you want to create the configuration
32 explicitly; it refuses to overwrite an existing file.
33  
34 See L<AnyEvent::MCP::Intro> for setting up and connecting to the servers,
35 L<AnyEvent::MCP/GLOBAL CONFIGURATION> for the configuration file format,
36 and L<AnyEvent::MCP::Server::Permissions/RULE FILES> for permission rules.
37  
38 =head1 COMMANDS
39  
40 =over
41  
42 =item init
43  
44 Create the private global configuration and master secret. Unlike ordinary
45 configuration loading, this fails rather than overwriting an existing file.
46 C<--config FILE> selects a different configuration file for this command.
47  
48 =item manage-rules NAME
49  
50 Open the YAD rule deletion and reordering interface for the named profile.
51 For a filesystem server started with C<--name code>, use profile C<fs-code>.
52  
53 =item edit-rules NAME
54  
55 Open the named profile's rule file in a text editor.
56  
57 =item manage-rules-file $file
58  
59 Open the rule manager for the explicit pathname C<$file>.
60  
61 =item edit-rules-file $file
62  
63 Open C<$file> in a text editor. Use these explicit-file commands for
64 applications whose rule files do not use the named-profile convention.
65  
66 =item create-user USER
67  
68 Enable a new named user; rotate a revoked user at the next generation.
69 Use --program or --instance for a scoped override.
70  
71 =item revoke-user USER
72  
73 Disable a user, preserving the generation for later rotation. In a narrower
74 scope, a negative definition also blocks inherited credentials. Implicit
75 global:default is revocable. Restart affected servers after changes; these
76 commands do not print secrets. Use server --print-url[=USER] for URLs.
77  
78 =back
79  
80 Named profiles use
81 C<anyevent-mcp/rules/NAME.ini> in the user configuration directory.
82 Editing uses C<$VISUAL>, C<$EDITOR>, C<sensible-editor> or C<vi>.
83 For server URLs, use each server's C<--print-url> option.
84  
85 =head1 OPTIONS
86  
87 =over
88  
89 =item --config FILE
90  
91 Use FILE instead of default global configuration
92 (C<$XDG_CONFIG_HOME/anyevent-mcp/global.ini>, or
93 C<~/.config/anyevent-mcp/global.ini>). Relevant to C<init> and user
94 management; rule management uses its own profile or pathname.
95  
96 =item --program NAME
97  
98 Select C<[program NAME]> for user management.
99  
100 =item --instance NAME
101  
102 Select C<[instance NAME]> for user management. Cannot combine with --program.
103  
104 =item --help
105  
106 Print command synopsis, available commands and configuration defaults,
107 then exit without running a command.
108  
109 =back
110  
111 =cut
112  
113 28 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
114  
115 28 ▶use Getopt::Long ();
Subroutine BEGIN: called CC=1 SCAR=0.0
116  
117 28 ▶use AnyEvent::MCP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
118 28 ▶use AnyEvent::MCP::Config ();
Subroutine BEGIN: called CC=1 SCAR=0.0
119 28 ▶use AnyEvent::MCP::File ();
Subroutine BEGIN: called CC=1 SCAR=0.0
120  
121 2 ▶sub usage {
Subroutine usage: called CC=1 SCAR=0.0
122 2 print <<'EOF';
123 Usage: anyevent-mcp [OPTIONS] COMMAND [ARGUMENTS]
124  
125 manage-rules NAME delete/reorder rules in YAD
126 edit-rules NAME edit rules with VISUAL, EDITOR, sensible-editor, vi
127 manage-rules-file FILE manage an explicit rules file
128 edit-rules-file FILE edit an explicit rules file
129 init create a private configuration and master secret
130 create-user USER enable or rotate a managed user
131 revoke-user USER revoke a managed user
132  
133 Options (before COMMAND):
134 --config FILE use a different global configuration file
135 --program NAME select [program NAME] for user management
136 --instance NAME select [instance NAME] for user management
137 --help show this help
138  
139 User definitions inherit from [global] through [program NAME] to
140 [instance NAME]; narrower scopes override wider ones. Without --program
141 or --instance, user commands modify [global]. Revoked users remain
142 disabled until recreated with a new token generation. The implicit
143 global:default user is enabled initially.
144  
145 Examples:
146 anyevent-mcp create-user chatgpt
147 anyevent-mcp --program mcp-fs create-user backup
148 anyevent-mcp --instance private revoke-user alice
149 anyevent-mcp revoke-user chatgpt
150 mcp-multibroker --print-urls # list all enabled user URLs
151 mcp-multibroker --print-url=chatgpt # show just chatgpt's URL
152  
153 Server changes take effect after restart. The selected connect_user
154 (default: default) authenticates auxiliary browser/Vim/reverse connections.
155  
156 Rule profiles are stored under $XDG_CONFIG_HOME/anyevent-mcp, falling back
157 to ~/.config/anyevent-mcp/rules (e.g. manage-rules fs-default).
158  
159 Configuration defaults to ~/.config/anyevent-mcp/global.ini, or the
160 corresponding location under XDG_CONFIG_HOME. Use --help for this text.
161  
162 A missing master secret is generated and saved automatically when a server
163 loads its configuration. Configuration format: see AnyEvent::MCP.
164 EOF
165 }
166  
167 10 ▶sub change_user {
Subroutine change_user: called CC=27 SCAR=33.3
168 10 my ($file, $section, $user, $command) = @_;
169  
170 10 my $lock = AnyEvent::MCP::File::lock $file;
171 10 ▶ open my $fh, "<:raw", $file or die "$file: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $file or die "${file}: $!\n" 10 0 10
172 10 local $/;
173 10 my $body = <$fh>;
174 10 ▶ close $fh or die "$file: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "${file}: $!\n" 10 0 10
175  
176 10 my @lines = split /(?<=\n)/, $body;
177 10 my ($current, $found, $position, $value);
178 10 my $key = "user.$user";
179  
180 10 for my $i (0 .. $#lines) {
181 50 ▶ if ($lines[$i] =~ /^\s*\[([^\]\r\n]+)\]\s*(?:[;#][^\r\n]*)?\r?\n?\z/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($lines[$i] =~ /^\s*\[([^\]\r\n]+)\]\s*(?:[;#][^\r\n]*)?\r?\n?\z/u) { } 17 33 50
elsif ($current eq $section and $lines[$i] =~ /^\s*\Q$key\E\s*=\s*([^\r\n;#]*)(?:[;#][^\r\n]*)?\r?\n?\z/u) { } 5 45 50
182 17 $current = $1;
183 17 ▶ $position = $i + 1 if $current eq $section;
Branch: 1 branch
BranchTrueFalseTotal
if $current eq $section 8 9 17
184 ▶ } elsif ($current eq $section && $lines[$i] =~ /^\s*\Q$key\E\s*=\s*([^\r\n;#]*)(?:[;#][^\r\n]*)?\r?\n?\z/) {
Condition100%3/3
$current eq $section and $lines[$i] =~ /^\s*\Q$key\E\s*=\s*([^\r\n;#]*)(?:[;#][^\r\n]*)?\r?\n?\z/u
!ll&&!rl&&r
17115
MC/DC100%2/2
$current eq $section and $lines[$i] =~ /^\s*\Q$key\E\s*=\s*([^\r\n;#]*)(?:[;#][^\r\n]*)?\r?\n?\z/u
$current eq $section $lines[$i] =~ /^\s*\Q$key\E\s*=\s*([^\r\n;#]*)(?:[;#][^\r\n]*)?\r?\n?\z/u
Truth table100%3/3
A and B
A= $current eq $sectionB= $lines[$i] =~ /^\s*\Q$key\E\s*=\s*([^\r\n;#]*)(?:[;#][^\r\n]*)?\r?\n?\z/u
ABresult
0X0
100
111
185 5 ▶ die "Duplicate $key in [$section]\n" if defined $found;
Branch: 1 branch
BranchTrueFalseTotal
if defined $found 0 5 5
186 5 ($found, $value) = ($i, $1);
187 5 $value =~ s/\s+\z//;
188 }
189 }
190  
191 10 my $generation;
192 10 ▶ if (defined $found) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $found) 5 5 10
193 5 ▶ $value =~ /\A-?[1-9][0-9]*\z/ && length $value < 18
Branch: 1 branch
BranchTrueFalseTotal
$value =~ /\A-?[1-9][0-9]*\z/u and length $value < 18 or die "Invalid $key generation '${value}'\n" 5 0 5
Condition33%1/3
$value =~ /\A-?[1-9][0-9]*\z/u and length $value < 18
!ll&&!rl&&r
005
MC/DC0%0/2
$value =~ /\A-?[1-9][0-9]*\z/u and length $value < 18
$value =~ /\A-?[1-9][0-9]*\z/u length $value < 18
Truth table33%1/3
A and B
A= $value =~ /\A-?[1-9][0-9]*\z/uB= length $value < 18
ABresult
0X0
100
111
194 or die "Invalid $key generation '$value'\n";
195 5 $generation = 0 + $value;
196 }
197  
198 10 ▶ if ($command eq "create-user") {
Branch: 1 branch
BranchTrueFalseTotal
if ($command eq 'create-user') { } 7 3 10
199 7 ▶ die "$key already enabled in [$section]\n"
Branch: 1 branch
BranchTrueFalseTotal
if defined $generation and $generation > 0 or not defined $generation and $section eq "global" and $user eq "default" 2 5 7
Condition100%12/12
defined $generation and $generation > 0 or not defined $generation and $section eq "global" and $user eq "default"
l!l&&r!l&&!r
115
defined $generation and $generation > 0
!ll&&!rl&&r
331
not defined $generation and $section eq "global" and $user eq "default"
!ll&&!rl&&r
411
not defined $generation and $section eq "global"
!ll&&!rl&&r
312
MC/DC80%4/5
defined $generation and $generation > 0 or not defined $generation and $section eq "global" and $user eq "default"
defined $generation $generation > 0 not defined $generation $section eq "global" $user eq "default"
Truth table55%5/9
A and B or not(C) and D and E
A= defined $generationB= $generation > 0C= not defined $generationD= $section eq "global"E= $user eq "default"
ABCDEresult
11XXX1
0X1111
101111
0X0XX0
0X10X0
0X1100
100XX0
1010X0
101100
200 if defined $generation && $generation > 0 || !defined $generation && $section eq "global" && $user eq "default";
201 5 ▶ $generation = defined $generation ? -$generation + 1 : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $generation ? : 3 2 5
202 } else {
203 3 ▶ die "$key already revoked in [$section]\n" if defined $generation && $generation < 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $generation and $generation < 0 0 3 3
Condition66%2/3
defined $generation and $generation < 0
!ll&&!rl&&r
210
MC/DC0%0/2
defined $generation and $generation < 0
defined $generation $generation < 0
Truth table66%2/3
A and B
A= defined $generationB= $generation < 0
ABresult
0X0
100
111
204 3 ▶ if (!defined $generation) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $generation) { } 2 1 3
205 2 ▶ ($section ne "global" || $user eq "default")
Branch: 1 branch
BranchTrueFalseTotal
$section ne "global" or $user eq "default" or die "$key is not enabled\n" 2 0 2
Condition66%2/3
$section ne "global" or $user eq "default"
l!l&&r!l&&!r
110
MC/DC0%0/2
$section ne "global" or $user eq "default"
$section ne "global" $user eq "default"
Truth table66%2/3
A or B
A= $section ne "global"B= $user eq "default"
ABresult
1X1
011
000
206 or die "$key is not enabled\n";
207 2 $generation = -1;
208 } else {
209 1 $generation = -$generation;
210 }
211 }
212  
213 8 ▶ if (defined $found) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $found) { } 4 4 8
elsif (defined $position) { } 2 2 4
214 4 $lines[$found] = "$key = $generation\n";
215 } elsif (defined $position) {
216 2 splice @lines, $position, 0, "$key = $generation\n";
217 } else {
218 2 push @lines, "\n[$section]\n$key = $generation\n";
219 }
220 8 AnyEvent::MCP::File::replace $file, join "", @lines;
221 8 close $lock;
222 8 ▶ print "$section: $user generation " . abs ($generation)
Branch: 1 branch
BranchTrueFalseTotal
$generation > 0 ? : 5 3 8
223 . ($generation > 0 ? " enabled\n" : " revoked\n");
224 }
225  
226 28 Getopt::Long::Configure ("no_auto_abbrev", "no_ignore_case", "require_order");
227 28 ▶Getopt::Long::GetOptions ("config=s" => \my $file, "program=s" => \my $program,
Branch: 1 branch
BranchTrueFalseTotal
&Getopt::Long::GetOptions("config=s", \my $file, "program=s", \my $program, "instance=s", \my $instance, "help", \my $help) or exit 2 28 0 28
228 "instance=s" => \my $instance, "help" => \my $help)
229 or exit 2;
230  
231 28 ▶if ($help) {
Branch: 1 branch
BranchTrueFalseTotal
if ($help) 2 26 28
232 2 usage;
233 2 exit 0;
234 }
235  
236 26 my $command = shift @ARGV;
237 26 ▶unless (defined $command) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $command) 0 26 26
238 0 usage;
239 0 exit 2;
240 }
241  
242 26 @ARGV = map AnyEvent::MCP::utf8dec $_, @ARGV;
243  
244 eval {
245 26 ▶ if ($command =~ /\A(?:manage-rules|edit-rules)(?:-file)?\z/ && @ARGV == 1) {
Branch: 3 branches
BranchTrueFalseTotal
if ($command =~ /\A(?:manage-rules|edit-rules)(?:-file)?\z/u and @ARGV == 1) { } 10 16 26
elsif ($command eq 'init' and not @ARGV) { } 1 15 16
elsif ($command =~ /\A(?:create-user|revoke-user)\z/u and @ARGV == 1) { } 10 5 15
Condition66%2/3
$command =~ /\A(?:manage-rules|edit-rules)(?:-file)?\z/u and @ARGV == 1
!ll&&!rl&&r
16010
MC/DC50%1/2
$command =~ /\A(?:manage-rules|edit-rules)(?:-file)?\z/u and @ARGV == 1
$command =~ /\A(?:manage-rules|edit-rules)(?:-file)?\z/u @ARGV == 1
Truth table66%2/3
A and B
A= $command =~ /\A(?:manage-rules|edit-rules)(?:-file)?\z/uB= @ARGV == 1
ABresult
0X0
100
111
246 10 my $path = $ARGV[0];
247 10 ▶ if ($command !~ /-file\z/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($command =~ /-file\z/u) 8 2 10
248 8 ▶ $path =~ /\A[A-Za-z0-9][A-Za-z0-9_.-]*\z/
Branch: 1 branch
BranchTrueFalseTotal
$path =~ /\A[A-Za-z0-9][A-Za-z0-9_.-]*\z/u or die "Invalid permission rule profile name\n" 7 1 8
249 or die "Invalid permission rule profile name\n";
250 7 ▶ my $config_home = $ENV{XDG_CONFIG_HOME} || ($ENV{HOME} || die "HOME is not set\n") . "/.config";
Condition25%1/4
$ENV{'XDG_CONFIG_HOME'} || ($ENV{'HOME'} || die("HOME is not set\n")) . '/.config'
l!l
70
$ENV{'HOME'} || die("HOME is not set\n")
l!l
00
MC/DC0%0/1
$ENV{'XDG_CONFIG_HOME'} || ($ENV{'HOME'} || die("HOME is not set\n")) . '/.config'
$ENV{'XDG_CONFIG_HOME'}
Truth table50%1/2
A
A= $ENV{'XDG_CONFIG_HOME'}
Aresult
11
00
251 7 ▶ $config_home =~ m{\A/} or die "XDG_CONFIG_HOME must be an absolute path\n";
Branch: 1 branch
BranchTrueFalseTotal
$config_home =~ m[\A/]u or die "XDG_CONFIG_HOME must be an absolute path\n" 7 0 7
252 7 $path = "$config_home/anyevent-mcp/rules/$path.ini";
253 }
254 9 ▶ my $action = $command =~ /\Amanage-/ ? "--manage-rules" : "--edit-rules";
Branch: 1 branch
BranchTrueFalseTotal
$command =~ /\Amanage-/u ? : 4 5 9
255 9 exec "anyevent-mcp-askpermission", $action, AnyEvent::MCP::utf8enc $path;
256 0 die "Cannot start permission rule tool: $!\n";
257 ▶ } elsif ($command eq "init" && !@ARGV) {
Condition66%2/3
$command eq "init" and not @ARGV
!ll&&!rl&&r
1501
MC/DC50%1/2
$command eq "init" and not @ARGV
$command eq "init" not @ARGV
Truth table66%2/3
A and not(B)
A= $command eq "init"B= not @ARGV
ABresult
0X0
100
111
258 1 ▶ my $config = AnyEvent::MCP::Config->init (defined $file ? (file => $file) : ());
Branch: 1 branch
BranchTrueFalseTotal
defined $file ? : 1 0 1
259 0 print "Created $config->{file}\n";
260 ▶ } elsif ($command =~ /\A(?:create-user|revoke-user)\z/ && @ARGV == 1) {
Condition66%2/3
$command =~ /\A(?:create-user|revoke-user)\z/u and @ARGV == 1
!ll&&!rl&&r
5010
MC/DC50%1/2
$command =~ /\A(?:create-user|revoke-user)\z/u and @ARGV == 1
$command =~ /\A(?:create-user|revoke-user)\z/u @ARGV == 1
Truth table66%2/3
A and B
A= $command =~ /\A(?:create-user|revoke-user)\z/uB= @ARGV == 1
ABresult
0X0
100
111
261 10 ▶ !defined $program || !defined $instance or die "--program and --instance are exclusive\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $program and defined $instance and die "--program and --instance are exclusive\n" 0 10 10
Condition66%2/3
defined $program and defined $instance
!ll&&!rl&&r
910
MC/DC0%0/2
defined $program and defined $instance
defined $program defined $instance
Truth table66%2/3
A and B
A= defined $programB= defined $instance
ABresult
0X0
100
111
262 10 my $user = AnyEvent::MCP::Config::server_name ($ARGV[0]);
263 10 ▶ my $scope = defined $program ? "program " . AnyEvent::MCP::Config::server_name ($program)
Branch: 2 branches
BranchTrueFalseTotal
defined $program ? : 1 9 10
defined $instance ? : 2 7 9
264 : defined $instance ? "instance " . AnyEvent::MCP::Config::server_name ($instance)
265 : "global";
266 10 ▶ my $config_file = $file // AnyEvent::MCP::Config::_file ();
Condition33%1/3
$file // AnyEvent::MCP::Config::_file()
l!l&&r!l&&!r
1000
MC/DC0%0/2
$file // AnyEvent::MCP::Config::_file()
$file AnyEvent::MCP::Config::_file()
Truth table33%1/3
A // B
A= $fileB= AnyEvent::MCP::Config::_file()
ABresult
1X1
011
000
267 10 AnyEvent::MCP::Config->new (file => $config_file);
268 10 change_user $config_file, $scope, $user, $command;
269 } else {
270 5 die "Invalid command or arguments; use --help\n";
271 }
272 8 1
273 26 ▶} or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { if ($command =~ /\A(?:manage-rules|edit-rules)(?:-file)?\z/u and @ARGV == 1) { my $path = $ARGV[0]; unless ($command =~ /-file\z/u) { $path =~ /\A[A-Za-z0-9][A-Za-z0-9_.-]*\z/u or die "Invalid permission rule profile name\n"; my $config_home = $ENV{"XDG_CONFIG_HOME"} || ($ENV{"HOME"} || die("HOME is not set\n")) . '/.config'; $config_home =~ m[\A/]u or die "XDG_CONFIG_HOME must be an absolute path\n"; $path = "$config_home/anyevent-mcp/rules/$path.ini"; } ; my $action = $command =~ /\Amanage-/u ? "--manage-rules" : "--edit-rules"; exec "anyevent-mcp-askpermission", $action, &AnyEvent::MCP::utf8enc($path); die "Cannot start permission rule tool: $!\n"; } elsif ($command eq "init" and not @ARGV) { my $config = "AnyEvent::MCP::Config"->init(defined $file ? ("file", $file) : ()); print qq[Created $config->{"file"}\n]; } elsif ($command =~ /\A(?:create-user|revoke-user)\z/u and @ARGV == 1) { defined $program and defined $instance and die "--program and --instance are exclusive\n"; my $user = AnyEvent::MCP::Config::server_name($ARGV[0]); my $scope = defined $program ? 'program ' . AnyEvent::MCP::Config::server_name($program) : (defined $instance ? 'instance ' . AnyEvent::MCP::Config::server_name($instance) : "global"); my $config_file = $file // AnyEvent::MCP::Config::_file(); "AnyEvent::MCP::Config"->new("file", $config_file); change_user($config_file, $scope, $user, $command); } else { die "Invalid command or arguments; use --help\n"; } ; 1 } } or do { print STDERR $@; exit 1 } 8 9 17
274 9 print STDERR $@;
275 9 exit 1;
276 };
277  
« bin/mcp-vim ↑ Summary lib/AnyEvent/MCP/IMAP/OAuth2.pm »