reading that entire folder everytime it stored a message there! Twice
because mark(1) does it too. The first is my fault, the second mh's.
I still haven't gone through mh to get rid of unnecessary readdirs, so
work around the problem here.
(get_highest_msgnum): Only read a directory once, saving the highest
message number and incrementing it on each call.
(mark): New function to add a message to a sequence.
(store_message): Use mark instead of running mark(1).
sub get_highest_msgnum {
my $mhfolder = shift;
my $dir;
sub get_highest_msgnum {
my $mhfolder = shift;
my $dir;
+ if (defined($msgnum_cache{$mhfolder})) {
+ return $msgnum_cache{$mhfolder}++;
+ }
+
$dir = "$mh/$mhfolder";
if (not opendir(DIR, $dir)) {
die("opendir($dir): $!");
$dir = "$mh/$mhfolder";
if (not opendir(DIR, $dir)) {
die("opendir($dir): $!");
+ return $msgnum_cache{$mhfolder} = $highest;
+}
+
+sub mark {
+ my $folder = shift;
+ my $msgnum = shift;
+ my $seq = shift;
+ my $fn = "$mh/$folder/.mh_sequences";
+ my $fh;
+ my @sequences;
+
+ if (not open($fh, $fn)) {
+ $!{ENOENT} or die("open($fn): $!");
+ } else {
+ @sequences = <$fh>;
+ chomp(@sequences);
+ }
+
+ my $marked = 0;
+ open($fh, '>', $fn) or die("open(>$fn): $!");
+ for $_ (@sequences) {
+ if (/^$seq: /) {
+ print($fh "$_ $msgnum\n");
+ $marked = 1;
+ } else {
+ print($fh "$_\n");
+ }
+ }
+ $marked or print($fh "$seq: $msgnum\n");
+ close($fh) or die("close(>$fn): $!");
if ($mhfolder eq 'SPAM') {
push(@SPAM, $msgnum);
} else {
if ($mhfolder eq 'SPAM') {
push(@SPAM, $msgnum);
} else {
- $status = system('mark', "+$mhfolder", "$msgnum", '-sequence',
- 'unseen', '-add');
- # XXX need to handle signalled and stopped, and print
- # the exit code or signal number.
- if (not WIFEXITED($status)) {
- die("Failed to run mark");
- } elsif (WEXITSTATUS($status) != 0) {
- die("Failed to mark message unseen.");
- }
+ mark($mhfolder, $msgnum, 'unseen');