4 # $Date: 92/05/12 14:34:18 $
9 # Subroutine initialize
10 # Set up the environment for the user and parse the incoming
14 local($passwd, $uid, $gid, $quota, $comment, $gcos);
16 ($user, $passwd, $uid, $gid, $quota, $comment, $gcos, $home, $shell) =
17 getpwnam($ARGV[0]); shift @ARGV;
21 $ENV{'SHELL'} = $shell;
22 $ENV{'TERM'} = "vt100";
24 &parse_message(STDIN);
29 # Subroutine parse_message
30 # Parse a message into headers, body and special variables
35 $/ = ''; # read input in paragraph mode
45 $body = "" if !defined($body);
53 ;# $sender comes from the UNIX-style From line (From strike...)
55 ($sender) = ($header =~ /^From\s+(\S+)/);
59 ;# fill out the headers associative array with fields from the mail
66 /^([\w-]*):\s*(.*)/ && do {
68 $mheader =~ tr/A-Z/a-z/;
69 if (($mheader eq "cc" || $mheader eq "to") && $headers{$mheader}) {
70 $headers{$mheader} .= ", $2";
71 } elsif ($mheader eq "received") {
74 $headers{$mheader} = $2;
78 @received = reverse(@received);
82 ;# for convenience, $subject is $headers{'subject'} and $precedence is
83 ;# $headers{'precedence'}
85 $subject = $headers{'subject'};
86 $subject = "(No subject)" unless $subject;
88 $precedence = $headers{'precedence'};
92 ;# create arrays for who was on the To, Cc lines
94 @cc = &expand($headers{'cc'});
95 @to = &expand($headers{'to'});
96 defined($headers{"apparently-to"}) && do {
97 $apparentlyto = $headers{"apparently-to"};
98 push(@to, &expand($apparentlyto));
102 ;# $from comes from From: line. $address is their email address.
103 ;# $organization is their site. for example, strike@pixel.convex.com
104 ;# yields an organization of convex.
106 $_ = $headers{'from'} ||
107 $headers{'resent-from'} ||
108 $headers{'sender'} ||
109 $headers{'resent-sender'} ||
110 $headers{'return-path'} ||
111 $headers{'reply-to'};
114 $friendly = $from = $address = $organization = "unknown";
118 ($friendly, $address, $from, $organization) = &parse_email_address($_);
123 # Subroutine parse_email_address
124 # Parse an email address into address, from, organization
125 # address is full Internet address, from is just the login
126 # name and organization is Internet hostname (without final domain)
128 sub parse_email_address {
130 local($friendly, $address, $from, $organization);
132 $organization = "local";
133 $friendly = "unknown";
135 # From: Disk Monitor Daemon (/usr/adm/bin/dfbitch) <daemon@hydra.convex.com>?
139 if (/(.*)\s*<[^>]+>$|<[^>]+>\s*(.*)$/) {
141 $friendly =~ s/\"//g;
142 } elsif (/\(([^\)]+)\)/) {
165 s/![A-Za-z0-9_@]*$//;
173 $organization = "unknown";
175 if (/\.(com|edu)$/) {
176 s/\.[A-Za-z0-9_]*$//;
179 s/\.[A-Za-z0-9_]*$//;
180 s/\.[A-Za-z0-9_]*$//;
187 return ($friendly, $address, $from, $organization);
192 # Subroutine vacation
193 # deliver a vacation message to the sender of this mail
197 local($vacfile) = $ENV{'HOME'} . "/" . ".vacation.msg";
198 local($msubject) = "\"Vacation mail for $ENV{'USER'} [Re: $subject]\" ";
199 local($vacaudit, $astat, $mstat);
203 return if (length($from) <= 0);
204 return if ($precedence =~ /(bulk|junk)/i);
205 return if ($from =~ /-REQUEST@/i);
207 @ignores = ('daemon', 'postmaster', 'mailer-daemon', 'mailer', 'root',);
208 grep(do {return if ($_ eq $from);}, @ignores);
211 ($vacaudit = $vacfile) =~ s/\.msg/\.log/;
213 $mstat = (stat($vacfile))[9];
214 $astat = (stat($vacaudit))[9];
215 unlink($vacaudit) if ($mstat > $astat);
218 open(VACAUDIT, "< $vacaudit") && do {
221 return if ($_ eq $from);
227 open(MAIL,"| /usr/ucb/Mail -s $msubject $address") || return;
228 open(VACFILE, "< $vacfile") || return;
230 s/\$SUBJECT/$subject/g;
236 open(VACAUDIT, ">> $vacaudit") || return;
237 print VACAUDIT "$from\n";
245 # expand a line (To, Cc, etc.) into a list of addressees.
251 return(@fccs) if /^$/;
253 for (split(/\s*,\s*/)) {
259 push(@fccs,$_) unless $seen{$_}++;
268 # Deliver the incoming mail message to the user's mail drop
272 &deposit("/usr/spool/mail/$user");
277 # Put the incoming mail into the specified mail drop (file)
284 open(MAIL, ">> $drop") || die "open: $!\n";
285 flock(MAIL, $LOCK_EX);
288 print MAIL "$header";
289 print MAIL "$body\n\n" if defined($body);
291 flock(MAIL, $LOCK_UN);
297 # Subroutine file_from
298 # Add the mail message to another mail drop in a log directory.
299 # The path of the mail drop is toplevel/organization/user
302 local($toplevel) = @_;
305 return if (length($from) <= 0);
306 return if ($from eq $user);
308 $toplevel = "log" if ($toplevel eq '');
310 $dir = "$home/$toplevel";
311 (!-d $dir) && mkdir($dir, 0700);
312 $dir .= "/$organization";
313 (!-d $dir) && mkdir($dir, 0700);
315 &deposit("$dir/$from");
320 # Subroutine openpipe
321 # Open a pipe to a command and write the mail message to it.
324 local($command) = @_;
326 open(CMD, "| $command") || die;
327 print CMD "$header\n";
328 print CMD "$body\n\n" if defined($body);