fix svc_phone non-numeric "phone numbers", RT#4204
[freeside.git] / FS / FS / svc_phone.pm
1 package FS::svc_phone;
2
3 use strict;
4 use vars qw( @ISA @pw_set );
5 use FS::Conf;
6 #use FS::Record qw( qsearch qsearchs );
7 use FS::svc_Common;
8
9 @ISA = qw( FS::svc_Common );
10
11 #avoid l 1 and o O 0
12 @pw_set = ( 'a'..'k', 'm','n', 'p-z', 'A'..'N', 'P'..'Z' , '2'..'9' );
13
14 =head1 NAME
15
16 FS::svc_phone - Object methods for svc_phone records
17
18 =head1 SYNOPSIS
19
20   use FS::svc_phone;
21
22   $record = new FS::svc_phone \%hash;
23   $record = new FS::svc_phone { 'column' => 'value' };
24
25   $error = $record->insert;
26
27   $error = $new_record->replace($old_record);
28
29   $error = $record->delete;
30
31   $error = $record->check;
32
33   $error = $record->suspend;
34
35   $error = $record->unsuspend;
36
37   $error = $record->cancel;
38
39 =head1 DESCRIPTION
40
41 An FS::svc_phone object represents a phone number.  FS::svc_phone inherits
42 from FS::Record.  The following fields are currently supported:
43
44 =over 4
45
46 =item svcnum
47
48 primary key
49
50 =item countrycode
51
52 =item phonenum
53
54 =item sip_password
55
56 =item pin
57
58 Voicemail PIN
59
60 =item phone_name
61
62 =back
63
64 =head1 METHODS
65
66 =over 4
67
68 =item new HASHREF
69
70 Creates a new phone number.  To add the number to the database, see L<"insert">.
71
72 Note that this stores the hash reference, not a distinct copy of the hash it
73 points to.  You can ask the object for a copy with the I<hash> method.
74
75 =cut
76
77 # the new method can be inherited from FS::Record, if a table method is defined
78 #
79 sub table_info {
80   {
81     'name' => 'Phone number',
82     'sorts' => 'phonenum',
83     'display_weight' => 60,
84     'cancel_weight'  => 80,
85     'fields' => {
86         'countrycode'  => { label => 'Country code',
87                             type  => 'text',
88                             disable_inventory => 1,
89                             disable_select => 1,
90                           },
91         'phonenum'     => 'Phone number',
92         'pin'          => { label => 'Personal Identification Number',
93                             type  => 'text',
94                             disable_inventory => 1,
95                             disable_select => 1,
96                           },
97         'sip_password' => 'SIP password',
98         'name'         => 'Name',
99     },
100   };
101 }
102
103 sub table { 'svc_phone'; }
104
105 =item search_sql STRING
106
107 Class method which returns an SQL fragment to search for the given string.
108
109 =cut
110
111 sub search_sql {
112   my( $class, $string ) = @_;
113   $class->search_sql_field('phonenum', $string );
114 }
115
116 =item label
117
118 Returns the phone number.
119
120 =cut
121
122 sub label {
123   my $self = shift;
124   my $phonenum = $self->phonenum; #XXX format it better
125   my $label = $phonenum;
126   $label .= ' ('.$self->phone_name.')' if $self->phone_name;
127   $label;
128 }
129
130 =item insert
131
132 Adds this record to the database.  If there is an error, returns the error,
133 otherwise returns false.
134
135 =cut
136
137 # the insert method can be inherited from FS::Record
138
139 =item delete
140
141 Delete this record from the database.
142
143 =cut
144
145 # the delete method can be inherited from FS::Record
146
147 =item replace OLD_RECORD
148
149 Replaces the OLD_RECORD with this one in the database.  If there is an error,
150 returns the error, otherwise returns false.
151
152 =cut
153
154 # the replace method can be inherited from FS::Record
155
156 =item suspend
157
158 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
159
160 =item unsuspend
161
162 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
163
164 =item cancel
165
166 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
167
168 =item check
169
170 Checks all fields to make sure this is a valid phone number.  If there is
171 an error, returns the error, otherwise returns false.  Called by the insert
172 and replace methods.
173
174 =cut
175
176 # the check method should currently be supplied - FS::Record contains some
177 # data checking routines
178
179 sub check {
180   my $self = shift;
181
182   my $conf = new FS::Conf;
183
184   my $phonenum = $self->phonenum;
185   my $phonenum_check_method;
186   if ( $conf->exists('svc_phone-allow_alpha_phonenum') ) {
187     $phonenum =~ s/\W//g;
188     $phonenum_check_method = 'ut_alpha';
189   } else {
190     $phonenum =~ s/\D//g;
191     $phonenum_check_method = 'ut_number';
192   }
193   $self->phonenum($phonenum);
194
195   my $error = 
196     $self->ut_numbern('svcnum')
197     || $self->ut_numbern('countrycode')
198     || $self->$phonenum_check_method('phonenum')
199     || $self->ut_anything('sip_password')
200     || $self->ut_numbern('pin')
201     || $self->ut_textn('phone_name')
202   ;
203   return $error if $error;
204
205   $self->countrycode(1) unless $self->countrycode;
206
207   unless ( length($self->sip_password) ) {
208
209     $self->sip_password(
210       join('', map $pw_set[ int(rand $#pw_set) ], (0..16) )
211     );
212
213   }
214
215   $self->SUPER::check;
216 }
217
218 =item check_pin
219
220 Checks the supplied PIN against the PIN in the database.  Returns true for a
221 sucessful authentication, false if no match.
222
223 =cut
224
225 sub check_pin {
226   my($self, $check_pin) = @_;
227   $check_pin eq $self->pin;
228 }
229
230 =item radius_reply
231
232 =cut
233
234 sub radius_reply {
235   my $self = shift;
236   #XXX Session-Timeout!  holy shit, need rlm_perl to ask for this in realtime
237   ();
238 }
239
240 =item radius_check
241
242 =cut
243
244 sub radius_check {
245   my $self = shift;
246   my %check = ();
247
248   my $conf = new FS::Conf;
249
250   $check{'User-Password'} = $conf->config('svc_phone-radius-default_password');
251
252   %check;
253 }
254
255 sub radius_groups {
256   ();
257 }
258
259 =back
260
261 =head1 BUGS
262
263 =head1 SEE ALSO
264
265 L<FS::svc_Common>, L<FS::Record>, L<FS::cust_svc>, L<FS::part_svc>,
266 L<FS::cust_pkg>, schema.html from the base documentation.
267
268 =cut
269
270 1;
271