use Mail::IMAPTalk instead of Mail::IMAPClient Mail::IMAPTalk
authorMatthias Förste foerste@schlittermann.de
Wed, 21 Dec 2011 14:14:37 +0100
branchMail::IMAPTalk
changeset 11 87ddf3322246
parent 10 d01a8cd90e33
child 12 db22b068b8cc
use Mail::IMAPTalk instead of Mail::IMAPClient
TODO
isearch
--- a/TODO	Wed Nov 09 21:08:32 2011 +0100
+++ b/TODO	Wed Dec 21 14:14:37 2011 +0100
@@ -1,1 +1,2 @@
+* hangs with certain mailbox names (.2007.11 fex, probably the leading dot)
 * how to (really) deal with possible errors? maybe '--keep-going'?
--- a/isearch	Wed Nov 09 21:08:32 2011 +0100
+++ b/isearch	Wed Dec 21 14:14:37 2011 +0100
@@ -25,7 +25,9 @@
 
 use Getopt::Long;
 use Pod::Usage;
-use Mail::IMAPClient;
+use Mail::IMAPTalk;
+
+sub folders($);
 
 my $opts;
 my %imapopts = (
@@ -47,9 +49,13 @@
     "password=s" => \$imapopts{Password},
     "p|port=s"   => \$imapopts{Port},
     "s|server=s" => \$imapopts{Server},
-    "u|user=s"   => \$imapopts{User}
+    "u|user=s"   => \$imapopts{Username}
 ) or pod2usage();
-defined $opts->{criteria} and defined $imapopts{User} or pod2usage();
+defined $opts->{criteria} and defined $imapopts{Username} or pod2usage();
+
+# quoting not supported, whitespace has to be escaped
+my @criteria = split /(?<!\\) /, $opts->{criteria};
+#my @criteria = qw(or or to schlittermann cc schlittermann bcc schlittermann);
 
 if (defined $imapopts{Password}) {
 
@@ -73,11 +79,13 @@
 
 }
 
-my $imap = Mail::IMAPClient->new(%imapopts)
+my $imap = Mail::IMAPTalk->new(%imapopts)
   or die "Could not connect to IMAP server\n";
 
-my $f = @ARGV ? [@ARGV] : $imap->folders;
-die "Can't get folderlist: " . $imap->LastError . "\n" unless $f;
+$imap->set_tracing(1) if $ENV{TRACE};
+
+my $f = @ARGV ? [@ARGV] : folders($imap);
+die "No folders found\n" unless $f;
 
 my $mids;
 
@@ -88,26 +96,27 @@
     print "Examining $_ ...\n";
     unless ($imap->examine($_)) {
 
-        my $e = $imap->LastError;
+        my $e = $@;
 
         # skip folder with invalid name
         if (   $e =~ /BAD Error in IMAP command EXAMINE: 8bit data in atom$/
             or $e =~ /NO Invalid mailbox name: /) {
 
-            warn "[WARN] Can't examine '$_': $e\n";
+            warn "[WARN] Can't examine '$_': $e";
             print "Skipping $_ ...\n";
             next;
 
         } else {
 
-            die "Can't examine '$_': $e\n";
+            die "Can't examine '$_': $e";
 
         }
 
     }
 
     print "Searching $_ ...\n";
-    if (my $m = $imap->search($opts->{criteria})) {
+    #if (my $m = $imap->search('1:*', $opts->{criteria})) {
+    if (my $m = $imap->search('1:*', @criteria)) {
         $mids->{$_} = $m;
     } elsif (my $e = $@) {
 
@@ -138,9 +147,9 @@
     if (@{$m}) {
 
         $imap->examine($f)
-          or die "Can't examine: " . $imap->LastError . "\n";
+          or die "Can't examine: " . $@ . "\n";
         my $h = $imap->parse_headers($mids->{$f}, @headers)
-          or die "Can't get summaries: " . $imap->LastError . "\n";
+          or die "Can't get summaries: " . $@ . "\n";
 
         for my $id (keys %{$h}) {
 
@@ -157,6 +166,25 @@
 
 print "Done\n";
 
+sub folders($) {
+    
+    my ($i) = @_;
+
+    my $l;
+    my ($r, $f) = ('', '*');
+    die "Can't list('$r', '$f'): $@" unless $l = $i->list($r, $f);
+
+    # list looks like this: [ [[flags1], separator1, name1], [[flags2], separator2, name2], ... ]
+    ref $l eq 'ARRAY' and return [ map $_->[2], @{$l} ];
+
+    # this should not happen
+    ref $l and die "array ref or ordinary scalar expexted\n";
+
+    # assuming empty list result
+    return [];
+
+}
+
 __END__
 
 =pod