fix_date.sh 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. # Received: field if this field resides in one line. Otherwise, it will
  23. # take the file modification time (using a RFC 2822-compliant form).
  24. # If no X-Original-Date already exist, the former Date value will be set
  25. # in such field.
  26. VERSION="0.0.5"
  27. version()
  28. {
  29. echo "$VERSION"
  30. exit 0
  31. }
  32. usage()
  33. {
  34. echo "usage:"
  35. echo " ${0##*/} [<switches>] <filename> [<filename> ..]"
  36. echo "switches:"
  37. echo " --help display this help then exit"
  38. echo " --version display version information then exit"
  39. echo " --force always force (re-)writing of Date: header"
  40. echo " --rfc force re-writing of Date: header when it's not RFC-compliant"
  41. echo " --debug turn on debug information (be more verbose)"
  42. echo " --strict use RFC-strict matching patterns for dates"
  43. echo " -- end of switches (in case a filename starts with a -)"
  44. exit $1
  45. }
  46. date_valid()
  47. {
  48. test $STRICT -eq 1 && \
  49. REGEXP="$DATE_REGEXP_STRICT" || \
  50. REGEXP="$DATE_REGEXP"
  51. echo "$1" | grep -qEim 1 "$REGEXP"
  52. DATE_VALID=$?
  53. }
  54. # use --force to always (re-)write the Date header
  55. # otherwise, the Date header will be written if only it doesn't exist
  56. FORCE=0
  57. # use --rfc to (re-)write the Date header when it's not RFC-compliant
  58. # otherwise, the Date header will be written if only it doesn't exist
  59. RFC=0
  60. # use --debug to display more information about what's performed
  61. DEBUG=0
  62. # use --strict to use strict matching patterns for date validation
  63. STRICT=0
  64. # 0 = valid, always valid until --strict is used, then date_valid overrides this value
  65. DATE_VALID=0
  66. while [ -n "$1" ]
  67. do
  68. case "$1" in
  69. --help) usage 0;;
  70. --version) version;;
  71. --force) FORCE=1;;
  72. --debug) DEBUG=1;;
  73. --rfc) RFC=1;;
  74. --strict) STRICT=1;;
  75. --) shift
  76. break;;
  77. -*) echo "error: unrecognized switch '$1'"
  78. usage 1;;
  79. *) break;;
  80. esac
  81. shift
  82. done
  83. if [ $FORCE -eq 1 -a $RFC -eq 1 ]
  84. then
  85. echo "error: use either --force or --rfc, but not both at the same time"
  86. usage 1
  87. fi
  88. test $# -lt 1 && \
  89. usage 1
  90. TMP="/tmp/${0##*/}.tmp"
  91. 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]+"
  92. 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]+"
  93. while [ -n "$1" ]
  94. do
  95. # skip if file is empty or doesn't exist
  96. if [ ! -s "$1" ]
  97. then
  98. shift
  99. continue
  100. fi
  101. X_ORIGINAL_DATE=$(grep -Eim 1 '^X-Original-Date: ' "$1" | cut -d ':' -f 2-)
  102. DATE=$(grep -Eim 1 '^Date: ' "$1" | cut -d ':' -f 2-)
  103. test $STRICT -eq 1 && \
  104. RECEIVED_DATE=$(grep -Eim 1 ";$DATE_REGEXP" "$1" | cut -d ';' -f 2) || \
  105. RECEIVED_DATE=$(grep -Eim 1 "; $DATE_REGEXP_STRICT" "$1" | cut -d ';' -f 2)
  106. FILE_DATE=$(ls -l --time-style="+%a, %d %b %Y %X %z" "$1" | tr -s ' ' ' ' | cut -d ' ' -f 6-11)
  107. # we could also use the system date as a possible replacement
  108. #SYSTEM_DATE="$(date -R)"
  109. # determine which replacement date to use
  110. if [ -z "$RECEIVED_DATE" ]
  111. then
  112. # don't forget the leading whitespace here
  113. REPLACEMENT_DATE=" $FILE_DATE"
  114. REPLACEMENT="file date"
  115. # REPLACEMENT_DATE=" $SYSTEM_DATE"
  116. # REPLACEMENT="system date"
  117. else
  118. REPLACEMENT_DATE="$RECEIVED_DATE"
  119. REPLACEMENT="received date"
  120. fi
  121. # ensure that a X-Original-Date is set (but don't override it)
  122. if [ -z "$X_ORIGINAL_DATE" ]
  123. then
  124. if [ -z "$DATE" ]
  125. then
  126. echo "X-Original-Date:$REPLACEMENT_DATE" > "$TMP"
  127. else
  128. test $FORCE -eq 1 && \
  129. echo "X-Original-Date:$DATE" > "$TMP"
  130. fi
  131. else
  132. :> "$TMP"
  133. fi
  134. # replace/set the date and write all lines
  135. test $RFC -eq 1 && \
  136. date_valid "$DATE"
  137. if [ -z "$DATE" ]
  138. then
  139. test $DEBUG -eq 1 && \
  140. echo "$1: date not found, using $REPLACEMENT now"
  141. echo "Date:$REPLACEMENT_DATE" >> "$TMP"
  142. cat "$1" >> "$TMP"
  143. else
  144. if [ $FORCE -eq 1 ]
  145. then
  146. test $DEBUG -eq 1 && \
  147. echo "$1: date already found, replacing with $REPLACEMENT"
  148. sed "s/^Date: .*/Date:$REPLACEMENT_DATE/" "$1" >> "$TMP"
  149. else
  150. if [ $RFC -eq 1 ]
  151. then
  152. if [ $DATE_VALID -ne 0 ]
  153. then
  154. test $DEBUG -eq 1 && \
  155. echo "$1: date already found but not RFC-compliant, replacing with $REPLACEMENT"
  156. sed "s/^Date: .*/Date:$REPLACEMENT_DATE/" "$1" >> "$TMP"
  157. else
  158. test $DEBUG -eq 1 && \
  159. echo "$1: date already found and RFC-compliant, skipping"
  160. cat "$1" >> "$TMP"
  161. fi
  162. else
  163. test $DEBUG -eq 1 && \
  164. echo "$1: date already found, skipping"
  165. cat "$1" >> "$TMP"
  166. fi
  167. fi
  168. fi
  169. # uncomment the following line to backup the original file
  170. #mv -f "$1" "$1.bak"
  171. mv -f "$TMP" "$1"
  172. if [ $? -ne 0 ]
  173. then
  174. echo "error while moving '$TMP' to '$1'"
  175. exit 1
  176. fi
  177. shift
  178. done
  179. exit 0