1 package FS::svc_domain;
4 use vars qw( @ISA $whois_hack $conf $smtpmachine
5 @defaultrecords $soadefaultttl $soaemail $soaexpire $soamachine
9 use Mail::Internet 1.44;
13 use FS::Record qw(fields qsearch qsearchs dbh);
20 use FS::domain_record;
23 @ISA = qw( FS::svc_Common );
25 #ask FS::UID to run this stuff for us later
26 $FS::UID::callback{'FS::domain'} = sub {
29 $smtpmachine = $conf->config('smtpmachine');
31 @defaultrecords = $conf->config('defaultrecords');
32 $soadefaultttl = $conf->config('soadefaultttl');
33 $soaemail = $conf->config('soaemail');
34 $soaexpire = $conf->config('soaexpire');
35 $soamachine = $conf->config('soamachine');
36 $soarefresh = $conf->config('soarefresh');
37 $soaretry = $conf->config('soaretry');
43 FS::svc_domain - Object methods for svc_domain records
49 $record = new FS::svc_domain \%hash;
50 $record = new FS::svc_domain { 'column' => 'value' };
52 $error = $record->insert;
54 $error = $new_record->replace($old_record);
56 $error = $record->delete;
58 $error = $record->check;
60 $error = $record->suspend;
62 $error = $record->unsuspend;
64 $error = $record->cancel;
68 An FS::svc_domain object represents a domain. FS::svc_domain inherits from
69 FS::svc_Common. The following fields are currently supported:
73 =item svcnum - primary key (assigned automatically for new accounts)
77 =item catchall - optional svcnum of an svc_acct record, designating an email catchall account.
87 Creates a new domain. To add the domain to the database, see L<"insert">.
91 sub table { 'svc_domain'; }
95 Adds this domain to the database. If there is an error, returns the error,
96 otherwise returns false.
98 The additional fields I<pkgnum> and I<svcpart> (see L<FS::cust_svc>) should be
99 defined. An FS::cust_svc record will be created and inserted.
101 The additional field I<action> should be set to I<N> for new domains or I<M>
104 A registration or transfer email will be submitted unless
105 $FS::svc_domain::whois_hack is true.
107 The additional field I<email> can be used to manually set the admin contact
108 email address on this email. Otherwise, the svc_acct records for this package
109 (see L<FS::cust_pkg>) are searched. If there is exactly one svc_acct record
110 in the same package, it is automatically used. Otherwise an error is returned.
112 If any I<soamachine> configuration file exists, an SOA record is added to
113 the domain_record table (see <FS::domain_record>).
115 If any records are defined in the I<defaultrecords> configuration file,
116 appropriate records are added to the domain_record table (see
117 L<FS::domain_record>).
125 local $SIG{HUP} = 'IGNORE';
126 local $SIG{INT} = 'IGNORE';
127 local $SIG{QUIT} = 'IGNORE';
128 local $SIG{TERM} = 'IGNORE';
129 local $SIG{TSTP} = 'IGNORE';
130 local $SIG{PIPE} = 'IGNORE';
132 my $oldAutoCommit = $FS::UID::AutoCommit;
133 local $FS::UID::AutoCommit = 0;
136 $error = $self->check;
137 return $error if $error;
139 return "Domain in use (here)"
140 if qsearchs( 'svc_domain', { 'domain' => $self->domain } );
142 my $whois = $self->whois;
143 if ( $self->action eq "N" && ! $whois_hack && $whois ) {
144 $dbh->rollback if $oldAutoCommit;
145 return "Domain in use (see whois)";
147 if ( $self->action eq "M" && ! $whois ) {
148 $dbh->rollback if $oldAutoCommit;
149 return "Domain not found (see whois)";
152 $error = $self->SUPER::insert;
154 $dbh->rollback if $oldAutoCommit;
158 $self->submit_internic unless $whois_hack;
161 my $soa = new FS::domain_record {
162 'svcnum' => $self->svcnum,
166 'recdata' => "$soamachine $soaemail ( ". time2str("%Y%m%d", time). "00 ".
167 "$soarefresh $soaretry $soaexpire $soadefaultttl )"
169 $error = $soa->insert;
171 $dbh->rollback if $oldAutoCommit;
172 return "couldn't insert SOA record for new domain: $error";
175 foreach my $record ( @defaultrecords ) {
176 my($zone,$af,$type,$data) = split(/\s+/,$record,4);
177 my $domain_record = new FS::domain_record {
178 'svcnum' => $self->svcnum,
184 my $error = $domain_record->insert;
186 $dbh->rollback if $oldAutoCommit;
187 return "couldn't insert record for new domain: $error";
193 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
200 Deletes this domain from the database. If there is an error, returns the
201 error, otherwise returns false.
203 The corresponding FS::cust_svc record will be deleted as well.
210 return "Can't delete a domain which has accounts!"
211 if qsearch( 'svc_acct', { 'domsvc' => $self->svcnum } );
213 return "Can't delete a domain with (svc_acct_sm) mail aliases!"
214 if defined( $FS::Record::dbdef->table('svc_acct_sm') )
215 && qsearch('svc_acct_sm', { 'domsvc' => $self->svcnum } );
217 #return "Can't delete a domain with (domain_record) zone entries!"
218 # if qsearch('domain_record', { 'svcnum' => $self->svcnum } );
220 local $SIG{HUP} = 'IGNORE';
221 local $SIG{INT} = 'IGNORE';
222 local $SIG{QUIT} = 'IGNORE';
223 local $SIG{TERM} = 'IGNORE';
224 local $SIG{TSTP} = 'IGNORE';
225 local $SIG{PIPE} = 'IGNORE';
227 my $oldAutoCommit = $FS::UID::AutoCommit;
228 local $FS::UID::AutoCommit = 0;
231 foreach my $domain_record ( reverse $self->domain_record ) {
232 my $error = $domain_record->delete;
234 $dbh->rollback if $oldAutoCommit;
239 my $error = $self->SUPER::delete;
241 $dbh->rollback if $oldAutoCommit;
245 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
248 =item replace OLD_RECORD
250 Replaces OLD_RECORD with this one in the database. If there is an error,
251 returns the error, otherwise returns false.
256 my ( $new, $old ) = ( shift, shift );
258 return "Can't change domain - reorder."
259 if $old->getfield('domain') ne $new->getfield('domain');
261 my $error = $new->SUPER::replace($old);
262 return $error if $error;
267 Just returns false (no error) for now.
269 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
273 Just returns false (no error) for now.
275 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
279 Just returns false (no error) for now.
281 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
285 Checks all fields to make sure this is a valid domain. If there is an error,
286 returns the error, otherwise returns false. Called by the insert and replace
289 Sets any fixed values; see L<FS::part_svc>.
296 my $x = $self->setfixed;
297 return $x unless ref($x);
300 my $error = $self->ut_numbern('svcnum')
301 || $self->ut_numbern('catchall')
303 return $error if $error;
307 if ( $self->svcnum ) {
308 my $cust_svc = qsearchs( 'cust_svc', { 'svcnum' => $self->svcnum } );
309 $pkgnum = $cust_svc->pkgnum;
311 $pkgnum = $self->pkgnum;
314 my($recref) = $self->hashref;
316 unless ( $whois_hack ) {
317 unless ( $self->email ) { #find out an email address
319 foreach ( qsearch( 'cust_svc', { 'pkgnum' => $pkgnum } ) ) {
320 my $svc_acct = qsearchs( 'svc_acct', { 'svcnum' => $_->svcnum } );
321 push @svc_acct, $svc_acct if $svc_acct;
324 if ( scalar(@svc_acct) == 0 ) {
325 return "Must order an account in package ". $pkgnum. " first";
326 } elsif ( scalar(@svc_acct) > 1 ) {
327 return "More than one account in package ". $pkgnum. ": specify admin contact email";
329 $self->email($svc_acct[0]->email );
334 #if ( $recref->{domain} =~ /^([\w\-\.]{1,22})\.(com|net|org|edu)$/ ) {
335 if ( $recref->{domain} =~ /^([\w\-]{1,63})\.(com|net|org|edu)$/ ) {
336 $recref->{domain} = "$1.$2";
338 } elsif ( $whois_hack && $recref->{domain} =~ /^([\w\-\.]+)$/ ) {
339 $recref->{domain} = $1;
341 return "Illegal domain ". $recref->{domain}.
342 " (or unknown registry - try \$whois_hack)";
345 $recref->{action} =~ /^(M|N)$/ or return "Illegal action";
346 $recref->{action} = $1;
348 if ( $recref->{catchall} ne '' ) {
349 my $svc_acct = qsearchs( 'svc_acct', { 'svcnum' => $recref->{catchall} } );
350 return "Unknown catchall" unless $svc_acct;
353 $self->ut_textn('purpose');
372 sort { $order{$a->rectype} <=> $order{$b->rectype} }
373 qsearch('domain_record', { svcnum => $self->svcnum } );
377 sub catchall_svc_acct {
379 if ( $self->catchall ) {
380 qsearchs( 'svc_acct', { 'svcnum' => $self->catchall } );
388 Returns the Net::Whois::Domain object (see L<Net::Whois>) for this domain, or
389 undef if the domain is not found in whois.
391 (If $FS::svc_domain::whois_hack is true, returns that in all cases instead.)
396 $whois_hack or new Net::Whois::Domain $_[0]->domain;
406 die "_whois depriciated";
409 =item submit_internic
411 Submits a registration email for this domain.
415 sub submit_internic {
417 carp "submit_internic depreciated";
424 Delete doesn't send a registration template.
426 All registries should be supported.
428 Should change action to a real field.
430 The $recref stuff in sub check should be cleaned up.
434 L<FS::svc_Common>, L<FS::Record>, L<FS::Conf>, L<FS::cust_svc>,
435 L<FS::part_svc>, L<FS::cust_pkg>, L<Net::Whois>, schema.html from the base
436 documentation, config.html from the base documentation.