#!/usr/bin/perl

use strict;
use warnings;
use File::Tail;
use Mail::Sender;
use diagnostics;
use DBI;

# sms to email and receivable through your phone for those without email address.
# sms is sent to  a short code eg. 999999.
# sms is received from 234992019122
# the access.log is passed and the following, recepient is extracted.
# 2008-06-26 13:19:25 Receive SMS [SMSC:xaxton] [SVC:] [ACT:] [BINF:] [from:+234992019119] [to:+234999191] [flags:-1:0:-1:0:-1] [msg:17:Hi this is a test] [udh:0:]
# #2008-06-26 13:19:25 SMS request sender:+234992019119 request: 'Hi this is a test' file answer: ''
#2008-06-26 13:23:50 Receive SMS [SMSC:xaxton] [SVC:] [ACT:] [BINF:] [from:+234992019122] [to:+234999191] [flags:-1:0:-1:0:-1] [msg:11:knottttslll] [udh:0:]
#
#
# need to run database query to fetch $sendee;
my $bulksmsurl = 'localhost';
# setup database connection to retrieve the phone number for sending alert.
 my ($userid, $recvd_to);
 my $dbuser='mailadm';
 my $dbpass='uarenotconcerned';
 my $dbport='3306';
 my $dbname="$bulksmsurl";
 my $dsource="dbi:mysql:fone2maildb:$dbname";
 my $dbh = DBI->connect( $dsource, $dbuser, $dbpass ) || die "Couldn't connect to asterdb:$!\n";
#for subscription
 my $loginsql = "select userid from fonealias where telno = ? limit 1";
 my $sth = $dbh->prepare($loginsql);

 sub sendee_mail {
 my $telno = shift;
 $sth->execute($telno) or die("Couldn't exec $sth!");
 my $sender_mail=$sth->fetchrow_array;
 return $sender_mail;
 }
#print sendee_mail($sendee);

my $name = "/var/log/bulksms/bulksms_access.log";
my ($mailreci, $mailsubj, @sms, $mailmsg, $mailsend, $sendee, $sender, $msg, $domain);
$domain = '@onet-ng.com';
open my $file, '<', $name or die "could not open $name: $!";
$file=File::Tail->new(name=>$name, maxinterval=>3, adjustafter=>5);
while (defined($_=$file->read))
	        {
        @sms = split/\[/;
        next if $sms[8]=~ m/#99999#/g;
        next unless $sms[6]=~ m/to:\+234999191\]/;
	$sendee = $sms[5];
        $sendee =~ s/from:\+(\d+)\]\s+/$1/;
#	$sendee i.= $domain;
	my $sendee1 = sendee_mail($sendee);
        $msg = $sms[8];
        $msg = (split/:/, $msg)[-1];
        $msg =~ s/(\w+)\s?\]/$1/;
#       i need only sender and $msg
        ($mailreci, $mailsubj, $mailmsg) = (split/,/, $msg, 3)[0..2];
	my $invexcl = "\x{00A1}";
	my $atsign = "\x{0040}";
	$mailreci =~ s/(\w+)$invexcl(\w+)/$1$atsign$2/g;
	print "$sendee1\n";
#       $sendee1 =~ s/(\w+)$invexcl(\w+)/$1$atsign$2/g;
	print  "To: $mailreci Subject: $mailsubj Message: $mailmsg sender : $sendee\n";
        	 
	$sender = new Mail::Sender {
                smtp => 'mail1.xxx-ng.com',
                from => "$sendee1",
                on_errors => undef,
        } or print "Can't create the Mail::Sender object: $Mail::Sender::Error\n";
        $sender->Open({
                to => "$mailreci",
        #        cc => "$mailreci",
                subject => "$mailsubj"
        }) or print "Can't open the message: $sender->{'error_msg'}\n";
        $sender->SendLineEnc("$mailmsg");
           $sender->Close() or print "Failed to send the message: $sender->{'error_msg'}\n";	
        
 }	
