Cleaned up and linked rules
[clearscm.git] / maps / bin / detail.cgi
1 #!/usr/bin/perl
2 #################################################################################
3 # File:         $RCSfile: detail.cgi,v $
4 # Revision:     $Revision: 1.1 $
5 # Description:  Displays list of email addresses based on report type.
6 # Author:       Andrew@DeFaria.com
7 # Created:      Fri Nov 29 14:17:21  2002
8 # Modified:     $Date: 2013/06/12 14:05:47 $
9 # Language:     perl
10 #
11 # (c) Copyright 2000-2006, Andrew@DeFaria.com, all rights reserved.
12 #
13 ################################################################################use strict;
14 use warnings;
15
16 use MIME::Words qw(:all);
17 use FindBin;
18 $0 = $FindBin::Script;
19
20 use lib $FindBin::Bin;
21
22 use MAPS;
23 use MAPSLog;
24 use MAPSUtil;
25 use MAPSWeb;
26 use CGI qw (:standard *table start_td end_td start_Tr end_Tr start_div end_div);
27 use CGI::Carp 'fatalsToBrowser';
28
29 my $type   = param ('type');
30 my $next   = param ('next');
31 my $lines  = param ('lines');
32 my $date   = param ('date');
33
34 $date ||= '';
35
36 my $userid;
37 my $current;
38 my $last;
39 my $prev;
40 my $total;
41 my $table_name = 'detail';
42
43 my %types = (
44   'blacklist'   => [
45     'Blacklist report',
46     'The following blacklisted users attempted to email you'
47   ],
48   'whitelist'   => [
49     'Delivered report',
50     'Delivered email from the following users'
51   ],
52   'nulllist'    => [
53     'Discarded report',
54     'Discarded messages from the following users'
55   ],
56   'error'       => [
57     'Error report',
58     'Errors detected'
59   ],
60   'mailloop'    => [
61     'MailLoop report',
62     'Automatically detected mail loops from the following users'
63   ],
64   'registered'  => [
65     'Registered report',
66     'The following users have recently registered'
67   ],
68   'returned'    => [
69     'Returned report',
70     'Sent Register reply to the following users'
71   ]
72 );
73
74 sub MakeButtons {
75   my $type = shift;
76
77   my $prev_button = $prev >= 0 ?
78     a ({-href      => "detail.cgi?type=$type;date=$date;next=$prev",
79         -accesskey => 'p',
80     }, '<img src=/maps/images/previous.gif border=0 alt=Previous align=middle>') : '';
81   my $next_button = ($next + $lines) < $total ?
82     a {-href      => "detail.cgi?type=$type;date=$date;next=" . ($next + $lines),
83        -accesskey => 'n',
84     }, '<img src=/maps/images/next.gif border=0 alt=Next align=middle>' : '';
85
86   my $buttons = $prev_button;
87
88   if ($type eq 'whitelist') {
89     $buttons = $buttons .
90       submit ({-name    => 'action',
91                -value   => 'Blacklist Marked',
92                -onClick => 'return CheckAtLeast1Checked (document.detail);'}) .
93       submit ({-name    => 'action',
94                -value   => 'Nulllist Marked',
95                -onClick => 'return CheckAtLeast1Checked (document.detail);'}) .
96       submit ({-name    => 'action',
97                -value   => 'Reset Marks',
98                -onClick => 'return ClearAll (document.detail);'});
99   } elsif ($type eq 'blacklist') {
100     $buttons = $buttons .
101       submit ({-name    => 'action',
102                -value   => 'Whitelist Marked',
103                -onClick => 'return CheckAtLeast1Checked (document.detail);'}) .
104       submit ({-name    => 'action',
105                -value   => 'Nulllist Marked',
106                -onClick => 'return CheckAtLeast1Checked (document.detail);'}) .
107       submit ({-name    => 'action',
108                -value   => 'Reset Marks',
109                -onClick => 'return ClearAll (document.detail);'});
110   } elsif ($type eq 'nulllist') {
111     $buttons = $buttons .
112       submit ({-name    => 'action',
113                -value   => 'Whitelist Marked',
114                -onClick => 'return CheckAtLeast1Checked (document.detail);'}) .
115       submit ({-name    => 'action',
116                -value   => 'Blacklist Marked',
117                -onClick => 'return CheckAtLeast1Checked (document.detail);'}) .
118       submit ({-name    => 'action',
119                -value   => 'Reset Marks',
120                -onClick => 'return ClearAll (document.detail);'});
121   } else {
122     $buttons = $buttons .
123       submit ({-name    => 'action',
124                -value   => 'Whitelist Marked',
125                -onClick => 'return CheckAtLeast1Checked (document.detail);'}) .
126       submit ({-name    => 'action',
127                -value   => 'Blacklist Marked',
128                -onClick => 'return CheckAtLeast1Checked (document.detail);'}) .
129       submit ({-name    => 'action',
130                -value   => 'Nulllist Marked',
131                -onClick => 'return CheckAtLeast1Checked (document.detail);'}) .
132       submit ({-name    => 'action',
133                -value   => 'Reset Marks',
134                -onClick => 'return ClearAll (document.detail);'});
135   } # if
136
137   return $buttons . $next_button;
138 } # MakeButtons
139
140 sub PrintTable {
141   my ($type) = @_;
142
143   my $current = $next + 1;
144
145   print div {-align => 'center'}, b (
146     '(' . $current . '-' . $last . ' of ' . $total . ')');
147   print start_form {
148     -method => 'post',
149     -action => 'processaction.cgi',
150     -name   => 'detail'
151   };
152   print start_table ({-align        => 'center',
153                       -id           => $table_name,
154                       -border       => 0,
155                       -cellspacing  => 0,
156                       -cellpadding  => 0,
157                       -width        => '100%'}) . "\n";
158
159   my $buttons = MakeButtons $type;
160
161   print start_div {-class => 'toolbar'};
162   print
163     Tr [
164       td {-class  => 'tablebordertopleft',
165           -valign => 'middle'},
166       td {-class  => 'tablebordertopright',
167           -valign => 'middle',
168           -align  => 'center'}, $buttons,
169     ];
170   print end_div;
171
172   foreach my $sender (ReturnSenders $userid, $type, $next, $lines, $date) {
173     my @msgs = ReturnMessages $userid, $sender;
174     my @msgs2 = @msgs;
175
176     my ($onlist, $rule);
177     $rule = 'none';
178
179     ($onlist, $rule) = OnNulllist $sender;
180
181     unless ($onlist) {
182       ($onlist, $rule) = OnBlacklist $sender;
183
184       unless ($onlist) {
185         ($onlist, $rule) = OnWhitelist $sender;
186       } # unless
187     } # unless
188
189     if ($rule) {
190       $rule =~ s/Matching rule: \(//;
191       $rule =~ s/\)//;
192
193       if ($rule =~ /(\w+):(\d+)/) {
194         my $list     = $1;
195         my $sequence = $2 - 1;
196         my $link     = "<a href=\"/maps/php/list.php?type=$list&next=$sequence\">$list:$2</a>";
197
198         $rule =~ s/\w+:\d+/$link/;
199       } # if
200     } # if
201
202     $next++;
203     print
204       start_Tr {-valign => 'middle'};
205     print
206       td {-class => 'tableborder'}, small ($next,
207         checkbox {-name  => "action$next",
208                   -label => ''}),
209           hidden ({-name    => "email$next",
210                    -default => $sender});
211     print
212       start_td {-align => 'left'};
213     print
214       start_table {-class       => 'tablerightdata',
215                    -cellpadding => 2,
216                    -callspacing => 0,
217                    -border      => 0,
218                    -width       => '100%',
219                    -bgcolor     => '#d4d0c8'};
220     print
221       td {-class   => 'tablelabel',
222           -valign  => 'middle',
223           -width   => '40'}, 'Sender:',
224       td {-class   => 'sender',
225           -valign  => 'middle',
226           -width   => '40%'},
227         a {-href   => "mailto:$sender?subject=$msgs2[0][0]"}, $sender,
228       td {
229           -valign  => 'middle'},
230           $rule;
231     print
232       end_table;
233
234     my $messages = 1;
235
236     foreach (@msgs) {
237       my $msg_date = pop @{$_};
238       my $subject  = pop @{$_};
239
240       if ($date eq substr ($msg_date, 0, 10)) {
241         $msg_date = b font {-color => 'green'}, SQLDatetime2UnixDatetime $msg_date;
242       } else {
243         $msg_date = SQLDatetime2UnixDatetime $msg_date;
244       } # if
245
246       $subject = $subject eq '' ? '&lt;Unspecified&gt;' : $subject;
247       $subject = decode_mimewords ($subject);
248       $subject =~ s/\>/&gt;/g;
249       $subject =~ s/\</&lt;/g;
250
251       print
252         start_table {-class       => 'tablerightdata',
253                      -cellpadding => 2,
254                      -cellspacing => 2,
255                      -border      => 0,
256                      -width       => '100%'};
257       my $msg_nbr = $messages;
258       print
259         Tr [
260           td {-class   => 'msgnbr',
261               -valign  => 'middle',
262               -rowspan => 2,
263               -width   => '2%'}, $messages++,
264           td {-class   => 'tablelabel',
265               -valign  => 'middle',
266               -width   => '45'}, 'Subject:',
267           td {-class   => 'subject',
268               -valign  => 'middle',
269               -bgcolor => '#ffffff'},
270            a {-href    => "display.cgi?sender=$sender;msg_nbr=$msg_nbr"}, $subject,
271           td {-class   => 'date',
272               -width   => '150',
273               -valign  => 'middle'}, $msg_date
274         ];
275       print end_table;
276     } # foreach
277     print end_td;
278     print end_Tr;
279   } # foreach
280
281   print start_div {-class => 'toolbar'};
282   print
283     Tr [
284       td {-class  => 'tableborderbottomleft',
285           -valign => 'middle'},
286       td {-class  => 'tableborderbottomright',
287           -valign => 'middle'},
288       $buttons
289     ];
290   print end_div;
291   print end_table;
292   print end_form;
293 } # PrintTable
294
295 # Main
296 my @scripts = ('ListActions.js');
297
298 my $heading_date =$date ne '' ? ' on ' . FormatDate ($date) : '';
299
300 $userid = Heading (
301   'getcookie',
302   '',
303   (ucfirst ($type) . ' Report'),
304   $types {$type} [0],
305   $types {$type} [1] . $heading_date,
306   $table_name,
307   @scripts
308 );
309
310 $userid ||= $ENV{USER};
311
312 SetContext $userid;
313 NavigationBar $userid;
314
315 unless ($lines) {
316   my %options = GetUserOptions $userid;
317   $lines = $options{'Page'};
318 } # unless
319
320 if ($date eq '') {
321   $condition .= "userid = '$userid' and type = '$type'";
322 } else {
323   my $sod = $date . ' 00:00:00';
324   my $eod = $date . ' 23:59:59';
325
326   $condition .= "userid = '$userid' and type = '$type' "
327               . "and timestamp > '$sod' and timestamp < '$eod' ";
328 } # if
329
330 $total = MAPSDB::count_distinct ('log', 'sender', $condition);
331
332 $next ||= 0;
333
334 $last = $next + $lines < $total ? $next + $lines : $total;
335
336 if (($next - $lines) > 0) {
337   $prev = $next - $lines;
338 } else {
339   $prev = $next eq 0 ? -1 : 0;
340 } # if
341
342 PrintTable $type;
343
344 Footing $table_name;
345
346 exit;