#!/usr/bin/perl
#
# xchk -
#     written by Charles Brabec
#
#     Displays an xwindows listing of unread mail
#
# This is free code. Enjoy.

use Tk;

# location of mail folders
$mdir = "/my/home/dir/.mail";

# orders list of folders to check
# each entry is "folder:x" where 
#     x=1 says list each unread message
#     x=0 says only list count of unread message
@boxes = ( 
   "inbox:1",
   "bugtraq:0",
   "lprng:0",
);

# how frequently (in milliseconds) should we refresh the list
$refresh = 10 * 1000; 

############ All the rest is code ##############

# setup our history database
%history = ();
foreach $line (@boxes) {
   my ($box,$tag) = split /:/, $line;
   $history{$box} = {};
   $history{$box}->{size} = 0;
   $history{$box}->{data} = [];
}

# open a window
$win = MainWindow->new();
$win->title("Unread Mail");

# trivial attempt to find a geom parameter and use it
if ($ARGV[0] =~ /^-geom/i) {
   $win->geometry($ARGV[1]);
}

# add a scrolling text area
$textarea = $win->Scrolled(
   'Text',
   -width => 98,
   -height => 24,
   -background => 'black',
   -foreground => 'white',
   -font => '-adobe-courier-medium-r-normal--10-100-75-75-m-62-iso8859-1',
   -scrollbars => 'e',
);

# setup some font/color tags for text in this area
$textarea->tagConfigure(
   'title',
   -background => 'black',
   -foreground => 'yellow',
   -font => '-adobe-courier-bold-r-normal--10-100-75-75-m-62-iso8859-1',
);
$textarea->tagConfigure(
   'from',
   -background => 'black',
   -foreground => 'red',
);
$textarea->tagConfigure(
   'to',
   -background => 'black',
   -foreground => 'cyan',
);
$textarea->tagConfigure(
   'subj',
   -background => 'black',
   -foreground => 'green',
);

# setup the refresh timer
$textarea->repeat(
   $refresh,
   \&check_mail,
);

# add a button to manually refresh the display
$resetbutton = $win->Button(
   -text => 'Check Now',
   -command => \&check_mail,
);

# add a button to kill the program
$quitbutton = $win->Button(
   -text => 'Quit',
   -command => sub { exit(0); },
);

# put all the items into the window
$textarea->pack(-side=>'top');
$quitbutton->pack(-side=>'right');
$resetbutton->pack(-side=>'right');

# refresh the display the first time
check_mail();

# and wait for events
MainLoop();

exit;

# routine to parse folders for unread messages and display
# results in the window
sub check_mail {
   @results = ();

#print STDERR "Starting check at ",time(),"\n";
   foreach $dat (@boxes) {
      ($box,$list) = split(/:/,$dat);
      $file = "$mdir/$box";
      my $size = (stat($file))[7];
      if ($size == $history{$box}->{size}) {
         push @results, @{$history{$box}->{data}};
         next;
      }
      $history{$box}->{size} = $size;
#print STDERR "Checking $box, size = $size\n";
      next if (!open (IFIL, $file));
      $history{$box}->{data} = [];
      $hdr=0; undef @lines;
      while (<IFIL>) {
         chop;
         $lin = $_;
         if (($hdr==0) && ($lin =~ /^From\s/)) {
            $hdr = 1;
            $from=""; $sub=""; $to=""; $stat=1;
         } elsif ($hdr) {
            if ($lin =~ /^\s*$/) {
               $hdr = 0;
               if ($stat) {
                  push @lines, [$from,$to,$sub];
               }
            } elsif ($lin =~ /^From:\s*(.*)$/) {
               $from = $1;
               $from =~ s/\s*\<[^\>]*\>//;
            } elsif ($lin =~ /^To:\s*(.*)$/) {
               $to = $1;
               $to =~ s/\s*\<[^\>]*\>//;
            } elsif ($lin =~ /^Subject:\s*(.*)$/i) {
               $sub = substr($1, 0, 48);
            } elsif ($lin =~ /^Status:\s*[^U\s]/) {
               $stat = 0;
            }
         }
      }
      close(IFIL);
   
      if ($#lines > -1) {
         $nm = $#lines + 1;
         if ($list > 0) {
            push @{$history{$box}->{data}},
               "====== $box, $nm new messages\n",'title';
            foreach $l (@lines) { 
               my $tmp;
               $tmp = substr($l->[1],0,24);
               $tmp .= " " x (25-length($tmp));
               push @{$history{$box}->{data}}, "$tmp", 'to';
               $tmp = substr($l->[0],0,24);
               $tmp .= " " x (25-length($tmp));
               push @{$history{$box}->{data}}, "$tmp", 'from';
               $tmp = $l->[2];
               push @{$history{$box}->{data}}, "$tmp\n", 'subj';
            }
         } else {
            push @{$history{$box}->{data}},
               "====== $box, $nm new messages\n",'title';
         }
      }
      push @results, @{$history{$box}->{data}};
   }

#print STDERR "So far, ",($#results+1)," results\n";
   
   if ($#results < 0) {
      push @results, "No new mail.\n", 'title';
   }

   $textarea->delete('0.0',end);
   while ($#results > -1) {
      my $str = shift @results;
      my $tag = shift @results;
      $textarea->insert(end,$str,$tag);
   }
}
