| 1 | #!/usr/bin/perl | ||
| 2 | |||
| 3 | package NetServer::SMTP; | ||
| 4 | |||
| 5 | $NetServer::SMTP::VERSION = "0.01"; | ||
| 6 | |||
| 7 | @ISA = (qw(NetServer NetServer::Generic)); | ||
| 8 | |||
| 9 | use Carp; | ||
| 10 | use File::Flock; | ||
| 11 | use Time::CTime; | ||
| 12 | use IO::Handle; | ||
| 13 | use IO::File; | ||
| 14 | use Data::Dumper; | ||
| 15 | use Net::SMTP; | ||
| 16 | use FreezeThaw qw(freeze thaw); | ||
| 17 | |||
| 18 | =pod | ||
| 19 | |||
| 20 | =head1 NAME | ||
| 21 | |||
| 22 | NetServer::SMTP - basic SMTP server class for Perl | ||
| 23 | |||
| 24 | =head1 SYNOPSIS | ||
| 25 | |||
| 26 | my ($self) = NetServer::SMTP->new(); | ||
| 27 | while (<STDIN>) { | ||
| 28 | next if (! defined($_)); | ||
| 29 | my (@vec) = split(/\s+/); | ||
| 30 | my ($fn) = shift @vec; | ||
| 31 | $fn = uc($fn); | ||
| 32 | if (grep(/$fn/i, @$NetServer::SMTP::States) != 0) { | ||
| 33 | $self->in($in); | ||
| 34 | $self->out($out); | ||
| 35 | $self->next_state_ok($fn) && do { | ||
| 36 | $self->$fn(@vec); | ||
| 37 | }; | ||
| 38 | } else { | ||
| 39 | print STDERR "What on earth does [$fn][", | ||
| 40 | join(" ", @vec), "] mean?\n"; | ||
| 41 | } | ||
| 42 | if ($self->{ERROR} > 0 ) { | ||
| 43 | $self->DESTROY(); | ||
| 44 | exit; | ||
| 45 | } | ||
| 46 | } | ||
| 47 | |||
| 48 | =head1 DESCRIPTION | ||
| 49 | |||
| 50 | A class that provides a basic SMTP server object and some methods. (Note | ||
| 51 | that it doesn't provide a B<run> method with a main execution loop -- hence | ||
| 52 | the above example.) | ||
| 53 | |||
| 54 | (C<Net::SMTP> provides a corresponding client class.) | ||
| 55 | |||
| 56 | It accepts requests in accordance with RFC 821, 4.5.1 ("Minimum | ||
| 57 | Implementation"). No attempt to verify the authenticity of the sender is | ||
| 58 | made; no attempt is made to filter out relay attacks or deliver the | ||
| 59 | mail, and it doesn't even attempt to check messages for RFC 822 compliance. | ||
| 60 | Instead, the mail is spooled in the form of a deep-frozen C<NetServer::SMTP> | ||
| 61 | object, dropped into the spool directory. | ||
| 62 | |||
| 63 | Spooled mail may be transmitted by (a) unfreezing it into a live | ||
| 64 | C<NetServer::SMTP> object and (b) invoking the B<send()> method on it. | ||
| 65 | This is the low-level delivery mechanism; it does not include a queueing | ||
| 66 | mechanism. | ||
| 67 | |||
| 68 | Only one delivery method is supported in the base C<NetServer::SMTP> | ||
| 69 | class; this is dumb SMTP forwarding to a remote smarthost. Child classes | ||
| 70 | derived from C<NetServer::SMTP> may provide alternative B<send()> | ||
| 71 | methods which override this default, for example to supply local | ||
| 72 | delivery methods, routing, and associated transport mechanisms for | ||
| 73 | transport via UUCP or other protocols. | ||
| 74 | |||
| 75 | =head2 To-do list | ||
| 76 | |||
| 77 | RFC822 checking of incoming mail to ensure it's not totally corrupt | ||
| 78 | (via Mail::Internet?). | ||
| 79 | |||
| 80 | Velocity checking implemented at the server level -- a semaphore | ||
| 81 | maintains a count of children spawned/reaped, and if the spawn rate goes | ||
| 82 | over a designated hot limit the server will begin issuing 421 responses. | ||
| 83 | |||
| 84 | Queued mail should be tracked via a queue database -- probably a tied | ||
| 85 | DB_File. The key to each record is the position in the queue; the value | ||
| 86 | is a record containing items like the number of retries (so far), the | ||
| 87 | filename of the message, whether to use local or remote delivery | ||
| 88 | methods, and so on. The queue is handled in a round-robin fashion by | ||
| 89 | a queue delivery subroutine. Additional methods for queueing needed. | ||
| 90 | |||
| 91 | Currently, mail is stored using Freeze::Thaw to dump a frozen NetServer::SMTP | ||
| 92 | object. A better solution would be to dump a Mail::Internet object and a | ||
| 93 | NetServer::SMTP object containing delivery and queueing metainformation. | ||
| 94 | (Expect this in release 0.02.) | ||
| 95 | |||
| 96 | =head2 States | ||
| 97 | |||
| 98 | C<NetServer::SMTP> implements a rather minimalist mechanism for checking | ||
| 99 | that state transactions within the SMTP protocol are valid. When entering | ||
| 100 | a given state (or receiving an SMTP command), it checks the hash of arrays | ||
| 101 | C<NetServer::SMTP::NFA>; this contains a list of acceptable antecedent | ||
| 102 | states for the requested command. If the current state isn't in this list, | ||
| 103 | it assumes an error has occured and complains. This is just about okay | ||
| 104 | for a simple program which is intended to receive and spool messages, | ||
| 105 | but it is not a good basis for extension (e.g. for the implementation | ||
| 106 | of a full ESMTP server architecture). In any event, state consistency | ||
| 107 | checking is carried out by calling C<next_state_ok()> -- more sophisticated | ||
| 108 | servers will want to override this. | ||
| 109 | |||
| 110 | Attempts to start an ESMTP session will therefore be politely rejected. | ||
| 111 | |||
| 112 | =head2 Methods | ||
| 113 | |||
| 114 | NetServer::SMTP knows about a few standard messages, and a basic set of SMTP | ||
| 115 | commands (each of which is implemented as a method): | ||
| 116 | |||
| 117 | =over 4 | ||
| 118 | |||
| 119 | =item new() | ||
| 120 | |||
| 121 | Create a new NetServer::SMTP object. The object is now available to process | ||
| 122 | incoming mail. It reads from STDIN, and writes responses to STDOUT; it | ||
| 123 | should be called via C<NetServer::Generic>. | ||
| 124 | |||
| 125 | Some initialisation parameters may be specified as a hash, or as a | ||
| 126 | filehandle referring to a configuration file, or as a configuration | ||
| 127 | file name. | ||
| 128 | |||
| 129 | Initialisation parameters may I<not> be changed once the object has | ||
| 130 | been created; you need to destroy it and start a new one. (Don't worry, | ||
| 131 | this isn't a major overhead.) | ||
| 132 | |||
| 133 | =item HELO | ||
| 134 | |||
| 135 | Commence new session | ||
| 136 | |||
| 137 | =item MAIL | ||
| 138 | |||
| 139 | Begin a new mail | ||
| 140 | |||
| 141 | =item RCPT | ||
| 142 | |||
| 143 | Specify recipients | ||
| 144 | |||
| 145 | =item DATA | ||
| 146 | |||
| 147 | Send data | ||
| 148 | |||
| 149 | =item RSET | ||
| 150 | |||
| 151 | Reset session | ||
| 152 | |||
| 153 | =item NOOP | ||
| 154 | |||
| 155 | Do nothing | ||
| 156 | |||
| 157 | =item QUIT | ||
| 158 | |||
| 159 | End session (spooling mail) | ||
| 160 | |||
| 161 | =back | ||
| 162 | |||
| 163 | These are the minimal SMTP commands required for a basic implementation | ||
| 164 | of RFC 821. Each command is handled via the autoloader, which knows what | ||
| 165 | to do. | ||
| 166 | |||
| 167 | In addition, NetServer::SMTP knows about the following extra commands, | ||
| 168 | which may not behave quite as you expect them to do: | ||
| 169 | |||
| 170 | =over 4 | ||
| 171 | |||
| 172 | =item HELP | ||
| 173 | |||
| 174 | Normally, provides help on a command. | ||
| 175 | |||
| 176 | =back | ||
| 177 | |||
| 178 | =cut | ||
| 179 | |||
| 180 | # known SMTP states | ||
| 181 | $NetServer::SMTP::States = [ qw(EHELO HELO MAIL RCPT DATA RSET NOOP | ||
| 182 | QUIT HELP DUMP TURN) ]; | ||
| 183 | |||
| 184 | # valid autoloaded methods | ||
| 185 | |||
| 186 | $NetServer::SMTP::methods = [ @$NetServer::SMTP::States, qw(IN OUT SERV) ]; | ||
| 187 | |||
| 188 | # known SMTP response codes | ||
| 189 | $NetServer::SMTP::Err = { | ||
| 190 | "211" => "System status, or system help reply", | ||
| 191 | "214" => "Help message \n %s", | ||
| 192 | "220" => "%s: ready for action", | ||
| 193 | "221" => "Bye! %s", | ||
| 194 | "250" => "OK: %s", | ||
| 195 | "251" => "User not local; will forward to <forward-path>", | ||
| 196 | "354" => "%s", | ||
| 197 | "421" => "%s Service not available, closing connection", | ||
| 198 | "450" => "Requested mail action not taken: mailbox unavailable", | ||
| 199 | "451" => "Requested action aborted: error in processing", | ||
| 200 | "452" => "Requested action not taken: insufficient system storage", | ||
| 201 | "500" => "Syntax error, command unrecognized", | ||
| 202 | "501" => "Syntax error in parameter or arguments: %s", | ||
| 203 | "502" => "Command not implemented %s", | ||
| 204 | "503" => "Bad sequence of commands", | ||
| 205 | "504" => "Command parameter not implemented", | ||
| 206 | "550" => "Requested action not taken: %s", | ||
| 207 | "551" => "User not local; please try %s", | ||
| 208 | "552" => "Requested mail action aborted: exceeded storage allocation", | ||
| 209 | "553" => "Requested action not taken: mailbox name not allowed", | ||
| 210 | "554" => "Transaction failed: %s" | ||
| 211 | }; | ||
| 212 | |||
| 213 | # legal SMTP state transitions -- each state is followed by an arrayref | ||
| 214 | # to its legal predecessors. To determine if a new state is legal, check | ||
| 215 | # to see if it's predecessor is in the array in NFA. | ||
| 216 | |||
| 217 | $NetServer::SMTP::NFA = { | ||
| 218 | "HELO" => [ "undef" ], | ||
| 219 | "EHELO" => [ "undef" ], | ||
| 220 | "MAIL" => [ qw(HELO RSET NOOP DATA) ], | ||
| 221 | "RCPT" => [ qw(MAIL NOOP RCPT RSET) ], | ||
| 222 | "MAIL" => [ qw(HELO NOOP DATA RSET) ], | ||
| 223 | "DATA" => [ qw(RCPT NOOP) ], | ||
| 224 | "TURN" => [ @$NetServer::SMTP::States ], | ||
| 225 | "NOOP" => [ @$NetServer::SMTP::States ], | ||
| 226 | "QUIT" => [ @$NetServer::SMTP::States ], | ||
| 227 | "HELP" => [ @$NetServer::SMTP::States ], | ||
| 228 | "DUMP" => [ @$NetServer::SMTP::States ], | ||
| 229 | "RSET" => [ @$NetServer::SMTP::States ], | ||
| 230 | }; | ||
| 231 | |||
| 232 | sub new { | ||
| 233 | # create a new NetServer::SMTP | ||
| 234 | my ($class) = shift; | ||
| 235 | my ($self) = bless {}, $class; | ||
| 236 | if (@_) { | ||
| 237 | $self = $self->initialise(@_); | ||
| 238 | } | ||
| 239 | if (! defined($self->{silent})) { | ||
| 240 | $self->respond(220, "leafmail $NetServer::SMTP::VERSION is ready"); | ||
| 241 | } | ||
| 242 | return $self; | ||
| 243 | } | ||
| 244 | |||
| 245 | sub initialise { | ||
| 246 | |||
| 247 | =pod | ||
| 248 | |||
| 249 | =item initialise() | ||
| 250 | |||
| 251 | Called by B<new()> to initialise the new object. Initialisation keys | ||
| 252 | may be specified as a hash, supplied as a parameter to the new | ||
| 253 | object, or as a filename or file handle containing a frozen | ||
| 254 | B<NetServer::SMTP> object which is users to overlay the object. | ||
| 255 | |||
| 256 | Recognized keys are: | ||
| 257 | |||
| 258 | =over 4 | ||
| 259 | |||
| 260 | =item myhost | ||
| 261 | |||
| 262 | My host name (FQDN) | ||
| 263 | |||
| 264 | =item allowed | ||
| 265 | |||
| 266 | array of aliases for hosts users allowed to send mail | ||
| 267 | |||
| 268 | =item silent | ||
| 269 | |||
| 270 | If silent, dont say hello when creating a new server (we have other | ||
| 271 | reasons for creating NetServer::SMTP objects, once in a while :) | ||
| 272 | |||
| 273 | =item relay | ||
| 274 | |||
| 275 | Relay hostname (FQDN) | ||
| 276 | |||
| 277 | =item ERROR | ||
| 278 | |||
| 279 | If this flag goes non-zero, Something Bad has happened and the session | ||
| 280 | should either terminate or refuse to proceed further | ||
| 281 | |||
| 282 | =item spooldir | ||
| 283 | |||
| 284 | Directory where spooled transactions are waiting | ||
| 285 | |||
| 286 | =back | ||
| 287 | |||
| 288 | =cut | ||
| 289 | |||
| 290 | my ($self) = shift ; | ||
| 291 | my (@junk); | ||
| 292 | if (scalar(@_) == 1) { | ||
| 293 | # it's a filehandle or filename -- open it and load the contents | ||
| 294 | my ($file); | ||
| 295 | my ($fn) = shift; | ||
| 296 | if (ref($fn) !~ /file/i) { | ||
| 297 | $file = new IO::File($fn, "r") or croak "Could not open $fn\n"; | ||
| 298 | } | ||
| 299 | my ($frozen) = join( "", $file->getlines() ); | ||
| 300 | $file->close(); | ||
| 301 | ($self, @junk) = thaw($frozen); | ||
| 302 | $self->{spooledfile} = $fn; | ||
| 303 | return $self; | ||
| 304 | } elsif (scalar(@_) % 2 == 0) { | ||
| 305 | # it's an initialisation hash -- overlay it on $self | ||
| 306 | %$self = (@_); | ||
| 307 | return $self; | ||
| 308 | } else { | ||
| 309 | croak "Don't know how to initialise from [", join("][", @_), "]\n"; | ||
| 310 | } | ||
| 311 | } | ||
| 312 | |||
| 313 | sub respond ($$;@) { | ||
| 314 | # issue a response code and the corresponding message | ||
| 315 | # NOTE: SMTP response messages are printf() format strings and | ||
| 316 | # positional substitution may occur if additional respond() parameters | ||
| 317 | # are available | ||
| 318 | my ($self) = shift; | ||
| 319 | my ($resp) = shift; | ||
| 320 | my (@args) = @_; | ||
| 321 | print STDOUT "$resp ", sprintf($NetServer::SMTP::Err->{$resp}, @args), | ||
| 322 | "\r\n"; | ||
| 323 | } | ||
| 324 | |||
| 325 | sub EHLO { | ||
| 326 | my $self = shift; | ||
| 327 | $self->respond(550, "I don't talk ESMTP"); | ||
| 328 | return; | ||
| 329 | } | ||
| 330 | |||
| 331 | sub HELP { | ||
| 332 | my $self = shift; | ||
| 333 | my $resp = "<<%%" | ||
| 334 | NetServer::SMTP $NetServer::SMTP::VERSION | ||
| 335 | |||
| 336 | Known Commands: | ||
| 337 | |||
| 338 | HELO MAIL RCPT DATA RSET NOOP QUIT HELP DUMP TURN | ||
| 339 | |||
| 340 | %% | ||
| 341 | $self->respond("214", $resp); | ||
| 342 | return; | ||
| 343 | } | ||
| 344 | |||
| 345 | sub HELO { | ||
| 346 | # say hello -- start a session | ||
| 347 | my ($self) = shift; | ||
| 348 | my ($next) = join(" ", @_); | ||
| 349 | $next =~ s/\r\n//;; | ||
| 350 | my ($f, $snd) = ""; | ||
| 351 | if ($next =~ /^from:/i) { | ||
| 352 | $next =~ /(from:*)\s+(.*)/i; | ||
| 353 | ($f, $snd) = ($1, $2); | ||
| 354 | $snd =~ s/[><]//g; | ||
| 355 | } else { | ||
| 356 | $snd = $next; | ||
| 357 | } | ||
| 358 | if (grep(/$snd/i, @{ $self->{allowed} }) != 0) { | ||
| 359 | $self->{host} = $snd; | ||
| 360 | my ($s) = $self->serv(); | ||
| 361 | my ($peer) = $s->peer(); | ||
| 362 | if ($self->{host} ne $peer->[0]) { | ||
| 363 | $self->respond(554, " lie to me at your peril!" ); | ||
| 364 | $self->{ERROR} = 1; | ||
| 365 | return; | ||
| 366 | } | ||
| 367 | my ($line) = "Hello to you too, $snd"; | ||
| 368 | $self->respond(250, $line); | ||
| 369 | } else { | ||
| 370 | $self->respond(421, $snd); | ||
| 371 | $self->{ERROR} = 1; | ||
| 372 | } | ||
| 373 | return; | ||
| 374 | } | ||
| 375 | |||
| 376 | sub MAIL { | ||
| 377 | # specify sender | ||
| 378 | my ($self) = shift; | ||
| 379 | my ($next) = join(" ", @_); | ||
| 380 | if ($next !~ /FROM:/i) { | ||
| 381 | $self->respond(501); | ||
| 382 | return; | ||
| 383 | } | ||
| 384 | my ($t, $from) = split(/:/, $next); | ||
| 385 | $from =~ s/<(.+)>/$1/; | ||
| 386 | if ($from !~ /\S+@\S+/) { | ||
| 387 | my ($serv) = $self->serv(); | ||
| 388 | $from .= "@" . $serv->peername() ; | ||
| 389 | } | ||
| 390 | $self->{from} = $from; | ||
| 391 | $self->respond(250, "sender seems okay (note: no MX or alias checking!)"); | ||
| 392 | return; | ||
| 393 | } | ||
| 394 | |||
| 395 | sub RCPT { | ||
| 396 | # append to recipient list -- this is an arrayref method | ||
| 397 | my ($self) = shift; | ||
| 398 | my ($next) = join(" ", @_); | ||
| 399 | if ($next !~ /TO:/i) { | ||
| 400 | $self->respond(501); | ||
| 401 | return; | ||
| 402 | } else { | ||
| 403 | my ($t, $rcpt) = split(/:/, $next); | ||
| 404 | $rcpt =~ s/<(.+)>/$1/; | ||
| 405 | if ($rcpt !~ /\S+@\S+/) { | ||
| 406 | my ($serv) = $self->serv(); | ||
| 407 | $rcpt .= "@" . $serv->peername() ; | ||
| 408 | } | ||
| 409 | push @{$self->{rcpt}}, $rcpt; | ||
| 410 | $self->respond(250, "recipient okay"); | ||
| 411 | return; | ||
| 412 | } | ||
| 413 | } | ||
| 414 | |||
| 415 | sub DATA { | ||
| 416 | # append message data, read from STDIN (unless optional filehandle is | ||
| 417 | # specified) | ||
| 418 | my ($self) = shift; | ||
| 419 | my ($in) = $self->in(); | ||
| 420 | my ($out) = $self->out(); | ||
| 421 | $self->respond(354, "Ready for data"); | ||
| 422 | my (@head, @body) = (); | ||
| 423 | my $state = "hdr"; | ||
| 424 | DATA: | ||
| 425 | while (defined($tmp = $in->getline())) { | ||
| 426 | $tmp =~ s/\r\n$//; | ||
| 427 | chomp $tmp; | ||
| 428 | last DATA if ($tmp =~ /^\.$/) ; | ||
| 429 | if ($tmp eq "") { | ||
| 430 | $state = "body"; | ||
| 431 | } | ||
| 432 | if ($state eq "hdr") { | ||
| 433 | push (@head, $tmp); | ||
| 434 | } else { | ||
| 435 | push (@body, $tmp); | ||
| 436 | } | ||
| 437 | } | ||
| 438 | # tricky bit; brand the data with our received: line | ||
| 439 | if ( (grep(/^From: /, @head) && | ||
| 440 | (grep(/^To: /, @head)) && | ||
| 441 | (grep(/^Subject: /, @head))) ) { | ||
| 442 | my $s = $self->serv(); | ||
| 443 | my $p = $s->peer(); | ||
| 444 | my ($received) = "Received: from " . $self->{host} . "(" . | ||
| 445 | $p->[0] . "[" . $p->[1] . "]) at " . | ||
| 446 | strftime("%c", localtime(time)) . " by " . $self->{myhost} . | ||
| 447 | " with SMTP via LeafMail ($NetServer::SMTP::VERSION) " . | ||
| 448 | "(relaying via " . $self->{relay} . ");" . | ||
| 449 | strftime(" %c", localtime(time)); | ||
| 450 | unshift(@head , $received); | ||
| 451 | my ($all) = join ("\r\n", @head, @body); | ||
| 452 | $self->{body} = $all; | ||
| 453 | if ($self->spool() == 0) { | ||
| 454 | my $sp = $self->{spooledfile}; | ||
| 455 | $self->respond(250, "Mail accepted (spooled in $sp)"); | ||
| 456 | } else { | ||
| 457 | $self->{ERROR} = 1; | ||
| 458 | } | ||
| 459 | } else { | ||
| 460 | # missing header lines -- one of From:, To:, or Subject: | ||
| 461 | $self->respond(501, "Missing header lines"); | ||
| 462 | } | ||
| 463 | return; | ||
| 464 | } | ||
| 465 | |||
| 466 | |||
| 467 | sub RSET { | ||
| 468 | my ($self) = shift; | ||
| 469 | $self->{rcpt} = $self->{from} = $self->{body} = undef; | ||
| 470 | $self->respond(250, "Reset OK"); | ||
| 471 | return; | ||
| 472 | } | ||
| 473 | |||
| 474 | sub NOOP { | ||
| 475 | my ($self) = shift; | ||
| 476 | $self->respond(250, "I'm waiting ..."); | ||
| 477 | return; | ||
| 478 | } | ||
| 479 | |||
| 480 | sub QUIT { | ||
| 481 | my $self = shift ; | ||
| 482 | $self->respond(221, "So long, and thanks for all the fish"); | ||
| 483 | $self->{ERROR}++; | ||
| 484 | return; | ||
| 485 | } | ||
| 486 | |||
| 487 | sub TURN { | ||
| 488 | my $self = shift; | ||
| 489 | $self->respond(502, "- I don't do scat"); | ||
| 490 | return; | ||
| 491 | } | ||
| 492 | |||
| 493 | sub DUMP { | ||
| 494 | my $self = shift ; | ||
| 495 | print STDOUT Dumper $self; | ||
| 496 | return; | ||
| 497 | } | ||
| 498 | |||
| 499 | sub spool { | ||
| 500 | # return 1 error, 0 for success | ||
| 501 | my $self = shift; | ||
| 502 | my ($tmphash) = {}; | ||
| 503 | my ($foo); | ||
| 504 | foreach $foo (qw(SERV IN OUT allowed prevstate)) { | ||
| 505 | next if (! defined($self->{$foo})); | ||
| 506 | $tmphash->{$foo} = $self->{$foo}; | ||
| 507 | delete $self->{$foo}; | ||
| 508 | } | ||
| 509 | my $tmp = $self->{spooldir} . "/spool." . $$ . time; | ||
| 510 | $self->{spooledfile} = $tmp; | ||
| 511 | my $frozen = freeze($self); | ||
| 512 | print STDERR "spooled to $tmp\n"; | ||
| 513 | # $self->{junk} = $junk; | ||
| 514 | my $sp = IO::File->new($tmp, "w"); | ||
| 515 | if (defined $sp) { | ||
| 516 | lock $tmp; | ||
| 517 | $sp->print($frozen); | ||
| 518 | $sp->close(); | ||
| 519 | unlock $tmp; | ||
| 520 | } else { | ||
| 521 | $self->respond(554, "I hate it when this happens!"); | ||
| 522 | return 1; | ||
| 523 | } | ||
| 524 | foreach my $foo (qw(serv in out allowed prevstate)) { | ||
| 525 | $self->{$foo} = $tmphash->{$foo} ; | ||
| 526 | } | ||
| 527 | return 0; | ||
| 528 | } | ||
| 529 | |||
| 530 | sub send { | ||
| 531 | my ($self) = shift; | ||
| 532 | my ($smtp, $abort) = 0; | ||
| 533 | if (defined ($self->{smtp_session} )) { | ||
| 534 | $smtp = $self->{smtp_session}; | ||
| 535 | } else { | ||
| 536 | my (%options) = ( | ||
| 537 | "hello" => $self->{myhost}, | ||
| 538 | "timeout" => 60 , | ||
| 539 | "Debug" => 0 | ||
| 540 | ); | ||
| 541 | $smtp = Net::SMTP->new($self->{relay}, %options ); | ||
| 542 | } | ||
| 543 | if (ref($smtp) =~ /smtp/i) { | ||
| 544 | $smtp->mail($self->{from}) || $abort++; | ||
| 545 | foreach (@{ $self->{rcpt} }) { | ||
| 546 | next if (! defined($_)); | ||
| 547 | print "sending to: $_\n"; | ||
| 548 | $smtp->to($_) || $abort++; | ||
| 549 | } | ||
| 550 | my $data = $self->{body}; | ||
| 551 | $smtp->data( $data ) || $abort++; | ||
| 552 | $self->{smtp_session} = $smtp; | ||
| 553 | } | ||
| 554 | if ($abort == 0) { | ||
| 555 | # nothing blew up, so don't remove the spooled file | ||
| 556 | if (unlink($self->{spooledfile}) != 1) { | ||
| 557 | warn "failed to unlink ", $self->{spooledfile}, ":$!\n"; | ||
| 558 | } | ||
| 559 | } else { | ||
| 560 | warn "Failed to send ", $self->{spooledfile}, ": try again later\n"; | ||
| 561 | return 0; | ||
| 562 | } | ||
| 563 | return $self->{spooledfile}; | ||
| 564 | } | ||
| 565 | |||
| 566 | sub prevstate { | ||
| 567 | # we're messing with an arrayref here, hence incompatability with AUTOLOAD | ||
| 568 | my ($self) = shift; | ||
| 569 | if (@_) { | ||
| 570 | push @{$self->{prevstate}}, @_; | ||
| 571 | } else { | ||
| 572 | my $tmp = pop @{$self->{prevstate}}; | ||
| 573 | push @{$self->{prevstate}}, $tmp; | ||
| 574 | return $tmp; | ||
| 575 | } | ||
| 576 | } | ||
| 577 | |||
| 578 | sub next_state_ok { | ||
| 579 | my ($self) = shift; | ||
| 580 | my ($next) = shift; | ||
| 581 | # check if next state is okay; return 0 if it's in wrong order, | ||
| 582 | # and emit an appropriate failure message | ||
| 583 | my ($prevstate) = $self->prevstate() || "undef"; | ||
| 584 | if (! grep(/$prevstate/i, @{ $NetServer::SMTP::NFA->{$next} } )) { | ||
| 585 | # $prevstate was not found in one of the permitted previous states | ||
| 586 | $self->respond(503); | ||
| 587 | # $self->{ERROR}++; | ||
| 588 | return 0; | ||
| 589 | } | ||
| 590 | $self->prevstate($next); | ||
| 591 | return 1; | ||
| 592 | } | ||
| 593 | |||
| 594 | sub AUTOLOAD { | ||
| 595 | # change state, process SMTP commands. Recognized commands are | ||
| 596 | my ($self) = shift; | ||
| 597 | my ($name) = uc($AUTOLOAD) ; | ||
| 598 | $name =~ s/.*://; | ||
| 599 | if (@_) { | ||
| 600 | if (! grep(/$name/i, @$NetServer::SMTP::methods)) { # States | ||
| 601 | $self->respond(502); | ||
| 602 | return; | ||
| 603 | } | ||
| 604 | return $self->{$name} = shift; | ||
| 605 | } else { | ||
| 606 | return $self->{$name}; | ||
| 607 | } | ||
| 608 | } | ||
| 609 | |||
| 610 | 1; | ||
| 611 |