- fixed configure.pl and makeDist.
[BackupPC.git] / bin / BackupPC_sendEmail
index 2a7184c..8357bdc 100755 (executable)
@@ -1,4 +1,4 @@
-#!/bin/perl -T
+#!/bin/perl
 #============================================================= -*-perl-*-
 #
 # BackupPC_sendEmail: send status emails to users and admins
 #============================================================= -*-perl-*-
 #
 # BackupPC_sendEmail: send status emails to users and admins
@@ -13,7 +13,7 @@
 #   Craig Barratt  <cbarratt@users.sourceforge.net>
 #
 # COPYRIGHT
 #   Craig Barratt  <cbarratt@users.sourceforge.net>
 #
 # COPYRIGHT
-#   Copyright (C) 2001  Craig Barratt
+#   Copyright (C) 2001-2003  Craig Barratt
 #
 #   This program is free software; you can redistribute it and/or modify
 #   it under the terms of the GNU General Public License as published by
 #
 #   This program is free software; you can redistribute it and/or modify
 #   it under the terms of the GNU General Public License as published by
 #
 #========================================================================
 #
 #
 #========================================================================
 #
-# Version 1.6.0_CVS, released 10 Dec 2002.
+# Version 3.0.0alpha, released 23 Jan 2006.
 #
 # See http://backuppc.sourceforge.net.
 #
 #========================================================================
 
 use strict;
 #
 # See http://backuppc.sourceforge.net.
 #
 #========================================================================
 
 use strict;
+no  utf8;
 use lib "/usr/local/BackupPC/lib";
 use BackupPC::Lib;
 use BackupPC::FileZIO;
 use lib "/usr/local/BackupPC/lib";
 use BackupPC::Lib;
 use BackupPC::FileZIO;
@@ -45,20 +46,22 @@ use BackupPC::FileZIO;
 use Data::Dumper;
 use Getopt::Std;
 use DirHandle ();
 use Data::Dumper;
 use Getopt::Std;
 use DirHandle ();
+use vars qw($Lang $TopDir $BinDir $LogDir %Conf);
 
 die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) );
 
 die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) );
-my $TopDir = $bpc->TopDir();
-my $BinDir = $bpc->BinDir();
-my %Conf   = $bpc->Conf();
+$TopDir = $bpc->TopDir();
+$LogDir = $bpc->LogDir();
+$BinDir = $bpc->BinDir();
+%Conf   = $bpc->Conf();
+$Lang   = $bpc->Lang();
 
 $bpc->ChildInit();
 
 use vars qw(%UserEmailInfo);
 
 $bpc->ChildInit();
 
 use vars qw(%UserEmailInfo);
-do "$TopDir/log/UserEmailInfo.pl";
+do "$LogDir/UserEmailInfo.pl";
 
 my %opts;
 
 my %opts;
-getopts("t", \%opts);
-if ( @ARGV != 0 ) {
+if ( !getopts("t", \%opts) || @ARGV != 0 ) {
     print("usage: $0 [-t]\n");
     exit(1);
 }
     print("usage: $0 [-t]\n");
     exit(1);
 }
@@ -80,7 +83,8 @@ my $mesg = "";
 
 my @badHosts = ();
 foreach my $host ( sort(keys(%Status)) ) {
 
 my @badHosts = ();
 foreach my $host ( sort(keys(%Status)) ) {
-    next if ( $Status{$host}{reason} ne "backup failed"
+    next if ( ($Status{$host}{reason} ne "Reason_backup_failed"
+               && $Status{$host}{reason} ne "Reason_restore_failed")
            || $Status{$host}{error} =~ /^lost network connection to host/ );
     push(@badHosts, "$host ($Status{$host}{error})");
 }
            || $Status{$host}{error} =~ /^lost network connection to host/ );
     push(@badHosts, "$host ($Status{$host}{error})");
 }
@@ -119,7 +123,7 @@ my @oldDirs = ();
 my @files = $d->read;
 $d->close;
 foreach my $host ( @files ) {
 my @files = $d->read;
 $d->close;
 foreach my $host ( @files ) {
-    next if ( $host eq "." || $host eq ".." || defined($Status{$host}) );
+    next if ( $host =~ /^\./ || defined($Status{$host}) );
     push(@oldDirs, "$TopDir/pc/$host");
 }
 if ( @oldDirs ) {
     push(@oldDirs, "$TopDir/pc/$host");
 }
 if ( @oldDirs ) {
@@ -137,10 +141,12 @@ EOF
 }
 
 if ( $mesg ne "" && $Conf{EMailAdminUserName} ne "" ) {
 }
 
 if ( $mesg ne "" && $Conf{EMailAdminUserName} ne "" ) {
+    my $headers = $Conf{EMailHeaders};
+    $headers .= "\n" if ( $headers !~ /\n$/ );
     $mesg = <<EOF;
 To: $Conf{EMailAdminUserName}
 Subject: BackupPC administrative attention needed
     $mesg = <<EOF;
 To: $Conf{EMailAdminUserName}
 Subject: BackupPC administrative attention needed
-
+$headers
 ${mesg}Regards,
 PC Backup Genie
 EOF
 ${mesg}Regards,
 PC Backup Genie
 EOF
@@ -167,11 +173,17 @@ foreach my $host ( sort(keys(%Status)) ) {
     my $user = $Hosts->{$host}{user};
     next if ( time - $UserEmailInfo{$user}{lastTime}
                         < $Conf{EMailNotifyMinDays} * 24*3600 );
     my $user = $Hosts->{$host}{user};
     next if ( time - $UserEmailInfo{$user}{lastTime}
                         < $Conf{EMailNotifyMinDays} * 24*3600 );
+    next if ($Conf{XferMethod} eq "archive" );
     my @Backups = $bpc->BackupInfoRead($host);
     my $numBackups = @Backups;
     if ( $numBackups == 0 ) {
     my @Backups = $bpc->BackupInfoRead($host);
     my $numBackups = @Backups;
     if ( $numBackups == 0 ) {
-        my $subj = "BackupPC: no backups of $host have succeeded";
-        sendUserEmail($user, $host, $Conf{EMailNoBackupEverMesg}, $subj, {
+        my $subj = defined($Conf{EMailNoBackupEverSubj})
+                       ? $Conf{EMailNoBackupEverSubj}
+                       : $Lang->{EMailNoBackupEverSubj};
+        my $mesg = defined($Conf{EMailNoBackupEverMesg})
+                       ? $Conf{EMailNoBackupEverMesg}
+                       : $Lang->{EMailNoBackupEverMesg};
+        sendUserEmail($user, $host, $mesg, $subj, {
                             userName => user2name($user)
                         }) if ( !defined($Jobs{$host}) );
         next;
                             userName => user2name($user)
                         }) if ( !defined($Jobs{$host}) );
         next;
@@ -208,8 +220,8 @@ foreach my $host ( sort(keys(%Status)) ) {
         while ( 1 ) {
             my $s = $fh->readLine();
             last if ( $s eq "" );
         while ( 1 ) {
             my $s = $fh->readLine();
             last if ( $s eq "" );
-            if ( $s =~ /^Error reading file.*\.pst : ERRDOS - ERRlock/
-                  || $s =~ /^Error reading file.*\.pst\. Got 0 bytes/ ) {
+            if ( $s =~ /^\s*Error reading file.*\.pst : (ERRDOS - ERRlock|NT_STATUS_FILE_LOCK_CONFLICT)/
+                  || $s =~ /^\s*Error reading file.*\.pst\. Got 0 bytes/ ) {
                 $badOutlook = 1;
                 last;
             }
                 $badOutlook = 1;
                 last;
             }
@@ -222,11 +234,16 @@ foreach my $host ( sort(keys(%Status)) ) {
         }
     }
     if ( time - $last > $Conf{EMailNotifyOldBackupDays} * 24*3600 ) {
         }
     }
     if ( time - $last > $Conf{EMailNotifyOldBackupDays} * 24*3600 ) {
-        my $subj = "BackupPC: no recent backups on $host";
+        my $subj = defined($Conf{EMailNoBackupRecentSubj})
+                       ? $Conf{EMailNoBackupRecentSubj}
+                       : $Lang->{EMailNoBackupRecentSubj};
+        my $mesg = defined($Conf{EMailNoBackupRecentMesg})
+                       ? $Conf{EMailNoBackupRecentMesg}
+                       : $Lang->{EMailNoBackupRecentMesg};
         my $firstTime = sprintf("%.1f",
                         (time - $Backups[0]{startTime}) / (24*3600));
         my $days = sprintf("%.1f", (time - $last) / (24 * 3600));
         my $firstTime = sprintf("%.1f",
                         (time - $Backups[0]{startTime}) / (24*3600));
         my $days = sprintf("%.1f", (time - $last) / (24 * 3600));
-        sendUserEmail($user, $host, $Conf{EMailNoBackupRecentMesg}, $subj, {
+        sendUserEmail($user, $host, $mesg, $subj, {
                             firstTime  => $firstTime,
                             days       => $days,
                             userName   => user2name($user),
                             firstTime  => $firstTime,
                             days       => $days,
                             userName   => user2name($user),
@@ -239,22 +256,29 @@ foreach my $host ( sort(keys(%Status)) ) {
                                              * 24 * 3600 ) {
         my($days, $howLong);
         if ( $lastGoodOutlook == 0 ) {
                                              * 24 * 3600 ) {
         my($days, $howLong);
         if ( $lastGoodOutlook == 0 ) {
-            $howLong = "not been backed up successfully";
+            $howLong = eval("qq{$Lang->{howLong_not_been_backed_up}}");
         } else {
             $days = sprintf("%.1f", (time - $lastGoodOutlook) / (24*3600));
         } else {
             $days = sprintf("%.1f", (time - $lastGoodOutlook) / (24*3600));
-            $howLong = "not been backed up for $days days";
+            $howLong = eval("qq{$Lang->{howLong_not_been_backed_up_for_days_days}}");
         }
         }
-        my $subj = "BackupPC: Outlook files on $host need to be backed up";
+        my $subj = defined($Conf{EMailOutlookBackupSubj})
+                       ? $Conf{EMailOutlookBackupSubj}
+                       : $Lang->{EMailOutlookBackupSubj};
+        my $mesg = defined($Conf{EMailOutlookBackupMesg})
+                       ? $Conf{EMailOutlookBackupMesg}
+                       : $Lang->{EMailOutlookBackupMesg};
         my $firstTime = sprintf("%.1f",
                         (time - $Backups[0]{startTime}) / (24*3600));
         my $lastTime = sprintf("%.1f",
                         (time - $Backups[$#Backups]{startTime}) / (24*3600));
         my $firstTime = sprintf("%.1f",
                         (time - $Backups[0]{startTime}) / (24*3600));
         my $lastTime = sprintf("%.1f",
                         (time - $Backups[$#Backups]{startTime}) / (24*3600));
-        sendUserEmail($user, $host, $Conf{EMailOutlookBackupMesg}, $subj, {
+        sendUserEmail($user, $host, $mesg, $subj, {
+                            days       => $days,
                             firstTime  => $firstTime,
                             lastTime   => $lastTime,
                             numBackups => $numBackups,
                             userName   => user2name($user),
                             howLong    => $howLong,
                             firstTime  => $firstTime,
                             lastTime   => $lastTime,
                             numBackups => $numBackups,
                             userName   => user2name($user),
                             howLong    => $howLong,
+                            serverHost => $Conf{ServerHost},
                         }) if ( !defined($Jobs{$host}) );
     }
 }
                         }) if ( !defined($Jobs{$host}) );
     }
 }
@@ -263,7 +287,8 @@ if ( !$opts{t} ) {
     my $dumpStr = Data::Dumper->Dump(
              [\%UserEmailInfo],
              [qw(*UserEmailInfo)]);
     my $dumpStr = Data::Dumper->Dump(
              [\%UserEmailInfo],
              [qw(*UserEmailInfo)]);
-    if ( open(HOST, ">$TopDir/log/UserEmailInfo.pl") ) {
+    if ( open(HOST, ">", "$LogDir/UserEmailInfo.pl") ) {
+       binmode(HOST);
         print(HOST $dumpStr);
         close(HOST);
     }
         print(HOST $dumpStr);
         close(HOST);
     }
@@ -281,10 +306,15 @@ sub user2name
 sub sendUserEmail
 {
     my($user, $host, $mesg, $subj, $vars) = @_;
 sub sendUserEmail
 {
     my($user, $host, $mesg, $subj, $vars) = @_;
-    $vars->{user} = $user;
-    $vars->{host} = $host;
-    $vars->{subj} = $subj;
-    $mesg =~ s/\$(\w+)/defined($vars->{$1}) ? $vars->{$1} : \$$1/eg;
+    $vars->{user}     = $user;
+    $vars->{host}     = $host;
+    $vars->{headers}  = $Conf{EMailHeaders};
+    $vars->{headers} .= "\n" if ( $vars->{headers} !~ /\n$/ );
+    $vars->{domain}   = $Conf{EMailUserDestDomain};
+    $vars->{CgiURL}   = $Conf{CgiURL};
+    $subj =~ s/\$(\w+)/defined($vars->{$1}) ? $vars->{$1} : "\$$1"/eg;
+    $vars->{subj}   = $subj;
+    $mesg =~ s/\$(\w+)/defined($vars->{$1}) ? $vars->{$1} : "\$$1"/eg;
     if ( $opts{t} ) {
         print("#" x 75, "\n");
         print $mesg;
     if ( $opts{t} ) {
         print("#" x 75, "\n");
         print $mesg;