procmail
[Top] [All Lists]

Re: Counting score program exit code and negation

1997-02-04 16:46:05
Ken Marsh asked,

| I'd like to count the '@' signs in the header fields, and use
| the results in a score. I've asked this question before, and
| appreciate the help, but previous responses posted here can't do
| this with internal egrep, they only count the number of LINES
| with '@' in them, which is not enough.

Those responses were wrong, Ken, or perhaps you misunderstood them.
Procmail's internal egrep most certainly can count the @-signs.

| According to the procmail manual, I should be able to run
| an external program and use the return code as a score
| if I "negate" it. It gives no examples (that I can find).

That is true, but you coded it incorrectly.

| I've tried:
| 
| :0 h c
| * -15^0
| * !? /usr/kmarsh/bin/countat

Er, no.  The weights and the condition have to be on the same line, as
Lars Kellogg-Stedman explained.  The way you have it now, it says this:

1. Subtract fifteen points if there is a null string anywhere in the head.
(There always is, so you've unconditionally started at -15.)

2. Require that countat return a bad exit code.

So what happens?  If countat returns a good exit code, the second condition
fails, so the action (a brace nest) is not executed.  If countat returns a
bad exit code, fine, but there is a score, and the score is not positive,
so the recipe still is considered a non-match, and the action is still not
executed.

What you wanted was to score -15 if countat returns a good exit and 0 if
it returns a bad one?  Then

  * -15^0 ? /usr/kmarsh/bin/countat

By the way, "h" is meaningless on a recipe whose action line is to launch
a brace nest.  If you want any or all recipes inside the braces to act only
on the head, they need their own h's: flags are not inherited.

By another way, if you have a $HOME/bin directory, you probably call a lot
of executables from it and ought to add it to your procmailrc's PATH.

| :0 h c
| * -15^0
| * -? /usr/kmarsh/bin/countat

"-?" is illegal syntax.

Now, if you really want to count all @-signs in the head and allow for
fifteen legit ones (From:, Message-Id:, a few Received:), here's how
you code it without outside programs:

  :0c # Are you sure you want a clone here?    
  * 1^1 @
  * -15^0
  {
   whatever you had in the braces
  }

Note that that counts @-signs, not lines with @-signs.  If you actually had
wanted to count lines with @-signs you could have done that this way:

  * 1^1 ^(_dot_)*(_at_)(_dot_)*$

but as you said, that's not what you want.

If there are sixteen or more @'s, procmail will fork a clone that will
skip all code in your .procmailrc up through that left brace and start
there, while the parent procmail will continue after the right brace.

If there are fifteen or fewer @'s, procmail will not fork a clone but
just pick up after the right brace.