session monitor updates
[freeside.git] / FS / FS / svc_domain.pm
1 package FS::svc_domain;
2
3 use strict;
4 use vars qw( @ISA $whois_hack $conf $mydomain $smtpmachine
5   $tech_contact $from $to @nameservers @nameserver_ips @template
6   @mxmachines @nsmachines $soadefaultttl $soaemail $soaexpire $soamachine
7   $soarefresh $soaretry
8 );
9 use Carp;
10 use Mail::Internet;
11 use Mail::Header;
12 use Date::Format;
13 use Net::Whois 1.0;
14 use FS::Record qw(fields qsearch qsearchs);
15 use FS::Conf;
16 use FS::svc_Common;
17 use FS::cust_svc;
18 use FS::svc_acct;
19 use FS::cust_pkg;
20 use FS::cust_main;
21 use FS::domain_record;
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   $mydomain = $conf->config('domain');
30   $smtpmachine = $conf->config('smtpmachine');
31
32   my($internic)="/registries/internic";
33   $tech_contact = $conf->config("$internic/tech_contact");
34   $from = $conf->config("$internic/from");
35   $to = $conf->config("$internic/to");
36   my(@ns) = $conf->config("$internic/nameservers");
37   @nameservers=map {
38     /^\s*\d+\.\d+\.\d+\.\d+\s+([^\s]+)\s*$/
39       or die "Illegal line in $internic/nameservers";
40     $1;
41   } @ns;
42   @nameserver_ips=map {
43     /^\s*(\d+\.\d+\.\d+\.\d+)\s+([^\s]+)\s*$/
44       or die "Illegal line in $internic/nameservers!";
45     $1;
46   } @ns;
47   @template = map { $_. "\n" } $conf->config("$internic/template");
48
49   @mxmachines    = $conf->config('mxmachines');
50   @nsmachines    = $conf->config('nsmachines');
51   $soadefaultttl = $conf->config('soadefaultttl');
52   $soaemail      = $conf->config('soaemail');
53   $soaexpire     = $conf->config('soaexpire');
54   $soamachine    = $conf->config('soamachine');
55   $soarefresh    = $conf->config('soarefresh');
56   $soaretry      = $conf->config('soaretry');
57
58 };
59
60 =head1 NAME
61
62 FS::svc_domain - Object methods for svc_domain records
63
64 =head1 SYNOPSIS
65
66   use FS::svc_domain;
67
68   $record = new FS::svc_domain \%hash;
69   $record = new FS::svc_domain { 'column' => 'value' };
70
71   $error = $record->insert;
72
73   $error = $new_record->replace($old_record);
74
75   $error = $record->delete;
76
77   $error = $record->check;
78
79   $error = $record->suspend;
80
81   $error = $record->unsuspend;
82
83   $error = $record->cancel;
84
85 =head1 DESCRIPTION
86
87 An FS::svc_domain object represents a domain.  FS::svc_domain inherits from
88 FS::svc_Common.  The following fields are currently supported:
89
90 =over 4
91
92 =item svcnum - primary key (assigned automatically for new accounts)
93
94 =item domain
95
96 =back
97
98 =head1 METHODS
99
100 =over 4
101
102 =item new HASHREF
103
104 Creates a new domain.  To add the domain to the database, see L<"insert">.
105
106 =cut
107
108 sub table { 'svc_domain'; }
109
110 =item insert
111
112 Adds this domain to the database.  If there is an error, returns the error,
113 otherwise returns false.
114
115 The additional fields I<pkgnum> and I<svcpart> (see L<FS::cust_svc>) should be 
116 defined.  An FS::cust_svc record will be created and inserted.
117
118 The additional field I<action> should be set to I<N> for new domains or I<M>
119 for transfers.
120
121 A registration or transfer email will be submitted unless
122 $FS::svc_domain::whois_hack is true.
123
124 The additional field I<email> can be used to manually set the admin contact
125 email address on this email.  Otherwise, the svc_acct records for this package 
126 (see L<FS::cust_pkg>) are searched.  If there is exactly one svc_acct record
127 in the same package, it is automatically used.  Otherwise an error is returned.
128
129 If any I<soamachine> configuration file exists, an SOA record is added to
130 the domain_record table (see <FS::domain_record>).
131
132 If any machines are defined in the I<nsmachines> configuration file, NS
133 records are added to the domain_record table (see L<FS::domain_record>).
134
135 If any machines are defined in the I<mxmachines> configuration file, MX
136 records are added to the domain_record table (see L<FS::domain_record>).
137
138 Any problems adding FS::domain_record records will emit warnings, but will
139 not return errors from this method.  If your configuration files are correct
140 you shouln't have any problems.
141
142 =cut
143
144 sub insert {
145   my $self = shift;
146   my $error;
147
148   local $SIG{HUP} = 'IGNORE';
149   local $SIG{INT} = 'IGNORE';
150   local $SIG{QUIT} = 'IGNORE';
151   local $SIG{TERM} = 'IGNORE';
152   local $SIG{TSTP} = 'IGNORE';
153   local $SIG{PIPE} = 'IGNORE';
154
155   $error = $self->check;
156   return $error if $error;
157
158   return "Domain in use (here)"
159     if qsearchs( 'svc_domain', { 'domain' => $self->domain } );
160
161   my $whois = $self->whois;
162   return "Domain in use (see whois)"
163     if ( $self->action eq "N" && ! $whois_hack && $whois );
164   return "Domain not found (see whois)"
165     if ( $self->action eq "M" && ! $whois );
166
167   $error = $self->SUPER::insert;
168   return $error if $error;
169
170   $self->submit_internic unless $whois_hack;
171
172   if ( $soamachine ) {
173     my $soa = new FS::domain_record {
174       'svcnum'  => $self->svcnum,
175       'reczone' => '@',
176       'recaf'   => 'IN',
177       'rectype' => 'SOA',
178       'recdata' => "$soamachine $soaemail ( ". time2str("%Y%m%e", time). "00 ".
179                    "$soarefresh $soaretry $soaexpire $soadefaultttl )"
180     };
181     $error = $soa->insert;
182     warn "WARNING: couldn't insert SOA record for new domain: $error" if $error;
183
184     foreach my $nsmachine ( @nsmachines ) {
185       my $ns = new FS::domain_record {
186         'svcnum'  => $self->svcnum,
187         'reczone' => '@',
188         'recaf'   => 'IN',
189         'rectype' => 'NS',
190         'recdata' => $nsmachine,
191       };
192       my $error = $ns->insert;
193       warn "WARNING: couldn't insert NS record for new domain: $error"
194         if $error;
195     }
196
197     foreach my $mxmachine ( @mxmachines ) {
198       my $mx = new FS::domain_record {
199         'svcnum'  => $self->svcnum,
200         'reczone' => '@',
201         'recaf'   => 'IN',
202         'rectype' => 'mx',
203         'recdata' => $mxmachine,
204       };
205       my $error = $mx->insert;
206       warn "WARNING: couldn't insert MX record for new domain: $error"
207         if $error;
208     }
209
210   }
211
212
213   ''; #no error
214 }
215
216 =item delete
217
218 Deletes this domain from the database.  If there is an error, returns the
219 error, otherwise returns false.
220
221 The corresponding FS::cust_svc record will be deleted as well.
222
223 =item replace OLD_RECORD
224
225 Replaces OLD_RECORD with this one in the database.  If there is an error,
226 returns the error, otherwise returns false.
227
228 =cut
229
230 sub replace {
231   my ( $new, $old ) = ( shift, shift );
232   my $error;
233
234   return "Can't change domain - reorder."
235     if $old->getfield('domain') ne $new->getfield('domain'); 
236
237   $new->SUPER::replace($old);
238
239 }
240
241 =item suspend
242
243 Just returns false (no error) for now.
244
245 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
246
247 =item unsuspend
248
249 Just returns false (no error) for now.
250
251 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
252
253 =item cancel
254
255 Just returns false (no error) for now.
256
257 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
258
259 =item check
260
261 Checks all fields to make sure this is a valid domain.  If there is an error,
262 returns the error, otherwise returns false.  Called by the insert and replace
263 methods.
264
265 Sets any fixed values; see L<FS::part_svc>.
266
267 =cut
268
269 sub check {
270   my $self = shift;
271   my $error;
272
273   my $x = $self->setfixed;
274   return $x unless ref($x);
275   my $part_svc = $x;
276
277   #hmm
278   my $pkgnum;
279   if ( $self->svcnum ) {
280     my $cust_svc = qsearchs( 'cust_svc', { 'svcnum' => $self->svcnum } );
281     $pkgnum = $cust_svc->pkgnum;
282   } else {
283     $pkgnum = $self->pkgnum;
284   }
285
286   my($recref) = $self->hashref;
287
288   unless ( $whois_hack ) {
289     unless ( $self->email ) { #find out an email address
290       my @svc_acct;
291       foreach ( qsearch( 'cust_svc', { 'pkgnum' => $pkgnum } ) ) {
292         my $svc_acct = qsearchs( 'svc_acct', { 'svcnum' => $_->svcnum } );
293         push @svc_acct, $svc_acct if $svc_acct;
294       }
295
296       if ( scalar(@svc_acct) == 0 ) {
297         return "Must order an account in package ". $pkgnum. " first";
298       } elsif ( scalar(@svc_acct) > 1 ) {
299         return "More than one account in package ". $pkgnum. ": specify admin contact email";
300       } else {
301         $self->email($svc_acct[0]->username. '@'. $mydomain);
302       }
303     }
304   }
305
306   #if ( $recref->{domain} =~ /^([\w\-\.]{1,22})\.(com|net|org|edu)$/ ) {
307   if ( $recref->{domain} =~ /^([\w\-]{1,22})\.(com|net|org|edu)$/ ) {
308     $recref->{domain} = "$1.$2";
309   # hmmmmmmmm.
310   } elsif ( $whois_hack && $recref->{domain} =~ /^([\w\-\.]+)$/ ) {
311     $recref->{domain} = $1;
312   } else {
313     return "Illegal domain ". $recref->{domain}.
314            " (or unknown registry - try \$whois_hack)";
315   }
316
317   $recref->{action} =~ /^(M|N)$/ or return "Illegal action";
318   $recref->{action} = $1;
319
320   $self->ut_textn('purpose');
321
322 }
323
324 =item whois
325
326 Returns the Net::Whois::Domain object (see L<Net::Whois>) for this domain, or
327 undef if the domain is not found in whois.
328
329 (If $FS::svc_domain::whois_hack is true, returns that in all cases instead.)
330
331 =cut
332
333 sub whois {
334   $whois_hack or new Net::Whois::Domain $_[0]->domain;
335 }
336
337 =item _whois
338
339 Depriciated.
340
341 =cut
342
343 sub _whois {
344   die "_whois depriciated";
345 }
346
347 =item submit_internic
348
349 Submits a registration email for this domain.
350
351 =cut
352
353 sub submit_internic {
354   my $self = shift;
355
356   my $cust_pkg = qsearchs( 'cust_pkg', { 'pkgnum' => $self->pkgnum } );
357   return unless $cust_pkg;
358   my $cust_main = qsearchs( 'cust_main', { 'custnum' => $cust_pkg->custnum } );
359   return unless $cust_main;
360
361   my %subs = (
362     'action'       => $self->action,
363     'purpose'      => $self->purpose,
364     'domain'       => $self->domain,
365     'company'      => $cust_main->company 
366                         || $cust_main->getfield('first'). ' '.
367                            $cust_main->getfield('last')
368                       ,
369     'city'         => $cust_main->city,
370     'state'        => $cust_main->state,
371     'zip'          => $cust_main->zip,
372     'country'      => $cust_main->country,
373     'last'         => $cust_main->getfield('last'),
374     'first'        => $cust_main->getfield('first'),
375     'daytime'      => $cust_main->daytime,
376     'fax'          => $cust_main->fax,
377     'email'        => $self->email,
378     'tech_contact' => $tech_contact,
379     'primary'      => shift @nameservers,
380     'primary_ip'   => shift @nameserver_ips,
381   );
382
383   #yuck
384   my @xtemplate = @template;
385   my @body;
386   my $line;
387   OLOOP: while ( defined( $line = shift @xtemplate ) ) {
388
389     if ( $line =~ /^###LOOP###$/ ) {
390       my(@buffer);
391       LOADBUF: while ( defined( $line = shift @xtemplate ) ) {
392         last LOADBUF if ( $line =~ /^###ENDLOOP###$/ );
393         push @buffer, $line;
394       }
395       my %lubs = (
396         'address'      => $cust_main->address2 
397                             ? [ $cust_main->address1, $cust_main->address2 ]
398                             : [ $cust_main->address1 ]
399                           ,
400         'secondary'    => [ @nameservers ],
401         'secondary_ip' => [ @nameserver_ips ],
402       );
403       LOOP: while (1) {
404         my @xbuffer = @buffer;
405         SUBLOOP: while ( defined( $line = shift @xbuffer ) ) {
406           if ( $line =~ /###(\w+)###/ ) {
407             #last LOOP unless my($lub)=shift@{$lubs{$1}};
408             next OLOOP unless my $lub = shift @{$lubs{$1}};
409             $line =~ s/###(\w+)###/$lub/e;
410             redo SUBLOOP;
411           } else {
412             push @body, $line;
413           }
414         } #SUBLOOP
415       } #LOOP
416
417     }
418
419     if ( $line =~ /###(\w+)###/ ) {
420       #$line =~ s/###(\w+)###/$subs{$1}/eg;
421       $line =~ s/###(\w+)###/$subs{$1}/e;
422       redo OLOOP;
423     } else {
424       push @body, $line;
425     }
426
427   } #OLOOP
428
429   my $subject;
430   if ( $self->action eq "M" ) {
431     $subject = "MODIFY DOMAIN ". $self->domain;
432   } elsif ( $self->action eq "N" ) { 
433     $subject = "NEW DOMAIN ". $self->domain;
434   } else {
435     croak "submit_internic called with action ". $self->action;
436   }
437
438   $ENV{SMTPHOSTS} = $smtpmachine;
439   $ENV{MAILADDRESS} = $from;
440   my $header = Mail::Header->new( [
441     "From: $from",
442     "To: $to",
443     "Sender: $from",
444     "Reply-To: $from",
445     "Date: ". time2str("%a, %d %b %Y %X %z", time),
446     "Subject: $subject",
447   ] );
448
449   my($msg)=Mail::Internet->new(
450     'Header' => $header,
451     'Body' => \@body,
452   );
453
454   $msg->smtpsend or die "Can't send registration email"; #die? warn?
455
456 }
457
458 =back
459
460 =head1 VERSION
461
462 $Id: svc_domain.pm,v 1.7 2000-06-29 11:12:20 ivan Exp $
463
464 =head1 BUGS
465
466 All BIND/DNS fields should be included (and exported).
467
468 Delete doesn't send a registration template.
469
470 All registries should be supported.
471
472 Should change action to a real field.
473
474 The $recref stuff in sub check should be cleaned up.
475
476 =head1 SEE ALSO
477
478 L<FS::svc_Common>, L<FS::Record>, L<FS::Conf>, L<FS::cust_svc>,
479 L<FS::part_svc>, L<FS::cust_pkg>, L<FS::SSH>, L<Net::Whois>, L<ssh>,
480 L<dot-qmail>, schema.html from the base documentation, config.html from the
481 base documentation.
482
483 =cut
484
485 1;
486
487