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