Subject: A newer version of the script (re: New Mail script)

From: Robert Nicholson <[email-masked]>
Date: Mon, 11 Apr 94 22:32 BST

Here's a newer version of the script that basically works like
mailstat in terms of what it produces but instead of using the log
file it actually scans each folder.

The idea is that when I use Elm to read a new message it's "tagged"
with a Status: RO. Anything not tagged this way is considered unread.

If you know how to return an array of totals including both total
number of messages and total unread from the scanmailbox script 
then please do make that modification and send me the new version.

The idea is to provide total for both of those fields. It's too late
for me to do anything more with that.

#!/usr/local/bin/perl

#Author Robert Nicholson, April 11th, 1994.

$MAILDIR="/Users/robert/Mail";
$total = 0;
print "     Total  Number Unread Folder\n";
print "     -----  ------ ------ ------\n";

opendir(MAIL,$MAILDIR) || die "can't open mail directory";
while ($_ = readdir(MAIL)) {
	if (/\./ || /\.\./ || /log/ || /sent/) {
		next;
	}
	$total += do scanmailbox($_,$MAILDIR);
}
closedir(MAIL);
print "     -----  ------ ------\n";
printf("%10d\n",$total);

# Search a mailbox and point out any unread messages.

sub scanmailbox {
local($mailbox,$d) = @_;
local($newmsg) = 0;
local($status) = 0;
local($i) = 0;
local($len) = 0;
local($msgs) = 0;
local($pathname) = '';
local($unread) = 0;

	$pathname=join('/',$d,$mailbox);

	open (MAILFOLDER,$pathname) || die "can't open mail folder";
	while (<MAILFOLDER>) {
		$len += length($_);

		if (/^From / && $newmsg == 0) {
			$newmsg=1;
			$msgs++;
		}
		if ($newmsg == 1) {
			$i++;
			if (/^From / || /^Subject:/ || /^Date:/) {
				$hdrs[$i] = $_;
			}
		}
		if (/^Status: RO/ && $newmsg == 1) {
			$status=1;
		}
		if (/^\n/ && $newmsg == 1) {

			if ($status == 0) {
				$unread++;

#				for (@hdrs) {
#					print "\t$_\r";
#				}
			}
			$newmsg=0;
			$status=0;
			$i=0;
			@hdrs = ();
		}
	}
	close(MAILFOLDER);
	printf("%10d %7d %6d %s\n",$len,$msgs,$unread,$mailbox);
	$len;
}