<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/perl
# --
# bin/otrs.PostMasterMailbox.pl - the global eMail handle for email2db
# Copyright (C) 2001-2014 OTRS AG, http://otrs.com/
# --
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU AFFERO General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
# or see http://www.gnu.org/licenses/agpl.txt.
# --

use strict;
use warnings;

use File::Basename;
use FindBin qw($RealBin);
use lib dirname($RealBin);
use lib dirname($RealBin) . '/Kernel/cpan-lib';
use lib dirname($RealBin) . '/Custom';

use Getopt::Std;

use Kernel::Config;
use Kernel::System::Encode;
use Kernel::System::Log;
use Kernel::System::Main;
use Kernel::System::Time;
use Kernel::System::DB;
use Kernel::System::PID;
use Kernel::System::PostMaster;
use Kernel::System::MailAccount;

# get options
my %Opts = ();
getopt( 'upshdftb', \%Opts );
if ( $Opts{h} ) {
    print "PostMasterMailbox.pl - Fetch mail accounts for OTRS\n";
    print "Copyright (C) 2001-2014 OTRS AG, http://otrs.com/\n";
    print "usage: PostMasterMailbox.pl -t &lt;TYPE&gt; (POP3|POP3S|IMAP|IMAPS) -s &lt;SERVER&gt; -u &lt;USER&gt; ";
    print "-p &lt;PASSWORD&gt; [-d 1-2] [-b &lt;BACKGROUND_INTERVAL_IN_MIN&gt;] [-f force]\n";
    exit 1;
}

# set debug
if ( !$Opts{d} ) {
    $Opts{d} = 0;
}

# check -b option
if ( $Opts{b} &amp;&amp; $Opts{b} !~ /^\d+$/ ) {
    print STDERR "ERROR: Need -b &lt;BACKGROUND_INTERVAL_IN_MIN&gt;, e. g. -b 5 for fetching emails ";
    print STDERR "every 5 minutes.\n";
    exit 1;
}

# create common objects
my %CommonObject = ();
$CommonObject{ConfigObject} = Kernel::Config-&gt;new();
$CommonObject{EncodeObject} = Kernel::System::Encode-&gt;new(%CommonObject);
$CommonObject{LogObject}    = Kernel::System::Log-&gt;new(
    LogPrefix =&gt; 'OTRS-otrs.PostMasterMailbox.pl',
    %CommonObject,
);
$CommonObject{MainObject} = Kernel::System::Main-&gt;new(%CommonObject);
$CommonObject{TimeObject} = Kernel::System::Time-&gt;new(%CommonObject);
$CommonObject{DBObject}   = Kernel::System::DB-&gt;new(%CommonObject);
$CommonObject{PIDObject}  = Kernel::System::PID-&gt;new(%CommonObject);

# create pid lock
if ( !$Opts{f} &amp;&amp; !$CommonObject{PIDObject}-&gt;PIDCreate( Name =&gt; 'PostMasterMailbox' ) ) {
    print "NOTICE: PostMasterMailbox.pl is already running (use '-f 1' if you want to start it ";
    print "forced)!\n";
    exit 1;
}
elsif ( $Opts{f} &amp;&amp; !$CommonObject{PIDObject}-&gt;PIDCreate( Name =&gt; 'PostMasterMailbox' ) ) {
    print "NOTICE: PostMasterMailbox.pl is already running but is starting again!\n";
}

# fetch mails -b is not used
if ( !$Opts{b} ) {
    Fetch(%CommonObject);
}

# while to run several times if -b is used
else {
    while (1) {

        # set new PID
        $CommonObject{PIDObject}-&gt;PIDCreate(
            Name  =&gt; 'PostMasterMailbox',
            Force =&gt; 1,
        );

        # fetch mails
        Fetch(%CommonObject);

        # sleep till next interval
        print "NOTICE: Waiting for next interval ($Opts{b} min)...\n";
        sleep 60 * $Opts{b};
    }
}

# delete pid lock
$CommonObject{PIDObject}-&gt;PIDDelete( Name =&gt; 'PostMasterMailbox' );
exit(0);

sub Fetch {
    my (%CommonObject) = @_;

    my $MailAccount = Kernel::System::MailAccount-&gt;new(%CommonObject);

    # debug info

    if ( $Opts{d} &gt; 1 ) {
        $CommonObject{LogObject}-&gt;Log(
            Priority =&gt; 'debug',
            Message  =&gt; 'Global OTRS email handle (PostMasterMailbox.pl) started...',
        );
    }

    if ( $Opts{s} || $Opts{u} || $Opts{p} || $Opts{t} ) {
        if ( !$Opts{t} ) {

            # delete pid lock
            $CommonObject{PIDObject}-&gt;PIDDelete( Name =&gt; 'PostMasterMailbox' );
            print STDERR "ERROR: Need -t &lt;TYPE&gt; (POP3|IMAP)\n";
            exit 1;
        }
        if ( !$Opts{s} ) {

            # delete pid lock
            $CommonObject{PIDObject}-&gt;PIDDelete( Name =&gt; 'PostMasterMailbox' );
            print STDERR "ERROR: Need -s &lt;SERVER&gt;\n";
            exit 1;
        }
        if ( !$Opts{u} ) {

            # delete pid lock
            $CommonObject{PIDObject}-&gt;PIDDelete( Name =&gt; 'PostMasterMailbox' );
            print STDERR "ERROR: Need -u &lt;USER&gt;\n";
            exit 1;
        }
        if ( !$Opts{p} ) {

            # delete pid lock
            $CommonObject{PIDObject}-&gt;PIDDelete( Name =&gt; 'PostMasterMailbox' );
            print STDERR "ERROR: Need -p &lt;PASSWORD&gt;\n";
            exit 1;
        }
        $MailAccount-&gt;MailAccountFetch(
            Login         =&gt; $Opts{u},
            Password      =&gt; $Opts{p},
            Host          =&gt; $Opts{s},
            Type          =&gt; $Opts{t},
            Trusted       =&gt; 0,
            DispatchingBy =&gt; '',
            QueueID       =&gt; 0,
            Debug         =&gt; $Opts{d},
            CMD           =&gt; 1,
            UserID        =&gt; 1,
        );
    }
    else {
        my %List = $MailAccount-&gt;MailAccountList( Valid =&gt; 1 );
        for my $Key ( sort keys %List ) {
            my %Data = $MailAccount-&gt;MailAccountGet( ID =&gt; $Key );
            $MailAccount-&gt;MailAccountFetch(
                %Data,
                Debug  =&gt; $Opts{d},
                CMD    =&gt; 1,
                UserID =&gt; 1,
            );
        }
    }

    # debug info
    if ( $Opts{d} &gt; 1 ) {
        $CommonObject{LogObject}-&gt;Log(
            Priority =&gt; 'debug',
            Message  =&gt; 'Global OTRS email handle (PostMasterMailbox.pl) stopped.',
        );
    }
}
</pre></body></html>