diff -r 000000000000 -r e05dded1b622 log.pl --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/log.pl Thu Aug 18 00:08:49 2005 +0000 @@ -0,0 +1,198 @@ +#! /usr/bin/perl +# $Id$ +# $URL$ +my $USAGE = <<'#'; +$ME [options] + --[no]db insert into log database [$opt_db] + --[no]mail send mails to @config::mailto [$opt_mail] + --message=s message +# + +use strict; +use warnings; +use File::Basename; +use File::Temp qw(tempfile); +use File::stat; +use Getopt::Long; +use Mail::Mailer; +use DBI; + +use lib "/etc/logbuch"; +use config; + +# print @config::mailto, "\n"; + +#+-------+---------------+------+-----+---------+----------------+ +#| Field | Type | Null | Key | Default | Extra | +#+-------+---------------+------+-----+---------+----------------+ +#| id | int(11) | | MUL | NULL | auto_increment | +#| host | varchar(255) | YES | | NULL | | +#| date | datetime | YES | | NULL | | +#| user | varchar(255) | YES | | NULL | | +#| mailto| varchar(255) | YES | | NULL | | +#| text | text | YES | MUL | NULL | | +#| stamp | timestamp(14) | YES | | NULL | | +#+-------+---------------+------+-----+---------+----------------+ + +my $ME = basename $0; + +my $DSN = "DBI:mysql:logbuch:pu.schlittermann.de"; +my $USER = "logbuch"; +my $PW = "HIDDEN"; + +my $LOG = "$ENV{HOME}/LOG"; +my $EDITOR = $ENV{VISUAL} || $ENV{EDITOR} || "vim"; +my $MAGIC = "#--- all changes below are ignored ---#\n"; + +my $opt_db = 1; +my $opt_mail = 1; +my $opt_message = ""; + + + +my $Dbh; + +sub identity(); +sub hostname(); +sub mailto(); + +MAIN: { + + GetOptions("db!" => \$opt_db, + "mail!" => \$opt_mail, + "message=s" => \$opt_message) + or die eval "\"$USAGE\""; + + if ($opt_message =~ /^\.?\// and -f $opt_message) { + @ARGV = ($opt_message); + $opt_message = join "", <>; + } elsif ($opt_message eq "-") { + $opt_message = join "", ; + } + + if ($opt_message =~ /\n/) { + $opt_message =~ s/\n/\n /g; + } + + if ($opt_db) { + $Dbh = DBI->connect($DSN, $USER, $PW, {RaiseError => 1}) + or die $DBI::errstr; + END { $Dbh->disconnect() if $Dbh; } + } + + # Temporärfile öffnen + my ($fh, $file); + END { unlink $file if $file; } + ($fh, $file) = tempfile(DIR => "/tmp"); + + # Kopftext eintragen + print $fh + "Date: ", scalar(localtime()), "\n", + "User: ", identity(), "\n", + "MailTo: ", mailto(), "\n", + "\n", + " * $opt_message", + "\n", + "\n", $MAGIC, "\n"; + + if (!-e $LOG) { + open(X, $_ = ">>$LOG") or die "Can't open $_: $!\n"; + close X; + }; + + open(IN, $_ = $LOG) or die "Can't open $_: $!\n"; + print $fh ; + close IN; + + if (!$opt_message) { + my $stamp = stat($file)->mtime(); + system($EDITOR, "+5", $file); + + if ($stamp == stat($file)->mtime()) { + print STDERR "Nothing changed. Discarding the note.\n"; + unlink $file; + exit 0; + } + } + + # Jetzt wie versprochen den (eventuell geänderten Rest) aus der + # Temp-Datei wegschneiden + { + my ($date, $user, $head, $text, $mailto); + my $pos; + + seek $fh, 0, 0; + for($pos = tell $fh; defined($_ = <$fh>); $pos = tell $fh) { + + $head .= "$_" if not $text and /^\S+:/; + + /^Date:\s+(.*)/ and $date = $1, next; + /^User:\s+(.*)/ and $user = $1, next; + /^MailTo:\s(.*)/ and $mailto = $1, next; + last if $_ eq $MAGIC; + + $text .= $_ if /\S/ || $text; # somit werden die ersten Leerzeilen übersprungen + } + + $text =~ s/\s*$//s; # Leerzeichen am Ende weg + + truncate $fh, $pos; + seek $fh, 0, 2; + + if ($opt_db) { + my $sth = $Dbh->prepare(" + INSERT INTO log (host, date, user, mailto, text) + VALUES(?, now(), ?, ?, ?)"); + $sth->execute(hostname(), $user, $mailto, $text); + print STDERR "Database entry inserted\n"; + } + + if ($opt_mail and $mailto) { + my $mailer = new Mail::Mailer "sendmail" + or die "Can't create Mailer: $!\n"; + + my $subject = (split /\n/, $text)[0]; + $subject =~ s/^\s*\S\s//; + $subject = "Service [". hostname(). "]: $subject"; + + $mailer->open({ + "To" => $mailto, + "Subject" => $subject}); + print $mailer $head, "\n", $text; + close $mailer; + print STDERR "Mail sent (to $mailto).\n"; + } + } + + # Und jetzt das aus der alten Datei dort anhängen + open(IN, $_ = $LOG) or die "Can't open $_: $!\n"; + print $fh ; + close $fh; + close IN; + + rename $file, $LOG; + +} + +sub identity() +{ + my $user = `who am i`; + chomp $user; + $user .= " [" . ($ENV{REMOTE_USER} || "-") . "]"; + return $user; +} + +sub hostname() +{ + my $r = `hostname -f`; + chomp($r); + return $r; +} + +sub mailto() +{ + return join(", ", @config::mailto); +} + +# vim:sts=4 sw=4 aw ai sm: +