]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/LeiMailSync.pm
lei forget-mail-sync: new command to drop sync information
[public-inbox.git] / lib / PublicInbox / LeiMailSync.pm
index 63076fa1c0712c45b82f44e5446ff05b8b1fe31c..d9c305800bad91bb4eab51f45e6609dec3afcc31 100644 (file)
@@ -302,4 +302,58 @@ sub match_imap_url {
                        "E: `$url' is ambiguous:\n\t".join("\n\t", @match)."\n";
 }
 
+# map CLI args to folder table entries, returns undef on failure
+sub arg2folder {
+       my ($self, $lei, $folders) = @_;
+       my @all = $self->folders;
+       my %all = map { $_ => 1 } @all;
+       my ($err, @no);
+       for (@$folders) {
+               next if $all{$_}; # ok
+               if (m!\A(maildir|mh):(.+)!i) {
+                       my $type = lc $1;
+                       my $d = "$type:".$lei->abs_path($2);
+                       push(@no, $_) unless $all{$d};
+                       $_ = $d;
+               } elsif (-d "$_/new" && -d "$_/cur") {
+                       my $d = 'maildir:'.$lei->abs_path($_);
+                       push(@no, $_) unless $all{$d};
+                       $_ = $d;
+               } elsif (m!\Aimaps?://!i) {
+                       my $orig = $_;
+                       my $res = match_imap_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;
+                               push @no, $orig;
+                       }
+               } else {
+                       push @no, $_;
+               }
+       }
+       if (@no) {
+               my $no = join("\n\t", @no);
+               $err->{fail} = <<EOF;
+No sync information for: $no
+Run `lei ls-mail-sync' to display valid choices
+EOF
+       }
+       $err;
+}
+
+sub forget_folder {
+       my ($self, $folder) = @_;
+       my ($fid, $sth);
+       $fid = delete($self->{fmap}->{$folder}) //
+               _fid_for($self, $folder) // return;
+       my $dbh = $self->{dbh};
+       $dbh->do('DELETE FROM blob2name WHERE fid = ?', undef, $fid);
+       $dbh->do('DELETE FROM blob2num WHERE fid = ?', undef, $fid);
+       $dbh->do('DELETE FROM folders WHERE fid = ?', undef, $fid);
+}
+
 1;