forked from tangaza/Common
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Entry.pm
341 lines (227 loc) · 7.79 KB
/
Entry.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#
# 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::Entry;
use Exporter;
@ISA = ('Exporter');
@EXPORT = ('configure_hook', 'pre_server_close_hook', 'write_to_log_hook', 'post_configure_hook');
use strict;
use Data::Dumper;
use POSIX qw(strftime);
use base 'Asterisk::FastAGI';
use DBI;
use Math::Random;
use Nokia::Common::Tools;
use Nokia::Common::Stamp;
use Nokia::Common::Sound;
use Nokia::Common::Phone;
use Nokia::Common::Auth;
=head1 NAME
Nokia::Common::Entry - Entry into the system
=head1 DESCRIPTION
Entry point into the system. It sets up the initial system and user state
necessary for a call to the system.
=head1 METHODS
=cut
######################################################################
=head2 entry
Entry point into the system. Invoked by Asterisk from extensions.conf.
It sets up the initial state of the system for each new call and cleans up
when the call terminates.
=cut
sub entry {
my ($self) = @_;
my @items_to_back = qw(fn_start_call fn_end_call fn_main_menu server welcome_msg);
my $self_backup = {};
foreach my $item (@items_to_back) {
$self_backup->{$item} = $self->{$item};
}
foreach my $key (keys %$self) {
delete $self->{$key};
}
foreach my $item (@items_to_back) {
$self->{$item} = $self_backup->{$item};
}
$self->log (3, "starting ".&get_channel_desc($self));
#&db_connect ($self);
$self->{stamp} = time;
$self->{callerid} = $self->input('callerid');
my $seed = $self->input('channel').' '.$self->{stamp}.' '.$self->{callerid};
$self->log (4, "seed $seed");
Math::Random::random_set_seed_from_phrase ($seed);
if (! &user_has_hungup($self)) {
$self->log (1, "NO EVAL TRAP");
#eval {
# No DB operations outside of here.
if (&init_user($self) < 0) {
$self->log (2, $self->{stamp}." bad callerid ".$self->{callerid});
#$self->agi->stream_file
# ([&msg($self, 'sorry'),
# &msg($self, 'your caller id is invalid'),
# &msg($self, 'goodbye')],"*","0");
$self->log (2, "sorry");
&stream_file ($self, 'sorry',"*","0");
$self->log (2, "done sorry");
return;
}
if (&start_call($self) eq 'ok') {
$self->{fn_main_menu}->($self);
if (! &user_has_hungup($self)) {
$self->log (3, "ending session for user_id ".$self->{user}->{id}.
" callerid ".$self->{callerid});
&stream_file ($self, 'goodbye',"*","0");
$self->agi->hangup ();
}
&end_call($self);
}
if ($@) {
$self->log (1, "Error caught by main: $@");
&stream_file(['an-error-has-occured', 'goodbye'],"*","0");
}
#};
}
my $endTime = time;
my $diffTime = $endTime - $self->{stamp};
$self->log (3, "ending Common::Entry callerid $self->{callerid}, ran for $diffTime sec");
}
######################################################################
=head2 start_call
Checks the state of the call to determine if the user is flashing or calling through
and then initializes the appropriate variables required by the system.
=cut
sub start_call {
my ($self) = @_;
$self->log (3, "starting welcome_msg");
# my $auth_res = &auth_pin ($self);
# if ($auth_res ne 'ok') {
# return $auth_res;
# }
$self->{cbstate} = $self->agi->get_variable("CBSTATE");
$self->log (3, "cbstate ".$self->{cbstate});
if ($self->{cbstate} eq 'calledback') {
$self->{user}->{seconds_remaining} = &get_seconds_remaining_count ($self);
$self->log (3, "calledback id ".$self->{user}->{id}.
" seconds remaining ".$self->{user}->{seconds_remaining});
if ($self->{newuser} == 1) {
my $NEWUSER_BONUS_SECONDS = 120;
$self->{user}->{seconds_remaining} += $NEWUSER_BONUS_SECONDS;
}
$self->log (2, "SKIPPING ALARMS");
#&set_alarms ($self);
}
# else user calledus
$self->log (2, "SKIPPING INVITATION CHECK");
#&check_for_invitation ($self);
if ($self->{newuser} == 1) {
$self->log (3, "newuser id ".$self->{user}->{id});
# TODO do extra stuff here if new user
# before main menu
# Such as selecting a language
}
$self->log (3, "ending welcome_msg");
$self->{fn_start_call}->($self);
return 'ok';
}
######################################################################
sub set_alarms {
my ($self) = @_;
$self->log (3, "start set_alarms");
# mention of alarms here
# http://www.voip-info.org/wiki-Asterisk+cmd+monitor
# More code in Mosoko/Entry set_alarms
}
######################################################################
=head2 end_call
Performs cleanup and logging when the call ends i.e. user terminates the call or the
call terminates because of an error.
=cut
sub end_call {
my ($self) = @_;
my $endTime = time;
my $seconds = $endTime - $self->{stamp};
my $call = $self->{server}{schema}->resultset('Calls')->create
({user_id => $self->{user}->{id}, seconds => $seconds,
cbstate => $self->{cbstate}});
$self->{fn_end_call}->($self);
$self->log (4, "end_call user ".$self->{user}->{id}." sec ".$seconds);
}
######################################################################
=head2 configure_hook
Sets up initial configurations e.g. sound, database, tools as part of the call setup
procedure.
See L<Net::Server>
=cut
sub configure_hook {
my $self = shift;
$self->log ("conf hook");
#print STDERR "conf hook";
&dbi_connect ($self);
&init_sound ($self);
#&init_places ($self);
&init_tools ($self);
}
######################################################################
=head2 pre_server_close_hook
Performs any necessary clean-ups just before the server terminates.
See L<Net::Server>.
=cut
sub pre_server_close_hook {
my $self = shift;
print STDERR "Running pre_server_close_hook\n";
&db_disconnect ($self);
}
######################################################################
sub post_configure_hook {
my $self = shift;
#$self->log (2, "db ".$self->get_property('DB_DSN'));
#$self->log (2, "db ".$self->get_property(DB_DSN));
#print STDERR Dumper ($self);
#exit (0);
}
######################################################################
sub pre_configure_hook {
my $self = shift;
#$self->log (2, "db ".$self->get_property('DB_DSN'));
#$self->log (2, "db ".$self->get_property(DB_DSN));
#print STDERR Dumper ($self);
}
######################################################################
=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];
# TODO Add uid
#$self->SUPER::log($level, "$date $func_name $msg");
#$self->log($level, "$date $func_name $msg");
$self->Net::Server::write_to_log_hook($level, "$date $func_name $msg");
return;
}
######################################################################
=head1 AUTHORS
Billy Odero, Jonathan Ledlie
Copyright (C) 2010 Nokia Corporation.
=cut
1;