Koha/C4/SMS.pm
Magnus Enger a4237785d9 Bug 19134: C4::SMS falils on long driver name
Code in C4::SMS takes the part of the SMS::Send-driver that comes after
SMS::Send and tries to turn it into part of a path to a YAML file
that can contain additional parameters to SMS::Send. The current
code works for e.g. SMS::Send::A::B, but if there is one or more
extra names, it fails to turn :: into /. So we have:
SMS::Send::A::B    -> SMS/Send/A/B
SMS::Send::A::B::C -> SMS/Send/A/B::C
This patch makes sure all occurrences of :: are turned into /, by
adding a "g" modifier at the end of the regex.

Testing:
Testing this preperly would take a whole lot of setup for a very
small change. I would suggest that the following two oneliners
are enough to demonstrate that the change makes sense:
$ perl -e '$x = "a:🅱️:c"; $x =~ s|::|/|; print $x, "\n";'
$ perl -e '$x = "a:🅱️:c"; $x =~ s|::|/|g; print $x, "\n";'

So:
- Check that the output of these oneliners make sense
- Check that the patch changes the code in a similar way to the
  change from the first oneliner to the second.

Signed-off-by: Aleisha Amohia <aleishaamohia@hotmail.com>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
2017-08-25 10:51:24 -03:00

151 lines
3.6 KiB
Perl

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 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 C4::Context;
use File::Spec;
=head1 METHODS
=cut
# 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;
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
=cut
sub driver {
my $self = shift;
# return 'US::SprintPCS';
return C4::Context->preference('SMSSendDriver');
}
1;
__END__