From: Jose Escobar on
I have this code in perl to send SMS via SMPP

Can somebody help me to translated to Ruby

I try the ruby-SMPP but it doesnt work well and i need it deploy it on
SUNOS 3

Please help

#!/usr/bin/perl -w

use IO::Socket;

$SMSCIP = 'localhost';
$SMSCPORT = '9988';
$SYSID = 'user';
$SYSTYPE = 'smpp';
$PWD = 'password';

$LSYSID = length($SYSID);
$LSYSID++;
$LSYSTYPE = length($SYSTYPE);
$LSYSTYPE++;
$LPWD = length($PWD);
$LPWD++;

$SYSID = pack("a$LSYSID",$SYSID);
$SYSTYPE = pack("a$LSYSTYPE",$SYSTYPE);
$PWD = pack("a$LPWD",$PWD);
$ADDR_TON = pack("C", 0);
$ADDR_NPI = pack("C", 0);
$INT_VERS = pack("C", 52);
$ADDR_RANG = pack("C", 0);

@SubmitSM_resp = ();
$phonenumber = $ARGV[0];
$message = $ARGV[1];
$origin = $ARGV[2];

my $sock = new IO::Socket::INET (
PeerAddr => $SMSCIP,
PeerPort => $SMSCPORT,
Proto => 'tcp',
); die "Could not create socket: $!\n" unless $sock;

$sock->autoflush(1);

########### START SEQUENCE ##############

$flag = 0;
&SendBindTransmit;
$RBTStat = &ReadBindTransmit;
if ($RBTStat ne "00000000") {
# print "Bind transmitter OK\n";
$flag = 1;
print "Bind transmitter response returned status code $RBTStat for
$phonenumber\n";
}
&SendSubmitSM($phonenumber, $message, $origin);
@SubmitSM_resp = &ReadSubmitSM;
if ($SubmitSM_resp[0] ne "00000000") {
# print "Submit message OK, message ID is $SubmitSM_resp[1]\n";
$flag = 1;
print "Submit message response returned status code $SubmitSM_resp[0]
for $phonenumber\n";
}
&SendUnbind;
$RUBStat = &ReadUnbind;
if ($RUBStat ne "00000000") {
# print "Unbind OK\n";
$flag = 1;
print "Unbind response returned status code $RUBStat for
$phonenumber\n";
}

if ($flag == 0) {
print "Message: $message\nSent successfully to: $phonenumber\n"
}
else {
print "Message unsuccessfully sent to $phonenumber\n"
}


########### END SEQUENCE ################

close($sock);


sub SendBindTransmit {
my $seq = 1;
my $cst = 0;
my $cln = 16;
my $cid = "00000002";
my $PDU_L = 20 + $LSYSID + $LSYSTYPE + $LPWD;
my $PDU_header = pack("NNNN", $PDU_L, $cid, $cst, $seq);

my $PDU = $PDU_header . $SYSID . $PWD . $SYSTYPE . $INT_VERS .
$ADDR_TON . $ADDR_NPI . $ADDR_RANG;
print $sock $PDU;
}


sub SendSubmitSM {
my $seq = 1;
my $cst = 0;
my $cln = 16;
my $cid = "00000004";
my $sertype = "\0";
my $s_addr_ton = pack("H2","05");
my $s_addr_npi = pack("H2","01");
my $s_addr = "$_[2]\0";
my $d_addr_ton = pack("H2","01");
my $d_addr_npi = pack("H2","01");
my $d_addr = $_[0] . "\0";
my $esm_class = pack("H2","01");
my $proto_id = pack("H2","00");
my $priority_flg = pack("H2","00");
my $sch_del_time = pack("H2","00");
my $val_period = pack("H2","00");
my $reg_del = pack("H2","00");
my $replace_ifpres = pack("H2","00");
my $data_coding = pack("H2","00");
my $sm_def_msgid = pack("H2","00");
my $mesg = $_[1];
my $mesg_l = length($mesg);

my $PDU_L = 28 + length($sertype) + length($s_addr) + length($d_addr)
+ length($val_period) + length($sch_del_time) + $mesg_l;
$mesg_l = pack("C",$mesg_l);
my $PDU_header = pack("NNNN", $PDU_L, $cid, $cst, $seq);
my $PDU = $PDU_header . $sertype . $s_addr_ton . $s_addr_npi . $s_addr
$d_addr_ton . $d_addr_npi . $d_addr . $esm_class . $proto_id .
$priority_flg . $sch_del_time . $val_period . $reg_del . $replace_ifpres
$data_coding . $sm_def_msgid . $mesg_l . $mesg;
print $sock $PDU;
}

sub ReadSubmitSM {
my $byte;
my $x = 0;
my $seq;
my $cst;
my $cln;
my $cid;
my $curs = 16;
my $msg_id;

while ($x < 4) {
sysread($sock, $byte, 4);
if ($x == 0) {
$cln = unpack('L',$byte);
# print 'PDU length: ',$cln," x:$x","\n";
}
elsif ($x == 1) {
$cid = unpack('H8',$byte);
# print 'Command ID: ',$cid," x:$x","\n";
}
elsif ($x == 2) {
$cst = unpack('H8',$byte);
# print 'Status of ReadSubmitSM: ',$cst," x:$x","\n";
}
elsif ($x == 3) {
$seq = unpack('L',$byte);
# print 'Sequence number: ',$seq," x:$x","\n";
}
else {
print 'Something wrong with subroutine ReadSubmitSM!, x=',"$x\n";
}
$x++;
}
if ($curs < $cln) {
$msg_id = GetCstring($sock);
}
return $cst, $msg_id;
}

sub ReadBindTransmit {
my $byte;
my $x = 0;
my $seq;
my $cst;
my $cln;
my $cid;
my $curs = 16;
my $sys_id;
my $ifver = "NA";

while ($x < 4) {
sysread($sock, $byte, 4);
if ($x == 0) {
$cln = unpack('L',$byte);
# print 'PDU length: ',$cln," x:$x","\n";
}
elsif ($x == 1) {
$cid = unpack('H8',$byte);
# print 'Command ID: ',$cid," x:$x","\n";
}
elsif ($x == 2) {
$cst = unpack('H8',$byte);
# print 'Status of ReadBindTransmit: ',$cst," x:$x","\n";
}
elsif ($x == 3) {
$seq = unpack('L',$byte);
# print 'Sequence number: ',$seq," x:$x","\n";
}
else {
print 'Something wrong with subroutine ReadBindTransmit!,
x=',"$x\n";
}
$x++;
}
if ($curs < $cln) {
$sys_id = GetCstring($sock);
$curs = $curs + length($sys_id);
}
if ($curs < $cln) {
$ifver = GetCstring($sock);
}
return $cst;
}


sub GetCstring {
my $chr = "";
my $SCK = $_[0];
my $cstr = "";

while ($chr ne "\0") {
$chr = getc($SCK);
$cstr = $cstr . $chr;
}
return $cstr;
}



sub SendUnbind {
my $seq = 1;
my $cst = 0;
my $cln = 16;
my $cid = "00000006";

print $sock pack("NNNN", $cln, $cid, $cst, $seq);
}

sub ReadUnbind {
my $byte;
my $x = 0;
my $y = 0;
my $cln;
my $cid;
my $cst;
my $seq;

while ($x < 4) {
sysread($sock, $byte, 4);
if ($x == 0) {
$cln = unpack('L',$byte);
# print 'PDU length: ',$cln," x:$x","\n";
}
elsif ($x == 1) {
$cid = unpack('H8',$byte);
# print 'Command ID: ',$cid," x:$x","\n";
}
elsif ($x == 2) {
$cst = unpack('H8',$byte);
# print 'Status of ReadBindTransmit: ',$cst," x:$x","\n";
}
elsif ($x == 3) {
$seq = unpack('L',$byte);
# print 'Sequence number: ',$seq," x:$x","\n";
}
else {
print 'Something wrong with subroutine ReadUnbind!, x=',"$x\n";
}
$x++;
}
return $cst;
}
--
Posted via http://www.ruby-forum.com/.