3 # * Copyright 2004 Tristan Chabredier <wwp@claws-mail.org>
5 # * This file is free software; you can redistribute it and/or modify it
6 # * under the terms of the GNU General Public License as published by
7 # * the Free Software Foundation; either version 2 of the License, or
8 # * (at your option) any later version.
10 # * This program is distributed in the hope that it will be useful, but
11 # * WITHOUT ANY WARRANTY; without even the implied warranty of
12 # * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # * General Public License for more details.
15 # * You should have received a copy of the GNU General Public License
16 # * along with this program; if not, write to the Free Software
17 # * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 # fix_date.sh helper script to fix non-standard date or add missing
20 # date header to emails
22 # usage: fix_date.sh <filename> [<filename>..]
23 # It will replace the Date: value w/ the one picked up from more recent
24 # Received: field if this field resides in one line. Otherwise, it will
25 # take the file modification time (using a RFC 2822-compliant form).
26 # If no X-Original-Date already exist, the former Date value will be set
41 echo " ${0##*/} [<switches>] <filename> [<filename> ..]"
43 echo " --help display this help then exit"
44 echo " --version display version information then exit"
45 echo " --force always force (re-)writing of Date: header"
46 echo " --rfc force re-writing of Date: header when it's not RFC-compliant"
47 echo " --debug turn on debug information (be more verbose)"
48 echo " --strict use RFC-strict matching patterns for dates"
49 echo " -- end of switches (in case a filename starts with a -)"
55 test $STRICT -eq 1 && \
56 REGEXP
="$DATE_REGEXP_STRICT" || \
59 echo "$1" |
grep -qEim 1 "$REGEXP"
63 # use --force to always (re-)write the Date header
64 # otherwise, the Date header will be written if only it doesn't exist
66 # use --rfc to (re-)write the Date header when it's not RFC-compliant
67 # otherwise, the Date header will be written if only it doesn't exist
69 # use --debug to display more information about what's performed
71 # use --strict to use strict matching patterns for date validation
73 # 0 = valid, always valid until --strict is used, then date_valid overrides this value
87 -*) echo "error: unrecognized switch '$1'"
94 if [ $FORCE -eq 1 -a $RFC -eq 1 ]
96 echo "error: use either --force or --rfc, but not both at the same time"
103 TMP
="/tmp/${0##*/}.tmp"
105 DATE_REGEXP
="( (Mon|Tue|Wed|Thu|Fri|Sat|Sun),)? [0-9]+ (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dev) [0-9]+ [0-9]+:[0-9]+:[0-9}+ [-+][0-9]+"
106 DATE_REGEXP_STRICT
="(Mon|Tue|Wed|Thu|Fri|Sat|Sun), [0-9]+ (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dev) [0-9]+ [0-9]+:[0-9]+:[0-9}+ [-+][0-9]+"
110 # skip if file is empty or doesn't exist
117 X_ORIGINAL_DATE
=$
(grep -Eim 1 '^X-Original-Date: ' "$1" | cut
-d ':' -f 2-)
118 DATE
=$
(grep -Eim 1 '^Date: ' "$1" | cut
-d ':' -f 2-)
119 test $STRICT -eq 1 && \
120 RECEIVED_DATE
=$
(grep -Eim 1 ";$DATE_REGEXP" "$1" | cut
-d ';' -f 2) || \
121 RECEIVED_DATE
=$
(grep -Eim 1 "; $DATE_REGEXP_STRICT" "$1" | cut
-d ';' -f 2)
122 FILE_DATE
=$
(ls -l --time-style="+%a, %d %b %Y %X %z" "$1" |
tr -s ' ' ' ' | cut
-d ' ' -f 6-11)
123 # we could also use the system date as a possible replacement
124 #SYSTEM_DATE="$(date -R)"
126 # determine which replacement date to use
127 if [ -z "$RECEIVED_DATE" ]
129 # don't forget the leading whitespace here
130 REPLACEMENT_DATE
=" $FILE_DATE"
131 REPLACEMENT
="file date"
132 # REPLACEMENT_DATE=" $SYSTEM_DATE"
133 # REPLACEMENT="system date"
135 REPLACEMENT_DATE
="$RECEIVED_DATE"
136 REPLACEMENT
="received date"
139 # ensure that a X-Original-Date is set (but don't override it)
140 if [ -z "$X_ORIGINAL_DATE" ]
144 echo "X-Original-Date:$REPLACEMENT_DATE" > "$TMP"
146 test $FORCE -eq 1 && \
147 echo "X-Original-Date:$DATE" > "$TMP"
153 # replace/set the date and write all lines
158 test $DEBUG -eq 1 && \
159 echo "$1: date not found, using $REPLACEMENT now"
160 echo "Date:$REPLACEMENT_DATE" >> "$TMP"
165 test $DEBUG -eq 1 && \
166 echo "$1: date already found, replacing with $REPLACEMENT"
167 sed "s/^Date: .*/Date:$REPLACEMENT_DATE/" "$1" >> "$TMP"
171 if [ $DATE_VALID -ne 0 ]
173 test $DEBUG -eq 1 && \
174 echo "$1: date already found but not RFC-compliant, replacing with $REPLACEMENT"
175 sed "s/^Date: .*/Date:$REPLACEMENT_DATE/" "$1" >> "$TMP"
177 test $DEBUG -eq 1 && \
178 echo "$1: date already found and RFC-compliant, skipping"
182 test $DEBUG -eq 1 && \
183 echo "$1: date already found, skipping"
189 # uncomment the following line to backup the original file
195 echo "error while moving '$TMP' to '$1'"