#!/usr/bin/perl -w ########################################################################### ########################################################################### ## Another AutoResponder Gone Haywire (aargh) ## ## This program design by moon@clan-io.net. Rewritten by Ben Hartshorne ## ## (ben@sandwich.net) because he was procrastinating for final exams. ## ## Bugs, comments, suggestions to ben-aargh@adphi.loyalty.org. ## ## Copyright 1999, Ben Hartshorne ## ## ## ## 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 the Free Software Foundation; either version 2 ## ## of the License, or (at your option) any later version. ## ## ## ## This program is distributed in the hope that it will be useful, ## ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## ## GNU General Public License for more details. ## ## ## ## You should have received a copy of the GNU General Public License ## ## along with this program; if not, write to the Free Software ## ## Foundation, Inc., ## ## 59 Temple Place - Suite 330, ## ## Boston, MA 02111-1307, USA. ## ########################################################################### ########################################################################### use Env qw(USER); my $VERSION=1.1; my $SENDMAIL = "/usr/sbin/sendmail"; my ($userHomeDir, $userAutorespondDir, $userRealName, $userEmailAddress); my ($bounceAddress, $replyFileName, @replyText); my $terseOutput = 0; #How much do you want to hear from me? my $userSettingsFile; my $writeUserConfFile = 1; my $date = `date +%Y%m%d%H%M%S`; while ($_ = shift and /^-/) { #pull each flag one at a time last if /^--$/; # end if there is a -- by itself if (/^-d/) { $debug = 1;next;} if (/^--debug/) { $debug = 1;next;} if (/^-h/) { $help = 1;next;} if (/^--help/) { $help = 1;next;} if (/^-V/) { $version = 1;next;} if (/^--version/) { $version = 1;next;} $usage = 1; } if ($usage){ print "USAGE: aargh [-hdV]\n"; } if ($debug){ warn "Debugging is On.\n"; } if ($version){ print "Aargh Version = $VERSION\n"; exit; } if ($help){ &printHelp(); exit; } ############################################# #### EDIT THIS LINE TO POINT TO CONF FILE ############################################# $systemConfigFile = "/etc/aargh.conf"; $alternateConfigFile = "aargh.conf"; ######################################################################## ########### Here is the entire program, start to finish ############## ######################################################################## &printWelcome(); &setup(); &getBounceAddress(); &getReplyText(); &verifyEverythingIsCorrect(); &writeReplyFile(); &finalReport(); ################################ Functions ############################## ### printHelp() ### prints out the help text. sub printHelp { print <); } } warn "my name is $USER" if $debug; &loadDefaultSettings(); ###Load all sorts of fun defaults. if (&loadUserSettings()){ ### overload the default settings with user specifics $writeUserConfFile = 0; ### if it does load, don't write a new one. } &checkForProcmail(); &checkSendmail(); &checkAutorespondDir(); &checkFullName(); &checkEmailAddress(); &checkTersness(); if ($writeUserConfFile) { &writeConfFile($userSettingsFile); } sub checkForProcmail{ if (-e "$userHomeDir/.forward" && -f "$userHomeDir/.forward" && -T "$userHomeDir/.forward") { unless (`grep procmail $userHomeDir/.forward`){ print "I\'m terribly sorry, but it seems you are not set up to use procmail.\nI suggest putting something like \n\n\"|IFS=\' \' && exec /usr/bin/procmail -f- || exit 75 \#$USER\"\n\nin a file called \".forward\" in your home directory. Note the quotes are required.\n\nThis is a fatal error.\n"; exit; } } else { #Thanks to Bjoern Frantzen for this patch to allow procmail as delivery agent. unless (`grep procmail /etc/sendmail.cf`) { print "I\'m terribly sorry, but it seems you are not set up to use procmail.\nI suggest putting something like \n\n\"|IFS=\' \' && exec /usr/bin/procmail -f- || exit 75 \#$USER\"\n\nin a file called \".forward\" in your home directory. Note the quotes are required.\n\nThis is a fatal error.\n"; exit; } } } sub checkSendmail { unless (-x $SENDMAIL) { print "I can't find sendmail. Where is it? (If you don't have sendmail,\n just give me the path to a comperable mail agent.) "; chop ($SENDMAIL = ); &checkSendmail(); } } sub checkAutorespondDir{ if (-d $userAutorespondDir){warn "aargh dir found at $userAutorespondDir" if $debug;} else { my $yesno = 'n'; my $dir = $userAutorespondDir; while($yesno eq 'n') { print "I have to make a directory for my home. I would like to call it $dir. Is that OK? (y/n/q) "; chop ($yesno = ); if ($yesno eq 'y') { system ("/bin/mkdir", $dir); } elsif ($yesno eq 'q') {print "\nSorry I couldn't help you.\nBye Bye!\n\n";exit} elsif ($yesno eq 'n') { print "Bummer. What would you like me to call it? "; chop ($dir = ); print "Allright, "; } } $userAutorespondDir = $dir; } } sub checkFullName { unless ($userRealName){ print "I don't know your real name. What is it? "; chop ($userRealName = ); $writeUserConfFile = 1; } } sub checkEmailAddress { unless ($userEmailAddress){ print "I don't know your e-mail address. What is it? "; chop ($userEmailAddress = ); $writeUserConfFile = 1; } } sub checkTersness { my $venTer; if ($writeUserConfFile) { print "Do you want me to be Verbose or Terse? (v/t) "; chop ($verTer = ); if ($verTer eq 'v') { $terseOutput = 0; } else { $terseOutput = 1; } } } } ### getBounceAddress() ### Ask the uer for the address to watch for to bounce things. sub getBounceAddress { my @bounceAddresses; print "To what e-mail address[es] would you like me to respond? "; print "\n This can be a fully or partially qualified e-mail address.\n" unless $terseOutput; print "You can enter several addresses as a comma seperated list, and\nit will match any one of them.\n" unless $terseOutput; chop ($bounceAddress = ); unless ($terseOutput) { unless ($bounceAddress =~ '@'){ print "I recommend you give a fully qualified address (user\@host.dom). Otherwise, y ou may respond to more people than you expect. is $bounceAddress OK? (y/n) "; chop ($yesno = ); if ($yesno eq 'n') { &getBounceAddress(); } } } if ($bounceAddress =~ ','){ $bounceAddress =~ s/,\W*/|/g; $bounceAddress = "{|" . $bounceAddress . "|}"; } warn "bounceAddress = $bounceAddress" if $debug; } ### getReplyText() ### Ask the user for the text to include in the bounce sub getReplyText { if ($terseOutput) { print "\nWhat response should I send?\n"; } else { print "\nYou must now enter the message that you would like bounced to $bounceAddress. End the message with a line with nothing but a single period. If you make a mistake, you should just edit the file created afterwards. I'll let you know what the name will be.\n"; } my $i = 0; LINE: while (1) { $line = ; last LINE if $line =~ /^\.$/; $replyText[$i] = $line; $i++; } $replyText[$i] = "\n-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n"; $replyText[$i + 1] = "This bounce created for $userRealName.\n$userEmailAddress\n\n"; warn "Reply Text is @replyText. it is $i lines long.\n" if $debug; } ### verifyEverythingIsCorrect() ### Ask if everything is right, cancel if it's not. sub verifyEverythingIsCorrect { if ($terseOutput) { print "I will reply with what's above, is that OK? (y/n) "; } else { print <); if ($yesno eq 'n'){ print "Are you sure? (y/n) "; chop ($yesno = ); if ($yesno eq 'y'){ print "Allright. I'm sorry I couldn't help you. It's been fun!\n\n"; exit; } } print "Great. "; } ### writeReplyFile() ### Go ahead and write the file to keep track of the bounce. sub writeReplyFile { print "I will now write it to $userAutorespondDir/$bounceAddress.reply.\n"; open(MYFILE, "+>$userAutorespondDir/$bounceAddress.proc"); print MYFILE <' -I'Precedence: Auto Reply' -A'X-Loop: $userEmailAddress' ; cat $userAutorespondDir/$bounceAddress.reply ;) | $SENDMAIL -t END_OF_PROCMAIL_RULE close(MYFILE); open(MYFILE, "+>$userAutorespondDir/$bounceAddress.reply"); print MYFILE @replyText; close(MYFILE); if (-e "$userHomeDir/.procmailrc") { `cp $userHomeDir/.procmailrc $userHomeDir/.procmailrc.bak.$date`; } `cat $userAutorespondDir/$bounceAddress.proc >> $userHomeDir/.procmailrc`; `chmod 644 $userHomeDir/.forward $userHomeDir/.procmailrc`; `rm $userAutorespondDir/$bounceAddress.proc`; } ### finalReport() ### Wrap things up, thank the user, and so on. sub finalReport { if ($terseOutput) { print "I'm done! Thanks for using my particular talents!\n\n-auto\n"; } else { print <$confFile")) { print CONF <); } $userSettingsFile = $userHomeDir . "/.aargh.conf"; $userAutorespondDir = $userHomeDir . "/.aargh"; warn "Values are: \n\thome directory = $userHomeDir \n\tsettings file = $userSettingsFile \n\tmy dir = $userHomeDir\n" if $debug; } } warn "userSettingsFile is $userSettingsFile" if $debug; sub readConfFile { my $confFile = shift; warn "Trying to read $confFile" if $debug; if (-e $confFile && #if file exists and -f $confFile && #it is i plain file and -T $confFile){ #it is a text file if (open CONF, $confFile) { warn "reading $confFile for defaults...\n" if $debug; while () { if (/^USER_HOME_DIR\s*(.*\$USER.*)$/) {$userHomeDir = $1 . $USER . $2;} if (/^USER_AUTORESPONCE_CONF_FILE\s*(.*\$USER.*)$/) {$userSettingsFile = $1 . $USER . $2;} if (/^USER_AUTORESPOND_DIR\s*(.*\$USER.*)$/) {$userAutorespondDir = $1 . $USER . $2;} if (/^SENDMAIL_PATH\s*(.*)$/) {$SENDMAIL = $1;} } close(CONF); } else { warn "I failed to open $confFile for reading, despite the fact that it exists.\n" if $debug; return 0; } #Tell my caller that I failed. } else { warn "I failed to open $confFile because it didn't test right.\n" if $debug; return 0; } return 1; } } ### loadUserSettings() ### Loads user specific settings from the argument passed ### Fills in ($userRealName, $userEmailAddress, $terse) with information. (if necessary) sub loadUserSettings { if (-e $userSettingsFile && #if file exists and -f $userSettingsFile && #it is i plain file and -T $userSettingsFile){ #it is a text file if (open CONF, $userSettingsFile) { warn "reading $userSettingsFile for user settings...\n" if $debug; while () { if (/^USER_REAL_NAME\s*(.*)$/) {$userRealName = $1;} if (/^USER_EMAIL_ADDRESS\s*(.*)$/) {$userEmailAddress = $1;} if (/^TERSE\s*YES$/) {$terseOutput = 1;} } close CONF; } else { warn "I failed to open $userSettingsFile for reading, despite the fact that it exists.\n" if $debug; return 0; } #Tell my caller that I failed. } else { warn "I failed to open $userSettingsFile because it didn't test right.\n" if $debug; return 0; } return 1; }