Remove js requirement for repeat searches
[mharc.git] / bin / extract-mesg-date
CommitLineData
2ea8f66b
IK
1#!/usr/bin/perl
2##--------------------------------------------------------------------------##
3## File:
4## $Id: extract-mesg-date,v 1.4 2002/09/15 03:33:08 ehood Exp $
5## Description:
6## See POD below or run program with -man option.
7##--------------------------------------------------------------------------##
8## Copyright (C) 2002 Earl Hood <earl@earlhood.com>
9##
10## This program is free software; you can redistribute it and/or modify
11## it under the terms of the GNU General Public License as published by
12## the Free Software Foundation; either version 2 of the License, or
13## (at your option) any later version.
14##
15## This program is distributed in the hope that it will be useful,
16## but WITHOUT ANY WARRANTY; without even the implied warranty of
17## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18## GNU General Public License for more details.
19##
20## You should have received a copy of the GNU General Public License
21## along with this program; if not, write to the Free Software
22## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23## 02111-1307, USA
24##--------------------------------------------------------------------------##
25
26package MHArc::extract_mesg_date;
27
28##--------------------------------------------------------------------------##
29# <x-boot-strap>
30BEGIN { die qq/CGI use FORBIDDEN!\n/ if (defined($ENV{'GATEWAY_INTERFACE'})); }
31my $Dir; BEGIN { $Dir = `dirname $0`; chomp $Dir; }
32use lib "$Dir/../lib"; # Add relative lib to search path
33# </x-boot-strap>
34##--------------------------------------------------------------------------##
35# <x-config>
36use MHArc::Config;
37my $config = MHArc::Config->load("$Dir/../lib/config.sh");
38# </x-config>
39##--------------------------------------------------------------------------##
40
41
42use Getopt::Long;
43use MHArc::Util qw( usage );
44use MHArc::MailUtil qw( extract_date );
45
46require 'mhamain.pl';
47
48my $debug = 0;
49my $verbose = 0;
50my $time_fmt = '%Y-%m';
51
52MAIN: {
53 # Load mhonarc code
54 mhonarc::initialize();
55 mhonarc::open_archive(
56 '-noarg',
57 '-quiet',
58 '-posixstrftime'
59 ) || die qq/ERROR: Unable to load MHonArc library\n/;
60 mhonarc::close_archive();
61
62 # Grap command-line options
63 my($opt_dfs);
64 my $clstatus = GetOptions(
65 "debug!" => \$debug,
66 "datefields=s" => \$opt_dfs,
67 "fmt=s" => \$time_fmt,
68
69 "help" => \$help,
70 "man" => \$man
71 );
72 usage(0) unless $clstatus;
73 usage(1) if $help;
74 usage(2) if $man;
75
76 if ($debug) {
77 $MHArc::MailUtil::Debug = 1;
78 }
79
80 my @date_fields = ();
81 if (defined($opt_dfs)) {
82 @date_fields = split(/:/, $opt_dfs);
83 } elsif (defined($config->{'MSG_DATE_FIELDS'})) {
84 @date_fields = split(/:/, $config->{'MSG_DATE_FIELDS'});
85 }
86 print "date_fields=@date_fields\n" if $debug;
87 my($fields, $header) = readmail::MAILread_file_header(\*STDIN);
88 print mhonarc::time2str(
89 $time_fmt, extract_date($fields, @date_fields), 1);
90
91} # End: MAIN
92
93##--------------------------------------------------------------------------##
94__END__
95
96=head1 NAME
97
98extract-mesg-date - Retrieve date of a mail message
99
100=head1 SYNOPSIS
101
102 extract-mesg-date [options]
103
104=head1 DESCRIPTION
105
106This program extracts the date of a mail message read in from
107standard input. The date of the message is determined by
108examining the following mail header fields in order:
109C<Received>, C<Delivery-Date>, C<Date>. The fields checked
110can be changed with the C<-datefields> option. If no date
111is found, than current local time is used.
112
113The date of the message will be echoed to standard output.
114The format of the date is controled by the C<-fmt> option.
115
116This program is provided as part of mharc to provide the ability to
117to determine the dates of messages during filtering.
118Example shell command usage:
119
120 mesg_date=`cat message | extract-mesg-date`
121
122Example useage within a procmail recipe:
123
124 :0 Wi
125 MESGDATE_=| extract-mesg-date -fmt '%Y-%m'
126
127 :0:
128 $MBOXROOT/.listsadmin/$MESGDATE_
129
130=head1 OPTIONS
131
132=over
133
134=item C<-datefields> I<date-fields-list>
135
136Specifies the message header fields to examine in determining the
137date of the message. Field names are separated by a colon.
138For example,
139
140 -datefields x-archive-date:received:date
141
142tells that C<X-Archive-Date>, C<Received>, and C<Date> should
143be examined.
144
145=item C<-debug>
146
147Print out debugging information.
148
149B<WARNING: DO NOT USE THIS OPTION WITHIN PROCMAILRC RULES.>
150
151=item C<-fmt> I<time-fmt-string>
152
153The time format to use. The format string syntax is the same as
154defined by C<strftime>.
155
156If C<-fmt> is not specified, than "C<%Y-%m>" is used.
157
158=item C<-help>
159
160Print out help message.
161
162=item C<-man>
163
164Print out the manpage.
165
166=back
167
168=head1 DEPENDENCIES
169
170This program uses functions within MHonArc's library. Therefore,
171MHonArc must be installed on your system and the MHonArc libraries
172located within Perl's include path.
173
174=head1 VERSION
175
176$Id: extract-mesg-date,v 1.4 2002/09/15 03:33:08 ehood Exp $
177
178=head1 AUTHOR
179
180Earl Hood, earl@earlhood.com
181
182This program is part of the mharc archiving system and comes with
183ABSOLUTELY NO WARRANTY and may be copied only under the terms of
184the GNU General Public License, which may be found in the mharc
185distribution.
186
187=cut
188