53fc47e2e24ace7d0336f7b66ded9ce27048c75e
[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
20 use CGI qw(:standard *table start_td end_td start_Tr end_Tr start_div end_div);
21 use CGI::Carp 'fatalsToBrowser';
22
23 use FindBin;
24
25 local $0 = $FindBin::Script;
26
27 use lib "$FindBin::Bin/../lib";
28 use lib "$FindBin::Bin/../../lib";
29
30 use MAPS;
31 use MAPSLog;
32 use MAPSWeb;
33 use DateUtils;
34
35 my $type   = param 'type';
36 my $next   = param 'next';
37 my $lines  = param 'lines';
38 my $date   = param 'date';
39
40 $date ||= '';
41
42 my ($userid, $current, $last, $prev, $total);
43
44 my $table_name = 'detail';
45
46 my %types = (
47   'blacklist'   => [
48     'Blacklist report',
49     'The following blacklisted users attempted to email you'
50   ],
51   'whitelist'   => [
52     'Delivered report',
53     'Delivered email from the following users'
54   ],
55   'nulllist'    => [
56     'Discarded report',
57     'Discarded messages from the following users'
58   ],
59   'error'       => [
60     'Error report',
61     'Errors detected'
62   ],
63   'mailloop'    => [
64     'MailLoop report',
65     'Automatically detected mail loops from the following users'
66   ],
67   'registered'  => [
68     'Registered report',
69     'The following users have recently registered'
70   ],
71   'returned'    => [
72     'Returned report',
73     'Sent Register reply to the following users'
74   ]
75 );
76
77 sub MakeButtons($) {
78   my ($type) = @_;
79
80   my $prev_button = $prev >= 0 ?
81     a ({-href      => "detail.cgi?type=$type;date=$date;next=$prev",
82         -accesskey => 'p',
83     }, '<img src=/maps/images/previous.gif border=0 alt=Previous align=middle>') : '';
84   my $next_button = ($next + $lines) < $total ?
85     a {-href      => "detail.cgi?type=$type;date=$date;next=" . ($next + $lines),
86        -accesskey => 'n',
87     }, '<img src=/maps/images/next.gif border=0 alt=Next align=middle>' : '';
88
89   my $buttons = $prev_button;
90
91   if ($type eq 'whitelist') {
92     $buttons = $buttons .
93       submit ({-name    => 'action',
94                -value   => 'Blacklist',
95                -onClick => 'return CheckAtLeast1Checked (document.detail);'}) . '&nbsp;' .
96       submit ({-name    => 'action',
97                -value   => 'Nulllist',
98                -onClick => 'return CheckAtLeast1Checked (document.detail);'}) . '&nbsp;' .
99       submit ({-name    => 'action',
100                -value   => 'Reset',
101                -onClick => 'return ClearAll (document.detail);'});
102   } elsif ($type eq 'blacklist') {
103     $buttons = $buttons .
104       submit ({-name    => 'action',
105                -value   => 'Whitelist',
106                -onClick => 'return CheckAtLeast1Checked (document.detail);'}) . '&nbsp;' .
107       submit ({-name    => 'action',
108                -value   => 'Nulllist',
109                -onClick => 'return CheckAtLeast1Checked (document.detail);'}) . '&nbsp;' .
110       submit ({-name    => 'action',
111                -value   => 'Reset',
112                -onClick => 'return ClearAll (document.detail);'});
113   } elsif ($type eq 'nulllist') {
114     $buttons = $buttons .
115       submit ({-name    => 'action',
116                -value   => 'Whitelist',
117                -onClick => 'return CheckAtLeast1Checked (document.detail);'}) . '&nbsp;' .
118       submit ({-name    => 'action',
119                -value   => 'Blacklist',
120                -onClick => 'return CheckAtLeast1Checked (document.detail);'}) . '&nbsp;' .
121       submit ({-name    => 'action',
122                -value   => 'Reset',
123                -onClick => 'return ClearAll (document.detail);'});
124   } else {
125     $buttons = $buttons .
126       submit ({-name    => 'action',
127                -value   => 'Whitelist',
128                -onClick => 'return CheckAtLeast1Checked (document.detail);'}) . '&nbsp;' .
129       submit ({-name    => 'action',
130                -value   => 'Blacklist',
131                -onClick => 'return CheckAtLeast1Checked (document.detail);'}) . '&nbsp;' .
132       submit ({-name    => 'action',
133                -value   => 'Nulllist',
134                -onClick => 'return CheckAtLeast1Checked (document.detail);'}) . '&nbsp;' .
135       submit ({-name    => 'action',
136                -value   => 'Reset',
137                -onClick => 'return ClearAll (document.detail);'});
138   } # if
139
140   print div {
141     -align => 'center',
142     -class => 'toolbar',
143   }, $buttons . $next_button;
144
145   return;
146 } # MakeButtons
147
148 sub Body($) {
149   my ($type) = @_;
150
151   my $current = $next + 1;
152
153   print div {-align => 'center'}, b (
154     '(' . $current . '-' . $last . ' of ' . $total . ')');
155   print start_form {
156     -method => 'post',
157     -action => 'processaction.cgi',
158     -name   => 'detail'
159   };
160
161   MakeButtons $type;
162
163   print start_table({-align        => 'center',
164                      -id           => $table_name,
165                      -border       => 0,
166                      -cellspacing  => 0,
167                      -cellpadding  => 0,
168                      -width        => '100%'}) . "\n";
169
170   print
171     Tr [
172       td {-class  => 'tablebordertopleft'},  '&nbsp;',
173       th {-class  => 'tableborder'},         'Sender',
174       th {-class  => 'tableborder'},         'List',
175       th {-class  => 'tableborder'},         'Hit Count',
176       th {-class  => 'tableborder'},         'Rule',
177       th {-class  => 'tablebordertopright'}, 'Comment',
178     ];
179
180   for my $sender (ReturnSenders(
181     userid   => $userid,
182     type     => $type,
183     start_at => $next,
184     lines    => $lines,
185     date     => $date
186   )) {
187     my $msgs = ReturnMessages(
188       userid => $userid,
189       sender => $sender,
190     );
191
192     # This is for the purposes of supplying a subject line if the mailto address
193     # is clicked on. It's kludgy because we are simply grabbing the subject line
194     # of the first email sent where there may be many emails from this sender.
195     # Still it is often the right subject (or a good enough one)
196     #
197     # A little tricky here because of transliteration. If I test for
198     # $msg->[0]{subject} when $msg->[0] is essentially empty I create the hash
199     # making it non empty. Therefore I need to first test if $msgs->[0] exists
200     # first.
201     my $heading = '';
202
203     if ($msgs->[0]) {
204       $heading = $msgs->[0]{subject} if $msgs->[0]{subject};
205     } # if
206
207     my ($onlist, $seq);
208
209     my $rule      = 'none';
210     my $hit_count = 0;
211
212     ($onlist, $rule, $seq, $hit_count) = OnWhitelist($sender, $userid, 0);
213
214     unless ($onlist) {
215       ($onlist, $rule, $seq, $hit_count) = OnBlacklist($sender, 0);
216
217       unless ($onlist) {
218         ($onlist, $rule, $seq, $hit_count) = OnNulllist($sender, 0);
219       } # unless
220     } # unless
221
222     my ($list, $sequence, $comment);
223
224     # Parse rule
225     if ($rule) {
226       if ($rule =~ /\((\w+):(\d+)\)\s+"(\S*)"/) {
227         $list     = $1;
228         $sequence = $2;
229         $rule     = $3;
230         $comment  = '';
231       } elsif ($rule =~ /\((\w+):(\d+)\)\s+"(\S*) - (.*)"/) {
232         $list     = $1;
233         $sequence = $2;
234         $rule     = $3;
235         $comment  = $4;
236       } # if
237
238       $rule =~ s/\\@/\@/;
239     } # if
240
241     $next++;
242
243     # Start Sender line
244     my $rowspan = @$msgs + 1;
245
246     print start_Tr {-valign => 'middle'};
247     print td {
248       -class => 'tableborder',
249       -rowspan => $rowspan,
250     }, small ($next,
251       checkbox {
252         -name  => "action$next",
253         -label => ''
254       }), hidden({
255         -name    => "email$next",
256         -default => $sender
257       });
258
259     # Get subject line
260     $heading = "?subject=$heading" if $heading;
261
262     print td {
263       -class => 'sender',
264     }, a {
265       -href  => "mailto:$sender$heading",
266     }, " $sender";
267
268     my $listlink = ($list and $sequence) ? "$list:$sequence" : '&nbsp;';
269
270     print td {
271       -class => 'tabledata',
272       -align => 'right',
273     }, a {
274       href  => "/maps/php/list.php?type=$list&next=" . ($sequence - 1),
275     }, $listlink,
276     td {
277       -class => 'tabledata',
278       -align => 'right',
279     }, "$hit_count&nbsp;",
280     td {
281       -class => 'tabledata',
282     }, $rule,
283     td {
284       -class => 'tablerightdata',
285     }, $comment;
286     print end_Tr;
287
288     for my $rec (@$msgs) {
289       if ($date eq substr ($rec->{timestamp}, 0, 10)) {
290         $rec->{date} = b font {-color => 'green'}, SQLDatetime2UnixDatetime $rec->{timestamp};
291       } else {
292         $rec->{date} = SQLDatetime2UnixDatetime $rec->{timestamp};
293       } # if
294
295       $rec->{subject} //= '&lt;Unspecified&gt;';
296       $rec->{subject} = decode_mimewords ($rec->{subject});
297       $rec->{subject} =~ s/\>/&gt;/g;
298       $rec->{subject} =~ s/\</&lt;/g;
299
300       print
301         Tr [
302           td {
303             -class   => 'subject',
304             -valign  => 'middle',
305             -bgcolor => '#ffffff',
306             -colspan => 4,
307           }, a {-href    => "display.cgi?sender=$sender;msg_date=$rec->{timestamp}"
308            }, '&nbsp;&nbsp;&nbsp;&nbsp;' . $rec->{subject},
309           td {-class   => 'tablerightdata',
310               -width   => '150',
311               -valign  => 'middle',
312               -align   => 'right'}, span {-class => 'date'}, $rec->{date},
313         ];
314     } # for
315   } # for
316
317   print
318     Tr [
319       td {-class  => 'tableborderbottomleft'},  '&nbsp;',
320       th {-class  => 'tableborder'},            '&nbsp;',
321       th {-class  => 'tableborder'},            '&nbsp;',
322       th {-class  => 'tableborder'},            '&nbsp;',
323       th {-class  => 'tableborder'},            '&nbsp;',
324       th {-class  => 'tableborderbottomright'}, '&nbsp;'
325     ];
326   print end_table;
327   print end_form;
328
329   MakeButtons $type;
330
331   return;
332 } # Body
333
334 # Main
335 my $condition;
336 my @scripts = ('ListActions.js');
337
338 my $heading_date =$date ne '' ? ' on ' . FormatDate ($date, 1) : '';
339
340 $userid = Heading(
341   'getcookie',
342   '',
343   (ucfirst ($type) . ' Report'),
344   $types{$type} [0],
345   $types{$type} [1] . $heading_date,
346   $table_name,
347   @scripts
348 );
349
350 $userid ||= $ENV{USER};
351
352 SetContext($userid);
353 NavigationBar($userid);
354
355 unless ($lines) {
356   my %options = GetUserOptions($userid);
357   $lines = $options{'Page'};
358 } # unless
359
360 if ($date eq '') {
361   $condition .= "type = '$type'";
362 } else {
363   my $sod = $date . ' 00:00:00';
364   my $eod = $date . ' 23:59:59';
365
366   $condition .= "type = '$type' and timestamp > '$sod' and timestamp < '$eod'";
367 } # if
368
369 $total = CountLog(
370   userid     => $userid,
371   additional => $condition,
372 );
373
374 $next ||= 0;
375
376 $last = $next + $lines < $total ? $next + $lines : $total;
377
378 if (($next - $lines) > 0) {
379   $prev = $next - $lines;
380 } else {
381   $prev = $next == 0 ? -1 : 0;
382 } # if
383
384 Body($type);
385
386 Footing($table_name);
387
388 exit;