#!/usr/bin/perl
#edit these variables for your needs
#which matching header-lines to show
@headpattern=("From:","To:","Subject:","CC:");
#no. of body-lines to show
$bodylines=10;
#do not edit below this line
####################################################################
# (c) 2001 by Christoph Baumann <cgb@debian.org>                   #
# The GNU Genral Public license applies to this program            #
# Thanks to Jorrit "J" Fahlke for patching the config file parser #
####################################################################


print "pop3browser 0.1 -- (c) 2001 by Christoph Baumann\n";
print "There is absolutely no warranty for this program!\n";
print "The GNU General Public License applies to this program.\n";
print "(see http://www.gnu.org)\n";
print "Enter 'help' for a command summary\n";





#read config data
if (-e "$ENV{\"HOME\"}/.pop3browserrc")
{
	 ($mode)=(stat("$ENV{\"HOME\"}/.pop3browserrc"))[2];
	 if ($mode != 0100600)
	 {
		  print "The configuration file was readable for other users than the file owner.\n";
		  print "Correcting it...\n";
		  #this file should only be read by the owner
		  chmod(0600,"$ENV{\"HOME\"}/.pop3browserrc") 
			or die "FATAL: could't chmod the configfile. Do you own it?";
	 }
	 open(CONFIG,"$ENV{\"HOME\"}/.pop3browserrc")
		or die "FATAL: could't open the configfile. Do you own it?";
	 $i=0;
	 
	 while (<CONFIG>)
	 {
		  chomp();
		  s/^\s+//;
		  next if /^\#/;
		  next if /^$/;
		  ($host[$i]{"host"},$host[$i]{"uid"},$host[$i]{"passwd"})=split(/\s+/, $_, 3);
		  $i+=1;	
	 }
	 $nhosts=$i;
	 close(CONFIG);
	 if ($nhosts==0)
	 {
		  print "No host definitions found in configuration file!\n";
		  exit 0;
	 }
}
else
{
	 print "No config file found!\n";
	 print "pop3browser needs the file .pop3browserrc in your home directory to work.\n";
	 print ".pop3browserrc contains the definitions for your pop3 accounts.\n";
	 print "One definition per line and in the following form:\n";
	 print "hostname userid password\n";
	 print "The entries in one line may be separated by spaces or tabs,\n";
	 print "lines starting with \# are ignored.\n";
	 exit 0;
}



use Net::POP3;

#init some vars
$connected=0;
$number=0;
$nopatterns=@headpattern;
$cmd=" ";

while (!($cmd =~ /quit/)) 
{
	 print "pop3browser>";
	 $cmd=<STDIN>;

	 if ($cmd =~ /login/)
	 {
		  if ($connected > 0)
		  {
				$folder->quit();
				print "Closing current connection...\n";
				for ($i=1;$i<=$number;$i+=1)
				{
					 $tags[$i]="";
				}
				$number=0;
				$connected=0;
		  }
		  chop($cmd);
		  @hurz=split(/\s+/,$cmd);
		  $hostno=$hurz[1];
		  $folder = Net::POP3->new($host[$hostno]{"host"});		  
		  $number=$folder->login($host[$hostno]{"uid"},$host[$hostno]{"passwd"});
		  
		  if ($number eq "0E0")
		  {
				$number=0;
		  }

		  if ($number eq "")
		  {
				print "Authentication failed\n";
		  }
		  else
		  {
				print $number," mails on ",$host[$hostno]{"host"}," for ",$host[$hostno]{"uid"},"\n";
				$connected=1;
				$list=$folder->list();
		  }
	 }

	 if ($cmd =~ /apop/)
	 {
		  if ($connected > 0)
		  {
				$folder->quit();
				print "Closing current connection...\n";
				@tags=();
				$number=0;
				$connected=0;
		  }
		  chop($cmd);
		  @hurz=split(/\s+/,$cmd);
		  $hostno=$hurz[1];
		  $folder = Net::POP3->new($host[$hostno]{"host"});		  
		  $number=$folder->apop($host[$hostno]{"uid"},$host[$hostno]{"passwd"});
		  
		  if ($number eq "")
		  {
				print "Authentication failed\n";
		  }
		  else
		  {
				print $number," mails on ",$host[$hostno]{"host"}," for ",$host[$hostno]{"uid"},"\n";
				$connected=1;
				$list=$folder->list();
		  }
	 }
	 
	 if ($cmd =~ /close/)
	 {
		  if ($connected > 0)
		  {
				$folder->quit();
				for ($i=1;$i<=$number;$i+=1)
				{
					 $tags[$i]="";
				}
				$number=0;
				$connected=0;
				print "Connection closed\n";
		  }
		  else
		  {
				print "No open connection...\n";
		  }
	 }

	 if ($cmd =~ /hosts/)
	 {
		  for ($i=0;$i<$nhosts;$i+=1)
		  {
				print $i,"\t",$host[$i]{"uid"},"@",$host[$i]{"host"},"\n";
		  }
	 }


	 if (($cmd =~ /list/)&&($connected > 0)) 
	 {
		  for ($key=1;$key<=$number;$key+=1) 
		  {
				print "msg no: $key \t size: $list->{$key} \t tags: $tags[$key]\n";
		  }  
	 }
	 
	 if (($cmd =~ /kill/)&&($connected > 0))
	 {
		  chop($cmd);
		  @hurz=split(/\s+/,$cmd);
		  $match=$hurz[1];
		  for ($key=1;$key<=$number;$key+=1) 
		  {
				print "Processing mail no. $key\n";
				$header=$folder->top($key,0);
				$lines=@$header;
				for ($i=0;$i<$lines;$i+=1)
				{
					 if ($header->[$i] =~ /$match/)
					 {
						  if ($tags[$key]!~ /d/)
						  {
								$tags[$key]="$tags[$key]d";
								print "number: $key matched!\n";
								$folder->delete($key);
						  }
					 }
				}
		  }  
	 }
	 
	 if (($cmd =~ /show/)&&($connected > 0))
	 {
		  chop($cmd);
		  @hurz=split(/\s+/,$cmd);
		  $key=$hurz[1];
		  $header=$folder->top($key,$bodylines);
		  $lines=@$header;
		  $body=0;
		  if ($tags[$key]!~ /s/)
		  {
				$tags[$key]="$tags[$key]s";
		  }
		  for ($i=0;$i<$lines;$i+=1)
		  {
				#body starts with an empty line
				if ($header->[$i] =~ /^\n/)
				{
					 $body=1;
				}

				#show only lines matching @headpatterns
				if ($body != 1)
				{
					 for ($j=0;$j<$nopatterns;$j+=1)
					 {
						  if ($header->[$i] =~ /^$headpattern[$j]/)
						  {
								print $header->[$i]; 
						  }
					 }
				} 
				else
				{
					 print $header->[$i];
				}
		  }
	 }

	 if (($cmd =~ /delete/)&&($connected > 0))
	 {
		  chop($cmd);
		  @hurz=split(/\s+/,$cmd);
		  $key=$hurz[1];
		  print "marking message no. $key for deletion\n";
		  $folder->delete($key);
		  if ($tags[$key]!~ /d/)
		  {
				$tags[$key]="$tags[$key]d";
		  }
	 }
	 
	 if ($cmd =~ /help/)
	 {
		  print "Supported commands:\n";
		  print "login n   : log into host no. n, use 'hosts' to get a list of available hosts\n";
		  print "apop n    : same as login but uses APOP\n"; 
		  print "close     : close current connection\n";
		  print "hosts     : list available hosts\n";
		  print "list      : list sizes and message numbers of the mails on the account\n";
		  print "show n    : show header and some body lines of mail number n\n";
		  print "delete n  : delete mail number n\n";
		  print "kill expr : delete mails matching expr in header\n";
		  print "quit      : quit program\n";
		  print "help      : print this help\n";
	 }
}

#close still open connections
if ($connected > 0)
{
	 $folder->quit();
	 print "Connection closed\n";
}
