You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@spamassassin.apache.org by raiden031 <mi...@yahoo.com> on 2011/06/16 17:05:53 UTC

Bizarre rule definitions

So I'm trying to understand the spamAssassin rules, and I found a couple of
things that don't make sense about the rules I downloaded.

1) Some of the header, body, and uri rules have regular expressions that are
not enclosed in '/' (ie. /pattern/i ).  Instead they are enclosed with 'm'
followed by another character.  

I have seen the following different ways to enclose a pattern:

/pattern/i   # As documented
m{pattern}i
m'pattern'i
m%pattern%i
m!pattern!i

Example below:

uri FU_END_ET m'/et/$'i

Is this valid and if so, why is it being done?

2) I don't understand the use of the '!~' for header rules.  The
documentation says '=~' means 'contains a regular expression' but '!~' means
'does not contain a regular expression'.  Yet in both cases, there is a
regular expression associated with the rule.  

header FH_FROMEML_NOTLD From:addr !~ /\./ [if-unset: foo@bar.com]
describe FH_FROM_EML_NOTLD E-mail address doesn't have TLD (.com, etc.)

For instance, could someone explain how the above rule works?  It looks like
to me it should hit whenever there is any From address populated regardless
of whether there's a .com or not.


-- 
View this message in context: http://old.nabble.com/Bizarre-rule-definitions-tp31861533p31861533.html
Sent from the SpamAssassin - Users mailing list archive at Nabble.com.


Re: Bizarre rule definitions

Posted by raiden031 <mi...@yahoo.com>.

Ok thanks all, I didn't realize the 'm' thing was part of Perl regex.  I
came from using java regular expressions where I don't deal with the '/' and
'm' characters.




Bowie Bailey wrote:
> 
> On 6/16/2011 11:05 AM, raiden031 wrote:
>> So I'm trying to understand the spamAssassin rules, and I found a couple
>> of
>> things that don't make sense about the rules I downloaded.
> 
> SpamAssassin uses Perl regular expressions.  For more info, look that up
> on Google and you should be able to find plenty of info.
> 
>> 1) Some of the header, body, and uri rules have regular expressions that
>> are
>> not enclosed in '/' (ie. /pattern/i ).  Instead they are enclosed with
>> 'm'
>> followed by another character.  
>>
>> I have seen the following different ways to enclose a pattern:
>>
>> /pattern/i   # As documented
>> m{pattern}i
>> m'pattern'i
>> m%pattern%i
>> m!pattern!i
>>
>> Example below:
>>
>> uri FU_END_ET m'/et/$'i
>>
>> Is this valid and if so, why is it being done?
> 
> This is done to make a pattern more readable (mainly to avoid the
> necessity of escaping the '/' character).
> 
> For example, both of these patterns will match a series of three slashes:
> 
>     /\/\/\//
>     m'///'
> 
> When you use the 'm' form, you can use almost any separator you want. 
> The examples you gave above are some of the most common.  You generally
> want to pick a separator character that does not appear in the expression.
> 
> FYI:  The 'i' at the end of the patterns above is a pattern modifier
> that makes the expression case-insensitive.
> 
>> 2) I don't understand the use of the '!~' for header rules.  The
>> documentation says '=~' means 'contains a regular expression' but '!~'
>> means
>> 'does not contain a regular expression'.  Yet in both cases, there is a
>> regular expression associated with the rule.  
>>
>> header FH_FROMEML_NOTLD From:addr !~ /\./ [if-unset: foo@bar.com]
>> describe FH_FROM_EML_NOTLD E-mail address doesn't have TLD (.com, etc.)
>>
>> For instance, could someone explain how the above rule works?  It looks
>> like
>> to me it should hit whenever there is any From address populated
>> regardless
>> of whether there's a .com or not.
> 
> The '!~' form means that the rule matches when the regular expression
> does NOT match.
> 
> For example:
> 
>     From:addr !~ /\./
> 
> The above pattern will match on any from address that does NOT contain a
> period.
> 
> -- 
> Bowie
> 
> 

-- 
View this message in context: http://old.nabble.com/Bizarre-rule-definitions-tp31861533p31862044.html
Sent from the SpamAssassin - Users mailing list archive at Nabble.com.


Re: Bizarre rule definitions

Posted by Bowie Bailey <Bo...@BUC.com>.
On 6/16/2011 11:05 AM, raiden031 wrote:
> So I'm trying to understand the spamAssassin rules, and I found a couple of
> things that don't make sense about the rules I downloaded.

SpamAssassin uses Perl regular expressions.  For more info, look that up
on Google and you should be able to find plenty of info.

> 1) Some of the header, body, and uri rules have regular expressions that are
> not enclosed in '/' (ie. /pattern/i ).  Instead they are enclosed with 'm'
> followed by another character.  
>
> I have seen the following different ways to enclose a pattern:
>
> /pattern/i   # As documented
> m{pattern}i
> m'pattern'i
> m%pattern%i
> m!pattern!i
>
> Example below:
>
> uri FU_END_ET m'/et/$'i
>
> Is this valid and if so, why is it being done?

This is done to make a pattern more readable (mainly to avoid the
necessity of escaping the '/' character).

For example, both of these patterns will match a series of three slashes:

    /\/\/\//
    m'///'

When you use the 'm' form, you can use almost any separator you want. 
The examples you gave above are some of the most common.  You generally
want to pick a separator character that does not appear in the expression.

FYI:  The 'i' at the end of the patterns above is a pattern modifier
that makes the expression case-insensitive.

> 2) I don't understand the use of the '!~' for header rules.  The
> documentation says '=~' means 'contains a regular expression' but '!~' means
> 'does not contain a regular expression'.  Yet in both cases, there is a
> regular expression associated with the rule.  
>
> header FH_FROMEML_NOTLD From:addr !~ /\./ [if-unset: foo@bar.com]
> describe FH_FROM_EML_NOTLD E-mail address doesn't have TLD (.com, etc.)
>
> For instance, could someone explain how the above rule works?  It looks like
> to me it should hit whenever there is any From address populated regardless
> of whether there's a .com or not.

The '!~' form means that the rule matches when the regular expression
does NOT match.

For example:

    From:addr !~ /\./

The above pattern will match on any from address that does NOT contain a
period.

-- 
Bowie

Re: Bizarre rule definitions

Posted by RW <rw...@googlemail.com>.
On Thu, 16 Jun 2011 08:05:53 -0700 (PDT)
raiden031 <mi...@yahoo.com> wrote:

> 

> header FH_FROMEML_NOTLD From:addr !~ /\./ [if-unset: foo@bar.com]
> describe FH_FROM_EML_NOTLD E-mail address doesn't have TLD (.com,
> etc.)
> 
> For instance, could someone explain how the above rule works?  It
> looks like to me it should hit whenever there is any From address
> populated regardless of whether there's a .com or not.


The rule is looking for an email address without a dot in it.

[if-unset: foo@bar.com] means that if From:addr has no value then use
foo@bar.com instead. This prevents the rule from being hit if there is
no From email address. AFAIK this isn't standard perl, but it is
documented in Mail::SpamAssassin::Conf

Most uses of this feature seem a bit gratuitous to me. 

Re: Bizarre rule definitions

Posted by Martin Gregorie <ma...@gregorie.org>.
On Thu, 2011-06-16 at 08:05 -0700, raiden031 wrote:
> So I'm trying to understand the spamAssassin rules, and I found a couple of
> things that don't make sense about the rules I downloaded.
> 
Everything you're puzzled about are standard Perl regular expression
syntax: to understand and write SA rules you really need to know how to
write regular expressions using the Perl regex rules.

They are fully described in books like "Programming Perl" by Wall,
Christiansen and Orwant (pub. O'Reilly), known as "The Camel Book".
Alternatively, you can find online descriptions by searching for "Perl
regular expression", "Perl regex" or "pcre". There are several flavours
of regular expression in use, so its important to learn about the right
one.

 
Martin