Ticket #240: report_order.diff

File report_order.diff, 14.8 kB (added by shalligan@…, 7 years ago)

Patch to add this functionality

  • scripts/send-quarantine-digests.pl

     
    8484    my $cfg = "/var/amavisd/maia/scripts/database.cfg"; 
    8585 
    8686    # CONFIGURE THIS: Base URL to maia scripts 
    87     my $base_url = "http://domain.net/"; 
     87    my $base_url = "http://example.com/"; 
    8888 
    8989    # CONFIGURE THIS: template directory 
    9090    my $template_dir = "/var/amavisd/maia/templates/"; 
     
    9696    #           = "received_date DIRECTION" 
    9797    #           = "recipient_id DIRECTION" 
    9898    # Where DIRECTION is ASC or DESC 
    99     my $ham_sort = "score DESC";   # puts the high scores at the top 
    100     my $spam_sort = "score ASC";   # puts the low scroes at the top 
    101     my $virus_sort = "received_date DESC"; 
    102     my $banned_file_sort = "received_date DESC"; 
    103     my $bad_header_sort = "received_date DESC"; 
     99    my %sort = (); 
     100    $sort{'ham'} = "score DESC";   # puts the high scores at the top 
     101    $sort{'spam'} = "score ASC";   # puts the low scroes at the top 
     102    $sort{'virus'} = "received_date DESC"; 
     103    $sort{'banned_file'} = "received_date DESC"; 
     104    $sort{'bad_header'} = "received_date DESC"; 
    104105     
    105106    my $titles = {  'spam'        =>  "Spam Quarantine", 
    106107                    'virus'       =>  "Virus Quarantine", 
     
    108109                    'bad_header'  =>  "Invalid Email Headers", 
    109110                    'ham'         =>  "Delivered Email" 
    110111                 }; 
     112    # The order of the sections of the digest report 
     113    # Valid elements are 'spam', 'ham', 'virus', banned_file', and 'bad_header' 
     114    # Omit any of these elements to leave them out of the report 
     115 
     116    my @report_order = ('spam','ham','virus','banned_file','bad_header'); 
     117 
    111118######################################################################## 
    112119# End of user-configurable portion.  There should be no need to modify # 
    113120# anything below this point.                                           # 
     
    242249 
    243250        #regenerate template vars for every user, to minimize memory usage 
    244251        my %vars = ( 
    245             'admin_email'     => $admin_email, 
    246             'date'            => $dbtimestamp, 
    247             'maia_user_id'    => $user_id, 
    248             'recipient'       => $user_email, 
    249             'titles'          => $titles, 
    250             'baseurl'         => $base_url 
    251         ); 
     252                    'admin_email'     => $admin_email, 
     253                    'date'            => $dbtimestamp, 
     254                    'maia_user_id'    => $user_id, 
     255                    'recipient'       => $user_email, 
     256                    'titles'          => $titles, 
     257                    'baseurl'         => $base_url 
     258                    ); 
     259        my %report_type = ( 
     260                           ham => 'H', 
     261                           spam => 'S', 
     262                           virus => 'V', 
     263                           banned_file => 'F', 
     264                           bad_header => 'B', 
     265                           ); 
     266         
     267        # We need to use an array to Separate Report Elements, since hashes can't keep reliable ordering 
     268        my $report_count = 0; 
     269        for my $report_element (@report_order) { 
     270         
     271            $select = "SELECT maia_mail_recipients.token, maia_mail.received_date, maia_mail.score, " . 
     272                "maia_mail.sender_email, maia_mail.subject " . 
     273                "FROM maia_mail, maia_mail_recipients " . 
     274                "WHERE maia_mail.id = maia_mail_recipients.mail_id " . 
     275                "AND maia_mail_recipients.type = '$report_type{$report_element}' " . 
     276                "AND maia_mail_recipients.recipient_id = ? " . 
     277                "ORDER BY maia_mail.$sort{$report_element}"; 
    252278 
    253  
    254         # Ham cache table 
    255         $select = "SELECT maia_mail_recipients.token, maia_mail.received_date, maia_mail.score, " . 
    256                          "maia_mail.sender_email, maia_mail.subject " . 
    257                   "FROM maia_mail, maia_mail_recipients " . 
    258                   "WHERE maia_mail.id = maia_mail_recipients.mail_id " . 
    259                   "AND maia_mail_recipients.type = 'H' " . 
    260                   "AND maia_mail_recipients.recipient_id = ? " . 
    261                   "ORDER BY maia_mail.$ham_sort"; 
    262         $sth = $dbh->prepare($select) 
    263                    or die (sprintf("Maia: [send-quarantine-digests] Couldn't prepare query: %s", $dbh->errstr)); 
    264         $sth->execute($user_id) 
    265             or die (sprintf("Maia: [send-quarantine-digests] Couldn't execute query: %s", $dbh->errstr)); 
    266         if ($sth->rows > 0) { 
    267             $at_least_one = 1; 
    268             $rowcount = 0; 
    269             while (@row = $sth->fetchrow_array()) { 
    270                 $token = $1 if $row[0] =~ /^([a-zA-Z0-9]{32})$/si; # untaint 
    271                 $received_date = $1 if $row[1] =~ /^(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})$/si; # untaint 
    272                 $score = $1 if $row[2] =~ /^(\d+\.\d+)$/si; # untaint 
    273                 $sender = $1 if $row[3] =~ /^(.+\@.+\..+)$/si; # untaint 
    274                 $subject = $1 if $row[4] =~ /^(.*)$/si; # untaint 
    275                 if ($subject eq "") { 
    276                     $subject = "(no subject)"; 
    277                 } 
    278  
    279                 $vars{'list'}{'ham'}[$rowcount]{'token'} =  $token; 
    280                 $vars{'list'}{'ham'}[$rowcount]{'received_date'} =  $received_date; 
    281                 $vars{'list'}{'ham'}[$rowcount]{'score'} =  $score; 
    282                 $vars{'list'}{'ham'}[$rowcount]{'sender'} =  $sender; 
    283                 $vars{'list'}{'ham'}[$rowcount]{'subject'} =  $subject; 
    284  
    285                 $rowcount++; 
    286             } 
    287         } 
    288         $sth->finish(); 
    289  
    290         # Spam Quarantine table 
    291         $select = "SELECT maia_mail_recipients.token, maia_mail.received_date, maia_mail.score, " . 
    292                          "maia_mail.sender_email, maia_mail.subject " . 
    293                   "FROM maia_mail, maia_mail_recipients " . 
    294                   "WHERE maia_mail.id = maia_mail_recipients.mail_id " . 
    295                   "AND maia_mail_recipients.type = 'S' " . 
    296                   "AND maia_mail_recipients.recipient_id = ? " . 
    297                   "ORDER BY maia_mail.$spam_sort"; 
    298         $sth = $dbh->prepare($select) 
    299                    or die (sprintf("Maia: [send-quarantine-digests] Couldn't prepare query: %s", $dbh->errstr)); 
    300         $sth->execute($user_id) 
    301             or die (sprintf("Maia: [send-quarantine-digests] Couldn't execute query: %s", $dbh->errstr)); 
    302         if ($sth->rows > 0) { 
    303             $at_least_one = 1; 
    304             $rowcount = 0; 
    305             while (@row = $sth->fetchrow_array()) { 
    306                 $token = $1 if $row[0] =~ /^([a-zA-Z0-9]{32})$/si; # untaint 
    307                 $received_date = $1 if $row[1] =~ /^(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})$/si; # untaint 
    308                 $score = $1 if $row[2] =~ /^(\d+\.\d+)$/si; # untaint 
    309                 $sender = $1 if $row[3] =~ /^(.+\@.+\..+)$/si; # untaint 
    310                 $subject = $1 if $row[4] =~ /^(.*)$/si; # untaint 
    311                 if ($subject eq "") { 
    312                     $subject = "(no subject)"; 
    313                 } 
    314  
    315                 $vars{'list'}{'spam'}[$rowcount]{'token'} =  $token; 
    316                 $vars{'list'}{'spam'}[$rowcount]{'received_date'} =  $received_date; 
    317                 $vars{'list'}{'spam'}[$rowcount]{'score'} =  $score; 
    318                 $vars{'list'}{'spam'}[$rowcount]{'sender'} =  $sender; 
    319                 $vars{'list'}{'spam'}[$rowcount]{'subject'} =  $subject; 
    320  
    321                 $rowcount++; 
    322             } 
    323         } 
    324         $sth->finish(); 
    325      
    326         # Virus Quarantine table 
    327         $select = "SELECT maia_mail_recipients.token, maia_mail.received_date, " . 
    328                          "maia_mail.sender_email, maia_mail.subject " . 
    329                   "FROM maia_mail, maia_mail_recipients " . 
    330                   "WHERE maia_mail.id = maia_mail_recipients.mail_id " . 
    331                   "AND maia_mail_recipients.type = 'V' " . 
    332                   "AND maia_mail_recipients.recipient_id = ? " . 
    333                   "ORDER BY maia_mail.$virus_sort"; 
    334         $sth = $dbh->prepare($select) 
    335                    or die (sprintf("Maia: [send-quarantine-digests] Couldn't prepare query: %s", $dbh->errstr)); 
    336         $sth->execute($user_id) 
    337             or die (sprintf("Maia: [send-quarantine-digests] Couldn't execute query: %s", $dbh->errstr)); 
    338         if ($sth->rows > 0) { 
    339             $at_least_one = 1; 
    340             $rowcount = 0; 
    341             while (@row = $sth->fetchrow_array()) { 
    342                 $token = $1 if $row[0] =~ /^([a-zA-Z0-9]{32})$/si; # untaint 
    343                 $received_date = $1 if $row[1] =~ /^(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})$/si; # untaint 
    344                 $sender = $1 if $row[2] =~ /^(.+\@.+\..+)$/si; # untaint 
    345                 $subject = $1 if $row[3] =~ /^(.*)$/si; # untaint 
    346                 if ($subject eq "") { 
    347                     $subject = "(no subject)"; 
    348                 } 
    349  
    350                 $vars{'list'}{'virus'}[$rowcount]{'token'} =  $token; 
    351                 $vars{'list'}{'virus'}[$rowcount]{'received_date'} =  $received_date; 
    352                 $vars{'list'}{'virus'}[$rowcount]{'sender'} =  $sender; 
    353                 $vars{'list'}{'virus'}[$rowcount]{'subject'} =  $subject; 
    354                 $rowcount++; 
    355             } 
    356         } 
    357         $sth->finish(); 
    358      
    359         # Attachment Quarantine table 
    360         $select = "SELECT maia_mail_recipients.token, maia_mail.received_date, " . 
    361                          "maia_mail.sender_email, maia_mail.subject " . 
    362                   "FROM maia_mail, maia_mail_recipients " . 
    363                   "WHERE maia_mail.id = maia_mail_recipients.mail_id " . 
    364                   "AND maia_mail_recipients.type = 'F' " . 
    365                   "AND maia_mail_recipients.recipient_id = ? " . 
    366                   "ORDER BY maia_mail.$banned_file_sort"; 
    367         $sth = $dbh->prepare($select) 
    368                    or die (sprintf("Maia: [send-quarantine-digests] Couldn't prepare query: %s", $dbh->errstr)); 
    369         $sth->execute($user_id) 
    370             or die (sprintf("Maia: [send-quarantine-digests] Couldn't execute query: %s", $dbh->errstr)); 
    371         if ($sth->rows > 0) { 
    372             $at_least_one = 1; 
    373             $rowcount = 0; 
    374             while (@row = $sth->fetchrow_array()) { 
    375                 $token = $1 if $row[0] =~ /^([a-zA-Z0-9]{32})$/si; # untaint 
    376                 $received_date = $1 if $row[1] =~ /^(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})$/si; # untaint 
    377                 $sender = $1 if $row[2] =~ /^(.+\@.+\..+)$/si; # untaint 
    378                 $subject = $1 if $row[3] =~ /^(.*)$/si; # untaint 
    379                 if ($subject eq "") { 
    380                     $subject = "(no subject)"; 
    381                 } 
    382  
    383                 $vars{'list'}{'banned_file'}[$rowcount]{'token'} =  $token; 
    384                 $vars{'list'}{'banned_file'}[$rowcount]{'received_date'} =  $received_date; 
    385                 $vars{'list'}{'banned_file'}[$rowcount]{'sender'} =  $sender; 
    386                 $vars{'list'}{'banned_file'}[$rowcount]{'subject'} =  $subject; 
    387  
    388                 $rowcount++; 
    389             } 
    390         } 
    391         $sth->finish(); 
    392      
    393         # Invalid Header Quarantine table 
    394         $select = "SELECT maia_mail_recipients.token, maia_mail.received_date, " . 
    395                          "maia_mail.sender_email, maia_mail.subject " . 
    396                   "FROM maia_mail, maia_mail_recipients " . 
    397                   "WHERE maia_mail.id = maia_mail_recipients.mail_id " . 
    398                   "AND maia_mail_recipients.type = 'B' " . 
    399                   "AND maia_mail_recipients.recipient_id = ? " . 
    400                   "ORDER BY maia_mail.$bad_header_sort"; 
    401         $sth = $dbh->prepare($select) 
    402                    or die (sprintf("Maia: [send-quarantine-digests] Couldn't prepare query: %s", $dbh->errstr)); 
    403         $sth->execute($user_id) 
    404             or die (sprintf("Maia: [send-quarantine-digests] Couldn't execute query: %s", $dbh->errstr)); 
    405         if ($sth->rows > 0) { 
    406             $at_least_one = 1; 
    407             $rowcount = 0; 
    408             while (@row = $sth->fetchrow_array()) { 
    409                 $token = $1 if $row[0] =~ /^([a-zA-Z0-9]{32})$/si; # untaint 
    410                 $received_date = $1 if $row[1] =~ /^(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})$/si; # untaint 
    411                 $sender = $1 if $row[2] =~ /^(.+\@.+\..+)$/si; # untaint 
    412                 $subject = $1 if $row[3] =~ /^(.*)$/si; # untaint 
    413                 if ($subject eq "") { 
    414                     $subject = "(no subject)"; 
    415                 } 
    416  
    417                 $vars{'list'}{'bad_header'}[$rowcount]{'token'} =  $token; 
    418                 $vars{'list'}{'bad_header'}[$rowcount]{'received_date'} =  $received_date; 
    419                 $vars{'list'}{'bad_header'}[$rowcount]{'sender'} =  $sender; 
    420                 $vars{'list'}{'bad_header'}[$rowcount]{'subject'} =  $subject; 
    421                 $rowcount++; 
    422             } 
    423         } 
    424         $sth->finish(); 
    425  
     279            $sth = $dbh->prepare($select) 
     280                or die (sprintf("Maia: [send-quarantine-digests] Couldn't prepare query: %s", $dbh->errstr)); 
     281            $sth->execute($user_id) 
     282                or die (sprintf("Maia: [send-quarantine-digests] Couldn't execute query: %s", $dbh->errstr)); 
     283            if ($sth->rows > 0) { 
     284                $at_least_one = 1; 
     285                $rowcount = 0; 
     286                while (@row = $sth->fetchrow_array()) { 
     287                    $token = $1 if $row[0] =~ /^([a-zA-Z0-9]{32})$/si; # untaint 
     288                    $received_date = $1 if $row[1] =~ /^(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})$/si; # untaint 
     289                    $score = $1 if $row[2] =~ /^(\d+\.\d+)$/si; # untaint 
     290                    $sender = $1 if $row[3] =~ /^(.+\@.+\..+)$/si; # untaint 
     291                    $subject = $1 if $row[4] =~ /^(.*)$/si; # untaint 
     292                    if ($subject eq "") { 
     293                        $subject = "(no subject)"; 
     294                    } 
     295                     
     296                    $vars{'list'}[$report_count]{$report_element}[$rowcount]{'token'} =  $token; 
     297                    $vars{'list'}[$report_count]{$report_element}[$rowcount]{'received_date'} =  $received_date; 
     298                    if ($report_element eq 'ham' || $report_element eq 'spam') { 
     299                        $vars{'list'}[$report_count]{$report_element}[$rowcount]{'score'} =  $score; 
     300                    } 
     301                    $vars{'list'}[$report_count]{$report_element}[$rowcount]{'sender'} =  $sender; 
     302                    $vars{'list'}[$report_count]{$report_element}[$rowcount]{'subject'} =  $subject; 
     303                     
     304                    $rowcount++; 
     305                } 
     306            } 
     307            $sth->finish(); 
     308            $report_count++ 
     309        } 
     310         
    426311        $vars{'confirm_token'} = generate_confirm_token($dbh, $user_id); 
    427312        my $output = ''; 
    428313        my $template = Template->new({INCLUDE_PATH => $template_dir, 
  • templates/digest.tpl

     
    1212<a href="[% baseurl %]/confirm.php?id=[% maia_user_id %]&ts=[% date %]&token=[% confirm_token %]&manage=true">[Log in]</a> to manage your Maia account</a> 
    1313</div> 
    1414<div>Maia Mailguard made the following decisions regarding you email. If it made a mistake, you may report any email that was presumed good, but was actually spam, and release any that were quarantined incorrectly by clicking the corresponding link.</div> 
    15 [% FOREACH l IN list %]  
     15[% FOREACH r IN list %] 
     16[% FOREACH l IN r %]  
    1617[% FOREACH l.value %] 
    1718[% IF loop.first %] 
    1819<div align="center"> 
     
    4647</table></div><p>&nbsp;</p> 
    4748[% END %] 
    4849[% END %] 
     50[% END %] 
    4951[% IF loop.last %] 
    5052<div align="center"> 
    5153<table border="0" cellspacing="2" cellpadding="2" width="100%">