<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/net/nf/bin/perl
#$Id: prsfrom.pl 1.2 1998/01/21 12:09:26 aburgers Exp aburgers $
# parse command-line arguments

require('getopt.pl');&amp;Getopt('o');

# print a help message

if ($opt_h) {
	print &lt;&lt;HELP;exit;
usage:
	$0 -h
	$0 [-o output_mailbox] [input_mailbox]

mhonarc extracts the date from a message from Date: or Received:
fields from the message-header. The sender is extracted from a From:
field. There are several cases when these fields are missing ( e.g.
out-boxes of Eudora, DEC-mailx). In all these cases it is possible to
extract the sender and the date from the message separator line.

$0 checks messages in mailbox input_mailbox (or standard input if
input_mailbox is not specified) for the presence of Date:, Received:
and From: fields.  If information is missing $0 attempts to construct
these fields from the message separator.  $0 assumes the message are
separated by a line of the following form.

&gt;From sender date

The new Date: and From: fields are written
directly after the message separator. A new mailbox is written to
standard output or the file specified with the -o option. If the -o
option is used some statistics are reported to standard output.
HELP
}

# open output-file

if ($opt_o) {
	open(OUT,"&gt;$opt_o") || die "Error opening file $opt_o\n";
	select OUT;
}

$msg = 0;
$inheader=0;
$date_found = 0;
$received_found = 0;
$from_found = 0;

# method
#
# The message header is assumed to start at a line starting with /^From /
# and end at the next blank line.
# The sender and the date are extracted from the /^From / line. The
# lines of the header are stored in array @headerlines and checked
# for the presence of Date:, Received: and From: fields.
#
# $inheader=1 means we are processing a header
# $inheader=0 means we are outside the a header

while (&lt;&gt;) {
	if ($inheader) { # process message-header
		push(@headerlines,$_);
		study;
		if      (/^date:/i) { 		# check for date field
			$date_found = 1;
		} elsif (/^received:/i) {	# check for received field
			$received_found = 1;
		} elsif (/^from:/i) {		# check for from field
			$from_found = 1;
		} elsif (/^\s*$/) {		# blank line ending header
			unless($date_found || $received_found) {
				if ($date) {
					print "Date: $date\n";
					$print_date++;
				} else {
					warn "No date in From field\n";
				}
			}
			unless($from_found) {
				if ($adress) {
					print "From: $adress\n";
					$print_from++;
				} else {
					warn "No adress in From field\n";
				}
			}

			# Copy header to new mailbox

			for $line (@headerlines) {
				print $line;
			}

			# Reset counters

			$inheader = 0;
			undef @headerlines;
			$date_found = 0;
			$received_found = 0;
			$from_found = 0;
		}
	}
	else { # process message-body and message separator
		if (/^From /) { #test for message-header
			($dum,$adress,$date) = split(' ',$_,3);
			$date   =~ s/\s*$//;
			$adress =~ s/\s*$//;
			$inheader = 1;
			$msg++;
		}
		print;
	}
}

# print statistics

if ($opt_o) {
	select STDOUT;
	print "Total number of messages found: $msg\n";
	print "Added a Date field to $print_date messages\n" if ($print_date);
	print "Added a From field to $print_from messages\n" if ($print_from);
}

__END__

=head1 NAME

B&lt;prsfrom&gt; - supply missing Date: and From: fields to mailboxes

=head1 SYNOPSIS

B&lt;prsfrom&gt; [B&lt;-o&gt; F&lt;output_mailbox&gt;] [F&lt;input_mailbox&gt;]

B&lt;prsfrom&gt; [B&lt;-h&gt;]

=head1 DESCRIPTION

B&lt;prsfrom&gt; is a tool meant to be used in conjunction with B&lt;mhonarc&gt;.
B&lt;mhonarc&gt; extracts the date from a message from Date: or Received:
fields from the message-header. The sender is extracted from a From:
field. There are several cases when these fields are missing ( e.g.
out-boxes of Eudora, DEC-mailx). In all these cases it is possible to
extract the sender and the date from the message separator line.

B&lt;prsfrom&gt; checks messages in mailbox F&lt;input_mailbox&gt; (or standard input
if F&lt;input_mailbox&gt; is not specified) for the presence of Date:,
Received:  and From: fields. If information is missing B&lt;prsfrom&gt;
attempts to construct these fields from the message separator.
B&lt;prsfrom&gt; assumes the message are separated by a line of the following
form.

&gt;From sender date

The new Date: and From: fields are written directly after the message
separator. A new mailbox is written to standard output or the file
specified with the -o option. If the -o option is used some statistics
are reported to standard output.

If the -h option is specified a usage summary is written to standard
output.

=head2 Options

=over

=item B&lt;-h&gt;

A usage summary is written to standard output. No further processing is
done

=item B&lt;-o&gt; F&lt;output_mailbox&gt;

Default the new mailbox is written to standard output. With the
-o option a file to receive the new mailbox can be specified.
If the -o option is specified, some statistics are written
to standard output.

=back

=head1 RESTRICTIONS

B&lt;prsfrom&gt; also changes the headers of message in
mailboxes included as attachments in other message.

=head1 RETURN VALUE

The return value of B&lt;prsfrom&gt; is always 0

=head1 SEE ALSO

=for html
See the &lt;a href="http://www.oac.uci.edu/indiv/ehood/mhonarc.html"&gt;mhonarc home-page&lt;/a&gt;.

=head1 AUTHOR

=begin latex

A.R. Burgers\\
Netherlands Energy Research Foundation ECN\\
P.O. Box 1, 1755 ZG Petten, The Netherlands\\
e-mail: burgers@ecn.nl

=end latex

=for text
 A.R. Burgers
 Netherlands Energy Research Foundation ECN
 P.O. Box 1, 1755 ZG Petten, The Netherlands
 e-mail: burgers@ecn.nl

=for html
A.R. Burgers &lt;br&gt;
Netherlands Energy Research Foundation ECN &lt;br&gt;
P.O. Box 1, 1755 ZG Petten, The Netherlands &lt;br&gt;
e-mail: &lt;a href="mailto:burgers@ecn.nl"&gt;burgers@ecn.nl &lt;/a&gt;
</pre></body></html>