#!/opt/bin/perl # ######################################################################### # # # Program for storing e-mail problem reports with automatic receipt # # # ######################################################################### # # # Version 1.4 - Written 11.02.96 by Steffen Beyer # # # ######################################################################### # # # Copyright (C) 1995 by software design & management GmbH & Co. KG # # # ######################################################################### # # # Configure here the name of your mail folder(s): # # (MUST be an ABSOLUTE path!!!) # # # $main_folder = '/u/sysadm/preport'; # # # $notes_folder = '/u/sysadm/notes'; # # # ######################################################################### # # Default settings: # $version = 'version 1.4'; # $self = $0; $self =~ s!^.*/!!; # $lockfile = "$main_folder.lock"; # $mailfile = "$main_folder.mail"; $tempfile = "$main_folder.temp"; # $ticket = "$main_folder.ticket"; # ######################################################################### # # Wait for semaphore: # $wait = "true"; # while ($wait) { # # Wait for semaphore to be released (if set): # while (-f $lockfile) { sleep(5); } # # When semaphore is gone, create own one: # unless (open(LOCKFILE, ">>$lockfile")) { unlink($lockfile); die "$self: can't write semaphore '$lockfile': $!\n"; } print LOCKFILE "$$\n"; close(LOCKFILE); # # Check wether semaphore is our's or was created # in the meantime by another copy of this process # unless (open(LOCKFILE, "<$lockfile")) { unlink($lockfile); die "$self: can't read semaphore '$lockfile': $!\n"; } $_ = ; chop; close(LOCKFILE); # # Loop if semaphore isn't our's: # $wait = ($_ != $$); if ($wait) { warn "Semaphore access conflict detected!\n"; warn "Waiting for other process to terminate.\n"; } } # # Get next ticket number: # $ticket_no = ''; # unless (open(TICKET, "<$ticket")) { unlink($lockfile); die "$self: can't read '$ticket': $!\n"; } # while () { chop; if (/^\d+$/) { $ticket_no = $_; } } # close(TICKET); # if ($ticket_no eq '') { unlink($lockfile); die "$self: no ticket number in '$ticket'!\n"; } # $ticket_no++; # if ($ticket_no > 9999) { $ticket_no = '0000'; } # unless (open(TICKET, ">$ticket")) { unlink($lockfile); die "$self: can't write '$ticket': $!\n"; } # print TICKET "$ticket_no\n"; # close(TICKET); # # Store and analyze incoming problem report: # $priority = 0; # $subject = ''; $reply_address = ''; $real_address = ''; $logic_address = ''; # unless (open(TEMPFILE, ">$tempfile")) { unlink($tempfile); unlink($lockfile); die "$self: can't write '$tempfile': $!\n"; } # while () { s![\r\n\f\t]+$!!; if (/^Eingegangen am:/) { $date = `date`; chop($date); $_ = "Eingegangen am: $date"; } print TEMPFILE "$_\n"; if (/^\s*\[\s*\S+\s*\]\s+gering/) { if ($priority < 1) { $priority = 1; } } if (/^\s*\[\s*\S+\s*\]\s+normal/) { if ($priority < 2) { $priority = 2; } } if (/^\s*\[\s*\S+\s*\]\s+dringend/) { if ($priority < 3) { $priority = 3; } } if ((/^Priority:/) || (/^\s*\[\s*\S+\s*\]\s+extrem wichtig/)) { $urgent = 1; if ($priority < 4) { $priority = 4; } } if (/^\s*\[\s*\S+\s*\]\s.*Bestellung/) { $buyorder = 1; } if (/^\s*\[\s*\S+\s*\]\s.*Lotus Notes/) { $lotus_notes = 1; } if (($subject eq '') && (/^Subject:\s*/)) { $subject = $'; } if (($reply_address eq '') && (/^Reply-To:\s+(\S+@\S+)/)) { $reply_address = $1; } if (($real_address eq '') && (/^From:\s+(\S+@\S+)/)) { $real_address = $1; } if (($logic_address eq '') && (/^From\s+(\S+@\S+)/)) { $logic_address = $1; } } # close(TEMPFILE); # if ($subject eq '') { if ($buyorder) { $subject = "Bestellung"; } else { $subject = "Problemreport"; } } if ($reply_address eq $real_address) { $real_address = ''; } if (($reply_address eq $logic_address) || ($real_address eq $logic_address)) { $logic_address = ''; } # # Append incoming problem report to appropriate folder: # $folder = $main_folder; # if ($lotus_notes) { $folder = $notes_folder; } # unless (open(TEMPFILE, "<$tempfile")) { # unlink($tempfile); # don't delete it in case we still need it unlink($lockfile); die "$self: can't read '$tempfile': $!\n"; } # unless (open(FOLDER, ">>$folder")) { # unlink($tempfile); # don't delete it in case we still need it unlink($lockfile); die "$self: can't append to '$folder': $!\n"; } # while () { if (/^Subject:/) { $_ = "Subject: $priority $ticket_no $subject\n"; } print FOLDER; } # close(FOLDER); # close(TEMPFILE); # # If signed as urgent or buy order, send special notification mail: # if ($urgent || $buyorder) { unless (open(MAILFILE, ">$mailfile")) { unlink($mailfile); unlink($tempfile); unlink($lockfile); die "$self: can't write '$mailfile': $!\n"; } if ($buyorder) { print MAILFILE "From: sysadm@bi.sdm.de (Bestellung)\n"; if ($urgent) { print MAILFILE "To: BESTELLUNG, URGENT_PREPORT\n"; } else { print MAILFILE "To: BESTELLUNG\n"; } } else { print MAILFILE "From: sysadm@bi.sdm.de (Problem Report)\n"; print MAILFILE "To: URGENT_PREPORT\n"; } if ($lotus_notes) { print MAILFILE "Cc: MIS_ADM_MUC, MIS_ADM_ETC\n"; } if ($urgent) { print MAILFILE "Priority: Urgent\n"; } print MAILFILE "Subject: $priority $ticket_no $subject\n\n"; $user = ''; if ($reply_address ne '') { $user = $reply_address; } if ($real_address ne '') { if ($user ne '') { $user .= ', '; } $user .= $real_address; } if ($logic_address ne '') { if ($user ne '') { $user .= ', '; } $user .= $logic_address; } if ($buyorder) { if ($urgent) { print MAILFILE "Dringende "; } print MAILFILE "Bestellung"; } else { print MAILFILE "Dringender Problemreport"; } if ($user ne '') { print MAILFILE "\n\nvon $user"; } print MAILFILE ":\n\n"; # unless (open(TEMPFILE, "<$tempfile")) { unlink($tempfile); close(MAILFILE); unlink($mailfile); unlink($lockfile); die "$self: can't read '$tempfile': $!\n"; } $copy = 0; while () { if ($copy) { print MAILFILE; } else { if (/^\s*$/) { $copy = 1; } } } close(TEMPFILE); # print MAILFILE <<"VERBATIM"; -- $self $version sysadm@bi.sdm.de VERBATIM # close(MAILFILE); # system("/bin/cat $mailfile | /opt/bin/sendmail -t"); # unlink($mailfile); } # # Delete temporary copy of incoming mail: # unlink($tempfile); # # If address was found and no error abort so far, send acknowledgement mail: # if (($reply_address ne '') || ($real_address ne '') || ($logic_address ne '')) { unless (open(MAILFILE, ">$mailfile")) { unlink($mailfile); unlink($lockfile); die "$self: can't write '$mailfile': $!\n"; } # if ($buyorder) { print MAILFILE "From: sysadm@bi.sdm.de (Bestellung)\n"; } else { print MAILFILE "From: sysadm@bi.sdm.de (Problem Report)\n"; } if ($reply_address ne '') { print MAILFILE "To: $reply_address\n"; if ($real_address ne '') { if ($logic_address ne '') { print MAILFILE "Cc: $real_address, $logic_address\n"; } else { print MAILFILE "Cc: $real_address\n"; } } else { if ($logic_address ne '') { print MAILFILE "Cc: $logic_address\n"; } } } elsif ($real_address ne '') { print MAILFILE "To: $real_address\n"; if ($logic_address ne '') { print MAILFILE "Cc: $logic_address\n"; } } else { print MAILFILE "To: $logic_address\n"; } if ($buyorder) { print MAILFILE <<"VERBATIM"; Subject: Bestaetigung: Bestellung erhalten! Deine Bestellung wurde registriert. VERBATIM } else { print MAILFILE <<"VERBATIM"; Subject: Bestaetigung: Problemreport erhalten! Dein Problemreport wurde registriert. VERBATIM } print MAILFILE <<"VERBATIM"; Registrierungs-Nummer: $ticket_no (Bitte bei allen Rueckfragen angeben!) Dein Subject war: $subject Angekreuzte Prioritaet: $priority (0 = keine Angabe, 1 = niedrigste, 4 = hoechste Prioritaet) Eingangs-Datum und -Uhrzeit: $date Im Moment befinden sich VERBATIM unshift(@INC, '/opt/lib/problem'); require "analyze.pl"; @count = &analyze; $sum = 0; for ( $i = $#count; $i >= 0; $i-- ) { printf(MAILFILE "%5s Problem Reports oder Bestellungen mit Prioritaet $i\n", $count[$i]) if ($count[$i] > 0); $sum += $count[$i]; } print MAILFILE <<"VERBATIM"; (also insgesamt $sum Problem Reports oder Bestellungen) in der Warteschlange oder in Arbeit. Diese Bestaetigung wird automatisch verschickt. Mit freundlichen Gruessen, Eure System-Administration. -- $self $version sysadm@bi.sdm.de VERBATIM # close(MAILFILE); # system("/bin/cat $mailfile | /opt/bin/sendmail -t"); # unlink($mailfile); } # # Release semaphore: # unlink($lockfile); # # Done. #