/usr/share/perl5/NetSDS/App/SMTPD.pm is in libnetsds-perl 1.301-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 | package NetSDS::App::SMTPD;
use 5.8.0;
use strict;
use warnings;
package NetSDS::App::SMTPD::Socket;
use IO::Socket;
use base 'NetSDS::App';
use version; our $VERSION = '1.301';
sub new {
my ( $proto, %args ) = @_;
my $class = ref $proto || $proto;
my $self = ( %args ? $class->SUPER::new(%args) : bless {}, $class );
return $self->create_socket( $args{'port'} );
}
sub create_socket {
my $self = shift;
my $socket = IO::Socket->new;
$socket->socket( PF_INET, SOCK_STREAM, scalar getprotobyname('tcp') );
$socket->blocking(0);
$self->{'_socket'} = $socket;
return $self;
}
sub get_socket_handle { +shift->{'_socket'} }
sub close { +shift->get_socket_handle->close }
package NetSDS::App::SMTPD::Client;
use Net::Server::Mail::SMTP;
use base 'NetSDS::App::SMTPD::Socket';
sub set_smtp {
my $self = shift;
$self->{'ip'} = shift;
$self->{'_smtp'} = Net::Server::Mail::SMTP->new( socket => $self->get_socket_handle );
return $self;
}
sub set_callback { +shift->get_smtp->set_callback(@_) }
sub process { +shift->get_smtp->process(@_) }
sub get_smtp { +shift->{'_smtp'} }
sub get_header { $_[0]->{'headers'}{ lc $_[1] } }
sub get_msg { +shift->{'msg'} }
sub get_ip { +shift->{'ip'} }
sub get_mail {
my ( $self, $data ) = @_;
my @lines = split /\r\n(?! )/, $$data;
$self->{'headers'} = {};
my $i;
for ( $i = 0 ; $lines[$i] ; $i++ ) {
my ( $key, $value ) = split /:\s*/, $lines[$i], 2;
$key = lc $key;
if ( exists $self->{'headers'}{$key} ) {
unless ( ref $self->{'headers'}{$key} ) {
my $temp = $self->{'headers'}{$key};
$self->{'headers'}{$key} = [ $temp, $value ];
} else {
push @{ $self->{'headers'}{$key} }, $value;
}
} else {
$self->{'headers'}{$key} = $value; #TODO fix me could be several Received
}
}
$self->{'msg'} = join "\r\n", @lines[ $i + 1 .. $#lines ];
return 1;
} ## end sub get_mail
package NetSDS::App::SMTPD;
use base 'NetSDS::App::SMTPD::Socket';
use IO::Socket;
sub create_socket {
my ( $self, $port ) = @_;
$port ||= 2525;
return unless $port;
$self->SUPER::create_socket;
setsockopt( $self->get_socket_handle, SOL_SOCKET, SO_REUSEADDR, 1 );
bind( $self->get_socket_handle, sockaddr_in( $port, INADDR_ANY ) ) or die "Can't use port $port";
listen( $self->get_socket_handle, SOMAXCONN ) or die "Can't listen on port: $port";
return $self;
}
sub can_read {
my $self = shift;
my $rin = '';
vec( $rin, fileno( $self->get_socket_handle ), 1 ) = 1;
return select( $rin, undef, undef, undef );
}
sub accept {
my $self = shift;
$self->can_read;
my $client = NetSDS::App::SMTPD::Client->new;
my $peer = accept( $client->get_socket_handle, $self->get_socket_handle );
if ($peer) {
$client->set_smtp( inet_ntoa( ( sockaddr_in($peer) )[1] ) );
$self->speak( "connection from ip [" . $client->get_ip . "]" );
$client->set_callback( DATA => \&data, $client );
return $client;
}
}
sub data {
my ( $smtp, $data ) = @_;
return $smtp->{'_context'}->get_mail($data);
}
sub process {
my $self = shift;
my $client = $self->accept;
return unless $client;
$client->process;
$client->close;
$self->speak( "connection from ip [" . $client->get_ip . "] closed" );
return $client;
}
1;
__END__
=head1 NAME
NetSDS::App::SMTPD
=head1 SYNOPSIS
use NetSDS::App::SMTPD
=head1 Packages
=head1 NetSDS::App::SMTPD::Socket
Needs for work with socket. This module is a parent for NetSDS::App::SMTPD and NetSDS::App::SMTPD::Client and
a child of a NetSDS::APP
=head3 ITEMS
=over 8
=item B<create_socket>
Creating a simple socket which could be transformed into a listening in NetSDS::App::SMTPD and
could be used in NetSDS::App::SMTPD::Client for accept connection
=item B<can_read>
This method uses for making a timeout before connections to the server:
if there is no connections to accept, program would be just waiting in select while the connection appeared.
=item B<close_socket>
Close socket
=back
=head1 NetSDS::App::SMTPD::Client
Provides the smtp protocol bu using Net::Server::Mail::SMTP.
Had attributes: smtp - an object of Net::Server::Mail::SMTP, ip -
ip of the remote host, headers - ref hash with headers of a message,
msg - a body of a message.
=head3 ITEMS
=over 8
=item B<set_callback> and B<process>
All that subs do - its only call the methods of a Net::Server::Mail::SMTP with the same name.
=item B<get_mail>
In this sub we parse message and set headers of the object and message body. This sub is call as a
callback on event DATA
=item B<get_header> and B<get_msg>
Get methods that make you access to a header of a msg and message body.
Example: $client->get_header('FROM') or $client->get_header('to');
=back
=head1 NetSDS::App::SMTPD
This module init a smtp-server.
=head3 ITEMS
=over 8
=item B<create_socket>
Init a listening socket by creating a simple socket Super::create_socket and make it listening.
=item B<data>
Takes - a message that has been received, parses them and prepare
the structure of headers, body for next actions
=item B<accept>
Waiting for an smtp connection and that accept it.
=item B<data>
=item B<process>
=back
=head1 Example
#!/usr/bin/env perl
use strict;
use warnings;
Receiver->run(
infinite => 1,
debug => 1,
verbose => 1,
conf_file => '../conf/mts-receiver.conf',
);
1;
package Receiver;
use base 'NetSDS::App::SMTPD';
sub process {
my $self = shift;
my $client = $self->SUPER::process;
#do something with msg;
my $from = $client->get_header('from');
my $msg = $client->get_msg;
.....
return $self;
};
or you could reinit process like this:
sub process {
my $self = shift;
my $client = $self->accept;
return unless $client;
$client->process;
#do something
......
$client->close;
return $self;
};
=head1 AUTHOR
Yana Kornienko <yana@netstyle.com.ua>
=cut
|