LINUXOR.SK ... open source notes ...

2007 - Notes on MDAs (procmail and maildrop)

category: howtoz · date: 2007-11-30 · source: old.linuxor.sk/MAIL/POSTFIX-HOWTOs/BH_Procmail_Maildrop.txt

The Slovak original of this document: 2007 - Poznámky k MDA (procmail a maildrop) (slovensky).

asciiart
|=---------------------------=[ Procmail/Maildrop ]=-----------------------=|
+---------------------------------------------------------------------------+
|                                                                           |
|                       Poznamky k MDA (procmail a maildrop)                |
|                                                                           |
|                                                                           |
+---------------------------------------------------------------------------+
|=-------------------------------=[ BH 2007 ]=-----------------------------=|

1. Procmail

asciiart
$HOME/procmailrc

1.1 Syntax pravidiel

asciiart
:0 [flagi] [ : [locallockfile] ]
<ziadna alebo viac podmienok (jedna na riadok)>
<presne jedna akcia>

1.2 Flagi

asciiart
H = Egrepne hlavicku (default).

B = Egrepne telo.

D = Set the internal egrep to tell upper case from lower case
    (default je nerozlisovat velkost pismen)

A = Toto pravidlo nebude vykonane pokial podmienka v poslednom nadradenom pravidle
    (at the current nesting level) without the flag "A" or "a" matches. This lets us
    dovoli zretazit akcie, ktore zavisia na spolocnej podmienke.

a = This flag means the same as "A", with the further condition that
    that the rule immediately before it has to have finished successfully
    before this rule runs.

E = This rule fires if the rule immediately before it
    nebolo vykonane. Vykonanie tohoto pravidla tiez vypne dalsie bezprostredne
    nasledujuce pravidla s flagom 'E'. Toto nam dovoli specifikovat akcie typu
    'else if'.

e = This rule fires only if the previous one failed
    (napr. riadok s akciou sa pokusil o vykonanie, ale skoncil s chybou).

h = Sends the header to the pipe, the file or the mail destination (the default).

b = Sends the body to the pipe, the file or the mail destination (the default).

f = Treats the pipe as a filter.

c = Generates a copy of this mail. This is meaningful in delivery rules

w = Waits for the filter or program to finish and checks its exit code
    (normally ignores it); if the filter fails, the text is left unfiltered.

W = Means the same as the flag "w", but hides every error message the program prints.

i = Ignores any write error in this rule
    (for example, when the pipe is closed early).

r = Raw mode; makes no attempt to have the mail end with an empty line,
    writes it as it is.

1.3 Specialne podmienky

asciiart
! = Invertuje podmienku.

$ = Vyhodnoti zvysok tejto podmienky rovnako ako shellovske substitucne pravidla
    vo vnutri uvodzoviek, preskoci biele znaky a potom to reparsuje.

? = Pouzije exitcode specifikovaneho programu.

< = Check whether the total size of the mail is smaller than the given number of bytes (decimal).

> = Obdobne ako '<'

nazovpremennej ?? = Porovna zvysok tejto podmienky voci hodnote tejto premennej prostredia
                    (ktora nemoze byt pseudopremennou). Specialny pripad je, ak meno premennej
                    matches "B", "H", "HB" or "BH"; this overrides the default entirely
                    hladania v header/body oblastiach definovane inicializacnymi flagmi v
                    tomto pravidle.

1.4 Rozsirene regularne vyrazy

asciiart
^       = Zaciatok riadka.
$       = Koniec riadka.
.       = Hociaky znak okrem znaku noveho riadka. (pr: .aco => laco, maco)
a*      = Sekvencia ziadneho alebo viacerych znakov 'a' (,a,aa,aaa). (pr: Lac* Hajzer => La Hajzer, Lac Hajzer, Lacccc Hajzer)
a+      = Sekvencia jedneho alebo viacerych znakov 'a' (a,aa,aaa). (pr: La(co)+ => Laco, Lacococo)
a?      = Ziaden alebo jeden znak 'a' . (pr: Laco? => Lac, Laco)
[^-a-d] = Any character that is not "-", "a", "b", "c", "d" or a newline.
[abcd]  = Znakova trieda, ktora znamena jeden z uvedenych znakov.
de|abc  = Sekvencia 'de' alebo 'abc' .
(abc)*  = Ziadna alebo viac sekvencii 'abc' .
\.      = Jedna bodka. Ak chceme zhodu s hociakym specialnym znakom pouzijeme pred nim znak '\' .

-----------------------------------------------------------------
 Priklad1: presmerovanie vsetkych emailov na inu emailovu adresu
-----------------------------------------------------------------
:0 H:
* ^To:.*mail@domena.sk
! inymail@inadomena.sk

-----------------------------------------------------------------
 Priklad2: presmerovanie vsetkych emailov na inu emailovu adresu + ulozenie
 do lokalneho postoveho priecinka
-----------------------------------------------------------------
:0 Hc:
* ^To:.*mail@domena.sk
! inymail@inadomena.sk

-----------------------------------------------------------------
 Priklad3: presmerovanie emailov podla predmetu na inu emailovu adresu
-----------------------------------------------------------------
:0 H:
* ^Subject: .*UDALOST
! udalost@domena.sk

-----------------------------------------------------------------
 Priklad4: triedenie emailov podla predmetu do priecinkov
-----------------------------------------------------------------
:0 H:
* ^Subject: .*UDALOST
~/udalosti

-----------------------------------------------------------------
 Example 5: an out-of-office auto-reply, which the user sets up for themselves by sending an e-mail.
-----------------------------------------------------------------
# touch /home/user/.procmailrc
# chown user:user /home/user/.procmailrc
# chmod 700 /home/user/.procmailrc

Editujeme subor /home/user/.procmailrc
--------------------------------------
$VACATION_PASSWORD - heslo pre zapnutie/vypnutie automatickej odpovede
$VACATION_DOMAIN_NAME - the e-mail address we are setting the auto-reply for (user@domena.sk)

if there is trouble, comment these lines out in /home/user/.procmailrc
----------------------------------------------------------------------
#LOGFILE=/tmp/procmailvacation.log
#VERBOSE=on

Zapnutie automatickej odpovede
------------------------------
Uzivatel posle mail zo subjektom: password vacation on (password je heslo definovane v .procmailrc)
Telo mailu sa stane textom automatickej odpovede.

Vypnutie automatickej odpovede
------------------------------
Uzivatel posle mail zo subjektom: password vacation off (password je heslo definovane v .procmailrc)

Z http://www.clarkconnect.com/wiki/index.php?title=Howtos_-_Procmail_Vacation_Auto-Reply_Recipe

------------------------------------ .procmailrc --------------------------------
# vim: ft=procmail

# User-managed vacation recipe for procmail
# Written by Jason Thaxter
#  (http://www.google.com/search?q=jason+thaxter)

# To use from a shell:
#
#   * Include this file in your procmail recipe.
#   * Define $VACATION_SENDER in your procmail recipe: it will be "from" this
#     address.
#   * Put your message text in a file specified by $VACATION_MSG.
#     (defaults to ".vacation_mesg")
#   * Remove the $VACATION_MSG file to turn off vacation messages.

# For users without shell access:
#
#   * Include this file in the procmail file.
#   * Set $VACATION_PASSWORD. (for security, this is mandatory)
#   * Define $VACATION_SENDER in your procmail recipe: it will be "from" this
#     address.
#   * E-mail a message with $VACATION_PASSWORD and $VACATION_ON in the subject
#     line. The body of the message becomes the vacation message. $VACATION_ON
#     can be set prior to the INCLUDERC, but it defaults to "vacation on".
#   * To turn it off, e-mail a message with $VACATION_PASSWORD and $VACATION_OFF
#     in the subject line. Likewise, $VACATION_OFF defaults to "vacation off".

# Note that you probably want this to execute *after* any mailing list or spam
# delivery recipes. You can set $VACATION_SKIP to disable vacation processing
# if it's inconvenient to skip this recipe.

# TODO:
#   * configurable regex so we can reply "from" whatever they sent it "to" (if
#     we can match on the regex, of course). kinda like mutt's "alternates".
#   * optionally, send a vacation message for EVERY incoming message from a
#     given email, not just the first one.
#   * Duh: better documentation.
#   * Nicer defaults for people who let mailing-list mail and spam hit
#     this recipe (though we will *always* try to avoid replying to those).

# -----------------------------------------------------------------------------
# Configurable variables: These variables allow you to use this vacation recipe
# as an include and customize it from your main procmail file.
#
# lockfile:
VACATION_LOCK=${VACATION_LOCK:-".vacation$LOCKEXT"}
# cache file:
VACATION_CACHE=${VACATION_CACHE:-".vacation_cache"}
# cache size:
VACATION_CACHE_SZ=${VACATION_CACHE_SZ:-8192}
# message file
VACATION_MSG=${VACATION_MSG:-".vacation_mesg"}
# what to use as the xloop header
HOSTNAME=${HOSTNAME:-`hostname`}
VACATION_XLOOP=${VACATION_XLOOP:-"$LOGNAME@$HOSTNAME"}
# base token for default $VACATION_ON and $VACATION_OFF
# so you could set this and not those individually
VACATION_COOKIE=${VACATION_COOKIE:-"vacation"}
VACATION_ON=${VACATION_ON:-"$VACATION_COOKIE on"}
VACATION_OFF=${VACATION_OFF:-"$VACATION_COOKIE off"}

# UNSTABLE:
# get the sender for later use
VACATION_MSG_SEND_TO=`formail -rtx To: | expand | sed -e 's/^[ ]*//g' -e 's/[ ]*$//g'`
# not sure what to do for weird cases of these... yet
VACATION_SENDMAILFROM=${VACATION_SENDMAILFROM:-"-f$VACATION_SENDER"}
VACATION_SENDMAILFLAGS="-oi -t $VACATION_SENDMAILFROM"
# who are we? needs to be extracted with a regex using ALTERNATES
#VACATION_RECIPIENT=`formail -XTo: \ | expand | sed -e 's/^[ ]*//g' -e 's/[ ]*$//g'`
# -----------------------------------------------------------------------------

SENDMAIL_CMD="$SENDMAIL $VACATION_SENDMAILFLAGS"
SHELL=/bin/sh

# check if we should send vacation message, add user to cache
:0 Whc: $VACATION_LOCK
# if i haven't been instructed to skip processing
* ? test -z $VACATION_SKIP
# if i have a vacation message file
* ? test -f $VACATION_MSG
# and the message is not from a daemon or mailer
* !^FROM_DAEMON
* !^FROM_MAILER
# not declared spam by spamassassin
* !^X-Spam-Flag: YES
# not discernably in a mailing list
* !^List-
* !^(Mailing-List|Approved-By|BestServHost|Resent-(Message-ID|Sender)):
* !^X-[^:]*-List:
* !^X-(Sent-To|(Listprocessor|Mailman)-Version):
# and not x-loop
* !^X-Loop: $VACATION_XLOOP
# add it to the cache
| formail -rD $VACATION_CACHE_SZ $VACATION_CACHE

:0 ehc
# if the name was not in the cache
# if we can find who we're sending it to
# and who we are sending this "From"
* ? test -n ${VACATION_MSG_SEND_TO}
* ? test -n ${VACATION_SENDER}
| (formail -r \
        -I"Precedence: junk" \
        -A"From: $VACATION_SENDER" \
        -A"X-Loop: $VACATION_XLOOP"; \
        cat  $VACATION_MSG ) | \
        $SENDMAIL_CMD

# Add/remove vacation message
:0
# only do this if we have a password set
* ? test -n $VACATION_PASSWORD
# and it's in the subject line
* $^Subject:.*${VACATION_PASSWORD}
{

        # VACATION ON
        # if subject line matches magic cookie for ON:
        :0
        * $^Subject:.*${VACATION_ON}
        {
                # pipe the body into the vacation message file
                :0c:$VACATION_LOCK
                | formail -I "" > $VACATION_MSG

                # add message to the body
                :0f
                | cat - ; \
                  echo; \
                  echo '---------- VACATION -----------------'; \
                  echo 'The above text was installed as your vacation message'
        }

        # VACATION OFF
        # if subject line matches magic cookie for OFF:
        # delete the vacation file and notify
        :0f
        * $^Subject:.*${VACATION_OFF}
        | cat -;  \
                echo '---------- VACATION -----------------'; \
                echo 'Removing message and cache: '; \
                rm -vf $VACATION_MSG; \
                rm -vf $VACATION_CACHE; \
                echo ; \
                echo "Removed vacation message."

}
------------------------------------ .procmailrc --------------------------------

----------
 Priklady
----------
http://www.erehwon.org/erehwon/procmailex.html

Maildrop

Procmail vs Maildrop

asciiart
Funkcia                         procmail                                maildrop
-----------------------------------------------------------------------------------------------
Dorucenie spravy                | /cesta/k/programu                     to "|/cesta/k/programu"
externym programom                                                      alebo                                                   alebo
                                                                        cc "|/cesta/k/programu"

Filtrovanie spravy              to iste, ale zaciatok pravidla          xfilter "/cesta/k/programu"
externym programom              musi obsahovat priznak f:
                                :0 f

Filtrovanie sprav               :0                                      if ($SIZE > 10000000)
podla velkosti                  * > 10000000                            to spravy_nad_10MB
                                spravy_nad_10MB                         alebo
                                                                        if ($LINES > 500)
                                                                        to spravy_nad_500_riadkov

Reformatting messages       the external program formail:            the external program reformail:
(napr. pridanie hlavicky)       :0 f                                    xfilter "reformail \ -A'X-Envelope-From: $FROM'"
                                | formail -A"Message-ID:"
                                                                        maildrop also carries the program reformime, which works with MIME messages

DOC

asciiart
man procmail
man procmailrc
man procmailex
man procmailsc

LINKZ

asciiart
http://pm-doc.sourceforge.net/
http://www.perlcode.org/tutorials/procmail/proctut/

http://www.impsec.org/email-tools/procmail-security.html
http://www.root.cz/clanky/procmail-vs-maildrop/
http://www.root.cz/clanky/procmail-pre-zaciatocnikov/
http://www.ii.com/internet/robots/procmail/qs/
http://partmaps.org/era/procmail/links.html
http://lipas.uwasa.fi/~ts/info/proctips.html
← howtoz(EN | SK)