Index: trunk/DNSDB.pm
===================================================================
--- trunk/DNSDB.pm	(revision 265)
+++ trunk/DNSDB.pm	(revision 269)
@@ -1352,5 +1352,4 @@
     # get the ID...
     ($rdns_id) = $dbh->selectrow_array("SELECT currval('revzones_rdns_id_seq')");
-#$dbh->selectrow_array("SELECT currval('users_user_id_seq')");
 
     _log($dbh, (rdns_id => $rdns_id, user_id => $userinfo{id}, group_id => $group, username => $userinfo{name},
@@ -1359,6 +1358,6 @@
     # ... and now we construct the standard records from the default set.  NB:  group should be variable.
     my $sth = $dbh->prepare("SELECT host,type,val,ttl FROM default_rev_records WHERE group_id=?");
-    my $sth_in = $dbh->prepare("INSERT INTO records (rdns_id,host,type,val,ttl)".
-	" VALUES ($rdns_id,?,?,?,?)");
+    my $sth_in = $dbh->prepare("INSERT INTO records (rdns_id,domain_id,host,type,val,ttl)".
+	" VALUES ($rdns_id,?,?,?,?,?)");
     $sth->execute($group);
     while (my ($host,$type,$val,$ttl) = $sth->fetchrow_array()) {
@@ -1369,4 +1368,5 @@
 	next if ($type == 65281 || $type == 65284);
       }
+
       $host =~ s/ADMINDOMAIN/$config{domain}/g;
 
@@ -1375,13 +1375,21 @@
       # While we're at it, we substitute $zone for ZONE in the value.
       if ($val eq 'ZONE') {
+##fixme?  do we care if we have multiple whole-zone templates?
 	$val = $zone->network;
       } elsif ($val =~ /ZONE/) {
 	my $tmpval = $val;
 	$tmpval =~ s/ZONE//;
+	# Bend the rules and allow single-trailing-number PTR or PTR template records to be inserted
+	# as either v4 or v6.  May make this an off-by-default config flag
+	# Note that the origin records that may trigger this **SHOULD** already have ZONE,\d
+	if ($type == 12 || $type == 65282) {
+	  $tmpval =~ s/[,.]/::/ if ($tmpval =~ /^[,.]\d+$/ && $zone->{isv6});
+	  $tmpval =~ s/[,:]+/./ if ($tmpval =~ /^(?:,|::)\d+$/ && !$zone->{isv6});
+	}
 	my $addr;
 	if (_ipparent($dbh, 'n', 'y', \$tmpval, $rdns_id, \$addr)) {
 	  $val = $addr->addr;
 	} else {
-	  $warnstr .= "Default record '$val $typemap{$type} $host' doesn't fit in $zone, skipping\n";
+	  $warnstr .= "\nDefault record '$val $typemap{$type} $host' doesn't fit in $zone, skipping";
 	  next;
 	}
@@ -1391,6 +1399,17 @@
       $host = _ZONE($zone, $host);
 
-##work
-      $sth_in->execute($host,$type,$val,$ttl);
+      # Fill in the forward domain ID if we can find it, otherwise:
+      # Coerce type down to PTR or PTR template if we can't
+      my $domid = 0;
+      if ($type >= 65280) {
+	if (!($domid = _hostparent($dbh, $host))) {
+	  $warnstr .= "\nRecord added as PTR instead of $typemap{$type};  domain not found for $host";
+	  $type = $reverse_typemap{PTR};
+	  $domid = 0;	# just to be explicit.
+	}
+      }
+
+      $sth_in->execute($domid,$host,$type,$val,$ttl);
+
       if ($typemap{$type} eq 'SOA') {
 	my @tmp1 = split /:/, $host;
@@ -1402,20 +1421,25 @@
       } else {
 	my $logentry = "[new $zone] Added record '$host $typemap{$type}";
-#	$logentry .= " [distance $dist]" if $typemap{$type} eq 'MX';
-#	$logentry .= " [priority $dist] [weight $weight] [port $port]" if $typemap{$type} eq 'SRV';
-	_log($dbh, (rdns_id => $rdns_id, user_id => $userinfo{id}, group_id => $group,
+	_log($dbh, (rdns_id => $rdns_id, domain_id => $domid, user_id => $userinfo{id}, group_id => $group,
 		username => $userinfo{name}, entry =>
 		$logentry." $val', TTL $ttl"));
-#print $logentry." $val', TTL $ttl\n";
       }
     }
+
+# Generate record based on provided pattern.  
+	my $addr;
+	if (_ipparent($dbh, 'n', 'y', \$tmpval, $rdns_id, \$addr)) {
+	  $val = $addr->addr;
+	} else {
+	  $warnstr .= "\nDefault record '$val $typemap{$type} $host' doesn't fit in $zone, skipping";
+	  next;
+	}
 
     # If there are warnings (presumably about default records skipped for cause) log them
     _log($dbh, (rdns_id => $rdns_id, user_id => $userinfo{id}, group_id => $group,
-	username => $userinfo{name}, entry => $warnstr))
+	username => $userinfo{name}, entry => "Warning(s) adding $zone:$warnstr"))
 	if $warnstr;
-#print $warnstr;
+
     # once we get here, we should have suceeded.
-#die "horribly\n";
     $dbh->commit;
   }; # end eval
