| 1 | package Net::Server::Mail::LMTP; | ||
| 2 | |||
| 3 | use 5.006; | ||
| 4 | use strict; | ||
| 5 | use base qw(Net::Server::Mail::ESMTP); | ||
| 6 | |||
| 7 | our $VERSION = "0.26"; | ||
| 8 | |||
| 9 | =pod | ||
| 10 | |||
| 11 | =head1 NAME | ||
| 12 | |||
| 13 | Net::Server::Mail::LMTP - A module to implement the LMTP protocol | ||
| 14 | |||
| 15 | |||
| 16 | =head1 SYNOPSIS | ||
| 17 | |||
| 18 | use Net::Server::Mail::LMTP; | ||
| 19 | |||
| 20 | my @local_domains = qw(example.com example.org); | ||
| 21 | my $server = IO::Socket::INET->new( Listen => 1, LocalPort => 25 ); | ||
| 22 | |||
| 23 | my $conn; | ||
| 24 | while($conn = $server->accept) | ||
| 25 | { | ||
| 26 | my $esmtp = Net::Server::Mail::LMTP->new( socket => $conn ); | ||
| 27 | # adding some handlers | ||
| 28 | $esmtp->set_callback(RCPT => \&validate_recipient); | ||
| 29 | $esmtp->set_callback(DATA => \&queue_message); | ||
| 30 | $esmtp->process(); | ||
| 31 | $conn->close(); | ||
| 32 | } | ||
| 33 | |||
| 34 | sub validate_recipient | ||
| 35 | { | ||
| 36 | my($session, $recipient) = @_; | ||
| 37 | |||
| 38 | my $domain; | ||
| 39 | if($recipient =~ /\@(.*)>\s*$/) | ||
| 40 | { | ||
| 41 | $domain = $1; | ||
| 42 | } | ||
| 43 | |||
| 44 | if(not defined $domain) | ||
| 45 | { | ||
| 46 | return(0, 513, 'Syntax error.'); | ||
| 47 | } | ||
| 48 | elsif(not(grep $domain eq $_, @local_domains)) | ||
| 49 | { | ||
| 50 | return(0, 554, "$recipient: Recipient address rejected: Relay access denied"); | ||
| 51 | } | ||
| 52 | |||
| 53 | return(1); | ||
| 54 | } | ||
| 55 | |||
| 56 | sub queue_message | ||
| 57 | { | ||
| 58 | my($session, $data) = @_; | ||
| 59 | |||
| 60 | my $sender = $session->get_sender(); | ||
| 61 | my @recipients = $session->get_recipients(); | ||
| 62 | |||
| 63 | return(0, 554, 'Error: no valid recipients') | ||
| 64 | unless(@recipients); | ||
| 65 | |||
| 66 | my $msgid = add_queue($sender, \@recipients, $data) | ||
| 67 | or return(0); | ||
| 68 | |||
| 69 | return(1, 250, "message queued $msgid"); | ||
| 70 | } | ||
| 71 | |||
| 72 | =head1 DESCRIPTION | ||
| 73 | |||
| 74 | This class implement the LMTP (RFC 2033) protocol. | ||
| 75 | |||
| 76 | This class inherit from Net::Server::Mail::ESMTP. Please see | ||
| 77 | L<Net::Server::Mail::ESMTP> for documentation of common methods. | ||
| 78 | |||
| 79 | =cut | ||
| 80 | |||
| 81 | sub init { | ||
| 82 | my ( $self, @args ) = @_; | ||
| 83 | my $rv = $self->SUPER::init(@args); | ||
| 84 | return $rv unless $rv eq $self; | ||
| 85 | |||
| 86 | $self->undef_verb('HELO'); | ||
| 87 | $self->undef_verb('EHLO'); | ||
| 88 | |||
| 89 | $self->def_verb( LHLO => 'lhlo' ); | ||
| 90 | |||
| 91 | # Required by RFC | ||
| 92 | $self->register('Net::Server::Mail::ESMTP::PIPELINING'); | ||
| 93 | |||
| 94 | return $self; | ||
| 95 | } | ||
| 96 | |||
| 97 | sub get_protoname { | ||
| 98 | return 'LMTP'; | ||
| 99 | } | ||
| 100 | |||
| 101 | =pod | ||
| 102 | |||
| 103 | =head1 EVENTS | ||
| 104 | |||
| 105 | Descriptions of callback who's can be used with set_callback | ||
| 106 | method. All handle takes the Net::Server::Mail::ESMTP object as first | ||
| 107 | argument and specific callback's arguments. | ||
| 108 | |||
| 109 | =head2 LHLO | ||
| 110 | |||
| 111 | Same as ESMTP EHLO, please see L<Net::Server::Mail::ESMTP>. | ||
| 112 | |||
| 113 | =cut | ||
| 114 | |||
| 115 | sub lhlo { | ||
| 116 | my ( $self, $hostname ) = @_; | ||
| 117 | |||
| 118 | unless ( defined $hostname && length $hostname ) { | ||
| 119 | $self->reply( 501, 'Syntax error in parameters or arguments' ); | ||
| 120 | return; | ||
| 121 | } | ||
| 122 | |||
| 123 | my $response = $self->get_hostname . ' Service ready'; | ||
| 124 | |||
| 125 | my @extends; | ||
| 126 | foreach my $extend ( $self->get_extensions ) { | ||
| 127 | push( @extends, join( ' ', $extend->keyword, $extend->parameter ) ); | ||
| 128 | } | ||
| 129 | |||
| 130 | $self->extend_mode(1); | ||
| 131 | $self->make_event( | ||
| 132 | name => 'LHLO', | ||
| 133 | arguments => [ $hostname, \@extends ], | ||
| 134 | on_success => sub { | ||
| 135 | |||
| 136 | # according to the RFC, LHLO ensures "that both the SMTP client | ||
| 137 | # and the SMTP server are in the initial state" | ||
| 138 | $self->{extend_mode} = 1; | ||
| 139 | $self->step_reverse_path(1); | ||
| 140 | $self->step_forward_path(0); | ||
| 141 | $self->step_maildata_path(0); | ||
| 142 | }, | ||
| 143 | success_reply => [ 250, [ $response, @extends ] ], | ||
| 144 | ); | ||
| 145 | |||
| 146 | return; | ||
| 147 | } | ||
| 148 | |||
| 149 | =pod | ||
| 150 | |||
| 151 | =head2 DATA | ||
| 152 | |||
| 153 | Override the default DATA event by a per recipient response. It will | ||
| 154 | be called for each recipients with data (in a scalar reference) as | ||
| 155 | first argument followed by the current recipient. | ||
| 156 | |||
| 157 | =cut | ||
| 158 | |||
| 159 | sub data { | ||
| 160 | my ( $self, $args ) = @_; | ||
| 161 | |||
| 162 | unless ( ref $self->step_forward_path eq 'ARRAY' | ||
| 163 | and @{ $self->step_forward_path } ) | ||
| 164 | { | ||
| 165 | $self->reply( 503, 'Bad sequence of commands' ); | ||
| 166 | return; | ||
| 167 | } | ||
| 168 | $self->SUPER::data($args); | ||
| 169 | } | ||
| 170 | |||
| 171 | sub data_finished { | ||
| 172 | my ( $self, $more_data ) = @_; | ||
| 173 | |||
| 174 | my @recipients = @{ $self->step_forward_path || [] }; | ||
| 175 | |||
| 176 | $self->{continuous_reply} = 1; | ||
| 177 | while ( my $forward_path = shift @recipients ) { | ||
| 178 | $self->{continuous_reply} = 0 unless @recipients; | ||
| 179 | $self->make_event( | ||
| 180 | name => 'DATA', | ||
| 181 | arguments => [ \$self->{_data}, $forward_path ], | ||
| 182 | success_reply => [ 250, 'Ok' ], | ||
| 183 | failure_reply => [ 550, "$forward_path Failed" ], | ||
| 184 | ); | ||
| 185 | } | ||
| 186 | $self->{continuous_reply} = 0; | ||
| 187 | |||
| 188 | # reinitiate the connection | ||
| 189 | $self->step_reverse_path(1); | ||
| 190 | $self->step_forward_path(0); | ||
| 191 | $self->step_maildata_path(0); | ||
| 192 | |||
| 193 | # if more data, handle it | ||
| 194 | if ($more_data) { | ||
| 195 | return $self->{process_operation}( $self, $more_data ); | ||
| 196 | } | ||
| 197 | else { | ||
| 198 | return; | ||
| 199 | } | ||
| 200 | } | ||
| 201 | |||
| 202 | sub reply { | ||
| 203 | my ( $self, $code, $msg ) = @_; | ||
| 204 | |||
| 205 | if ( not ref $msg and $self->{continuous_reply} ) { | ||
| 206 | my $out = $self->{out}; | ||
| 207 | print $out "$code-$msg\r\n"; | ||
| 208 | } | ||
| 209 | else { | ||
| 210 | $self->SUPER::reply( $code, $msg ); | ||
| 211 | } | ||
| 212 | } | ||
| 213 | |||
| 214 | =pod | ||
| 215 | |||
| 216 | =head1 SEE ALSO | ||
| 217 | |||
| 218 | Please, see L<Net::Server::Mail>, L<Net::Server::Mail::SMTP> | ||
| 219 | and L<Net::Server::Mail::ESMTP>. | ||
| 220 | |||
| 221 | =head1 AUTHOR | ||
| 222 | |||
| 223 | Olivier Poitrey E<lt>rs@rhapsodyk.netE<gt> | ||
| 224 | |||
| 225 | =head1 AVAILABILITY | ||
| 226 | |||
| 227 | Available on CPAN. | ||
| 228 | |||
| 229 | anonymous Git repository: | ||
| 230 | |||
| 231 | git clone git://github.com/rs/net-server-mail.git | ||
| 232 | |||
| 233 | Git repository on the web: | ||
| 234 | |||
| 235 | L<https://github.com/rs/net-server-mail> | ||
| 236 | |||
| 237 | =head1 BUGS | ||
| 238 | |||
| 239 | Please use CPAN system to report a bug (http://rt.cpan.org/). | ||
| 240 | |||
| 241 | =head1 LICENCE | ||
| 242 | |||
| 243 | This library is free software; you can redistribute it and/or modify | ||
| 244 | it under the terms of the GNU Lesser General Public License as | ||
| 245 | published by the Free Software Foundation; either version 2.1 of the | ||
| 246 | License, or (at your option) any later version. | ||
| 247 | |||
| 248 | This library is distributed in the hope that it will be useful, but | ||
| 249 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 250 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 251 | Lesser General Public License for more details. | ||
| 252 | |||
| 253 | You should have received a copy of the GNU Lesser General Public | ||
| 254 | License along with this library; if not, write to the Free Software | ||
| 255 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 | ||
| 256 | USA | ||
| 257 | |||
| 258 | =head1 COPYRIGHT | ||
| 259 | |||
| 260 | Copyright (C) 2002 - Olivier Poitrey, 2007 - Xavier Guimard | ||
| 261 | |||
| 262 | =cut | ||
| 263 | |||
| 264 | 1; |