Virtual field merge
[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 1.0;
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
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 =cut
116
117 sub insert {
118   my $self = shift;
119   my $error;
120
121   local $SIG{HUP} = 'IGNORE';
122   local $SIG{INT} = 'IGNORE';
123   local $SIG{QUIT} = 'IGNORE';
124   local $SIG{TERM} = 'IGNORE';
125   local $SIG{TSTP} = 'IGNORE';
126   local $SIG{PIPE} = 'IGNORE';
127
128   my $oldAutoCommit = $FS::UID::AutoCommit;
129   local $FS::UID::AutoCommit = 0;
130   my $dbh = dbh;
131
132   $error = $self->check;
133   return $error if $error;
134
135   return "Domain in use (here)"
136     if qsearchs( 'svc_domain', { 'domain' => $self->domain } );
137
138   my $whois = $self->whois;
139   if ( $self->action eq "N" && ! $whois_hack && $whois ) {
140     $dbh->rollback if $oldAutoCommit;
141     return "Domain in use (see whois)";
142   }
143   if ( $self->action eq "M" && ! $whois ) {
144     $dbh->rollback if $oldAutoCommit;
145     return "Domain not found (see whois)";
146   }
147
148   $error = $self->SUPER::insert;
149   if ( $error ) {
150     $dbh->rollback if $oldAutoCommit;
151     return $error;
152   }
153
154   $self->submit_internic unless $whois_hack;
155
156   if ( $soamachine ) {
157     my $soa = new FS::domain_record {
158       'svcnum'  => $self->svcnum,
159       'reczone' => '@',
160       'recaf'   => 'IN',
161       'rectype' => 'SOA',
162       'recdata' => "$soamachine $soaemail ( ". time2str("%Y%m%d", time). "00 ".
163                    "$soarefresh $soaretry $soaexpire $soadefaultttl )"
164     };
165     $error = $soa->insert;
166     if ( $error ) {
167       $dbh->rollback if $oldAutoCommit;
168       return "couldn't insert SOA record for new domain: $error";
169     }
170
171     foreach my $record ( @defaultrecords ) {
172       my($zone,$af,$type,$data) = split(/\s+/,$record,4);
173       my $domain_record = new FS::domain_record {
174         'svcnum'  => $self->svcnum,
175         'reczone' => $zone,
176         'recaf'   => $af,
177         'rectype' => $type,
178         'recdata' => $data,
179       };
180       my $error = $domain_record->insert;
181       if ( $error ) {
182         $dbh->rollback if $oldAutoCommit;
183         return "couldn't insert record for new domain: $error";
184       }
185     }
186
187   }
188
189   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
190
191   ''; #no error
192 }
193
194 =item delete
195
196 Deletes this domain from the database.  If there is an error, returns the
197 error, otherwise returns false.
198
199 The corresponding FS::cust_svc record will be deleted as well.
200
201 =cut
202
203 sub delete {
204   my $self = shift;
205
206   return "Can't delete a domain which has accounts!"
207     if qsearch( 'svc_acct', { 'domsvc' => $self->svcnum } );
208
209   #return "Can't delete a domain with (domain_record) zone entries!"
210   #  if qsearch('domain_record', { 'svcnum' => $self->svcnum } );
211
212   local $SIG{HUP} = 'IGNORE';
213   local $SIG{INT} = 'IGNORE';
214   local $SIG{QUIT} = 'IGNORE';
215   local $SIG{TERM} = 'IGNORE';
216   local $SIG{TSTP} = 'IGNORE';
217   local $SIG{PIPE} = 'IGNORE';
218
219   my $oldAutoCommit = $FS::UID::AutoCommit;
220   local $FS::UID::AutoCommit = 0;
221   my $dbh = dbh;
222
223   foreach my $domain_record ( reverse $self->domain_record ) {
224     my $error = $domain_record->delete;
225     if ( $error ) {
226       $dbh->rollback if $oldAutoCommit;
227       return $error;
228     }
229   }
230
231   my $error = $self->SUPER::delete;
232   if ( $error ) {
233     $dbh->rollback if $oldAutoCommit;
234     return $error;
235   }
236
237   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
238 }
239
240 =item replace OLD_RECORD
241
242 Replaces OLD_RECORD with this one in the database.  If there is an error,
243 returns the error, otherwise returns false.
244
245 =cut
246
247 sub replace {
248   my ( $new, $old ) = ( shift, shift );
249
250   return "Can't change domain - reorder."
251     if $old->getfield('domain') ne $new->getfield('domain'); 
252
253   my $error = $new->SUPER::replace($old);
254   return $error if $error;
255 }
256
257 =item suspend
258
259 Just returns false (no error) for now.
260
261 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
262
263 =item unsuspend
264
265 Just returns false (no error) for now.
266
267 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
268
269 =item cancel
270
271 Just returns false (no error) for now.
272
273 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
274
275 =item check
276
277 Checks all fields to make sure this is a valid domain.  If there is an error,
278 returns the error, otherwise returns false.  Called by the insert and replace
279 methods.
280
281 Sets any fixed values; see L<FS::part_svc>.
282
283 =cut
284
285 sub check {
286   my $self = shift;
287
288   my $x = $self->setfixed;
289   return $x unless ref($x);
290   #my $part_svc = $x;
291
292   my $error = $self->ut_numbern('svcnum')
293               || $self->ut_numbern('catchall')
294   ;
295   return $error if $error;
296
297   #hmm
298   my $pkgnum;
299   if ( $self->svcnum ) {
300     my $cust_svc = qsearchs( 'cust_svc', { 'svcnum' => $self->svcnum } );
301     $pkgnum = $cust_svc->pkgnum;
302   } else {
303     $pkgnum = $self->pkgnum;
304   }
305
306   my($recref) = $self->hashref;
307
308   unless ( $whois_hack ) {
309     unless ( $self->email ) { #find out an email address
310       my @svc_acct;
311       foreach ( qsearch( 'cust_svc', { 'pkgnum' => $pkgnum } ) ) {
312         my $svc_acct = qsearchs( 'svc_acct', { 'svcnum' => $_->svcnum } );
313         push @svc_acct, $svc_acct if $svc_acct;
314       }
315
316       if ( scalar(@svc_acct) == 0 ) {
317         return "Must order an account in package ". $pkgnum. " first";
318       } elsif ( scalar(@svc_acct) > 1 ) {
319         return "More than one account in package ". $pkgnum. ": specify admin contact email";
320       } else {
321         $self->email($svc_acct[0]->email );
322       }
323     }
324   }
325
326   #if ( $recref->{domain} =~ /^([\w\-\.]{1,22})\.(com|net|org|edu)$/ ) {
327   if ( $recref->{domain} =~ /^([\w\-]{1,63})\.(com|net|org|edu)$/ ) {
328     $recref->{domain} = "$1.$2";
329   # hmmmmmmmm.
330   } elsif ( $whois_hack && $recref->{domain} =~ /^([\w\-\.]+)$/ ) {
331     $recref->{domain} = $1;
332   } else {
333     return "Illegal domain ". $recref->{domain}.
334            " (or unknown registry - try \$whois_hack)";
335   }
336
337   $recref->{action} =~ /^(M|N)$/ or return "Illegal action";
338   $recref->{action} = $1;
339
340   if ( $recref->{catchall} ne '' ) {
341     my $svc_acct = qsearchs( 'svc_acct', { 'svcnum' => $recref->{catchall} } );
342     return "Unknown catchall" unless $svc_acct;
343   }
344
345   my $error = $self->ut_textn('purpose')
346            or $self->SUPER::check;
347   return $error if $error;
348
349 }
350
351 =item domain_record
352
353 =cut
354
355 sub domain_record {
356   my $self = shift;
357
358   my %order = (
359     SOA => 1,
360     NS => 2,
361     MX => 3,
362     CNAME => 4,
363     A => 5,
364   );
365
366   sort { $order{$a->rectype} <=> $order{$b->rectype} }
367     qsearch('domain_record', { svcnum => $self->svcnum } );
368
369 }
370
371 sub catchall_svc_acct {
372   my $self = shift;
373   if ( $self->catchall ) {
374     qsearchs( 'svc_acct', { 'svcnum' => $self->catchall } );
375   } else {
376     '';
377   }
378 }
379
380 =item whois
381
382 Returns the Net::Whois::Domain object (see L<Net::Whois>) for this domain, or
383 undef if the domain is not found in whois.
384
385 (If $FS::svc_domain::whois_hack is true, returns that in all cases instead.)
386
387 =cut
388
389 sub whois {
390   $whois_hack or new Net::Whois::Domain $_[0]->domain;
391 }
392
393 =item _whois
394
395 Depriciated.
396
397 =cut
398
399 sub _whois {
400   die "_whois depriciated";
401 }
402
403 =item submit_internic
404
405 Submits a registration email for this domain.
406
407 =cut
408
409 sub submit_internic {
410   #my $self = shift;
411   carp "submit_internic depreciated";
412 }
413
414 =back
415
416 =head1 BUGS
417
418 Delete doesn't send a registration template.
419
420 All registries should be supported.
421
422 Should change action to a real field.
423
424 The $recref stuff in sub check should be cleaned up.
425
426 =head1 SEE ALSO
427
428 L<FS::svc_Common>, L<FS::Record>, L<FS::Conf>, L<FS::cust_svc>,
429 L<FS::part_svc>, L<FS::cust_pkg>, L<Net::Whois>, schema.html from the base
430 documentation, config.html from the base documentation.
431
432 =cut
433
434 1;
435
436