Browse Source

SMS - widespread cleanup and moving code away from Cyprus/Turkey-specific implementation. Much more needed.

Signed-off-by: Chris Cormack <crc@liblime.com>
Signed-off-by: Joshua Ferraro <jmf@liblime.com>
3.0.x
Joe Atzberger 15 years ago
committed by Joshua Ferraro
parent
commit
9c8a1e29f8
  1. 191
      C4/SMS.pm
  2. 45
      sms/sms.pl
  3. 125
      sms/sms_listen.pl
  4. 4
      sms/sms_listen_windows_start.pl

191
C4/SMS.pm

@ -2,18 +2,17 @@ package C4::SMS;
#Written by tgarip@neu.edu.tr for SMS message sending and other SMS related services
use strict;
use warnings;
use LWP::UserAgent;
use C4::Context;
use vars qw($VERSION @ISA @EXPORT);
my $user=C4::Context->config('smsuser');
my $pwd=C4::Context->config('smspass');
my $uri ="https://spgw.kktcell.com/smshttpproxy/SmsHttpProxyServlet";
use vars qw($VERSION @ISA @EXPORT);
BEGIN {
require Exporter;
@ISA = qw(Exporter);
$VERSION = 0.02;
$VERSION = 0.03;
@EXPORT = qw(
&get_sms_auth
&send_sms
@ -27,123 +26,115 @@ BEGIN {
);
}
our $user = C4::Context->config('smsuser');
our $pwd = C4::Context->config('smspass');
our $uri = "https://spgw.kktcell.com/smshttpproxy/SmsHttpProxyServlet";
sub get_sms_auth {
my $ua = LWP::UserAgent->new;
my $commands;
my $res=$ua->post($uri,[cmd=>'REGISTER',pUser=>$user,pPwd=>$pwd]);
my $ua = LWP::UserAgent->new;
my $commands;
my $res=$ua->post($uri,[cmd=>'REGISTER',pUser=>$user,pPwd=>$pwd]);
if ($res->is_success){
$commands=parse_content($res->content);
$commands=parse_content($res->content);
}
return($commands,$ua);
return($commands,$ua);
}
sub send_sms{
my $ua=shift;
my $phone=shift;
my $message=shift;
my $session=shift;
my $res=$ua->post($uri,[cmd=>'SENDSMS',pUser=>$user,pPwd=>$pwd,pSessionId=>$session,pService_Code=>4130,pMsisdn=>$phone,
sub send_sms {
my $ua = shift or return undef;
my $phone=shift;
my $message=shift;
my $session=shift;
my $res=$ua->post($uri,[cmd=>'SENDSMS',pUser=>$user,pPwd=>$pwd,pSessionId=>$session,pService_Code=>4130,pMsisdn=>$phone,
pContent=>$message]);
return parse_content($res->content);
return parse_content($res->content);
}
sub read_sms{
my $ua=shift;
my $session=shift;
my $res=$ua->post($uri,[cmd=>'GETSMS',pUser=>$user,pPwd=>$pwd,pSessionId=>$session,pService_Code=>4130]);
return parse_content($res->content);
sub read_sms {
my $ua = shift or return undef;
my $session=shift;
my $res=$ua->post($uri,[cmd=>'GETSMS',pUser=>$user,pPwd=>$pwd,pSessionId=>$session,pService_Code=>4130]);
return parse_content($res->content);
}
sub parse_content{
my $content=shift;
my %commands;
my @attributes=split /&/,$content;
sub parse_content {
my $content = shift;
my %commands;
my @attributes = split /&/,$content;
foreach my $params(@attributes){
my (@param)=split /=/,$params;
$commands{$param[0]}=$param[1];
my (@param) = split /=/,$params;
$commands{$param[0]}=$param[1];
}
return(\%commands);
return(\%commands);
}
sub error_codes{
my $error=shift;
if ($error==-1){
return "Closed session - Retry ";
}elsif($error==-2){
return "Invalid session - Retry ";
}elsif($error==-3){
return "Invalid password" ;
}elsif($error==-103){
return "Invalid user";
}elsif($error==-422){
return "Invalid Parameter";
}elsif($error==-426){
return "User doesn’t have permission to send message";
}elsif($error==-700){
return "No permission";
}elsif($error==-801){
return " Msdisn count differs-warn administartor";
}elsif($error==-803){
return "Content count differs from XSER count";
}elsif($error==-1101){
return " Insufficient Credit Do not retry" ;
}elsif($error==-1104){
return "Invalid Phone number";
}elsif($error==-10001){
return " Internal system error- Tell Turkcell/Telsim";
}elsif($error==-9005){
return " No messages to read";
}elsif ($error){
return "Unknow error no $error occured - tell Turkcell/Telsim";
}
sub error_codes {
my $error = shift;
($error== -1) and return "Closed session - Retry";
($error== -2) and return "Invalid session - Retry";
($error== -3) and return "Invalid password";
($error== -103) and return "Invalid user";
($error== -422) and return "Invalid Parameter";
($error== -426) and return "User does not have permission to send message";
($error== -700) and return "No permission";
($error== -801) and return "Msdisn count differs - warn administartor";
($error== -803) and return "Content count differs from XSER count";
($error== -1101) and return "Insufficient Credit - Do not retry";
($error== -1104) and return "Invalid Phone number";
($error==-10001) and return "Internal system error - Notify provider";
($error== -9005) and return "No messages to read";
if ($error){
warn "Unknown SMS error '$error' occured";
return "Unknown SMS error '$error' occured";
}
}
sub parse_phone{
## checks acceptable phone numbers
## Fix to accept Telsim when available (542 numbers)
my $phone=shift;
$phone=~s/^0//g;
$phone=~s/ //g;
my $length=length($phone);
if ($length==10 || $length==12){
my $code=substr($phone,0,3) if $length==10;
$code=substr($phone,0,5) if $length==12;
if ($code=~/533/){
return $phone;
}else{
return 0;
sub parse_phone {
## checks acceptable phone numbers
## FIXME: accept Telsim when available (542 numbers)
my $phone=shift;
$phone=~s/^0//g;
$phone=~s/ //g;
my $length=length($phone);
if ($length==10 || $length==12){
my $code=substr($phone,0,3) if $length==10;
$code=substr($phone,0,5) if $length==12;
if ($code=~/533/){
return $phone;
}
}
}else{
return 0;
}
return 0;
}
sub parse_message{
my $message=shift;
$message=~s/ / /g;
my @parsed=split / /,$message;
return (@parsed);
sub parse_message {
my $message = shift;
$message =~ s/ / /g;
my @parsed = split / /, $message;
return (@parsed);
}
sub write_sms{
my ($userid,$message,$phone)=@_;
my $dbh=C4::Context->dbh;
my $sth=$dbh->prepare("INSERT into sms_messages(userid,message,user_phone,date_received) values(?,?,?,now())");
$sth->execute($userid,$message,$phone);
$sth->finish;
return $dbh->{'mysql_insertid'};
sub write_sms {
my ($userid,$message,$phone)=@_;
my $dbh=C4::Context->dbh;
my $sth=$dbh->prepare("INSERT into sms_messages(userid,message,user_phone,date_received) values(?,?,?,now())");
$sth->execute($userid,$message,$phone);
$sth->finish;
return $dbh->{'mysql_insertid'}; # FIXME: mysql specific
}
sub mod_sms{
my ($smsid,$message)=@_;
my $dbh=C4::Context->dbh;
my $sth=$dbh->prepare("UPDATE sms_messages set reply=? ,date_replied=now() where smsid=?");
$sth->execute($message,$smsid);
$sth->finish;
sub mod_sms {
my ($smsid,$message)=@_;
my $dbh=C4::Context->dbh;
my $sth=$dbh->prepare("UPDATE sms_messages set reply=?, date_replied=now() where smsid=?");
$sth->execute($message,$smsid);
}
sub kill_sms{
#end a session
my $ua=shift;
my $session=shift;
my $res=$ua->post($uri,[cmd=>'KILLSESSION',pSessionId=>$session]);
sub kill_sms {
#end a session
my $ua = shift or return undef;
my $session = shift;
my $res = $ua->post($uri,[cmd=>'KILLSESSION',pSessionId=>$session]);
}
1;
__END__

45
sms/sms.pl

@ -5,39 +5,32 @@ use CGI;
use C4::SMS;
use C4::Output;
use C4::Auth;
my ($res,$ua);
my %commands;
my $query = new CGI;
my $message=$query->param('message');
my $phone=$query->param('phone');
my $operation=$query->param('operation');
my $message = $query->param( 'message' );
my $phone = $query->param( 'phone' );
my $operation = $query->param('operation');
my $result;
my $errorcode;
my ($template, $loggedinuser, $cookie)
= get_template_and_user({template_name => "sms/sms-home.tmpl",
query => $query,
type => "intranet",
authnotrequired => 0,
flagsrequired => {circulate => 1},
debug => 1,
});
if ($operation eq"sendsms"){
$phone=parse_phone($phone);
if ($phone>0){
##write to a queue and exit
my $me=C4::Context->userenv;
my $card=$me->{cardnumber};
$result=write_sms($card,$message,$phone);
}else{
$errorcode=-1104;
}
query => $query,
type => "intranet",
authnotrequired => 0,
flagsrequired => {circulate => 1},
debug => 1,
});
if ($operation eq "sendsms"){
$phone=parse_phone($phone);
if ($phone){
##write to a queue and exit
my $me=C4::Context->userenv;
$result=write_sms($me->{cardnumber},$message,$phone);
} else {
$errorcode=-1104;
}
}
my $error=error_codes($errorcode);
$template->param(error=>$error);
output_html_with_http_headers $query, $cookie, $template->output;

125
sms/sms_listen.pl

@ -1,11 +1,14 @@
#!/usr/bin/perl
use strict;
use warnings;
use C4::SMS;
use C4::Auth;
use C4::Context;
use C4::Members;
use C4::Circulation;
my ($res,$ua);
my %commands;
my $message;
@ -20,111 +23,103 @@ STARTAGAIN:
($res,$ua)=get_sms_auth();
AGAIN:
$errorcode=0;
if ($res->{pRetCode}==200){
$result=read_sms($ua,$res->{pSessionId});
if ($res->{pRetCode}==200){
$result=read_sms($ua,$res->{pSessionId});
$errorcode=$result->{pErrCode};
print "connected\n";
}else{
print "connected\n";
} else {
kill_sms($ua,$res->{pSessionId});
warn (error_codes($res->{pErrCode}),$res->{pErrcode}) ;
# sleep $wait;
goto FINISH;
}
}
if ($errorcode && $errorcode !=-9005){
kill_sms($ua,$res->{pSessionId});
warn error_codes($errorcode) ;
# sleep $wait;
goto FINISH;
}elsif ($errorcode ==-9005){
print "no more messages to read\n";
goto WAITING;
kill_sms($ua,$res->{pSessionId});
warn error_codes($errorcode) ;
# sleep $wait;
goto FINISH;
} elsif ($errorcode ==-9005){
print "no more messages to read\n";
goto WAITING;
}
#Parse the message to a useful hash
my @action=parse_message( $result->{pContent});
## Log the request in our database;
$smsid=write_sms($action[1], $result->{pContent},$result->{pMsisdn});
$smsid=write_sms($action[1], $result->{pContent},$result->{pMsisdn});
print "message logged\n";
##Now do the service required
if (uc($action[0]) eq "RN"){
print "dealing request\n";
my ($ok,$cardnumber)=C4::Auth::checkpw($dbh,$action[1],$action[2]);
if($ok){
print "dealing request\n";
my ($ok,$cardnumber)=C4::Auth::checkpw($dbh,$action[1],$action[2]);
unless ($ok) {
##wrong user/pass
$message="Yanlis kullanici/sifre! :Wrong username/password!";
my $send=send_message($result,$message,$smsid);
goto AGAIN;
}
my $item=getiteminformation(undef,0,$action[3]);
if ($item){
if ($item){
my $borrower=getmember($cardnumber);
my $status=renewstatus(undef,$borrower->{borrowernumber},$item->{itemnumber});
if ($status==1){
my $date=renewbook(undef,$borrower->{borrowernumber},$item->{itemnumber});
$message="Uzatildi :Renewed ".$item->{barcode}." : ".$date;
my $send=send_message($result,$message,$smsid);
}elsif($status==2){
$message="Cok erken- yenilenmedi! :Too early-not renewed:".$item->{barcode};
my $send=send_message($result,$message,$smsid);
}elsif($status==3){
$message="Uzatamazsiniz GERI getiriniz! :No more renewals RETURN the item:".$item->{barcode};
my $send=send_message($result,$message,$smsid);
}elsif($status==4){
$message="Ayirtildi GERI getiriniz! :Reserved RETURN the item:".$item->{barcode};
my $send=send_message($result,$message,$smsid);
}elsif($status==0){
$message="Uzatilamaz! :Can not renew:".$item->{barcode};
my $send=send_message($result,$message,$smsid);
if ($status==1) {
my $date=renewbook(undef,$borrower->{borrowernumber},$item->{itemnumber});
$message="Uzatildi :Renewed ".$item->{barcode}." : ".$date;
} elsif($status==2) {
$message="Cok erken- yenilenmedi! :Too early-not renewed:".$item->{barcode};
} elsif($status==3) {
$message="Uzatamazsiniz GERI getiriniz! :No more renewals RETURN the item:".$item->{barcode};
} elsif($status==4) {
$message="Ayirtildi GERI getiriniz! :Reserved RETURN the item:".$item->{barcode};
} elsif($status==0) {
$message="Uzatilamaz! :Can not renew:".$item->{barcode};
}
}else{
} else {
$message="Yanlis barkot! :Wrong barcode!";
my $send=send_message($result,$message,$smsid);
}#wrong barcode
}else{
$message="Yanlis kullanici/sifre! :Wrong username/password!";
my $send=send_message($result,$message,$smsid);
}##wrong user/pass
}else{
## reply about error
$message="Yanlis mesaj formati! :Wrong message! :
}
} else {
## reply about error
$message="Yanlis mesaj formati! :Wrong message! :
RN usercardno password barcode";
my $send=send_message($result,$message,$smsid);
}### wrong service
} ### wrong service
send_message($result,$message,$smsid);
goto AGAIN;
WAITING:
##Now send the messages waiting in queue
my $smssth=$dbh->prepare("SELECT smsid,user_phone,message from sms_messages where date_replied like '0000-00-00%' ");
$smssth->execute();
my @phones;
while (my $data=$smssth->fetchrow_hashref){
push @phones,$data;
push @phones,$data;
}
$smssth->finish;
foreach my $user(@phones){
print "replying $user->{user_phone}";
my $send=send_sms($ua,$user->{user_phone},$user->{message},$res->{pSessionId});
my $reply="--failed\n";
print "replying $user->{user_phone}";
my $send=send_sms($ua,$user->{user_phone},$user->{message},$res->{pSessionId});
my $reply="--failed\n";
if ($send->{pRetCode}==200){
$reply= "--replied\n";
mod_sms($user->{smsid},"Sent");
$reply= "--replied\n";
mod_sms($user->{smsid},"Sent");
}
print $reply;
print $reply;
}
$dbh->disconnect;
sub send_message{
my ($mes,$message,$smsid)=@_;
my $send=send_sms($ua,$mes->{pMsisdn},$message,$res->{pSessionId});
sub send_message {
my ($mes,$message,$smsid)=@_;
my $send=send_sms($ua,$mes->{pMsisdn},$message,$res->{pSessionId});
if ($send->{pRetCode}==200){
mod_sms($smsid,$message);
}else{
my $error=error_codes($send->{pErrCode});
mod_sms($smsid,"Not replied error:".$error);
mod_sms($smsid,$message);
} else {
my $error=error_codes($send->{pErrCode});
mod_sms($smsid,"Not replied error:".$error);
}
return $send;
return $send;
}
FINISH:
1;

4
sms/sms_listen_windows_start.pl

@ -1,5 +1,5 @@
#!/usr/bin/perl
# script that starts the zebraquee
# script that starts the zebraqueue
# Written by TG on 01/08/2006
use strict;
@ -11,6 +11,6 @@ my $input=new CGI;
my $fileplace=C4::Context->config('intranetdir');
my $fullpath=$fileplace."/cgi-bin/sms";
my $ZebraObj;
my $pid=Win32::Process::Create($ZebraObj, "C:/perl/bin/perl.exe",'perl sms_listen.pl', 0, DETACHED_PROCESS,$fullpath) ;
my $pid=Win32::Process::Create($ZebraObj, "C:/perl/bin/perl.exe", 'perl sms_listen.pl', 0, DETACHED_PROCESS, $fullpath);
print $input->redirect("/cgi-bin/koha/mainpage.pl?pid=$pid");

Loading…
Cancel
Save