home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / perl / scripts-convex / clones / vacation < prev    next >
Encoding:
Text File  |  1992-03-03  |  5.9 KB  |  287 lines

  1. #!/usr/local/bin/perl
  2. #
  3. # vacation program
  4. # Larry Wall <lwall@jpl-devvax.jpl.nasa.gov>
  5. #
  6. # updates by Tom Christiansen <tchrist@convex.com>
  7.  
  8. $vacation = $0;
  9. $vacation = '/usr/local/bin/nvacation' unless $vacation =~ m#^/#;
  10.  
  11. $user = $ENV{'USER'} || $ENV{'LOGNAME'} || getlogin || (getpwuid($>))[0];
  12. $editor = $ENV{'VISUAL'} || $ENV{'EDITOR'} || 'vi';
  13. $pager = $ENV{'PAGER'} || 'more';
  14.  
  15. $default_msg = <<'EOF';
  16. I will not be reading my mail for a while.
  17. Your mail regarding "$SUBJECT" will be read when I return.
  18. EOF
  19.  
  20.  
  21. if (!@ARGV) {        # interactive use, a la Sun
  22.     &interactive();
  23.     die "not reached";
  24. }
  25.  
  26. @ignores = ( 
  27.     'daemon', 
  28.      'postmaster', 
  29.      'mailer-daemon', 
  30.      'mailer',
  31.      'root',
  32.      );
  33.  
  34. # set-up time scale suffix ratios
  35. %scale = (
  36.     's', 1,
  37.     'm', 60,
  38.     'h', 60 * 60,
  39.     'd', 24 * 60 * 60,
  40.     'w', 7 * 24 * 60 * 60,
  41.     );
  42.  
  43.  
  44. while ($ARGV[0] =~ /^-/) {
  45.     $_ = shift;
  46.     if (/^-I/i) {  # eric allman's source has both cases
  47.     chdir;
  48.     &initialize;
  49.     exit;
  50.     } elsif (/^-j/) {
  51.     $opt_j++;
  52.     } elsif (/^-f(.*)/) {
  53.     &save_file($1 ? $1 : shift);
  54.     } elsif (/^-a(.*)/) {
  55.     &save_alias($1 ? $1 : shift);
  56.     } elsif (/^-t([\d.]*)([smhdw])/) {
  57.     $timeout = $1;
  58.     $timeout *= $scale{$2} if $2;
  59.     } else {
  60.     die "Unrecognized switch: $_\n";
  61.     }
  62. }
  63.  
  64. if (!@ARGV) {
  65.     &interactive();
  66.     die "not reached";
  67. }
  68.  
  69.  
  70. $user = shift;
  71. push(@ignores, $user);
  72. push(@aliases, $user);
  73. die "Usage: vacation username\n" if $user eq '' || @ARGV;
  74.  
  75. $home = (getpwnam($user))[7];
  76. die "No home directory for user $user\n" unless $home;
  77. chdir $home || die "Can't chdir to $home: $!\n";
  78.  
  79. $timeout = 7 * 24 * 60 * 60 unless $timeout;
  80.  
  81. dbmopen(VAC, ".vacation", 0666) || die "Can't open vacation dbm files: $!\n";
  82.  
  83.  
  84. $/ = '';            # paragraph mode
  85. $header = <>;
  86. $header =~ s/\n\s+/ /g;        # fix continuation lines
  87. $* = 1;
  88.  
  89. exit if $header =~ /^Precedence:\s*(bulk|junk)/i;
  90. exit if $header =~ /^From.*-REQUEST@/i;
  91.  
  92. for (@ignores) {
  93.     exit if $header =~ /^From.*\b$_\b/i;
  94.  
  95. ($from) = ($header =~ /^From\s+(\S+)/);    # that's the Unix-style From line
  96. die "No \"From\" line!!!!\n" if $from eq "";
  97.  
  98. ($subject) = ($header =~ /Subject:\s+(.*)/);
  99. $subject = "(No subject)" unless $subject;
  100. $subject =~ s/\s+$//;
  101.  
  102. ($to) = ($header =~ /To:\s+(.*)/);
  103. ($cc) = ($header =~ /Cc:(\s+.*)/);
  104. $to .= ', '.$cc if $cc;
  105.  
  106. if (open(MSG,'.vacation.msg')) {
  107.     undef $/;
  108.     $msg = <MSG>;
  109.     close MSG;
  110. } else {
  111.     $msg = $default_msg;
  112. $msg =~ s/\$SUBJECT/$subject/g;        # Sun's vacation does this
  113.  
  114. unless ($opt_j) {
  115.     foreach $name (@aliases) {
  116.     $ok++ if $to =~ /\b$name\b/;
  117.     }
  118.     exit unless $ok;
  119. }
  120.  
  121. $lastdate = $VAC{$from};
  122. $now = time;
  123. if ($lastdate ne '') {
  124.     ($lastdate) = unpack("L",$lastdate);
  125.     exit unless $lastdate;
  126.     exit if $now < $lastdate + $timeout;
  127. }
  128.  
  129. $VAC{$from} = pack("L", $now);
  130. dbmclose(VAC);
  131.  
  132.  
  133. open(MAIL, "|/usr/lib/sendmail -oi -t $from") || die "Can't run sendmail: $!\n";
  134.  
  135. print MAIL <<EOF;
  136. To: $from
  137. Subject: This is a recording... [Re: $subject]
  138. Precedence: junk
  139.  
  140. EOF
  141. print MAIL $msg;
  142. close MAIL;
  143. exit;
  144.  
  145. sub yorn {
  146.     local($answer);
  147.     for (;;) {
  148.     print $_[0];
  149.     $answer = <STDIN>;
  150.     last if $answer =~ /^[yn]/i;
  151.     print "Please answer \"yes\" or \"no\" ('y' or 'n')\n";
  152.     }
  153.     $answer =~ /^y/i;
  154. }
  155.  
  156. sub interactive {
  157.     chdir;
  158.     chop($cwd = `pwd`);
  159.  
  160.     &disable if -f '.forward';
  161.  
  162.     print <<EOF;
  163. This program can be used to answer your mail automatically
  164. when you go away on vacation.
  165. EOF
  166.  
  167.     for (;;) {
  168.     if (-f '.vacation.msg') {
  169.         print "\nYou already have a message file in $cwd/.vacation.msg.\n";
  170.         $see = &yorn("Would you like to see it? ");
  171.         system $pager, '.vacation.msg' if $see;
  172.         $edit = &yorn("Would you like to edit it? ");
  173.     }
  174.     else {
  175.         &make_default;
  176.         print <<EOF;
  177.  
  178. I've created a default vacation message in ~/.vacation.msg.  This
  179. message will be automatically returned to anyone sending you mail while
  180. you're out.
  181.  
  182. Press return when ready to continue, and you will enter your favorite
  183. editor ($editor) to edit the messasge to your own tastes.  
  184.  
  185. EOF
  186.         $| = 1;
  187.         print "Press return to continue: "; 
  188.         <STDIN>;
  189.         $edit = 1;
  190.     }
  191.  
  192.     last unless $edit;
  193.     system $editor, '.vacation.msg';
  194.     last;
  195.     }
  196.  
  197.  
  198.     print "\nTo enable the vacation feature a \".forward\" file is created.\n";
  199.     if (&yorn("Would you like to enable the vacation feature now? ")) {
  200.     &initialize;
  201.     print <<EOF;
  202.  
  203. Ok, vacation feature ENABLED.  Please remember to turn it off when
  204. you get back from vacation.  Bon voyage!
  205. EOF
  206.     }
  207.     else {
  208.         print "Ok, vacation feature NOT enabled.\n";
  209.     }
  210.  
  211.     exit;
  212. }
  213.  
  214. sub initialize {
  215.     &zero('.vacation.pag');
  216.     &zero('.vacation.dir');
  217.     open(FOR, ">.forward") || die "Can't create .forward: $!\n";
  218.     print FOR "\\$user, \"|$vacation $user\"\n";
  219.     close FOR;
  220.     &make_default;
  221.  
  222. sub zero {
  223.     local($FILE) = @_;
  224.     open (FILE, ">$FILE") || die "can't creat $FILE: $!";
  225.     close FILE;
  226.  
  227.  
  228. sub save_file {
  229.     local($FILE) = @_;
  230.     local($_);
  231.  
  232.     open FILE || die "can't open $FILE: $!";
  233.  
  234.     while (<FILE>) {
  235.     push(@ignores, split);
  236.     } 
  237.     close FILE;
  238.  
  239. sub disable {
  240.     print "\nYou have a .forward file in your home directory containing: ",
  241.       "\n",
  242.       `cat .forward`,
  243.       "\n";
  244.     if (&yorn(
  245. "Would you like to remove it and disable the vacation feature? ")) {
  246.     unlink('.forward') || die "Can't unlink .forward: $!\n";
  247.     dbmopen(VAC, '.vacation', 0444) || die "no .vacation dbmfile\n";
  248.     sub byvalue { $VAC{$a} <=> $VAC{$b}; }
  249.     if (@keys = sort byvalue keys %VAC) {
  250.         require 'ctime.pl';
  251.         open (PAGER, "|$pager") || die "can't open $pager: $!";
  252.         print PAGER <<EOF;
  253. While you were away, mail was sent to the following addresses:
  254.  
  255. EOF
  256.         for $key (@keys) {
  257.         ($when) = unpack("L", $VAC{$key});
  258.         printf PAGER "%-20s %s", $key, &ctime($when);
  259.         } 
  260.         close PAGER;
  261.     }
  262.     print "Back to normal reception of mail.\n";
  263.     exit;
  264.     } else {
  265.     print "Ok, vacation feature NOT disabled.\n";
  266.     }
  267.  
  268. sub make_default {
  269.     return if $edit;
  270.     open(MSG, ">.vacation.msg") || die "Can't create .vacation.msg: $!\n";
  271.     print MSG $default_msg;
  272.     close MSG;
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279.  
  280.