forked from tangaza/Common
-
Notifications
You must be signed in to change notification settings - Fork 1
/
SMSQueue.pm
181 lines (129 loc) · 4.02 KB
/
SMSQueue.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#
# Common: Shared library for voice-based applications.
#
# Copyright (C) 2010 Nokia Corporation.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Authors: Billy Odero, Jonathan Ledlie
#
package Nokia::Common::SMSQueue;
use base qw(Net::Server);
use strict;
use POSIX qw(strftime);
use Data::Dumper;
use Nokia::Common::Tools;
=head1 NAME
Nokia::Common::SMSQueue - SMS buffer daemon
=head1 DESCRIPTION
Library for simple daemon that buffers outgoing SMS messages.
Started with daemon/sms-send-daemon.pl.
=head2 METHODS
=cut
######################################################################
=head2 process_request
Server side of sms queue.
Waits for SMS messages and sends them out FIFO
=cut
sub process_request {
my $self = shift;
my $line = <STDIN>;
my (@items) = split (/\s/,$line);
if ($#items < 2) {
$self->log (4, "not enough args ".$#items);
return;
}
my $phone = $items[0];
shift (@items);
my $msg = join (' ',@items);
# Add some sleeping / buffering?
if (!defined ($phone)) {
$self->log (4, "phone not defined");
return;
}
if (!defined ($msg)) {
$self->log (4, "msg not defined");
return;
}
$self->log (3, "queue phone $phone msg $msg");
&send_sms ($self, $phone, $msg);
}
#######################################################################
=head2 send_sms
Send out SMS's from the queue.
=over 4
=item Args:
$phone: The phone number that is to receive the SMS
$message: The message being sent
=item Example:
&send_sms ($self, $self->{user}->{phone}, "Here is the message");
=back
=cut
sub send_sms {
my ($self, $phone, $message) = @_;
$self->log (4, "trying sms to $phone msg $message");
my $browser = LWP::UserAgent->new;
my $prefs = $self->get_property('prefs');
my $sms_url = $prefs->{sms}->{url};
my $origin = $self->get_property('origin');
my $url = URI->new($sms_url);
#TODO: Figure out how to use multiple sms sending options depending on
#origin of the sms
if ($origin eq 'ke') {
$url->query_form
('username' => $prefs->{sms}->{user},
'password' => $prefs->{sms}->{pass},
'source' => $prefs->{sms}->{source},
'destination' => $phone,
'message' => $message
);
}
else {
$url->query_form
('to' => $phone,
'text' => $message,
'username' => $self->get_property("sms_username_$origin"),
'password' => $self->get_property("sms_password_$origin")
);
}
$self->log (4, "created query form $url");
my $response = $browser->get($url);
$self->log (4, "sent sms to $phone msg $message");
# check return code??
if (! ($response->is_success)) {
$self->log (1, "sending sms to $phone failed: ".$response->status_line);
return -1;
}
$self->log (4, "all done");
return 0;
}
######################################################################
=head2 write_to_log_hook
This hook handles writing to log files
See L<Net::Server>.
=cut
sub write_to_log_hook {
my $self = shift;
my $level = shift;
my $msg = shift || '';
my $date = strftime("[%d-%b-%Y %H:%M:%S]:", localtime);
my $func_name = (caller(2))[3];
$self->Net::Server::write_to_log_hook($level, "$date $func_name $msg");
}
######################################################################
=head1 AUTHORS
Billy Odero, Jonathan Ledlie
Copyright (C) 2010 Nokia Corporation.
=cut
1;