-
Notifications
You must be signed in to change notification settings - Fork 2
/
msummary
executable file
·153 lines (141 loc) · 3.8 KB
/
msummary
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env perl
# msummary - summarize a single message on stdin
# There is special-purpose knowledge of specific mailing lists in
# here, because I prefer to see a summary of OpenBSD changelog
# messages so I don't have to guess what they are about. YMMV.
use strict;
use warnings;
use POSIX qw(strftime);
use Time::ParseDate;
use Term::ANSIColor;
our $COLORED = $ENV{'MSUMMARY_COLOR'} ? int($ENV{'MSUMMARY_COLOR'}) : 1;
our $MAXSUBJ = 0;
our $hdr = '';
our $interesting = qr/^(from|to|cc|date|subject|x-spam-flag|list-id)$/i;
our $something = 0;
our %all;
our @now = localtime(time);
our $midnight = parsedate(strftime("%Y-%m-%d 00:00:00",@now));
our $thisyear = parsedate(strftime("%Y-01-01 00:00:00",@now));
sub clean {
my($str) = @_;
$str =~ s/^\s+//;
$str =~ s/\s+$//;
$str =~ s/\s+/ /gs;
return $str;
}
sub parse {
if ($hdr !~ /^([^:]+?):\s*(\S.*)$/) {
warn("MALFORMED: $hdr\n");
} else {
my($n,$v) = ($1,$2);
if ($n =~ $interesting) {
$n = lc($n);
if ($all{$n}) {
$all{$n} .= ",$v";
} else {
$all{$n} = $v;
}
++$something;
}
}
$hdr = '';
}
sub docolor { $COLORED ? color(@_) : '' }
sub printsumm {
my $spam = '';
if ($all{'x-spam-flag'} && ($all{'x-spam-flag'} eq 'YES')) {
$spam = docolor('red').'*'.docolor('reset').' ';
}
my $list = '';
if ($all{'list-id'}) {
my $lid = $all{'list-id'};
$lid = $1 if $lid =~ /^.*<([^>]+)>$/;
$list = docolor('yellow')."[$lid]".docolor('reset').' ';
}
my $rawdate = $all{'date'};
my $t = parsedate($rawdate) || 0;
my $tfmt = "%H:%M";
if ($t < $midnight) {
$tfmt = "%d %b %H:%M";
if ($t < $thisyear) {
$tfmt = "%d %b %Y %H:%M";
}
}
my $d = strftime($tfmt,localtime($t));
my $date = docolor('bold blue')."$d ".docolor('reset');
my $from = $all{'from'} ?
docolor('green').$all{'from'}.docolor('reset') :
docolor('red').'?'.docolor('reset');
my $to = '?';
if ($all{'to'} || $all{'cc'}) {
$to = $all{'to'} || '';
if ($all{'cc'}) {
$to .= ',' if $to;
$to .= $all{'cc'};
}
}
my $subj = $all{'subject'};
if ($MAXSUBJ && (length($subj) > $MAXSUBJ)) {
$subj = substr($subj,0,$MAXSUBJ)." ...";
}
$subj = '-no subject-' unless $subj;
$subj = docolor('bold green').$subj.docolor('reset');
my $cont = '';
$cont = docolor('magenta').' ... '.$all{'_content'}.docolor('reset')
if exists $all{'_content'};
print STDERR "${date}${spam}${list}${from} -> ${to}: ${subj}${cont}\n";
}
sub summ {
my($fh) = @_;
while (defined(my $line = <$fh>)) {
$line =~ s/\n+$//;
last if $line =~ /^\s*$/;
parse() if $hdr && $line =~ /^(\S[^:]+):\s*(\S.*)$/;
$line =~ s/^[\t\s]+/ /;
$hdr .= $line;
}
parse() if $hdr;
if ($all{'list-id'} &&
($all{'list-id'} =~ /.*-changes\.openbsd\.org/)) {
# OpenBSD CVS change log emails
my($module,$mod,@mods,$in_mods,@dirs,$nfiles,$summstr);
$nfiles = 0;
while (defined(my $line = <$fh>)) {
chomp($line);
if ($line =~ /^Module\sname:\s+(\S.*)$/) {
$module = $1;
} elsif ($line =~ /^(Modified|Removed)\sfiles:/) {
$mod = substr(lc($1),0,3);
push(@mods,$mod);
$in_mods = 1;
} elsif ($line =~ /^Log\smessage:/) {
if (!$in_mods) {
# import
$line = <$fh> || ''; # next line
$line = clean($line);
$summstr = $line;
last;
} # else it's a log message farther down, ignore
} elsif ($in_mods) {
my @parts = map { clean($_) } split(/:/, $line);
if (@parts == 2) {
push(@dirs,shift(@parts));
}
if (@parts) {
my @f = split(/\s+/,$parts[0]);
$nfiles += scalar(@f);
}
} # else ignore it
}
my $filez = ($nfiles == 1) ? "file": "files";
my $ndirs = scalar(@dirs);
my $dirz = ($ndirs == 1) ? "dir": "dirs";
my $summ = '';
$summ .= '<'.join(",",@mods).'> ';
$summstr ||= "$nfiles $filez in $ndirs $dirz: @dirs";
$all{'_content'} = $summ . $summstr;
}
printsumm() if $something;
}
summ(\*STDIN);