3
# $Id: dspam_notify,v 1.05 2010/03/18 03:18:59 sbajic Exp $
7
# Enter the location of you dspam.conf, dspam binary and path to dspam_admin/dspam_stats.
8
$DSPAMCONF = '@sysconfdir@/dspam.conf';
9
$DSPAM_BINARY = '@bindir@/@dspam_transformed@';
12
# Who will the notifications be sent from?
13
$FROM = 'dspam@domain.tld';
15
# What will the notification subject be?
16
$SUBJECT = 'Daily Spam Quarantine Summary';
18
# What text to display in the body?
19
$BODY = qq!<p>This report has been sent to you from the Anti-Spam service hosted at ISP.com. Below is a list of items in your quarantine area. You can view or release a message by clicking on the links (right). If you no longer wish to receive these reports then you may change the option on the 'Preferences' page.</p>!;
22
$DSPAM_URL = 'https://dspam.domain.tld';
24
# Maximum of entries to show in mail
27
# Address of your SMTP server? localhost should be fine.
28
$SERVER = 'localhost';
30
# Port of your SMTP server? 25 should be fine
33
# Enable User Preference Checking (Very CPU Intensive!!!) Not Recommended for more than 500 email accounts.
36
######################################
37
# No need to config below this point.#
38
######################################
41
#Build the Quarantine URL
42
$QUARANTINE_URL = $DSPAM_URL . '/dspam.cgi?template=quarantine';
44
# Autodetect scale and preference extension support
47
$PREFERENCES_EXTENSION = 0;
49
my $x = `$DSPAM_BINARY --version`;
50
$PREFERENCES_EXTENSION = 1 if ($x =~ /--enable-preferences-extension/);
51
$LARGE_SCALE = 1 if ($x =~ /--enable-large-scale/);
52
$DOMAIN_SCALE = 1 if ($x =~ /--enable-domain-scale/) ;
56
my ($SEC,$MIN,$HOUR,$MDAY,$MON,$YEAR,$WDAY,$YDAY,$ISDST) = localtime(time);
58
# Array containing Days of the week abreviations
59
@WEEKDAYS = ('Sun','Mon','Tue','Wed','Thur','Fri','Sat');
61
# Array containing Month abreviations
62
@MONTHS = ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
67
$DAY_ABR = $WEEKDAYS[$D];
68
$MONTH_ABR = $MONTHS[$M];
72
$TODAY = $DAY_ABR . " " . $MONTH_ABR . " " . $DAY_NUM;
74
# Get the location of DSPAM home and if AllowOverride is set for dailyQuarantineSummary
78
open(DCONF, $DSPAMCONF) || die("Could not open " . $DSPAMCONF . " file!");
81
my($directive, $value) = split(/\s+/);
82
$DSPAMHOME = $value if ($directive eq "Home");
83
$ALLOW_OVERRIDE = "on" if ($directive eq "AllowOverride" && $value eq "dailyQuarantineSummary");
84
if ($directive eq "Preference") {
85
if ($value =~ /^\s*[\"\']?dailyQuarantineSummary[\t ]*=[\t ]*on[\"\']?/) {
88
$DEFAULT_PREF = "off";
91
last if($DSPAMHOME ne "" && $ALLOW_OVERRIDE ne "" && $DEFAULT_PREF ne "");
94
$ALLOW_OVERRIDE = "off" if($ALLOW_OVERRIDE eq "");
95
$DEFAULT_PREF = "off" if($DEFAULT_PREF eq "");
96
if (! -d $DSPAMHOME) {
97
die("Could not determine DSPAM home!");
100
# Create list of users having TP bigger then zero
101
open(IN, "$BINDIR/dspam_stats|");
105
my($username, $tp, $tn, $fp, $fn, $sc, $nc) = (split(/\s+/))[0,2,4,6,8,10,12];
109
($tp, $tn, $fp, $fn, $sc, $nc) = (split(/\s+/))[2,4,6,8,10,12];
111
push(@RECIPIENT_LIST, $username) if $tp != 0;
115
# Get default user preference for dailyQuarantineSummary
116
if ($PREF_CHECK == 1 && $PREFERENCES_EXTENSION == 1) {
117
open(PIPE, "$BINDIR/dspam_admin agg pref 'default'|");
120
my($directive, $value) = split(/\=/);
121
if ($directive eq "dailyQuarantineSummary") {
122
$DEFAULT_PREF = $value;
129
# Gather Recipient Quarantine Info
130
foreach $RECIPIENT (@RECIPIENT_LIST) {
132
# Get User Preference from dspam_admin
133
if ($ALLOW_OVERRIDE eq "on") { # Check for Allow Overides
134
open(PIPE, "$BINDIR/dspam_admin li pref " . quotemeta($RECIPIENT) . "|");
137
my($directive, $value) = split(/\=/);
138
if ($directive eq "dailyQuarantineSummary") {
139
if ($value ne "on" && $value ne "off") {
140
$USER_PREF = $DEFAULT_PREF; # User Preference in valid, use default preference
149
$USER_PREF = $DEFAULT_PREF; # Overrides off, use default preference
152
# Build path to Quarantine .mbox
153
if ($DOMAIN_SCALE == 1) { # Format Quarantine path for Domain Scale
154
my($u, $D) = (split(/@/, $RECIPIENT));
155
$MBOX = $DSPAMHOME . "/data/" . $D . "/" . $u . "/" . $u . ".mbox";
156
} elsif ($LARGE_SCALE == 1) { # Format Quarantine path for Large Scale
157
$u = substr($RECIPIENT, 0, 1);
158
$s = substr($RECIPIENT, 1, 1);
159
$MBOX = $DSPAMHOME . "/data/" . $u . "/" . $s . "/" . $RECIPIENT . "/" . $RECIPIENT . ".mbox";
160
} else { # Format Quarantine path for Normal Scale
161
$MBOX = $DSPAMHOME . "/data/" . $RECIPIENT . "/" . $RECIPIENT . ".mbox";
164
# Get total amount of quarantine messages and their signature
170
if ($USER_PREF ne "off" && -e $MBOX) { # Check if .mbox file exists and user pref
171
open(MBOX, "<$MBOX") || die("Could not open " . $MBOX . " file!");
174
next if ($_ !~ /^From QUARANTINE/);
175
$TOTAL++; # Count Total messages in Quarantine
176
$NEW++ if ($_ =~ /^From QUARANTINE $TODAY/); # Count New messages in Quarantine
177
$QSUBJECT = '<None Specified>';
181
my($key, $val) = split(/\: ?/, $_, 2);
182
if ($key =~ /^Subject$/i) {
185
$QSUBJECT = $val if ($val ne "");
187
if ($key =~ /^X\-DSPAM\-Signature$/) {
189
$QSUBJECT =~ s/</</g;
190
$QSUBJECT =~ s/>/>/g;
191
$QSUBJECT = substr($QSUBJECT, 0, 50) . "..." if (length($QSUBJECT)>50);
192
push(@Q_SUBJECT, $QSUBJECT);
199
push(@Q_SUBJECT_ITEMS, join("\n", @Q_SUBJECT));
200
push(@Q_SIG_ITEMS, join("\n", @Q_SIG));
201
push(@Q_NEW_ITEMS, $NEW); # Send Count to Array for later use
202
push(@Q_TOTAL_ITEMS, $TOTAL); # Send Count to Array for later use
209
@Q_ROW_COLOR=('CCCCCC','FFFFFF');
210
$SMTP = Net::SMTP->new( # Establish SMTP Connection
211
Host => $SERVER . ":" . $PORT,
212
Timeout => 30) || die ("Could not connect to SMTP server " . $SERVER . ":" . $PORT . "; $!");
213
for ($I = 0; $I <= $#RECIPIENT_LIST; $I++) { # Loop through Recipients List and send the message
214
if (@Q_TOTAL_ITEMS[$I] != 0) { # Don't send reminders to users with empty quarantines
216
$SMTP->to($RECIPIENT_LIST[$I]);
218
$SMTP->datasend("To: $RECIPIENT_LIST[$I]\n");
219
$SMTP->datasend("Subject: $SUBJECT\n");
220
$SMTP->datasend("Mime-Version: 1.0\n");
221
$SMTP->datasend("Content-Type: text/html; charset=UTF-8\n");
222
$SMTP->datasend("<HTML>\n");
223
$SMTP->datasend("<HEAD>\n");
224
$SMTP->datasend("<TITLE>DSPAM Quarantine Summary for $RECIPIENT_LIST[$I]</TITLE>\n");
225
$SMTP->datasend("<META HTTP-EQUIV='Content-Type' CONTENT='text/html; charset=utf-8'>\n");
226
$SMTP->datasend("</HEAD>\n");
227
$SMTP->datasend("<BODY>\n");
228
$SMTP->datasend($BODY ."\n");
229
$SMTP->datasend("<TABLE>\n");
230
$SMTP->datasend("<TR><TD>Quarantine Summary for</TD><TD>$RECIPIENT_LIST[$I]</TD></TR>\n");
231
$SMTP->datasend("<TR><TD>Date</TD><TD>$TODAY, $YEAR</TD></TR>\n");
232
$SMTP->datasend("<TR><TD COLSPAN='2'> </TD></TR>\n");
233
$SMTP->datasend("<TR><TD>New Messages</TD><TD>@Q_NEW_ITEMS[$I]</TD></TR>\n");
234
$SMTP->datasend("<TR><TD>Total Messages</TD><TD>@Q_TOTAL_ITEMS[$I]</TD></TR>\n");
235
$SMTP->datasend("</TABLE>\n");
236
$SMTP->datasend("<BR>\n");
237
$SMTP->datasend("<TABLE>\n");
238
@Q_SUBJECT = split(/\n/,@Q_SUBJECT_ITEMS[$I]);
239
@Q_SIG = split(/\n/,@Q_SIG_ITEMS[$I]);
240
for ($J = 0; $J <= $#Q_SIG; $J++) {
241
my $QCOMMAND = $QUARANTINE_URL . "&user=" . $RECIPIENT_LIST[$I] . "&signatureID=" . @Q_SIG[$J];
243
$QROW_COLOR = 1 if(($J % 2) != 0);
244
$SMTP->datasend("<TR>");
245
if ($J >= $MAX_ITEMS) {
246
$SMTP->datasend("<TD COLSPAM='3' STYLE='background-color:" . @Q_ROW_COLOR[$QROW_COLOR] . ";'>To display more then " . $MAX_ITEMS . " messages, please visit the DSPAM Control Center.</TD>\n");
247
$SMTP->datasend("</TR>\n");
250
$SMTP->datasend("<TD STYLE='background-color:" . @Q_ROW_COLOR[$QROW_COLOR] . ";'>" . @Q_SUBJECT[$J] . "</TD>");
251
$SMTP->datasend("<TD STYLE='background-color:" . @Q_ROW_COLOR[$QROW_COLOR] . ";'><A HREF='" . $QCOMMAND . "&command=viewMessage' TARGET='_blank'>View</A></TD>");
252
$SMTP->datasend("<TD STYLE='background-color:" . @Q_ROW_COLOR[$QROW_COLOR] . ";'><A HREF='" . $QCOMMAND . "&command=processFalsePositive' TARGET='_blank'>Release</A></TD>");
253
$SMTP->datasend("</TR>\n");
255
$SMTP->datasend("</TABLE>\n");
256
$SMTP->datasend("<BR>\n");
257
$SMTP->datasend("Please remember to check <A HREF='$QUARANTINE_URL' TARGET='_blank'>Your Quarantine</A> regularly.\n");
258
$SMTP->datasend("</BODY>\n");
259
$SMTP->datasend("</HTML>\n");
263
$SMTP->quit; # Close SMTP Connection