-
Notifications
You must be signed in to change notification settings - Fork 0
/
spammonitor.pl
executable file
·228 lines (211 loc) · 6.95 KB
/
spammonitor.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/usr/bin/env perl
# spammonitor.pl
# Author: Daniel Davidson <danield@igb.illinois.edu>, David Slater <dslater@igb.illinois.edu>
#
# Script checks a person's Junk folder and sends an email digest of spam messages
#
use strict;
use warnings;
use local::lib './perl5';
use Email::Simple;
use Email::Folder;
use Email::Delete qw[delete_message];
use Date::Calc qw(Delta_Days check_date);
use Net::SMTP;
use Getopt::Long;
use File::Basename;
sub help() {
print "Usage: $0\n";
print "Script checks a person's Junk folder and sends an email digest of spam messages\n";
print "\t-u|--user User to run on (Defaults to all Users)\n";
print "\t--dry-run Does dry run only\n";
print "\t--verbose Verbose messaging\n";
print "\t-h|--help Prints this help\n";
exit 0;
}
sub send_mail {
my $email = $_[0];
my $message = $_[1];
my $from_email = $_[2];
my $email_subject = $_[3];
my $from_name = $_[4];
my $mail_host = $_[5];
my $smtp = Net::SMTP->new($mail_host);
$smtp->mail($from_email);
$smtp->to($email);
$smtp->data();
$smtp->datasend("To: $email\n");
$smtp->datasend("Subject: $email_subject\n");
$smtp->datasend("Content-Type: text/html; charset=\"US-ASCII\"\n");
$smtp->datasend("From: $from_name <$from_email>\n");
$smtp->datasend("\n");
$smtp->datasend("$message");
$smtp->dataend();
$smtp->quit;
}
sub get_name {
my $username = $_[0];
#my $cmd = "ldapsearch -x 'uid=$username' cn | grep 'cn:' | cut -f 2 -d ':' | xargs";
my $cmd = "getent passwd $username | cut -f 5 -d ':'";
my $fullname = `$cmd`;
return($fullname);
}
my $dryrun = 0;
my $verbose = 0;
my $user = "";
GetOptions ("u|user=s" => \$user,
"dry-run" => \$dryrun,
"verbose" => \$verbose,
"h|help" => sub { help() },
) or die("\n");
my $delta=10;
my $sleep=1;
my $homedirectory1='/home/a-m';
my $homedirectory2='/home/n-z';
my $maildirectory='mail';
my $spamfolder='Junk';
my $mailhost='localhost';
my $fromemail='do-not-reply@igb.illinois.edu';
my $adminemail='help@igb.illinois.edu';
my $fromname='IGB Detect Spam';
my $domain='@igb.illinois.edu';
my %monthtonum = (
Jan => '1',
Feb => '2',
Mar => '3',
Apr => '4',
May => '5',
Jun => '6',
Jul => '7',
Aug => '8',
Sep => '9',
Oct => '10',
Nov => '11',
Dec => '12'
);
my @now=localtime(time);
$now[4]++;
$now[5]+=1900;
my $current_path = dirname(__FILE__);
my $css_file = "$current_path/bootstrap.min.css";
open FILE, $css_file or die "Couldn't open file: $!";
my $css = do {local $/; <FILE> };
close FILE;
my @uids = ();
if ($user ne "") {
my @cmd = ("id $user > /dev/null 2>&1");
my $user_exists = system(@cmd) == 0 or die "User $user does not exist\n";
@uids[0] = $user;
}
else { #Grab all users
opendir(HOMEDIR,$homedirectory1);
@uids=grep ! /^\./, readdir(HOMEDIR);
closedir HOMEDIR;
opendir(HOMEDIR,$homedirectory2);
my @uids2=grep ! /^\./, readdir(HOMEDIR);
closedir HOMEDIR;
push(@uids,@uids2);
}
while(my $uid=shift(@uids)) {
#check if user is in a-m or n-z
my $full_spam_path;
if($uid=~/^([a-m])/) {
$full_spam_path="/home/a-m/$uid/$maildirectory/$spamfolder";
}
elsif($uid=~/^([n-z])/) {
$full_spam_path="/home/a-m/$uid/$maildirectory/$spamfolder";
}
if(-e "$full_spam_path") {
my $box=Email::Folder->new("$full_spam_path") or die "Unable to open $full_spam_path";
my @deletemessages=();
my %todaysspam=();
while(my $mail=$box->next_message) {
my $body=$mail->body;
my $subject=$mail->header("Subject");
my $from=$mail->header("From");
my $date=$mail->header("Date");
my $messageid=$mail->header('Message-ID');
my @date=split(/\s+/,$date);
if($date[3]=~/\d{4}/ and $monthtonum{$date[2]} and $date[1]>=1 and $date[1]<=31) {
if(check_date($date[3],$monthtonum{$date[2]},$date[1])) {
#print "$date:@date[3],$monthtonum{@date[2]},@date[1]\n";
my $deltadays=Delta_Days($date[3],$monthtonum{$date[2]},$date[1],
$now[5],$now[4],$now[3]);
if($deltadays==0 and $from ne "") {
$todaysspam{$messageid}{'from'}=$from;
$todaysspam{$messageid}{'date'}="$date[2]-$date[1]-$date[3]";
$todaysspam{$messageid}{'subject'}=$subject;
}
unless($deltadays < $delta) {
if ($verbose) {
print "Deleted message with subject $subject and from $from $deltadays day old\n";
}
push(@deletemessages,$messageid);
}
else {
if ($verbose) {
print "Keep message with subject $subject from $from\n";
}
}
}
else {
push(@deletemessages,$messageid);
}
}
else {
if ($verbose) {
print "Invalid date header with subject $subject from $from, deleting message\n";
}
push(@deletemessages,$messageid);
}
}
if (!$dryrun) {
foreach my $deletemessage (@deletemessages) {
delete_message(
from => "$full_spam_path",
matching => sub { my $message=shift; $message->header('Message-ID') =~ $deletemessage; }
);
}
}
sleep($sleep);
my $fullname = get_name($uid);
my $to = "$uid$domain";
my $send_subject = "IGB Spam Received $now[4]-$now[3]-$now[5]";
my $send_message = "<html><head>\n";
$send_message .= "<style media=\"all\" type=\"text/css\">\n";
$send_message .= "$css";
$send_message .= "</style></head>\n";
$send_message .= "<body>";
$send_message .= "<div class='container-fluid'><div class='col-sm-8 offset-sm-4'>";
$send_message .= "<br><p>$fullname, </p>";
$send_message .= "<p>Today the following messages have been quarantined by the IGB Spam Filter.\n";
$send_message .= "If you wish to retrieve any of these messages, they are located in your <strong>Junk</strong> folder\n";
$send_message .= "You can access the Junk folder by accessing the IGB webmail at <a href=\"http://mail.igb.illinois.edu\">http://mail.igb.illinois.edu</a>.\n";
$send_message .= "or using IMAP to connect to the server.</p>\n";
$send_message .= "<p>Spam messages over <strong>$delta</strong> days old are automatically deleted.</div></p>";
$send_message .= "<div class='col-sm-8 offset-sm-4'><p><table class='table table-striped table-bordered table-sm'>\n";
$send_message .= "<tr><th>From</th><th>Date</th><th>Subject</th></tr>\n";
foreach my $key (keys %todaysspam){
$send_message .= "<tr><td>$todaysspam{$key}{'from'}</td><td>$todaysspam{$key}{'date'}</td><td>$todaysspam{$key}{'subject'}</td></tr>\n";
}
$send_message .= "</table></div>\n";
$send_message .= "<div class='col-sm-8 offset-sm-4'>\n";
$send_message .= "<p>IGB Computer Network Resource Group\n";
$send_message .= "<br><a href='mailto:$adminemail'>$adminemail</a>\n";
$send_message .= "</div></div></body></html>\n";
if (keys %todaysspam) {
if (!$dryrun) {
send_mail($to,$send_message,$fromemail,$send_subject,$fromname,$mailhost);
my $num_spam = scalar keys %todaysspam;
if ($verbose) {
print "$uid email sent. $num_spam spam messages\n";
}
}
}
else {
if ($verbose) {
print "$uid has no spam. Email not sent\n";
}
}
}
}