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