1 package FS::svc_domain;
4 use base qw( FS::svc_Parent_Mixin FS::svc_CGP_Mixin FS::svc_CGPRule_Mixin
6 use vars qw( $whois_hack $conf
7 @defaultrecords $soadefaultttl $soaemail $soaexpire $soamachine
11 use Scalar::Util qw( blessed );
14 use Net::Domain::TLD qw(tld_exists);
15 use FS::Record qw(fields qsearch qsearchs dbh);
21 use FS::domain_record;
24 #ask FS::UID to run this stuff for us later
25 $FS::UID::callback{'FS::domain'} = sub {
28 @defaultrecords = $conf->config('defaultrecords');
29 $soadefaultttl = $conf->config('soadefaultttl');
30 $soaemail = $conf->config('soaemail');
31 $soaexpire = $conf->config('soaexpire');
32 $soamachine = $conf->config('soamachine');
33 $soarefresh = $conf->config('soarefresh');
34 $soaretry = $conf->config('soaretry');
40 FS::svc_domain - Object methods for svc_domain records
46 $record = new FS::svc_domain \%hash;
47 $record = new FS::svc_domain { 'column' => 'value' };
49 $error = $record->insert;
51 $error = $new_record->replace($old_record);
53 $error = $record->delete;
55 $error = $record->check;
57 $error = $record->suspend;
59 $error = $record->unsuspend;
61 $error = $record->cancel;
65 An FS::svc_domain object represents a domain. FS::svc_domain inherits from
66 FS::svc_Common. The following fields are currently supported:
70 =item svcnum - primary key (assigned automatically for new accounts)
74 =item catchall - optional svcnum of an svc_acct record, designating an email catchall account.
80 =item registrarnum - Registrar (see L<FS::registrar>)
82 =item registrarkey - Registrar key or password for this domain
84 =item setup_date - UNIX timestamp
86 =item renewal_interval - Number of days before expiration date to start renewal
88 =item expiration_date - UNIX timestamp
100 Creates a new domain. To add the domain to the database, see L<"insert">.
108 'display_weight' => 20,
109 'cancel_weight' => 60,
111 'domain' => 'Domain',
113 label => 'Parent domain / Communigate administrator domain',
115 select_table => 'svc_domain',
116 select_key => 'svcnum',
117 select_label => 'domain',
118 disable_inventory => 1,
121 'max_accounts' => { label => 'Maximum number of accounts',
122 'disable_inventory' => 1,
125 label => 'Communigate aliases',
127 disable_inventory => 1,
130 'cgp_accessmodes' => {
131 label => 'Communigate enabled services',
132 type => 'communigate_pro-accessmodes',
133 disable_inventory => 1,
137 'acct_def_cgp_accessmodes' => {
138 label => 'Acct. default Communigate enabled services',
139 type => 'communigate_pro-accessmodes',
140 disable_inventory => 1,
143 'acct_def_password_selfchange' => { label => 'Acct. default Password modification',
145 disable_inventory => 1,
148 'acct_def_password_recover' => { label => 'Acct. default Password recovery',
150 disable_inventory => 1,
153 'acct_def_cgp_deletemode' => {
154 label => 'Acct. default Communigate message delete method',
156 select_list => [ 'Move To Trash', 'Immediately', 'Mark' ],
157 disable_inventory => 1,
160 'acct_def_cgp_emptytrash' => {
161 label => 'Acct. default Communigate on logout remove trash',
163 select_list => __PACKAGE__->cgp_emptytrash_values,
164 disable_inventory => 1,
167 'acct_def_quota' => {
168 label => 'Acct. default Quota', #Mail storage limit
170 disable_inventory => 1,
173 'acct_def_file_quota'=> {
174 label => 'Acct. default File storage limit',
176 disable_inventory => 1,
179 'acct_def_file_maxnum'=> {
180 label => 'Acct. default Number of files limit',
182 disable_inventory => 1,
185 'acct_def_file_maxsize'=> {
186 label => 'Acct. default File size limit',
188 disable_inventory => 1,
191 'acct_def_cgp_rulesallowed' => {
192 label => 'Acct. default Allowed mail rules',
194 select_list => [ '', 'No', 'Filter Only', 'All But Exec', 'Any' ],
195 disable_inventory => 1,
198 'acct_def_cgp_rpopallowed' => {
199 label => 'Acct. default RPOP modifications',
202 'acct_def_cgp_mailtoall' => {
203 label => 'Acct. default Accepts mail to "all"',
206 'acct_def_cgp_addmailtrailer' => {
207 label => 'Acct. default Add trailer to sent mail',
210 'acct_def_cgp_archiveafter' => {
211 label => 'Archive messages after',
214 -2 => 'default(730 days)',
221 1209600 => '2 weeks',
222 2592000 => '30 days',
223 7776000 => '90 days',
224 15552000 => '180 days',
225 31536000 => '365 days',
226 63072000 => '730 days',
228 disable_inventory => 1,
232 label => 'Mail trailer',
234 disable_inventory => 1,
237 'acct_def_cgp_language' => {
238 label => 'Acct. default language',
240 select_list => [ '', qw( English Arabic Chinese Dutch French German Hebrew Italian Japanese Portuguese Russian Slovak Spanish Thai ) ],
241 disable_inventory => 1,
244 'acct_def_cgp_timezone' => {
245 label => 'Acct. default time zone',
247 select_list => __PACKAGE__->cgp_timezone_values,
248 disable_inventory => 1,
251 'acct_def_cgp_skinname' => {
252 label => 'Acct. default layout',
254 select_list => [ '', '***', 'GoldFleece', 'Skin2' ],
255 disable_inventory => 1,
258 'acct_def_cgp_prontoskinname' => {
259 label => 'Acct. default Pronto style',
261 select_list => [ '', 'Pronto', 'Pronto-darkflame', 'Pronto-steel', 'Pronto-twilight', ],
262 disable_inventory => 1,
265 'acct_def_cgp_sendmdnmode' => {
266 label => 'Acct. default send read receipts',
268 select_list => [ '', 'Never', 'Manually', 'Automatically' ],
269 disable_inventory => 1,
276 sub table { 'svc_domain'; }
279 my($class, $string) = @_;
280 $class->search_sql_field('domain', $string);
295 =item insert [ , OPTION => VALUE ... ]
297 Adds this domain to the database. If there is an error, returns the error,
298 otherwise returns false.
300 The additional fields I<pkgnum> and I<svcpart> (see L<FS::cust_svc>) should be
301 defined. An FS::cust_svc record will be created and inserted.
303 The additional field I<action> should be set to I<N> for new domains, I<M>
304 for transfers, or I<I> for no action (registered elsewhere).
306 The additional field I<email> can be used to manually set the admin contact
307 email address on this email. Otherwise, the svc_acct records for this package
308 (see L<FS::cust_pkg>) are searched. If there is exactly one svc_acct record
309 in the same package, it is automatically used. Otherwise an error is returned.
311 If any I<soamachine> configuration file exists, an SOA record is added to
312 the domain_record table (see <FS::domain_record>).
314 If any records are defined in the I<defaultrecords> configuration file,
315 appropriate records are added to the domain_record table (see
316 L<FS::domain_record>).
318 Currently available options are: I<depend_jobnum>
320 If I<depend_jobnum> is set (to a scalar jobnum or an array reference of
321 jobnums), all provisioning jobs will have a dependancy on the supplied
322 jobnum(s) (they will not run until the specific job(s) complete(s)).
330 local $SIG{HUP} = 'IGNORE';
331 local $SIG{INT} = 'IGNORE';
332 local $SIG{QUIT} = 'IGNORE';
333 local $SIG{TERM} = 'IGNORE';
334 local $SIG{TSTP} = 'IGNORE';
335 local $SIG{PIPE} = 'IGNORE';
337 my $oldAutoCommit = $FS::UID::AutoCommit;
338 local $FS::UID::AutoCommit = 0;
341 $error = $self->SUPER::insert(@_);
343 $dbh->rollback if $oldAutoCommit;
348 my $soa = new FS::domain_record {
349 'svcnum' => $self->svcnum,
353 'recdata' => "$soamachine $soaemail ( ". time2str("%Y%m%d", time). "00 ".
354 "$soarefresh $soaretry $soaexpire $soadefaultttl )"
356 $error = $soa->insert;
358 $dbh->rollback if $oldAutoCommit;
359 return "couldn't insert SOA record for new domain: $error";
362 foreach my $record ( @defaultrecords ) {
363 my($zone,$af,$type,$data) = split(/\s+/,$record,4);
364 my $domain_record = new FS::domain_record {
365 'svcnum' => $self->svcnum,
371 my $error = $domain_record->insert;
373 $dbh->rollback if $oldAutoCommit;
374 return "couldn't insert record for new domain: $error";
380 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
387 Deletes this domain from the database. If there is an error, returns the
388 error, otherwise returns false.
390 The corresponding FS::cust_svc record will be deleted as well.
397 return "Can't delete a domain which has accounts!"
398 if qsearch( 'svc_acct', { 'domsvc' => $self->svcnum } );
400 #return "Can't delete a domain with (domain_record) zone entries!"
401 # if qsearch('domain_record', { 'svcnum' => $self->svcnum } );
403 local $SIG{HUP} = 'IGNORE';
404 local $SIG{INT} = 'IGNORE';
405 local $SIG{QUIT} = 'IGNORE';
406 local $SIG{TERM} = 'IGNORE';
407 local $SIG{TSTP} = 'IGNORE';
408 local $SIG{PIPE} = 'IGNORE';
410 my $oldAutoCommit = $FS::UID::AutoCommit;
411 local $FS::UID::AutoCommit = 0;
414 foreach my $domain_record ( reverse $self->domain_record ) {
415 my $error = $domain_record->delete;
417 $dbh->rollback if $oldAutoCommit;
418 return "can't delete DNS entry: ".
419 join(' ', map $domain_record->$_(),
420 qw( reczone recaf rectype recdata )
426 my $error = $self->SUPER::delete(@_);
428 $dbh->rollback if $oldAutoCommit;
432 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
435 =item replace OLD_RECORD
437 Replaces OLD_RECORD with this one in the database. If there is an error,
438 returns the error, otherwise returns false.
445 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
449 return "Can't change domain - reorder."
450 if $old->getfield('domain') ne $new->getfield('domain')
451 && ! $conf->exists('svc_domain-edit_domain');
453 # Better to do it here than to force the caller to remember that svc_domain is weird.
454 $new->setfield(action => 'I');
455 my $error = $new->SUPER::replace($old, @_);
456 return $error if $error;
461 Just returns false (no error) for now.
463 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
467 Just returns false (no error) for now.
469 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
473 Just returns false (no error) for now.
475 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
479 Checks all fields to make sure this is a valid domain. If there is an error,
480 returns the error, otherwise returns false. Called by the insert and replace
483 Sets any fixed values; see L<FS::part_svc>.
490 my $x = $self->setfixed;
491 return $x unless ref($x);
494 my $error = $self->ut_numbern('svcnum')
495 || $self->ut_numbern('catchall')
496 || $self->ut_numbern('max_accounts')
497 || $self->ut_anything('trailer') #well
498 || $self->ut_textn('cgp_aliases') #well
499 || $self->ut_enum('acct_def_password_selfchange', [ '', 'Y' ])
500 || $self->ut_enum('acct_def_password_recover', [ '', 'Y' ])
501 || $self->ut_textn('acct_def_cgp_accessmodes')
502 || $self->ut_alphan('acct_def_quota')
503 || $self->ut_alphan('acct_def_file_quota')
504 || $self->ut_alphan('acct_def_maxnum')
505 || $self->ut_alphan('acct_def_maxsize')
507 || $self->ut_alphasn('acct_def_cgp_rulesallowed')
508 || $self->ut_enum('acct_def_cgp_rpopallowed', [ '', 'Y' ])
509 || $self->ut_enum('acct_def_cgp_mailtoall', [ '', 'Y' ])
510 || $self->ut_enum('acct_def_cgp_addmailtrailer', [ '', 'Y' ])
511 || $self->ut_snumbern('acct_def_cgp_archiveafter')
513 || $self->ut_alphasn('acct_def_cgp_deletemode')
514 || $self->ut_enum('acct_def_cgp_emptytrash',
515 $self->cgp_emptytrash_values )
516 || $self->ut_alphan('acct_def_cgp_language')
517 || $self->ut_textn('acct_def_cgp_timezone')
518 || $self->ut_textn('acct_def_cgp_skinname')
519 || $self->ut_textn('acct_def_cgp_prontoskinname')
520 || $self->ut_alphan('acct_def_cgp_sendmdnmode')
523 return $error if $error;
527 if ( $self->svcnum ) {
528 my $cust_svc = qsearchs( 'cust_svc', { 'svcnum' => $self->svcnum } );
529 $pkgnum = $cust_svc->pkgnum;
531 $pkgnum = $self->pkgnum;
534 my($recref) = $self->hashref;
536 #if ( $recref->{domain} =~ /^([\w\-\.]{1,22})\.(com|net|org|edu)$/ ) {
537 if ( $recref->{domain} =~ /^([\w\-]{1,63})\.(com|net|org|edu|tv|info|biz)$/ ) {
538 $recref->{domain} = "$1.$2";
539 $recref->{suffix} ||= $2;
541 } elsif ( $whois_hack && $recref->{domain} =~ /^([\w\-\.\/]+)\.(\w+)$/ ) {
542 $recref->{domain} = "$1.$2";
543 # need to match a list of suffixes - no guarantee they're top-level..
544 # http://wiki.mozilla.org/TLD_List
545 # but this will have to do for now...
546 $recref->{suffix} ||= $2;
548 return "Illegal domain ". $recref->{domain}.
549 " (or unknown registry - try \$whois_hack)";
552 $self->suffix =~ /(^|\.)(\w+)$/
553 or return "can't parse suffix for TLD: ". $self->suffix;
555 return "No such TLD: .$tld" unless tld_exists($tld);
557 if ( $recref->{catchall} ne '' ) {
558 my $svc_acct = qsearchs( 'svc_acct', { 'svcnum' => $recref->{catchall} } );
559 return "Unknown catchall" unless $svc_acct;
562 $self->ut_alphan('suffix')
563 or $self->ut_foreign_keyn('registrarnum', 'registrar', 'registrarnum')
564 or $self->ut_textn('registrarkey')
565 or $self->ut_numbern('setup_date')
566 or $self->ut_numbern('renewal_interval')
567 or $self->ut_numbern('expiration_date')
568 or $self->SUPER::check;
572 sub _check_duplicate {
577 if ( qsearchs( 'svc_domain', { 'domain' => $self->domain } ) ) {
578 return "Domain in use (here)";
603 #'SOA' => sub { $_[0]->recdata cmp $_[1]->recdata }, #sure hope not though
604 # 'SOA' => sub { 0; },
605 # 'NS' => sub { 0; },
606 'MX' => sub { my( $a_weight, $a_name ) = split(/\s+/, $_[0]->recdata);
607 my( $b_weight, $b_name ) = split(/\s+/, $_[1]->recdata);
608 $a_weight <=> $b_weight or $a_name cmp $b_name;
610 'CNAME' => sub { $_[0]->reczone cmp $_[1]->reczone },
611 'A' => sub { $_[0]->reczone cmp $_[1]->reczone },
613 # 'TXT' => sub { 0; },
614 'PTR' => sub { $_[0]->reczone <=> $_[1]->reczone },
617 map { $_ } #return $self->num_domain_record( PARAMS ) unless wantarray;
618 sort { $order{$a->rectype} <=> $order{$b->rectype}
619 or &{ $sort{$a->rectype} || sub { 0; } }($a, $b)
621 qsearch('domain_record', { svcnum => $self->svcnum } );
625 sub catchall_svc_acct {
627 if ( $self->catchall ) {
628 qsearchs( 'svc_acct', { 'svcnum' => $self->catchall } );
636 # Returns the Net::Whois::Domain object (see L<Net::Whois>) for this domain, or
637 # undef if the domain is not found in whois.
639 (If $FS::svc_domain::whois_hack is true, returns that in all cases instead.)
644 #$whois_hack or new Net::Whois::Domain $_[0]->domain;
645 #$whois_hack or die "whois_hack not set...\n";
652 Delete doesn't send a registration template.
654 All registries should be supported.
656 Should change action to a real field.
658 The $recref stuff in sub check should be cleaned up.
662 L<FS::svc_Common>, L<FS::Record>, L<FS::Conf>, L<FS::cust_svc>,
663 L<FS::part_svc>, L<FS::cust_pkg>, L<Net::Whois>, schema.html from the base
664 documentation, config.html from the base documentation.