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