X-Git-Url: http://koha-dev.rot13.org:8081/gitweb/?a=blobdiff_plain;f=C4%2FSMS.pm;h=35d687b33e5c0d19be0998cc2bc62e3dc1824370;hb=34a27e01b138170f38324568a9d4fad596176ff8;hp=319ac33ff4525f923c4f3680847cca4d7833f576;hpb=76064f46232b9dc64da861dccf320be855fdd22c;p=koha_fer diff --git a/C4/SMS.pm b/C4/SMS.pm index 319ac33ff4..35d687b33e 100644 --- a/C4/SMS.pm +++ b/C4/SMS.pm @@ -1,149 +1,106 @@ package C4::SMS; -#Written by tgarip@neu.edu.tr for SMS message sending and other SMS related services + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 2 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along with +# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place, +# Suite 330, Boston, MA 02111-1307 USA + +=head1 NAME + +C4::SMS - send SMS messages + +=head1 SYNOPSIS + +my $success = C4::SMS->send_sms( message => 'This is my text message', + destination => '212-555-1212' ); + +=head1 DESCRIPTION + + + +=cut use strict; -require Exporter; -use LWP::UserAgent; +use warnings; + use C4::Context; -use vars qw($VERSION @ISA @EXPORT); -$VERSION = 0.01; -my $user=C4::Context->config('smsuser'); -my $pwd=C4::Context->config('smspass'); -my $uri ="https://spgw.kktcell.com/smshttpproxy/SmsHttpProxyServlet"; - - - -@ISA = qw(Exporter); - -@EXPORT = qw( -&get_sms_auth -&send_sms -&read_sms -&error_codes -&parse_phone -&parse_message -&write_sms -&mod_sms -&kill_sms -); - -sub get_sms_auth { - 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); - } -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, - pContent=>$message]); -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 parse_content{ -my $content=shift; -my %commands; -my @attributes=split /&/,$content; - foreach my $params(@attributes){ - my (@param)=split /=/,$params; - $commands{$param[0]}=$param[1]; - } -return(\%commands); -} +use vars qw( $VERSION ); -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"; -} +BEGIN { + $VERSION = 0.03; } -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; - } -}else{ -return 0; -} -} +=head1 METHODS -sub parse_message{ -my $message=shift; -$message=~s/ / /g; -my @parsed=split / /,$message; -return (@parsed); -} +=cut -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'}; -} +# The previous implmentation used username and password. +# our $user = C4::Context->config('smsuser'); +# our $pwd = C4::Context->config('smspass'); + +=head2 send_sms + +=cut + +sub send_sms { + my $self = shift; + my $params= shift; + + foreach my $required_parameter ( qw( message destination ) ) { + # Should I warn in some way? + return unless defined $params->{ $required_parameter }; + } + + eval { require SMS::Send; }; + if ( $@ ) { + # we apparently don't have SMS::Send. Return a failure. + return; + } + + # This allows the user to override the driver. See SMS::Send::Test + my $driver = exists $params->{'driver'} ? $params->{'driver'} : $self->driver(); + return unless $driver; -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; + # warn "using driver: $driver to send message to $params->{'destination'}"; + + # Create a sender + my $sender = SMS::Send->new( $driver, + _login => C4::Context->preference('SMSSendUsername'), + _password => C4::Context->preference('SMSSendPassword'), + ); + + # Send a message + my $sent = $sender->send_sms( to => $params->{'destination'}, + text => $params->{'message'}, + ); + # warn 'failure' unless $sent; + return $sent; } -sub kill_sms{ -#end a session -my $ua=shift; -my $session=shift; - my $res=$ua->post($uri,[cmd=>'KILLSESSION',pSessionId=>$session]); + +=head2 driver + +=cut + +sub driver { + my $self = shift; + + # return 'US::SprintPCS'; + return C4::Context->preference('SMSSendDriver'); + } + 1; -__END__ \ No newline at end of file + +__END__ +