#!/usr/bin/env perl
use 5.20.0;
use strict;
use warnings;
use Mail::Milter::Authentication::Pragmas;
# ABSTRACT: A Perl Mail Authentication Milter client
# PODNAME: authentication_milter_client
our $VERSION = '3.20241011'; # VERSION
use Email::Simple;
use Getopt::Long;
use Pod::Usage;
use Mail::Milter::Authentication::Client;
use Mail::Milter::Authentication::Config;

# CONFIG
my $help         = 0;
my $mailer_name  = 'test.mta.example.com';
my $connect_ip   = '127.0.0.1';
my $connect_name = 'localhost.localdomain';
my $connect_port = '54321';
my $connect_type = 'tcp4';
my $helo_host    = 'localhost.example.com';
my $mail_from    = 'test_user@localhost.example.com';
my $rcpt_to      = 'test_user@mta.example.com';
my $mail_file    = q{};
my $prefix;

GetOptions (
    'mailer_name=s'  => \$mailer_name,
    'connect_ip=s'   => \$connect_ip,
    'connect_name=s' => \$connect_name,
    'connect_port=s' => \$connect_port,
    'connect_type=s' => \$connect_type,
    'helo_host=s'    => \$helo_host,
    'mail_from=s'    => \$mail_from,
    'rcpt_to=s'      => \$rcpt_to,
    'mail_file=s'    => \$mail_file,
    "help"           => \$help,
    "prefix=s"       => \$prefix,
) or die "Error in command line arguments\n";

if ( $help ) {
    usage();
    exit 0;
}

if ( $prefix ) {
    $Mail::Milter::Authentication::Config::PREFIX = $prefix;
}

sub usage {
    pod2usage( -verbose => 2 );
}

my $args = {
    'mailer_name'   => $mailer_name,
    'connect_ip'    => $connect_ip,
    'connect_name'  => $connect_name,
    'connect_port'  => $connect_port,
    'connect_type'  => $connect_type,
    'helo_host'     => $helo_host,
    'mail_from'     => $mail_from,
    'rcpt_to'       => $rcpt_to,
};

die "Please supply a mail file" if ! $mail_file;

if ( $mail_file eq '-' ) {
    my $mail_data = q{};
    while ( my $l = <> ) {
        $mail_data .= $l;
    }
    $args->{'mail_data'} = $mail_data;
}
else {
    if ( ! -e $mail_file ) {
        die "Mail file $mail_file does not exist";
    }
    $args->{'mail_file'} = $mail_file;
}

my $client = Mail::Milter::Authentication::Client->new( $args );

$client->process();

print $client->result();

__END__

=pod

=encoding UTF-8

=head1 NAME

authentication_milter_client - A Perl Mail Authentication Milter client

=head1 VERSION

version 3.20241011

=head1 USAGE

  authentication_milter_client [--mailer_name <name>] \
    [--connect_ip <ip>] [--connect_name <name] [--connect_port <port>] [--connect_type <type>] \
    [--helo_host <host>] [--mail_from <from>] [--rcpt_to <rcpt_to>] \
    [--mail_file <filename>] \
    [--prefix <dir>] \
    [-h|--help]

=head1 OPTIONS

=over

=item -h|--help

  Show this help.

=item --mail_file <file>

  Filename of file containing the email contents.
  If the filename is set to - then the client will read from STDIN

=item --prefix <dir>

  Read configuration from dir rather than /etc/

=item --mailer_name <name>

The name (fqdn) of the MTA

=item --connect_ip <ip>

The IP address of the host connecting to the mailer.

=item --connect_name <hostname>

The name of the host connecting to the mailer.

=item --connect_port <port>

The port of the connection to the mailer.

=item --connect_type <type>

The type of connection to the mailer (eg tcp4).

=item --helo_host <hostname>

The string passed in the HELO stage of the SMTP transaction.

=item --mail_from <address>

The string passed in the MAIL FROM stage of the SMTP transaction.

=item --rcpt_to <address>

The string passed in the RCPT TO stage of the SMTP transaction.

=back

=head1 AUTHOR

Marc Bradshaw <marc@marcbradshaw.net>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2020 by Marc Bradshaw.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut