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