Debian Codegraph @

scripts/lsmail

raw
1 #!/usr/bin/perl -w
2
3 use strict;
4 use lib '.';
5 use Mail::Box::Manager;
6
7 my $VERSION = '2.019';
8
9 Mail::Box->VERSION($VERSION);
10
11 =head1 NAME
12
13 lsmail - list a mailbox
14
15 =head1 SYNOPSIS
16
17 lsmail [--entities][--header][--nheader][--size]
18 [--threads][--verbose][--help] mailboxes
19
20 =cut
21
22 sub usage($)
23 { my $rc = shift;
24
25 warn <<USAGE;
26 Usage: $0 [options] mailbox
27 options:
28 --entities number of entities
29 --header print only mails containting
30 a key=value pair in header
31 --nheader same as --header, but negated
32 --size print size
33 --threads list in threads
34 --verbose print what is done including warnings etc.
35 --help -? show this help
36 USAGE
37
38 exit $rc;
39 }
40
41 my %option =
42 ( entitities => 0
43 , size => 0
44 , threads => 0
45 , help => 0
46 , verbose => 0
47 );
48
49 sub get_options()
50 { use Getopt::Long;
51
52 GetOptions(\%option,
53 'entities',
54 'size',
55 'threads',
56 'help|?!',
57 'verbose',
58 'header=s%',
59 'nheader=s%'
60 );
61 }
62
63 sub trace(@) { warn @_,"\n" if $option{verbose} }
64
65 sub format_item ($) {
66 my $message = shift;
67 chomp(my $ret = $message->head->get('subject') || '<no subject>');
68 eval {
69 $ret .= " - ".scalar $message->parts if $option{entities};
70 };
71 return $ret;
72 }
73
74 #####
75 ##### MAIN
76 #####
77
78 usage 22 unless get_options;
79 usage 0 if $option{help};
80
81 open STDERR, ">/dev/null" if not $option{verbose};
82
83 my @mailboxes = @ARGV;
84 my $mgr = Mail::Box::Manager->new;
85
86 #
87 # Open the folders.
88 #
89
90 my @folders;
91 my @open_options =
92 ( access => 'r'
93 , extract => 'LAZY'
94 );
95
96 if(@mailboxes)
97 { foreach my $mailbox (@mailboxes)
98 { trace "Opening folder $mailbox.";
99
100 my $folder = $mgr->open(folder => $mailbox, @open_options);
101
102 die "Mailbox $mailbox cannot be opened.\n"
103 unless $folder;
104
105 push @folders, $folder;
106 }
107 }
108 else
109 { trace "Opening default folder";
110
111 my $folder = $mgr->open(@open_options);
112
113 die "Default mailbox cannot be read.\n"
114 unless $folder;
115
116 push @folders, $folder;
117 }
118
119 #
120 # List the folders
121 #
122
123 if($option{threads})
124 { trace "Collecting threads for all folders.";
125 my $threads = $mgr->threads(folders => \@folders);
126
127 trace "Dumping threads.";
128 foreach my $thread ($threads->sortedAll){
129 print $thread->threadToString(\&format_item) unless not $thread;
130 }
131 }
132 else
133 { trace "List subjects.";
134 foreach my $folder (@folders) {
135 open STDERR, ">/dev/null";
136
137 MESSAGE:
138 foreach my $message ($folder->messages) {
139
140 if($option{header}) {
141 for my $h (keys %{$option{header}}) {
142 my $pat = $option{header}{$h};
143 next MESSAGE unless $message->head->get($h) =~ /$pat/;
144 }
145 }
146
147 if($option{nheader}) {
148 for my $h (keys %{$option{nheader}}) {
149 my $pat = $option{nheader}{$h};
150 next MESSAGE if $message->head->get($h) =~ /$pat/;
151 }
152 }
153
154 eval {
155 my $subject = $message->head->get('subject')
156 || '<no subject>';
157 print "subject: ", $subject, "\n";
158 };
159
160 # check further options
161 printf "size: %.1fK\n", $message->size / 1024 if $option{size};
162 printf "entities: %d\n", scalar $message->parts
163 if $option{entities};
164
165 print "-" x 40, "\n";
166 }
167 }
168 }
169
170 #
171 # Close folders
172 #
173
174 $mgr->closeAllFolders;
175
176 exit 0;
177 #-------------------------------------------
178
179 __END__
180
181 =head1 DESCRIPTION
182
183 List the contents of one or more mailboxes. Specifying more than one
184 name is only useful when you read in threads, in which case the messages
185 of both folders are merged.
186
187 Options:
188
189 =over 4
190
191 =item --entities =E<gt> BOOLEAN
192
193 (or C<-e>) show the number of MIME-entities in messages. When --threads is
194 also given, the number is appended to the subject. Slows down lsmail
195 significantly since a mail message must be fully parsed to get the entities
196 information.
197
198 =item --size =E<gt> BOOLEAN
199
200 (or C<-s>) also show size of message in non-threaded output.
201
202 =item --header HEADER-FIELD=PATTERN
203
204 only display mails whose HEADER-FIELD (for instance 'subject' or 'from')
205 contains PATTERN.
206
207 =item --nheader HEADER-FIELD=PATTERN
208
209 do not display mails whose HEADER-FIELD contains PATTERN.
210
211 =item --threads =E<gt> BOOLEAN
212
213 (or C<-t> or C<-nothreads> or C<-not>) list the messages in threads. The
214 threads are sorted by time: the thread with the oldest starting message comes
215 first.
216
217 =item --help =E<gt> BOOLEAN
218
219 (or C<--help> or C<-?>) Print a brief help
220
221 =item --verbose =E<gt> BOOLEAN
222
223 (or C<--verbose> or C<--noverbose>) Be verbose about the progress of the
224 program. This is especially useful for debugging purposes.
225
226 =back
227
228 =head1 AUTHOR
229
230 Mark Overmeer (F<[email protected]>).
231 All rights reserved. This program is free software; you can redistribute
232 it and/or modify it under the same terms as Perl itself.
233
234 Additions by Tassilo v. Parseval (F<[email protected]>)
235
236 =head1 VERSION
237
238 This code is beta, version 2.019
239
240 =cut
241
242 1;
243