-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathPhone.pm
187 lines (132 loc) · 4.48 KB
/
Phone.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
#
# 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::Phone;
use Exporter;
@ISA = ('Exporter');
@EXPORT = ('is_valid_outbound_number','parse_callerid');
use strict;
=head1 NAME
Nokia::Common::Phone - Checks incoming phone number
=head1 DESCRIPTION
This module parses caller-ids for to try and determine the origin of the call.
It also checks caller-ids for outgoing calls to make sure that the system only
calls valid numbers e.g avoid calling 0800 numbers.
=head1 METHODS
=cut
######################################################################
=head2 is_valid_outbound_number
Checks whether the number is valid depending on the origin of the call
=over 4
=item Args:
$number: The phone number being validated
=back
=cut
sub is_valid_outbound_number {
my ($self, $number) = @_;
if (length($$number) < 7 || length($$number) > 20) {
return 0;
}
# We do not want to call any number arbitrarily
# i.e. 800 numbers, "900" numbers
# Kenyan numbers
#if ($$number =~ /^0[\d]{2}|7[1-3][\d][\d]{6,7}$/) {
if ($$number =~ /^0(\d{2}|7\d{2})\d{6,7}$/) {
return 1;
}
# US numbers for testing
# Prepend 1 if missing
if ($$number =~ /^1?\d\d\d\d\d\d\d\d\d\d$/) {
if ($$number !~ /^1/) {
$$number = '1'.$$number;
}
return 1;
}
$self->log ("accepting for DEMO");
#return 0;
return 1;
}
######################################################################
=head2 parse_callerid
Parses the caller id to determine what country the call is originating from
=cut
sub parse_callerid {
my ($self) = @_;
my $callerid = $self->{callerid};
$self->{user}->{incoming_phone} = $callerid;
$self->{user}->{outgoing_phone} = $callerid;
$self->log (4, "parsing callerid $callerid");
# US number
if ($callerid =~ /^1(\d\d\d)(\d\d\d\d\d\d\d)$/ ||
$callerid =~ /^1-(\d\d\d)-(\d\d\d\d\d\d\d)$/ ||
$callerid =~ /^(\d\d\d)(\d\d\d\d\d\d\d)$/ ||
$callerid =~ /^(\d\d\d)-(\d\d\d-\d\d\d\d)$/) {
$self->{user}->{place_id} = 1;
$self->{user}->{phone} = $self->{callerid};
$self->{user}->{phone} =~ s/-//;
$self->log (4, "matched US number $callerid");
return 1;
}
# Finland number
if ($callerid =~ /^358(\d+)$/) {
$self->{user}->{place_id} = 1;
$self->{user}->{phone} = $self->{callerid};
$self->{user}->{phone} =~ s/-//;
$self->log (4, "matched Finland number $callerid");
return 1;
}
#Kenya number
#Allow formats 254(0**|71*|72*|73*|)(******(*))
#the '*' represents any numeric digit 0-9
#
#if its a safaricom number prepend 254 for callerid (db record)
#and 0 for outgoing_phone
if ($callerid =~ m/^7\d{8}$/) {
#$self->{user}->{outgoing_phone} = "0".$callerid;
$callerid = "254".$callerid;
}
#if ($callerid =~ m/^254(0[\d]{2}|7[1-3][\d][\d]{6,7})$/ ||
if ($callerid =~ m/^2547\d{8}$/) {
# Caller IDs are numbers only. i.e. no dashes, parentheses etc...at least at this end
#$self->{user}->{phone} = $callerid;
# Todo set the location based on the phone number?
# What about other major cities in EA?
$self->{user}->{place_id} = 1;
$self->{user}->{outgoing_phone} = "0".substr($callerid, 3);
$self->{user}->{callerid} = $callerid;
$self->{user}->{phone} = $callerid;
$self->{callerid} = $callerid;
$self->log (4, "matched Kenya number ".$self->{user}->{phone});
return 1;
}
#if all else fails
$self->{user}->{phone} = $callerid;
$self->{user}->{callerid} = $callerid;
$self->{callerid} = $callerid;
$self->log (1, "Parse callerid failed $callerid");
#return -1;
$self->log (1, "Parse callerid accepting for DEMO");
return 1;
}
=head1 AUTHORS
Billy Odero, Jonathan Ledlie
Copyright (C) 2010 Nokia Corporation.
=cut
1;