~ubuntu-branches/ubuntu/precise/dspam/precise

« back to all changes in this revision

Viewing changes to .pc/009_dspam-notify.diff/src/tools/dspam_notify.in

  • Committer: Bazaar Package Importer
  • Author(s): Julien Valroff, Julien Valroff, Thomas Preud'homme
  • Date: 2011-05-08 13:43:52 UTC
  • mfrom: (1.2.1 upstream) (8.2.2 experimental)
  • mto: This revision was merged to the branch mainline in revision 23.
  • Revision ID: james.westby@ubuntu.com-20110508134352-dxjx9m6tx1cbzlhq
Tags: 3.9.1~rc1+git20110419.29261fb+dfsg-1
[ Julien Valroff ]
* New git snapshot
* Install all dspam_* tools setgid so that they can be used by standard
  users
* Add symbols file for libdspam7
* Upload to unstable

[ Thomas Preud'homme ]
* Fix permissions on dspam_stats and missing opt-{in,out} directories
  (Closes: #394443)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/perl
 
2
#
 
3
# $Id: dspam_notify,v 1.05 2010/03/18 03:18:59 sbajic Exp $
 
4
 
 
5
use Net::SMTP;
 
6
 
 
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@';
 
10
$BINDIR = '@bindir@';
 
11
 
 
12
# Who will the notifications be sent from?
 
13
$FROM = 'dspam@domain.tld';
 
14
  
 
15
# What will the notification subject be?
 
16
$SUBJECT = 'Daily Spam Quarantine Summary';
 
17
 
 
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>!;
 
20
 
 
21
# Quarantine URL
 
22
$DSPAM_URL = 'https://dspam.domain.tld';
 
23
 
 
24
# Maximum of entries to show in mail
 
25
$MAX_ITEMS = 200;
 
26
 
 
27
# Address of your SMTP server? localhost should be fine.
 
28
$SERVER = 'localhost';
 
29
 
 
30
# Port of your SMTP server? 25 should be fine
 
31
$PORT = '25';
 
32
 
 
33
# Enable User Preference Checking (Very CPU Intensive!!!) Not Recommended for more than 500 email accounts.
 
34
$PREF_CHECK = 0;
 
35
 
 
36
######################################
 
37
# No need to config below this point.#
 
38
######################################
 
39
 
 
40
 
 
41
#Build the Quarantine URL
 
42
$QUARANTINE_URL = $DSPAM_URL . '/dspam.cgi?template=quarantine';
 
43
 
 
44
# Autodetect scale and preference extension support
 
45
$LARGE_SCALE = 0;
 
46
$DOMAIN_SCALE = 0;
 
47
$PREFERENCES_EXTENSION = 0;
 
48
do {
 
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/) ;
 
53
};
 
54
 
 
55
# Date Formatting
 
56
my ($SEC,$MIN,$HOUR,$MDAY,$MON,$YEAR,$WDAY,$YDAY,$ISDST) = localtime(time);
 
57
  
 
58
# Array containing Days of the week abreviations
 
59
@WEEKDAYS = ('Sun','Mon','Tue','Wed','Thur','Fri','Sat');
 
60
    
 
61
# Array containing Month abreviations
 
62
@MONTHS = ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
 
63
    
 
64
$D = (localtime)[6];
 
65
$M = (localtime)[4];
 
66
 
 
67
$DAY_ABR = $WEEKDAYS[$D];
 
68
$MONTH_ABR = $MONTHS[$M];
 
69
$DAY_NUM = $MDAY; 
 
70
$YEAR += 1900;
 
71
 
 
72
$TODAY = $DAY_ABR . " " . $MONTH_ABR . " " . $DAY_NUM;
 
73
    
 
74
# Get the location of DSPAM home and if AllowOverride is set for dailyQuarantineSummary
 
75
$DSPAMHOME = "";
 
76
$ALLOW_OVERRIDE = "";
 
77
$DEFAULT_PREF = "";
 
78
open(DCONF, $DSPAMCONF) || die("Could not open " . $DSPAMCONF . " file!");
 
79
while(<DCONF>) {
 
80
  chomp;
 
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[\"\']?/) {
 
86
      $DEFAULT_PREF = "on";
 
87
    } else {
 
88
      $DEFAULT_PREF = "off";
 
89
    }
 
90
  }
 
91
  last if($DSPAMHOME ne "" && $ALLOW_OVERRIDE ne "" && $DEFAULT_PREF ne "");
 
92
}
 
93
close(DCONF);
 
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!");
 
98
}
 
99
 
 
100
# Create list of users having TP bigger then zero
 
101
open(IN, "$BINDIR/dspam_stats|");
 
102
while(<IN>) {
 
103
  chomp;
 
104
  s/:/ /g;
 
105
  my($username, $tp, $tn, $fp, $fn, $sc, $nc) = (split(/\s+/))[0,2,4,6,8,10,12];
 
106
  if ($tp eq "") {
 
107
    $_ = <IN>;
 
108
    s/:/ /g;
 
109
    ($tp, $tn, $fp, $fn, $sc, $nc) = (split(/\s+/))[2,4,6,8,10,12];
 
110
  }
 
111
  push(@RECIPIENT_LIST, $username) if $tp != 0;
 
112
}
 
113
close(IN);
 
114
 
 
115
# Get default user preference for dailyQuarantineSummary
 
116
if ($PREF_CHECK == 1 && $PREFERENCES_EXTENSION == 1) {
 
117
  open(PIPE, "$BINDIR/dspam_admin agg pref 'default'|");
 
118
  while(<PIPE>) {
 
119
    chomp;
 
120
    my($directive, $value) = split(/\=/);
 
121
    if ($directive eq "dailyQuarantineSummary") {
 
122
      $DEFAULT_PREF = $value;
 
123
      last;
 
124
    }
 
125
  }
 
126
  close(PIPE);
 
127
}
 
128
 
 
129
# Gather Recipient Quarantine Info
 
130
foreach $RECIPIENT (@RECIPIENT_LIST) {
 
131
 
 
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) . "|");
 
135
    while(<PIPE>) {
 
136
      chomp;
 
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
 
141
        } else {
 
142
          $USER_PREF = $value;
 
143
        }
 
144
        last;
 
145
      }
 
146
    }
 
147
    close(PIPE);
 
148
  } else {
 
149
    $USER_PREF = $DEFAULT_PREF;                                                 # Overrides off, use default preference
 
150
  }
 
151
 
 
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";
 
162
  }
 
163
 
 
164
  # Get total amount of quarantine messages and their signature
 
165
  $NEW = 0;
 
166
  $TOTAL = 0;
 
167
  $SIG = '';
 
168
  @Q_SIG = ();
 
169
  @Q_SUBJECT = ();
 
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!");
 
172
    while(<MBOX>) {
 
173
      s/\r?\n//;
 
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>';
 
178
      while(<MBOX>) {
 
179
        s/\r?\n//;
 
180
        last if ($_ eq "");
 
181
        my($key, $val) = split(/\: ?/, $_, 2);
 
182
        if ($key =~ /^Subject$/i) {
 
183
          $val =~ s/^\s+//;
 
184
          $val =~ s/\s+$//;
 
185
          $QSUBJECT = $val if ($val ne "");
 
186
        }
 
187
        if ($key =~ /^X\-DSPAM\-Signature$/) {
 
188
          push(@Q_SIG, $val);
 
189
          $QSUBJECT =~ s/</&lt;/g;
 
190
          $QSUBJECT =~ s/>/&gt;/g;
 
191
          $QSUBJECT = substr($QSUBJECT, 0, 50) . "..." if (length($QSUBJECT)>50);
 
192
          push(@Q_SUBJECT, $QSUBJECT);
 
193
          last;
 
194
        }
 
195
      }
 
196
    }
 
197
    close(MBOX);
 
198
  }
 
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
 
203
  @Q_SUBJECT = ();
 
204
  @Q_SIG = ();
 
205
}
 
206
 
 
207
 
 
208
# Send some emails
 
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
 
215
    $SMTP->mail($FROM);
 
216
    $SMTP->to($RECIPIENT_LIST[$I]);
 
217
    $SMTP->data();
 
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'>&nbsp;</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];
 
242
      my $QROW_COLOR = 0;
 
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");
 
248
        last;
 
249
      }
 
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");
 
254
    }
 
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");
 
260
    $SMTP->dataend();
 
261
  }
 
262
}
 
263
$SMTP->quit;                                                                    # Close SMTP Connection