procmail
[Top] [All Lists]

Re: Extract Year and Month from email

2007-08-23 10:14:54
On 23-Aug-2007, at 10:54, rouble wrote:
Hi All,

Is there a way to write a procmail recipe to achieve the following:

:0:
<Some Condition>
.Mail.{Year from Date header of this mail}.{Month from Date header  
of this mail}

Yes, it is possible, though I found using the Received header from my  
own mailserver to be far more reliable in extracting the correct date/ 
time stamp for a message. First off, the format is 100% predictable  
since it's YOUR mailserver, and second, you know it's the right dates  
tamp.

This does a lot more than you probably need, and was written as a  
group effort by the members of this list.  I take no real credit for  
it as I mostly whined and prodded to get it done.

# .procmail_datefix
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, fix the From_ header
: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"


   # This is a very clever bit, I think
     
MONTHSTRING=Jan01Feb02Mar03Apr04May05Jun06Jul07Aug08Sep09Oct10Nov11Dec12

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

    # yyyy-mm format
    MYDATE=$MYYEAR-$MYMONTH

    # If the message is not from the current month and year, mark it  
read
    :0fw
    * 9876543210^0 ! $MYYEAR ?? $CYEAR
    * 9876543210^0 ! $MYMONTH ?? $CMONTH
    | formail -I"Status: RO"

}

-- 
Women and cats will do as they please, and men and dogs should relax  
and get used to the idea.


____________________________________________________________
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

<Prev in Thread] Current Thread [Next in Thread>