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