]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/LeiMailSync.pm
lei inspect: support NNTP URLs
[public-inbox.git] / lib / PublicInbox / LeiMailSync.pm
index 8f584ccbb7ad8f68a7c1d0af4aaf8f4d4026f7a8..d9b9e117a742b8801f7d9bfb6ff4c3924139c010 100644 (file)
@@ -5,6 +5,7 @@
 package PublicInbox::LeiMailSync;
 use strict;
 use v5.10.1;
+use parent qw(PublicInbox::Lock);
 use DBI;
 use PublicInbox::ContentHash qw(git_sha);
 use Carp ();
@@ -13,6 +14,11 @@ sub dbh_new {
        my ($self, $rw) = @_;
        my $f = $self->{filename};
        my $creat = $rw && !-s $f;
+       if ($creat) {
+               require PublicInbox::Spawn;
+               open my $fh, '+>>', $f or Carp::croak "open($f): $!";
+               PublicInbox::Spawn::nodatacow_fd(fileno($fh));
+       }
        my $dbh = DBI->connect("dbi:SQLite:dbname=$f",'','', {
                AutoCommit => 1,
                RaiseError => 1,
@@ -21,7 +27,7 @@ sub dbh_new {
                sqlite_use_immediate_transaction => 1,
        });
        # no sqlite_unicode, here, all strings are binary
-       create_tables($dbh) if $rw;
+       create_tables($self, $dbh) if $rw;
        $dbh->do('PRAGMA journal_mode = WAL') if $creat;
        $dbh->do('PRAGMA case_sensitive_like = ON');
        $dbh;
@@ -29,13 +35,24 @@ sub dbh_new {
 
 sub new {
        my ($cls, $f) = @_;
-       bless { filename => $f, fmap => {} }, $cls;
+       bless {
+               filename => $f,
+               fmap => {},
+               lock_path => "$f.flock",
+       }, $cls;
 }
 
-sub lms_write_prepare { ($_[0]->{dbh} //= dbh_new($_[0], 1)) };
+sub lms_write_prepare { ($_[0]->{dbh} //= dbh_new($_[0], 1)); $_[0] }
+
+sub lms_pause {
+       my ($self) = @_;
+       $self->{fmap} = {};
+       delete $self->{dbh};
+}
 
 sub create_tables {
-       my ($dbh) = @_;
+       my ($self, $dbh) = @_;
+       my $lk = $self->lock_for_scope;
 
        $dbh->do(<<'');
 CREATE TABLE IF NOT EXISTS folders (
@@ -115,8 +132,15 @@ EOM
        $fid;
 }
 
+sub add_folders {
+       my ($self, @folders) = @_;
+       my $lk = $self->lock_for_scope;
+       for my $f (@folders) { $self->{fmap}->{$f} //= fid_for($self, $f, 1) }
+}
+
 sub set_src {
        my ($self, $oidbin, $folder, $id) = @_;
+       my $lk = $self->lock_for_scope;
        my $fid = $self->{fmap}->{$folder} //= fid_for($self, $folder, 1);
        my $sth;
        if (ref($id)) { # scalar name
@@ -134,6 +158,7 @@ INSERT OR IGNORE INTO blob2num (oidbin, fid, uid) VALUES (?, ?, ?)
 
 sub clear_src {
        my ($self, $folder, $id) = @_;
+       my $lk = $self->lock_for_scope;
        my $fid = $self->{fmap}->{$folder} //= fid_for($self, $folder, 1);
        my $sth;
        if (ref($id)) { # scalar name
@@ -152,6 +177,7 @@ DELETE FROM blob2num WHERE fid = ? AND uid = ?
 # Maildir-only
 sub mv_src {
        my ($self, $folder, $oidbin, $id, $newbn) = @_;
+       my $lk = $self->lock_for_scope;
        my $fid = $self->{fmap}->{$folder} //= fid_for($self, $folder, 1);
        my $sth = $self->{dbh}->prepare_cached(<<'');
 UPDATE blob2name SET name = ? WHERE fid = ? AND oidbin = ? AND name = ?
@@ -221,12 +247,14 @@ sub location_stats {
 SELECT COUNT(name) FROM blob2name WHERE fid = ?
 
        $ret->{'name.count'} = $row if $row;
+       my $ntype = ($folder =~ m!\A(?:nntps?|s?news)://!i) ? 'article' :
+               (($folder =~ m!\Aimaps?://!i) ? 'uid' : "TODO<$folder>");
        for my $op (qw(count min max)) {
                ($row) = $dbh->selectrow_array(<<"", undef, $fid);
 SELECT $op(uid) FROM blob2num WHERE fid = ?
 
                $row or last;
-               $ret->{"uid.$op"} = $row;
+               $ret->{"$ntype.$op"} = $row;
        }
        $ret;
 }
@@ -343,6 +371,30 @@ sub match_imap_url {
                        "E: `$url' is ambiguous:\n\t".join("\n\t", @match)."\n";
 }
 
+sub match_nntp_url ($$$) {
+       my ($self, $url, $all) = @_; # $all = [ $lms->folders ];
+       $all //= [ $self->folders ];
+       require PublicInbox::URInntps;
+       my $want = PublicInbox::URInntps->new($url)->canonical;
+       my ($s, $h, $p) = ($want->scheme, $want->host, $want->port);
+       my $ng = $want->group; # force scalar (no article ranges)
+       my @uri = map { PublicInbox::URInntps->new($_)->canonical }
+               grep(m!\A\Q$s\E://.*?\Q$h\E\b.*?/\Q$ng\E\b!, @$all);
+       my @match;
+       for my $x (@uri) {
+               next if $x->group ne $ng || $x->host ne $h || $x->port != $p;
+               # maybe user was forgotten on CLI:
+               if (defined($x->userinfo) && !defined($want->userinfo)) {
+                       push @match, $x;
+               } elsif (($x->userinfo//"\0") eq ($want->userinfo//"\0")) {
+                       push @match, $x;
+               }
+       }
+       return @match if wantarray;
+       scalar(@match) <= 1 ? $match[0] :
+                       "E: `$url' is ambiguous:\n\t".join("\n\t", @match)."\n";
+}
+
 # returns undef on failure, number on success
 sub group2folders {
        my ($self, $lei, $all, $folders) = @_;
@@ -402,6 +454,18 @@ sub arg2folder {
                                $_ = $$res;
                                push(@{$err->{qerr}}, <<EOM);
 # using `$res' instead of `$orig'
+EOM
+                       } else {
+                               $lei->err($res) if defined $res;
+                               push @no, $orig;
+                       }
+               } elsif (m!\A(?:nntps?|s?news)://!i) {
+                       my $orig = $_;
+                       my $res = match_nntp_url($self, $orig, \@all);
+                       if (ref $res) {
+                               $_ = $$res;
+                               push(@{$err->{qerr}}, <<EOM);
+# using `$res' instead of `$orig'
 EOM
                        } else {
                                $lei->err($res) if defined $res;
@@ -421,18 +485,23 @@ EOF
        $err;
 }
 
-sub forget_folder {
-       my ($self, $folder) = @_;
-       my $fid = delete($self->{fmap}->{$folder}) //
-               fid_for($self, $folder) // return;
-       for my $t (qw(blob2name blob2num folders)) {
-               $self->{dbh}->do("DELETE FROM $t WHERE fid = ?", undef, $fid);
+sub forget_folders {
+       my ($self, @folders) = @_;
+       my $lk = $self->lock_for_scope;
+       for my $folder (@folders) {
+               my $fid = delete($self->{fmap}->{$folder}) //
+                       fid_for($self, $folder) // next;
+               for my $t (qw(blob2name blob2num folders)) {
+                       $self->{dbh}->do("DELETE FROM $t WHERE fid = ?",
+                                       undef, $fid);
+               }
        }
 }
 
 # only used for changing canonicalization errors
 sub rename_folder {
        my ($self, $old, $new) = @_;
+       my $lk = $self->lock_for_scope;
        my $ofid = delete($self->{fmap}->{$old}) //
                fid_for($self, $old) // return;
        eval {