]> git.proxmox.com Git - pmg-api.git/blobdiff - PMG/MailQueue.pm
add support for avast virus scanner
[pmg-api.git] / PMG / MailQueue.pm
index 2a972f03e54fd263b5ba2478e02f89b8a5148c02..37d30d7efdb7f35cdaed3991a4d4179998a57c2f 100644 (file)
@@ -2,7 +2,6 @@ package PMG::MailQueue;
 
 use strict;
 use warnings;
-use Carp;
 
 use PVE::SafeSyslog;
 use MIME::Parser;
@@ -20,9 +19,21 @@ our $spooldir = "/var/spool/pmg";
 
 my $fileseq = rand 1000;
 
-sub create_sppoldirs {
+sub create_spooldirs {
+    my ($lcid, $cleanup) = @_;
+
+    # if requested, remove any stale date
+    File::Path::remove_tree(
+       "$spooldir/cluster", "$spooldir/active",
+       "$spooldir/virus", "$spooldir/spam") if $cleanup;
+
     File::Path::make_path(
        "$spooldir/active", "$spooldir/spam", "$spooldir/virus");
+
+    if ($lcid) {
+       mkpath "$spooldir/cluster/$lcid/virus";
+       mkpath "$spooldir/cluster/$lcid/spam";
+    }
 }
 
 # called on service startup to remove any stale files
@@ -47,7 +58,7 @@ sub new_fileid {
     my $subsubdir = '';
 
     if (!($fh = IO::File->new ($path, 'w+', 0600))) {
-       croak "unable to create file '$path': $! : ERROR";
+       die "unable to create file '$path': $! : ERROR";
     }
 
     if (my $st = stat ($fh)) {
@@ -57,7 +68,7 @@ sub new_fileid {
        }
     } else {
        unlink $path;
-       croak "unable to stat file: $! : ERROR";
+       die "unable to stat file: $! : ERROR";
     }
 
     mkdir "$dir/$subdir/$subsubdir";
@@ -66,7 +77,7 @@ sub new_fileid {
 
     if (!rename ($path, "$dir/$subpath")) {
        unlink $path;
-       croak "unable to rename file: ERROR";
+       die "unable to rename file: ERROR";
     }
 
     return ($fh, $uid, $subpath);
@@ -157,10 +168,10 @@ sub quarantinedb_insert {
        $insert_cmds .= $dbh->quote ($sender) . ',';
        $insert_cmds .= $dbh->quote ($file) . ');';
 
-       my $tid = int (rand (0x0fffffff));
-
        my $now = time();
 
+       my $tid = int(rand(0x0fffffff));
+
        foreach my $r (@$targets) {
            my $pmail = get_primary_mail ($ldap, $r);
            my $receiver;
@@ -176,7 +187,7 @@ sub quarantinedb_insert {
                "(CMailStore_CID, CMailStore_RID, PMail, Receiver, TicketID, Status, MTime) " .
                "VALUES ($lcid, currval ('cmailstore_id_seq'), $pmail, $receiver, $tid, 'N', $now); ";
 
-           # (Mailstore_ID, TicketID) must be unique
+           # Note: Tuple (CID, RID, TicketID) must be unique
            $tid = ($tid + 1) & 0x0fffffff;
        }
 
@@ -257,12 +268,9 @@ sub quarantine_mail {
 
     eval {
        if ($lcid) {
-           if ($qtype eq 'V') {
-               mkpath "$spooldir/cluster/$lcid/virus";
-           } else {
-               mkpath "$spooldir/cluster/$lcid/spam";
-           }
-           ($fh, $uid, $path) = new_fileid ($spooldir, "cluster/$lcid/$subpath");
+           my $subdir = "cluster/$lcid/$subpath";
+           mkpath $subdir;
+           ($fh, $uid, $path) = new_fileid ($spooldir, $subdir);
        } else {
            ($fh, $uid, $path) = new_fileid ($spooldir, $subpath);
        }
@@ -350,9 +358,9 @@ sub _new_mime_parser {
 
     # Create and set the output directory:
     (-d $self->{dumpdir} || mkdir ($self->{dumpdir} ,0755)) ||
-       croak "can't create $self->{dumpdir}: $! : ERROR";
+       die "can't create $self->{dumpdir}: $! : ERROR";
     (-w $self->{dumpdir}) ||
-       croak "can't write to directory $self->{dumpdir}: $! : ERROR";
+       die "can't write to directory $self->{dumpdir}: $! : ERROR";
 
     $parser->output_dir($self->{dumpdir});
 
@@ -371,7 +379,7 @@ sub parse_mail {
 
     eval {
        if (!($entity = $parser->read($self->{fh}))) {
-           croak "$self->{logid}: unable to parse message: ERROR";
+           die "$self->{logid}: unable to parse message: ERROR";
        }
     };