+ $cleanup_timer ||= PublicInbox::EvCleanup::later(*cleanup_task);
+ $CLEANUP->{"$self"} = $self;
+}
+
+sub _set_uint ($$$) {
+ my ($opts, $field, $default) = @_;
+ my $val = $opts->{$field};
+ if (defined $val) {
+ $val = $val->[-1] if ref($val) eq 'ARRAY';
+ $val = undef if $val !~ /\A\d+\z/;
+ }
+ $opts->{$field} = $val || $default;
+}
+
+sub _set_limiter ($$$) {
+ my ($self, $pi_config, $pfx) = @_;
+ my $lkey = "-${pfx}_limiter";
+ $self->{$lkey} ||= eval {
+ # full key is: publicinbox.$NAME.httpbackendmax
+ my $mkey = $pfx.'max';
+ my $val = $self->{$mkey} or return;
+ my $lim;
+ if ($val =~ /\A\d+\z/) {
+ require PublicInbox::Qspawn;
+ $lim = PublicInbox::Qspawn::Limiter->new($val);
+ } elsif ($val =~ /\A[a-z][a-z0-9]*\z/) {
+ $lim = $pi_config->limiter($val);
+ warn "$mkey limiter=$val not found\n" if !$lim;
+ } else {
+ warn "$mkey limiter=$val not understood\n";
+ }
+ $lim;
+ }
+}
+
+sub new {
+ my ($class, $opts) = @_;
+ my $v = $opts->{address} ||= 'public-inbox@example.com';
+ my $p = $opts->{-primary_address} = ref($v) eq 'ARRAY' ? $v->[0] : $v;
+ $opts->{domain} = ($p =~ /\@(\S+)\z/) ? $1 : 'localhost';
+ my $pi_config = delete $opts->{-pi_config};
+ _set_limiter($opts, $pi_config, 'httpbackend');
+ _set_uint($opts, 'feedmax', 25);
+ $opts->{nntpserver} ||= $pi_config->{'publicinbox.nntpserver'};
+ bless $opts, $class;