#!/usr/bin/perl
use Net::Telnet ();
use POSIX qw(strftime);
$dt_string = strftime "%Y%m%d", localtime;
$tm_string = strftime "%H:%M:%S", localtime;
$drv = "d:\\";
$flname = "_xxxsms.log";
$logfile = "$drv$dt_string$flname";
# Let us create a decent Perl log file to which we can log debugging information
open (perl_log_file, ">>$logfile");
# Let us lock the file
flock perl_log_file, 2;
print perl_log_file "####################################################\n" ;
# Let us start logging to our perl log file
print perl_log_file "Starting xxxsmsemail at $tm_string $dt_string and using log file - $logfile...\n" ;
print perl_log_file "xxxsmsemail Now about to send email ..\n";
`pdm_mail -m`;
print perl_log_file "xxxsmsemail executed pdm_mail and so in theory sent the email...\n";
(@message) = @ARGV;
$TEXTS = $message[0];
# Let us pause here for a few minutes while we wait for service desk to create the file passed onto us
unless (-e $TEXTS && -f $TEXTS) {
print perl_log_file "We now sleeping for 300sec or 5 minutes while waiting for service desk to create our notification file $TEXTS ..\n";
sleep 300;
}
open(TEXTS) or die("Could not open notification file $TEXTS.");
my $skline;
my $objtype;
my $lnum = 1;
my $calltype = "";
while( ($skline = <TEXTS>) && $lnum < 3){
chomp($skline);
print perl_log_file "$lnum: $skline\n";
if ($lnum == 2) {$objtype = substr($skline,8)};
$lnum++;
}
close(TEXTS);
print perl_log_file "Object type from first section (line 2 in file $TEXTS) is: $objtype\n";
open(TEXTS) or die("Could not open notification file $TEXTS.");
my $skline2;
my $cell;
my $work;
my $locn;
my $skval="null";
foreach $skline2 (<TEXTS>) {
($fhead, $skval) = split(':',$skline2);
#chop($skval);
# do line-by-line processing.
if ($fhead eq 'CustomerCell') {$cell = $skval};
if ($fhead eq 'CustomerWork') {$work = $skval};
if ($fhead eq 'CustomerLocation') {$locn = $skval};
if ($fhead eq 'CustomerFloor') {$flor = $skval};
if ($fhead eq 'CustomerPillar') {$pilr = $skval};
};
close(TEXTS);
print perl_log_file "found values using the colon split:- cell-$cell work-$work location-$locn floor-$flor pillar-$pilr\n";
open(TEXTS) or die("Could not open notification file $TEXTS.");
foreach $line (<TEXTS>) {
($fhead, $fval) = split('=',$line);
#chop($fval);
# do line-by-line processing.
if ($objtype eq 'cr') {
if ($fhead eq 'NX_NTF_REF_NUM') {$callnum = $fval};
}
if ($objtype eq 'chg') {
if ($fhead eq 'NX_NTF_CHG_REF_NUM') {$callnum = $fval};
}
if ($fhead eq 'NX_NTF_SUMMARY') {$summ = $fval};
if ($fhead eq 'NX_NTF_DESCRIPTION') {$desc = $fval};
if ($fhead eq 'NX_NTF_CATEGORY.SYM') {$catg = $fval};
if ($fhead eq 'NX_NTF_PHONE_NUMBER') {$asgncell = $fval};
if ($fhead eq 'NX_NTF_ASSIGNEE.COMBO_NAME') {$asgnname = $fval};
if ($fhead eq 'NX_NTF_CUSTOMER.COMBO_NAME') {$custname = $fval};
};
close(TEXTS);
$debug0="================== Stripped out Values ==================================
Let us peruse the actual values we got from file $TEXTS :
objtype = $objtype
callnum = $callnum
summary = $summ
desc = $desc
catg= $catg
asgncell = $asgncell
asgnname = $asgnname
custname = $custname
custcell = $cell
custwork# = $work
custloc = $locn
floor = $flor
pillar = $pilr
=======================================================================\n";
print perl_log_file "$debug0";
chomp($desc);
chomp($callnum);
chomp($catg);
chomp($summ);
chomp($cell);
chomp($flor);
chomp($pilr);
chomp($work);
chomp($locn);
chomp($asgncell);
chomp($custname);
chomp($asgnname);
$mil="$callnum $custname $cell $work $summ $locn $flor $pilr $catg $desc";
$transmitter=new Net::Telnet (Timeout => 5, Port => 4321);
$transmitter->open("***.xx.x.***");
# $transmitter->open("***.xx.x.***");
$transmitter->binmode(1);
$transmitter->telnetmode(0);
$transmitter->output_record_separator("");
$bindstring=join("","#0000000000|LOGON|9999|xxxSDESKLIVE|xxxSDESK123LIVE|000000000000000|00000000|0000|","\x0D","\x0A");
# truncate if message is longer then 160 characters
if ( length( $mil ) > 160 ) {
# remove extra characters after 160
$mil = substr( $mil, 0, 160 );
print perl_log_file "The value of dollar mil truncated to 160 is : ####${mil}####\n";
} else {
# pad with spaces upto 160 characters
$start = length( $mil ) + 1;
for ( $t = $start; $t < 161; $t++ ) {
$mil .= " ";
}
print perl_log_file "The value of dollar mil padded to 160 is : ####${mil}####\n";
}
print perl_log_file "About to send sms for $calltype number ###$callnum### to ###${asgnname}### using cellnumber ###${asgncell}### and the message ###${mil}###..\n";
$sms=join("","#",$asgncell,"|",$mil,"|0000|000000000000000|00000000|0000|END|","\x0D","\x0A");
print perl_log_file "xxxsmsemail SMS RC=", $transmitter->print($bindstring) , "\n";
$transmitter->print($sms);
print perl_log_file " transmitter RC=",$transmitter->get(), "\n";
$transmitter->close;
print perl_log_file "Ending xxxsmsemail at $tm_string $dt_string ..\n";
# Let us close the Perl log file to which we logged debugging information
# Unlock the file first which we locked at the beginning after opening it
flock perl_log_file, 8;
close (perl_log_file);