summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Ankarström <john@ankarstrom.se>2021-05-26 01:33:06 +0200
committerJohn Ankarström <john@ankarstrom.se>2021-05-26 01:34:47 +0200
commitca98b2ba8799b40ca0fe5b38b34c817a0215b119 (patch)
treee4ee153b32bd245939b9488a9aad8bcc87282ebb
parent61205f34eccdaf65d639cf15f7599ebe4830d5f9 (diff)
downloadmum-ca98b2ba8799b40ca0fe5b38b34c817a0215b119.tar.gz
Improve 'm-pop' script
Username and password have been moved to environment variables. Command-line interface has improved.
-rwxr-xr-xsrc/m-filter (renamed from src/fh)4
-rwxr-xr-xsrc/m-pop (renamed from src/pop)50
2 files changed, 29 insertions, 25 deletions
diff --git a/src/fh b/src/m-filter
index 74c84a4..756f8b6 100755
--- a/src/fh
+++ b/src/m-filter
@@ -1,6 +1,8 @@
#!/usr/bin/perl -p
-# fh -- filter headers
+# m-filter -- filter headers
+
+# Edit this script to your own preferences.
BEGIN {
sub header { $_ =~ shift .. not $next =~ /^[ \t]/ }
diff --git a/src/pop b/src/m-pop
index dbdd8c6..1a0e0ff 100755
--- a/src/pop
+++ b/src/m-pop
@@ -1,28 +1,36 @@
#!/usr/bin/perl
-# pop -- retrieve (new) messages via POP3
+# m-pop -- retrieve (new) messages via POP3
use strict;
use warnings;
+use Getopt::Std;
use Net::POP3;
use POSIX;
-use Term::ReadKey;
use Sys::Hostname;
-# Flush STDERR
-select STDERR; $|++; select STDOUT;
+our $VERSION = '0.01';
# Process arguments
-if (@ARGV != 2) {
- print STDERR "usage: $0 mbox mbox.i\n";
+my %opt;
+$Getopt::Std::STANDARD_HELP_VERSION = 1;
+getopts('m:i:j:', \%opt);
+HELP_MESSAGE() unless $opt{m} and $opt{i};
+sub HELP_MESSAGE {
+ print STDERR <<USAGE;
+usage: $0 -m mbox -i mbox.i [-j mbox.i.out]
+USAGE
exit 1;
}
+# Open TTY for reading and writing
+open my $tty, '+<:unix', '/dev/tty' or die "Could not open /dev/tty: $!";
+
# Get UIDs of existing messages
my ($mbox, $index, @existing_uids);
-if (-e $ARGV[1]) {
- open $index, '<', $ARGV[1] or die "Could not open $ARGV[1]: $!";
+if (-e $opt{i}) {
+ open $index, '<', $opt{i} or die "Could not open $opt{i}: $!";
local $/ = ''; # paragraph mode
while (<$index>) {
push @existing_uids, $1 if /^M-UID: (.*)$/m;
@@ -31,28 +39,22 @@ if (-e $ARGV[1]) {
}
# Open mbox and index files for appending
-open $mbox, '>>', $ARGV[0] or die "Could not open $ARGV[0]: $!";
-open $index, '>>', $ARGV[1] or die "Could not open $ARGV[1]: $!";
+open $mbox, '>>', $opt{m} or die "Could not open $opt{m}: $!";
+$opt{j} = $opt{i} if not $opt{j};
+open $index, '>>', $opt{j} or die "Could not open $opt{j}: $!";
# Get POP3 server, username and password
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;
-}
+
+die "USERNAME variable not set\n" if not $ENV{USERNAME};
+die "PASSWORD variable not set\n" if not $ENV{PASSWORD};
# Connect to POP3 server
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";
+$pop->login($ENV{USERNAME}, $ENV{PASSWORD}) or die "Could not log into server\n";
# Retrieve ids and uids of all messages
my @ids = sort { $a <=> $b } keys %{$pop->list};
@@ -61,7 +63,7 @@ my %uids = %{$pop->uidl};
# Handle SIGINT
my $sigint = 0;
$SIG{INT} = sub {
- print STDERR "\nSafely ending retrieval...\n";
+ print $tty "\nSafely ending retrieval...\n";
$sigint = 1;
};
@@ -72,7 +74,7 @@ my $i = 0;
for my $id (@ids) {
next if grep { $_ eq $uids{$id} } @existing_uids;
- print STDERR "\r$id/$ids[-1]";
+ print $tty "\r$id/$ids[-1]";
my @msg;
{
@@ -123,7 +125,7 @@ INDEX
# Set offset for next message
$offset += $message_length + 1;
}
-print STDERR "\n";
+print $tty "\n";
close $index;
close $mbox;