From: ianmdlvl Date: Wed, 22 Nov 2006 19:53:31 +0000 (+0000) Subject: new script summarise-mailbox-preserving-privacy X-Git-Tag: debian_version_4_1_11 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?a=commitdiff_plain;ds=sidebyside;h=812c3e1f13c35ab65d0eb4aafbaab0ac5d4ea8b8;hp=1f82b533574de79be2ffc793e55449b54566e6a2;p=chiark-utils.git new script summarise-mailbox-preserving-privacy --- diff --git a/debian/changelog b/debian/changelog index 424dbcf..9da722f 100644 --- a/debian/changelog +++ b/debian/changelog @@ -4,8 +4,9 @@ chiark-utils (4.1.11) unstable; urgency=low on 2.6.x, but not with 2.4.x due to stupid stupid LVM bug where sizes are reported in 512-byte kilobytes but only sometimes. Also fixed to cope better with dm-based LVM volumes. + * New script `summarise-mailbox-preserving-privacy'. - -- + -- Ian Jackson Wed, 22 Nov 2006 19:53:17 +0000 chiark-utils (4.1.10) unstable; urgency=low diff --git a/scripts/Makefile b/scripts/Makefile index c029306..2d78f27 100644 --- a/scripts/Makefile +++ b/scripts/Makefile @@ -24,7 +24,7 @@ include ../settings.make SCRIPTS= palm-datebook-reminders random-word expire-iso8601 \ genspic2gnuplot gnucap2genspic ngspice2genspic \ cvs-repomove cvs-adjustroot remountresizereiserfs \ - hexterm + hexterm summarise-mailbox-preserving-privacy MANPAGES1= palm-datebook-reminders CSCRIPTS= named-conf diff --git a/scripts/summarise-mailbox-preserving-privacy b/scripts/summarise-mailbox-preserving-privacy new file mode 100755 index 0000000..fe39a6e --- /dev/null +++ b/scripts/summarise-mailbox-preserving-privacy @@ -0,0 +1,154 @@ +#!/usr/bin/perl +# usage: +# summarise-mailbox-preserving-privacy \ +# [] [--] +# our options: +# -S default: "summary of messages on " +# -F default: $HOME/.summarise-mailbox/last +# -f passed to from(1) must use -F if it contains / or | +# -s passed to from(1) must use -F if it contains / or | +# -q throw away stderr and always exit 0 +# -- end of our options + +use strict (qw(refs)); +use POSIX; +use IO::Handle; +use Fcntl (qw(:flock)); + +@from_options=(); +$sendmail= '/usr/sbin/sendmail -odi -oee -oi -t'; + +umask 077; +while (@ARGV && $ARGV[0] =~ m/\-/) { + $_= shift; + if (m/^\-[fs]/) { + push @from_options, $_; + } elsif (s/^\-S//) { + $subject= $_; + } elsif (s/^\-F//) { + $statefile= $_; + } elsif (s/^\-q$//) { + $quiet= 1; + open STDERR, ">/dev/null"; + eval('END { $?=0; }'); + } elsif (m/^\-\-$/) { + last; + } else { + die "$0: unknown option \`$_'\n"; + } +} +die unless @ARGV==1; +$emailto= shift @ARGV; + +unless (defined $subject) { + my ($our_hostname); + open M, "/etc/mailname" or die $!; + defined($our_hostname= ) or die $!; + chomp($our_hostname); + close M; + $subject= "summary of messages on $our_hostname"; +} + +unless (defined $statefile) { + my ($dir); + die "$0: -F needed with that -f or -s\n" + if grep m:[|/]:, @from_options; + die "$0: no HOME in environment\n" unless defined $ENV{'HOME'}; + $dir= $ENV{'HOME'}.'/.summarise-mailbox'; + mkdir $dir, 02700 or $!==&EEXIST or die "$dir: $!"; + $statefile= $dir.'/last'. + join('|',@from_options); +} + +$statefile= "./$statefile" unless $statefile =~ m,^/,; +$lockfile= $statefile.'.lock'; +$errfile= $statefile.'.err'; + +open L, "+> $lockfile" or die "$lockfile: $!"; +flock L, LOCK_EX or die "$lockfile: $!"; + +if ($quiet) { + open STDERR, "> $errfile"; +} + +sub parse($) { + my ($incr, $lasttime) = @_; + $lasttime= ''; + while (defined($_= )) { + print N or die "$statefile.new: $!" if $incr>0; + $have{$_} += $incr; + $have += $incr; + m/^From .* (\w+ \w+ \d+ [0-9:]+ \d+)$/ or die "$_ ?"; + $lasttime= $1; + } + die $! if F->error; + return $lasttime; +} + +if (open F, "< $statefile\0") { + $old_lasttime= parse(-1); + close F or die $!; +} elsif ($! != &ENOENT) { + die "$statefile $!"; +} + +open N, "> $statefile.new" or die "$statefile.new: $!"; + +$child= open F, "-|"; defined $child or die $!; +if (!$child) { + exec "from",@from_options; + die "$!"; +} + +$did_have= $have; +$new_lasttime= parse(+1); +$?=0; close F or die "$? $!"; + +close N or die "$statefile.new: $!"; + +if ($new_lasttime ne $old_lasttime and $new_lasttime ne '') { + push @reasons, "Timestamp of last message in mailbox changed."; +} + +map { $total_more+=$_ if $_>0 } values %have; + +if ($total_more) { + push @reasons, "$total_more message(s)". + " which were not previously present."; +} elsif ($have>0) { + push @reasons, "More messages than previously reported."; +} + +exit 0 unless @reasons; + +$new_have= $have - $did_have; + +$msg= <