1 package FS::svc_domain;
4 use base qw( FS::svc_Parent_Mixin FS::svc_CGPRule_Mixin FS::svc_Common );
5 use vars qw( $whois_hack $conf
6 @defaultrecords $soadefaultttl $soaemail $soaexpire $soamachine
10 use Scalar::Util qw( blessed );
13 use Net::Domain::TLD qw(tld_exists);
14 use FS::Record qw(fields qsearch qsearchs dbh);
20 use FS::domain_record;
23 #ask FS::UID to run this stuff for us later
24 $FS::UID::callback{'FS::domain'} = sub {
27 @defaultrecords = $conf->config('defaultrecords');
28 $soadefaultttl = $conf->config('soadefaultttl');
29 $soaemail = $conf->config('soaemail');
30 $soaexpire = $conf->config('soaexpire');
31 $soamachine = $conf->config('soamachine');
32 $soarefresh = $conf->config('soarefresh');
33 $soaretry = $conf->config('soaretry');
39 FS::svc_domain - Object methods for svc_domain records
45 $record = new FS::svc_domain \%hash;
46 $record = new FS::svc_domain { 'column' => 'value' };
48 $error = $record->insert;
50 $error = $new_record->replace($old_record);
52 $error = $record->delete;
54 $error = $record->check;
56 $error = $record->suspend;
58 $error = $record->unsuspend;
60 $error = $record->cancel;
64 An FS::svc_domain object represents a domain. FS::svc_domain inherits from
65 FS::svc_Common. The following fields are currently supported:
69 =item svcnum - primary key (assigned automatically for new accounts)
73 =item catchall - optional svcnum of an svc_acct record, designating an email catchall account.
79 =item registrarnum - Registrar (see L<FS::registrar>)
81 =item registrarkey - Registrar key or password for this domain
83 =item setup_date - UNIX timestamp
85 =item renewal_interval - Number of days before expiration date to start renewal
87 =item expiration_date - UNIX timestamp
99 Creates a new domain. To add the domain to the database, see L<"insert">.
107 'display_weight' => 20,
108 'cancel_weight' => 60,
110 'domain' => 'Domain',
112 label => 'Parent domain / Communigate administrator domain',
114 select_table => 'svc_domain',
115 select_key => 'svcnum',
116 select_label => 'domain',
117 disable_inventory => 1,
120 'max_accounts' => { label => 'Maximum number of accounts',
121 'disable_inventory' => 1,
124 label => 'Communigate aliases',
126 disable_inventory => 1,
129 'cgp_accessmodes' => {
130 label => 'Communigate enabled services',
131 type => 'communigate_pro-accessmodes',
132 disable_inventory => 1,
136 'acct_def_cgp_accessmodes' => {
137 label => 'Acct. default Communigate enabled services',
138 type => 'communigate_pro-accessmodes',
139 disable_inventory => 1,
142 'acct_def_password_selfchange' => { label => 'Acct. default Password modification',
144 disable_inventory => 1,
147 'acct_def_password_recover' => { label => 'Acct. default Password recovery',
149 disable_inventory => 1,
152 'acct_def_cgp_deletemode' => {
153 label => 'Acct. default Communigate message delete method',
155 select_list => [ 'Move To Trash', 'Immediately', 'Mark' ],
156 disable_inventory => 1,
159 'acct_def_cgp_emptytrash' => {
160 label => 'Acct. default Communigate on logout remove trash',
162 disable_inventory => 1,
165 'acct_def_quota' => {
166 label => 'Acct. default Quota', #Mail storage limit
168 disable_inventory => 1,
171 'acct_def_file_quota'=> {
172 label => 'Acct. default File storage limit',
174 disable_inventory => 1,
177 'acct_def_file_maxnum'=> {
178 label => 'Acct. default Number of files limit',
180 disable_inventory => 1,
183 'acct_def_file_maxsize'=> {
184 label => 'Acct. default File size limit',
186 disable_inventory => 1,
189 'acct_def_cgp_rulesallowed' => {
190 label => 'Acct. default Allowed mail rules',
192 select_list => [ '', 'No', 'Filter Only', 'All But Exec', 'Any' ],
193 disable_inventory => 1,
196 'acct_def_cgp_rpopallowed' => {
197 label => 'Acct. default RPOP modifications',
200 'acct_def_cgp_mailtoall' => {
201 label => 'Acct. default Accepts mail to "all"',
204 'acct_def_cgp_addmailtrailer' => {
205 label => 'Acct. default Add trailer to sent mail',
209 label => 'Mail trailer',
211 disable_inventory => 1,
214 'acct_def_cgp_language' => {
215 label => 'Acct. default language',
217 select_list => [ '', qw( English Arabic Chinese Dutch French German Hebrew Italian Japanese Portuguese Russian Slovak Spanish Thai ) ],
218 disable_inventory => 1,
221 'acct_def_cgp_timezone' => {
222 label => 'Acct. default time zone',
226 '(+0100) Algeria/Congo',
227 '(+0200) Egypt/South Africa',
228 '(+0300) Saudi Arabia',
231 '(+0600) Bangladesh',
232 '(+0700) Thailand/Vietnam',
233 '(+0800) China/Malaysia',
234 '(+0900) Japan/Korea',
235 '(+1000) Queensland',
236 '(+1100) Micronesia',
238 '(+1300) Tonga/Kiribati',
239 '(+1400) Christmas Islands',
240 '(-0100) Azores/Cape Verde',
241 '(-0200) Fernando de Noronha',
242 '(-0300) Argentina/Uruguay',
243 '(-0400) Venezuela/Guyana',
244 '(-0500) Haiti/Peru',
245 '(-0600) Central America',
248 '(-0900) Marquesas Islands',
249 '(-1000) Hawaii/Tahiti',
259 'Australia/Adelaide',
261 'Australia/NorthernTerritory',
268 'NewZealand/Auckland',
269 'NorthAmerica/Alaska',
270 'NorthAmerica/Atlantic',
271 'NorthAmerica/Central',
272 'NorthAmerica/Eastern',
273 'NorthAmerica/Mountain',
274 'NorthAmerica/Pacific',
275 'Russia/Ekaterinburg',
278 'Russia/Krasnoyarsk',
280 'Russia/Novosibirsk',
281 'Russia/Vladivostok',
283 'SouthAmerica/Brasil',
284 'SouthAmerica/Chile',
285 'SouthAmerica/Paraguay',
287 disable_inventory => 1,
290 'acct_def_cgp_skinname' => {
291 label => 'Acct. default layout',
293 select_list => [ '', '***', 'GoldFleece', 'Skin2' ],
294 disable_inventory => 1,
297 'acct_def_cgp_prontoskinname' => {
298 label => 'Acct. default Pronto style',
300 select_list => [ '', 'Pronto', 'Pronto-darkflame', 'Pronto-steel', 'Pronto-twilight', ],
301 disable_inventory => 1,
304 'acct_def_cgp_sendmdnmode' => {
305 label => 'Acct. default send read receipts',
307 select_list => [ '', 'Never', 'Manually', 'Automatically' ],
308 disable_inventory => 1,
315 sub table { 'svc_domain'; }
318 my($class, $string) = @_;
319 $class->search_sql_field('domain', $string);
334 =item insert [ , OPTION => VALUE ... ]
336 Adds this domain to the database. If there is an error, returns the error,
337 otherwise returns false.
339 The additional fields I<pkgnum> and I<svcpart> (see L<FS::cust_svc>) should be
340 defined. An FS::cust_svc record will be created and inserted.
342 The additional field I<action> should be set to I<N> for new domains, I<M>
343 for transfers, or I<I> for no action (registered elsewhere).
345 A registration or transfer email will be submitted unless
346 $FS::svc_domain::whois_hack is true.
348 The additional field I<email> can be used to manually set the admin contact
349 email address on this email. Otherwise, the svc_acct records for this package
350 (see L<FS::cust_pkg>) are searched. If there is exactly one svc_acct record
351 in the same package, it is automatically used. Otherwise an error is returned.
353 If any I<soamachine> configuration file exists, an SOA record is added to
354 the domain_record table (see <FS::domain_record>).
356 If any records are defined in the I<defaultrecords> configuration file,
357 appropriate records are added to the domain_record table (see
358 L<FS::domain_record>).
360 Currently available options are: I<depend_jobnum>
362 If I<depend_jobnum> is set (to a scalar jobnum or an array reference of
363 jobnums), all provisioning jobs will have a dependancy on the supplied
364 jobnum(s) (they will not run until the specific job(s) complete(s)).
372 local $SIG{HUP} = 'IGNORE';
373 local $SIG{INT} = 'IGNORE';
374 local $SIG{QUIT} = 'IGNORE';
375 local $SIG{TERM} = 'IGNORE';
376 local $SIG{TSTP} = 'IGNORE';
377 local $SIG{PIPE} = 'IGNORE';
379 my $oldAutoCommit = $FS::UID::AutoCommit;
380 local $FS::UID::AutoCommit = 0;
383 $error = $self->SUPER::insert(@_);
385 $dbh->rollback if $oldAutoCommit;
390 my $soa = new FS::domain_record {
391 'svcnum' => $self->svcnum,
395 'recdata' => "$soamachine $soaemail ( ". time2str("%Y%m%d", time). "00 ".
396 "$soarefresh $soaretry $soaexpire $soadefaultttl )"
398 $error = $soa->insert;
400 $dbh->rollback if $oldAutoCommit;
401 return "couldn't insert SOA record for new domain: $error";
404 foreach my $record ( @defaultrecords ) {
405 my($zone,$af,$type,$data) = split(/\s+/,$record,4);
406 my $domain_record = new FS::domain_record {
407 'svcnum' => $self->svcnum,
413 my $error = $domain_record->insert;
415 $dbh->rollback if $oldAutoCommit;
416 return "couldn't insert record for new domain: $error";
422 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
429 Deletes this domain from the database. If there is an error, returns the
430 error, otherwise returns false.
432 The corresponding FS::cust_svc record will be deleted as well.
439 return "Can't delete a domain which has accounts!"
440 if qsearch( 'svc_acct', { 'domsvc' => $self->svcnum } );
442 #return "Can't delete a domain with (domain_record) zone entries!"
443 # if qsearch('domain_record', { 'svcnum' => $self->svcnum } );
445 local $SIG{HUP} = 'IGNORE';
446 local $SIG{INT} = 'IGNORE';
447 local $SIG{QUIT} = 'IGNORE';
448 local $SIG{TERM} = 'IGNORE';
449 local $SIG{TSTP} = 'IGNORE';
450 local $SIG{PIPE} = 'IGNORE';
452 my $oldAutoCommit = $FS::UID::AutoCommit;
453 local $FS::UID::AutoCommit = 0;
456 foreach my $domain_record ( reverse $self->domain_record ) {
457 my $error = $domain_record->delete;
459 $dbh->rollback if $oldAutoCommit;
460 return "can't delete DNS entry: ".
461 join(' ', map $domain_record->$_(),
462 qw( reczone recaf rectype recdata )
468 my $error = $self->SUPER::delete(@_);
470 $dbh->rollback if $oldAutoCommit;
474 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
477 =item replace OLD_RECORD
479 Replaces OLD_RECORD with this one in the database. If there is an error,
480 returns the error, otherwise returns false.
487 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
491 return "Can't change domain - reorder."
492 if $old->getfield('domain') ne $new->getfield('domain')
493 && ! $conf->exists('svc_domain-edit_domain');
495 # Better to do it here than to force the caller to remember that svc_domain is weird.
496 $new->setfield(action => 'I');
497 my $error = $new->SUPER::replace($old, @_);
498 return $error if $error;
503 Just returns false (no error) for now.
505 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
509 Just returns false (no error) for now.
511 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
515 Just returns false (no error) for now.
517 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
521 Checks all fields to make sure this is a valid domain. If there is an error,
522 returns the error, otherwise returns false. Called by the insert and replace
525 Sets any fixed values; see L<FS::part_svc>.
532 my $x = $self->setfixed;
533 return $x unless ref($x);
536 my $error = $self->ut_numbern('svcnum')
537 || $self->ut_numbern('catchall')
538 || $self->ut_numbern('max_accounts')
539 || $self->ut_anything('trailer') #well
540 || $self->ut_textn('cgp_aliases') #well
541 || $self->ut_enum('acct_def_password_selfchange', [ '', 'Y' ])
542 || $self->ut_enum('acct_def_password_recover', [ '', 'Y' ])
543 || $self->ut_textn('acct_def_cgp_accessmodes')
544 || $self->ut_alphan('acct_def_quota')
545 || $self->ut_alphan('acct_def_file_quota')
546 || $self->ut_alphan('acct_def_maxnum')
547 || $self->ut_alphan('acct_def_maxsize')
549 || $self->ut_alphasn('acct_def_cgp_rulesallowed')
550 || $self->ut_enum('acct_def_cgp_rpopallowed', [ '', 'Y' ])
551 || $self->ut_enum('acct_def_cgp_mailtoall', [ '', 'Y' ])
552 || $self->ut_enum('acct_def_cgp_addmailtrailer', [ '', 'Y' ])
553 #XXX archive messages
555 || $self->ut_alphasn('acct_def_cgp_deletemode')
556 || $self->ut_alphan('acct_def_cgp_emptytrash')
557 || $self->ut_alphan('acct_def_cgp_language')
558 || $self->ut_textn('acct_def_cgp_timezone')
559 || $self->ut_textn('acct_def_cgp_skinname')
560 || $self->ut_textn('acct_def_cgp_prontoskinname')
561 || $self->ut_alphan('acct_def_cgp_sendmdnmode')
563 #XXX rules, archive rule, spam foldering rule(s)
565 return $error if $error;
569 if ( $self->svcnum ) {
570 my $cust_svc = qsearchs( 'cust_svc', { 'svcnum' => $self->svcnum } );
571 $pkgnum = $cust_svc->pkgnum;
573 $pkgnum = $self->pkgnum;
576 my($recref) = $self->hashref;
578 #if ( $recref->{domain} =~ /^([\w\-\.]{1,22})\.(com|net|org|edu)$/ ) {
579 if ( $recref->{domain} =~ /^([\w\-]{1,63})\.(com|net|org|edu|tv|info|biz)$/ ) {
580 $recref->{domain} = "$1.$2";
581 $recref->{suffix} ||= $2;
583 } elsif ( $whois_hack && $recref->{domain} =~ /^([\w\-\.]+)\.(\w+)$/ ) {
584 $recref->{domain} = "$1.$2";
585 # need to match a list of suffixes - no guarantee they're top-level..
586 # http://wiki.mozilla.org/TLD_List
587 # but this will have to do for now...
588 $recref->{suffix} ||= $2;
590 return "Illegal domain ". $recref->{domain}.
591 " (or unknown registry - try \$whois_hack)";
594 $self->suffix =~ /(^|\.)(\w+)$/
595 or return "can't parse suffix for TLD: ". $self->suffix;
597 return "No such TLD: .$tld" unless tld_exists($tld);
599 if ( $recref->{catchall} ne '' ) {
600 my $svc_acct = qsearchs( 'svc_acct', { 'svcnum' => $recref->{catchall} } );
601 return "Unknown catchall" unless $svc_acct;
604 $self->ut_alphan('suffix')
605 or $self->ut_foreign_keyn('registrarnum', 'registrar', 'registrarnum')
606 or $self->ut_textn('registrarkey')
607 or $self->ut_numbern('setup_date')
608 or $self->ut_numbern('renewal_interval')
609 or $self->ut_numbern('expiration_date')
610 or $self->SUPER::check;
614 sub _check_duplicate {
619 if ( qsearchs( 'svc_domain', { 'domain' => $self->domain } ) ) {
620 return "Domain in use (here)";
644 #'SOA' => sub { $_[0]->recdata cmp $_[1]->recdata }, #sure hope not though
645 # 'SOA' => sub { 0; },
646 # 'NS' => sub { 0; },
647 'MX' => sub { my( $a_weight, $a_name ) = split(/\s+/, $_[0]->recdata);
648 my( $b_weight, $b_name ) = split(/\s+/, $_[1]->recdata);
649 $a_weight <=> $b_weight or $a_name cmp $b_name;
651 'CNAME' => sub { $_[0]->reczone cmp $_[1]->reczone },
652 'A' => sub { $_[0]->reczone cmp $_[1]->reczone },
654 # 'TXT' => sub { 0; },
655 'PTR' => sub { $_[0]->reczone <=> $_[1]->reczone },
658 map { $_ } #return $self->num_domain_record( PARAMS ) unless wantarray;
659 sort { $order{$a->rectype} <=> $order{$b->rectype}
660 or &{ $sort{$a->rectype} || sub { 0; } }($a, $b)
662 qsearch('domain_record', { svcnum => $self->svcnum } );
666 sub catchall_svc_acct {
668 if ( $self->catchall ) {
669 qsearchs( 'svc_acct', { 'svcnum' => $self->catchall } );
677 # Returns the Net::Whois::Domain object (see L<Net::Whois>) for this domain, or
678 # undef if the domain is not found in whois.
680 (If $FS::svc_domain::whois_hack is true, returns that in all cases instead.)
685 #$whois_hack or new Net::Whois::Domain $_[0]->domain;
686 #$whois_hack or die "whois_hack not set...\n";
693 Delete doesn't send a registration template.
695 All registries should be supported.
697 Should change action to a real field.
699 The $recref stuff in sub check should be cleaned up.
703 L<FS::svc_Common>, L<FS::Record>, L<FS::Conf>, L<FS::cust_svc>,
704 L<FS::part_svc>, L<FS::cust_pkg>, L<Net::Whois>, schema.html from the base
705 documentation, config.html from the base documentation.