| 1 |
# |
| 2 |
# This file is best read with ``perldoc writing.pod'' |
| 3 |
# |
| 4 |
|
| 5 |
### |
| 6 |
# Conventions: |
| 7 |
# plugin names: F<myplugin>, F<qpsmtpd-async> |
| 8 |
# constants: I<LOGDEBUG> |
| 9 |
# smtp commands, answers: B<HELO>, B<250 Queued!> |
| 10 |
# |
| 11 |
# Notes: |
| 12 |
# * due to restrictions of some POD parsers, no C<<$object->method()>> |
| 13 |
# are allowed, use C<$object-E<gt>method()> |
| 14 |
# |
| 15 |
|
| 16 |
=head1 Writing your own plugins |
| 17 |
|
| 18 |
This is a walk through a new queue plugin, which queues the mail to a (remote) |
| 19 |
QMQP-Server. |
| 20 |
|
| 21 |
First step is to pull in the necessary modules |
| 22 |
|
| 23 |
use IO::Socket; |
| 24 |
use Text::Netstring qw( netstring_encode |
| 25 |
netstring_decode |
| 26 |
netstring_verify |
| 27 |
netstring_read ); |
| 28 |
|
| 29 |
We know, we need a server to send the mails to. This will be the same |
| 30 |
for every mail, so we can use arguments to the plugin to configure this |
| 31 |
server (and port). |
| 32 |
|
| 33 |
Inserting this static config is done in C<register()>: |
| 34 |
|
| 35 |
sub register { |
| 36 |
my ($self, $qp, @args) = @_; |
| 37 |
|
| 38 |
die "No QMQP server specified in qmqp-forward config" |
| 39 |
unless @args; |
| 40 |
|
| 41 |
$self->{_qmqp_timeout} = 120; |
| 42 |
|
| 43 |
if ($args[0] =~ /^([\.\w_-]+)$/) { |
| 44 |
$self->{_qmqp_server} = $1; |
| 45 |
} |
| 46 |
else { |
| 47 |
die "Bad data in qmqp server: $args[0]"; |
| 48 |
} |
| 49 |
|
| 50 |
$self->{_qmqp_port} = 628; |
| 51 |
if (@args > 1 and $args[1] =~ /^(\d+)$/) { |
| 52 |
$self->{_qmqp_port} = $1; |
| 53 |
} |
| 54 |
|
| 55 |
$self->log(LOGWARN, "WARNING: Ignoring additional arguments.") |
| 56 |
if (@args > 2); |
| 57 |
} |
| 58 |
|
| 59 |
We're going to write a queue plugin, so we need to hook to the I<queue> |
| 60 |
hook. |
| 61 |
|
| 62 |
sub hook_queue { |
| 63 |
my ($self, $transaction) = @_; |
| 64 |
|
| 65 |
$self->log(LOGINFO, "forwarding to $self->{_qmqp_server}:" |
| 66 |
."$self->{_qmqp_port}"); |
| 67 |
|
| 68 |
The first step is to open a connection to the remote server. |
| 69 |
|
| 70 |
my $sock = IO::Socket::INET->new( |
| 71 |
PeerAddr => $self->{_qmqp_server}, |
| 72 |
PeerPort => $self->{_qmqp_port}, |
| 73 |
Timeout => $self->{_qmqp_timeout}, |
| 74 |
Proto => 'tcp') |
| 75 |
or $self->log(LOGERROR, "Failed to connect to " |
| 76 |
."$self->{_qmqp_server}:" |
| 77 |
."$self->{_qmqp_port}: $!"), |
| 78 |
return(DECLINED); |
| 79 |
$sock->autoflush(1); |
| 80 |
|
| 81 |
=over 4 |
| 82 |
|
| 83 |
=item * |
| 84 |
|
| 85 |
The client starts with a safe 8-bit text message. It encodes the message |
| 86 |
as the byte string C<firstline\012secondline\012 ... \012lastline>. (The |
| 87 |
last line is usually, but not necessarily, empty.) The client then encodes |
| 88 |
this byte string as a netstring. The client also encodes the envelope |
| 89 |
sender address as a netstring, and encodes each envelope recipient address |
| 90 |
as a netstring. |
| 91 |
|
| 92 |
The client concatenates all these netstrings, encodes the concatenation |
| 93 |
as a netstring, and sends the result. |
| 94 |
|
| 95 |
(from L<http://cr.yp.to/proto/qmqp.html>) |
| 96 |
|
| 97 |
=back |
| 98 |
|
| 99 |
The first idea is to build the package we send, in the order described |
| 100 |
in the paragraph above: |
| 101 |
|
| 102 |
my $message = $transaction->header->as_string; |
| 103 |
$transaction->body_resetpos; |
| 104 |
while (my $line = $transaction->body_getline) { |
| 105 |
$message .= $line; |
| 106 |
} |
| 107 |
$message = netstring_encode($message); |
| 108 |
$message .= netstring_encode($transaction->sender->address); |
| 109 |
for ($transaction->recipients) { |
| 110 |
push @rcpt, $_->address; |
| 111 |
} |
| 112 |
$message .= join "", netstring_encode(@rcpt); |
| 113 |
print $sock netstring_encode($message) |
| 114 |
or do { |
| 115 |
my $err = $!; |
| 116 |
$self->_disconnect($sock); |
| 117 |
return(DECLINED, "Failed to print to socket: $err"); |
| 118 |
}; |
| 119 |
|
| 120 |
This would mean, we have to hold the full message in memory... Not good |
| 121 |
for large messages, and probably even slower (for large messages). |
| 122 |
|
| 123 |
Luckily it's easy to build a netstring without the help of the |
| 124 |
C<Text::Netstring> module if you know the size of the string (for more |
| 125 |
info about netstrings see L<http://cr.yp.to/proto/netstrings.txt>). |
| 126 |
|
| 127 |
We start with the sender and recipient addresses: |
| 128 |
|
| 129 |
my ($addrs, $headers, @rcpt); |
| 130 |
$addrs = netstring_encode($transaction->sender->address); |
| 131 |
for ($transaction->recipients) { |
| 132 |
push @rcpt, $_->address; |
| 133 |
} |
| 134 |
$addrs .= join "", netstring_encode(@rcpt); |
| 135 |
|
| 136 |
Ok, we got the sender and the recipients, now let's see what size the |
| 137 |
message is. |
| 138 |
|
| 139 |
$headers = $transaction->header->as_string; |
| 140 |
my $msglen = length($headers) + $transaction->body_length; |
| 141 |
|
| 142 |
We've got everything we need. Now build the netstrings for the full package |
| 143 |
and the message. |
| 144 |
|
| 145 |
First the beginning of the netstring of the full package |
| 146 |
|
| 147 |
# (+ 2: the ":" and "," of the message's netstring) |
| 148 |
print $sock ($msglen + length($msglen) + 2 + length($addrs)) |
| 149 |
.":" |
| 150 |
."$msglen:$headers" ### beginning of messages netstring |
| 151 |
or do { |
| 152 |
my $err = $!; |
| 153 |
$self->_disconnect($sock); |
| 154 |
return(DECLINED, |
| 155 |
"Failed to print to socket: $err"); |
| 156 |
}; |
| 157 |
|
| 158 |
Go to beginning of the body |
| 159 |
|
| 160 |
$transaction->body_resetpos; |
| 161 |
|
| 162 |
If the message is spooled to disk, read the message in |
| 163 |
blocks and write them to the server |
| 164 |
|
| 165 |
if ($transaction->body_fh) { |
| 166 |
my $buff; |
| 167 |
my $size = read $transaction->body_fh, $buff, 4096; |
| 168 |
unless (defined $size) { |
| 169 |
my $err = $!; |
| 170 |
$self->_disconnect($sock); |
| 171 |
return(DECLINED, "Failed to read from body_fh: $err"); |
| 172 |
} |
| 173 |
while ($size) { |
| 174 |
print $sock $buff |
| 175 |
or do { |
| 176 |
my $err = $!; |
| 177 |
$self->_disconnect($sock); |
| 178 |
return(DECLINED, "Failed to print to socket: $err"); |
| 179 |
}; |
| 180 |
|
| 181 |
$size = read $transaction->body_fh, $buff, 4096; |
| 182 |
unless (defined $size) { |
| 183 |
my $err = $!; |
| 184 |
$self->_disconnect($sock); |
| 185 |
return(DECLINED, |
| 186 |
"Failed to read from body_fh: $err"); |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
Else we have to read it line by line ... |
| 192 |
|
| 193 |
else { |
| 194 |
while (my $line = $transaction->body_getline) { |
| 195 |
print $sock $line |
| 196 |
or do { |
| 197 |
my $err = $!; |
| 198 |
$self->_disconnect($sock); |
| 199 |
return(DECLINED, "Failed to print to socket: $err"); |
| 200 |
}; |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
Message is at the server, now finish the package. |
| 205 |
|
| 206 |
print $sock "," # end of messages netstring |
| 207 |
.$addrs # sender + recpients |
| 208 |
."," # end of netstring of |
| 209 |
# the full package |
| 210 |
or do { |
| 211 |
my $err = $!; |
| 212 |
$self->_disconnect($sock); |
| 213 |
return(DECLINED, |
| 214 |
"Failed to print to socket: $err"); |
| 215 |
}; |
| 216 |
|
| 217 |
We're done. Now let's see what the remote qmqpd says... |
| 218 |
|
| 219 |
|
| 220 |
=over 4 |
| 221 |
|
| 222 |
=item * |
| 223 |
|
| 224 |
(continued from L<http://cr.yp.to/proto/qmqp.html>:) |
| 225 |
|
| 226 |
The server's response is a nonempty string of 8-bit bytes, encoded as a |
| 227 |
netstring. |
| 228 |
|
| 229 |
The first byte of the string is either K, Z, or D. K means that the |
| 230 |
message has been accepted for delivery to all envelope recipients. This |
| 231 |
is morally equivalent to the 250 response to DATA in SMTP; it is subject |
| 232 |
to the reliability requirements of RFC 1123, section 5.3.3. Z means |
| 233 |
temporary failure; the client should try again later. D means permanent |
| 234 |
failure. |
| 235 |
|
| 236 |
Note that there is only one response for the entire message; the server |
| 237 |
cannot accept some recipients while rejecting others. |
| 238 |
|
| 239 |
=back |
| 240 |
|
| 241 |
|
| 242 |
my $answer = netstring_read($sock); |
| 243 |
$self->_disconnect($sock); |
| 244 |
|
| 245 |
if (defined $answer and netstring_verify($answer)) { |
| 246 |
$answer = netstring_decode($answer); |
| 247 |
|
| 248 |
$answer =~ s/^K// and return(OK, |
| 249 |
"Queued! $answer"); |
| 250 |
$answer =~ s/^Z// and return(DENYSOFT, |
| 251 |
"Deferred: $answer"); |
| 252 |
$answer =~ s/^D// and return(DENY, |
| 253 |
"Denied: $answer"); |
| 254 |
} |
| 255 |
|
| 256 |
If this is the only F<queue/*> plugin, the client will get a 451 temp error: |
| 257 |
|
| 258 |
return(DECLINED, "Protocol error"); |
| 259 |
} |
| 260 |
|
| 261 |
sub _disconnect { |
| 262 |
my ($self,$sock) = @_; |
| 263 |
if (defined $sock) { |
| 264 |
eval { close $sock; }; |
| 265 |
undef $sock; |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
=cut |
| 270 |
|
| 271 |
# vim: ts=2 sw=2 expandtab |