1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
|
# --
# Copyright (C) 2001-2021 OTRS AG, https://otrs.com/
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (GPL). If you
# did not receive this file, see https://www.gnu.org/licenses/gpl-3.0.txt.
# --
## nofilter(TidyAll::Plugin::OTRS::Perl::NoExitInConsoleCommands)
package Kernel::System::Console::Command::Maint::PostMaster::MailAccountFetch;
use strict;
use warnings;
use parent qw(Kernel::System::Console::BaseCommand);
use POSIX ":sys_wait_h";
use Time::HiRes qw(sleep);
our @ObjectDependencies = (
'Kernel::System::Log',
'Kernel::System::MailAccount',
'Kernel::System::PID',
);
sub Configure {
my ( $Self, %Param ) = @_;
$Self->Description('Fetch incoming emails from configured mail accounts.');
$Self->AddOption(
Name => 'mail-account-id',
Description => "Fetch mail only from this account (default: fetch from all).",
Required => 0,
HasValue => 1,
ValueRegex => qr{^\d+$}smx,
);
$Self->AddOption(
Name => 'force-pid',
Description => "Start even if another process is still registered in the database.",
Required => 0,
HasValue => 0,
);
$Self->AddOption(
Name => 'debug',
Description => "Print debug info to the OTRS log.",
Required => 0,
HasValue => 0,
);
$Self->AddOption(
Name => 'timeout',
Description =>
"Timeout in seconds to kill the child process, that does the mail fetching (default: 600).",
Required => 0,
HasValue => 1,
ValueRegex => qr{^\d+$}smx,
);
return;
}
sub PreRun {
my ($Self) = @_;
my $PIDObject = $Kernel::OM->Get('Kernel::System::PID');
my $PIDCreated = $PIDObject->PIDCreate(
Name => 'MailAccountFetch',
Force => $Self->GetOption('force-pid'),
TTL => 600, # 10 minutes
);
if ( !$PIDCreated ) {
my $Error = "Unable to register the process in the database. Is another instance still running?\n";
$Error .= "You can use --force-pid to override this check.\n";
die $Error;
}
my $Debug = $Self->GetOption('debug');
my $Name = $Self->Name();
if ($Debug) {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'debug',
Message => "OTRS email handle ($Name) started.",
);
}
return;
}
sub Run {
my ( $Self, %Param ) = @_;
my $MailAccountObject = $Kernel::OM->Get('Kernel::System::MailAccount');
my %List = $MailAccountObject->MailAccountList( Valid => 1 );
my $Debug = $Self->GetOption('debug');
my $Timeout = $Self->GetOption('timeout') || 600;
my $MailAccountID = $Self->GetOption('mail-account-id');
if ( !%List ) {
if ($MailAccountID) {
$Self->PrintError("Could not find mail account $MailAccountID.");
return $Self->ExitCodeError();
}
$Self->Print("\n<yellow>No configured mail accounts found!</yellow>\n\n");
return $Self->ExitCodeOk();
}
# Setup alarm signal handler to kill the running child process if we reach the given timeout.
local $SIG{ALRM} = sub {
$Self->PrintError("Timeout of $Timeout seconds reached, killing child process!\n");
kill 9, $Self->{ChildPID};
};
# Set the timeout to kill the child.
alarm $Timeout;
# Destroy objects for the child processes.
$Kernel::OM->ObjectsDiscard(
Objects => [
'Kernel::System::DB',
],
ForcePackageReload => 0,
);
$Self->Print("\n<yellow>Spawning child process to fetch incoming messages from mail accounts...</yellow>\n\n");
# Create a child process.
my $PID = fork;
# Could not create child.
if ( $PID < 0 ) {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'error',
Message => "Unable to fork to a child process for mail account fetching!"
);
$Self->PrintError("Unable to fork to a child process for mail account fetching!");
return $Self->ExitCodeError();
}
# We're in the child process.
if ( !$PID ) {
my $ErrorCount;
my $FetchedCount;
KEY:
for my $Key ( sort keys %List ) {
next KEY if ( $MailAccountID && $Key != $MailAccountID );
my %Data = $MailAccountObject->MailAccountGet( ID => $Key );
$Self->Print("<yellow>$Data{Host} ($Data{Type})...</yellow>\n");
# It is needed for capture the standard error.
my $Status;
my $ErrorMessage;
eval {
# Localize the standard error, everything will be restored after the eval block.
local *STDERR;
# Redirect the standard error to a variable.
open STDERR, ">>", \$ErrorMessage;
$Status = $MailAccountObject->MailAccountFetch(
%Data,
Debug => $Self->GetOption('debug'),
CMD => 1,
UserID => 1,
);
};
# Hide password contained in error message and print message back to standard error.
# Please see bug#12829 for more information.
if ($ErrorMessage) {
$ErrorMessage =~ s/\Q$Data{Password}\E/********/g;
print STDERR $ErrorMessage;
}
if ($Status) {
$FetchedCount++;
}
else {
$ErrorCount++;
}
}
my $ExitCode = $Self->ExitCodeOk();
if ($ErrorCount) {
# Error messages printed by backend.
$ExitCode = $Self->ExitCodeError();
}
if ( !$FetchedCount && $MailAccountID ) {
$Self->PrintError("Could not find mail account $MailAccountID.");
$ExitCode = $Self->ExitCodeError();
}
# Close child process at the end.
exit $ExitCode;
}
# Remember the child process ID.
$Self->{ChildPID} = $PID;
# Check the status of the child process every 0.1 seconds.
# Wait for the child process to be finished.
WAIT:
while (1) {
last WAIT if !$Self->{ChildPID};
sleep 0.1;
my $WaitResult = waitpid( $PID, WNOHANG );
if ( $WaitResult == -1 ) {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'error',
Message => "Child process exited with errors: $?",
);
$Self->{ChildPID} = undef;
}
$Self->{ChildPID} = undef if $WaitResult;
}
alarm 0;
$Self->Print("<green>Done.</green>\n\n");
return $Self->ExitCodeOk();
}
sub PostRun {
my ($Self) = @_;
# Destroy objects for the child processes.
$Kernel::OM->ObjectsDiscard(
Objects => [
'Kernel::System::DB',
],
ForcePackageReload => 0,
);
my $Debug = $Self->GetOption('debug');
my $Name = $Self->Name();
if ($Debug) {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'debug',
Message => "OTRS email handle ($Name) stopped.",
);
}
return $Kernel::OM->Get('Kernel::System::PID')->PIDDelete( Name => 'MailAccountFetch' );
}
1;
|