Commit | Line | Data |
---|---|---|
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 | ||
26 | package MHArc::extract_mesg_date; | |
27 | ||
28 | ##--------------------------------------------------------------------------## | |
29 | # <x-boot-strap> | |
30 | BEGIN { die qq/CGI use FORBIDDEN!\n/ if (defined($ENV{'GATEWAY_INTERFACE'})); } | |
31 | my $Dir; BEGIN { $Dir = `dirname $0`; chomp $Dir; } | |
32 | use lib "$Dir/../lib"; # Add relative lib to search path | |
33 | # </x-boot-strap> | |
34 | ##--------------------------------------------------------------------------## | |
35 | # <x-config> | |
36 | use MHArc::Config; | |
37 | my $config = MHArc::Config->load("$Dir/../lib/config.sh"); | |
38 | # </x-config> | |
39 | ##--------------------------------------------------------------------------## | |
40 | ||
41 | ||
42 | use Getopt::Long; | |
43 | use MHArc::Util qw( usage ); | |
44 | use MHArc::MailUtil qw( extract_date ); | |
45 | ||
46 | require 'mhamain.pl'; | |
47 | ||
48 | my $debug = 0; | |
49 | my $verbose = 0; | |
50 | my $time_fmt = '%Y-%m'; | |
51 | ||
52 | MAIN: { | |
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 | ||
98 | extract-mesg-date - Retrieve date of a mail message | |
99 | ||
100 | =head1 SYNOPSIS | |
101 | ||
102 | extract-mesg-date [options] | |
103 | ||
104 | =head1 DESCRIPTION | |
105 | ||
106 | This program extracts the date of a mail message read in from | |
107 | standard input. The date of the message is determined by | |
108 | examining the following mail header fields in order: | |
109 | C<Received>, C<Delivery-Date>, C<Date>. The fields checked | |
110 | can be changed with the C<-datefields> option. If no date | |
111 | is found, than current local time is used. | |
112 | ||
113 | The date of the message will be echoed to standard output. | |
114 | The format of the date is controled by the C<-fmt> option. | |
115 | ||
116 | This program is provided as part of mharc to provide the ability to | |
117 | to determine the dates of messages during filtering. | |
118 | Example shell command usage: | |
119 | ||
120 | mesg_date=`cat message | extract-mesg-date` | |
121 | ||
122 | Example 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 | ||
136 | Specifies the message header fields to examine in determining the | |
137 | date of the message. Field names are separated by a colon. | |
138 | For example, | |
139 | ||
140 | -datefields x-archive-date:received:date | |
141 | ||
142 | tells that C<X-Archive-Date>, C<Received>, and C<Date> should | |
143 | be examined. | |
144 | ||
145 | =item C<-debug> | |
146 | ||
147 | Print out debugging information. | |
148 | ||
149 | B<WARNING: DO NOT USE THIS OPTION WITHIN PROCMAILRC RULES.> | |
150 | ||
151 | =item C<-fmt> I<time-fmt-string> | |
152 | ||
153 | The time format to use. The format string syntax is the same as | |
154 | defined by C<strftime>. | |
155 | ||
156 | If C<-fmt> is not specified, than "C<%Y-%m>" is used. | |
157 | ||
158 | =item C<-help> | |
159 | ||
160 | Print out help message. | |
161 | ||
162 | =item C<-man> | |
163 | ||
164 | Print out the manpage. | |
165 | ||
166 | =back | |
167 | ||
168 | =head1 DEPENDENCIES | |
169 | ||
170 | This program uses functions within MHonArc's library. Therefore, | |
171 | MHonArc must be installed on your system and the MHonArc libraries | |
172 | located 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 | ||
180 | Earl Hood, earl@earlhood.com | |
181 | ||
182 | This program is part of the mharc archiving system and comes with | |
183 | ABSOLUTELY NO WARRANTY and may be copied only under the terms of | |
184 | the GNU General Public License, which may be found in the mharc | |
185 | distribution. | |
186 | ||
187 | =cut | |
188 |