- mysql 4.1 is available; update documentation
[freeside.git] / FS / FS / svc_domain.pm
1 package FS::svc_domain;
2
3 use strict;
4 use vars qw( @ISA $whois_hack $conf $smtpmachine
5   @defaultrecords $soadefaultttl $soaemail $soaexpire $soamachine
6   $soarefresh $soaretry
7 );
8 use Carp;
9 use Mail::Internet 1.44;
10 use Mail::Header;
11 use Date::Format;
12 use Net::Whois 1.0;
13 use FS::Record qw(fields qsearch qsearchs dbh);
14 use FS::Conf;
15 use FS::svc_Common;
16 use FS::cust_svc;
17 use FS::svc_acct;
18 use FS::cust_pkg;
19 use FS::cust_main;
20 use FS::domain_record;
21 use FS::queue;
22
23 @ISA = qw( FS::svc_Common );
24
25 #ask FS::UID to run this stuff for us later
26 $FS::UID::callback{'FS::domain'} = sub { 
27   $conf = new FS::Conf;
28
29   $smtpmachine = $conf->config('smtpmachine');
30
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');
38
39 };
40
41 =head1 NAME
42
43 FS::svc_domain - Object methods for svc_domain records
44
45 =head1 SYNOPSIS
46
47   use FS::svc_domain;
48
49   $record = new FS::svc_domain \%hash;
50   $record = new FS::svc_domain { 'column' => 'value' };
51
52   $error = $record->insert;
53
54   $error = $new_record->replace($old_record);
55
56   $error = $record->delete;
57
58   $error = $record->check;
59
60   $error = $record->suspend;
61
62   $error = $record->unsuspend;
63
64   $error = $record->cancel;
65
66 =head1 DESCRIPTION
67
68 An FS::svc_domain object represents a domain.  FS::svc_domain inherits from
69 FS::svc_Common.  The following fields are currently supported:
70
71 =over 4
72
73 =item svcnum - primary key (assigned automatically for new accounts)
74
75 =item domain
76
77 =item catchall - optional svcnum of an svc_acct record, designating an email catchall account.
78
79 =back
80
81 =head1 METHODS
82
83 =over 4
84
85 =item new HASHREF
86
87 Creates a new domain.  To add the domain to the database, see L<"insert">.
88
89 =cut
90
91 sub table { 'svc_domain'; }
92
93 =item insert
94
95 Adds this domain to the database.  If there is an error, returns the error,
96 otherwise returns false.
97
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.
100
101 The additional field I<action> should be set to I<N> for new domains or I<M>
102 for transfers.
103
104 A registration or transfer email will be submitted unless
105 $FS::svc_domain::whois_hack is true.
106
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.
111
112 If any I<soamachine> configuration file exists, an SOA record is added to
113 the domain_record table (see <FS::domain_record>).
114
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>).
118
119 =cut
120
121 sub insert {
122   my $self = shift;
123   my $error;
124
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';
131
132   my $oldAutoCommit = $FS::UID::AutoCommit;
133   local $FS::UID::AutoCommit = 0;
134   my $dbh = dbh;
135
136   $error = $self->check;
137   return $error if $error;
138
139   return "Domain in use (here)"
140     if qsearchs( 'svc_domain', { 'domain' => $self->domain } );
141
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)";
146   }
147   if ( $self->action eq "M" && ! $whois ) {
148     $dbh->rollback if $oldAutoCommit;
149     return "Domain not found (see whois)";
150   }
151
152   $error = $self->SUPER::insert;
153   if ( $error ) {
154     $dbh->rollback if $oldAutoCommit;
155     return $error;
156   }
157
158   $self->submit_internic unless $whois_hack;
159
160   if ( $soamachine ) {
161     my $soa = new FS::domain_record {
162       'svcnum'  => $self->svcnum,
163       'reczone' => '@',
164       'recaf'   => 'IN',
165       'rectype' => 'SOA',
166       'recdata' => "$soamachine $soaemail ( ". time2str("%Y%m%d", time). "00 ".
167                    "$soarefresh $soaretry $soaexpire $soadefaultttl )"
168     };
169     $error = $soa->insert;
170     if ( $error ) {
171       $dbh->rollback if $oldAutoCommit;
172       return "couldn't insert SOA record for new domain: $error";
173     }
174
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,
179         'reczone' => $zone,
180         'recaf'   => $af,
181         'rectype' => $type,
182         'recdata' => $data,
183       };
184       my $error = $domain_record->insert;
185       if ( $error ) {
186         $dbh->rollback if $oldAutoCommit;
187         return "couldn't insert record for new domain: $error";
188       }
189     }
190
191   }
192
193   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
194
195   ''; #no error
196 }
197
198 =item delete
199
200 Deletes this domain from the database.  If there is an error, returns the
201 error, otherwise returns false.
202
203 The corresponding FS::cust_svc record will be deleted as well.
204
205 =cut
206
207 sub delete {
208   my $self = shift;
209
210   return "Can't delete a domain which has accounts!"
211     if qsearch( 'svc_acct', { 'domsvc' => $self->svcnum } );
212
213   #return "Can't delete a domain with (domain_record) zone entries!"
214   #  if qsearch('domain_record', { 'svcnum' => $self->svcnum } );
215
216   local $SIG{HUP} = 'IGNORE';
217   local $SIG{INT} = 'IGNORE';
218   local $SIG{QUIT} = 'IGNORE';
219   local $SIG{TERM} = 'IGNORE';
220   local $SIG{TSTP} = 'IGNORE';
221   local $SIG{PIPE} = 'IGNORE';
222
223   my $oldAutoCommit = $FS::UID::AutoCommit;
224   local $FS::UID::AutoCommit = 0;
225   my $dbh = dbh;
226
227   foreach my $domain_record ( reverse $self->domain_record ) {
228     my $error = $domain_record->delete;
229     if ( $error ) {
230       $dbh->rollback if $oldAutoCommit;
231       return $error;
232     }
233   }
234
235   my $error = $self->SUPER::delete;
236   if ( $error ) {
237     $dbh->rollback if $oldAutoCommit;
238     return $error;
239   }
240
241   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
242 }
243
244 =item replace OLD_RECORD
245
246 Replaces OLD_RECORD with this one in the database.  If there is an error,
247 returns the error, otherwise returns false.
248
249 =cut
250
251 sub replace {
252   my ( $new, $old ) = ( shift, shift );
253
254   return "Can't change domain - reorder."
255     if $old->getfield('domain') ne $new->getfield('domain'); 
256
257   my $error = $new->SUPER::replace($old);
258   return $error if $error;
259 }
260
261 =item suspend
262
263 Just returns false (no error) for now.
264
265 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
266
267 =item unsuspend
268
269 Just returns false (no error) for now.
270
271 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
272
273 =item cancel
274
275 Just returns false (no error) for now.
276
277 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
278
279 =item check
280
281 Checks all fields to make sure this is a valid domain.  If there is an error,
282 returns the error, otherwise returns false.  Called by the insert and replace
283 methods.
284
285 Sets any fixed values; see L<FS::part_svc>.
286
287 =cut
288
289 sub check {
290   my $self = shift;
291
292   my $x = $self->setfixed;
293   return $x unless ref($x);
294   #my $part_svc = $x;
295
296   my $error = $self->ut_numbern('svcnum')
297               || $self->ut_numbern('catchall')
298   ;
299   return $error if $error;
300
301   #hmm
302   my $pkgnum;
303   if ( $self->svcnum ) {
304     my $cust_svc = qsearchs( 'cust_svc', { 'svcnum' => $self->svcnum } );
305     $pkgnum = $cust_svc->pkgnum;
306   } else {
307     $pkgnum = $self->pkgnum;
308   }
309
310   my($recref) = $self->hashref;
311
312   unless ( $whois_hack ) {
313     unless ( $self->email ) { #find out an email address
314       my @svc_acct;
315       foreach ( qsearch( 'cust_svc', { 'pkgnum' => $pkgnum } ) ) {
316         my $svc_acct = qsearchs( 'svc_acct', { 'svcnum' => $_->svcnum } );
317         push @svc_acct, $svc_acct if $svc_acct;
318       }
319
320       if ( scalar(@svc_acct) == 0 ) {
321         return "Must order an account in package ". $pkgnum. " first";
322       } elsif ( scalar(@svc_acct) > 1 ) {
323         return "More than one account in package ". $pkgnum. ": specify admin contact email";
324       } else {
325         $self->email($svc_acct[0]->email );
326       }
327     }
328   }
329
330   #if ( $recref->{domain} =~ /^([\w\-\.]{1,22})\.(com|net|org|edu)$/ ) {
331   if ( $recref->{domain} =~ /^([\w\-]{1,22})\.(com|net|org|edu)$/ ) {
332     $recref->{domain} = "$1.$2";
333   # hmmmmmmmm.
334   } elsif ( $whois_hack && $recref->{domain} =~ /^([\w\-\.]+)$/ ) {
335     $recref->{domain} = $1;
336   } else {
337     return "Illegal domain ". $recref->{domain}.
338            " (or unknown registry - try \$whois_hack)";
339   }
340
341   $recref->{action} =~ /^(M|N)$/ or return "Illegal action";
342   $recref->{action} = $1;
343
344   if ( $recref->{catchall} ne '' ) {
345     my $svc_acct = qsearchs( 'svc_acct', { 'svcnum' => $recref->{catchall} } );
346     return "Unknown catchall" unless $svc_acct;
347   }
348
349   $self->ut_textn('purpose');
350
351 }
352
353 =item domain_record
354
355 =cut
356
357 sub domain_record {
358   my $self = shift;
359
360   my %order = (
361     SOA => 1,
362     NS => 2,
363     MX => 3,
364     CNAME => 4,
365     A => 5,
366   );
367
368   sort { $order{$a->rectype} <=> $order{$b->rectype} }
369     qsearch('domain_record', { svcnum => $self->svcnum } );
370
371 }
372
373 sub catchall_svc_acct {
374   my $self = shift;
375   if ( $self->catchall ) {
376     qsearchs( 'svc_acct', { 'svcnum' => $self->catchall } );
377   } else {
378     '';
379   }
380 }
381
382 =item whois
383
384 Returns the Net::Whois::Domain object (see L<Net::Whois>) for this domain, or
385 undef if the domain is not found in whois.
386
387 (If $FS::svc_domain::whois_hack is true, returns that in all cases instead.)
388
389 =cut
390
391 sub whois {
392   $whois_hack or new Net::Whois::Domain $_[0]->domain;
393 }
394
395 =item _whois
396
397 Depriciated.
398
399 =cut
400
401 sub _whois {
402   die "_whois depriciated";
403 }
404
405 =item submit_internic
406
407 Submits a registration email for this domain.
408
409 =cut
410
411 sub submit_internic {
412   #my $self = shift;
413   carp "submit_internic depreciated";
414 }
415
416 =back
417
418 =head1 BUGS
419
420 Delete doesn't send a registration template.
421
422 All registries should be supported.
423
424 Should change action to a real field.
425
426 The $recref stuff in sub check should be cleaned up.
427
428 =head1 SEE ALSO
429
430 L<FS::svc_Common>, L<FS::Record>, L<FS::Conf>, L<FS::cust_svc>,
431 L<FS::part_svc>, L<FS::cust_pkg>, L<Net::Whois>, schema.html from the base
432 documentation, config.html from the base documentation.
433
434 =cut
435
436 1;
437
438