summaryrefslogtreecommitdiffstats
path: root/etc/countmail
blob: 974f1436b767a831052b743d989eb6bde06ee7a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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;
}