Bug 13029: Allow to pass additional parameters to SMS::Send drivers
[koha.git] / C4 / SMS.pm
1 package C4::SMS;
2
3 # Copyright 2007 Liblime
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 =head1 NAME
21
22 C4::SMS - send SMS messages
23
24 =head1 SYNOPSIS
25
26 my $success = C4::SMS->send_sms({ message     => 'This is my text message',
27                                   destination => '212-555-1212' });
28
29 =head1 DESCRIPTION
30
31
32
33 =cut
34
35 use strict;
36 use warnings;
37
38 use C4::Context;
39 use File::Spec;
40
41
42
43 =head1 METHODS
44
45 =cut
46
47 # The previous implmentation used username and password.
48 # our $user = C4::Context->config('smsuser');
49 # our $pwd  = C4::Context->config('smspass');
50
51 =head2 send_sms
52
53 =cut
54
55 sub send_sms {
56     my $self = shift;
57     my $params= shift;
58
59     foreach my $required_parameter ( qw( message destination ) ) {
60         # Should I warn in some way?
61         return unless defined $params->{ $required_parameter };
62     }
63
64     eval { require SMS::Send; };
65     if ( $@ ) {
66         # we apparently don't have SMS::Send. Return a failure.
67         return;
68     }
69
70     # This allows the user to override the driver. See SMS::Send::Test
71     my $driver = exists $params->{'driver'} ? $params->{'driver'} : $self->driver();
72     return unless $driver;
73
74
75     my ($sent, $sender);
76
77     my $subpath = $driver;
78     $subpath =~ s|::|/|;
79
80     my $conf_file = File::Spec->catfile(
81         C4::Context->config('installdir'),
82         'etc', 'sms', 'driver', $subpath
83     ) . q{.yaml};
84     my %args;
85     if ( -f $conf_file ) {
86         require YAML;
87         my $conf = YAML::LoadFile( $conf_file );
88         %args = map { q{_} . $_ => $conf->{$_} } keys %$conf;
89     }
90
91     eval {
92         # Create a sender
93         $sender = SMS::Send->new(
94             $driver,
95             _login    => C4::Context->preference('SMSSendUsername'),
96             _password => C4::Context->preference('SMSSendPassword'),
97             %args,
98         );
99
100         # Send a message
101         $sent = $sender->send_sms(
102             to   => $params->{destination},
103             text => $params->{message},
104         );
105     };
106
107     #We might die because SMS::Send $driver is not defined or the sms-number has a bad format
108     #Catch those errors and fail the sms-sending gracefully.
109     if ($@) {
110         warn $@;
111         return;
112     }
113     # warn 'failure' unless $sent;
114     return $sent;
115 }
116
117 =head2 driver
118
119 =cut
120
121 sub driver {
122     my $self = shift;
123
124     # return 'US::SprintPCS';
125     return C4::Context->preference('SMSSendDriver');
126
127 }
128
129 1;
130
131 __END__
132