fix_date.sh 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. #!/bin/sh
  2. # * Copyright 2004 Tristan Chabredier <wwp@claws-mail.org>
  3. # *
  4. # * This file is free software; you can redistribute it and/or modify it
  5. # * under the terms of the GNU General Public License as published by
  6. # * the Free Software Foundation; either version 3 of the License, or
  7. # * (at your option) any later version.
  8. # *
  9. # * This program is distributed in the hope that it will be useful, but
  10. # * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. # * General Public License for more details.
  13. # *
  14. # * You should have received a copy of the GNU General Public License
  15. # * along with this program; if not, write to the Free Software
  16. # * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. #
  18. # fix_date.sh helper script to fix non-standard date or add missing
  19. # date header to emails
  20. # usage: fix_date.sh <filename> [<filename>..]
  21. # It will replace the Date: value w/ the one picked up from more recent
  22. # Fetchinfo time header, Received: field.. Otherwise, it will take the file
  23. # modification time (using a RFC 2822-compliant form).
  24. # Any already existing X-Original-Date is kept, if missing we're adding it
  25. # if the Date: was set (even if set w/ non conform value)
  26. # TODO: fallback to X-OriginalArrivalTime: ?
  27. VERSION="0.1.2"
  28. version()
  29. {
  30. echo "$VERSION"
  31. exit 0
  32. }
  33. usage()
  34. {
  35. echo "usage:"
  36. echo " ${0##*/} [<switches>] <filename> [<filename> ..]"
  37. echo "switches:"
  38. echo " --help display this help then exit"
  39. echo " --version display version information then exit"
  40. echo " --force always force (re-)writing of Date: header"
  41. echo " --rfc force re-writing of Date: header when it's not RFC-compliant"
  42. echo " --debug turn on debug information (be more verbose)"
  43. echo " --strict use RFC-strict matching patterns for dates"
  44. echo " -- end of switches (in case a filename starts with a -)"
  45. exit $1
  46. }
  47. date_valid()
  48. {
  49. test $STRICT -eq 1 && \
  50. REGEXP="$DATE_REGEXP_STRICT" || \
  51. REGEXP="$DATE_REGEXP"
  52. echo "$1" | grep -qEim 1 "$REGEXP"
  53. DATE_VALID=$?
  54. }
  55. dump_date_fields()
  56. {
  57. test -z "$X_ORIGINAL_DATE" -a -n "$DATE" && \
  58. echo "X-Original-Date:$DATE" >> "$TMP"
  59. echo "Date:$REPLACEMENT_DATE" >> "$TMP"
  60. }
  61. # use --force to always (re-)write the Date header
  62. # otherwise, the Date header will be written if only it doesn't exist
  63. FORCE=0
  64. # use --rfc to (re-)write the Date header when it's not RFC-compliant
  65. # otherwise, the Date header will be written if only it doesn't exist
  66. RFC=0
  67. # use --debug to display more information about what's performed
  68. DEBUG=0
  69. # use --strict to use strict matching patterns for date validation
  70. STRICT=0
  71. # 0 = valid, always valid until --strict is used, then date_valid overrides this value
  72. DATE_VALID=0
  73. while [ -n "$1" ]
  74. do
  75. case "$1" in
  76. --help) usage 0;;
  77. --version) version;;
  78. --force) FORCE=1;;
  79. --debug) DEBUG=1;;
  80. --rfc) RFC=1;;
  81. --strict) STRICT=1;;
  82. --) shift
  83. break;;
  84. -*) echo "error: unrecognized switch '$1'"
  85. usage 1;;
  86. *) break;;
  87. esac
  88. shift
  89. done
  90. if [ $FORCE -eq 1 -a $RFC -eq 1 ]
  91. then
  92. echo "error: use either --force or --rfc, but not both at the same time"
  93. usage 1
  94. fi
  95. test $# -lt 1 && \
  96. usage 1
  97. TMP="/tmp/${0##*/}.tmp"
  98. HEADERS="/tmp/${0##*/}.headers.tmp"
  99. BODY="/tmp/${0##*/}.body.tmp"
  100. DATE_REGEXP='( (Mon|Tue|Wed|Thu|Fri|Sat|Sun),)? [0-9]+ (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) [0-9]+ [0-9]+:[0-9]+:[0-9]+ [-+]?[0-9]+'
  101. DATE_REGEXP_STRICT='(Mon|Tue|Wed|Thu|Fri|Sat|Sun), [0-9]+ (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) [0-9]+ [0-9]+:[0-9]+:[0-9]+ [-+]?[0-9]+'
  102. while [ -n "$1" ]
  103. do
  104. # skip if file is empty or doesn't exist
  105. if [ ! -s "$1" ]
  106. then
  107. shift
  108. continue
  109. fi
  110. SKIP=0
  111. # split headers and body
  112. # get the empty line (separation between headers and body)
  113. SEP=`grep -nEm1 "^$" "$1" 2>/dev/null | cut -d ':' -f 1`
  114. if [ -z "$SEP" -o "$SEP" = "0" ]
  115. then
  116. cp -f "$1" "$HEADERS"
  117. :> "$BODY"
  118. else
  119. sed -n '1,'`expr $SEP - 1`'p' "$1" > "$HEADERS"
  120. sed '1,'`expr $SEP - 1`'d' "$1" > "$BODY"
  121. fi
  122. # work on headers only
  123. # get the Date and X-Original-Date
  124. X_ORIGINAL_DATE=`sed -n '/^X-Original-Date:/,/^[^\t]/p' "$HEADERS" | head -n -1 | cut -d ':' -f 2-`
  125. DATE=`sed -n '/^Date:/,/^[^\t]/p' "$HEADERS" | head -n -1 | cut -d ':' -f 2-`
  126. # work on headers, minus Date and X-Original-Date
  127. test -n "$X_ORIGINAL_DATE" && \
  128. sed -i '/^X-Original-Date:/,/^[^\t]/d' "$HEADERS"
  129. test -n "$DATE" && \
  130. sed -i '/^Date:/,/^[^\t]/d' "$HEADERS"
  131. # found a replacement date in Fetchinfo headers
  132. FETCH_DATE=`grep -im1 'X-FETCH-TIME: ' "$HEADERS" | cut -d ' ' -f 2-`
  133. # or in Received: headers ..
  134. test $STRICT -eq 1 && \
  135. REGEXP="$DATE_REGEXP" || \
  136. REGEXP="$DATE_REGEXP_STRICT"
  137. RECEIVED_DATE=`sed -n '/^Received:/,/^[^\t]/p' "$HEADERS" | head -n -1 | grep -Eoim 1 "$REGEXP"`
  138. # .. or from FS
  139. FILE_DATE=`LC_ALL=POSIX LANG=POSIX ls -l --time-style="+%a, %d %b %Y %X %z" "$1" | tr -s ' ' | cut -d ' ' -f 6-11`
  140. # we could also use the system date as a possible replacement
  141. SYSTEM_DATE="`date -R`"
  142. # determine which replacement date to use
  143. if [ -z "$FETCH_DATE" ]
  144. then
  145. if [ -z "$RECEIVED_DATE" ]
  146. then
  147. # don't forget the leading whitespace here
  148. REPLACEMENT_DATE=" $FILE_DATE"
  149. REPLACEMENT="file date"
  150. # REPLACEMENT_DATE=" $SYSTEM_DATE"
  151. # REPLACEMENT="system date"
  152. else
  153. REPLACEMENT_DATE="$RECEIVED_DATE"
  154. REPLACEMENT="received date"
  155. fi
  156. else
  157. # don't forget the leading whitespace here
  158. REPLACEMENT_DATE=" $FETCH_DATE"
  159. REPLACEMENT="Fetchinfo time header"
  160. fi
  161. # ensure that the original X-Original-Date is kept
  162. :> "$TMP"
  163. if [ -n "$X_ORIGINAL_DATE" ]
  164. then
  165. echo "X-Original-Date:$X_ORIGINAL_DATE" >> "$TMP"
  166. fi
  167. # replace/set the date and write all lines
  168. test $RFC -eq 1 && \
  169. date_valid "$DATE"
  170. if [ -z "$DATE" ]
  171. then
  172. test $DEBUG -eq 1 && \
  173. echo "$1: date not found, using $REPLACEMENT now"
  174. dump_date_fields
  175. else
  176. if [ $FORCE -eq 1 ]
  177. then
  178. test $DEBUG -eq 1 && \
  179. echo "$1: date already found, replacing with $REPLACEMENT"
  180. dump_date_fields
  181. else
  182. if [ $RFC -eq 1 ]
  183. then
  184. if [ $DATE_VALID -ne 0 ]
  185. then
  186. test $DEBUG -eq 1 && \
  187. echo "$1: date already found but not RFC-compliant, replacing with $REPLACEMENT"
  188. dump_date_fields
  189. else
  190. test $DEBUG -eq 1 && \
  191. echo "$1: date already found and RFC-compliant, skipping"
  192. SKIP=1
  193. fi
  194. else
  195. test $DEBUG -eq 1 && \
  196. echo "$1: date already found, skipping"
  197. SKIP=1
  198. fi
  199. fi
  200. fi
  201. if [ $SKIP -eq 0 ]
  202. then
  203. # uncomment the following line to backup the original file
  204. #mv -f "$1" "$1.bak"
  205. cat "$HEADERS" >> "$TMP"
  206. cat "$BODY" >> "$TMP"
  207. mv -f "$TMP" "$1"
  208. if [ $? -ne 0 ]
  209. then
  210. echo "error while moving '$TMP' to '$1'"
  211. exit 1
  212. fi
  213. fi
  214. rm -f "$HEADERS" "$BODY" "$TMP" >/dev/null 2>&1
  215. shift
  216. done
  217. exit 0