allow spaces in zip codes, for (at least) canada. pointed out by
[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   local $SIG{PIPE} = 'IGNORE';
125
126   $error = $self->check;
127   return $error if $error;
128
129   return "Domain in use (here)"
130     if qsearchs( 'svc_domain', { 'domain' => $self->domain } );
131
132   my $whois = ($self->_whois)[0];
133   return "Domain in use (see whois)"
134     if ( $self->action eq "N" && $whois !~ /^No match for/ );
135   return "Domain not found (see whois)"
136     if ( $self->action eq "M" && $whois =~ /^No match for/ );
137
138   $error = $self->SUPER::insert;
139   return $error if $error;
140
141   $self->submit_internic unless $whois_hack;
142
143   ''; #no error
144 }
145
146 =item delete
147
148 Deletes this domain from the database.  If there is an error, returns the
149 error, otherwise returns false.
150
151 The corresponding FS::cust_svc record will be deleted as well.
152
153 =item replace OLD_RECORD
154
155 Replaces OLD_RECORD with this one in the database.  If there is an error,
156 returns the error, otherwise returns false.
157
158 =cut
159
160 sub replace {
161   my ( $new, $old ) = ( shift, shift );
162   my $error;
163
164   return "Can't change domain - reorder."
165     if $old->getfield('domain') ne $new->getfield('domain'); 
166
167   $new->SUPER::replace($old);
168
169 }
170
171 =item suspend
172
173 Just returns false (no error) for now.
174
175 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
176
177 =item unsuspend
178
179 Just returns false (no error) for now.
180
181 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
182
183 =item cancel
184
185 Just returns false (no error) for now.
186
187 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
188
189 =item check
190
191 Checks all fields to make sure this is a valid domain.  If there is an error,
192 returns the error, otherwise returns false.  Called by the insert and replace
193 methods.
194
195 Sets any fixed values; see L<FS::part_svc>.
196
197 =cut
198
199 sub check {
200   my $self = shift;
201   my $error;
202
203   my $x = $self->setfixed;
204   return $x unless ref($x);
205   my $part_svc = $x;
206
207   #hmm
208   my $pkgnum;
209   if ( $self->svcnum ) {
210     my $cust_svc = qsearchs( 'cust_svc', { 'svcnum' => $self->svcnum } );
211     $pkgnum = $cust_svc->pkgnum;
212   } else {
213     $pkgnum = $self->pkgnum;
214   }
215
216   my($recref) = $self->hashref;
217
218   unless ( $whois_hack ) {
219     unless ( $self->email ) { #find out an email address
220       my @svc_acct;
221       foreach ( qsearch( 'cust_svc', { 'pkgnum' => $pkgnum } ) ) {
222         my $svc_acct = qsearchs( 'svc_acct', { 'svcnum' => $_->svcnum } );
223         push @svc_acct, $svc_acct if $svc_acct;
224       }
225
226       if ( scalar(@svc_acct) == 0 ) {
227         return "Must order an account in package ". $pkgnum. " first";
228       } elsif ( scalar(@svc_acct) > 1 ) {
229         return "More than one account in package ". $pkgnum. ": specify admin contact email";
230       } else {
231         $self->email($svc_acct[0]->username. '@'. $mydomain);
232       }
233     }
234   }
235
236   #if ( $recref->{domain} =~ /^([\w\-\.]{1,22})\.(com|net|org|edu)$/ ) {
237   if ( $recref->{domain} =~ /^([\w\-]{1,22})\.(com|net|org|edu)$/ ) {
238     $recref->{domain} = "$1.$2";
239   # hmmmmmmmm.
240   } elsif ( $whois_hack && $recref->{domain} =~ /^([\w\-\.]+)$/ ) {
241     $recref->{domain} = $1;
242   } else {
243     return "Illegal domain ". $recref->{domain}.
244            " (or unknown registry - try \$whois_hack)";
245   }
246
247   $recref->{action} =~ /^(M|N)$/ or return "Illegal action";
248   $recref->{action} = $1;
249
250   $self->ut_textn('purpose');
251
252 }
253
254 =item _whois
255
256 Executes the command:
257
258   whois do $domain
259
260 and returns the output.
261
262 (Always returns I<No match for domian "$domain".> if
263 $FS::svc_domain::whois_hack is set true.)
264
265 =cut
266
267 sub _whois {
268   my $self = shift;
269   my $domain = $self->domain;
270   return ( "No match for domain \"$domain\"." ) if $whois_hack;
271   open(WHOIS, "whois do $domain |");
272   return <WHOIS>;
273 }
274
275 =item submit_internic
276
277 Submits a registration email for this domain.
278
279 =cut
280
281 sub submit_internic {
282   my $self = shift;
283
284   my $cust_pkg = qsearchs( 'cust_pkg', { 'pkgnum' => $self->pkgnum } );
285   return unless $cust_pkg;
286   my $cust_main = qsearchs( 'cust_main', { 'custnum' => $cust_pkg->custnum } );
287   return unless $cust_main;
288
289   my %subs = (
290     'action'       => $self->action,
291     'purpose'      => $self->purpose,
292     'domain'       => $self->domain,
293     'company'      => $cust_main->company 
294                         || $cust_main->getfield('first'). ' '.
295                            $cust_main->getfield('last')
296                       ,
297     'city'         => $cust_main->city,
298     'state'        => $cust_main->state,
299     'zip'          => $cust_main->zip,
300     'country'      => $cust_main->country,
301     'last'         => $cust_main->getfield('last'),
302     'first'        => $cust_main->getfield('first'),
303     'daytime'      => $cust_main->daytime,
304     'fax'          => $cust_main->fax,
305     'email'        => $self->email,
306     'tech_contact' => $tech_contact,
307     'primary'      => shift @nameservers,
308     'primary_ip'   => shift @nameserver_ips,
309   );
310
311   #yuck
312   my @xtemplate = @template;
313   my @body;
314   my $line;
315   OLOOP: while ( defined( $line = shift @xtemplate ) ) {
316
317     if ( $line =~ /^###LOOP###$/ ) {
318       my(@buffer);
319       LOADBUF: while ( defined( $line = shift @xtemplate ) ) {
320         last LOADBUF if ( $line =~ /^###ENDLOOP###$/ );
321         push @buffer, $line;
322       }
323       my %lubs = (
324         'address'      => $cust_main->address2 
325                             ? [ $cust_main->address1, $cust_main->address2 ]
326                             : [ $cust_main->address1 ]
327                           ,
328         'secondary'    => [ @nameservers ],
329         'secondary_ip' => [ @nameserver_ips ],
330       );
331       LOOP: while (1) {
332         my @xbuffer = @buffer;
333         SUBLOOP: while ( defined( $line = shift @xbuffer ) ) {
334           if ( $line =~ /###(\w+)###/ ) {
335             #last LOOP unless my($lub)=shift@{$lubs{$1}};
336             next OLOOP unless my $lub = shift @{$lubs{$1}};
337             $line =~ s/###(\w+)###/$lub/e;
338             redo SUBLOOP;
339           } else {
340             push @body, $line;
341           }
342         } #SUBLOOP
343       } #LOOP
344
345     }
346
347     if ( $line =~ /###(\w+)###/ ) {
348       #$line =~ s/###(\w+)###/$subs{$1}/eg;
349       $line =~ s/###(\w+)###/$subs{$1}/e;
350       redo OLOOP;
351     } else {
352       push @body, $line;
353     }
354
355   } #OLOOP
356
357   my $subject;
358   if ( $self->action eq "M" ) {
359     $subject = "MODIFY DOMAIN ". $self->domain;
360   } elsif ( $self->action eq "N" ) { 
361     $subject = "NEW DOMAIN ". $self->domain;
362   } else {
363     croak "submit_internic called with action ". $self->action;
364   }
365
366   $ENV{SMTPHOSTS} = $smtpmachine;
367   $ENV{MAILADDRESS} = $from;
368   my $header = Mail::Header->new( [
369     "From: $from",
370     "To: $to",
371     "Sender: $from",
372     "Reply-To: $from",
373     "Date: ". time2str("%a, %d %b %Y %X %z", time),
374     "Subject: $subject",
375   ] );
376
377   my($msg)=Mail::Internet->new(
378     'Header' => $header,
379     'Body' => \@body,
380   );
381
382   $msg->smtpsend or die "Can't send registration email"; #die? warn?
383
384 }
385
386 =back
387
388 =head1 VERSION
389
390 $Id: svc_domain.pm,v 1.6 1999-01-25 12:26:17 ivan Exp $
391
392 =head1 BUGS
393
394 All BIND/DNS fields should be included (and exported).
395
396 Delete doesn't send a registration template.
397
398 All registries should be supported.
399
400 Should change action to a real field.
401
402 The $recref stuff in sub check should be cleaned up.
403
404 =head1 SEE ALSO
405
406 L<FS::svc_Common>, L<FS::Record>, L<FS::Conf>, L<FS::cust_svc>,
407 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.
408
409 =head1 HISTORY
410
411 ivan@voicenet.com 97-jul-21
412
413 rewrite ivan@sisd.com 98-mar-10
414
415 add internic bits ivan@sisd.com 98-mar-14
416
417 Changed 'day' to 'daytime' because Pg6.3 reserves the day word
418         bmccane@maxbaud.net     98-apr-3
419
420 /var/spool/freeside/conf/registries/internic/, Mail::Internet, etc.
421 ivan@sisd.com 98-jul-17-19
422
423 pod, some FS::Conf (not complete) ivan@sisd.com 98-sep-23
424
425 $Log: svc_domain.pm,v $
426 Revision 1.6  1999-01-25 12:26:17  ivan
427 yet more mod_perl stuff
428
429 Revision 1.5  1998/12/30 00:30:47  ivan
430 svc_ stuff is more properly OO - has a common superclass FS::svc_Common
431
432 Revision 1.3  1998/11/13 09:56:57  ivan
433 change configuration file layout to support multiple distinct databases (with
434 own set of config files, export, etc.)
435
436 Revision 1.2  1998/10/14 08:18:21  ivan
437 More informative error messages and better doc for admin contact email stuff
438
439
440 =cut
441
442 1;
443
444