1 package FS::svc_domain;
4 use vars qw( @ISA $whois_hack $conf
5 @defaultrecords $soadefaultttl $soaemail $soaexpire $soamachine
11 use FS::Record qw(fields qsearch qsearchs dbh);
14 use FS::svc_Parent_Mixin;
19 use FS::domain_record;
22 @ISA = qw( FS::svc_Parent_Mixin FS::svc_Common );
24 #ask FS::UID to run this stuff for us later
25 $FS::UID::callback{'FS::domain'} = sub {
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');
40 FS::svc_domain - Object methods for svc_domain records
46 $record = new FS::svc_domain \%hash;
47 $record = new FS::svc_domain { 'column' => 'value' };
49 $error = $record->insert;
51 $error = $new_record->replace($old_record);
53 $error = $record->delete;
55 $error = $record->check;
57 $error = $record->suspend;
59 $error = $record->unsuspend;
61 $error = $record->cancel;
65 An FS::svc_domain object represents a domain. FS::svc_domain inherits from
66 FS::svc_Common. The following fields are currently supported:
70 =item svcnum - primary key (assigned automatically for new accounts)
74 =item catchall - optional svcnum of an svc_acct record, designating an email catchall account.
80 =item registrarnum - Registrar (see L<FS::registrar>)
82 =item registrarkey - Registrar key or password for this domain
84 =item setup_date - UNIX timestamp
86 =item renewal_interval - Number of days before expiration date to start renewal
88 =item expiration_date - UNIX timestamp
98 Creates a new domain. To add the domain to the database, see L<"insert">.
106 'display_weight' => 20,
107 'cancel_weight' => 60,
109 'domain' => 'Domain',
114 sub table { 'svc_domain'; }
117 my($class, $string) = @_;
118 $class->search_sql_field('domain', $string);
133 =item insert [ , OPTION => VALUE ... ]
135 Adds this domain to the database. If there is an error, returns the error,
136 otherwise returns false.
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.
141 The additional field I<action> should be set to I<N> for new domains or I<M>
144 A registration or transfer email will be submitted unless
145 $FS::svc_domain::whois_hack is true.
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.
152 If any I<soamachine> configuration file exists, an SOA record is added to
153 the domain_record table (see <FS::domain_record>).
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>).
159 Currently available options are: I<depend_jobnum>
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)).
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';
178 my $oldAutoCommit = $FS::UID::AutoCommit;
179 local $FS::UID::AutoCommit = 0;
182 $error = $self->SUPER::insert(@_);
184 $dbh->rollback if $oldAutoCommit;
189 my $soa = new FS::domain_record {
190 'svcnum' => $self->svcnum,
194 'recdata' => "$soamachine $soaemail ( ". time2str("%Y%m%d", time). "00 ".
195 "$soarefresh $soaretry $soaexpire $soadefaultttl )"
197 $error = $soa->insert;
199 $dbh->rollback if $oldAutoCommit;
200 return "couldn't insert SOA record for new domain: $error";
203 foreach my $record ( @defaultrecords ) {
204 my($zone,$af,$type,$data) = split(/\s+/,$record,4);
205 my $domain_record = new FS::domain_record {
206 'svcnum' => $self->svcnum,
212 my $error = $domain_record->insert;
214 $dbh->rollback if $oldAutoCommit;
215 return "couldn't insert record for new domain: $error";
221 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
228 Deletes this domain from the database. If there is an error, returns the
229 error, otherwise returns false.
231 The corresponding FS::cust_svc record will be deleted as well.
238 return "Can't delete a domain which has accounts!"
239 if qsearch( 'svc_acct', { 'domsvc' => $self->svcnum } );
241 #return "Can't delete a domain with (domain_record) zone entries!"
242 # if qsearch('domain_record', { 'svcnum' => $self->svcnum } );
244 local $SIG{HUP} = 'IGNORE';
245 local $SIG{INT} = 'IGNORE';
246 local $SIG{QUIT} = 'IGNORE';
247 local $SIG{TERM} = 'IGNORE';
248 local $SIG{TSTP} = 'IGNORE';
249 local $SIG{PIPE} = 'IGNORE';
251 my $oldAutoCommit = $FS::UID::AutoCommit;
252 local $FS::UID::AutoCommit = 0;
255 foreach my $domain_record ( reverse $self->domain_record ) {
256 my $error = $domain_record->delete;
258 $dbh->rollback if $oldAutoCommit;
259 return "can't delete DNS entry: ".
260 join(' ', map $domain_record->$_(),
261 qw( reczone recaf rectype recdata )
267 my $error = $self->SUPER::delete;
269 $dbh->rollback if $oldAutoCommit;
273 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
276 =item replace OLD_RECORD
278 Replaces OLD_RECORD with this one in the database. If there is an error,
279 returns the error, otherwise returns false.
284 my ( $new, $old ) = ( shift, shift );
286 # We absolutely have to have an old vs. new record to make this work.
287 $old = $new->replace_old unless defined($old);
289 return "Can't change domain - reorder."
290 if $old->getfield('domain') ne $new->getfield('domain');
292 # Better to do it here than to force the caller to remember that svc_domain is weird.
293 $new->setfield(action => 'M');
294 my $error = $new->SUPER::replace($old);
295 return $error if $error;
300 Just returns false (no error) for now.
302 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
306 Just returns false (no error) for now.
308 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
312 Just returns false (no error) for now.
314 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
318 Checks all fields to make sure this is a valid domain. If there is an error,
319 returns the error, otherwise returns false. Called by the insert and replace
322 Sets any fixed values; see L<FS::part_svc>.
329 my $x = $self->setfixed;
330 return $x unless ref($x);
333 my $error = $self->ut_numbern('svcnum')
334 || $self->ut_numbern('catchall')
336 return $error if $error;
340 if ( $self->svcnum ) {
341 my $cust_svc = qsearchs( 'cust_svc', { 'svcnum' => $self->svcnum } );
342 $pkgnum = $cust_svc->pkgnum;
344 $pkgnum = $self->pkgnum;
347 my($recref) = $self->hashref;
349 #if ( $recref->{domain} =~ /^([\w\-\.]{1,22})\.(com|net|org|edu)$/ ) {
350 if ( $recref->{domain} =~ /^([\w\-]{1,63})\.(com|net|org|edu|tv|info|biz)$/ ) {
351 $recref->{domain} = "$1.$2";
352 $recref->{suffix} ||= $2;
354 } elsif ( $whois_hack && $recref->{domain} =~ /^([\w\-\.]+)\.(\w+)$/ ) {
355 $recref->{domain} = "$1.$2";
356 # need to match a list of suffixes - no guarantee they're top-level..
358 return "Illegal domain ". $recref->{domain}.
359 " (or unknown registry - try \$whois_hack)";
363 if ( $recref->{catchall} ne '' ) {
364 my $svc_acct = qsearchs( 'svc_acct', { 'svcnum' => $recref->{catchall} } );
365 return "Unknown catchall" unless $svc_acct;
368 $self->ut_alphan('suffix')
369 or $self->ut_foreign_keyn('registrarnum', 'registrar', 'registrarnum')
370 or $self->ut_textn('registrarkey')
371 or $self->ut_numbern('setup_date')
372 or $self->ut_numbern('renewal_interval')
373 or $self->ut_numbern('expiration_date')
374 or $self->SUPER::check;
378 sub _check_duplicate {
383 if ( qsearchs( 'svc_domain', { 'domain' => $self->domain } ) ) {
384 return "Domain in use (here)";
408 #'SOA' => sub { $_[0]->recdata cmp $_[1]->recdata }, #sure hope not though
409 # 'SOA' => sub { 0; },
410 # 'NS' => sub { 0; },
411 'MX' => sub { my( $a_weight, $a_name ) = split(/\s+/, $_[0]->recdata);
412 my( $b_weight, $b_name ) = split(/\s+/, $_[1]->recdata);
413 $a_weight <=> $b_weight or $a_name cmp $b_name;
415 'CNAME' => sub { $_[0]->reczone cmp $_[1]->reczone },
416 'A' => sub { $_[0]->reczone cmp $_[1]->reczone },
418 # 'TXT' => sub { 0; },
419 'PTR' => sub { $_[0]->reczone <=> $_[1]->reczone },
422 sort { $order{$a->rectype} <=> $order{$b->rectype}
423 or &{ $sort{$a->rectype} || sub { 0; } }($a, $b)
425 qsearch('domain_record', { svcnum => $self->svcnum } );
429 sub catchall_svc_acct {
431 if ( $self->catchall ) {
432 qsearchs( 'svc_acct', { 'svcnum' => $self->catchall } );
440 # Returns the Net::Whois::Domain object (see L<Net::Whois>) for this domain, or
441 # undef if the domain is not found in whois.
443 (If $FS::svc_domain::whois_hack is true, returns that in all cases instead.)
448 #$whois_hack or new Net::Whois::Domain $_[0]->domain;
449 #$whois_hack or die "whois_hack not set...\n";
459 die "_whois deprecated";
466 Delete doesn't send a registration template.
468 All registries should be supported.
470 Should change action to a real field.
472 The $recref stuff in sub check should be cleaned up.
476 L<FS::svc_Common>, L<FS::Record>, L<FS::Conf>, L<FS::cust_svc>,
477 L<FS::part_svc>, L<FS::cust_pkg>, L<Net::Whois>, schema.html from the base
478 documentation, config.html from the base documentation.