#!/usr/bin/perl -w
## $Id: cscsend,v 1.18 2000/04/12 16:44:41 muhri Exp $
use vars qw( %prefs $libpath );

use strict;

BEGIN { 
 $libpath = $0;
 $libpath =~ s!/[^/]+$!!;
 $libpath =~ s!/bin$!/lib/cscmail!;
 if (! -e $libpath) { die "Can't find required files in $libpath"; };
}

use lib "$libpath";
use lib "$libpath/modules/lib/perl5/site_perl/5.005";

require "csclib.pl";

#if copy of cscsend running then stop
if (open(TMP,"<$prefs{'MailDir'}/cscsend.running")) { 

printf("CSCsend is already running. if this is not true, please delete\n
         cscsend.running in $prefs{'MailDir'}\n");
	 
exit; }
  
#touch lock file
open(TMP,">$prefs{'MailDir'}/cscsend.running");
print TMP "1\n";
close(TMP);

use IO::Socket;
use Sys::Hostname;
use Text::ParseWords;

my ($conn, $sql, $query, @row, $sql2, $query2, $smtp, @test, $tmp, @body, $sender, $msg, $mailline);
my $crlf="\r\n";

&read_prefs;

$conn=&open_db_conn;

$sql = "select id, accountid, sentto, sentfrom, subject, replyto, cc from messages where boxid = ?";
$query = $conn->prepare($sql);
$query->execute(2);
while (@row=$query->fetchrow_array()) {
	my ($msgid, $acctid, $to, $from, $subj, $reply, $cc) = @row;
	$sql2 = "select smtp from accounts where id = ?";
	$query2=$conn->prepare($sql2);
	$query2->execute($acctid);
	($smtp) = $query2->fetchrow_array();
 	@test = gethostbyname($smtp) or next;
	$tmp = filename_to_tree("$prefs{'MailDir'}/$msgid", 1);
	open (TMP, "<$tmp");
	@body = <TMP>;
	close(TMP);

	# open connection to the smtp server
	$sender=IO::Socket::INET->new( PeerAddr => $smtp,
 					PeerPort => 'smtp(25)',
					Proto => 'tcp',
		        		Timeout=>60);
	$msg=$sender->getline();
	if($msg=~/^[45](.*)/){
		print "$smtp error: smtp error $1\n";
		next;
	}
 
	# send helo
	$sender->send("helo ".hostname().$crlf);
	$msg=$sender->getline();
	if($msg=~/^[45](.*)/){
		print "$smtp error: smtp error $1\n";
		next;
	}
 
	# send mail from
	$_=$from;
	if (/\<(.*)\>/){
		$from="<$1>";
	} else {
		$from="<".$from.">";
	}
	$from=~s/ //g;

	$sender->send("mail from: ".$from.$crlf);
	$msg=$sender->getline();
	if($msg=~/^[45](.*)/){
	 	print "$smtp error: smtp error $1\n";
		next;
	}
 # send rcpt to for each mail adress
	foreach (quotewords(", ", 1, $to), quotewords(", ", 1, $cc)){
		#pull <blah@foo.org> from address, or assume address is in blah@foo.org format and just <> it (ie: if you set address as "Kordik, Steven" <stevek@voila.net> this will pull the <stevek@voila.net> out.. if you set address as stevek@voila.net it will just use that, wrapping it in <>) Obviously, this breaks if the To: line contains invalid addresses... but hey.. we should check for that in cscmail send function and popup an &err_dialog() to warn... 
		if (/\<(.*)\>/){
			$_="<$1>";
		} else {
			$_="<$_>";
		}
		s/ //g;
		$sender->send("rcpt to: ".$_.$crlf);
 		$msg=$sender->getline();
 		if($msg=~/^[45](.*)/){
 			print "$smtp error: smtp error $1\n";
			next;
 		}
	}

	# print beginning of data
	$sender->send("data".$crlf);
	$msg=$sender->getline();
	if($msg=~/^[45](.*)/){
	 	print "$smtp error: smtp error $1\n";
		next;
	}
 
 # send the body
	foreach $mailline (@body){
 		 $mailline=~s/\n/$crlf/g;
		 $sender->send($mailline);
	}
 
	# print data end (a dot)
	$sender->send($crlf.".".$crlf);
	$msg=$sender->getline();
	if($msg=~/^[45](.*)/){
	 	print "$smtp error: smtp error $1\n";
		next;
	}

	$sender->send("quit".$crlf);
	$sender->close();
	$sql2 = "update messages set boxid=3 where id = ?";
	$query2=$conn->prepare($sql2);
	$query2->execute($msgid);
	open(TMP,">$prefs{'MailDir'}/.cscmail.refresh");
	print TMP ("1\n");
	close(TMP);
}

unlink("$prefs{'MailDir'}/cscsend.running");
if ($query) {$query->finish;}
if ($query2) {$query2->finish;}
$conn->disconnect;
