1#! @im_path_perl@
2################################################################
3###
4###		  imgrep :: Internet Message g/RE/p
5###
6### Author:  Internet Message Group <img@mew.org>
7### Created: Apr 23, 1997
8### Revised: Apr 23, 2007
9###
10
11BEGIN {
12    @im_my_siteperl@
13    @im_src_siteperl@
14};
15
16$Prog = 'imgrep';
17my $VERSION_DATE = "20161010";
18my $VERSION_NUMBER = "153";
19my $VERSION = "${Prog} version ${VERSION_DATE}(IM${VERSION_NUMBER})";
20my $VERSION_INFORMATION = "${Prog} (IM ${VERSION_NUMBER}) ${VERSION_DATE}
21Copyright (C) 1999 IM developing team
22This is free software; see the source for copying conditions.  There is NO
23warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
24";
25
26##
27## Require packages
28##
29
30use IM::Config;
31use IM::Folder;
32use IM::Grep;
33use IM::Util;
34use integer;
35use strict;
36use vars qw($Prog $EXPLANATION @OptConfig
37	    $opt_format @opt_src $opt_casefold $opt_expression
38	    $opt_delimiter $opt_dupchecktarget $opt_verbose $opt_debug
39	    $opt_quiet $opt_help $opt_version);
40
41##
42## Environments
43##
44
45$opt_format = 'number';		# use number, folder, or path
46$opt_casefold = 'yes';		# default case-insensitive
47
48my @default_ranges = ();
49my @opt_folders = ();
50
51my $eval_string = '';		# store search engine
52
53# regexp for range syntax (sequence not supported)
54my $range_element = '(\\d+|cur|first|last|next|prev|new)';
55my $range_regexp = "($range_element(-$range_element|:[+-]?\\d+)?|all)";
56my $ranges_regexp = "$range_regexp(,$range_regexp)?";
57
58$EXPLANATION = "$VERSION
59search for mail/news messages
60
61Usage: $Prog [OPTIONS] [FOLDER] [RANGE]
62";
63
64@OptConfig = (
65    'src;F@;;' => 'msg source: "+folder:[range[,range..]]"',
66    'format;s;;' => 'Output format: "number", "folder", or "path"'.
67	"\n\t\t(default: \"number\" for single target folder".
68	"\n\t\t	     \"path\" for multiple target folders)",
69    'casefold;b;on;' => 'Case sensitivity'.
70	"\n\t\t(This option affects both fieldname and pattern)",
71    'expression;s;;' => 'Search expression:'.
72	"\n\n\t\tUsable keyword".
73        "\n\t\t\t& &&		     logical AND".
74        "\n\t\t\t| ||		     logical OR".
75        "\n\t\t\t!		     logical NOT".
76        "\n\t\t\t( )		     parens".
77        "\n\t\t\tfieldname=pattern	     search pattern for the field".
78        "\n\n\t\tSpecial fieldname:".
79        "\n\t\t\thead	     Entire header".
80        "\n\t\t\tbody	     Entire body".
81        "\n\t\t\tall	     Entire message",
82    'delimiter;s;\n\n|\n----\n;' => 'Mail header delimiter',
83    'dupchecktarget,D;s;none;' => 'Duplicate Check Target',
84    'quiet;b;;'   => 'Suppress informational messages',
85    'verbose;b;;' => 'With diagnostic messages',
86    'debug;d;;'   => 'With debug messages',
87    'help;b;;'    => "Display this help and exit",
88    'version,V;b;;' => "Output version information and exit",
89    );
90
91##
92## Profile and option processing
93##
94
95init_opt(\@OptConfig);
96read_cfg();
97read_opt(\@ARGV); # help?
98print("${VERSION_INFORMATION}") && exit $EXIT_SUCCESS if $opt_version;
99help($EXPLANATION) && exit $EXIT_SUCCESS if $opt_help;
100debug_option($opt_debug) if $opt_debug;
101
102##
103## Main
104##
105
106my $default_folder = cur_folder(); # must set here
107
108#
109# Fix some valuables
110#
111
112@opt_src = sortuniq(@opt_src);
113
114@opt_src or @opt_src = ($default_folder);
115@default_ranges = (@ARGV ? @ARGV : 'all');
116
117if ($opt_format eq 'number' and @opt_src > 1) {
118    $opt_format = 'path';
119}
120
121#
122# Compile expression
123#
124
125$eval_string = parse_expression($opt_expression, $opt_casefold);
126
127#
128# Iterate each folder
129#
130my $matched = 0;
131
132 FOLDER:
133    foreach (@opt_src) {
134	my($folder, $ranges) = (/^(.*?)(:$ranges_regexp)?$/);
135	$ranges =~ s/^:(.*)$/$1/;
136	my $folder_dir;
137	my @ranges = split(',', $ranges);
138	my @messages = ();
139
140	if ($folder =~ /^\-/) {
141	    im_warn("Newsspool $folder search not supported (ignored)\n");
142	    next FOLDER;
143	}
144
145	@ranges = ($ranges ? @ranges : @default_ranges);
146
147	$folder_dir = expand_path($folder);
148
149	@messages = grep_folder($folder_dir, $eval_string,
150				$opt_dupchecktarget, @ranges);
151	if (@messages) {
152	    $matched += @messages;
153	    foreach (@messages) {
154		print "$folder:" if ($opt_format eq 'folder');
155		print "$folder_dir/" if ($opt_format eq 'path');
156		print "$_\n";
157		# flush('STDOUT');
158	    }
159	}
160    }
161
162$matched
163    or do {
164	if ($opt_quiet) {
165	    exit $EXIT_ERROR;
166	} else {
167	    im_die2("no message matches specifications\n");
168	}
169    };
170exit $EXIT_SUCCESS;
171
172##
173## End of Main
174##
175##################################################
176
177__END__
178
179=head1 NAME
180
181imgrep - search for mail/news messages
182
183=head1 SYNOPSIS
184
185B<imgrep> [OPTIONS] [FOLDER] [RANGE]
186
187=head1 DESCRIPTION
188
189The I<imgrep> command enumerates mail/news messages matched given patterns.
190
191This command is provided by IM (Internet Message).
192
193=head1 OPTIONS
194
195=over 5
196
197=item I<-s, --src=FOLDER,FOLDER...>
198
199message source: "+folder:[range[,range..]]".
200
201"--src=+xxx" is equivalent to "+xxx".
202
203=item I<-f, --format=STRING>
204
205Output format: "number", "folder", or "path".
206
207(default: "number" for single target folder, "path" for multiple target folders)
208
209=item I<-c, --casefold={on,off}>
210
211Case sensitivity.
212(This option affects both fieldname and pattern.)
213
214Default value is "on".
215
216=item I<-e, --expression=STRING>
217
218Search expression:
219
220Usable keyword
221
222	& &&	logical AND
223
224	| ||	logical OR
225
226	!	logical NOT
227
228	( )	parens
229
230	fieldname=pattern	search pattern for the field
231
232Special fieldname:
233
234	head	Entire header
235
236	body	Entire body
237
238	all	Entire message
239
240=item I<-d, --delimiter=STRING>
241
242Mail header delimiter.
243
244Default value is "\n\n|\n----\n".
245
246=item I<-D, --dupchecktarget=STRING>
247
248Duplicate check target ('none', 'message-id', or 'message-id+subject').
249Default value is 'none'.
250
251=item I<-q, --quiet={on,off}>
252
253Do not show any messages.
254
255=item I<-v, --verbose={on,off}>
256
257Print verbose messages when running.
258
259=item I<--debug=DEBUG_OPTION>
260
261Print debug messages when running.
262
263=item I<-h, --help>
264
265Display help message and exit.
266
267=item I<--version>
268
269Output version information and exit.
270
271=back
272
273=head1 COPYRIGHT
274
275IM (Internet Message) is copyrighted by IM developing team.
276You can redistribute it and/or modify it under the modified BSD
277license.  See the copyright file for more details.
278
279=cut
280
281### Copyright (C) 1997, 1998, 1999 IM developing team
282### All rights reserved.
283###
284### Redistribution and use in source and binary forms, with or without
285### modification, are permitted provided that the following conditions
286### are met:
287###
288### 1. Redistributions of source code must retain the above copyright
289###    notice, this list of conditions and the following disclaimer.
290### 2. Redistributions in binary form must reproduce the above copyright
291###    notice, this list of conditions and the following disclaimer in the
292###    documentation and/or other materials provided with the distribution.
293### 3. Neither the name of the team nor the names of its contributors
294###    may be used to endorse or promote products derived from this software
295###    without specific prior written permission.
296###
297### THIS SOFTWARE IS PROVIDED BY THE TEAM AND CONTRIBUTORS ``AS IS'' AND
298### ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
299### IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
300### PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE TEAM OR CONTRIBUTORS BE
301### LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
302### CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
303### SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
304### BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
305### WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
306### OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
307### IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
308
309### Local Variables:
310### mode: perl
311### End:
312