summaryrefslogtreecommitdiff
path: root/etc/countmail
diff options
context:
space:
mode:
authorAxel Beckert <abe@deuxchevaux.org>2011-09-03 14:05:25 +0200
committerAxel Beckert <abe@deuxchevaux.org>2011-09-03 14:05:25 +0200
commit9d943c7539f59f42ba8f8052497dc541fe5f0167 (patch)
treec8725127bf7a68d3d7fe57adca45c204b25adf21 /etc/countmail
parent0e9a09d7718f02726b12924f7ddb05a992202aa3 (diff)
downloadscreen-9d943c7539f59f42ba8f8052497dc541fe5f0167.tar.gz
Imported Upstream version 4.0.2upstream/4.0.2
Diffstat (limited to 'etc/countmail')
-rwxr-xr-xetc/countmail67
1 files changed, 67 insertions, 0 deletions
diff --git a/etc/countmail b/etc/countmail
new file mode 100755
index 0000000..974f143
--- /dev/null
+++ b/etc/countmail
@@ -0,0 +1,67 @@
+#!/usr/bin/perl
+
+sub countmsgs {
+ return -1 unless open(M, "<$mbox");
+ my $inhdr = 0;
+ my $cl = undef;
+ my $msgread = 0;
+ my $count = 0;
+ while(<M>) {
+ if (!$inhdr && /^From\s+\S+\s+(?i:sun|mon|tue|wed|thu|fri|sat)\s+(?i:jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\s+\d+\s/) {
+ $inhdr = 1;
+ $msgread = 0;
+ undef $cl;
+ next;
+ }
+ if ($inhdr) {
+ if (/^content-length:\s+(\d+)/i) {
+ $cl = 0+$1;
+ next;
+ }
+ if (/^status:\s+(\S)/i) {
+ $msgread = 1 unless $1 eq 'N' || $1 eq 'U';
+ next;
+ }
+ if ($_ eq "\n") {
+ $count++ if !$msgread;
+ seek(M, $cl, 1) if defined $cl;
+ $inhdr = 0;
+ }
+ }
+ }
+ close M;
+ return $count;
+}
+
+$| = 1;
+$mbox = $ARGV[0] || $ENV{'MAIL'};
+$oldfmt = $ARGV[1] || "%4d ";
+$newfmt = $ARGV[2] || "\005{Rk}%4d \005{-}";
+
+@oldstat = stat($mbox);
+if (!@oldstat) {
+ print "\005{Rk} ??? \005{-}\n";
+ exit 1;
+}
+$oldcount = 0;
+while(1) {
+ $count = countmsgs($mbox);
+ if ($count == -1) {
+ print "\005{Rk} ??? \005{-}\n";
+ } elsif ($count < $oldcount || $count == 0) {
+ printf "$oldfmt\n", $count;
+ } else {
+ printf "$newfmt\n", $count;
+ }
+ $oldcount = $count;
+ while (1) {
+ @newstat = stat($mbox);
+ if (!@newstat) {
+ print "\005{Rk} ??? \005{-}\n";
+ exit 1;
+ }
+ last if $newstat[7] != $oldstat[7] || $newstat[9] != $oldstat[9];
+ sleep 1;
+ }
+ @oldstat = @newstat;
+}