#!/usr/bin/perl -T
#----------------------------------------------------------------------
-# Pull mails from an IMAP mailbox and deliver them to an SMTP session
+# Pull mails from an IMAP mailbox and deliver them to a SMTP session
# Copyright © 2016 Guilhem Moulin <guilhem@fripost.org>
#
# This program is free software: you can redistribute it and/or modify
, [$ARGV[0]]
, statefile => qr/\A(\P{Control}+)\z/
, mailbox => qr/\A([\x01-\x7F]+)\z/
- , 'deliver-method' => qr/\A((?:[ls]mtp:)?\[.*\](?::\d+)?)\z/
+ , 'deliver-method' => qr/\A([ls]mtp:\[.*\]:\d+)\z/
, 'deliver-ehlo' => qr/\A(\P{Control}+)\z/
, 'deliver-rcpt' => qr/\A(\P{Control}+)\z/
- , 'purge-after' => qr/\A(\d+)d\z/
+ , 'purge-after' => qr/\A(\d+)\z/
)->{$ARGV[0]};
my ($MAILBOX, $STATE);
unless (defined $SMTP) {
# TODO we need to be able to reconnect when the server closes
# the connection due to a timeout (RFC 5321 section 4.5.3.2)
- my ($fam, $addr, $port) = (PF_INET, $CONF->{'deliver-method'} // 'smtp:[127.0.0.1]:10024', 25);
+ my ($fam, $addr) = (PF_INET, $CONF->{'deliver-method'} // 'smtp:[127.0.0.1]:25');
$addr =~ s/^([ls]mtp):// or die;
my $ehlo = $1 eq 'lmtp' ? 'LHO' : $1 eq 'smtp' ? 'EHLO' : die;
$ehlo .= ' '. ($CONF->{'deliver-ehlo'} // 'localhost.localdomain');
- $port = $1 if $addr =~ s/:(\d+)$//;
+ my $port = $addr =~ s/:(\d+)$// ? $1 : die;
$addr =~ s/^\[(.*)\]$/$1/ or die;
$fam = PF_INET6 if $addr =~ /:/;
$addr = Socket::inet_pton($fam, $addr) // die "Invalid address $addr\n";
my $LAST_PURGED;
sub purge() {
my $days = $CONF->{'purge-after'} // return;
- $days =~ s/d$//;
my ($uidnext) = $IMAP->get_cache('UIDNEXT');
return unless 1<$uidnext;
my $set = "1:".($uidnext-1);
pull($ignore);
purge();
};
-exit 0 unless defined $CONFIG{idle};
+unless (defined $CONFIG{idle}) {
+ $IMAP->logout();
+ exit 0;
+}
$CONFIG{idle} = 1740 if defined $CONFIG{idle} and $CONFIG{idle} == 0; # 29 mins
while(1) {