SOA serial number problem with Date::Format %e: no leading zero
[freeside.git] / FS / FS / svc_domain.pm
index c6d1248..01aa21a 100644 (file)
@@ -3,19 +3,22 @@ package FS::svc_domain;
 use strict;
 use vars qw( @ISA $whois_hack $conf $mydomain $smtpmachine
   $tech_contact $from $to @nameservers @nameserver_ips @template
+  @mxmachines @nsmachines $soadefaultttl $soaemail $soaexpire $soamachine
+  $soarefresh $soaretry
 );
 use Carp;
 use Mail::Internet;
 use Mail::Header;
 use Date::Format;
-use Net::Whois; #0.24;
-use FS::Record qw(fields qsearch qsearchs);
+use Net::Whois 1.0;
+use FS::Record qw(fields qsearch qsearchs dbh);
 use FS::Conf;
 use FS::svc_Common;
 use FS::cust_svc;
 use FS::svc_acct;
 use FS::cust_pkg;
 use FS::cust_main;
+use FS::domain_record;
 
 @ISA = qw( FS::svc_Common );
 
@@ -43,6 +46,15 @@ $FS::UID::callback{'FS::domain'} = sub {
   } @ns;
   @template = map { $_. "\n" } $conf->config("$internic/template");
 
+  @mxmachines    = $conf->config('mxmachines');
+  @nsmachines    = $conf->config('nsmachines');
+  $soadefaultttl = $conf->config('soadefaultttl');
+  $soaemail      = $conf->config('soaemail');
+  $soaexpire     = $conf->config('soaexpire');
+  $soamachine    = $conf->config('soamachine');
+  $soarefresh    = $conf->config('soarefresh');
+  $soaretry      = $conf->config('soaretry');
+
 };
 
 =head1 NAME
@@ -114,6 +126,19 @@ email address on this email.  Otherwise, the svc_acct records for this package
 (see L<FS::cust_pkg>) are searched.  If there is exactly one svc_acct record
 in the same package, it is automatically used.  Otherwise an error is returned.
 
+If any I<soamachine> configuration file exists, an SOA record is added to
+the domain_record table (see <FS::domain_record>).
+
+If any machines are defined in the I<nsmachines> configuration file, NS
+records are added to the domain_record table (see L<FS::domain_record>).
+
+If any machines are defined in the I<mxmachines> configuration file, MX
+records are added to the domain_record table (see L<FS::domain_record>).
+
+Any problems adding FS::domain_record records will emit warnings, but will
+not return errors from this method.  If your configuration files are correct
+you shouln't have any problems.
+
 =cut
 
 sub insert {
@@ -127,6 +152,10 @@ sub insert {
   local $SIG{TSTP} = 'IGNORE';
   local $SIG{PIPE} = 'IGNORE';
 
+  my $oldAutoCommit = $FS::UID::AutoCommit;
+  local $FS::UID::AutoCommit = 0;
+  my $dbh = dbh;
+
   $error = $self->check;
   return $error if $error;
 
@@ -134,16 +163,72 @@ sub insert {
     if qsearchs( 'svc_domain', { 'domain' => $self->domain } );
 
   my $whois = $self->whois;
-  return "Domain in use (see whois)"
-    if ( $self->action eq "N" && $whois );
-  return "Domain not found (see whois)"
-    if ( $self->action eq "M" && ! $whois );
+  if ( $self->action eq "N" && ! $whois_hack && $whois ) {
+    $dbh->rollback if $oldAutoCommit;
+    return "Domain in use (see whois)";
+  }
+  if ( $self->action eq "M" && ! $whois ) {
+    $dbh->rollback if $oldAutoCommit;
+    return "Domain not found (see whois)";
+  }
 
   $error = $self->SUPER::insert;
-  return $error if $error;
+  if ( $error ) {
+    $dbh->rollback if $oldAutoCommit;
+    return $error;
+  }
 
   $self->submit_internic unless $whois_hack;
 
+  if ( $soamachine ) {
+    my $soa = new FS::domain_record {
+      'svcnum'  => $self->svcnum,
+      'reczone' => '@',
+      'recaf'   => 'IN',
+      'rectype' => 'SOA',
+      'recdata' => "$soamachine $soaemail ( ". time2str("%Y%m%d", time). "00 ".
+                   "$soarefresh $soaretry $soaexpire $soadefaultttl )"
+    };
+    $error = $soa->insert;
+    if ( $error ) {
+      $dbh->rollback if $oldAutoCommit;
+      return "couldn't insert SOA record for new domain: $error";
+    }
+
+    foreach my $nsmachine ( @nsmachines ) {
+      my $ns = new FS::domain_record {
+        'svcnum'  => $self->svcnum,
+        'reczone' => '@',
+        'recaf'   => 'IN',
+        'rectype' => 'NS',
+        'recdata' => $nsmachine,
+      };
+      my $error = $ns->insert;
+      if ( $error ) {
+        $dbh->rollback if $oldAutoCommit;
+        return "couldn't insert NS record for new domain: $error";
+      }
+    }
+
+    foreach my $mxmachine ( @mxmachines ) {
+      my $mx = new FS::domain_record {
+        'svcnum'  => $self->svcnum,
+        'reczone' => '@',
+        'recaf'   => 'IN',
+        'rectype' => 'MX',
+        'recdata' => $mxmachine,
+      };
+      my $error = $mx->insert;
+      if ( $error ) {
+        $dbh->rollback if $oldAutoCommit;
+        return "couldn't insert MX record for new domain: $error";
+      }
+    }
+
+  }
+
+  $dbh->commit or die $dbh->errstr if $oldAutoCommit;
+
   ''; #no error
 }
 
@@ -257,8 +342,8 @@ sub check {
 
 =item whois
 
-Returns the Net::Whois object corresponding to this domain, or undef if
-the domain is not found in whois.
+Returns the Net::Whois::Domain object (see L<Net::Whois>) for this domain, or
+undef if the domain is not found in whois.
 
 (If $FS::svc_domain::whois_hack is true, returns that in all cases instead.)
 
@@ -393,7 +478,7 @@ sub submit_internic {
 
 =head1 VERSION
 
-$Id: svc_domain.pm,v 1.1 1999-08-04 09:03:53 ivan Exp $
+$Id: svc_domain.pm,v 1.12 2001-06-03 17:22:52 ivan Exp $
 
 =head1 BUGS
 
@@ -410,7 +495,7 @@ The $recref stuff in sub check should be cleaned up.
 =head1 SEE ALSO
 
 L<FS::svc_Common>, L<FS::Record>, L<FS::Conf>, L<FS::cust_svc>,
-L<FS::part_svc>, L<FS::cust_pkg>, L<FS::SSH>, L<Net::Whois>, L<ssh>,
+L<FS::part_svc>, L<FS::cust_pkg>, L<Net::Whois>, L<ssh>,
 L<dot-qmail>, schema.html from the base documentation, config.html from the
 base documentation.