nottoomuch-addresses-1.2.sh nottoomuch-addresses-1.3.sh
10# $ nottoomuch-addresses.sh $10# $ nottoomuch-addresses.sh $
11#11#
12# Created: Thu 27 Oct 2011 17:38:46 EEST too12# Created: Thu 27 Oct 2011 17:38:46 EEST too
13# Last modified: Tue 06 Dec 2011 20:00:00 EET too13# Last modified: Mon 12 Dec 2011 17:41:05 EET too
1414
15# Add this to your notmuch elisp configuration file:15# Add this to your notmuch elisp configuration file:
16#16#
2222
23# HISTORY23# HISTORY
24#24#
25# Version 1.3 2011-12-12 15:41:05 UTC
26# * Changed to store/show addresses in 'newest first' order
27# * Changed addresses file header to force address file rebuild.
28#
25# Version 1.2 2011-12-06 18:00:00 UTC29# Version 1.2 2011-12-06 18:00:00 UTC
26# * Changed search work case-insensitively -- grep(1) does it locale-aware.30# * Changed search work case-insensitively -- grep(1) does it locale-aware.
27# * Changed this program execute from /bin/sh (wrapper).31# * Changed this program execute from /bin/sh (wrapper).
36# * Initial release40# * Initial release
3741
38#!perl42#!perl
43# line 44
3944
40use 5.8.1;45use 5.8.1;
41use strict;46use strict;
4954
50my $configdir = ($ENV{XDG_CONFIG_HOME}||$ENV{HOME}.'/.config').'/nottoomuch';55my $configdir = ($ENV{XDG_CONFIG_HOME}||$ENV{HOME}.'/.config').'/nottoomuch';
51my $adbpath = $configdir . '/addresses';56my $adbpath = $configdir . '/addresses';
57my $ignpath = $configdir . '/addresses.ignore';
5258
53unless (@ARGV)59unless (@ARGV)
54{60{
75 exit ( Pod::Perldoc->run() );81 exit ( Pod::Perldoc->run() );
76}82}
7783
78my $ignpath = $configdir . '/addresses.ignore';
79
80my @list;84my @list;
8185
82if ($ARGV[0] eq '--update')86if ($ARGV[0] eq '--update')
95 unlink $adbpath if defined $ARGV[1] and $ARGV[1] eq '--rebuild';99 unlink $adbpath if defined $ARGV[1] and $ARGV[1] eq '--rebuild';
96100
97 my ($sstr, $acount) = (0, 0);101 my ($sstr, $acount) = (0, 0);
98 my $stime = time;
99 if (-s $adbpath) {102 if (-s $adbpath) {
100 die "Cannot open '$adbpath': $!\n" unless open I, '<', $adbpath;103 die "Cannot open '$adbpath': $!\n" unless open I, '<', $adbpath;
101 no warnings;104 sysread I, $_, 18;
102 $sstr = int <I>;105 # new header: "v2/dd/dd/dd/dd/dd/" where / == '\n'
103 }106 if (/^v2\n(\d\d)\n(\d\d)\n(\d\d)\n(\d\d)\n(\d\d)\n$/) {
104 if ($sstr > 1e8) { # arbitrary value: 86400 * 1157.4 (1973-03-03 09:46:40Z)
105 $sstr -= 86400 * 7; # one week extra to (re)look.107 $sstr = "$1$2$3$4$5" - 86400 * 7; # one week extra to (re)look.
108 $sstr = 0 if $sstr < 0;
109 }
110 close I if $sstr == 0;
111 }
112 if ($sstr > 0) {
106 print "Updating '$adbpath', since $sstr.\n";113 print "Updating '$adbpath', since $sstr.\n";
107 $sstr .= '..';114 $sstr .= '..';
108 @list = grep { chomp; length; } <I>;
109 close I;
110 $acount = scalar @list;
111 }115 }
112 else {116 else {
113 die "'$adbpath' exists but contains unknown content!\n" if -s $adbpath;
114 print "Creating '$adbpath'. This may take some time...\n";117 print "Creating '$adbpath'. This may take some time...\n";
115 $sstr = '*';118 $sstr = '*';
116 }119 }
117 my %hash = map { $_ => 1 } @list;120 my %hash;
118 if (-f $ignpath) {121 if (-f $ignpath) {
119 die "Cannot open '$ignpath': $!\n" unless open I, '<', $ignpath;122 die "Cannot open '$ignpath': $!\n" unless open J, '<', $ignpath;
120 while (<I>) {123 while (<J>) {
121 chomp;124 chomp;
122 $hash{$_} = 1;125 $hash{$_} = 1;
123 }126 }
124 close I;127 close J;
125 }128 }
129
130 my $sometime = time;
131 die "Cannot open '$adbpath.new': $!\n" unless open O, '>', $adbpath.'.new';
132 $_ = $sometime; s/(..)/$1\n/g;
133 print O "v2\n$_";
126134
127 # The following code block is from Email::Address, almost verbatim.135 # The following code block is from Email::Address, almost verbatim.
128 # The reasons to snip code I instead of just 'use Email::Address' are:136 # The reasons to snip code I instead of just 'use Email::Address' are:
197 # In this particular purpose the cache code used in...205 # In this particular purpose the cache code used in...
198 my %seen; # ...Email::Address is "replaced" by %seen & %hash.206 my %seen; # ...Email::Address is "replaced" by %seen & %hash.
199207
200 my $ptime = $stime + 5;208 my $ptime = $sometime + 5;
209 my $new = 0;
201 $| = 1;210 $| = 1;
202 open I, '-|', qw/notmuch show/, $sstr;211 open P, '-|', qw/notmuch show/, $sstr;
203 while (<I>) {212 while (<P>) {
204 next unless /^From:\s/i or /^To:\s/i or /^Cc:\s/i;213 next unless /^From:\s/i or /^To:\s/i or /^Cc:\s/i;
205 s/\s+$//;214 s/\s+$//;
206 s/^.*?:\s+//;215 s/^.*?:\s+//;
207216
208 if (time > $ptime) {217 if (time > $ptime) {
209 my $c = qw(/ - \ |)[int ($ptime / 5) % 4];218 my $c = qw(/ - \ |)[int ($ptime / 5) % 4];
210 print "$c new addresses gathered: ", scalar @list - $acount, "\r";219 print "$c addresses gathered: ", $new, "\r";
211 $ptime += 5;220 $ptime += 5;
212 }221 }
213222
278 tr/_/ / unless /@/; 1; } @comments;287 tr/_/ / unless /@/; 1; } @comments;
279 #@comments = grep { defined } @comments;288 #@comments = grep { defined } @comments;
280289
281 $_ = join ' ', @phrase, $userhost, @comments;290 $_ = join(' ', @phrase, $userhost, @comments) . "\n";
282 next if defined $hash{$_};291 next if defined $hash{$_};
283 push @list, $_;292 print O $_;
284 $hash{$_} = 1;293 $hash{$_} = 1;
294 $new++;
285 }295 }
286 # --8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<--296 # --8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<--
287 }297 }
298 undef %seen;
299 close P;
300 my $all = $new;
301 if ($sstr ne '*') {
302 my $dropped = 0;
303 while (<I>) {
304 $dropped++, next unless /@/ and ! defined $hash{$_};
305 $all++;
306 print O $_;
307 }
308 close I;
309 $new -= $dropped;
310 }
311 close O;
288 undef %hash;312 undef %hash;
289 undef %seen;313 #link $adbpath, $adbpath . '.' . $sometime;
290 my $etime = time;314 rename $adbpath . '.new', $adbpath or
291 open O, '>', $adbpath or die "Cannot write to '$adbpath': $!\n";315 die "Cannot rename '$adbpath.new' to '$adbpath': $!\n";
292 printf O "%010d\n", $etime;316 $sometime = time - $sometime;
293 print O join("\n", sort @list), "\n";
294 close O;
295 my $ecount = scalar @list;
296 my $count = $ecount - $acount;
297 $etime -= $stime;
298 print "Added $count addresses in $etime seconds. ";317 print "Added $new addresses in $sometime seconds. ";
299 print "Total number of addresses: $ecount.\n";318 print "Total number of addresses: $all.\n";
300 exit 0;319 exit 0;
301}320}
302321
318337
319=head1 VERSION338=head1 VERSION
320339
3211.2 (2011-12-06)3401.3 (2011-12-12)
322341
323=head1 OPTIONS342=head1 OPTIONS
324343

Generated by htmldiff.sh