AnyEvent-MCP

 view release on metacpan or  search on metacpan

bin/anyevent-mcp  view on Meta::CPAN

A missing master secret is generated and saved automatically when a server
loads its configuration. Configuration format: see AnyEvent::MCP.
EOF
}

sub change_user {
   my ($file, $section, $user, $command) = @_;

   my $lock = AnyEvent::MCP::file_lock $file;
   my $config = AnyEvent::MCP::Config->new (file => $file)->load ($lock);
   my $values = $config->{section}{$section} ||= {};
   my $key = "user.$user";
   my $generation = $values->{$key};
   if (defined $generation) {
      $generation =~ /\A-?[1-9][0-9]*\z/ && length $generation < 18
         or die "Invalid $key generation '$generation'\n";
      $generation = 0 + $generation;
   }

   if ($command eq "create-user") {
      die "$key already enabled in [$section]\n"
         if defined $generation && $generation > 0 || !defined $generation && $section eq "global" && $user eq "default";
      $generation = defined $generation ? -$generation + 1 : 1;
   } else {
      die "$key already revoked in [$section]\n" if defined $generation && $generation < 0;
      if (!defined $generation) {
         ($section ne "global" || $user eq "default")
            or die "$key is not enabled\n";
         $generation = -1;
      } else {
         $generation = -$generation;
      }
   }

   $values->{$key} = $generation;
   $config->save;
   close $lock or die "$file: $!\n";
   print "$section: $user generation " . abs ($generation)
      . ($generation > 0 ? " enabled\n" : " revoked\n");
}

Getopt::Long::Configure ("no_auto_abbrev", "no_ignore_case", "require_order");
Getopt::Long::GetOptions
   "config=s"   => \my $file,
   "program=s"  => \my $program,
   "instance=s" => \my $instance,
   "help"       => \my $help,
   or exit 2;

if ($help) {
   usage;
   exit 0;
}

my $command = shift @ARGV;
unless (defined $command) {
   usage;
   exit 2;
}

@ARGV = map AnyEvent::MCP::utf8dec $_, @ARGV;

eval {
   if ($command =~ /\A(?:manage-rules|edit-rules)(?:-file)?\z/ && @ARGV == 1) {
      my $path = $ARGV[0];
      if ($command !~ /-file\z/) {
         $path =~ /\A[A-Za-z0-9][A-Za-z0-9_.-]*\z/
            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/} or die "XDG_CONFIG_HOME must be an absolute path\n";
         $path = "$config_home/anyevent-mcp/rules/$path.ini";
      }
      my $action = $command =~ /\Amanage-/ ? "--manage-rules" : "--edit-rules";
      exec "anyevent-mcp-askpermission", $action, AnyEvent::MCP::utf8enc $path;
      die "Cannot start permission rule tool: $!\n";
   } elsif ($command eq "init" && !@ARGV) {
      my $config = AnyEvent::MCP::Config::global_config (defined $file ? (file => $file) : ());
      print "Initialized $config->{file}\n";
   } elsif ($command =~ /\A(?:create-user|revoke-user)\z/ && @ARGV == 1) {
      !defined $program || !defined $instance or die "--program and --instance are exclusive\n";
      my $user = AnyEvent::MCP::Config::user_name ($ARGV[0]);
      my $scope = defined $program ? "program " . AnyEvent::MCP::instance_name ($program)
                : defined $instance ? "instance " . AnyEvent::MCP::instance_name ($instance)
                : "global";
      my $config_file = $file // AnyEvent::MCP::Config::_file ();
      AnyEvent::MCP::Config->new (file => $config_file)->load->ensure_secret;
      change_user $config_file, $scope, $user, $command;
   } else {
      die "Invalid command or arguments; use --help\n";
   }
   1
} or do {
   print STDERR $@;
   exit 1;
};



( run in 0.464 second using v1.01-cache-2.11-cpan-234fa220139 )