#!/usr/bin/perl
use Net::POP3;

# Let's take the target POP server's name!
print "POP server[192.168.1.1]: ";
chop($popserver = <STDIN>);
if ($popserver eq "")
{
   # taking the default hosename!
   $popserver = "192.168.1.1";
}

# Ok, let's take the username! Default is current username
$default_username = getpwuid $<;
print "username at $popserver [$default_username]: ";
chop($username = <STDIN>);
if ($username eq "")
{
   # Ok, taking the default user ID
   $username = $default_username;
}

# Ok, now let's take the passwd!
# getting the password by turning the echo off
system "stty -echo";
print "Enter password for $username\@$popserver: ";
chop($password = <STDIN>);
print "\n";
system "stty echo";


# Shows popserver and username
print "Ok, now trying to get connected to $popserver ....\n";

# Let's call the constructor with one value!
$remoteserver = new Net::POP3($popserver);
if (! $remoteserver)  # If the connection was a failure
{
   # Let's be gentle! Not required!
   print "Sorry, dude. Couldn't connect to $popserver...\n";
   print "Check whether your popserver($popserver) is alive or not.\n";
   print "\n";
   print "        ping $popserver       \n";
   print "\n";
   print "will be a nice try.\n";
   exit 1;
}
else
{
   print "Connected successfully to $popserver\n";
}

print "Now trying to login...\n";
$total_message = $remoteserver->login($username, $password);
print "You got $total_message mail(s) in $popserver\n";
&showheaders;
while(1 == 1) # An infinite loop!
{
   # Ok, Let's view message bodies also!
   print " View   message ---> v[msg_no]\n";
   print " Delete message ---> d[msg_no]\n";
   print " Show all  hdrs ---> s        \n";
   print " Quit           ---> q/Q      \n\n";
   print " COMMAND> ";
   chop($command = <STDIN>);
   if ($command =~ /^v+[0-9]+$/)
   {
      $message_no = substr $command, 1;
      &viewmessage($message_no);
   }
   if ($command =~ /^d+[0-9]+$/)
   {
      print "have to delete $command\n";
      $message_no = substr $command, 1;
      &deletemessage($message_no);
   }
   if ($command =~ /^[qQ]$/)
   {
      print "Do you want to update your remote mbox?\[y/n\] ";
      chop($dummy = <STDIN>);
      if ($dummy =~ /^y$/ || $dummy eq "")
      {
         last;
      }
      else
      {
         $remoteserver->reset();
	 last;
      }
   }
   if ($command =~ /^s$/)
   {
      &showheaders;
   }
}

# Ok, let's close the connection according to the rule!
print "Closing the connection from $popserver now...\n";
$remoteserver->quit();

exit 0;

# A subroutine to show headers.....
# using global variables :(
sub showheaders
{
   # Now let's see how many mails are there in $remoteserver
   if ($total_message eq "")
   {
      print "Sorry Bro! Login failed! Try another session\n";
   }
   elsif ($total_message > 0)
   {
      ######################################################
      # Header grepping goes here                          
      ######################################################
      #print "Let's get the headers
      $heads_size = $remoteserver->list();

      # Since a reference to a hash is returned, we
      # convert it back to normal hash
      %heads_size = %$heads_size;

      # Now let's print message number and head sizes
      foreach $msg_no (keys %heads_size)
      {
         # Let's print the message_number first!
         print " $msg_no ";

         # Let's see the headers summary! 
         $head_array = $remoteserver->top($msg_no);

         # Since a ref to an array is returned, we
         # covert it back to an array
         @head_array = @$head_array;

         foreach $line (@head_array)
         {
            chop $line;

	    # Let's get the From header
	    if ($line =~ /^From: /)
	    {
	       $line =~ s/^From: //;
               print " $line";
            }

            # Let's get the Subject header
	    if ($line =~ /^Subject: /)
	    {
	       $line =~ s/^Subject: //;
	       print " $line";
	    }
         }
         print "\n";
         #print " $heads_size{$msg_no} octets\n";
      }
   }
   else
   {
      print "Sorry dude, you don't have any message in $popserver!\n";
   }
}

# A subroutine to view message from the remote mbox
# usage: &viewmessage($msg_no);
sub viewmessage
{
   my($msg_no) = ($_[0]);
   # Now let's see how many mails are there in $remoteserver
   if ($total_message < $msg_no)
   {
      print "Wrong message number: $msg_no\n";
   }
   elsif ($total_message >= $msg_no  && $total_message > 0)
   {
      print "Message No: $msg_no\n";
      ######################################################
      # Let's get a whole message!                          
      ######################################################
      $allmsg = $remoteserver->get($msg_no);

      $i = 0;
      foreach $line (@$allmsg)
      {
         print "$line";

	 # Pause it after each 20 lines?
	 if ($i++ > 20)
	 {
	    chop($dummy = <STDIN>);
	    $i = 0;
	 }
      }
      return;
   }
}

# A subroutine to view message from the remote mbox
# usage: &viewmessage($msg_no);
sub deletemessage
{
   my($msg_no) = ($_[0]);
   # Now let's see how many mails are there in $remoteserver
   if ($total_message < ($msg_no))
   {
      print "Wrong message number: $msg_no\n";
   }
   elsif ($total_message >= $msg_no && $total_message >= 0)
   {
      print "Deleting Message: $msg_no\[y/n\] ";
      chop($dummy = <STDIN>);
      if ($dummy =~ /^y$/)
      {
         $remoteserver->delete($msg_no);
         print "Mark for deletion is set for message: $msg_no\n";
      }
   }
}
