3 # $Id: fakemail,v 1.10 2005/03/18 15:44:00 lastcraft Exp $
5 use Net
::Server
::Mail
::SMTP
;
10 # Bail out if missing parameters.
12 my ($host, $port, $path, $log, $background);
18 'background' => \
$background);
20 if (! defined($host) or ! defined($port) or ! defined($path)) {
21 die "Usage: ./fakemail.pl\n" .
22 " --host=<localdomain>\n" .
23 " --port=<port number>\n" .
24 " --path=<path to save mails>\n" .
25 " --log=<optional file to append messages to>\n" .
34 die ($!) unless defined ($child_pid);
39 POSIX
::setsid
() or die ('Cannot detach from session: $!');
43 $SIG{INT
} = $SIG{TERM
} = $SIG{HUP
} = \
&quit
;
44 $SIG{PIPE
} = 'IGNORE';
57 message
('Starting fakemail');
58 $server = new IO
::Socket
::INET
(Listen
=> 1,
64 message
("Cannot start fakemail: $!");
67 message
("Listening on port $port");
68 while ($socket = $server->accept()) {
69 message
('Starting request');
70 my $smtp = new Net
::Server
::Mail
::SMTP
(socket => $socket);
71 $smtp->set_callback(RCPT
=> \
&validate_recipient
);
72 $smtp->set_callback(DATA
=> \
&queue_message
);
74 close_socket
($socket);
76 message
('Request done');
78 message
("Cannot accept requests");
84 sub validate_recipient
{
85 my($session, $recipient) = @_;
90 my ($session, $data) = @_;
92 message
("Incoming mail");
93 my $sender = $session->get_sender();
94 my @recipients = $session->get_recipients();
95 foreach my $recipient (@recipients) {
96 message
("Capturing mail to $recipient");
97 open(FILE
, "> " . get_filename_from_recipient
($recipient));
100 message
("Mail to $recipient saved");
102 message
("Incoming mail dispatched");
103 return (1, 250, "message queued");
107 $SIG{INT
} = $SIG{TERM
} = $SIG{HUP
} = 'IGNORE';
108 message
('Stopping fakemail');
109 if (defined($socket)) {
110 close_socket
($socket);
113 close_socket
($server);
115 message
('fakemail stopped');
123 $ret = $socket->flush();
124 $ret = $socket->shutdown(2);
125 $ret = $socket->close();
134 sub get_filename_from_recipient
{
135 my $recipient = shift;
137 $recipient =~ s/<|>//g;
138 if (! defined($counts{$recipient})) {
139 $counts{$recipient} = 1;
141 return "$path/$recipient." . $counts{$recipient}++;
149 open(FILE
, ">> $log.$$");
150 print FILE
localtime() . ": $message\n";