-
Notifications
You must be signed in to change notification settings - Fork 1
/
XmtpClient.pm
297 lines (215 loc) · 5.87 KB
/
XmtpClient.pm
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
290
291
292
293
294
295
296
package Net::XmtpClient;
=head1 NAME
Net::XmtpClient - Implement SMTP/LMTP client skeleton
=head1 SYNOPSIS
use Net::XmtpClient;
my $Client = Net::XmtpClient->new(Server => $Server, Port => $Port);
my $Socket = $Client->{Socket};
my ($Code, $Rem, $Other);
($Code, $Rem, $Other) = $Client->banner();
($Code, $Rem, $Other) = $Client->helo('host');
($Code, $Rem, $Other) = $Client->mail_from('[email protected]');
($Code, $Rem, $Other) = $Client->rcpt_to('[email protected]');
($Code, $Rem, $Other) = $Client->data();
while (<>) {
chomp;
print $Socket $_, "\r\n";
}
($Code, $Rem, $Other) = $Client->end_data_smtp();
my @Responses = $Client->end_data_lmtp();
=head1 DESCRIPTION
This module implements a SMTP/LMTP client. You connect to
a server, and send the various SMTP/LMTP commands by
calling the appropriate methods.
=head1 RETURN VALUES
Each method call returns the same basic response, a
3 element array with:
=over 4
=item Code
The SMTP/LMTP response code
=item Rem
The remainder of the final response line
=item Other
An array ref with any other response lines that
were returned before the final response line
=back
=cut
# Use modules/constants {{{
use IO::Socket::INET;
use IO::Socket::UNIX;
# Avoid UTF-8 regexp issues. Treat everything as pure
# binary data
no utf8;
use bytes;
# Standard use items
use Data::Dumper;
use strict;
use warnings;
# }}}
=head1 METHODS
=over 4
=cut
sub new {
my $Proto = shift;
my $Class = ref($Proto) || $Proto;
my %Args = @_;
$Args{Server} || die "No 'Server' specified";
# Set ourself to empty to start with
my $Self = {};
bless ($Self, $Class);
my $Server = $Self->{Server} = $Args{Server};
my $Port = $Self->{Port} = $Args{Port} || 25;
my $Debug = $Self->{Debug} = $Args{Debug} || 0;
my $Logger = $Self->{Logger} = $Args{Logger} || 0;
$Self->{LastSent} = '';
$Self->{LastResp} = '';
$Self->{RespCode} = '';
$Self->debug("Connecting to '$Server' on '$Port'");
my $Socket;
if ($Server =~ m{^/}) {
$Socket = IO::Socket::UNIX->new(
Peer => $Server,
Timeout => 120,
Type => SOCK_STREAM
) || return undef;
} else {
$Socket = IO::Socket::INET->new(
PeerAddr => $Server,
PeerPort => $Port,
Timeout => 120,
Proto => 'tcp',
Type => SOCK_STREAM
) || return undef;
}
# Force flushing after every write to the socket
my $ofh = select($Socket); $| = 1; select ($ofh);
$Self->{Socket} = $Socket;
# Read server banner response
$Self->read_server_response();
return $Self;
}
sub debug {
my ($Self, $Msg) = @_;
if ($Self->{Debug}) {
$Msg = "$Msg. Last sent=$Self->{LastSent}, code=$Self->{RespCode}, response=$Self->{LastResp}\n";
if ( $Self->{Logger} ) {
$Self->{Logger}->mylog($Msg);
} else {
warn $Msg;
}
}
}
=item I<read_server_response($Self)>
Get the response code and message from the server
for the last sent command. Returns the response
code.
=cut
sub read_server_response {
my $Self = shift;
my $Socket = $Self->{Socket};
my @RespLines;
# Get response line (repeat if continued response)
my $Resp = <$Socket> || '';
$Resp =~ s/\r\n$//;
while ($Resp =~ /^\d{3}-(.*)$/) {
push @RespLines, $1;
$Resp = <$Socket> || '';
$Resp =~ s/\r\n$//;
}
my ($Code) = $Resp =~ /^(\d{3})\s+(.*)$/;
push @RespLines, $2;
$Self->{RespCode} = $Code || '';
$Self->{LastResp} = $2 || '';
$Self->{RespLines} = \@RespLines;
$Self->debug("Completed command");
return $Code;
}
sub last_response {
my $Self = shift;
return ($Self->{RespCode} || '', $Self->{LastResp} || '');
}
sub full_response {
my $Self = shift;
return ($Self->{RespCode} || '', @{$Self->{RespLines} || []});
}
sub no_socket {
my $Self = shift;
my $Resp = "No connection to server";
$Self->{RespLines} = [ $Resp ];
$Self->{RespCode} = '421';
$Self->{LastResp} = $Resp;
}
sub send_to_client {
my $Self = shift;
my $Socket = $Self->{Socket};
$Self->{LastSent} = $_[0];
print $Socket shift(@_) . "\r\n";
}
sub helo {
my $Self = shift;
return $Self->no_socket() unless $Self->{Socket};
$Self->send_to_client("HELO " . join(' ', @_));
return $Self->read_server_response();
}
sub ehlo {
my $Self = shift;
return $Self->no_socket() unless $Self->{Socket};
$Self->send_to_client("EHLO " . join(' ', @_));
return $Self->read_server_response();
}
sub lhlo {
my $Self = shift;
return $Self->no_socket() unless $Self->{Socket};
$Self->send_to_client("LHLO " . join(' ', @_));
return $Self->read_server_response();
}
sub noop {
my $Self = shift;
return $Self->no_socket() unless $Self->{Socket};
$Self->send_to_client("NOOP");
return $Self->read_server_response();
}
sub mail_from {
my $Self = shift;
return $Self->no_socket() unless $Self->{Socket};
$Self->send_to_client("MAIL FROM:" . join(' ', "<" . shift(@_) . ">", @_));
return $Self->read_server_response();
}
sub rcpt_to {
my $Self = shift;
return $Self->no_socket() unless $Self->{Socket};
$Self->send_to_client("RCPT TO:" . join(' ', "<" . shift(@_) . ">", @_));
return $Self->read_server_response();
}
sub data {
my $Self = shift;
return $Self->no_socket() unless $Self->{Socket};
$Self->send_to_client("DATA");
return $Self->read_server_response();
}
sub rset {
my $Self = shift;
return $Self->no_socket() unless $Self->{Socket};
$Self->send_to_client("RSET");
return $Self->read_server_response();
}
=back
=cut
=head1 AUTHOR
Rob Mueller E<lt>[email protected]<gt>
=cut
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2003-2017 by FastMail Pty Ltd
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut
sub quit {
my $Self = shift;
return $Self->no_socket() unless $Self->{Socket};
$Self->send_to_client("QUIT");
my $Res = $Self->read_server_response();
close($Self->{Socket});
$Self->{Socket} = undef;
return $Res;
}
1;