File: smtp-gateway.pl

package info (click to toggle)
libnet-server-mail-perl 0.28-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 268 kB
  • sloc: perl: 1,299; makefile: 2
file content (132 lines) | stat: -rw-r--r-- 3,195 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/perl -w
# 
# Olivier Poitrey <rs@rhapsodyk.net>
# 8th november 2002
# 
# smtp-gateway.pl: A simple SMTP gateway example.

require 5.006;
use strict;
use POSIX qw(setsid);
use Getopt::Std;
use IO::Socket;
use IO::Select;
use Net::Server::Mail::ESMTP;
use Net::SMTP;


my %opts = (p => 25, h => 'localhost', r => '', d => 0);
getopts('dp:h:r:', \%opts);

my $remote = $opts{r};
unless($remote)
{
    print STDERR "Needs a remote server (-r option)\n";
    exit 1;
}

unless($opts{d})
{
    # become a daemon
    fork and exit;
    setsid;
}

# start to listen
my $server = IO::Socket::INET->new(
    Listen      => 1,
    LocalPort   => $opts{p},
    LocalHost   => $opts{h},
) or die "can't listen $opts{h}:$opts{p}";
my $select = IO::Select->new($server);

my(@ready, $fh, %session_pool);
while(@ready = $select->can_read)
{
    foreach $fh (@ready)
    {
        if($fh == $server)
        {
            my $new = $server->accept();
            $new->blocking(0);
            my $smtpout = Net::SMTP->new( $remote, Debug => $opts{d} ) or do
            {
                $new->print("Service unavailable\n");
                $new->close();
            };
            my $smtpin = Net::Server::Mail::ESMTP->new( socket => $new )
              or die "can't start server on port $opts{p}";
            $smtpin->register('Net::Server::Mail::ESMTP::PIPELINING');
            $smtpin->register('Net::Server::Mail::ESMTP::8BITMIME');
            $smtpin->set_callback(HELO => \&gate_helo, $smtpout);
            $smtpin->set_callback(MAIL => \&gate_mail, $smtpout);
            $smtpin->set_callback(RCPT => \&gate_rcpt, $smtpout);
            $smtpin->set_callback('DATA-INIT' => \&gate_datainit, $smtpout);
            $smtpin->set_callback('DATA-PART' => \&gate_datapart, $smtpout);
            $smtpin->set_callback(DATA => \&gate_dataend, $smtpout);
            $smtpin->set_callback(QUIT => \&gate_quit, $smtpout);
            $smtpin->banner();
            $session_pool{$new} = $smtpin;
            $select->add($new);
        }
        else
        {
            my $operation = join '', <$fh>;
            my $rv = $session_pool{$fh}->process_once($operation);
            if(defined $rv)
            {
                $select->remove($fh);
                delete $session_pool{$fh};
                $fh->close();
            }
        }
    }
}

sub gate_helo
{
    # Net::SMTP send HELO by himself
    return;
}

sub gate_mail
{
    my($session, $address) = @_;
    my $smtpout = $session->get_context();
    return $smtpout->mail($address);
}

sub gate_rcpt
{
    my($session, $address) = @_;
    my $smtpout = $session->get_context();
    return $smtpout->to($address);
}

sub gate_datainit
{
    my($session) = @_;
    my $smtpout = $session->get_context();
    return $smtpout->data();
}

sub gate_datapart
{
    my($session, $dataref) = @_;
    my $smtpout = $session->get_context();
    return $smtpout->datasend($$dataref);
}

sub gate_dataend
{
    my($session, $dataref) = @_;
    my $smtpout = $session->get_context();
    return $smtpout->dataend();
}

sub gate_quit
{
    my($session) = @_;
    my $smtpout = $session->get_context();
    return $smtpout->quit();
}