procmail
[Top] [All Lists]

Re: adding a missing Date: header

2008-10-03 20:39:43
On 3-Oct-2008, at 17:27, Dallman Ross wrote:
We could stop there.  But note that each message coming in has a
timestamp in the From_ header.  Let's use it.


I almost posted datefix, but thought I'd just answer the asked question for once instead of going into a lot of detail.

This is the .datefix that I run on my incoming mail.. Or, well, used to at least. This is kind of backwards, as it uses the Received headers to rebuild the From_ but that's a mere detail.

$ cat datefix_procmail
WEEKDAYS = '(S(un|at)|Mon|T(ue|hu)|Wed|Fri)'
MONTHS = '(J(an|u[ln])|Feb|Ma[ry]|A(pr|ug)|Sep|Oct|Nov|Dec)'
WHICHRECVD = 'by [^     ]*(mail.)?covisp.net'
YEARS = '(199[0-9]|20[0-9][0-9])'
TIMESTAMP = '([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]'
# dates between 1990-2099 ok. Adjust following as needed.
# NB: DATE is only trusted from the WHICHRECVD server.

# proper Received header, should work 100% of the time.
RCVD_STAMP = "$WEEKDAYS, [0-9]+ $MONTHS $YEARS $TIMESTAMP"

# improper Received header, handles "Tue, 01" and "Tue,  1"
# ONLY use this version if your mailspool includes messages
# with improper headers on the WHICHRECVD line
#RCVD_STAMP = "$WEEKDAYS, +[0-9]+ $MONTHS $YEARS $TIMESTAMP"

# Get the received header I want the date from
# (when the message hit my mailsserver)
:0
* $ ^Received:.*$WHICHRECVD.*\/$RCVD_STAMP
{ xDATE = "$MATCH" }

# short-circuit w/o data
:0fw
* xDATE ?? ^^^^
| formail -i"X-Date-Munge: FAILED"

# If we didn't bail out early...
:0 E
{
   # Build the new "From " header by extracting the date in the
   # right order from xDate

   # From: Sun, 02 Mar 2003 08:29:12
   # To:   Sun Mar 2 08:29:12 2003
   :0
   * $ xDATE ?? ^^\/$WEEKDAYS
   { ENVELOPE = "<$CLEANFROM>  $MATCH" }
   :0
   * $ xDATE ?? ^^$WEEKDAYS, +[0-9]+ \/$MONTHS
   { ENVELOPE = "$ENVELOPE $MATCH"
     MYMONTH=$MATCH
   }
   :0
   * $ xDATE ?? ^^$WEEKDAYS, +\/[0-9]+
#   { ENVELOPE = "$ENVELOPE $MATCH" }
# If you are using the broken RCVD_STAMP Comment out
# the previous line and uncomment this block
   {
         PADD = "0"$MATCH
         :0
         * PADD ?? ...
         {
            :0
            * PADD ?? ^^.\/..
            { PADD = $MATCH }
         }
      ENVELOPE = "$ENVELOPE $PADD"
   }

   :0
   * $ xDATE ?? ()\/$TIMESTAMP
   { ENVELOPE = "$ENVELOPE $MATCH" }

   :0
   * $ xDATE ?? $MONTHS \/$YEARS
   { ENVELOPE = "$ENVELOPE $MATCH"
     MYYEAR=$MATCH
   }

   # Make sure the $ENVELOPE matches the desired format
   # If it does, rewrite the From_
   :0 fhw
   * $ ENVELOPE ?? $WEEKDAYS $MONTHS [0-9]+ $TIMESTAMP $YEARS^^
   | sed "s,^\(From \).*,\1$ENVELOPE,"
       # And add a header showing we've altered the message
       :0 afw
       | formail -i"X-Date-Munge: SUCCESS"

       # Otherwise, show that the attempt failed.
       :0 efw
       | formail -i"X-Date-Munge: FAILED"


MONTHSTRING=Jan01Feb02Mar03Apr04May05Jun06Jul07Aug08Sep09Oct10Nov11Dec12

   :0
   * $ MONTHSTRING ?? $MYMONTH\/..
   { MYMONTH=$MATCH }

   MYDATE=$MYYEAR-$MYMONTH

}



--
"Katrina, $4 gas, a trillion dollar war, rising unemployment,
     deregulated housing market, global warming...NO MORE!"
     http://is.gd/2mxY



____________________________________________________________
procmail mailing list   Procmail homepage: http://www.procmail.org/
procmail(_at_)lists(_dot_)RWTH-Aachen(_dot_)DE
http://MailMan.RWTH-Aachen.DE/mailman/listinfo/procmail