1 package FS::svc_domain;
4 use vars qw( @ISA $whois_hack $conf
5 @defaultrecords $soadefaultttl $soaemail $soaexpire $soamachine
9 use Scalar::Util qw( blessed );
12 use Net::Domain::TLD qw(tld_exists);
13 use FS::Record qw(fields qsearch qsearchs dbh);
16 use FS::svc_Parent_Mixin;
21 use FS::domain_record;
24 @ISA = qw( FS::svc_Parent_Mixin FS::svc_Common );
26 #ask FS::UID to run this stuff for us later
27 $FS::UID::callback{'FS::domain'} = sub {
30 @defaultrecords = $conf->config('defaultrecords');
31 $soadefaultttl = $conf->config('soadefaultttl');
32 $soaemail = $conf->config('soaemail');
33 $soaexpire = $conf->config('soaexpire');
34 $soamachine = $conf->config('soamachine');
35 $soarefresh = $conf->config('soarefresh');
36 $soaretry = $conf->config('soaretry');
42 FS::svc_domain - Object methods for svc_domain records
48 $record = new FS::svc_domain \%hash;
49 $record = new FS::svc_domain { 'column' => 'value' };
51 $error = $record->insert;
53 $error = $new_record->replace($old_record);
55 $error = $record->delete;
57 $error = $record->check;
59 $error = $record->suspend;
61 $error = $record->unsuspend;
63 $error = $record->cancel;
67 An FS::svc_domain object represents a domain. FS::svc_domain inherits from
68 FS::svc_Common. The following fields are currently supported:
72 =item svcnum - primary key (assigned automatically for new accounts)
76 =item catchall - optional svcnum of an svc_acct record, designating an email catchall account.
82 =item registrarnum - Registrar (see L<FS::registrar>)
84 =item registrarkey - Registrar key or password for this domain
86 =item setup_date - UNIX timestamp
88 =item renewal_interval - Number of days before expiration date to start renewal
90 =item expiration_date - UNIX timestamp
102 Creates a new domain. To add the domain to the database, see L<"insert">.
110 'display_weight' => 20,
111 'cancel_weight' => 60,
113 'domain' => 'Domain',
114 'max_accounts' => { label => 'Maximum number of accounts',
115 'disable_inventory' => 1,
118 label => 'Communigate aliases',
120 disable_inventory => 1,
123 'cgp_accessmodes' => {
124 label => 'Communigate enabled services',
125 type => 'communigate_pro-accessmodes',
126 disable_inventory => 1,
133 sub table { 'svc_domain'; }
136 my($class, $string) = @_;
137 $class->search_sql_field('domain', $string);
152 =item insert [ , OPTION => VALUE ... ]
154 Adds this domain to the database. If there is an error, returns the error,
155 otherwise returns false.
157 The additional fields I<pkgnum> and I<svcpart> (see L<FS::cust_svc>) should be
158 defined. An FS::cust_svc record will be created and inserted.
160 The additional field I<action> should be set to I<N> for new domains, I<M>
161 for transfers, or I<I> for no action (registered elsewhere).
163 A registration or transfer email will be submitted unless
164 $FS::svc_domain::whois_hack is true.
166 The additional field I<email> can be used to manually set the admin contact
167 email address on this email. Otherwise, the svc_acct records for this package
168 (see L<FS::cust_pkg>) are searched. If there is exactly one svc_acct record
169 in the same package, it is automatically used. Otherwise an error is returned.
171 If any I<soamachine> configuration file exists, an SOA record is added to
172 the domain_record table (see <FS::domain_record>).
174 If any records are defined in the I<defaultrecords> configuration file,
175 appropriate records are added to the domain_record table (see
176 L<FS::domain_record>).
178 Currently available options are: I<depend_jobnum>
180 If I<depend_jobnum> is set (to a scalar jobnum or an array reference of
181 jobnums), all provisioning jobs will have a dependancy on the supplied
182 jobnum(s) (they will not run until the specific job(s) complete(s)).
190 local $SIG{HUP} = 'IGNORE';
191 local $SIG{INT} = 'IGNORE';
192 local $SIG{QUIT} = 'IGNORE';
193 local $SIG{TERM} = 'IGNORE';
194 local $SIG{TSTP} = 'IGNORE';
195 local $SIG{PIPE} = 'IGNORE';
197 my $oldAutoCommit = $FS::UID::AutoCommit;
198 local $FS::UID::AutoCommit = 0;
201 $error = $self->SUPER::insert(@_);
203 $dbh->rollback if $oldAutoCommit;
208 my $soa = new FS::domain_record {
209 'svcnum' => $self->svcnum,
213 'recdata' => "$soamachine $soaemail ( ". time2str("%Y%m%d", time). "00 ".
214 "$soarefresh $soaretry $soaexpire $soadefaultttl )"
216 $error = $soa->insert;
218 $dbh->rollback if $oldAutoCommit;
219 return "couldn't insert SOA record for new domain: $error";
222 foreach my $record ( @defaultrecords ) {
223 my($zone,$af,$type,$data) = split(/\s+/,$record,4);
224 my $domain_record = new FS::domain_record {
225 'svcnum' => $self->svcnum,
231 my $error = $domain_record->insert;
233 $dbh->rollback if $oldAutoCommit;
234 return "couldn't insert record for new domain: $error";
240 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
247 Deletes this domain from the database. If there is an error, returns the
248 error, otherwise returns false.
250 The corresponding FS::cust_svc record will be deleted as well.
257 return "Can't delete a domain which has accounts!"
258 if qsearch( 'svc_acct', { 'domsvc' => $self->svcnum } );
260 #return "Can't delete a domain with (domain_record) zone entries!"
261 # if qsearch('domain_record', { 'svcnum' => $self->svcnum } );
263 local $SIG{HUP} = 'IGNORE';
264 local $SIG{INT} = 'IGNORE';
265 local $SIG{QUIT} = 'IGNORE';
266 local $SIG{TERM} = 'IGNORE';
267 local $SIG{TSTP} = 'IGNORE';
268 local $SIG{PIPE} = 'IGNORE';
270 my $oldAutoCommit = $FS::UID::AutoCommit;
271 local $FS::UID::AutoCommit = 0;
274 foreach my $domain_record ( reverse $self->domain_record ) {
275 my $error = $domain_record->delete;
277 $dbh->rollback if $oldAutoCommit;
278 return "can't delete DNS entry: ".
279 join(' ', map $domain_record->$_(),
280 qw( reczone recaf rectype recdata )
286 my $error = $self->SUPER::delete(@_);
288 $dbh->rollback if $oldAutoCommit;
292 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
295 =item replace OLD_RECORD
297 Replaces OLD_RECORD with this one in the database. If there is an error,
298 returns the error, otherwise returns false.
305 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
309 return "Can't change domain - reorder."
310 if $old->getfield('domain') ne $new->getfield('domain')
311 && ! $conf->exists('svc_domain-edit_domain');
313 # Better to do it here than to force the caller to remember that svc_domain is weird.
314 $new->setfield(action => 'I');
315 my $error = $new->SUPER::replace($old, @_);
316 return $error if $error;
321 Just returns false (no error) for now.
323 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
327 Just returns false (no error) for now.
329 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
333 Just returns false (no error) for now.
335 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
339 Checks all fields to make sure this is a valid domain. If there is an error,
340 returns the error, otherwise returns false. Called by the insert and replace
343 Sets any fixed values; see L<FS::part_svc>.
350 my $x = $self->setfixed;
351 return $x unless ref($x);
354 my $error = $self->ut_numbern('svcnum')
355 || $self->ut_numbern('catchall')
356 || $self->ut_numbern('max_accounts')
357 || $self->ut_textn('cgp_aliases') #well
359 return $error if $error;
363 if ( $self->svcnum ) {
364 my $cust_svc = qsearchs( 'cust_svc', { 'svcnum' => $self->svcnum } );
365 $pkgnum = $cust_svc->pkgnum;
367 $pkgnum = $self->pkgnum;
370 my($recref) = $self->hashref;
372 #if ( $recref->{domain} =~ /^([\w\-\.]{1,22})\.(com|net|org|edu)$/ ) {
373 if ( $recref->{domain} =~ /^([\w\-]{1,63})\.(com|net|org|edu|tv|info|biz)$/ ) {
374 $recref->{domain} = "$1.$2";
375 $recref->{suffix} ||= $2;
377 } elsif ( $whois_hack && $recref->{domain} =~ /^([\w\-\.]+)\.(\w+)$/ ) {
378 $recref->{domain} = "$1.$2";
379 # need to match a list of suffixes - no guarantee they're top-level..
380 # http://wiki.mozilla.org/TLD_List
381 # but this will have to do for now...
382 $recref->{suffix} ||= $2;
384 return "Illegal domain ". $recref->{domain}.
385 " (or unknown registry - try \$whois_hack)";
388 $self->suffix =~ /(^|\.)(\w+)$/
389 or return "can't parse suffix for TLD: ". $self->suffix;
391 return "No such TLD: .$tld" unless tld_exists($tld);
393 if ( $recref->{catchall} ne '' ) {
394 my $svc_acct = qsearchs( 'svc_acct', { 'svcnum' => $recref->{catchall} } );
395 return "Unknown catchall" unless $svc_acct;
398 $self->ut_alphan('suffix')
399 or $self->ut_foreign_keyn('registrarnum', 'registrar', 'registrarnum')
400 or $self->ut_textn('registrarkey')
401 or $self->ut_numbern('setup_date')
402 or $self->ut_numbern('renewal_interval')
403 or $self->ut_numbern('expiration_date')
404 or $self->SUPER::check;
408 sub _check_duplicate {
413 if ( qsearchs( 'svc_domain', { 'domain' => $self->domain } ) ) {
414 return "Domain in use (here)";
438 #'SOA' => sub { $_[0]->recdata cmp $_[1]->recdata }, #sure hope not though
439 # 'SOA' => sub { 0; },
440 # 'NS' => sub { 0; },
441 'MX' => sub { my( $a_weight, $a_name ) = split(/\s+/, $_[0]->recdata);
442 my( $b_weight, $b_name ) = split(/\s+/, $_[1]->recdata);
443 $a_weight <=> $b_weight or $a_name cmp $b_name;
445 'CNAME' => sub { $_[0]->reczone cmp $_[1]->reczone },
446 'A' => sub { $_[0]->reczone cmp $_[1]->reczone },
448 # 'TXT' => sub { 0; },
449 'PTR' => sub { $_[0]->reczone <=> $_[1]->reczone },
452 map { $_ } #return $self->num_domain_record( PARAMS ) unless wantarray;
453 sort { $order{$a->rectype} <=> $order{$b->rectype}
454 or &{ $sort{$a->rectype} || sub { 0; } }($a, $b)
456 qsearch('domain_record', { svcnum => $self->svcnum } );
460 sub catchall_svc_acct {
462 if ( $self->catchall ) {
463 qsearchs( 'svc_acct', { 'svcnum' => $self->catchall } );
471 # Returns the Net::Whois::Domain object (see L<Net::Whois>) for this domain, or
472 # undef if the domain is not found in whois.
474 (If $FS::svc_domain::whois_hack is true, returns that in all cases instead.)
479 #$whois_hack or new Net::Whois::Domain $_[0]->domain;
480 #$whois_hack or die "whois_hack not set...\n";
487 Delete doesn't send a registration template.
489 All registries should be supported.
491 Should change action to a real field.
493 The $recref stuff in sub check should be cleaned up.
497 L<FS::svc_Common>, L<FS::Record>, L<FS::Conf>, L<FS::cust_svc>,
498 L<FS::part_svc>, L<FS::cust_pkg>, L<Net::Whois>, schema.html from the base
499 documentation, config.html from the base documentation.