From 59780fbaf9f1572c89deffab1ae3dd42015e4a36 Mon Sep 17 00:00:00 2001 From: IKEDA Soji Date: Wed, 5 Jan 2022 12:47:15 +0900 Subject: [PATCH 1/3] CLI: Improve error messages --- src/lib/Sympa/CLI.pm | 53 ++++++++++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 22 deletions(-) diff --git a/src/lib/Sympa/CLI.pm b/src/lib/Sympa/CLI.pm index 02cd7992f..ed726cca3 100644 --- a/src/lib/Sympa/CLI.pm +++ b/src/lib/Sympa/CLI.pm @@ -40,19 +40,18 @@ use Sympa::Tools::Data; sub run { my $class = shift; my $options = shift if @_ and ref $_[0] eq 'HASH'; - my $module = shift; + my $command = shift; my @argv = @_; # Load module for the command. - unless ($module and $module !~ /\W/) { - print STDERR "Unable to use %s module: Illegal module\n"; + unless ($command and $command !~ /\W/) { + printf STDERR "Invalid argument '%s' (command is expected)\n", + $command; return undef; } - $module = sprintf '%s::%s', $class, $module; - + my $module = sprintf '%s::%s', $class, $command; unless (eval sprintf 'require %s', $module and $module->isa($class)) { - printf STDERR "Unable to use %s module: %s\n", - $module, $EVAL_ERROR || "Not a $class class"; + printf STDERR "Invalid command '%s'\n", $command; return undef; } @@ -60,8 +59,7 @@ sub run { if (@argv and length($argv[0] // '') and $argv[0] !~ /\W/) { my $subdir = $INC{($module =~ s|::|/|gr) . '.pm'} =~ s/[.]pm\z//r; if (<$subdir/*.pm>) { - $module->run(($options ? ($options) : ()), @argv); - exit 0; + return $module->run(($options ? ($options) : ()), @argv); } } @@ -80,7 +78,7 @@ sub run { ) { printf STDERR "See '%s help %s'\n", $PROGRAM_NAME, join ' ', split /::/, ($module =~ s/\ASympa::CLI:://r); - exit 1; + return undef; } # Get privileges and load config if necessary. @@ -104,28 +102,38 @@ sub run { } elsif (@argv and defined $argv[0]) { @a = (shift @argv); } else { - printf STDERR "Missing %s.\n", $defs; - exit 1; + printf STDERR "Missing argument (%s is expected)\n", $defs; + return undef; } foreach my $arg (@a) { my $val; foreach my $def (split /[|]/, $defs) { if ($def eq 'list') { - unless (0 <= index $arg, '@') { - $val = Sympa::List->new($arg, $Conf::Conf{'domain'}); - } elsif ($arg =~ /\A[^\@]+\@[^\@]*\z/) { - $val = Sympa::List->new($arg); + if (index($arg, '@') < 0 and index($defs, 'domain') < 0) { + $val = Sympa::List->new($arg, $Conf::Conf{'domain'}, + {just_try => 1}); + } elsif ($arg =~ /\A([^\@]+)\@([^\@]*)\z/) { + my ($name, $domain) = ($1, $2); + $val = Sympa::List->new( + $name, + $domain || $Conf::Conf{'domain'}, + {just_try => 1} + ); } } elsif ($def eq 'list_id') { - unless (0 <= index $arg, '@') { + if (index($arg, '@') < 0 and index($defs, 'domain') < 0) { $val = $arg; } elsif ($arg =~ /\A[^\@]+\@[^\@]*\z/) { $val = $arg; } } elsif ($def eq 'family') { - my ($family_name, $domain) = split /\@\@/, $arg, 2; - if (length $family_name) { - $val = Sympa::Family->new($family_name, + if (index($arg, '@@') < 0 and index($defs, 'domain') < 0) + { + $val = + Sympa::Family->new($arg, $Conf::Conf{'domain'}); + } elsif ($arg =~ /\A([^\@]+)\@\@([^\@]*)\z/) { + my ($name, $domain) = ($1, $2); + $val = Sympa::Family->new($name, $domain || $Conf::Conf{'domain'}); } } elsif ($def eq 'domain') { @@ -144,8 +152,9 @@ sub run { if (defined $val) { push @parsed_argv, $val; } else { - printf STDERR "Unknown %s \"%s\".\n", $defs, $arg; - exit 1; + printf STDERR "Invalid argument '%s' (%s is expected)\n", + $arg, $defs; + return undef; } } } From 04b9232c6656024b172635c1df9ed03107a67bb5 Mon Sep 17 00:00:00 2001 From: IKEDA Soji Date: Wed, 5 Jan 2022 17:22:37 +0900 Subject: [PATCH 2/3] i18n'ized messages --- src/lib/Sympa/CLI.pm | 58 +++++++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 28 deletions(-) diff --git a/src/lib/Sympa/CLI.pm b/src/lib/Sympa/CLI.pm index ed726cca3..4cdfd49aa 100644 --- a/src/lib/Sympa/CLI.pm +++ b/src/lib/Sympa/CLI.pm @@ -37,21 +37,32 @@ use Sympa::Mailer; use Sympa::Template; use Sympa::Tools::Data; +my $language = Sympa::Language->instance; + sub run { my $class = shift; my $options = shift if @_ and ref $_[0] eq 'HASH'; my $command = shift; my @argv = @_; + if ($class eq 'Sympa::CLI') { + # Deal with some POSIX locales (LL.encoding) + my @langs = + map {s/[.].*\z//r} grep {defined} @ENV{qw(LANGUAGE LC_ALL LANG)}; + $language->set_lang(@langs, 'en-US', 'en'); + } + # Load module for the command. unless ($command and $command !~ /\W/) { - printf STDERR "Invalid argument '%s' (command is expected)\n", - $command; + warn $language->gettext_sprintf( + 'Invalid argument \'%s\' (command is expected)', $command) + . "\n"; return undef; } my $module = sprintf '%s::%s', $class, $command; unless (eval sprintf 'require %s', $module and $module->isa($class)) { - printf STDERR "Invalid command '%s'\n", $command; + warn $language->gettext_sprintf('Invalid command \'%s\'', $command) + . "\n"; return undef; } @@ -76,19 +87,17 @@ sub run { $module->_options ) ) { - printf STDERR "See '%s help %s'\n", $PROGRAM_NAME, join ' ', - split /::/, ($module =~ s/\ASympa::CLI:://r); + warn $language->gettext_sprintf('See \'%s help %s\'', + $PROGRAM_NAME, join ' ', split /::/, + ($module =~ s/\ASympa::CLI:://r)) + . "\n"; return undef; } # Get privileges and load config if necessary. - # Otherwise only setup language. - if ($module->_need_priv) { - $module->arrange(%options); - } else { - my $lang = $ENV{'LANGUAGE'} || $ENV{'LC_ALL'} || $ENV{'LANG'}; - $module->set_lang($options{'lang'}, $lang); - } + # Otherwise only setup language if specified. + $language->set_lang($options{lang}) if $options{lang}; + $module->arrange(%options) if $module->_need_priv; # Parse arguments. my @parsed_argv = (); @@ -102,7 +111,9 @@ sub run { } elsif (@argv and defined $argv[0]) { @a = (shift @argv); } else { - printf STDERR "Missing argument (%s is expected)\n", $defs; + warn $language->gettext_sprintf( + 'Missing argument (%s is expected)', $defs) + . "\n"; return undef; } foreach my $arg (@a) { @@ -152,8 +163,10 @@ sub run { if (defined $val) { push @parsed_argv, $val; } else { - printf STDERR "Invalid argument '%s' (%s is expected)\n", - $arg, $defs; + warn $language->gettext_sprintf( + 'Invalid argument \'%s\' (%s is expected)', + $arg, $defs) + . "\n"; return undef; } } @@ -232,7 +245,7 @@ sub arrange { Conf::get_sympa_conf(); } - $class->set_lang($options{'lang'}, $Conf::Conf{'lang'}); + $language->set_lang($Conf::Conf{'lang'}) unless $options{lang}; ## Main program if (!chdir($Conf::Conf{'home'})) { @@ -281,16 +294,6 @@ sub arrange { $is_arranged = 1; } -sub set_lang { - my $class = shift; - my @langs = @_; - - foreach (@langs) { - s/[.].*\z// if defined; # Compat.<2.3.3 & some POSIX locales - } - Sympa::Language->instance->set_lang(@langs, 'en-US', 'en'); -} - # Moved from: _report() in sympa.pl. sub _report { my $class = shift; @@ -315,7 +318,7 @@ sub _report { $message ||= $report_entry; $message =~ s/\n/ /g; - printf STDERR "%s [%s] %s\n", $action, $report_type, $message; + warn sprintf "%s [%s] %s\n", $action, $report_type, $message; } return $spindle->success ? 1 : undef; @@ -343,7 +346,6 @@ my @getoptions_messages = ( sub _translate_warn { my $output = shift; - my $language = Sympa::Language->instance; foreach my $item (@getoptions_messages) { my $format = $item->{'gettext_id'}; my $regexp = quotemeta $format; From 40b6c5cd3368f6384c4c29d13559b4bf50bf25fa Mon Sep 17 00:00:00 2001 From: IKEDA Soji Date: Wed, 12 Jan 2022 11:47:13 +0900 Subject: [PATCH 3/3] Prevent duplicated errors with some commands. --- src/lib/Sympa/Request/Handler/get.pm | 1 + src/lib/Sympa/Request/Handler/move_user.pm | 1 + 2 files changed, 2 insertions(+) diff --git a/src/lib/Sympa/Request/Handler/get.pm b/src/lib/Sympa/Request/Handler/get.pm index 91e849423..d1593ca68 100644 --- a/src/lib/Sympa/Request/Handler/get.pm +++ b/src/lib/Sympa/Request/Handler/get.pm @@ -61,6 +61,7 @@ sub _twist { $log->syslog('info', 'GET %s %s from %s refused, no archive for list %s', $which, $arc, $sender, $which); + $self->{finish} = 1; return undef; } diff --git a/src/lib/Sympa/Request/Handler/move_user.pm b/src/lib/Sympa/Request/Handler/move_user.pm index 90afd6624..ea85b6790 100644 --- a/src/lib/Sympa/Request/Handler/move_user.pm +++ b/src/lib/Sympa/Request/Handler/move_user.pm @@ -59,6 +59,7 @@ sub _twist { $log->syslog('info', 'No change on email'); $self->add_stash($request, 'user', 'no_email_changed', {email => $email}); + $self->{finish} = 1; return 1; }