ee2d2f4352ec04d190e7583b355fe1d84fbad480
[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-2021, 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   my @senders = ReturnSenders(
181     userid   => $userid,
182     type     => $type,
183     start_at => $next,
184     lines    => $lines,
185     date     => $date
186   );
187
188   for my $sender (@senders) {
189     my $msgs = ReturnMessages(
190       userid => $userid,
191       sender => $sender,
192     );
193
194     my $leftclass    = 'tableleftdata';
195     my $dataclass    = 'tabledata';
196     my $rightclass   = 'tablerightdata';
197     my $senderclass  = 'sender';
198     my $subjectclass = 'subject';
199
200     # Check to see if this is the last line
201     if ((($next + 1) % $lines) == (@senders % $lines)) {
202       # We always "bottom" the first column
203       $leftclass = 'tablebottomleft';
204
205       # Check to see if there any message lines to display
206       unless (@$msgs) {
207         $dataclass   = 'tablebottomdata';
208         $rightclass  = 'tablebottomright';
209         $senderclass = 'senderbottom';
210       } # unless
211     } # if
212
213     # This is for the purposes of supplying a subject line if the mailto address
214     # is clicked on. It's kludgy because we are simply grabbing the subject line
215     # of the first email sent where there may be many emails from this sender
216     # Still it is often the right subject (or a good enough one)
217     #
218     # A little tricky here because of transliteration. If I test for
219     # $msg->[0]{subject} when $msg->[0] is essentially empty I create the hash
220     # making it non empty. Therefore I need to first test if $msgs->[0] exists
221     # first.
222     my $heading = '';
223
224     if ($msgs->[0]) {
225       $heading = $msgs->[0]{subject} if $msgs->[0]{subject};
226     } # if
227
228     my ($onlist, $seq);
229
230     my $rule      = 'none';
231     my $hit_count = 0;
232
233     ($onlist, $rule, $seq, $hit_count) = OnWhitelist($sender, $userid, 0);
234
235     unless ($onlist) {
236       ($onlist, $rule, $seq, $hit_count) = OnBlacklist($sender, 0);
237
238       unless ($onlist) {
239         ($onlist, $rule, $seq, $hit_count) = OnNulllist($sender, 0);
240       } # unless
241     } # unless
242
243     $hit_count //= '';
244
245     my $list     = '';
246     my $sequence = 0;
247     my $comment  = '';
248
249     # Parse rule
250     if ($rule) {
251       if ($rule =~ /\((\w+):(\d+)\)\s+"(\S*)"/) {
252         $list     = $1;
253         $sequence = $2;
254         $rule     = $3;
255         $comment  = '';
256       } elsif ($rule =~ /\((\w+):(\d+)\)\s+"(\S*) - (.*)"/) {
257         $list     = $1;
258         $sequence = $2;
259         $rule     = $3;
260         $comment  = $4;
261       } # if
262
263       $rule =~ s/\\@/\@/;
264     } # if
265
266     $sequence //= 0;
267     $next++;
268
269     # Start Sender line
270     my $rowspan = @$msgs + 1;
271
272     print start_Tr {-valign => 'middle'};
273     print td {
274       -class   => $leftclass,
275       -align   => 'right',
276       -valign  => 'middle',
277       -rowspan => $rowspan,
278     }, $next,
279       checkbox {
280         -name   => "action$next",
281         -label  => '',
282         -valign => 'middle',
283       };
284
285       print hidden({
286         -name    => "email$next",
287         -default => $sender,
288       });
289
290     # Get subject line
291     $heading = "?subject=$heading" if $heading;
292
293     print td {
294       -class => $senderclass,
295     }, a {
296       -href  => "mailto:$sender$heading",
297     }, "&nbsp;$sender";
298
299     my $listlink = ($list and $sequence) ? "$list:$sequence" : '&nbsp;';
300
301     print td {
302       -class => $dataclass,
303       -align => 'right',
304     }, a {
305       href  => "/maps/php/list.php?type=$list&next=" . ($sequence - 1),
306     }, $listlink,
307     td {
308       -class => $dataclass,
309       -align => 'right',
310     }, "$hit_count&nbsp;",
311     td {
312       -class => $dataclass,
313     }, $rule,
314     td {
315       -class => $rightclass,
316     }, $comment;
317     print end_Tr;
318
319     my $msgnbr = 0;
320
321     for my $rec (@$msgs) {
322       $msgnbr++;
323
324       # We increased $next earlier so do not add 1 here
325       if (($next % $lines) == (@senders % $lines)) {
326         $dataclass    = 'tablebottomdata';
327         $rightclass   = 'tablebottomright' if $msgnbr == @$msgs;
328
329         # Only subjectbottom the last message
330         $subjectclass = 'subjectbottom' if $msgnbr == @$msgs;
331       } # if
332
333       if ($date eq substr ($rec->{timestamp}, 0, 10)) {
334         $rec->{date} = b font {-color => 'green'}, SQLDatetime2UnixDatetime $rec->{timestamp};
335       } else {
336         $rec->{date} = SQLDatetime2UnixDatetime $rec->{timestamp};
337       } # if
338
339       $rec->{subject} //= '&lt;Unspecified&gt;';
340       $rec->{subject} = decode_mimewords ($rec->{subject});
341       $rec->{subject} =~ s/\>/&gt;/g;
342       $rec->{subject} =~ s/\</&lt;/g;
343
344       print
345         Tr [
346           td {
347             -class   => $subjectclass,
348             -colspan => 4,
349           }, a {
350             -href    => "display.cgi?sender=$sender;msg_date=$rec->{timestamp}",
351            }, '&nbsp;&nbsp;&nbsp;&nbsp;' . $rec->{subject},
352           td {-class => $rightclass,
353               -width => '150',
354               -align => 'right'}, span {-class => 'date'}, $rec->{date} . '&nbsp',
355         ];
356     } # for
357   } # for
358
359   print end_table;
360   print end_form;
361
362   MakeButtons $type;
363
364   return;
365 } # Body
366
367 # Main
368 my $condition;
369 my @scripts = ('ListActions.js');
370
371 my $heading_date =$date ne '' ? ' on ' . FormatDate ($date, 1) : '';
372
373 $userid = Heading(
374   'getcookie',
375   '',
376   (ucfirst ($type) . ' Report'),
377   $types{$type} [0],
378   $types{$type} [1] . $heading_date,
379   $table_name,
380   @scripts
381 );
382
383 $userid ||= $ENV{USER};
384
385 SetContext($userid);
386 NavigationBar($userid);
387
388 unless ($lines) {
389   my %options = GetUserOptions($userid);
390   $lines = $options{'Page'};
391 } # unless
392
393 if ($date eq '') {
394   $condition .= "type = '$type'";
395 } else {
396   my $sod = $date . ' 00:00:00';
397   my $eod = $date . ' 23:59:59';
398
399   $condition .= "type = '$type' and timestamp > '$sod' and timestamp < '$eod'";
400 } # if
401
402 # Need to count distinct on sender
403 $total = CountLogDistinct(
404   userid     => $userid,
405   column     => 'sender',
406   additional => $condition,
407 );
408
409 $next ||= 0;
410
411 $last = $next + $lines < $total ? $next + $lines : $total;
412
413 if (($next - $lines) > 0) {
414   $prev = $next - $lines;
415 } else {
416   $prev = $next == 0 ? -1 : 0;
417 } # if
418
419 Body($type);
420
421 Footing($table_name);
422
423 exit;