-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmail-notify.pl
executable file
·182 lines (174 loc) · 5.06 KB
/
mail-notify.pl
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/perl
use strict;
use Number::Format qw(:subs);
use Mail::Message::Field::Full;
my $notify_long = "/usr/bin/notify-send -u critical -i kmail -t 30000 ";
my $notify_short = "/usr/bin/notify-send -u normal -i kmail -t 10000 ";
my $notify_middle = "/usr/bin/notify-send -u normal -i kmail -t 20000 ";
my $notify_appointment = "/usr/bin/notify-send -u low -i korganizer -t 900000 ";
my $subject;
my $body;
my $from;
my $date;
my $folder;
my $info;
my $show_long = 0;
my $show_appointment = 0;
if( $ARGV[0] eq "--impressions" ) {
doImpressions();
} elsif( $ARGV[0] eq "--reminder" ) {
doReminder();
} else {
doBackground();
}
sub doBackground {
# Normal mode, parse procmail log
open LOG, "<$ENV{HOME}/.procmail.log";
seek LOG, 0, 2;
while ( 1 ) {
$_ = <LOG>;
chomp;
if ( length $_ ) {
if ( /Subject\: (.*)$/i ) {
$body = Mail::Message::Field::Full->decode( $1 );
#print "body=$body\n";
if ( $body =~ /(adserv|adssl|ad\d\d\d|select|deliverydb|repsrv|cache|opensw).*(CRITICAL|crashed)/ ) {
$subject = uc( $1 );
$subject =~ s/"//g;
$subject =~ s/>/>/g;
$subject =~ s/</</g;
#print "subject=$subject\n";
$show_long = 1;
}
$body =~ s/<br>/\n/g;
$body =~ s/<tab>/\t/g;
} elsif ( /From ([^ ]+) (.*)$/i ) {
$from = $1;
$date = $2;
$from =~ s/"//g;
$from =~ s/>/>/g;
$from =~ s/</</g;
#print "from=$from\n";
} elsif ( /Folder\: \/home\/osar\/([^ ]+)\/new.*/ ) {
$folder = $1;
#print "folder='$folder'\n";
if ( $folder eq "Maildir" or
$folder eq "Maildir/.Datacenter" or
$folder eq "Maildir/.Services" ) {
$info = "<i>$from</i>\n<u>$folder</u>\n<b>$body</b>";
if ( $show_long ) {
#print "$notify_long \"$subject\" \"$info\"\n";
system "$notify_long \"$subject - $date\" \"$info\"";
} else {
#print "$notify_short \"Info\" \"$info\"\n";
system "$notify_short \"Info - $date\" \"$info\"";
}
}
$show_long = 0;
}
} else {
sleep 5;
seek F, 0, 1;
}
while( my $f=</tmp/mail-notify-*> ) {
chomp $f;
if( $f =~ /mail-notify-(\d+)$/ ) {
my $ts = $1;
if( time() - $ts < 60 ) {
open NOTIFY, "<$f";
my @cmd = <NOTIFY>;
close NOTIFY;
system join( "", @cmd );
}
unlink $f;
}
}
}
close F;
}
sub doReminder {
my @mail = <STDIN>;
my $type = "";
my $title = "";
my $start = "";
my $end = "";
my $dur = "";
my $loc = "";
foreach my $line (@mail) {
if( $line =~ /^Start: (.*)$/ ) {
$start = $1;
} elsif( $line =~ /^End: (.*)$/ ) {
$end = $1;
} elsif( $line =~ /^Duration: (.*)$/ ) {
$dur = $1;
} elsif( $line =~ /^Location: (.*)$/ ) {
$loc = $1;
} elsif( $line =~ /^Type: (.*)$/ ) {
$type = $1;
} elsif( $line =~ /^Title: (.*)$/ ) {
$title = $1;
}
}
open NOTIFY, ">/tmp/mail-notify-".time();
print NOTIFY "$notify_appointment \"$type - $start\" \"<b>Title:</b>\t\t$title\n<b>Duration:</b>\t$dur\n<b>Location:</b>\t$loc\"";
close NOTIFY;
print join( "", @mail );
}
sub doImpressions {
my @mail = <STDIN>;
my $lastweek = 0;
my $yesterday = 0;
for( my $i = 0; $i <= $#mail; $i++ ) {
if( index( $mail[$i], "COMPARE TO LASTWEEK" ) > -1 ) {
$lastweek = 1;
$i++;
} elsif( index( $mail[$i], "COMPARE TO YESTERDAY" ) > -1 ) {
$yesterday = 1;
$i++;
} elsif( $lastweek == 1 && index( $mail[$i], "-----" ) == 0 ) {
$lastweek = $mail[$i-1];
chomp $lastweek;
$lastweek =~ s/^ +//g;
$lastweek =~ s/ +/ /g;
my( $day, $lw, $today, $perc ) = split / /, $lastweek;
$lw = format_picture( $lw, '###,###,###,###' );
$lw =~ s/^ +//;
$today = format_picture( $today, '###,###,###,###' );
$today =~ s/^ +//;
$lastweek = " \t$lw\t<b>$today\t$perc</b>\n";
my $sum = $mail[$i+1];
chomp $sum;
$sum =~ s/ +/ /g;
( $day, $lw, $today, $perc ) = split / /, $sum;
$lw = format_picture( $lw, '###,###,###,###' );
$lw =~ s/^ +//;
$today = format_picture( $today, '###,###,###,###' );
$today =~ s/^ +//;
$lastweek.= "<b>S:</b>\t$lw\t<b>$today\t$perc</b>";
} elsif( $yesterday == 1 && index( $mail[$i], "-----" ) == 0 ) {
$yesterday = $mail[$i-1];
chomp $yesterday;
$yesterday =~ s/^ +//g;
$yesterday =~ s/ +/ /g;
my( $day, $lw, $today, $perc ) = split / /, $yesterday;
$lw = format_picture( $lw, '###,###,###,###' );
$lw =~ s/^ +//;
$today = format_picture( $today, '###,###,###,###' );
$today =~ s/^ +//;
$yesterday = " \t$lw\t<b>$today\t$perc</b>\n";
my $sum = $mail[$i+1];
chomp $sum;
$sum =~ s/ +/ /g;
( $day, $lw, $today, $perc ) = split / /, $sum;
$lw = format_picture( $lw, '###,###,###,###' );
$lw =~ s/^ +//;
$today = format_picture( $today, '###,###,###,###' );
$today =~ s/^ +//;
$yesterday.= "<b>S:</b>\t$lw\t<b>$today\t$perc</b>";
}
}
open NOTIFY, ">/tmp/mail-notify-".time();
print NOTIFY "$notify_middle \"Impressions $ARGV[1]\" \"<b>Last Week:</b>\n$lastweek\n\n<b>Yesterday:</b>\n$yesterday\"";
close NOTIFY;
print join( "", @mail );
}