EOM
my $opt = $lei->{opt};
my $all = $opt->{all};
- my @all = $lms->folders;
if (defined $all) { # --all=<local|remote>
my %x = map { $_ => $_ } split(/,/, $all);
my @ok = grep(defined, delete(@x{qw(local remote), ''}));
EOM
}
my (%seen, @inc);
+ my @all = $lms->folders;
for my $ok (@ok) {
if ($ok eq 'local') {
@inc = grep(!m!\A[a-z0-9\+]+://!i, @all);
no --mail-sync folders known to lei
EOM
} else {
- my %all = map { $_ => 1 } @all;
- my @no;
- for (@folders) {
- next if $all{$_}; # ok
- if (-d "$_/new" && -d "$_/cur") {
- my $d = 'maildir:'.$lei->abs_path($_);
- push(@no, $_) unless $all{$d};
- $_ = $d;
- } elsif (m!\Aimaps?://!i) {
- my $orig = $_;
- my $res = $lms->match_imap_url($orig, $all);
- if (ref $res) {
- $_ = $$res;
- $lei->qerr(<<EOM);
-# using `$res' instead of `$orig'
-EOM
- } else {
- $lei->err($res) if defined $res;
- push @no, $orig;
- }
- } else {
- push @no, $_;
- }
- }
- my $no = join("\n\t", @no);
- return $lei->fail(<<EOF) if @no;
-No sync information for: $no
-Run `lei ls-mail-sync' to display valid choices
-EOF
+ my $err = $lms->arg2folder($lei, \@folders);
+ $lei->qerr(@{$err->{qerr}}) if $err->{qerr};
+ return $lei->fail($err->{fail}) if $err->{fail};
}
my $self = bless { lse => $lse }, __PACKAGE__;
$lei->{opt}->{'mail-sync'} = 1; # for prepare_inputs
my $ent = {};
my $lse = $lei->{lse} or return $ent;
my $lms = $lse->lms or return $ent;
- my @folders;
- if ($folder =~ m!\Aimaps?://!i) {
- @folders = map { $_->as_string } $lms->match_imap_url($folder);
- } elsif ($folder =~ m!\A(maildir|mh):(.+)!i) {
- my $type = lc $1;
- $folders[0] = "$type:".$lei->abs_path($2);
- } elsif (-d $folder) {
- $folders[0] = 'maildir:'.$lei->abs_path($folder);
- } else {
- $lei->fail("$folder not understood");
+ my $folders = [ $folder ];
+ my $err = $lms->arg2folder($lei, $folders);
+ if ($err) {
+ if ($err->{fail}) {
+ $lei->qerr("# no folders match $folder (non-fatal)");
+ @$folders = ();
+ }
+ $lei->qerr(@{$err->{qerr}}) if $err->{qerr};
}
- $lei->qerr("# no folders match $folder (non-fatal)") if !@folders;
- for my $f (@folders) {
+ for my $f (@$folders) {
$ent->{$f} = $lms->location_stats($f); # may be undef
}
$ent
"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;
+}
+
1;