简体   繁体   中英

Procmail variable based if condition

I am running procmail recipe that would trigger some of my applications the moment I receive a specific email. I have the whole thing working but now I need to build conditions into the recipe as to not make it run again and again as to avoid multiple instances of the same program since I have procmail trigger every 10minutes. Problem is i'm not exactly sure how 'if' sentences are made in procmail.

Here is the recipe I have so far:

:0
* ^Subject: .*Email Subject!
| export DISPLAY=:0.0; 
  xrandr --size 1360x768;\
  firefox "link"; \
  timeout 10s recordmydesktop --fps 30; \
  xrandr --size 1366x768

The easy and idiomatic way to have a critical section in Procmail is to use a lock file.

# Notice the second colon and the name of the lock file to use
:0:firefox.lock
* ^Subject: .*Email Subject!
| export DISPLAY=:0.0; 
  xrandr --size 1360x768;\
  firefox "link"; \
  timeout 10s recordmydesktop --fps 30; \
  xrandr --size 1366x768

This will create $MAILDIR/firefox.lock when the recipe is evaluated, and remove it when the recipe finishes. If the file already exists, Procmail will wait until it disappears, or eventually time out (which could cause the incoming message to bounce).

If you need a critical section spanning multiple recipes, you can assign to the "magical" variable LOCKFILE and set it to an empty value when you are done.

LOCKFILE=firefox.lock

# ... Your recipes here ...

LOCKFILE=

(Obscurely, the equals sign on the last line of this example is optional; but I recommend against that usage.)

See man 5 procmailrc for (much) more, including LOCKSLEEP and LOCKTIMEOUT .

The trivial answer to "how to say 'if' in Procmail" is to use a condition. You already have one; the action will only trigger if the message's headers match the regular expression ^Subject:.*Email Subject! . You can nest these conditions, test variables, external commands, etc. Here's a silly made-up example to demonstrate them all.

# If $FOO is set and non-empty
:0
* FOO ?? .
{
    # ... then enter this nested block
    # Does $HOME/bar exist?
    :0
    * ? test -e $HOME/bar
    barista

    # Otherwise, unconditionally deliver to foolish
    :0
    foolish
}

The block is entered if the variable FOO is set. Procmail uses your environment variables, so you can set it before invoking Procmail (depending on Procmail's options; it will only inherit a sanitized copy of your environment by default) or on its command line as well as in your recipe file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM