Subject: Yet another maildir/xbiff tool

From: Jonathan Kamens <[email-masked]>
Date: Thu, 9 Dec 1999 14:06:47 -0500

Here's yet another tool for making maildir work with xbiff.  The
comment at the top of the script explains why this one is better than
some of the others at www.qmail.org.  Feel free to put this up on the
Web site.

  jik

#!/usr/bin/perl

# mdcheckmail.pl - a fully functional xbiff mail checker for maildir,
# written in Perl
# 
# Written by Jonathan Kamens <[email-masked]>.  Please
# send me any comments, fixes, suggestions, etc.
#
# To use: 
#
# 1) Modify the $maildir variable below to reflect where your mail
#    directory lives.
# 2) If you want the script to store its state somewhere other than
#    the file ~/prevmail, modify the $prevfile setting too.
# 3) Make sure the script file is executable.
# 4) Configure xbiff to use it by putting its path in the
#    "xbiff*checkCommand" X resource or specifying it with the
#    "-checkCommand" command-line option; see the xbiff man page for
#    more information.
#
# This script was inspired by a similar shell script written by Philip
# Hands <[email-masked]>.  However, it improves on that script in a
# number of ways.  The two biggest improvements are: (1) it doesn't
# call any external processes at all, so it's more efficient; and (2)
# it notices if the newest message in the maildir has been read by the
# user, e.g., because the user has run "mdfrm.pl" or a similar
# utility, and if so, returns the exit status indicating that there is
# no new mail.  This means that if you receive new mail, then xbiff
# alerts you, then you run "mdfrm.pl" to see a summary of the mail,
# xbiff will unhighlight itself and not alert again until more new
# mail arrives.  This makes the maildir functionality of xbiff
# equivalent to the mbox-file functionality.

$maildir = "$ENV{'HOME'}/.Maildir";
$prevfile = "$ENV{'HOME'}/.prevmail";

##
## You shouldn't need to modify anything below here.
##

$msgdir = "$maildir/new";
($whoami = $0) =~ s,.*/,,;

opendir(DIR, $msgdir) || die "$whoami: opening $msgdir: $!\n";

defined(@msgs = readdir(DIR)) || exit(2);

closedir(DIR) || die "$whoami: closing $msgdir: $!\n";

@msgs = sort grep($_ ne '.' && $_ ne '..', @msgs);

if (! @msgs) {
    unlink($prevfile);
    exit(2);
}

$newsum = unpack("%16C*", "@msgs");
$oldsum = do $prevfile if (-f $prevfile);

if ($newsum != $oldsum) {
    open(SUM, ">$prevfile") || die "$whoami: opening $prevfile for writing: $!\n";
    print(SUM $newsum) || die "$whoami: writing to $prevfile: $!\n";
    close(SUM) || die "$whoami: closing $prevfile after writing: $!\n";
    $exit_value = 0;
}
else {
    $exit_value = 1;
}

$lastmsg = pop @msgs;

if (-A "$msgdir/$lastmsg" < -M _) {
    exit(2);
}

exit($exit_value);