log.pl
changeset 1 a918a61afe98
parent 0 e05dded1b622
child 2 b1954e2f261c
equal deleted inserted replaced
0:e05dded1b622 1:a918a61afe98
     1 #! /usr/bin/perl 
     1 #! /usr/bin/perl 
     2 # $Id$
     2 # $Id$
     3 # $URL$
     3 # $URL$
     4 my $USAGE = <<'#';
     4 my $USAGE = <<'#';
     5 $ME [options]
     5 $ME [options]
     6     --[no]db  insert into log database [$opt_db]
     6     --[no]db	insert into log database [$opt_db]
     7     --[no]mail send mails to @config::mailto [$opt_mail]
     7     --[no]mail	send mails to @config::mailto [$opt_mail]
     8     --message=s message
     8     --message=s message
       
     9                 if the message looks like a file name (e.g.:
       
    10 		./text or /tmp/text, the message is read
       
    11 		from this file)
       
    12              
     9 #
    13 #
    10 
    14 
    11 use strict;
    15 use strict;
    12 use warnings;
    16 use warnings;
    13 use File::Basename;
    17 use File::Basename;
    78 	$Dbh = DBI->connect($DSN, $USER, $PW, {RaiseError => 1})
    82 	$Dbh = DBI->connect($DSN, $USER, $PW, {RaiseError => 1})
    79 	    or die $DBI::errstr;
    83 	    or die $DBI::errstr;
    80 	END { $Dbh->disconnect() if $Dbh; }
    84 	END { $Dbh->disconnect() if $Dbh; }
    81     }
    85     }
    82 
    86 
    83     # Temporärfile öffnen
    87     # Temporärfile öffnen
    84     my ($fh, $file);
    88     my ($fh, $file);
    85     END { unlink $file if $file; }
    89     END { unlink $file if $file; }
    86     ($fh, $file) = tempfile(DIR => "/tmp");
    90     ($fh, $file) = tempfile(DIR => "/tmp");
    87 
    91 
    88     # Kopftext eintragen
    92     # Kopftext eintragen
   113 	    unlink $file;
   117 	    unlink $file;
   114 	    exit 0;
   118 	    exit 0;
   115 	}
   119 	}
   116     }
   120     }
   117 
   121 
   118     # Jetzt wie versprochen den (eventuell geänderten Rest) aus der
   122     # Jetzt wie versprochen den (eventuell geänderten Rest) aus der
   119     # Temp-Datei wegschneiden
   123     # Temp-Datei wegschneiden
   120     {
   124     {
   121 	my ($date, $user, $head, $text, $mailto);
   125 	my ($date, $user, $head, $text, $mailto);
   122 	my $pos;
   126 	my $pos;
   123 
   127 
   129 	    /^Date:\s+(.*)/ and $date = $1, next;
   133 	    /^Date:\s+(.*)/ and $date = $1, next;
   130 	    /^User:\s+(.*)/ and $user = $1, next;
   134 	    /^User:\s+(.*)/ and $user = $1, next;
   131 	    /^MailTo:\s(.*)/ and $mailto = $1, next;
   135 	    /^MailTo:\s(.*)/ and $mailto = $1, next;
   132 	    last if $_ eq $MAGIC;
   136 	    last if $_ eq $MAGIC;
   133 
   137 
   134 	    $text .= $_ if /\S/ || $text;   # somit werden die ersten Leerzeilen übersprungen
   138 	    $text .= $_ if /\S/ || $text;   # somit werden die ersten Leerzeilen übersprungen
   135 	}
   139 	}
   136 
   140 
   137 	$text  =~ s/\s*$//s; # Leerzeichen am Ende weg
   141 	$text  =~ s/\s*$//s; # Leerzeichen am Ende weg
   138 
   142 
   139 	truncate $fh, $pos;
   143 	truncate $fh, $pos;
   162 	    close $mailer;
   166 	    close $mailer;
   163 	    print STDERR "Mail sent (to $mailto).\n";
   167 	    print STDERR "Mail sent (to $mailto).\n";
   164 	}
   168 	}
   165     }
   169     }
   166 
   170 
   167     # Und jetzt das aus der alten Datei dort anhängen
   171     # Und jetzt das aus der alten Datei dort anhängen
   168     open(IN, $_ = $LOG) or die "Can't open $_: $!\n";
   172     open(IN, $_ = $LOG) or die "Can't open $_: $!\n";
   169     print $fh <IN>;
   173     print $fh <IN>;
   170     close $fh;
   174     close $fh;
   171     close IN;
   175     close IN;
   172 
   176