#!/usr/bin/perl use strict; use warnings; use IO::File; use Net::POP3; use Data::Dumper; use Carp; { my ( $hostname, $username, $password, $port, $ssl ) = parse_config(); my $pop = Net::POP3->new( $hostname, Timeout => 60, Debug => 1 ); print "connected to server $hostname.\n"; if ($ssl) { if ( $pop->apop( $username, $password ) <= 0 ) { carp "Cannot login with SSL! $!"; } } else { if ( $pop->login( $username, $password ) <= 0 ) { carp "Cannot login! $!"; } } print "logged in\n"; #print Dumper $pop->capa(); my $msgnums = $pop->list; foreach my $msgnum ( keys %$msgnums ) { my $msg = $pop->get($msgnum); save_message($msg); last; } $pop->quit(); } # todo sub message_exists { my $msgno = shift; } sub save_message { my $msg = shift; my $spool = $ENV{HOME} . "/spool"; my $fh = new IO::File $spool, "w"; if ( defined $fh ) { print $fh join( "", @$msg ); $fh->close(); } } sub parse_config { my $config = $ENV{HOME} . "/.delivermailrc"; carp "~/.delivermailrc $!" unless -f $config; my ( $hostname, $username, $password, $port, $ssl ); my $fh = new IO::File $config, "r"; if ( defined $fh ) { while (<$fh>) { next if m|^\#|; s|\s||g; next unless $_; my ( $label, $value ) = split( "=", $_ ); # remove quotes if any if ( $value =~ m/^(\"|\')/ ) { my $quote = $1; $value =~ s/$quote//g; } if ( lc($label) eq "username" ) { $username = $value; next; } if ( lc($label) eq "password" ) { $password = $value; next; } if ( lc($label) eq "hostname" ) { $hostname = $value; next; } if ( lc($label) eq "port" ) { $port = $value; next; } if ( lc($label) eq "ssl" ) { $ssl = lc($value) eq "on" ? 1 : 0; next; } } $fh->close; return ( $hostname, $username, $password, $port, $ssl ); } }