This commit was generated by cvs2svn to compensate for changes in r4888,
[freeside.git] / FS / FS / svc_domain.pm
1 package FS::svc_domain;
2
3 use strict;
4 use vars qw( @ISA $whois_hack $conf
5   @defaultrecords $soadefaultttl $soaemail $soaexpire $soamachine
6   $soarefresh $soaretry
7 );
8 use Carp;
9 use Date::Format;
10 #use Net::Whois::Raw;
11 use FS::Record qw(fields qsearch qsearchs dbh);
12 use FS::Conf;
13 use FS::svc_Common;
14 use FS::cust_svc;
15 use FS::svc_acct;
16 use FS::cust_pkg;
17 use FS::cust_main;
18 use FS::domain_record;
19 use FS::queue;
20
21 @ISA = qw( FS::svc_Common );
22
23 #ask FS::UID to run this stuff for us later
24 $FS::UID::callback{'FS::domain'} = sub { 
25   $conf = new FS::Conf;
26
27   @defaultrecords = $conf->config('defaultrecords');
28   $soadefaultttl = $conf->config('soadefaultttl');
29   $soaemail      = $conf->config('soaemail');
30   $soaexpire     = $conf->config('soaexpire');
31   $soamachine    = $conf->config('soamachine');
32   $soarefresh    = $conf->config('soarefresh');
33   $soaretry      = $conf->config('soaretry');
34
35 };
36
37 =head1 NAME
38
39 FS::svc_domain - Object methods for svc_domain records
40
41 =head1 SYNOPSIS
42
43   use FS::svc_domain;
44
45   $record = new FS::svc_domain \%hash;
46   $record = new FS::svc_domain { 'column' => 'value' };
47
48   $error = $record->insert;
49
50   $error = $new_record->replace($old_record);
51
52   $error = $record->delete;
53
54   $error = $record->check;
55
56   $error = $record->suspend;
57
58   $error = $record->unsuspend;
59
60   $error = $record->cancel;
61
62 =head1 DESCRIPTION
63
64 An FS::svc_domain object represents a domain.  FS::svc_domain inherits from
65 FS::svc_Common.  The following fields are currently supported:
66
67 =over 4
68
69 =item svcnum - primary key (assigned automatically for new accounts)
70
71 =item domain
72
73 =item catchall - optional svcnum of an svc_acct record, designating an email catchall account.
74
75 =back
76
77 =head1 METHODS
78
79 =over 4
80
81 =item new HASHREF
82
83 Creates a new domain.  To add the domain to the database, see L<"insert">.
84
85 =cut
86
87 sub table { 'svc_domain'; }
88
89 =item insert [ , OPTION => VALUE ... ]
90
91 Adds this domain to the database.  If there is an error, returns the error,
92 otherwise returns false.
93
94 The additional fields I<pkgnum> and I<svcpart> (see L<FS::cust_svc>) should be 
95 defined.  An FS::cust_svc record will be created and inserted.
96
97 The additional field I<action> should be set to I<N> for new domains or I<M>
98 for transfers.
99
100 A registration or transfer email will be submitted unless
101 $FS::svc_domain::whois_hack is true.
102
103 The additional field I<email> can be used to manually set the admin contact
104 email address on this email.  Otherwise, the svc_acct records for this package 
105 (see L<FS::cust_pkg>) are searched.  If there is exactly one svc_acct record
106 in the same package, it is automatically used.  Otherwise an error is returned.
107
108 If any I<soamachine> configuration file exists, an SOA record is added to
109 the domain_record table (see <FS::domain_record>).
110
111 If any records are defined in the I<defaultrecords> configuration file,
112 appropriate records are added to the domain_record table (see
113 L<FS::domain_record>).
114
115 Currently available options are: I<depend_jobnum>
116
117 If I<depend_jobnum> is set (to a scalar jobnum or an array reference of
118 jobnums), all provisioning jobs will have a dependancy on the supplied
119 jobnum(s) (they will not run until the specific job(s) complete(s)).
120
121 =cut
122
123 sub insert {
124   my $self = shift;
125   my $error;
126
127   local $SIG{HUP} = 'IGNORE';
128   local $SIG{INT} = 'IGNORE';
129   local $SIG{QUIT} = 'IGNORE';
130   local $SIG{TERM} = 'IGNORE';
131   local $SIG{TSTP} = 'IGNORE';
132   local $SIG{PIPE} = 'IGNORE';
133
134   my $oldAutoCommit = $FS::UID::AutoCommit;
135   local $FS::UID::AutoCommit = 0;
136   my $dbh = dbh;
137
138   $error = $self->check;
139   return $error if $error;
140
141   return "Domain in use (here)"
142     if qsearchs( 'svc_domain', { 'domain' => $self->domain } );
143
144   my $whois = $self->whois;
145   if ( $self->action eq "N" && ! $whois_hack && $whois ) {
146     $dbh->rollback if $oldAutoCommit;
147     return "Domain in use (see whois)";
148   }
149   if ( $self->action eq "M" && ! $whois ) {
150     $dbh->rollback if $oldAutoCommit;
151     return "Domain not found (see whois)";
152   }
153
154   $error = $self->SUPER::insert(@_);
155   if ( $error ) {
156     $dbh->rollback if $oldAutoCommit;
157     return $error;
158   }
159
160   $self->submit_internic unless $whois_hack;
161
162   if ( $soamachine ) {
163     my $soa = new FS::domain_record {
164       'svcnum'  => $self->svcnum,
165       'reczone' => '@',
166       'recaf'   => 'IN',
167       'rectype' => 'SOA',
168       'recdata' => "$soamachine $soaemail ( ". time2str("%Y%m%d", time). "00 ".
169                    "$soarefresh $soaretry $soaexpire $soadefaultttl )"
170     };
171     $error = $soa->insert;
172     if ( $error ) {
173       $dbh->rollback if $oldAutoCommit;
174       return "couldn't insert SOA record for new domain: $error";
175     }
176
177     foreach my $record ( @defaultrecords ) {
178       my($zone,$af,$type,$data) = split(/\s+/,$record,4);
179       my $domain_record = new FS::domain_record {
180         'svcnum'  => $self->svcnum,
181         'reczone' => $zone,
182         'recaf'   => $af,
183         'rectype' => $type,
184         'recdata' => $data,
185       };
186       my $error = $domain_record->insert;
187       if ( $error ) {
188         $dbh->rollback if $oldAutoCommit;
189         return "couldn't insert record for new domain: $error";
190       }
191     }
192
193   }
194
195   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
196
197   ''; #no error
198 }
199
200 =item delete
201
202 Deletes this domain from the database.  If there is an error, returns the
203 error, otherwise returns false.
204
205 The corresponding FS::cust_svc record will be deleted as well.
206
207 =cut
208
209 sub delete {
210   my $self = shift;
211
212   return "Can't delete a domain which has accounts!"
213     if qsearch( 'svc_acct', { 'domsvc' => $self->svcnum } );
214
215   #return "Can't delete a domain with (domain_record) zone entries!"
216   #  if qsearch('domain_record', { 'svcnum' => $self->svcnum } );
217
218   local $SIG{HUP} = 'IGNORE';
219   local $SIG{INT} = 'IGNORE';
220   local $SIG{QUIT} = 'IGNORE';
221   local $SIG{TERM} = 'IGNORE';
222   local $SIG{TSTP} = 'IGNORE';
223   local $SIG{PIPE} = 'IGNORE';
224
225   my $oldAutoCommit = $FS::UID::AutoCommit;
226   local $FS::UID::AutoCommit = 0;
227   my $dbh = dbh;
228
229   foreach my $domain_record ( reverse $self->domain_record ) {
230     my $error = $domain_record->delete;
231     if ( $error ) {
232       $dbh->rollback if $oldAutoCommit;
233       return "can't delete DNS entry: ".
234              join(' ', map $domain_record->$_(),
235                            qw( reczone recaf rectype recdata )
236                  ).
237              ":$error";
238     }
239   }
240
241   my $error = $self->SUPER::delete;
242   if ( $error ) {
243     $dbh->rollback if $oldAutoCommit;
244     return $error;
245   }
246
247   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
248 }
249
250 =item replace OLD_RECORD
251
252 Replaces OLD_RECORD with this one in the database.  If there is an error,
253 returns the error, otherwise returns false.
254
255 =cut
256
257 sub replace {
258   my ( $new, $old ) = ( shift, shift );
259
260   return "Can't change domain - reorder."
261     if $old->getfield('domain') ne $new->getfield('domain'); 
262
263   # Better to do it here than to force the caller to remember that svc_domain is weird.
264   $new->setfield(action => 'M');
265   my $error = $new->SUPER::replace($old);
266   return $error if $error;
267 }
268
269 =item suspend
270
271 Just returns false (no error) for now.
272
273 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
274
275 =item unsuspend
276
277 Just returns false (no error) for now.
278
279 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
280
281 =item cancel
282
283 Just returns false (no error) for now.
284
285 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
286
287 =item check
288
289 Checks all fields to make sure this is a valid domain.  If there is an error,
290 returns the error, otherwise returns false.  Called by the insert and replace
291 methods.
292
293 Sets any fixed values; see L<FS::part_svc>.
294
295 =cut
296
297 sub check {
298   my $self = shift;
299
300   my $x = $self->setfixed;
301   return $x unless ref($x);
302   #my $part_svc = $x;
303
304   my $error = $self->ut_numbern('svcnum')
305               || $self->ut_numbern('catchall')
306   ;
307   return $error if $error;
308
309   #hmm
310   my $pkgnum;
311   if ( $self->svcnum ) {
312     my $cust_svc = qsearchs( 'cust_svc', { 'svcnum' => $self->svcnum } );
313     $pkgnum = $cust_svc->pkgnum;
314   } else {
315     $pkgnum = $self->pkgnum;
316   }
317
318   my($recref) = $self->hashref;
319
320   unless ( $whois_hack ) {
321     unless ( $self->email ) { #find out an email address
322       my @svc_acct;
323       foreach ( qsearch( 'cust_svc', { 'pkgnum' => $pkgnum } ) ) {
324         my $svc_acct = qsearchs( 'svc_acct', { 'svcnum' => $_->svcnum } );
325         push @svc_acct, $svc_acct if $svc_acct;
326       }
327
328       if ( scalar(@svc_acct) == 0 ) {
329         return "Must order an account in package ". $pkgnum. " first";
330       } elsif ( scalar(@svc_acct) > 1 ) {
331         return "More than one account in package ". $pkgnum. ": specify admin contact email";
332       } else {
333         $self->email($svc_acct[0]->email );
334       }
335     }
336   }
337
338   #if ( $recref->{domain} =~ /^([\w\-\.]{1,22})\.(com|net|org|edu)$/ ) {
339   if ( $recref->{domain} =~ /^([\w\-]{1,63})\.(com|net|org|edu)$/ ) {
340     $recref->{domain} = "$1.$2";
341   # hmmmmmmmm.
342   } elsif ( $whois_hack && $recref->{domain} =~ /^([\w\-\.]+)$/ ) {
343     $recref->{domain} = $1;
344   } else {
345     return "Illegal domain ". $recref->{domain}.
346            " (or unknown registry - try \$whois_hack)";
347   }
348
349   $recref->{action} =~ /^(M|N)$/
350     or return "Illegal action: ". $recref->{action};
351   $recref->{action} = $1;
352
353   if ( $recref->{catchall} ne '' ) {
354     my $svc_acct = qsearchs( 'svc_acct', { 'svcnum' => $recref->{catchall} } );
355     return "Unknown catchall" unless $svc_acct;
356   }
357
358   $self->ut_textn('purpose')
359     or $self->SUPER::check;
360
361 }
362
363 =item domain_record
364
365 =cut
366
367 sub domain_record {
368   my $self = shift;
369
370   my %order = (
371     'SOA'   => 1,
372     'NS'    => 2,
373     'MX'    => 3,
374     'CNAME' => 4,
375     'A'     => 5,
376     'TXT'   => 6,
377     'PTR'   => 7,
378   );
379
380   sort { $order{$a->rectype} <=> $order{$b->rectype} }
381     qsearch('domain_record', { svcnum => $self->svcnum } );
382
383 }
384
385 sub catchall_svc_acct {
386   my $self = shift;
387   if ( $self->catchall ) {
388     qsearchs( 'svc_acct', { 'svcnum' => $self->catchall } );
389   } else {
390     '';
391   }
392 }
393
394 =item whois
395
396 # Returns the Net::Whois::Domain object (see L<Net::Whois>) for this domain, or
397 # undef if the domain is not found in whois.
398
399 (If $FS::svc_domain::whois_hack is true, returns that in all cases instead.)
400
401 =cut
402
403 sub whois {
404   #$whois_hack or new Net::Whois::Domain $_[0]->domain;
405   $whois_hack or die "whois_hack not set...\n";
406 }
407
408 =item _whois
409
410 Depriciated.
411
412 =cut
413
414 sub _whois {
415   die "_whois depriciated";
416 }
417
418 =item submit_internic
419
420 Submits a registration email for this domain.
421
422 =cut
423
424 sub submit_internic {
425   #my $self = shift;
426   carp "submit_internic depreciated";
427 }
428
429 =back
430
431 =head1 BUGS
432
433 Delete doesn't send a registration template.
434
435 All registries should be supported.
436
437 Should change action to a real field.
438
439 The $recref stuff in sub check should be cleaned up.
440
441 =head1 SEE ALSO
442
443 L<FS::svc_Common>, L<FS::Record>, L<FS::Conf>, L<FS::cust_svc>,
444 L<FS::part_svc>, L<FS::cust_pkg>, L<Net::Whois>, schema.html from the base
445 documentation, config.html from the base documentation.
446
447 =cut
448
449 1;
450
451