#!/usr/bin/perl -w

#########################################################################
# proccolor.pl 0.1.0 - Procmail log colorizer.                          #
# by Paul Duncan <pabs@pablotron.org>                                   #
# http://www.pablotron.org/                                             #
#                                                                       #
# Usage:                                                                #
#   # tail -f .procmail/log | proccolor.pl                              #
#      or                                                               #
#   # proccolor.pl .procmail/log                                        #
#                                                                       #
#   You may also want to edit the %colors hash below.                   #
#########################################################################

use strict;
no strict 'subs';

# if you want to change the color settings, you should really modify the
# %colors hash below, not this escape sequence hash
my %base_colors = (
  'black'         => "\033[0;30m", 
  'blue'          => "\033[0;34m", 
  'green'         => "\033[0;32m", 
  'cyan'          => "\033[0;36m", 
  'red'           => "\033[0;31m", 
  'purple'        => "\033[0;35m", 
  'brown'         => "\033[0;33m", 
  'light_grey'    => "\033[0;37m", 
  'dark_grey'     => "\033[1;30m", 
  'light_blue'    => "\033[1;34m", 
  'light_green'   => "\033[1;32m", 
  'light_cyan'    => "\033[1;36m", 
  'light_red'     => "\033[1;31m", 
  'light_purple'  => "\033[1;35m", 
  'yellow'        => "\033[1;33m", 
  'white'         => "\033[1;37m", 
  'nothing'       => "\033[0m", 
);

#################
# USER SETTINGS #
#################
my $spam_folder = "SPAM"; # name (case-sensitive) of spam folder
my %colors = (
  'base'        => $base_colors{'nothing'},     # default color
  'error'       => $base_colors{'light_red'},   # error color
  'hdr_from'    => $base_colors{'light_grey'},  # From header
  'email'       => $base_colors{'cyan'},        # source email
  'date'        => $base_colors{'light_green'}, # arrival date
  'hdr_subject' => $base_colors{'light_grey'},  # Subject header
  'subject'     => $base_colors{'light_cyan'},  # subject
  'hdr_folder'  => $base_colors{'light_grey'},  # Folder header
  'folder'      => $base_colors{'white'},       # folder
  'folder_num'  => $base_colors{'light_blue'},  # folder number
  'folder_spam' => $base_colors{'yellow'},      # SPAM folder
);

# determine input method
my $in = *STDIN;
open $in, "tail -f --follow=name $ARGV[0] |" if (@ARGV);
die "Couldn't open input pipe: $!\n" unless $in;

# loop forever
while (1) {
  my $line = <$in>;
  # print "DEBUG: $line";

  next unless $line;
  
  if ($line =~ /^From/) {
    $line =~ s/From (.+?@[^ ]+)\s+(.*)$/$colors{'hdr_from'}From$colors{'base'} $colors{'email'}$1$colors{'base'} $colors{'date'}$2$colors{'base'}/;
  } elsif ($line =~ /^\s*Subject:/) {
    $line =~ s/Subject: (.+)$/$colors{'hdr_subject'}Subject:$colors{'base'} $colors{'subject'}$1$colors{'base'}/;
  } elsif ($line =~ /^\s*Folder:/) {
    if ($line =~ /\b$spam_folder\b/) {
      $line =~ s/Folder: (.+)(\s+)(\d+)$/$colors{'hdr_folder'}Folder:$colors{'base'} $colors{'folder_spam'}$1$colors{'base'}$2$colors{'folder_num'}$3$colors{'base'}/;
    } else {
      $line =~ s/Folder: (.+)(\s+)(\d+)$/$colors{'hdr_folder'}Folder:$colors{'base'} $colors{'folder'}$1$colors{'base'}$2$colors{'folder_num'}$3$colors{'base'}/;
    }
  } elsif ($line =~ /^procmail:/) {
    $line =~ s/^(.*)$/$colors{'error'}$1$colors{'base'}/;
  } else {
    # don't do shit with the line
  }
  
  print "$line";
}

