Bug 21206: (QA follow-up) Rebase problem and leftover mocked GetItem
[koha.git] / C4 / SMS.pm
index d7d115f..eff72b3 100644 (file)
--- a/C4/SMS.pm
+++ b/C4/SMS.pm
 package C4::SMS;
-#Written by tgarip@neu.edu.tr for SMS message sending and other SMS related services
+
+# Copyright 2007 Liblime
+# Copyright 2015 Biblibre
+# Copyright 2016 Catalyst
+#
+# 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 3 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, see <http://www.gnu.org/licenses>.
+
+=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
+
+A wrapper for SMS::Send.
+
+Can use a yaml file for config, the path to which is in the koha-conf.xml
+<sms_send_config>__KOHA_CONF_DIR__/sms_send/</sms_send_config>
+
+Each file needs to be in the format of
+__KOHA_CONF_DIR__/sms_send/<driver>.yaml
+
+For example for SMS::Send::UK::Kapow the config would be
+
+/etc/koha/sites/instancename/sms_send/UK/Kapow.yaml for package install
+or
+/etc/koha/sms_send/UK/Kapow.yaml for tarball
+
+A underscore character is prepended to all parameter names so they are
+treated as driver-specific options (leading underscore must not appear
+in config file).
+
+=cut
 
 use strict;
 use warnings;
 
-use LWP::UserAgent;
 use C4::Context;
+use File::Spec;
 
-use vars qw($VERSION @ISA @EXPORT);
-
-BEGIN {
-       require Exporter;
-       @ISA = qw(Exporter);
-       $VERSION = 0.03;
-       @EXPORT = qw(
-               &get_sms_auth 
-               &send_sms 
-               &read_sms
-               &error_codes
-               &parse_phone
-               &parse_message
-               &write_sms
-               &mod_sms
-               &kill_sms
-       );
-}
 
-our $user = C4::Context->config('smsuser');
-our $pwd  = C4::Context->config('smspass');
-our $uri  = "https://spgw.kktcell.com/smshttpproxy/SmsHttpProxyServlet";
 
+=head1 METHODS
 
-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);
-}
+=cut
 
-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);
-}
+# The previous implmentation used username and password.
+# our $user = C4::Context->config('smsuser');
+# our $pwd  = C4::Context->config('smspass');
 
-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);
-}
+=head2 send_sms
 
-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);
-}
+=cut
 
-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 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;
+
+    my ($sent, $sender);
+
+    my $subpath = $driver;
+    $subpath =~ s|::|/|g;
+
+    my $sms_send_config = C4::Context->config('sms_send_config');
+    my $conf_file = defined $sms_send_config
+        ? File::Spec->catfile( $sms_send_config, $subpath )
+        : $subpath;
+    $conf_file .= q{.yaml};
+
+    my %args;
+    if ( -f $conf_file ) {
+        require YAML;
+        my $conf = YAML::LoadFile( $conf_file );
+        %args = map { q{_} . $_ => $conf->{$_} } keys %$conf;
+    }
+
+    eval {
+        # Create a sender
+        $sender = SMS::Send->new(
+            $driver,
+            _login    => C4::Context->preference('SMSSendUsername'),
+            _password => C4::Context->preference('SMSSendPassword'),
+            %args,
+        );
+
+        # Send a message
+        $sent = $sender->send_sms(
+            to   => $params->{destination},
+            text => $params->{message},
+        );
+    };
+
+    #We might die because SMS::Send $driver is not defined or the sms-number has a bad format
+    #Catch those errors and fail the sms-sending gracefully.
+    if ($@) {
+        warn $@;
+        return;
+    }
+    # warn 'failure' unless $sent;
+    return $sent;
 }
 
-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;
-               }
-       }
-       return 0;
-}
+=head2 driver
 
-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'};        # FIXME: mysql specific
-}
+sub driver {
+    my $self = shift;
 
-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);
-}
+    # return 'US::SprintPCS';
+    return C4::Context->preference('SMSSendDriver');
 
-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__
+