#! /usr/bin/perl

use strict;
use Socket;

my ($remote, $port, $iaddr, $paddr, $proto, $line, $state, $now_string,
    $fname, $recv_fnd, $fline, $prefix);
my $EOL = "\015\012";
my $localname = " ";                # your machine name goes here
my $version_information = "Message Integrator v0.1";

sub xprint {
  print "$prefix >>> $_[0]\n";
  print SOCK "$_[0]$EOL";
}
sub sprint {
  print SOCK "$_[0]$EOL";
}
sub ehlo {
  xprint("EHLO $localname");
}
sub mfrom {
  xprint("MAIL FROM: $_[0]");
}
sub rto {
  xprint("RCPT TO: $_[0]");
}
sub sdata {
  xprint "DATA";
}
sub squit {
  xprint "QUIT";
}
sub rset {
  xprint "RSET";
}
sub findot {
  xprint ".";
}

$remote  = shift || die "Server must be specified";
$port    = shift || 25;  # standard smtp
if ($port =~ /\D/) { $port = getservbyname($port, 'tcp') }
die "No port" unless $port;

$iaddr   = inet_aton($remote)               || die "no host: $remote";

$paddr   = sockaddr_in($port, $iaddr);

$proto   = getprotobyname('tcp');

socket(SOCK, PF_INET, SOCK_STREAM, $proto)  || die "socket: $!";
connect(SOCK, $paddr)    || die "connect: $!";
select(SOCK); $| = 1; select(STDOUT);

$state = "greet";

$prefix = "no prefix";

stdinwhile: while ((defined($fname = <>)) or ($state = "panic")) {
  if ($state ne "panic") {
    chomp $fname;
    open(FILE, '<', $fname) or die ("Failed ($!) opening file: $fname");
    undef $recv_fnd;
    $prefix = $fname;
    $prefix =~ s-.*/--;
    print "\n\nWorking on file $fname\n";
  }

 mainwhile: while (defined($line = <SOCK>)) {
    my ($prefix, $command, $params, $target, $text, $originator);
    $line =~ s/[\015\012]//g;
    print "<<< ${line}\n";
    next mainwhile if ($line =~ /^...-/);
    print "$state\n";
    if ($state eq "quit") {
      last stdinwhile;
    }
    if ($state eq "panic") {
      squit();
      $state = "quit";
      next mainwhile;
    }
    if ($line =~ /^[45].. /) {
      rset();
      $state = "panic";
      next mainwhile;
    }
    if ($line =~ /^3.. /) {
      if ($state ne "data") {
	rset();
	$state = "panic";
	next mainwhile;
      }
      # send the actual data!
      $state = "message";
      $now_string = gmtime;

    filewhile: while ((defined($fline = <FILE>))) {
	chomp $fline;
	if (!defined $recv_fnd) {
	  if ($fline =~ /^Received:/) {
	    xprint "Received: by $version_information at $now_string.";
	    $recv_fnd = "yes";
	  }
	}
	if ($fline =~ /^Return-Path: /i) {
	  next filewhile;
	}
	if ($fline =~ /^.$/) {
	  $fline = "..";
	}
	sprint "$fline";
      }
      xprint ".";
      $state = "finaldot";
      next mainwhile;
    }
    if ($state eq "greet") {
      ehlo();
      $state = "premsg";
      next mainwhile;
    }
    if ($state eq "premsg") {
      mfrom('< @ >');          # your address goes here
      $state = "mail from";
      next mainwhile;
    }
    if ($state eq "mail from") {
      rto('< @gmail.com>');    # your gmail address goes here
      $state = "rcpt to";
      next mainwhile;
    }
    if ($state eq "rcpt to") {
      sdata();
      $state = "data";
      next mainwhile;
    }
    if ($state eq "finaldot") {
      rset();
      #$state = "panic";
      $state = "premsg";
      last mainwhile;
    }
  }
  close (FILE);
}
close (SOCK) || die "close: $!";
exit;


