Adding X-Backup-File header with name of file procmail backed up message to
am 25.10.2006 03:24:53 von vmontressor
I'm using procmail and have the standard
:0 c
backup
recipe at the top of my .procmailrc.
I'd like to add an X-Backup-File header to each received email message,
saying what backup file the email message was saved to by that recipe.
I have in mind something like this:
:0 f
| formail -I "X-Backup-File: $MAILDIR/backup/$MSGPREFIX(something)"
I just don't know what "(something)" ought to be. I didn't see
anything promising in the list of envars in "man procmailrc."
It seems like 99.9% of the time, it would suffice to replace the backup
recipe with this:
:0 f
{
:0 c
backup
:0 f
| formail -I "X-Backup-File: $MAILDIR/backup/`/bin/ls -1t
$MAILDIR/backup/ | head -1`"
}
.... but not if something else managed to write a file into
$MAILDIR/backup in between the two actions.
Any better ideas? Pointers to overlooked documentation would be
fantastic, thanks!
Re: Adding X-Backup-File header with name of file procmail backedup message to
am 30.10.2006 22:48:44 von Garen Erdoisa
vmontressor@yahoo.com wrote:
> I'm using procmail and have the standard
>
> :0 c
> backup
>
> recipe at the top of my .procmailrc.
>
> I'd like to add an X-Backup-File header to each received email message,
> saying what backup file the email message was saved to by that recipe.
>
> I have in mind something like this:
>
> :0 f
> | formail -I "X-Backup-File: $MAILDIR/backup/$MSGPREFIX(something)"
>
> I just don't know what "(something)" ought to be. I didn't see
> anything promising in the list of envars in "man procmailrc."
>
> It seems like 99.9% of the time, it would suffice to replace the backup
> recipe with this:
>
> :0 f
No need to use the filter flag on a nested recipe block. It's not a
"filter" in this case, it's just the start of a procmail nesting block.
> {
> :0 c
> backup
>
> :0 f
> | formail -I "X-Backup-File: $MAILDIR/backup/`/bin/ls -1t
> $MAILDIR/backup/ | head -1`"
It would be a better strategy to create the string you wish to use for
the filename into a variable, then use that variable to stamp the header
and for the backup filename instead of letting procmail decide on the
name of the file to use, then trying to have procmail try to "fetch"
that file name just created to use to stamp the header. Example below.
> }
>
> ... but not if something else managed to write a file into
> $MAILDIR/backup in between the two actions.
Avoiding two procmail processes from writing to the file at the same
time is accomplished by using a procmail lock file when you have
procmail write to the file. Example below.
>
> Any better ideas? Pointers to overlooked documentation would be
> fantastic, thanks!
>
-=-=-procmail example-=-=-
# End of line char for use in Procmail LOG lines.
NS="
"
# Capture the current process ID
PID="$$"
# Capture a random number for use in filenames to avoid any temp
# race conditions
RANDOM=`echo $RANDOM`
# Capture a date stamp into a variable done in terms of the number of
# seconds since the epoc. This will create a very unique number
DATESTAMP=` date +%s `
# Use any combination of PID and RANDOM and/or DATESTAMP as part
# of your backup filename
BACKUPFILENAME="${MAILDIR}/backup/msg.${PID}.${RANDOM}"
:0
{
# Since this sort of thing is usually done as a cache, you
# you probabally will not want to save more than the last
# 500 or so messages.
:0c:
${BACKUPFILENAME}
# keep only the last 500 messages in the backup cache.
:0 ic
| cd ${MAILDIR}/backup && rm -f dummy `ls -t msg.* | sed -e 1,500d`
# Note the action in the procmail log file.
# This may be useful trace information for when things go wrong.
LOG="${$$}$_:backup filename: ${BACKUPFILENAME}${NS}"
# stamp the new header line onto the email in the procmail
# pipeline.
:0 f
| formail -I "X-Backup-File: ${BACKUPFILENAME} "
}
Hope this helps.
--
Garen