#!/usr/bin/perl # Copyright (C) 1997 Anthony DeBoer # # This program is free software; you can redistribute it and/or modify # it under the terms of version 2 of the GNU General Public License as # published by the Free Software Foundation. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # delete ($ENV{'NNTPSERVER'}); # export NEWSLIB=$HOME/Mail # export NEWSSPOOL=$HOME/Mail # export DOTDIR=$HOME/Mail $HOME = $ENV{'HOME'}; $DISPO = $HOME . '/.disposition'; $RULES = $HOME . '/.maildir/inbox/new/.rules'; $MAILDIR = $HOME . '/Mail'; $ACTIVE = $MAILDIR . '/active'; $LISTSRC = $MAILDIR . '/.newsrc'; $KILLDIR = $HOME . '/News/.kill/'; $ENV{'DOTDIR'} = $MAILDIR; if ($ARGV[0] eq '-a') { $opt_a = 1; } elsif ($#ARGV == -1) { open(RULES) || die "Cannot open $RULES: $!\n"; while() { chop; $dispo{$1} = 1 if /\s+(\S+)/; } close(RULES); } else { for $_ (@ARGV) { $dispo{$_} = 1; } } chdir($MAILDIR); opendir(MAILDIR, $MAILDIR) || die "Cannot opendir $MAILDIR: $!\n"; for $f (readdir(MAILDIR)) { next if $f =~ /^\./; next unless -d $f; next unless $opt_a || $dispo{$f}; push(@folders, $f); } closedir(MAILDIR); open(ACTIVE, ">$ACTIVE") || die "Cannot write $ACTIVE: $!\n"; open(LISTSRC, ">$LISTSRC") || die "Cannot write $LISTSRC: $!\n"; for $group (sort (@folders)) { # print "$group"; $DIR = $MAILDIR . "\/$group"; opendir(DIR, $DIR) || die "Cannot opendir $DIR: $!\n"; $hiwat = $lowat = $unread = 0; @commas = (); @dotfiles = (); @rcline = (); for $f (readdir(DIR)) { # push(@dotfiles, $f), next if $f eq '.xmhcache' || $f eq '.mh_sequences'; next if $f =~ /^\./; push(@commas, $f), next if $f =~ /^\,/; next unless $f =~ /^\d+$/; $unread++; if ($f > $hiwat) { push(@rcline, sprintf("%d-%d", $hiwat+1, $f-1)); $hiwat = $f; } $lowat = $f if $f < $lowat || $lowat == 0; $i = 0; while($i <= $#rcline) { # print $i, " --> ", join(',', @rcline), "\n"; $_ = $rcline[$i]; if (/^(\d+)\-(\d+)$/) { $xl = $1, $xh = $2; if ($xl > $xh) { splice @rcline, $i, 1; } elsif ($xl == $xh) { $rcline[$i] = $xl; } elsif ($xl <= $f && $xh >= $f) { $e1 = sprintf("%d-%d", $xl, $f-1); $e2 = sprintf("%d-%d", $f+1, $xh); splice @rcline, $i, 1, $e1, $e2; } else { $i++; } } else { if ($_ eq $f) { splice @rcline, $i, 1; } else { $i++; } } } } closedir(DIR); push(@commas, @dotfiles) unless $unread; # print STDERR "$group - $unread unread [", join(' ', @dotfiles), "]\n"; $nrm = $#commas + 1; if ($nrm) { print STDERR "$group - $nrm unlinked\n"; chdir($DIR) && unlink(@commas); } if ($lowat) { # stat $DIR/$lowat @sbuf = stat("$DIR\/$lowat"); if ($sbuf[9]) { $oldtime = $sbuf[9], $oldfold = $group if $sbuf[9] < $oldtime || !$oldtime; } else { print STDERR "Cannot stat $DIR\/$lowat: $!\n"; } } $lowat = 1 if $lowat == 0; printf ACTIVE "%s %010d %05d y\n", $group, $hiwat, $lowat; print LISTSRC $group, ": ", join(',', @rcline), "\n"; &fixkillfile($group); } close(ACTIVE); close(LISTSRC); print STDERR "Folder with oldest unread message: $oldfold\n" if $oldfold; system "$HOME/.trnbin/trn"; exec "tmclup"; sub fixkillfile { local($group) = @_; local($KILLFILE) = $KILLDIR . $group; if (open(KILLFILE)) { local($_, $kb, $rflag); while() { if (/^(THRU|\<)/) { $rflag = 1; } else { $kb .= $_; } } close(KILLFILE); if ($rflag) { unlink($KILLFILE) || die "Cannot unlink $KILLFILE: $!\n"; if ($kb eq '') { print STDERR "Purged killfile for $group\n"; } else { print STDERR "Rewriting killfile for $group\n"; open(KILLFILE, ">$KILLFILE") || die "Cannot open $KILLFILE for writing: $!\n"; syswrite KILLFILE, $kb, length($kb) || die "Cannot write to $KILLFILE: $!\n"; close(KILLFILE) || die "Cannot close $KILLFILE after writing: $!\n"; } } else { # print STDERR "Unchanged killfile for $group\n"; } } else { # print STDERR "No killfile $KILLFILE for $group\n"; } }