From ca98b2ba8799b40ca0fe5b38b34c817a0215b119 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Wed, 26 May 2021 01:33:06 +0200 Subject: Improve 'm-pop' script Username and password have been moved to environment variables. Command-line interface has improved. --- src/fh | 23 ----------- src/m-filter | 25 +++++++++++ src/m-pop | 132 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/pop | 130 ---------------------------------------------------------- 4 files changed, 157 insertions(+), 153 deletions(-) delete mode 100755 src/fh create mode 100755 src/m-filter create mode 100755 src/m-pop delete mode 100755 src/pop diff --git a/src/fh b/src/fh deleted file mode 100755 index 74c84a4..0000000 --- a/src/fh +++ /dev/null @@ -1,23 +0,0 @@ -#!/usr/bin/perl -p - -# fh -- filter headers - -BEGIN { - sub header { $_ =~ shift .. not $next =~ /^[ \t]/ } - sub skip { $_ = '' } -} - -($_, $next) = ($next, $_); -skip if header qr/^X-(?!( - Mailer - | Rspamd-\S+ -):)/xi; -skip if header qr/^( - Received - | Received-SPF - | (DKIM|DomainKey)-Signature - | ARC-\S+ - | IronPort-\S+ -):/x; - -END { print $next } diff --git a/src/m-filter b/src/m-filter new file mode 100755 index 0000000..756f8b6 --- /dev/null +++ b/src/m-filter @@ -0,0 +1,25 @@ +#!/usr/bin/perl -p + +# m-filter -- filter headers + +# Edit this script to your own preferences. + +BEGIN { + sub header { $_ =~ shift .. not $next =~ /^[ \t]/ } + sub skip { $_ = '' } +} + +($_, $next) = ($next, $_); +skip if header qr/^X-(?!( + Mailer + | Rspamd-\S+ +):)/xi; +skip if header qr/^( + Received + | Received-SPF + | (DKIM|DomainKey)-Signature + | ARC-\S+ + | IronPort-\S+ +):/x; + +END { print $next } diff --git a/src/m-pop b/src/m-pop new file mode 100755 index 0000000..1a0e0ff --- /dev/null +++ b/src/m-pop @@ -0,0 +1,132 @@ +#!/usr/bin/perl + +# m-pop -- retrieve (new) messages via POP3 + +use strict; +use warnings; + +use Getopt::Std; +use Net::POP3; +use POSIX; +use Sys::Hostname; + +our $VERSION = '0.01'; + +# Process arguments +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 <) { + push @existing_uids, $1 if /^M-UID: (.*)$/m; + } + close $index; +} + +# Open mbox and index files for appending +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; + +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($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}; +my %uids = %{$pop->uidl}; + +# Handle SIGINT +my $sigint = 0; +$SIG{INT} = sub { + print $tty "\nSafely ending retrieval...\n"; + $sigint = 1; +}; + +# Append new messages to mbox and index files +chomp(my $date = asctime(localtime(time))); +my $offset = 0; +my $i = 0; + +for my $id (@ids) { + next if grep { $_ eq $uids{$id} } @existing_uids; + print $tty "\r$id/$ids[-1]"; + + my @msg; + { + local $/ = "\r\n"; + chomp(@msg = @{$pop->get($id)}); + } + + # Make From_ line + my ($from, $from_alt); + my $j = 0; # index of empty line before message body + for (@msg) { + last if /^$/; + $j++; + $from = $1 if /^From: (.*)$/; + $from_alt = $1 if /^from: (.*)$/; + } + $from = $from_alt if $from_alt and not $from; + $from = $1 if $from and $from =~ /^.* <(.*?@.*?)>$/; + $from = 'MAILER-DAEMON@' . hostname if not $from; + my $from_ = "From $from $date"; + + # Calculate message length + my ($head_length, $body_length, $message_length); + $head_length += length($_)+1 for (@msg[0..$j-1]); + $body_length += length($_)+1 for (@msg[$j..$#msg]); + $message_length = length($from_) + 1 + $head_length + $body_length; + + # Append message to mbox and index files + local $" = "\n"; + print $mbox <quit; diff --git a/src/pop b/src/pop deleted file mode 100755 index dbdd8c6..0000000 --- a/src/pop +++ /dev/null @@ -1,130 +0,0 @@ -#!/usr/bin/perl - -# pop -- retrieve (new) messages via POP3 - -use strict; -use warnings; - -use Net::POP3; -use POSIX; -use Term::ReadKey; -use Sys::Hostname; - -# Flush STDERR -select STDERR; $|++; select STDOUT; - -# Process arguments -if (@ARGV != 2) { - print STDERR "usage: $0 mbox mbox.i\n"; - exit 1; -} - -# 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]: $!"; - local $/ = ''; # paragraph mode - while (<$index>) { - push @existing_uids, $1 if /^M-UID: (.*)$/m; - } - close $index; -} - -# 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]: $!"; - -# 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; -} - -# 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"; - -# 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; -my $i = 0; - -for my $id (@ids) { - next if grep { $_ eq $uids{$id} } @existing_uids; - print STDERR "\r$id/$ids[-1]"; - - my @msg; - { - local $/ = "\r\n"; - chomp(@msg = @{$pop->get($id)}); - } - - # Make From_ line - my ($from, $from_alt); - my $j = 0; # index of empty line before message body - for (@msg) { - last if /^$/; - $j++; - $from = $1 if /^From: (.*)$/; - $from_alt = $1 if /^from: (.*)$/; - } - $from = $from_alt if $from_alt and not $from; - $from = $1 if $from and $from =~ /^.* <(.*?@.*?)>$/; - $from = 'MAILER-DAEMON@' . hostname if not $from; - my $from_ = "From $from $date"; - - # Calculate message length - my ($head_length, $body_length, $message_length); - $head_length += length($_)+1 for (@msg[0..$j-1]); - $body_length += length($_)+1 for (@msg[$j..$#msg]); - $message_length = length($from_) + 1 + $head_length + $body_length; - - # Append message to mbox and index files - local $" = "\n"; - print $mbox <quit; -- cgit v1.2.3