Unverified Commit a97f0d6f authored by IKEDA Soji's avatar IKEDA Soji Committed by GitHub
Browse files

Merge pull request #1226 from ikedas/modern_open by ikedas

Cleanup: Avoid using bareword filehandles etc.
parents 027c985b 81ed555d
......@@ -27,6 +27,7 @@ use lib '--modulesdir--';
use strict;
use warnings;
use English qw(-no_match_vars);
use File::Path qw();
use Getopt::Long;
use Conf;
......@@ -120,35 +121,23 @@ if (-d $dest_dir) {
}
if ($opt{'input-directory'}) {
unless (-d $opt{'input-directory'}) {
die "Parameter input-directory (%s) is not a directory",
$opt{'input-directory'};
}
opendir DIR, $opt{'input-directory'} || die;
foreach my $file (sort grep (!/^\.\.?$/, readdir(DIR))) {
my @msgs; #FIXME: Not used anymore.
open ARCFILE, $opt{'input-directory'} . '/' . $file;
my @msg = <ARCFILE>;
push @msgs, \@msg;
$msg_count++;
close ARCFILE;
}
closedir DIR;
opendir my $dh, $opt{'input-directory'} or die $ERRNO;
$msg_count +=
scalar(
grep { !/\A[.][.]?\z/ and -r ($opt{'input-directory'} . '/' . $_) }
readdir $dh);
closedir $dh;
} else {
print STDERR "Bursting archives\n";
foreach my $arc_file (<$home_sympa/$listname/archives/log*>) {
my ($first, $new);
my $msg = [];
my @msgs;
## Split the archives file
# Split the archives file.
print '.';
open ARCFILE, $arc_file;
while (<ARCFILE>) {
open my $ifh, '<', $arc_file;
while (<$ifh>) {
if (/^------- THIS IS A RFC934 (COMPILANT|COMPLIANT) DIGEST/) {
$first = 1;
$new = 1;
......@@ -167,17 +156,10 @@ if ($opt{'input-directory'}) {
undef $new;
}
}
close ARCFILE;
close $ifh;
##Dump
#foreach my $i (0..$#msgs) {
# printf "******** Message %d *******\n", $i;
# print @{$msgs[$i]};
#}
## Store messages in web arc
# Store messages in web arc.
store_messages(\@msgs, $dest_dir);
}
}
......@@ -281,25 +263,19 @@ sub store_messages {
$month = 'KNOWN';
}
# New month
if (!-d "$dest_dir/$year-$month") {
print "\nNew directory $year-$month\n";
`mkdir $dest_dir/$year-$month`;
}
if (!-d "$dest_dir/$year-$month/arctxt") {
`mkdir $dest_dir/$year-$month/arctxt`;
}
# New month.
File::Path::make_path("$dest_dir/$year-$month/arctxt", {mode => 0755})
unless -d "$dest_dir/$year-$month/arctxt";
$nummsg{$year}{$month}++
while (-e "$dest_dir/$year-$month/arctxt/$nummsg{$year}{$month}");
# Save message
open DESTFILE,
">$dest_dir/$year-$month/arctxt/$nummsg{$year}{$month}";
print DESTFILE @{$msg};
close DESTFILE;
# `mv $m $dest_dir/$year-$month/arctxt/$nummsg{$year}{$month}`;
# Save message.
open my $ofh, '>',
sprintf '%s/%s-%s/arctxt/%s', $dest_dir, $year, $month,
$nummsg{$year}{$month};
print $ofh join '', @{$msg};
close $ofh;
$nummsg{$year}{$month}++;
}
......
......@@ -36,7 +36,6 @@ use Sympa::Constants;
use Sympa::DatabaseManager;
use Sympa::Language;
use Sympa::List;
use Sympa::Log;
use Sympa::Tools::File;
my %options;
......@@ -50,9 +49,6 @@ $OUTPUT_AUTOFLUSH = 1;
# die unless ($s =~ /^y$/i);
#}
# FIXME: Is logging required?
my $log = Sympa::Log->instance;
## Load sympa config
unless (Conf::load()) {
die 'config_error';
......@@ -137,28 +133,28 @@ foreach my $vr (keys %{$Conf::Conf{'robots'}}) {
push @directories, $list->{'dir'};
if (-d "$list->{'dir'}/templates") {
push @directories, "$list->{'dir'}/templates";
if (-d ($list->{'dir'} . '/templates')) {
push @directories, $list->{'dir'} . '/templates';
}
if (-d "$list->{'dir'}/wws_templates") {
push @directories, "$list->{'dir'}/wws_templates";
if (-d ($list->{'dir'} . '/wws_templates')) {
push @directories, $list->{'dir'} . '/wws_templates';
}
}
}
## List .tpl files
foreach my $d (@directories) {
unless (opendir DIR, $d) {
my $dh;
unless (opendir $dh, $d) {
printf STDERR "Error: Cannot read %s directory : %s\n", $d, $ERRNO;
next;
}
foreach my $tpl (sort grep(/\.tpl$/, readdir DIR)) {
foreach my $tpl (sort grep {/\.tpl$/} readdir $dh) {
push @templates, "$d/$tpl";
}
closedir DIR;
closedir $dh;
}
my $total;
......@@ -168,7 +164,8 @@ foreach my $tpl (@templates) {
if ($tpl =~ /mhonarc\-ressources$/) {
rename $tpl, "$tpl.incompatible";
printf STDERR
"File $tpl could not be translated to TT2 ; it has been renamed $tpl.incompatible. You should customize a standard mhonarc_rc.tt2 file\n";
"File %s could not be translated to TT2 ; it has been renamed %s.incompatible. You should customize a standard mhonarc_rc.tt2 file\n",
$tpl, $tpl;
next;
}
......@@ -231,8 +228,8 @@ foreach my $tpl (@templates) {
mode => 0755,
)
) {
$log->syslog('err', 'Unable to set rights on %s',
$Conf::Conf{'db_name'});
print STDERR "Error: Unable to set rights on %s: %s\n",
$dest_path, $ERRNO;
next;
}
}
......@@ -256,29 +253,30 @@ print "\n$total template files have been converted\n";
sub convert {
my ($in_file, $out_file) = @_;
## Convert tpl file
unless (open TPL, $in_file) {
print STDERR "Cannot open $in_file : $ERRNO\n";
# Convert tpl file.
my ($ifh, $ofh);
unless (open $ifh, '<', $in_file) {
printf STDERR "Cannot open %s: %s\n", $in_file, $ERRNO;
return undef;
}
if ($out_file) {
unless (open TT2, ">$out_file") {
print STDERR "Cannot create $out_file : $ERRNO\n";
unless (open $ofh, '>', $out_file) {
printf STDERR "Cannot create %s: %s\n", $out_file, $ERRNO;
return undef;
}
}
while (<TPL>) {
while (<$ifh>) {
if ($out_file) {
print TT2 _translate($_);
print $ofh _translate($_);
} else {
print STDOUT _translate($_);
}
}
close TT2 if ($out_file);
close TPL;
close $ofh if $out_file;
close $ifh;
printf "Template file $in_file has been converted to $out_file\n";
printf "Template file %s has been converted to %s\n", $in_file, $out_file;
unless (
Sympa::Tools::File::set_file_rights(
......@@ -287,8 +285,8 @@ sub convert {
group => Sympa::Constants::GROUP
)
) {
$log->syslog('err', 'Unable to set rights on %s',
$Conf::Conf{'db_name'});
print STDERR "Error: Unable to set rights on %s: %s\n",
$out_file, $ERRNO;
return undef;
}
}
......
......@@ -234,7 +234,7 @@ $sth->finish();
# save hashes for later if hash db file is specified
if (defined($cache) && $savecache && $hashes_changed) {
printf "Saving hashes in $cache\n";
printf "Saving hashes in %s\n", $cache;
save_hashes($cache, $hashes);
}
......@@ -304,16 +304,21 @@ sub read_hashes {
my ($f) = @_;
my $h = {};
open(HASHES, "<$f") || die "$0: read_hashes: open $f: $!\n";
while (<HASHES>) {
next if (/^$/ || /^\#/); # ignore blank lines/comments
chomp;
open my $ifh, '<', $f
or die sprintf "%s: read_hashes: open %s: %s\n", $PROGRAM_NAME, $f,
$ERRNO;
while (<$ifh>) {
chomp $_;
next if /^$/ or /^#/; # ignore blank lines/comments
my ($email, $checksum, $type, $hash) = split(/ /, $_, 4);
warn "$0: parse error: $_\n", next
unless ($email && $checksum && $type && $hash);
die "$0: $email: unsupported hash type $type\n"
unless ($type =~ /^(md5|bcrypt)$/);
unless ($email and $checksum and $type and $hash) {
warn sprintf "%s: parse error: %s\n", $PROGRAM_NAME, $_;
next;
}
die sprintf "%s: %s: unsupported hash type %s\n", $PROGRAM_NAME,
$email, $type
unless grep { $type eq $_ } qw(md5 bcrypt);
$h->{$email} = {
'email_user' => $email,
......@@ -322,7 +327,7 @@ sub read_hashes {
'hash' => $hash
};
}
close(HASHES);
close $ifh;
return $h;
}
......@@ -332,18 +337,20 @@ sub save_hashes {
my $tmpfile = "$f.tmp.$$";
open(HASHES, ">$tmpfile") || die "$0: save_hashes: open $tmpfile: $!\n";
open my $ofh, '>', $tmpfile
or die sprintf "%s: save_hashes: open %s: %s\n", $PROGRAM_NAME,
$tmpfile, $ERRNO;
# prevent world/group access
chmod 0600, $tmpfile;
foreach my $email_user (sort keys %$h) {
my $u = $h->{$email_user};
printf HASHES "%s %s %s %s\n",
$u->{'email_user'}, $u->{'checksum'},
$u->{'type'}, $u->{'hash'};
printf $ofh "%s %s %s %s\n",
$u->{email_user}, $u->{checksum},
$u->{type}, $u->{hash};
}
close(HASHES);
close $ofh;
rename($f, "$f.old");
rename($tmpfile, $f);
......
......@@ -4304,17 +4304,17 @@ sub do_active_lists {
## get msg count for each list
foreach my $l (keys(%{$param->{'which'}})) {
my $list = Sympa::List->new($l, $robot);
my $file = "$list->{'dir'}/msg_count";
my $file = $list->{'dir'} . '/msg_count';
 
my %count;
 
if (open(MSG_COUNT, $file)) {
while (<MSG_COUNT>) {
if (open my $ifh, '<', $file) {
while (<$ifh>) {
if ($_ =~ /^(\d+)\s(\d+)$/) {
$count{$1} = $2;
}
}
close MSG_COUNT;
close $ifh;
 
$param->{'which'}{$l}{'msg_count'} =
count_total_msg_since($oldest_day, \%count);
......@@ -7558,7 +7558,6 @@ sub do_reject {
}
 
wwslog('info', '(%s)', $in{'id'});
my $file;
 
$param->{'blocklist_added'} = 0;
$param->{'blocklist_ignored'} = 0;
......@@ -7630,34 +7629,36 @@ sub do_reject {
}
}
 
if ( ($in{'signal_spam'})
&& ($Conf::Conf{'reporting_spam_script_path'} ne '')) {
if (-x $Conf::Conf{'reporting_spam_script_path'}) {
unless (
open(SCRIPT, "|$Conf::Conf{'reporting_spam_script_path'}"
)
) {
$log->syslog('err',
"could not execute $Conf::Conf{'reporting_spam_script_path'}"
);
}
my $script = $Conf::Conf{'reporting_spam_script_path'};
if ($in{'signal_spam'} and length($script // '')) {
my $pipeout;
unless (open $pipeout, '|-', $script) {
$log->syslog('err', 'Could not execute %s: %m', $script);
} else {
# Sending encrypted form in case a crypted message would be
# sent by error.
print SCRIPT $message->as_string;
print $pipeout $message->as_string;
 
if (close(SCRIPT)) {
$log->syslog('info',
"message $file reported as spam by $param->{'user'}{'email'}"
if (close $pipeout) {
$log->syslog(
'info', 'Message %s reported as spam by %s',
$message, $param->{'user'}{'email'}
);
} elsif ($ERRNO) {
$log->syslog(
'err',
'Could not report message %s as spam (close failed): %m',
$message
);
} else {
$log->syslog('err',
"could not report message $file as spam (close failed)"
$log->syslog(
'err',
'Could not report message %s as spam: %s terminated with status %s',
$message,
$script,
($CHILD_ERROR >> 8)
);
}
} else {
$log->syslog('err',
"ignoring parameter reporting_spam_script_path, value $Conf::Conf{'reporting_spam_script_path'} because not an executable script"
);
}
}
 
......@@ -9328,11 +9329,11 @@ sub do_get_inactive_lists {
foreach my $list (@$all_lists) {
my $last_message = 0;
 
if (open COUNT, $list->{'dir'} . '/msg_count') {
while (<COUNT>) {
if (open my $ifh, '<', $list->{'dir'} . '/msg_count') {
while (<$ifh>) {
$last_message = $1 if (/^(\d+)\s/ && ($1 > $last_message));
}
close COUNT;
close $ifh;
 
} else {
wwslog(
......@@ -12020,23 +12021,21 @@ sub creation_picture_file {
return undef;
}
 
unless (open(FF, '>', $path . '/index.html')) {
unless (open my $ofh, '>', $path . '/index.html') {
wwslog('err', 'Unable to create dir %s/index.html', $path);
}
chmod 0644, $path . '/index.html';
close FF;
}
 
my $fh = $query->upload('uploaded_file');
unless (open FILE, '>:bytes', "$path/$fname") {
my $ifh = $query->upload('uploaded_file');
my $ofh;
unless (open $ofh, '>', "$path/$fname") {
wwslog('err', 'Cannot open file %s/%s: %m', $path, $fname);
add_stash('intern', 'cannot_upload', {path => "$path/$fname"});
wwslog('err', 'Cannot open file %s/%s: %s', $path, $fname, $ERRNO);
return undef;
}
while (<$fh>) {
print FILE;
}
close FILE;
while (<$ifh>) {print $ofh $_};
close $ofh;
chmod 0644, "$path/$fname";
}
 
......
......@@ -347,21 +347,19 @@ sub get_wwsympa_conf {
# server.
sub get_robots_list {
$log->syslog('debug2', "Retrieving the list of robots on the server");
my @robots_list;
unless (opendir DIR, $Conf{'etc'}) {
my $dh;
unless (opendir $dh, $Conf{'etc'}) {
$log->syslog('err',
'Unable to open directory %s for virtual robots config',
$Conf{'etc'});
return undef;
}
foreach my $robot (readdir DIR) {
my $robot_config_file = "$Conf{'etc'}/$robot/robot.conf";
next unless (-d "$Conf{'etc'}/$robot");
next unless (-f $robot_config_file);
push @robots_list, $robot;
}
closedir(DIR);
return \@robots_list;
my @robots_list =
grep { !/\A[.]/ and -f ($Conf{'etc'} . '/' . $_ . '/robot.conf') }
readdir $dh;
closedir $dh;
return [@robots_list];
}
## Returns a hash containing the values of all the parameters of the group
......@@ -502,17 +500,10 @@ sub conf_2_db {
# defaults).
my $robots_conf = load_robots();
unless (opendir DIR, $Conf{'etc'}) {
$log->syslog('err',
'Unable to open directory %s for virtual robots config',
$Conf{'etc'});
return undef;
}
foreach my $robot (readdir(DIR)) {
next unless (-d "$Conf{'etc'}/$robot");
next unless (-f "$Conf{'etc'}/$robot/robot.conf");
my $robots_ref = get_robots_list();
return undef unless $robots_ref;
foreach my $robot (@$robots_ref) {
my $config;
if (my $result_of_config_loading = _load_config_file_to_hash(
$Conf{'etc'} . '/' . $robot . '/robot.conf'
......@@ -538,9 +529,8 @@ sub conf_2_db {
}
}
}
closedir(DIR);
# store in database sympa;conf and wwsympa.conf
# Store sympa.conf into database.
## Load configuration file. Ignoring database config and get result
my $global_conf;
......@@ -570,8 +560,9 @@ sub checkfiles_as_root {
## Check aliases file
unless (-f $Conf{'sendmail_aliases'}
|| ($Conf{'sendmail_aliases'} =~ /^none$/i)) {
unless (open ALIASES, ">$Conf{'sendmail_aliases'}") {
or lc $Conf{'sendmail_aliases'} eq 'none') {
my $ofh;
unless (open $ofh, '>', $Conf{'sendmail_aliases'}) {
$log->syslog(
'err',
"Failed to create aliases file %s",
......@@ -580,11 +571,11 @@ sub checkfiles_as_root {
return undef;
}
print ALIASES
print $ofh
"## This aliases file is dedicated to Sympa Mailing List Manager\n";
print ALIASES
print $ofh
"## You should edit your sendmail.mc or sendmail.cf file to declare it\n";
close ALIASES;
close $ofh;
$log->syslog(
'notice',
"Created missing file %s",
......@@ -807,9 +798,9 @@ sub valid_robot {
# return undef if none was found
# Old name: get_sso_by_id().
sub get_auth_service {
my $robot = shift;
my $auth_type = shift;
my $service_id = shift;
my $robot = shift;
my $auth_type = shift;
my $service_id = shift;
if ($auth_type eq 'cas') {
return undef unless $service_id;
......@@ -946,14 +937,15 @@ sub _load_auth {
'authentication_info_url' => 'http(s)?:/.*'
);
## Open the configuration file or return and read the lines.
unless (open(IN, $config_file)) {
# Open the configuration file or return and read the lines.
my $ifh;
unless (open $ifh, '<', $config_file) {
$log->syslog('notice', 'Unable to open %s: %m', $config_file);
return undef;
}
## Parsing auth.conf
while (<IN>) {
while (<$ifh>) {
$line_num++;
next if (/^\s*[\#\;]/o);
......@@ -996,7 +988,7 @@ sub _load_auth {
}
## process current paragraph
if (/^\s+$/o || eof(IN)) {
if (/^\s+$/o or eof $ifh) {
if (defined($current_paragraph)) {
# Parameters obsoleted as of 6.2.15.
if ($current_paragraph->{use_start_tls}) {
......@@ -1097,7 +1089,7 @@ sub _load_auth {
next;
}
}
close(IN);
close $ifh;
return \@paragraphs;
......@@ -1110,12 +1102,13 @@ sub load_charset {
my $config_file = Sympa::search_fullpath('*', 'charset.conf');
return {} unless $config_file;
unless (open CONFIG, $config_file) {
my $ifh;
unless (open $ifh, '<', $config_file) {
$log->syslog('err', 'Unable to read configuration file %s: %m',
$config_file);
return {};
}
while (<CONFIG>) {
while (<$ifh>) {
chomp $_;
s/\s*#.*//;
s/^\s+//;
......@@ -1132,7 +1125,7 @@ sub load_charset {
$charset->{$lang} = $cset;
}
close CONFIG;
close $ifh;
return $charset;
}
......@@ -1185,11 +1178,12 @@ sub load_nrcpt_by_domain {
my $valid_dom = 0;
## Open the configuration file or return and read the lines.
unless (open IN, '<', $config_file) {
my $ifh;
unless (open $ifh, '<', $config_file) {
$log->syslog('err', 'Unable to open %s: %m', $config_file);
return;
}
while (<IN>) {
while (<$ifh>) {
$line_num++;
next if (/^\s*$/o || /^[\#\;]/o);
if (/^(\S+)\s+(\d+)$/io) {
......@@ -1205,7 +1199,7 @@ sub load_nrcpt_by_domain {
$config_err++;
}
}
close IN;
close $ifh;
return $nrcpt_by_domain;
}
......@@ -1386,12 +1380,13 @@ sub load_generic_conf_file {