# Global stuff for JC's perl clone of the mh package. use User::pwent qw(:FIELDS); $ENV{'PATH'} = '/u/guests/jc/mh:/u/guests/jc/p/cgi:/bin:/usr/bin:/usr/local/bin:/usr/eecs/bin'; ($P = $0) =~ s".*/""; $V = $ENV{"V_$P"} || $ENV{'V_MH'} || $ENV{'V'} || 1; %M2m = ('Jan' => '01', 'Feb' => '02', 'Mar' => '03', 'Apr' => '04', 'May' => '05', 'Jun' => '06', 'Jul' => '07', 'Aug' => '08', 'Sep' => '09', 'Oct' => '10', 'Nov' => '11', 'Dec' => '12'); $outcols = 80; # Default to punch-card width ;-) if (-t STDIN) { @stty = `/bin/stty -a`; for $l (@stty) { if ($l =~ / (\d+) col/) { $outcols = $1; print "Window has $outcols columns.\n" if $V>2; } } } &usrinfo(); # Make sure we have our user information $FLDR = 'inbox'; # Default mail folder $LNAM = $ENV{'LOGNAME'} || $ENV{'USER'} || 'nobody'; $HOME = $ENV{'HOME'} || "/home/$LNAM"; $MDIR = $ENV{'MAILDIR'} || "$ENV{'HOME'}/Mail"; $FDIR = $MDIR . '/' . &getfolder($MDIR,$fldr); print "$P: FDIR=\"$FDIR\"\n" if $V>1; sub getfolder { # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # # Find the name of the current folder for a mail directory. The 2nd arg is # # the default to use if the folder can't be found, usually 'inbox'. # # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # local($mdir,$fldr) = @_; $fldr = 'inbox' unless $fldr; if (open(SEQ,"$mdir/.folder")) { while ($l = ) { if ($l =~ /^\s*(\w+)[\s\r]*$/) { if (-d "$mdir/$1") { $fldr = $1; print "$0: Folder $fldr from $mdir/$1\n" if $V>2; last; } else { printf STDERR "$0: \"$1 is not a folder (in $mdir/$1)\n"; } } } close SEQ; } return $fldr; } sub setfolder { # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # # Set the current folder. # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # local($mdir,$fldr) = @_; local(*SEQ); $fldr = 'inbox' unless $fldr; unless (open(SEQ,">$mdir/.folder")) { print STDERR "$0: Can't write $mdir/.folder ($!)\n"; return 0; } print SEQ "$fldr\n"; close SEQ; $FLDR = $fldr; return $FLDR; } sub getindex { # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # # Get the index for a folder. # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # local($mdir,$fldr,$mndx) = @_; $mdir = $MDIR unless $mdir; $fldr = $FLDR unless $fldr; unless (open(SEQ,"$mdir/$fldr/.mh_sequences")) { print STDERR "$P:getindex: Can't read $mdir/$fldr/.mh_sequences ($!)\n"; return($mndx || 1); } while ($l = ) { if ($l =~ /^cur:\s*(\d+)[\s\r]*$/) { $mndx = $1; last; } } close SEQ; return($mndx || 1); } sub maxindex { # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # # Find the highest index in an email folder. This is done by listing the # # folder's files whose names are integers, and returning the highest name. # # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # local($mdir,$fldr) = @_; local(*DIR,$dir,$f,@files,$imax); $dir = "$mdir/$fldr"; unless (opendir(DIR,$dir)) { print STDERR "$P:maxindex: Can't read dir \"%s\" ($!)\n"; return 0; } @files = readdir(DIR); closedir(DIR); $imax = 1; for $f (@files) { if ($f =~ /^(\d+)[\s\r]*$/) { $imax = $1 if $imax < $1; } } print "Highest index in folder $fldr is $imax.\n" if $V>1; return $imax; } sub msgdesc { # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # # Output a message description. This is used by the List and Show commands to # tell the user about mail messages. # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # local($ndx,$flg,$date,$from,$subj) = @_; local($yy,$mm,$dd,$mmm); if (($d,$mmm,$c,$yy) = ($date =~ /^\w*,\s*(\d+)\s+(\w\w\w) (\d*)(\d\d) /)) { $date = sprintf "%02d%02d%02d",$4,$M2m{$2},$1, } elsif (($mmm,$d,$c,$yy) = ($date =~ /^\w*,\s*(\w\w\w) (\d+)\s+(\d*)(\d\d) /)) { $date = sprintf "%02d%02d%02d",$4,$M2m{$1},$2, } elsif (($d,$mmm,$c,$yy) = ($date =~ /^(\d+)\s+(\w\w\w) (\d*)(\d\d) /)) { $date = sprintf "%02d%02d%02d",$4,$M2m{$2},$1, } elsif (($mmm,$d,$c,$yy) = ($date =~ /^(\w\w\w) (\d+)\s+(\d*)(\d\d) /)) { $date = sprintf "%02d%02d%02d",$yy,$M2m{$mmm},$d, # "Fri May 16 09:28:00 2003" } elsif (($mmm,$d,$c,$yy) = ($date =~ /^\w* (\w\w\w) (\d+)\s+\d+:\d+:\d+ (\d*)(\d\d)/)) { $date = sprintf "%02d%02d%02d",$yy,$M2m{$mmm},$d, } else { printf STDERR "$P: Can't parse date \"$date\" in message $ndx.\n"; $date = '000000'; } $date = substr $date,0,6; $from = substr $from,0,16; $subj = substr $subj,0,($outcols - 30); printf "%4d%s%6s %16s $subj\n",$ndx,$flg,$date,$from; } sub setindex { # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # # Set the index for a folder. It is kept in a file ".mh_sequences". We also # # add a line giving the path, to prevent confusion when moving things around. # # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # local($mdir,$fldr,$ndx) = @_; local($gotcur,$gotpth,$l,@lines,*SEQ,$pth); $ndx = 1 unless $ndx > 0; $pth = "$mdir/$fldr/.mh_sequences"; if (open(SEQ,$pth)) { @lines = ; for $l (@lines) { if ($l =~ /^cur:/) {$l = "cur: $ndx\n"; $gotcur++} if ($l =~ /^pth:/) {$l = "pth: $pth\n"; $gotpth++} } close SEQ; unless ($gotcur) {push @lines, "cur: $ndx\n"} unless ($gotpth) {push @lines, "pth: $pth\n"} } else { print STDERR "$P:setindex: Can't read $pth ($!)\n"; # return($ndx || 1); push @lines, "cur: $ndx\n"; push @lines, "pth: $pth\n"; } unless (open(SEQ,">$pth")) { print STDERR "$0: Can't write $pth ($!)\n"; return 0; } print SEQ @lines; close SEQ; print "Current message in \"$fldr\" is $ndx\n" if $V>2; return($ndx || 1); } sub usrinfo { my $F = 'usrinfo'; # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # # Figure out assorted user information. This is mostly useful when we've been # # called from cron or a web server, and don't have the usual shell # # environment all set up for us. # # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # my($euid,$x); ($cwd = `/bin/pwd`) =~ s/[\r\s]+$//; print "$F: cwd = \"$cwd\"\n" if $V>1; $euid = $>; # Our effective user ID number $ruid = $<; # Our real user ID number print "$F: euid = \"$euid\"\n" if $V>1; print "$F: ruid = \"$ruid\"\n" if $V>1; getpwuid($euid); print "$F: name = \"$pw_name\"\n" if $V>1; print "$F: passwd = \"$pw_passwd\"\n" if $V>1; print "$F: uid = \"$pw_uid\"\n" if $V>1; print "$F: gid = \"$pw_gid\"\n" if $V>1; #rint "$F: quota = \"$pw_quota\"\n" if $V>1; #rint "$F: comment = \"$pw_comment\"\n" if $V>1; #rint "$F: gcos = \"$pw_gcos\"\n" if $V>1; print "$F: homedir = \"$pw_dir\"\n" if $V>1; #rint "$F: shell = \"$pw_shell\"\n" if $V>1; #rint "$F: expire = \"$pw_expire\"\n" if $V>1; $ENV{'USER'} = $pw_name; $ENV{'LOGNAME'} = $pw_name; $ENV{'HOME'} = $pw_dir; $ENV{'SHELL'} = $pw_shell; $MAIL = "/var/mail/$pw_name"; print "$F:ENV{MAIL}= \"$MAIL\"\n" if $V>3; if (defined $MAIL && -e $MAIL) { print "$F:ENV{MAIL}= \"$MAIL\" exists.\n" if $V>5; if (-f $MAIL) { print "$F:ENV{MAIL}= \"$MAIL\" exists and is a file.\n" if $V>1; } else { print "$F:ENV{MAIL}= \"$MAIL\" exists and is not a file.\n" if $V>0; $MAIL = ''; # Wipe it out and try again. } } else { print "$F:ENV{MAIL}= \"$MAIL\" does not exist.\n" if $V>1; $MAIL = $ENV{'MAIL'} = ''; } unless (-f $MAIL) { print "$F:ENV{MAIL}= \"$MAIL\"\n" if $V>1 && $MAIL; print "$F: Look for mailbox file ...\n" if $V>1; if (-f ($x = "/var/mail/$pw_name")) {$MAIL = $x} elsif (-f ($x = "/usr/mail/$pw_name")) {$MAIL = $x} elsif (-f ($x = "/var/spool/mail/$pw_name")) {$MAIL = $x} elsif (-f ($x = "/usr/spool/mail/$pw_name")) {$MAIL = $x} else { print STDERR "$F: Can't find MAIL file for user '$pw_name'.\n" if $V>0; print "$F: Can't find MAIL file for user '$pw_name'.\n" if $V>0; } print "$F:ENV{MAIL}= \"$MAIL\"\n" if $V>1; $ENV{'MAIL'} = $MAIL; } } # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # 1;