Fix sortability of mail queue dates
authorJamie Cameron <jcameron@webmin.com>
Mon, 16 Feb 2009 22:45:12 +0000 (22:45 +0000)
committerJamie Cameron <jcameron@webmin.com>
Mon, 16 Feb 2009 22:45:12 +0000 (22:45 +0000)
postfix/config.info
postfix/mailq.cgi
postfix/postfix-lib.pl
web-lib-funcs.pl

index b2bf55e..42ab2ae 100644 (file)
@@ -5,7 +5,7 @@ sort_mode=Sort tables by,1,0-Order in file,1-Name
 columns=Columns for aliases and other tables,1,2-2,1-1
 show_cmts=Show descriptions in tables?,1,1-Yes,0-No
 prefix_cmts=Prefix all description comments with <tt>Webmin</tt>?,1,1-Yes,0-No
-mailq_sort=Sort mail queue by,1,0-Queue ID,1-From address,2-To address,4-Status,5-Size
+mailq_sort=Sort mail queue by,1,0-Queue ID,1-From address,2-To address,4-Status,5-Size,6-Sent date
 mailq_count=Show size of mail queue on main page?,1,0-Yes,1-No
 check_config=Test config file after change?,1,1-Yes,0-No
 index_check=Check Postfix configuration on main page?,1,1-Yes,0-No
index 596d1b6..4d8a883 100755 (executable)
@@ -9,6 +9,7 @@ require './boxes-lib.pl';
 $access{'mailq'} || &error($text{'mailq_ecannot'});
 &ui_print_header(undef, $text{'mailq_title'}, "");
 
+# Get queued messages and sort
 @qfiles = &list_queue();
 if ($config{'mailq_sort'} == 0) {
        @qfiles = sort { $a->{'id'} cmp $b->{'id'} } @qfiles;
@@ -25,6 +26,9 @@ elsif ($config{'mailq_sort'} == 4) {
 elsif ($config{'mailq_sort'} == 5) {
        @qfiles = sort { $b->{'size'} <=> $a->{'size'} } @qfiles;
        }
+elsif ($config{'mailq_sort'} == 6) {
+       @qfiles = sort { $b->{'time'} <=> $a->{'time'} } @qfiles;
+       }
 
 if (@qfiles) {
        if (@qfiles > $config{'perpage'}) {
index 3fe6ebd..6b55c2a 100644 (file)
@@ -1224,15 +1224,13 @@ if (!$rv || $rv->code || !$rv->all_entries) {
 # gives a new number of mapping
 sub init_new_mapping
 {
-$maps = &get_maps($_[0]);
-
+my $maps = &get_maps($_[0]);
 my $max_number = 0;
-
-foreach $trans (@{$maps})
-{
-if ($trans->{'number'} > $max_number) { $max_number = $trans->{'number'}; }
-}
-
+foreach $trans (@{$maps}) {
+       if ($trans->{'number'} > $max_number) {
+               $max_number = $trans->{'number'};
+               }
+       }
 return $max_number+1;
 }
 
@@ -1241,15 +1239,15 @@ sub postfix_mail_file
 {
 local @s = &postfix_mail_system();
 if ($s[0] == 0) {
-return "$s[1]/$_[0]";
-}
+       return "$s[1]/$_[0]";
+       }
 elsif (@_ > 1) {
-return "$_[7]/$s[1]";
-}
+       return "$_[7]/$s[1]";
+       }
 else {
-local @u = getpwnam($_[0]);
-return "$u[7]/$s[1]";
-}
+       local @u = getpwnam($_[0]);
+       return "$u[7]/$s[1]";
+       }
 }
 
 # postfix_mail_system()
@@ -1259,17 +1257,17 @@ return "$u[7]/$s[1]";
 sub postfix_mail_system
 {
 if (!defined(@mail_system_cache)) {
-local $home_mailbox = &get_current_value("home_mailbox");
-if ($home_mailbox) {
-@mail_system_cache = $home_mailbox =~ /^(.*)\/$/ ?
-       (2, $1) : (1, $home_mailbox);
-}
-else {
-local $mail_spool_directory =
-       &get_current_value("mail_spool_directory");
-@mail_system_cache = (0, $mail_spool_directory);
-}
-}
+       local $home_mailbox = &get_current_value("home_mailbox");
+       if ($home_mailbox) {
+               @mail_system_cache = $home_mailbox =~ /^(.*)\/$/ ?
+                       (2, $1) : (1, $home_mailbox);
+               }
+       else {
+               local $mail_spool_directory =
+                       &get_current_value("mail_spool_directory");
+               @mail_system_cache = (0, $mail_spool_directory);
+               }
+       }
 return wantarray ? @mail_system_cache : $mail_system_cache[0];
 }
 
@@ -1280,20 +1278,26 @@ sub list_queue
 local @qfiles;
 &open_execute_command(MAILQ, $config{'mailq_cmd'}, 1, 1);
 while(<MAILQ>) {
-next if (/^(\S+)\s+is\s+empty/i || /^\s+Total\s+requests:/i);
-if (/^([^\s\*\!]+)[\*\!]?\s*(\d+)\s+(\S+\s+\S+\s+\d+\s+\d+:\d+:\d+)\s+(.*)/) {
-push(@qfiles, { 'id' => $1,
-               'size' => $2,
-               'date' => $3,
-               'from' => $4 });
-}
-elsif (/\((.*)\)/ && @qfiles) {
-$qfiles[$#qfiles]->{'status'} = $1;
-}
-elsif (/^\s+(\S+)/ && @qfiles) {
-$qfiles[$#qfiles]->{'to'} .= "$1 ";
-}
-}
+       next if (/^(\S+)\s+is\s+empty/i || /^\s+Total\s+requests:/i);
+       if (/^([^\s\*\!]+)[\*\!]?\s*(\d+)\s+(\S+\s+\S+\s+\d+\s+\d+:\d+:\d+)\s+(.*)/) {
+               local $q = { 'id' => $1, 'size' => $2,
+                             'date' => $3, 'from' => $4 };
+               if (defined(&parse_mail_date)) {
+                       local $t = &parse_mail_date($q->{'date'});
+                       if ($t) {
+                               $q->{'date'} = &make_date($t, 0, 'yyyy/mm/dd');
+                               $q->{'time'} = $t;
+                               }
+                       }
+               push(@qfiles, $q);
+               }
+       elsif (/\((.*)\)/ && @qfiles) {
+               $qfiles[$#qfiles]->{'status'} = $1;
+               }
+       elsif (/^\s+(\S+)/ && @qfiles) {
+               $qfiles[$#qfiles]->{'to'} .= "$1 ";
+               }
+       }
 close(MAILQ);
 return @qfiles;
 }
@@ -1303,10 +1307,10 @@ return @qfiles;
 sub parse_queue_file
 {
 local @qfiles = ( &recurse_files("$config{'mailq_dir'}/active"),
-  &recurse_files("$config{'mailq_dir'}/incoming"),
-  &recurse_files("$config{'mailq_dir'}/deferred"),
-  &recurse_files("$config{'mailq_dir'}/corrupt"),
-  &recurse_files("$config{'mailq_dir'}/hold"),
+                 &recurse_files("$config{'mailq_dir'}/incoming"),
+                 &recurse_files("$config{'mailq_dir'}/deferred"),
+                 &recurse_files("$config{'mailq_dir'}/corrupt"),
+                 &recurse_files("$config{'mailq_dir'}/hold"),
 );
 local $f = $_[0];
 local ($file) = grep { $_ =~ /\/$f$/ } @qfiles;
@@ -1315,31 +1319,31 @@ local $mode = 0;
 local ($mail, @headers);
 &open_execute_command(QUEUE, "$config{'postcat_cmd'} ".quotemeta($file), 1, 1);
 while(<QUEUE>) {
-if (/^\*\*\*\s+MESSAGE\s+CONTENTS/ && !$mode) {           # Start of headers
-$mode = 1;
-}
-elsif (/^\*\*\*\s+HEADER\s+EXTRACTED/ && $mode) {  # End of email
-       last;
-       }
-elsif ($mode == 1 && /^\s*$/) {                           # End of headers
-       $mode = 2;
-       }
-elsif ($mode == 1 && /^(\S+):\s*(.*)/) {          # Found a header
-       push(@headers, [ $1, $2 ]);
-       }
-elsif ($mode == 1 && /^(\s+.*)/) {                # Header continuation
-       $headers[$#headers]->[1] .= $1 unless($#headers < 0);
-       }
-elsif ($mode == 2) {                              # Part of body
-       $mail->{'size'} += length($_);
-       $mail->{'body'} .= $_;
+       if (/^\*\*\*\s+MESSAGE\s+CONTENTS/ && !$mode) {    # Start of headers
+               $mode = 1;
+               }
+       elsif (/^\*\*\*\s+HEADER\s+EXTRACTED/ && $mode) {  # End of email
+               last;
+               }
+       elsif ($mode == 1 && /^\s*$/) {                    # End of headers
+               $mode = 2;
+               }
+       elsif ($mode == 1 && /^(\S+):\s*(.*)/) {           # Found a header
+               push(@headers, [ $1, $2 ]);
+               }
+       elsif ($mode == 1 && /^(\s+.*)/) {                 # Header continuation
+               $headers[$#headers]->[1] .= $1 unless($#headers < 0);
+               }
+       elsif ($mode == 2) {                               # Part of body
+               $mail->{'size'} += length($_);
+               $mail->{'body'} .= $_;
+               }
        }
-}
 close(QUEUE);
 $mail->{'headers'} = \@headers;
 foreach $h (@headers) {
-$mail->{'header'}->{lc($h->[0])} = $h->[1];
-}
+       $mail->{'header'}->{lc($h->[0])} = $h->[1];
+       }
 return $mail;
 }
 
index 1f07366..c2127e4 100755 (executable)
@@ -1531,18 +1531,26 @@ $main::has_command_cache{$_[0]} = $rv;
 return $rv;
 }
 
-=head2 make_date(seconds, [date-only])
+=head2 make_date(seconds, [date-only], [fmt])
 
 Converts a Unix date/time in seconds to a human-readable form, by default
-formatted like dd/mmm/yyyy hh:mm:ss.
+formatted like dd/mmm/yyyy hh:mm:ss. Parameters are :
+
+=item seconds - Unix time is seconds to convert.
+
+=item date-only - If set to 1, exclude the time from the returned string.
+
+=item fmt - Optional, one of dd/mon/yyyy, dd/mm/yyyy, mm/dd/yyyy or yyyy/mm/dd
 
 =cut
 sub make_date
 {
-local ($secs, $only) = @_;
+local ($secs, $only, $fmt) = @_;
 local @tm = localtime($secs);
 local $date;
-local $fmt = $gconfig{'dateformat'} || 'dd/mon/yyyy';
+if (!$fmt) {
+       $fmt = $gconfig{'dateformat'} || 'dd/mon/yyyy';
+       }
 if ($fmt eq 'dd/mon/yyyy') {
        $date = sprintf "%2.2d/%s/%4.4d",
                        $tm[3], $text{"smonth_".($tm[4]+1)}, $tm[5]+1900;