| 1 | # | ||
| 2 | # $Id$ | ||
| 3 | # | ||
| 4 | # network::smtp Brik | ||
| 5 | # | ||
| 6 | package Metabrik::Network::Smtp; | ||
| 7 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 2 | ▶ | use base qw(Metabrik); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | sub brik_properties { | ||
| 13 | return { | ||
| 14 | revision => '$Revision$', | ||
| 15 | tags => [ qw(unstable) ], | ||
| 16 | author => 'GomoR <GomoR[at]metabrik.org>', | ||
| 17 | license => 'http://opensource.org/licenses/BSD-3-Clause', | ||
| 18 | attributes => { | ||
| 19 | server => [ qw(server) ], | ||
| 20 | port => [ qw(port) ], | ||
| 21 | hello => [ qw(domain) ], | ||
| 22 | username => [ qw(username) ], | ||
| 23 | password => [ qw(password) ], | ||
| 24 | auth_mechanism => [ qw(none|GSSAPI) ], | ||
| 25 | use_starttls => [ qw(0|1) ], | ||
| 26 | _smtp => [ qw(INTERNAL) ], | ||
| 27 | }, | ||
| 28 | attributes_default => { | ||
| 29 | server => 'localhost', | ||
| 30 | port => 25, | ||
| 31 | auth_mechanism => 'none', | ||
| 32 | use_starttls => 0, | ||
| 33 | }, | ||
| 34 | commands => { | ||
| 35 | open => [ qw(server|OPTIONAL port|OPTIONAL username|OPTIONAL password|OPTIONAL) ], | ||
| 36 | close => [ ], | ||
| 37 | }, | ||
| 38 | need_packages => { | ||
| 39 | ubuntu => [ qw(libgssapi-perl) ], | ||
| 40 | debian => [ qw(libgssapi-perl) ], | ||
| 41 | kali => [ qw(libgssapi-perl) ], | ||
| 42 | freebsd => [ qw(p5-GSSAPI) ], | ||
| 43 | }, | ||
| 44 | require_modules => { | ||
| 45 | 'Authen::SASL' => [ qw(Perl) ], | ||
| 46 | 'Authen::SASL::Perl::GSSAPI' => [ ], | ||
| 47 | 'Net::SMTP' => [ ], | ||
| 48 | }, | ||
| 49 | }; | ||
| 50 | } | ||
| 51 | |||
| 52 | sub open { | ||
| 53 | my $self = shift; | ||
| 54 | my ($server, $port, $username, $password) = @_; | ||
| 55 | |||
| 56 | $server ||= $self->server; | ||
| 57 | $port ||= $self->port; | ||
| 58 | $self->brik_help_run_undef_arg('open', $server) or return; | ||
| 59 | $self->brik_help_run_undef_arg('open', $port) or return; | ||
| 60 | |||
| 61 | $username ||= $self->username; | ||
| 62 | $password ||= $self->password; | ||
| 63 | if (!defined($username)) { | ||
| 64 | $username = ''; | ||
| 65 | } | ||
| 66 | if (!defined($password)) { | ||
| 67 | $password = ''; | ||
| 68 | } | ||
| 69 | my $auth_mechanism = $self->auth_mechanism; | ||
| 70 | |||
| 71 | my @args = ( | ||
| 72 | $server, | ||
| 73 | Port => $port, | ||
| 74 | ); | ||
| 75 | |||
| 76 | my $hello = $self->hello; | ||
| 77 | if (defined($hello)) { | ||
| 78 | push @args, ( Hello => $hello ); | ||
| 79 | } | ||
| 80 | |||
| 81 | my $smtp; | ||
| 82 | eval { | ||
| 83 | $smtp = Net::SMTP->new(@args); | ||
| 84 | }; | ||
| 85 | if (! defined($smtp) || $@) { | ||
| 86 | chomp($@); | ||
| 87 | return $self->log->error("open: Net::SMTP new failed for server [$server] port [$port] with [$!]: [$@]"); | ||
| 88 | } | ||
| 89 | |||
| 90 | if ($auth_mechanism ne 'none') { | ||
| 91 | my $sasl = Authen::SASL->new( | ||
| 92 | mechanism => $auth_mechanism, | ||
| 93 | callback => { | ||
| 94 | pass => $password, | ||
| 95 | user => $username, | ||
| 96 | } | ||
| 97 | ); | ||
| 98 | |||
| 99 | $smtp->starttls(); | ||
| 100 | |||
| 101 | my $r = $smtp->auth($sasl); | ||
| 102 | $self->log->info("open: auth: returned [$r]"); | ||
| 103 | } | ||
| 104 | elsif ($self->use_starttls) { | ||
| 105 | $smtp->starttls() or return $self->log->error("open: can't STARTTLS"); | ||
| 106 | } | ||
| 107 | |||
| 108 | #$self->log->info(Data::Dumper::Dumper($smtp)); | ||
| 109 | |||
| 110 | return $self->_smtp($smtp); | ||
| 111 | } | ||
| 112 | |||
| 113 | sub close { | ||
| 114 | my $self = shift; | ||
| 115 | |||
| 116 | my $smtp = $self->_smtp; | ||
| 117 | if (defined($smtp)) { | ||
| 118 | $smtp->quit; | ||
| 119 | $self->_smtp(undef); | ||
| 120 | } | ||
| 121 | |||
| 122 | return 1; | ||
| 123 | } | ||
| 124 | |||
| 125 | 1; | ||
| 126 | |||
| 127 | __END__ | ||