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