package PublicInbox::LeiMailSync;
use strict;
use v5.10.1;
+use parent qw(PublicInbox::Lock);
use DBI;
use PublicInbox::ContentHash qw(git_sha);
use Carp ();
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,
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;
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 (
$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
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
# 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 = ?
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;
}
"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) = @_;
$_ = $$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;
$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 {