partly working
authorMatthias Förste foerste@schlittermann.de
Fri, 28 Oct 2011 14:45:13 +0200
changeset 0 5f2dde2b27d1
child 1 4fd1c0261e37
partly working
isearch
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/isearch	Fri Oct 28 14:45:13 2011 +0200
@@ -0,0 +1,198 @@
+#!/usr/bin/perl
+
+#    Copyright (C) 2011  Matthias Förste
+#
+#    This program is free software: you can redistribute it and/or modify
+#    it under the terms of the GNU General Public License as published by
+#    the Free Software Foundation, either version 3 of the License, or
+#    (at your option) any later version.
+#
+#    This program is distributed in the hope that it will be useful,
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#    GNU General Public License for more details.
+#
+#    You should have received a copy of the GNU General Public License
+#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+#    Matthias Förste <foerste@schlittermann.de>
+
+=encoding utf8
+=cut
+
+use strict;
+use warnings;
+
+use Getopt::Long;
+use Pod::Usage;
+use Net::IMAP::Client 0.95;
+
+my $opts;
+my %imapopts = (
+    server => 'localhost',
+    port   => 143,
+);
+
+GetOptions(
+    "charset=s"    => \$opts->{charset},
+    "c|criteria=s" => \$opts->{criteria},
+    "h|help"       => sub { pod2usage( -verbose => 0, -exitval => 0 ) },
+    "m|man"        => sub {
+        pod2usage(
+            -verbose   => 2,
+            -exitval   => 0,
+            -noperldoc => ( `perldoc -V 2>/dev/null`, $? != 0 )[-1]
+        );
+    },
+    "password=s" => \$imapopts{pass},
+    "p|port=s"   => \$imapopts{port},
+    "s|server=s" => \$imapopts{server},
+    "u|user=s"   => \$imapopts{user}
+) or pod2usage();
+defined $opts->{criteria} and defined $imapopts{user} or pod2usage();
+
+if ( defined $imapopts{pass} ) {
+
+    if ( -r $imapopts{pass} ) {
+
+        open P, '<', $imapopts{pass}
+          or die "\nCan't open '$imapopts{pass}' for reading: $!";
+        $imapopts{pass} = <P>;
+        chomp $imapopts{pass};
+
+    }
+
+} else {
+    print "\nPassword:";
+
+    use Term::ReadKey;
+
+    ReadMode 'noecho';
+    $imapopts{pass} = ReadLine 0;
+    ReadMode 0;
+
+}
+
+my $imap = Net::IMAP::Client->new(%imapopts)
+  or die "\nCould not connect to IMAP server";
+
+$imap->login
+  or die "\nLogin failed: " . $imap->last_error;
+
+my @f = ( @ARGV or $imap->folders );
+die "\nCan't get folderlist: " . $imap->last_error unless @f;
+
+my $mids;
+
+print "\n";
+$| = 1;
+
+for (@f) {
+
+    next unless /volke.*2011/;
+
+    print "\nExamining $_ ...";
+    $imap->examine($_)
+      or die "\nCan't examine: " . $imap->last_error;
+    print "\nSearching $_ ...";
+    my $m = $imap->search( $opts->{criteria}, undef, $opts->{charset} )
+      or die "\nCan't search: " . $imap->last_error;
+    $mids->{$_} = $m;
+
+}
+
+for ( sort keys %{$mids} ) {
+
+    my $m = $mids->{$_};
+
+    if ( @{$m} ) {
+
+        $imap->examine($_)
+          or die "\nCan't examine: " . $imap->last_error;
+        my $s = $imap->get_summaries( $mids->{$_} )
+          or die "\nCan't get summaries: " . $imap->last_error;
+
+        for ( @{$s} ) {
+
+            print "\n" . $_->subject;
+
+        }
+
+    }
+
+}
+
+print "\nDone\n";
+
+__END__
+
+=pod
+
+=head1 NAME
+
+isearch - search mailbox(es) on imap server
+
+=head1 SYNOPSIS
+
+isearch -c|--criteria criteria
+        [--charset charset]
+        [--password password]
+        [-p|--port port]
+        [-s|--server host]
+        -u|--user user
+        [mailbox1 [mailbox2 .. ]]
+
+isearch -m|--man
+        -h|--help
+
+=head1 DESCRIPTION
+
+Search through one or more imap mailbox(es). If no mailbox is specified then
+every mailbox will be searched.
+
+=head1 OPTIONS
+
+=over
+
+=item B<-c|--criteria> I<criteria>
+
+The criteria to search for. See the section about the SEARCH command in the
+IMAP RFC (3501 currently) for valid criteria.
+
+=item B<--charset> I<charset>
+
+Charset to use for IMAP Search. Defaults to UTF-8.
+
+=item B<--password> I<password>
+
+The password to use for authentication against the imap server. If this is the
+name of a readable file then the password will be read from it. You will be
+asked for it when connecting if you dont provide it via commandline.
+
+=item B<-p|--port> I<port>
+
+The port to connect to. This may be a portname(?) or a portnumber. Defaults to 143.
+
+=item B<-s|--server> I<host>
+
+The host to connect to. This may be a hostname or an ip address. Defaults to localhost.
+
+=item B<-u|--user> I<user>
+
+The username to use for authentication against the imap server.
+
+=back
+
+=head1 TODO
+
+Encryption.
+
+=head1 FILES
+
+None yet.
+
+=head1 AUTHOR
+
+Matthias Förste <foerste@schlittermann.de>
+
+=cut