summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Ankarström <john@ankarstrom.se>2021-05-19 12:15:31 +0200
committerJohn Ankarström <john@ankarstrom.se>2021-05-19 12:15:31 +0200
commitcdf4df2126bb0fe5c33bcb77992f3877da290c17 (patch)
tree08fdbeb90a0b3702c80414b5ba71fbe153dd1f1a
parent6481d4daecab58c5a94acb89adc5f883f10805fb (diff)
downloadmum-cdf4df2126bb0fe5c33bcb77992f3877da290c17.tar.gz
Improve SIGINT handling
It should only be active for the main loop.
-rwxr-xr-xsrc/pop39
1 files changed, 24 insertions, 15 deletions
diff --git a/src/pop b/src/pop
index 826a975..3134c14 100755
--- a/src/pop
+++ b/src/pop
@@ -10,10 +10,6 @@ use POSIX;
use Term::ReadKey;
use Sys::Hostname;
-# Handle SIGINT
-my $sigint = 0;
-$SIG{INT} = sub { $sigint = 1 };
-
# Flush STDERR
select STDERR; $|++; select STDOUT;
@@ -41,20 +37,36 @@ open $mbox, '>>', $ARGV[0] or die "Could not open $ARGV[0]: $!";
open $index, '>>', $ARGV[1] or die "Could not open $ARGV[1]: $!";
# Get POP3 server, username and password
-print 'Enter password: ';
-ReadMode 'noecho';
-chomp(my $password = ReadLine(0));
-print "\n";
-ReadMode 0;
+my $server = 'pop3.mailbox.org';
+my $ssl = 1;
+my $timeout = 5;
+my $username = 'john@ankarstrom.se';
+my $password;
+
+if (not $password) {
+ print 'Enter password: ';
+ ReadMode 'noecho';
+ chomp($password = ReadLine(0));
+ print "\n";
+ ReadMode 0;
+}
# Connect to POP3 server
-my $pop = Net::POP3->new('pop3.mailbox.org', SSL => 1, Timeout => 5);
-$pop->login('john@ankarstrom.se', $password) or die "$!";
+my $pop = Net::POP3->new($server, SSL => $ssl, Timeout => $timeout)
+ or die "Could not connect to POP3 server $server: $!\n";
+$pop->login($username, $password) or die "Could not log into server\n";
# Retrieve ids and uids of all messages
my @ids = sort { $a <=> $b } keys %{$pop->list};
my %uids = %{$pop->uidl};
+# Handle SIGINT
+my $sigint = 0;
+$SIG{INT} = sub {
+ print STDERR "\nSafely ending retrieval...\n";
+ $sigint = 1;
+};
+
# Append new messages to mbox and index files
chomp(my $date = asctime(localtime(time)));
my $offset = 0;
@@ -119,10 +131,7 @@ Offset: $offset
INDEX
- if ($sigint) {
- print STDERR "\n";
- exit 130;
- }
+ exit 130 if $sigint;
# Set offset for next message
$offset += $content_length + 1;