procmail
[Top] [All Lists]

Re: Problem with regex and backreferences

2002-01-06 22:02:50
On  7 Jan, Andrew Peacock wrote:
| Hi,
| I've read around the regex info for procmail, and I've not been able to 
| solve my problem,despite using regex in perl for a long time, but I do 
| appreciate they are different - I'm just not sure exactly how :-)
| 
| What I want to do is to catch all emails coming in to
| 
| subscribe-(.*)@domain.com
| 
| forwarded to subscribe(_at_)domain(_dot_)com with the regex match in the 
subject.
| 
| What I've got so far is:
| 
| :0
| ^TO_subscribe-(.*)@domain.com
|| formail -k -X "From: " \
|                  -I "Subject: \1" -X "Subject: " \
|                  -I "To: subscribe(_at_)domain(_dot_)com" -X "To: " \
|                  -I "X-Loop: procmail(_at_)domain(_dot_)com" -X "X-Loop" \
|     | $SENDMAIL $SENDMAILFLAGS -t
| 
| This does forward the mail, but does not capture the regex match
| 
| I've also tried using $1 instead of \1 for the variable - no difference.
| 
| Can anyone point me in the right direction?
| 

Procmail doesn't grok back references. As TjL pointed out, you have to
use procmail's MATCH token "\/" which captures everything that follows
the token to the variable $MATCH. One thing to remember when doing stuff
like this, is that the procmail regexp engine is stingy to the left of
the token and greedy to the right. So you ought to be able to do
something like:

:0
* ^TO_subscribe-\/(_dot_)*(_at_)domain(_dot_)com
* MATCH ?? ^^\/[^(_at_)]+
| formail -k -X "From: " \
                 -I "Subject: $MATCH" -X "Subject: " \
                 -I "To: subscribe(_at_)domain(_dot_)com" -X "To: " \
                 -I "X-Loop: procmail(_at_)domain(_dot_)com" -X "X-Loop" \
    | $SENDMAIL $SENDMAILFLAGS -t

Note the leading "*" on the conditions. Your's was missing that. This
might not do what's expected if there was any chance of an address with 
more than one "@". I would think most MTA's would be configured today to
reject such messages to prevent relaying, but I have seen some spam
where quotes protected the extra @ (e.g "abc@"@dom.tld). I mention this
just as a possible gotcha in case you want to add more conditions to be
sure. Lastly, IIRC, some older procmail versions don't allow the use of
MATCH in succeeding conditions of the same recipe, but I can't tell you
offhand at what version that becomes an issue.

Don Hammond

-- 
Reply to list please, or append "6" to "procmail" in address if you must.
Spammers' unrelenting address harvesting forces me to this...reluctantly.


_______________________________________________
procmail mailing list
procmail(_at_)lists(_dot_)RWTH-Aachen(_dot_)DE
http://MailMan.RWTH-Aachen.DE/mailman/listinfo/procmail

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