X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=C4%2FSMS.pm;h=eff72b358c0a5e2af6c09c88c002f2e5fb7fe047;hb=5b160af2a051dc3693f04dabf320d0995cf26160;hp=457a662e643f1eacb04e4a66958fff038b5586ba;hpb=279687488cbb998305f9662d3779e9b325a98047;p=koha.git diff --git a/C4/SMS.pm b/C4/SMS.pm index 457a662e64..eff72b358c 100644 --- a/C4/SMS.pm +++ b/C4/SMS.pm @@ -1,19 +1,23 @@ package C4::SMS; +# 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 2 of the License, or (at your option) any later -# version. +# 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. +# 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 +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . =head1 NAME @@ -21,12 +25,28 @@ C4::SMS - send SMS messages =head1 SYNOPSIS -my $success = C4::SMS->send_sms( message => 'This is my text message', - destination => '212-555-1212' ); +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 +__KOHA_CONF_DIR__/sms_send/ + +Each file needs to be in the format of +__KOHA_CONF_DIR__/sms_send/.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 @@ -34,12 +54,9 @@ use strict; use warnings; use C4::Context; +use File::Spec; -use vars qw( $VERSION ); -BEGIN { - $VERSION = 0.03; -} =head1 METHODS @@ -72,28 +89,52 @@ sub send_sms { my $driver = exists $params->{'driver'} ? $params->{'driver'} : $self->driver(); return unless $driver; - # 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'}, - ); + 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; } =head2 driver -=over 4 - -=back - =cut sub driver {