#!/usr/bin/perl
#
#  Check and print POP3 mail.
#
###################

$popuser = "test";
$poppass = "test";
$server = "irev.net";

###################

if ($ARGV[0]) { $debug = 1; }
my $port = 110;
my $AF_INET = 2;
my $SOCK_STREAM = 1;
my (@hostent,$result);
my $sockaddr = 'S n a4 x8';
@hostent = gethostbyname($server);
my $that = pack($sockaddr, $AF_INET, $port, $hostent[4]);
if (socket(POP, $AF_INET, $SOCK_STREAM, 0)) {
	if (connect(POP, $that)) {
		print "+ success: Connect\n" if ($debug);
		select(POP); $| = 1; select(STDOUT); $|=1;
		print POP "user $popuser\n";
		print POP "pass $poppass\n";
		until (<POP> =~ /\n/) {
			print $_ if ($debug);
		}
		print POP "list\n";
		my $stop = 0;
		while ($stop == 0) {
			$line = <POP>;
			print "$line" if ($debug);
			$line =~ s/\n//g;
			$line =~ s/\r//g;
			if ($line =~ /^\d+/) {
				$emailcount = push(@emails,$line);
			}
			if ($line =~ /\./) {
				$stop = 1;
			}
		}
		print "+ total emails: $emailcount\n" if ($debug);
		foreach $email (@emails) {
			$email =~ /(\d+)/;
			print "+ getting $1\n" if ($debug);
			print POP "retr $1\n";
			$stop = 0;
			while ($stop == 0) {
				$eline = <POP>;
				print $eline;
				if ($eline =~ /^\./) {
					$stop = 1;
				}
			}
		}
		print " + $emailcount emails total displayed\n";
	}
}

