#!/usr/bin/perl -w # procmail setup script v.old&undated # Copyright 2001 Ben Livingston # Released under the GNU General Public License Version 2 # Viewable online at http://www.gnu.org/copyleft/gpl.html $feature = 0; $comma = 0; sub check_forward { if (!`grep '"| /usr/bin/procmail || exit 75 "' ../.forward`) { print "Would you like me to set up your .forward file? (Y/N) "; $temp = ; chomp $temp; if (lc($temp) eq "y") { print `echo '"| /usr/bin/procmail || exit 75 "' > /home/$ENV{'USER'}/.forward`; print "Your .forward is now in business! Press any key."; ; print `clear`; } } } sub check_pinerc { if (open(PINERC,"/home/$ENV{'USER'}/.pinerc")) { $pinerc_ok = 0; while () { chomp; if (/enable-incoming-folders/) { $pinerc_ok = 1; } } } close PINERC; if (!$pinerc_ok) { print "Pine is not set up for Procmail. Would you like me to set it up for you? "; $pinerc_setup = lc(); if (($pinerc_setup eq "y\n") || ($pinerc_setup eq "yes\n")) { print "Setting up Pine"; open(PINERC,"/home/$ENV{'USER'}/.pinerc"); open(NEW_PINERC,">/tmp/pinerc.$ENV{'USER'}"); while () { if ($feature == 1) { if (/,$/) { print NEW_PINERC $_; } elsif (/\w$/) { chop; print NEW_PINERC "$_,\n\tenable-incoming-folders\n"; $feature = 0; } elsif (/^$/) { print NEW_PINERC "\tenable-incoming-folders\n\n"; $feature = 0; } } elsif (!/^feature-list=/) { print NEW_PINERC $_; } elsif (/^feature-list=$/) { print NEW_PINERC "feature-list=enable-incoming-folders"; } elsif (/^feature-list=\w/) { $feature = 1; if (/,$/) { $comma = 1; print NEW_PINERC "$_"; } else { chop; print NEW_PINERC "$_,\n"; } } } close (PINERC); close (NEW_PINERC); print `mv /tmp/pinerc.$ENV{'USER'} /home/$ENV{'USER'}/.pinerc`; print " . . . DONE!\n"; } #. . . DONE!\n"; } } sub read_procmailrc { open(PROCMAILRC,"/home/$ENV{'USER'}/.procmailrc"); while () { chomp; if (($flag) && (!/^$/)) { push(@filter_folder, $_); $flag = 0; } elsif (/^\*\s/) { push(@filter_expr, $_); $flag = 1; } } close (PROCMAILRC); } sub main_menu { $menu_choice = "0"; while ($menu_choice ne "4") { $menu_choice = "0"; print <<"(END OF MAIN MENU)"; Procmail Setup 1) List Filters 2) Add Filter 3) Remove Filter 4) Quit (END OF MAIN MENU) print "What'll it be? "; $menu_choice = ; chomp $menu_choice; if ($menu_choice eq "1") { &list_filters; print "Press any key."; ; print `clear`; } elsif ($menu_choice eq "2") { &add_filter; } elsif ($menu_choice eq "3") { &list_filters; &remove_filter; } elsif ($menu_choice eq "4") { print "Type \"YES\" to write changes: "; if ( eq "YES\n") { &write_procmailrc; &write_pinerc; print "Changes written.\n"; } else { print "Changes not written.\n"; } print "Hope you enjoyed setting up Procmail! --Ben\n\n"; } else { print `clear`; } } } sub list_filters { $length = 6; for ($x = 0; $x < scalar(@filter_expr); $x++) { if (length($filter_folder[$x]) > $length) { $length = length($filter_folder[$x]); } } print "\n# FOLDER ", " " x ($length - 6), "HEADER RULE FILTER\n"; print "-- ", "-" x $length, " ", "------- -------------- -----\n"; for ($x = 0; $x < scalar(@filter_expr); $x++) { print $x + 1, " $filter_folder[$x] ", " " x ($length - length($filter_folder[$x])); if ($filter_expr[$x] =~ /\*\s\^To:/) { print "To "; } elsif ($filter_expr[$x] =~ /\*\s\^From:/) { print "From "; } elsif ($filter_expr[$x] =~ /\*\s\^Subject:/) { print "Subject "; } else { print " "; } if ($' =~ / /) { print "beginning with "; } elsif ($' =~ /\.\*/) { print "containing "; } print "$'\n"; } print "\n"; } sub remove_filter { print "Enter filter number: "; $remove_number = ; chomp $remove_number; if ($remove_number =~ /^\d+$/) { if (1 <= $remove_number && $remove_number <= scalar(@filter_expr)) { print "Are you sure? (Y/N) "; $temp = ; chomp $temp; if (lc($temp) eq "y") { print "Removing filter $remove_number. Press any key to continue."; $remove_number--; splice @filter_folder, $remove_number, 1; splice @filter_expr, $remove_number, 1; ; print `clear`; } else { print "Filter removal aborted. Press any key."; ; print `clear`; } } else { print "Try an EXISTING filter number. Press any key."; ; print `clear`; } } else { print "Since when did that become a number? Press any key."; ; print `clear`; } } sub add_filter { print "\n1) To\n2) From\n3) Subject\n\nWhat field do you want to filter by? "; $choice = ; chomp $choice; if ($choice eq "1") { $new_filter = "* ^To:"; } elsif ($choice eq "2") { $new_filter = "* ^From:"; } elsif ($choice eq "3") { $new_filter = "* ^Subject:"; } else { print "Invalid choice. Press any key."; ; print `clear`; goto &main_menu; } print "\n1) Beginning with (line must begin with the text)\n2) Containing (matches the text anywhere in the line)\n\nChoose: "; $choice = ; chomp $choice; if ($choice eq "1") { $new_filter .= " "; } elsif ($choice eq "2") { $new_filter .= ".*"; } else { print "Invalid choice. Press any key."; ; print `clear`; goto &main_menu; } print "Enter the text you want to filter: "; $filter_text = ; chomp $filter_text; if ($filter_text) { $new_filter .= $filter_text; } else { print "You need some text! Press any key."; ; print `clear`; goto &main_menu; } print "Incoming folder to filter to (\"/dev/null\" to delete it): "; $folder_choice = ; chomp $folder_choice; if ($folder_choice) { push @filter_folder, $folder_choice; push @filter_expr, $new_filter; print "Filter added. Press any key."; ; print `clear`; } else { print "You need a folder name! Press any key."; ; print `clear`; goto &main_menu; } } sub write_procmailrc { $temp = "/tmp/procmailrc.$ENV{'USER'}"; open(NEWPROCMAILRC,">$temp"); print NEWPROCMAILRC "PATH=/bin:/usr/bin\nMAILDIR=/home/$ENV{'USER'}/mail\nLOGFILE=\$MAILDIR/from\n\n"; for ($x = 0; $x < scalar(@filter_expr); $x++) { print NEWPROCMAILRC ":0:\n$filter_expr[$x]\n$filter_folder[$x]\n\n"; } close(NEWPROCMAILRC); print `mv /tmp/procmailrc.$ENV{'USER'} /home/$ENV{'USER'}/.procmailrc`; } sub write_pinerc { @sorted_filter_folder = $filter_folder[0]; for ($x = 0; $x < scalar(@filter_folder); $x++) { $flag = 0; for ($y = 0; $y < scalar(@sorted_filter_folder); $y++) { if ($filter_folder[$x] eq $sorted_filter_folder[$y]) { $flag = 1; } } if (!$flag) { push @sorted_filter_folder, $filter_folder[$x]; } } open(PINERC,"/home/$ENV{'USER'}/.pinerc"); open(NEW_PINERC,">/tmp/pinerc.$ENV{'USER'}"); $flag = 0; while () { if ($flag) { if (/^$/) { $flag = 0; } } else { if (!/^incoming-folders=/) { print NEW_PINERC $_; } elsif (/^incoming-folders=/) { $flag = 1; print NEW_PINERC "incoming-folders="; for ($y = 0; $y < scalar(@sorted_filter_folder); $y++) { if ($sorted_filter_folder[$y] eq "/dev/null") { } elsif ($y == 0) { print NEW_PINERC "\"$sorted_filter_folder[$y]\" mail/$sorted_filter_folder[$y],\n"; } elsif ($y == scalar(@sorted_filter_folder) - 1) { print NEW_PINERC "\t\"$sorted_filter_folder[$y]\" mail/$sorted_filter_folder[$y]\n"; } else { print NEW_PINERC "\t\"$sorted_filter_folder[$y]\" mail/$sorted_filter_folder[$y],\n"; } } print NEW_PINERC "\n"; } } } close(PINERC); close(NEW_PINERC); print `mv /tmp/pinerc.$ENV{'USER'} /home/$ENV{'USER'}/.pinerc`; } print `clear`; &check_forward; &check_pinerc; &read_procmailrc; &main_menu;