+ for my $hook (@stop_hooks) {
+ $hook->(\%batons);
+ }
+
+ return %folders;
+}
+
+sub build_dot_folders {
+ my $folders = shift;
+ my $fh = shift;
+ my $fn;
+
+ if (defined($fh)) {
+ while (<$fh>) {
+ chomp;
+ $folders->{$_}++;
+ }
+ }
+
+ eval { ($fh, $fn) = tempfile("$mh/.folders.XXXXX") };
+ if ($@) {
+ warn("$@");
+ return;
+ }
+
+ for my $folder (sort { $folder_sorter->($a,$b) } keys(%$folders)) {
+ print($fh "$folder\n");
+ }
+
+ if (not close($fh)) {
+ warn("close($fn): $!");
+ unlink($fn) or warn("unlink($fn): $!");
+ return;
+ }
+
+ rename($fn, "$mh/.folders") or warn("rename($fn, $mh/.folders): $!");
+}
+
+sub create_dot_folders {
+ if (-f "$HOME/.minc") {
+ require "$HOME/.minc";
+ }
+
+ my %folders;
+ open(my $fh, '-|', 'folders', '-fast', '-recur')
+ or die("open(folders|): $!");
+ build_dot_folders(\%folders, $fh);
+ return 0;
+}
+
+sub update_dot_folders {
+ my $folders = shift;
+ my $fh;
+
+ if (not open($fh, "$mh/.folders") and not $!{ENOENT}) {
+ # For ENOENT, we go ahead and create it, else we error and
+ # don't clobber it.
+ warn("open($mh/.folders): $!");
+ return;
+ }
+
+ build_dot_folders($folders, $fh);
+}
+
+# XXX Could use some unification with getfiles...
+sub maildir_spam {
+ my $dir = "$MAILDIR/spam/new";
+
+ if (not chdir($dir)) {
+ $!{ENOENT} or print(STDERR "skipping maildir spam: chdir($dir): $!\n");
+ return;
+ }
+
+ if (not opendir(DIR, '.')) {
+ print(STDERR "skipping maildir spam: opendir($dir): $!\n");
+ return;
+ }
+
+ $! = 0;
+ my @spams = readdir(DIR);
+ if ($! != 0) {
+ print(STDERR "skipping maildir spam: readdir($dir): $!\n");
+ return;
+ }
+
+ closedir(DIR);
+
+ for my $msg (@spams) {
+ ($msg eq '.' or $msg eq '..') and next;
+ my $msgnum = store_message($msg, 'SPAM');
+ # Store the original file name for scan_spam in -n mode.
+ $SPAM{$msgnum} = [undef, $msg];
+ }
+}
+
+sub scan_spam {
+ my ($msgnum, $header, $tuple, $msg);
+
+ # Unlike filter_mail, we don't need to print the folder name.
+ # Calculate how many columns would be allocated to it...
+ my $nf = int($COLUMNS * $SCAN_P_FOLDER);
+ # ...and add that amount to COLUMNS to calculate the number of columns to
+ # allocate to msgnum and from snippet, thus filling the line without
+ # printing the folder name.
+ my $nm = int(($COLUMNS + $nf) * $SCAN_P_MESSAGE);
+ my $nF = int(($COLUMNS + $nf) * $SCAN_P_FROM);
+ my $ns = $COLUMNS - $nm - $nF - 3;
+
+ for $msgnum (sort(keys(%SPAM))) {
+ $tuple = $SPAM{$msgnum};
+ if (defined($tuple->[0])) {
+ # Filed by filter_mail, so we have the header.
+ $header = $tuple->[0];
+ } elsif (defined($tuple->[1])) {
+ # Filed by maildir_spam, so we don't have the header.
+ if ($run) {
+ # The message has been filed, load it from $mh.
+ $msg = "$mh/SPAM/$msgnum";
+ } else {
+ # The message has not been filed, load it from the maildir.
+ # $tuple->[1] is just a basename, not a path; this works
+ # because maildir_spam did chdir(Maildir/spam/new).
+ $msg = $tuple->[1];
+ }
+ $header = get_headers($msg);
+ } else {
+ print(STDERR
+ "BUG: corrupt SPAM tuple, neither element defined",
+ " for message $msgnum\n");
+ next;
+ }
+ print(scan_line($header, '', $msgnum, 0, $nm, $nF, $ns),
+ "\n");
+ }