1 package FS::svc_domain;
4 use vars qw( @ISA $whois_hack $conf
5 @defaultrecords $soadefaultttl $soaemail $soaexpire $soamachine
9 use Scalar::Util qw( blessed );
12 use Net::Domain::TLD qw(tld_exists);
13 use FS::Record qw(fields qsearch qsearchs dbh);
16 use FS::svc_Parent_Mixin;
21 use FS::domain_record;
24 @ISA = qw( FS::svc_Parent_Mixin FS::svc_Common );
26 #ask FS::UID to run this stuff for us later
27 $FS::UID::callback{'FS::domain'} = sub {
30 @defaultrecords = $conf->config('defaultrecords');
31 $soadefaultttl = $conf->config('soadefaultttl');
32 $soaemail = $conf->config('soaemail');
33 $soaexpire = $conf->config('soaexpire');
34 $soamachine = $conf->config('soamachine');
35 $soarefresh = $conf->config('soarefresh');
36 $soaretry = $conf->config('soaretry');
42 FS::svc_domain - Object methods for svc_domain records
48 $record = new FS::svc_domain \%hash;
49 $record = new FS::svc_domain { 'column' => 'value' };
51 $error = $record->insert;
53 $error = $new_record->replace($old_record);
55 $error = $record->delete;
57 $error = $record->check;
59 $error = $record->suspend;
61 $error = $record->unsuspend;
63 $error = $record->cancel;
67 An FS::svc_domain object represents a domain. FS::svc_domain inherits from
68 FS::svc_Common. The following fields are currently supported:
72 =item svcnum - primary key (assigned automatically for new accounts)
76 =item catchall - optional svcnum of an svc_acct record, designating an email catchall account.
82 =item registrarnum - Registrar (see L<FS::registrar>)
84 =item registrarkey - Registrar key or password for this domain
86 =item setup_date - UNIX timestamp
88 =item renewal_interval - Number of days before expiration date to start renewal
90 =item expiration_date - UNIX timestamp
102 Creates a new domain. To add the domain to the database, see L<"insert">.
110 'display_weight' => 20,
111 'cancel_weight' => 60,
113 'domain' => 'Domain',
115 label => 'Parent domain / Communigate administrator domain',
117 select_table => 'svc_domain',
118 select_key => 'svcnum',
119 select_label => 'domain',
120 disable_inventory => 1,
123 'max_accounts' => { label => 'Maximum number of accounts',
124 'disable_inventory' => 1,
127 label => 'Communigate aliases',
129 disable_inventory => 1,
132 'cgp_accessmodes' => {
133 label => 'Communigate enabled services',
134 type => 'communigate_pro-accessmodes',
135 disable_inventory => 1,
139 'acct_def_cgp_accessmodes' => {
140 label => 'Acct. default Communigate enabled services',
141 type => 'communigate_pro-accessmodes',
142 disable_inventory => 1,
145 'acct_def_password_selfchange' => { label => 'Acct. default Password modification',
147 disable_inventory => 1,
150 'acct_def_password_recover' => { label => 'Acct. default Password recovery',
152 disable_inventory => 1,
155 'acct_def_cgp_deletemode' => {
156 label => 'Acct. default Communigate message delete method',
158 select_list => [ 'Move To Trash', 'Immediately', 'Mark' ],
159 disable_inventory => 1,
162 'acct_def_cgp_emptytrash' => {
163 label => 'Acct. default Communigate on logout remove trash',
165 disable_inventory => 1,
168 'acct_def_quota' => {
169 label => 'Acct. default Quota', #Mail storage limit
171 disable_inventory => 1,
174 'acct_def_file_quota'=> {
175 label => 'Acct. default File storage limit',
177 disable_inventory => 1,
180 'acct_def_file_maxnum'=> {
181 label => 'Acct. default Number of files limit',
183 disable_inventory => 1,
186 'acct_def_file_maxsize'=> {
187 label => 'Acct. default File size limit',
189 disable_inventory => 1,
192 'acct_def_cgp_rulesallowed' => {
193 label => 'Acct. default Allowed mail rules',
195 select_list => [ '', 'No', 'Filter Only', 'All But Exec', 'Any' ],
196 disable_inventory => 1,
199 'acct_def_cgp_rpopallowed' => {
200 label => 'Acct. default RPOP modifications',
203 'acct_def_cgp_mailtoall' => {
204 label => 'Acct. default Accepts mail to "all"',
207 'acct_def_cgp_addmailtrailer' => {
208 label => 'Acct. default Add trailer to sent mail',
212 label => 'Mail trailer',
215 'acct_def_cgp_language' => {
216 label => 'Acct. default language',
218 select_list => [ '', qw( English Arabic Chinese Dutch French German Hebrew Italian Japanese Portuguese Russian Slovak Spanish Thai ) ],
219 disable_inventory => 1,
222 'acct_def_cgp_timezone' => {
223 label => 'Acct. default time zone',
227 '(+0100) Algeria/Congo',
228 '(+0200) Egypt/South Africa',
229 '(+0300) Saudi Arabia',
232 '(+0600) Bangladesh',
233 '(+0700) Thailand/Vietnam',
234 '(+0800) China/Malaysia',
235 '(+0900) Japan/Korea',
236 '(+1000) Queensland',
237 '(+1100) Micronesia',
239 '(+1300) Tonga/Kiribati',
240 '(+1400) Christmas Islands',
241 '(-0100) Azores/Cape Verde',
242 '(-0200) Fernando de Noronha',
243 '(-0300) Argentina/Uruguay',
244 '(-0400) Venezuela/Guyana',
245 '(-0500) Haiti/Peru',
246 '(-0600) Central America',
249 '(-0900) Marquesas Islands',
250 '(-1000) Hawaii/Tahiti',
260 'Australia/Adelaide',
262 'Australia/NorthernTerritory',
269 'NewZealand/Auckland',
270 'NorthAmerica/Alaska',
271 'NorthAmerica/Atlantic',
272 'NorthAmerica/Central',
273 'NorthAmerica/Eastern',
274 'NorthAmerica/Mountain',
275 'NorthAmerica/Pacific',
276 'Russia/Ekaterinburg',
279 'Russia/Krasnoyarsk',
281 'Russia/Novosibirsk',
282 'Russia/Vladivostok',
284 'SouthAmerica/Brasil',
285 'SouthAmerica/Chile',
286 'SouthAmerica/Paraguay',
288 disable_inventory => 1,
291 'acct_def_cgp_skinname' => {
292 label => 'Acct. default layout',
294 select_list => [ '', '***', 'GoldFleece', 'Skin2' ],
295 disable_inventory => 1,
299 'acct_def_cgp_sendmdnmode' => {
300 label => 'Acct. default send read receipts',
302 select_list => [ '', 'Never', 'Manually', 'Automatically' ],
303 disable_inventory => 1,
310 sub table { 'svc_domain'; }
313 my($class, $string) = @_;
314 $class->search_sql_field('domain', $string);
329 =item insert [ , OPTION => VALUE ... ]
331 Adds this domain to the database. If there is an error, returns the error,
332 otherwise returns false.
334 The additional fields I<pkgnum> and I<svcpart> (see L<FS::cust_svc>) should be
335 defined. An FS::cust_svc record will be created and inserted.
337 The additional field I<action> should be set to I<N> for new domains, I<M>
338 for transfers, or I<I> for no action (registered elsewhere).
340 A registration or transfer email will be submitted unless
341 $FS::svc_domain::whois_hack is true.
343 The additional field I<email> can be used to manually set the admin contact
344 email address on this email. Otherwise, the svc_acct records for this package
345 (see L<FS::cust_pkg>) are searched. If there is exactly one svc_acct record
346 in the same package, it is automatically used. Otherwise an error is returned.
348 If any I<soamachine> configuration file exists, an SOA record is added to
349 the domain_record table (see <FS::domain_record>).
351 If any records are defined in the I<defaultrecords> configuration file,
352 appropriate records are added to the domain_record table (see
353 L<FS::domain_record>).
355 Currently available options are: I<depend_jobnum>
357 If I<depend_jobnum> is set (to a scalar jobnum or an array reference of
358 jobnums), all provisioning jobs will have a dependancy on the supplied
359 jobnum(s) (they will not run until the specific job(s) complete(s)).
367 local $SIG{HUP} = 'IGNORE';
368 local $SIG{INT} = 'IGNORE';
369 local $SIG{QUIT} = 'IGNORE';
370 local $SIG{TERM} = 'IGNORE';
371 local $SIG{TSTP} = 'IGNORE';
372 local $SIG{PIPE} = 'IGNORE';
374 my $oldAutoCommit = $FS::UID::AutoCommit;
375 local $FS::UID::AutoCommit = 0;
378 $error = $self->SUPER::insert(@_);
380 $dbh->rollback if $oldAutoCommit;
385 my $soa = new FS::domain_record {
386 'svcnum' => $self->svcnum,
390 'recdata' => "$soamachine $soaemail ( ". time2str("%Y%m%d", time). "00 ".
391 "$soarefresh $soaretry $soaexpire $soadefaultttl )"
393 $error = $soa->insert;
395 $dbh->rollback if $oldAutoCommit;
396 return "couldn't insert SOA record for new domain: $error";
399 foreach my $record ( @defaultrecords ) {
400 my($zone,$af,$type,$data) = split(/\s+/,$record,4);
401 my $domain_record = new FS::domain_record {
402 'svcnum' => $self->svcnum,
408 my $error = $domain_record->insert;
410 $dbh->rollback if $oldAutoCommit;
411 return "couldn't insert record for new domain: $error";
417 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
424 Deletes this domain from the database. If there is an error, returns the
425 error, otherwise returns false.
427 The corresponding FS::cust_svc record will be deleted as well.
434 return "Can't delete a domain which has accounts!"
435 if qsearch( 'svc_acct', { 'domsvc' => $self->svcnum } );
437 #return "Can't delete a domain with (domain_record) zone entries!"
438 # if qsearch('domain_record', { 'svcnum' => $self->svcnum } );
440 local $SIG{HUP} = 'IGNORE';
441 local $SIG{INT} = 'IGNORE';
442 local $SIG{QUIT} = 'IGNORE';
443 local $SIG{TERM} = 'IGNORE';
444 local $SIG{TSTP} = 'IGNORE';
445 local $SIG{PIPE} = 'IGNORE';
447 my $oldAutoCommit = $FS::UID::AutoCommit;
448 local $FS::UID::AutoCommit = 0;
451 foreach my $domain_record ( reverse $self->domain_record ) {
452 my $error = $domain_record->delete;
454 $dbh->rollback if $oldAutoCommit;
455 return "can't delete DNS entry: ".
456 join(' ', map $domain_record->$_(),
457 qw( reczone recaf rectype recdata )
463 my $error = $self->SUPER::delete(@_);
465 $dbh->rollback if $oldAutoCommit;
469 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
472 =item replace OLD_RECORD
474 Replaces OLD_RECORD with this one in the database. If there is an error,
475 returns the error, otherwise returns false.
482 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
486 return "Can't change domain - reorder."
487 if $old->getfield('domain') ne $new->getfield('domain')
488 && ! $conf->exists('svc_domain-edit_domain');
490 # Better to do it here than to force the caller to remember that svc_domain is weird.
491 $new->setfield(action => 'I');
492 my $error = $new->SUPER::replace($old, @_);
493 return $error if $error;
498 Just returns false (no error) for now.
500 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
504 Just returns false (no error) for now.
506 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
510 Just returns false (no error) for now.
512 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
516 Checks all fields to make sure this is a valid domain. If there is an error,
517 returns the error, otherwise returns false. Called by the insert and replace
520 Sets any fixed values; see L<FS::part_svc>.
527 my $x = $self->setfixed;
528 return $x unless ref($x);
531 my $error = $self->ut_numbern('svcnum')
532 || $self->ut_numbern('catchall')
533 || $self->ut_numbern('max_accounts')
534 || $self->ut_anything('trailer') #well
535 || $self->ut_textn('cgp_aliases') #well
536 || $self->ut_enum('acct_def_password_selfchange', [ '', 'Y' ])
537 || $self->ut_enum('acct_def_password_recover', [ '', 'Y' ])
538 || $self->ut_textn('acct_def_cgp_accessmodes')
539 || $self->ut_alphan('acct_def_quota')
540 || $self->ut_alphan('acct_def_file_quota')
541 || $self->ut_alphan('acct_def_maxnum')
542 || $self->ut_alphan('acct_def_maxsize')
544 || $self->ut_alphasn('acct_def_cgp_rulesallowed')
545 || $self->ut_enum('acct_def_cgp_rpopallowed', [ '', 'Y' ])
546 || $self->ut_enum('acct_def_cgp_mailtoall', [ '', 'Y' ])
547 || $self->ut_enum('acct_def_cgp_addmailtrailer', [ '', 'Y' ])
548 #XXX archive messages
550 || $self->ut_alphasn('acct_def_cgp_deletemode')
551 || $self->ut_alphan('acct_def_cgp_emptytrash')
552 || $self->ut_alphan('acct_def_cgp_language')
553 || $self->ut_textn('acct_def_cgp_timezone')
554 || $self->ut_textn('acct_def_cgp_skinname')
556 || $self->ut_alphan('acct_def_cgp_sendmdnmode')
558 #XXX rules, archive rule, spam foldering rule(s)
560 return $error if $error;
564 if ( $self->svcnum ) {
565 my $cust_svc = qsearchs( 'cust_svc', { 'svcnum' => $self->svcnum } );
566 $pkgnum = $cust_svc->pkgnum;
568 $pkgnum = $self->pkgnum;
571 my($recref) = $self->hashref;
573 #if ( $recref->{domain} =~ /^([\w\-\.]{1,22})\.(com|net|org|edu)$/ ) {
574 if ( $recref->{domain} =~ /^([\w\-]{1,63})\.(com|net|org|edu|tv|info|biz)$/ ) {
575 $recref->{domain} = "$1.$2";
576 $recref->{suffix} ||= $2;
578 } elsif ( $whois_hack && $recref->{domain} =~ /^([\w\-\.]+)\.(\w+)$/ ) {
579 $recref->{domain} = "$1.$2";
580 # need to match a list of suffixes - no guarantee they're top-level..
581 # http://wiki.mozilla.org/TLD_List
582 # but this will have to do for now...
583 $recref->{suffix} ||= $2;
585 return "Illegal domain ". $recref->{domain}.
586 " (or unknown registry - try \$whois_hack)";
589 $self->suffix =~ /(^|\.)(\w+)$/
590 or return "can't parse suffix for TLD: ". $self->suffix;
592 return "No such TLD: .$tld" unless tld_exists($tld);
594 if ( $recref->{catchall} ne '' ) {
595 my $svc_acct = qsearchs( 'svc_acct', { 'svcnum' => $recref->{catchall} } );
596 return "Unknown catchall" unless $svc_acct;
599 $self->ut_alphan('suffix')
600 or $self->ut_foreign_keyn('registrarnum', 'registrar', 'registrarnum')
601 or $self->ut_textn('registrarkey')
602 or $self->ut_numbern('setup_date')
603 or $self->ut_numbern('renewal_interval')
604 or $self->ut_numbern('expiration_date')
605 or $self->SUPER::check;
609 sub _check_duplicate {
614 if ( qsearchs( 'svc_domain', { 'domain' => $self->domain } ) ) {
615 return "Domain in use (here)";
639 #'SOA' => sub { $_[0]->recdata cmp $_[1]->recdata }, #sure hope not though
640 # 'SOA' => sub { 0; },
641 # 'NS' => sub { 0; },
642 'MX' => sub { my( $a_weight, $a_name ) = split(/\s+/, $_[0]->recdata);
643 my( $b_weight, $b_name ) = split(/\s+/, $_[1]->recdata);
644 $a_weight <=> $b_weight or $a_name cmp $b_name;
646 'CNAME' => sub { $_[0]->reczone cmp $_[1]->reczone },
647 'A' => sub { $_[0]->reczone cmp $_[1]->reczone },
649 # 'TXT' => sub { 0; },
650 'PTR' => sub { $_[0]->reczone <=> $_[1]->reczone },
653 map { $_ } #return $self->num_domain_record( PARAMS ) unless wantarray;
654 sort { $order{$a->rectype} <=> $order{$b->rectype}
655 or &{ $sort{$a->rectype} || sub { 0; } }($a, $b)
657 qsearch('domain_record', { svcnum => $self->svcnum } );
661 sub catchall_svc_acct {
663 if ( $self->catchall ) {
664 qsearchs( 'svc_acct', { 'svcnum' => $self->catchall } );
672 # Returns the Net::Whois::Domain object (see L<Net::Whois>) for this domain, or
673 # undef if the domain is not found in whois.
675 (If $FS::svc_domain::whois_hack is true, returns that in all cases instead.)
680 #$whois_hack or new Net::Whois::Domain $_[0]->domain;
681 #$whois_hack or die "whois_hack not set...\n";
688 Delete doesn't send a registration template.
690 All registries should be supported.
692 Should change action to a real field.
694 The $recref stuff in sub check should be cleaned up.
698 L<FS::svc_Common>, L<FS::Record>, L<FS::Conf>, L<FS::cust_svc>,
699 L<FS::part_svc>, L<FS::cust_pkg>, L<Net::Whois>, schema.html from the base
700 documentation, config.html from the base documentation.