Fixed long standing bug about displaying proper message
[clearscm.git] / maps / bin / detail.cgi
index 7631983..55c9cbd 100755 (executable)
@@ -1,5 +1,6 @@
 #!/usr/bin/perl
-#################################################################################
+################################################################################
+#
 # File:         $RCSfile: detail.cgi,v $
 # Revision:     $Revision: 1.1 $
 # Description:  Displays list of email addresses based on report type.
 #
 # (c) Copyright 2000-2006, Andrew@DeFaria.com, all rights reserved.
 #
-################################################################################use strict;
+################################################################################
+use strict;
 use warnings;
 
 use MIME::Words qw(:all);
 use FindBin;
 $0 = $FindBin::Script;
 
-use lib $FindBin::Bin;
+use lib "$FindBin::Bin/../lib";
 
 use MAPS;
 use MAPSLog;
 use MAPSUtil;
 use MAPSWeb;
-use CGI qw (:standard *table start_td end_td start_Tr end_Tr start_div end_div);
+use CGI qw(:standard *table start_td end_td start_Tr end_Tr start_div end_div);
 use CGI::Carp 'fatalsToBrowser';
 
-my $type   = param ('type');
-my $next   = param ('next');
-my $lines  = param ('lines');
-my $date   = param ('date');
+my $type   = param('type');
+my $next   = param('next');
+my $lines  = param('lines');
+my $date   = param('date');
 
 $date ||= '';
 
@@ -72,7 +74,7 @@ my %types = (
 );
 
 sub MakeButtons {
-  my $type = shift;
+  my ($type) = @_;
 
   my $prev_button = $prev >= 0 ?
     a ({-href      => "detail.cgi?type=$type;date=$date;next=$prev",
@@ -88,49 +90,49 @@ sub MakeButtons {
   if ($type eq 'whitelist') {
     $buttons = $buttons .
       submit ({-name    => 'action',
-               -value   => 'Blacklist Marked',
+               -value   => 'Blacklist',
                -onClick => 'return CheckAtLeast1Checked (document.detail);'}) .
       submit ({-name    => 'action',
-               -value   => 'Nulllist Marked',
+               -value   => 'Nulllist',
                -onClick => 'return CheckAtLeast1Checked (document.detail);'}) .
       submit ({-name    => 'action',
-               -value   => 'Reset Marks',
+               -value   => 'Reset',
                -onClick => 'return ClearAll (document.detail);'});
   } elsif ($type eq 'blacklist') {
     $buttons = $buttons .
       submit ({-name    => 'action',
-               -value   => 'Whitelist Marked',
+               -value   => 'Whitelist',
                -onClick => 'return CheckAtLeast1Checked (document.detail);'}) .
       submit ({-name    => 'action',
-               -value   => 'Nulllist Marked',
+               -value   => 'Nulllist',
                -onClick => 'return CheckAtLeast1Checked (document.detail);'}) .
       submit ({-name    => 'action',
-               -value   => 'Reset Marks',
+               -value   => 'Reset',
                -onClick => 'return ClearAll (document.detail);'});
   } elsif ($type eq 'nulllist') {
     $buttons = $buttons .
       submit ({-name    => 'action',
-               -value   => 'Whitelist Marked',
+               -value   => 'Whitelist',
                -onClick => 'return CheckAtLeast1Checked (document.detail);'}) .
       submit ({-name    => 'action',
-               -value   => 'Blacklist Marked',
+               -value   => 'Blacklist',
                -onClick => 'return CheckAtLeast1Checked (document.detail);'}) .
       submit ({-name    => 'action',
-               -value   => 'Reset Marks',
+               -value   => 'Reset',
                -onClick => 'return ClearAll (document.detail);'});
   } else {
     $buttons = $buttons .
       submit ({-name    => 'action',
-               -value   => 'Whitelist Marked',
+               -value   => 'Whitelist',
                -onClick => 'return CheckAtLeast1Checked (document.detail);'}) .
       submit ({-name    => 'action',
-               -value   => 'Blacklist Marked',
+               -value   => 'Blacklist',
                -onClick => 'return CheckAtLeast1Checked (document.detail);'}) .
       submit ({-name    => 'action',
-               -value   => 'Nulllist Marked',
+               -value   => 'Nulllist',
                -onClick => 'return CheckAtLeast1Checked (document.detail);'}) .
       submit ({-name    => 'action',
-               -value   => 'Reset Marks',
+               -value   => 'Reset',
                -onClick => 'return ClearAll (document.detail);'});
   } # if
 
@@ -149,12 +151,12 @@ sub PrintTable {
     -action => 'processaction.cgi',
     -name   => 'detail'
   };
-  print start_table ({-align        => 'center',
-                      -id           => $table_name,
-                      -border       => 0,
-                      -cellspacing  => 0,
-                      -cellpadding  => 0,
-                      -width        => '100%'}) . "\n";
+  print start_table({-align        => 'center',
+                     -id           => $table_name,
+                     -border       => 0,
+                     -cellspacing  => 0,
+                     -cellpadding  => 0,
+                     -width        => '100%'}) . "\n";
 
   my $buttons = MakeButtons $type;
 
@@ -169,8 +171,37 @@ sub PrintTable {
     ];
   print end_div;
 
-  foreach my $sender (ReturnSenders $userid, $type, $next, $lines, $date) {
-    my @msgs = ReturnMessages $userid, $sender;
+  for my $sender (ReturnSenders($userid, $type, $next, $lines, $date)) {
+    my @msgs  = ReturnMessages($userid, $sender);
+    my @msgs2 = @msgs;
+
+    my ($onlist, $seq);
+
+    my $rule      = 'none';
+    my $hit_count = 0;
+
+    ($onlist, $rule, $seq, $hit_count) = OnWhitelist($sender, $userid, 0);
+
+    unless ($onlist) {
+      ($onlist, $rule, $seq, $hit_count) = OnBlacklist($sender, 0);
+
+      unless ($onlist) {
+        ($onlist, $rule, $seq, $hit_count) = OnNulllist($sender, 0);
+      } # unless
+    } # unless
+
+    if ($rule) {
+      $rule =~ s/Matching rule: \(//;
+      $rule =~ s/\)//;
+
+      if ($rule =~ /(\w+):(\d+)/) {
+        my $list     = $1;
+        my $sequence = $2 - 1;
+        my $link     = "<a href=\"/maps/php/list.php?type=$list&next=$sequence\">$list:$2</a>/$hit_count";
+
+        $rule =~ s/\w+:\d+/$link/;
+      } # if
+    } # if
 
     $next++;
     print
@@ -179,7 +210,7 @@ sub PrintTable {
       td {-class => 'tableborder'}, small ($next,
         checkbox {-name  => "action$next",
                   -label => ''}),
-          hidden ({-name     => "email$next",
+          hidden ({-name    => "email$next",
                    -default => $sender});
     print
       start_td {-align => 'left'};
@@ -190,21 +221,30 @@ sub PrintTable {
                    -border      => 0,
                    -width       => '100%',
                    -bgcolor     => '#d4d0c8'};
+
+    # Get subject line
+    my $heading = $msgs2[0][0] || '';
+    $heading = "?subject=$heading" if $heading;
     print
-      td {-class => 'tablelabel',
-          -valign => 'middle',
-          -width  => '40'}, 'Sender:',
-      td {-class  => 'sender',
-          -valign => 'middle'},
-      a {-href    => "mailto:$sender"}, $sender;
+      td {-class   => 'tablelabel',
+          -valign  => 'middle',
+          -width   => '40'}, 'Sender:',
+      td {-class   => 'sender',
+          -valign  => 'middle',
+          -width   => '40%'},
+        a {-href   => "mailto:$sender$heading"}, $sender,
+      td {
+          -valign  => 'middle'},
+          $rule;
     print
       end_table;
 
     my $messages = 1;
 
-    foreach (@msgs) {
-      my $msg_date = pop @{$_};
-      my $subject  = pop @{$_};
+    for (@msgs) {
+      my $msg_date  = pop @{$_};
+      my $link_date = $msg_date;
+      my $subject   = pop @{$_};
 
       if ($date eq substr ($msg_date, 0, 10)) {
         $msg_date = b font {-color => 'green'}, SQLDatetime2UnixDatetime $msg_date;
@@ -223,7 +263,6 @@ sub PrintTable {
                      -cellspacing => 2,
                      -border      => 0,
                      -width       => '100%'};
-      my $msg_nbr = $messages;
       print
         Tr [
           td {-class   => 'msgnbr',
@@ -236,16 +275,16 @@ sub PrintTable {
           td {-class   => 'subject',
               -valign  => 'middle',
               -bgcolor => '#ffffff'},
-           a {-href    => "display.cgi?sender=$sender;msg_nbr=$msg_nbr"}, $subject,
+           a {-href    => "display.cgi?sender=$sender;msg_date=$link_date"}, $subject,
           td {-class   => 'date',
-              -width   => '130',
+              -width   => '150',
               -valign  => 'middle'}, $msg_date
         ];
       print end_table;
-    } # foreach
+    } # for
     print end_td;
     print end_Tr;
-  } # foreach
+  } # for
 
   print start_div {-class => 'toolbar'};
   print
@@ -259,14 +298,17 @@ sub PrintTable {
   print end_div;
   print end_table;
   print end_form;
+
+  return;
 } # PrintTable
 
 # Main
+my $condition;
 my @scripts = ('ListActions.js');
 
 my $heading_date =$date ne '' ? ' on ' . FormatDate ($date) : '';
 
-$userid = Heading (
+$userid = Heading(
   'getcookie',
   '',
   (ucfirst ($type) . ' Report'),
@@ -278,11 +320,11 @@ $userid = Heading (
 
 $userid ||= $ENV{USER};
 
-SetContext $userid;
-NavigationBar $userid;
+SetContext($userid);
+NavigationBar($userid);
 
 unless ($lines) {
-  my %options = GetUserOptions $userid;
+  my %options = GetUserOptions($userid);
   $lines = $options{'Page'};
 } # unless
 
@@ -296,7 +338,7 @@ if ($date eq '') {
               . "and timestamp > '$sod' and timestamp < '$eod' ";
 } # if
 
-$total = MAPSDB::count_distinct ('log', 'sender', $condition);
+$total = count_distinct('log', 'sender', $condition);
 
 $next ||= 0;
 
@@ -305,11 +347,11 @@ $last = $next + $lines < $total ? $next + $lines : $total;
 if (($next - $lines) > 0) {
   $prev = $next - $lines;
 } else {
-  $prev = $next eq 0 ? -1 : 0;
+  $prev = $next == 0 ? -1 : 0;
 } # if
 
-PrintTable $type;
+PrintTable($type);
 
-Footing $table_name;
+Footing($table_name);
 
 exit;