]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/Watch.pm
watch: connect to NNTP and IMAP in config order
[public-inbox.git] / lib / PublicInbox / Watch.pm
index 8a457b815579543a8785ec2b31d33757a76797ed..c64689a159ee88f4714b3585af9d3ec994c030f5 100644 (file)
@@ -46,6 +46,7 @@ sub new {
        my ($class, $cfg) = @_;
        my (%mdmap, $spamc);
        my (%imap, %nntp); # url => [inbox objects] or 'watchspam'
+       my (@imap, @nntp);
 
        # "publicinboxwatch" is the documented namespace
        # "publicinboxlearn" is legacy but may be supported
@@ -61,8 +62,10 @@ sub new {
                                $mdmap{"$dir/cur"} = 'watchspam';
                        } elsif ($url = imap_url($dir)) {
                                $imap{$url} = 'watchspam';
+                               push @imap, $url;
                        } elsif ($url = nntp_url($dir)) {
                                $nntp{$url} = 'watchspam';
+                               push @nntp, $url;
                        } else {
                                warn "unsupported $k=$dir\n";
                        }
@@ -92,11 +95,13 @@ sub new {
                        } elsif ($url = imap_url($watch)) {
                                return if is_watchspam($url, $imap{$url}, $ibx);
                                compile_watchheaders($ibx);
-                               push @{$imap{$url} ||= []}, $ibx;
+                               my $n = push @{$imap{$url} ||= []}, $ibx;
+                               push @imap, $url if $n == 1;
                        } elsif ($url = nntp_url($watch)) {
                                return if is_watchspam($url, $nntp{$url}, $ibx);
                                compile_watchheaders($ibx);
-                               push @{$nntp{$url} ||= []}, $ibx;
+                               my $n = push @{$nntp{$url} ||= []}, $ibx;
+                               push @nntp, $url if $n == 1;
                        } else {
                                warn "watch unsupported: $k=$watch\n";
                        }
@@ -118,6 +123,8 @@ sub new {
                pi_cfg => $cfg,
                imap => scalar keys %imap ? \%imap : undef,
                nntp => scalar keys %nntp? \%nntp : undef,
+               imap_order => scalar(@imap) ? \@imap : undef,
+               nntp_order => scalar(@nntp) ? \@nntp: undef,
                importers => {},
                opendirs => {}, # dirname => dirhandle (in progress scans)
                ops => [], # 'quit', 'full'
@@ -280,55 +287,6 @@ sub watch_fs_init ($) {
        PublicInbox::DirIdle->new([keys %{$self->{mdmap}}], $cb);
 }
 
-sub cfg_intvl ($$$) {
-       my ($cfg, $key, $url) = @_;
-       my $v = $cfg->urlmatch($key, $url) // return;
-       $v =~ /\A[0-9]+(?:\.[0-9]+)?\z/s and return $v + 0;
-       if (ref($v) eq 'ARRAY') {
-               $v = join(', ', @$v);
-               warn "W: $key has multiple values: $v\nW: $key ignored\n";
-       } else {
-               warn "W: $key=$v is not a numeric value in seconds\n";
-       }
-}
-
-sub cfg_bool ($$$) {
-       my ($cfg, $key, $url) = @_;
-       my $orig = $cfg->urlmatch($key, $url) // return;
-       my $bool = $cfg->git_bool($orig);
-       warn "W: $key=$orig for $url is not boolean\n" unless defined($bool);
-       $bool;
-}
-
-# flesh out common IMAP-specific data structures
-sub imap_common_init ($) {
-       my ($self) = @_;
-       my $cfg = $self->{pi_cfg};
-       my $mic_args = {}; # scheme://authority => Mail:IMAPClient arg
-       for my $url (sort keys %{$self->{imap}}) {
-               my $uri = PublicInbox::URIimap->new($url);
-               my $sec = uri_section($uri);
-               for my $k (qw(Starttls Debug Compress)) {
-                       my $bool = cfg_bool($cfg, "imap.$k", $url) // next;
-                       $mic_args->{$sec}->{$k} = $bool;
-               }
-               my $to = cfg_intvl($cfg, 'imap.timeout', $url);
-               $mic_args->{$sec}->{Timeout} = $to if $to;
-               for my $k (qw(pollInterval idleInterval)) {
-                       $to = cfg_intvl($cfg, "imap.$k", $url) // next;
-                       $self->{imap_opt}->{$sec}->{$k} = $to;
-               }
-               my $k = 'imap.fetchBatchSize';
-               my $bs = $cfg->urlmatch($k, $url) // next;
-               if ($bs =~ /\A([0-9]+)\z/) {
-                       $self->{imap_opt}->{$sec}->{batch_size} = $bs;
-               } else {
-                       warn "$k=$bs is not an integer\n";
-               }
-       }
-       $mic_args;
-}
-
 sub imap_import_msg ($$$$$) {
        my ($self, $url, $uid, $raw, $flags) = @_;
        # our target audience expects LF-only, save storage
@@ -667,21 +625,7 @@ sub poll_fetch_reap {
 
 sub watch_imap_init ($$) {
        my ($self, $poll) = @_;
-       eval { require PublicInbox::IMAPClient } or
-               die "Mail::IMAPClient is required for IMAP:\n$@\n";
-       eval { require PublicInbox::IMAPTracker } or
-               die "DBD::SQLite is required for IMAP\n:$@\n";
-
-       my $mic_args = imap_common_init($self); # read args from config
-
-       # make sure we can connect and cache the credentials in memory
-       $self->{mic_arg} = {}; # schema://authority => IMAPClient->new args
-       my $mics = {}; # schema://authority => IMAPClient obj
-       for my $url (sort keys %{$self->{imap}}) {
-               my $uri = PublicInbox::URIimap->new($url);
-               $mics->{uri_section($uri)} //= mic_for($self, $url, $mic_args);
-       }
-
+       my $mics = imap_common_init($self); # read args from config
        my $idle = []; # [ [ url1, intvl1 ], [url2, intvl2] ]
        for my $url (keys %{$self->{imap}}) {
                my $uri = PublicInbox::URIimap->new($url);
@@ -706,7 +650,7 @@ sub nntp_common_init ($) {
        my ($self) = @_;
        my $cfg = $self->{pi_cfg};
        my $nn_args = {}; # scheme://authority => Net::NNTP->new arg
-       for my $url (sort keys %{$self->{nntp}}) {
+       for my $url (@{$self->{nntp_order}}) {
                my $sec = uri_section(uri_new($url));
 
                # Debug and Timeout are passed to Net::NNTP->new
@@ -818,10 +762,10 @@ sub watch_nntp_init ($$) {
 
        # make sure we can connect and cache the credentials in memory
        $self->{nn_arg} = {}; # schema://authority => Net::NNTP->new args
-       for my $url (sort keys %{$self->{nntp}}) {
+       for my $url (@{$self->{nntp_order}}) {
                nn_for($self, $url, $nn_args);
        }
-       for my $url (keys %{$self->{nntp}}) {
+       for my $url (@{$self->{nntp_order}}) {
                my $uri = uri_new($url);
                my $sec = uri_section($uri);
                my $intvl = $self->{nntp_opt}->{$sec}->{pollInterval};