-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmnewdirs
executable file
·107 lines (99 loc) · 3.06 KB
/
mnewdirs
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env perl
##
# find maildirs with new messages by default spit out lines like this:
# dir count
# in this form dir will have /new at the end
# if the -n option is specified, we instead spit out:
# maildir
# which is just dirname $dir
#
# we normally ignore folders whose names end with -a-<yyyy>-<mm>-<dd>
# (e.g. BankMail-a-2021-01-01) so that this convention can be used to
# name archive folders that should not normally be displayed. use the
# -a option to disable this and display all folders with new mail
##
use strict;
use warnings;
use File::Basename;
use File::Find;
use Getopt::Std;
our $VERBOSE = $ENV{VERBOSE} || 0;
our %newdirs = (); # dir => #files
our %opts;
our $MBLAZE = $ENV{'MBLAZE'} || join('/',$ENV{'HOME'},'.mblaze');
sub mhdr {
open(MHDR, "mhdr -h $_[0] $MBLAZE/profile |")
or die "mhdr: $!";
my $val = <MHDR>;
close(MHDR);
chomp($val) if $val;
$val //= $_[1];
foreach my $envar (sort keys %ENV) {
my $enval = $ENV{$envar};
$val =~ s/\b\$$envar\b/$enval/g;
}
return $val;
}
our $maildir = mhdr("MaildirBase") || $ENV{'MAILDIR'} ||
join('/',$ENV{'HOME'},'mail');
$maildir = $ENV{"HOME"}."/Maildir" unless -d $maildir;
die "no maildir and no MAILDIR set!?\n" unless -d $maildir;
our $archive_regexp = mhdr("ArchiveRegexp",'-a-\d{4}-\d{2}-\d{2}');
our $archive_re = qr/$archive_regexp/;
our $spam_regexp = mhdr("SpamFolderRegexp",'^Spam.*');
our $spam_re = qr/$spam_regexp/;
our @newdirs_list = (map { ($_ !~ m,^/,)?"${maildir}/$_":"$_" }
split(/\s*,\s*/,mhdr("NewMailFolderList","$maildir")));
our $total_msgs = 0;
sub check {
warn("### check: $File::Find::name\n")
if $VERBOSE && $VERBOSE > 2;
return undef unless $opts{'a'} || $File::Find::dir !~ $archive_re;
return undef unless $opts{'a'} || $File::Find::dir !~ $spam_re;
warn("### !spam: $File::Find::name\n")
if $VERBOSE && $VERBOSE > 3;
if ($File::Find::name =~ /\/new\/\d+\..*$/) {
++$total_msgs;
my $already = $newdirs{$File::Find::dir}++;
warn("### -- $File::Find::dir: matched\n")
unless !$VERBOSE || $already;
warn("### ++ $File::Find::dir => $total_msgs\n")
if $VERBOSE && $VERBOSE > 3;
}
}
sub mungdir {
my($dir) = @_;
if (!$opts{'n'}) {
$dir =~ s,^${maildir}/,,;
}
return dirname($dir);
}
sub maildirz {
map { $_ .= '/new' unless $_ =~ /\/new$/; $_ }
grep { $_ !~ /.*\/(cur|tmp|\.|\..)$/ }
map { glob($_) } @_
}
# main program
getopts('antTov',\%opts);
our $fmt = $opts{'n'} ? "%s\n" : "%s %d\n";
@newdirs_list = () if $opts{'o'};
unshift(@newdirs_list,@ARGV);
@newdirs_list = maildirz(@newdirs_list);
$VERBOSE ||= $opts{'v'};
warn("## $0 opts: ".join(', ',map{"$_=$opts{$_}"} keys(%opts))."\n")
if $VERBOSE;
warn("## ".scalar(@newdirs_list)." newdirs_list: ".join(', ',@newdirs_list)."\n")
if $VERBOSE;
warn("## spam_re=/$spam_re/ archive_re=/$archive_re/\n")
if $VERBOSE;
find(\&check,@newdirs_list);
if ($opts{t} || $opts{T}) {
my $total_folders = scalar(keys(%newdirs));
print "$total_msgs in $total_folders\n";
exit(0) if $opts{t};
}
foreach (sort keys %newdirs) {
my @out = (mungdir($_));
push(@out, $newdirs{$_}) unless $opts{'n'};
printf($fmt,@out);
}