3 # summarise-mailbox-preserving-privacy \
4 # [<our options>] [--] <email>
6 # -S<subject> default: "summary of messages on <mailname>"
7 # -F<state file> default: $HOME/.summarise-mailbox/last<from options>
8 # -f<mailbox> passed to from(1) must use -F if it contains / or |
9 # -s<sender> passed to from(1) must use -F if it contains / or |
10 # -q throw away stderr and always exit 0
11 # -- end of our options
13 use strict (qw(refs));
16 use Fcntl (qw(:flock));
19 $sendmail= '/usr/sbin/sendmail -odi -oee -oi -t';
22 while (@ARGV && $ARGV[0] =~ m/\-/) {
25 push @from_options, $_;
32 open STDERR, ">/dev/null";
33 eval('END { $?=0; }');
37 die "$0: unknown option \`$_'\n";
41 $emailto= shift @ARGV;
43 unless (defined $subject) {
45 open M, "/etc/mailname" or die $!;
46 defined($our_hostname= <M>) or die $!;
49 $subject= "summary of messages on $our_hostname";
52 unless (defined $statefile) {
54 die "$0: -F needed with that -f or -s\n"
55 if grep m:[|/]:, @from_options;
56 die "$0: no HOME in environment\n" unless defined $ENV{'HOME'};
57 $dir= $ENV{'HOME'}.'/.summarise-mailbox';
58 mkdir $dir, 02700 or $!==&EEXIST or die "$dir: $!";
59 $statefile= $dir.'/last'.
60 join('|',@from_options);
63 $statefile= "./$statefile" unless $statefile =~ m,^/,;
64 $lockfile= $statefile.'.lock';
65 $errfile= $statefile.'.err';
67 open L, "+> $lockfile" or die "$lockfile: $!";
68 flock L, LOCK_EX or die "$lockfile: $!";
71 open STDERR, "> $errfile";
75 my ($incr, $lasttime) = @_;
77 while (defined($_= <F>)) {
78 print N or die "$statefile.new: $!" if $incr>0;
81 m/^From .* (\w+ \w+ \d+ [0-9:]+ \d+)$/ or die "$_ ?";
88 if (open F, "< $statefile\0") {
89 $old_lasttime= parse(-1);
91 } elsif ($! != &ENOENT) {
95 open N, "> $statefile.new" or die "$statefile.new: $!";
97 $child= open F, "-|"; defined $child or die $!;
99 exec "from",@from_options;
104 $new_lasttime= parse(+1);
105 $?=0; close F or die "$? $!";
107 close N or die "$statefile.new: $!";
109 if ($new_lasttime ne $old_lasttime and $new_lasttime ne '') {
110 push @reasons, "Timestamp of last message in mailbox changed.";
113 map { $total_more+=$_ if $_>0 } values %have;
116 push @reasons, "$total_more message(s)".
117 " which were not previously present.";
119 push @reasons, "More messages than previously reported.";
122 exit 0 unless @reasons;
124 $new_have= $have - $did_have;
130 Regarding your mailbox @from_options:
132 Changes detected since last report:
136 $msg .= join "\n", map { " $_" } @reasons;
142 There are $new_have message(s).
143 The last is dated: $new_lasttime.
150 open S, "| $sendmail" or die $!;
151 print S $msg or die $!;
152 $?=0; close S or die "$? $!";
154 rename "$statefile.new", $statefile or die $!;