Mailing List Archive

Show regex capture group in logs?
Hi, I want to capture part of a regex match (capture group) to put in
logs. This example using subject header works but is there a better way?

warn set acl_c_temp = ${if match{$h_Subject:}{<regex with capture
group>}{$1}{none}}
!condition = ${if eq{$acl_c_temp}{none}}
logwrite = Notice: subject match is: $acl_c_temp

This also risky because must be careful to make sure "none" could never
come from the regex match group

PS: is there better local variable to use?

--
## subscription configuration (requires account):
## https://lists.exim.org/mailman3/postorius/lists/exim-users.lists.exim.org/
## unsubscribe (doesn't require an account):
## exim-users-unsubscribe@lists.exim.org
## Exim details at http://www.exim.org/
## Please use the Wiki with this list - http://wiki.exim.org/
Re: Show regex capture group in logs? [ In reply to ]
On 16/05/2023 20:36, MRob via Exim-users wrote:
> Hi, I want to capture part of a regex match (capture group) to put in logs. This example using subject header works but is there a better way?
>
> warn set acl_c_temp = ${if match{$h_Subject:}{<regex with capture group>}{$1}{none}}
>      !condition = ${if eq{$acl_c_temp}{none}}
>      logwrite = Notice: subject match is: $acl_c_temp
>
> This also risky because must be careful to make sure "none" could never come from the regex match group

Why not shring the word "none" to an empty string?

> PS: is there better local variable to use?

In what sense "better" ?
--
Cheers,
Jeremy


--
## subscription configuration (requires account):
## https://lists.exim.org/mailman3/postorius/lists/exim-users.lists.exim.org/
## unsubscribe (doesn't require an account):
## exim-users-unsubscribe@lists.exim.org
## Exim details at http://www.exim.org/
## Please use the Wiki with this list - http://wiki.exim.org/
Re: Show regex capture group in logs? [ In reply to ]
D?a 16. mája 2023 19:36:00 UTC používate? MRob via Exim-users <exim-users@lists.exim.org> napísal:
>Hi, I want to capture part of a regex match (capture group) to put in logs. This example using subject header works but is there a better way?
>
>warn set acl_c_temp = ${if match{$h_Subject:}{<regex with capture group>}{$1}{none}}
> !condition = ${if eq{$acl_c_temp}{none}}
> logwrite = Notice: subject match is: $acl_c_temp

Empty variable is the same as undefined:

set acl_m_temp = ${if match{$h_Subject:}{<regex>}{$1}}
condition = ${if def:acl_m_temp}
...

Note, use acl_m_... for that, one connection can transfer
multiple unrelated messages, once acl_c_... will be set,
it can remain in that state for other messages in the
same connection (while not in this case, but anyway).
The acl_m_... will be cleared for every message for you
by exim itself.

regards


--
Slavko
https://www.slavino.sk/

--
## subscription configuration (requires account):
## https://lists.exim.org/mailman3/postorius/lists/exim-users.lists.exim.org/
## unsubscribe (doesn't require an account):
## exim-users-unsubscribe@lists.exim.org
## Exim details at http://www.exim.org/
## Please use the Wiki with this list - http://wiki.exim.org/
Re: Show regex capture group in logs? [ In reply to ]
On Tue, 16 May 2023, MRob via Exim-users wrote:

> Hi, I want to capture part of a regex match (capture group) to put in logs.
> This example using subject header works but is there a better way?
>
> warn set acl_c_temp = ${if match{$h_Subject:}{<regex with capture
> group>}{$1}{none}}
> !condition = ${if eq{$acl_c_temp}{none}}
> logwrite = Notice: subject match is: $acl_c_temp
>
> This also risky because must be careful to make sure "none" could never come
> from the regex match group
>
> PS: is there better local variable to use?

Since it is just conditional logging of a header
would a system filter be the place for this ?

--
Andrew C. Aitchison Kendal, UK
andrew@aitchison.me.uk

--
## subscription configuration (requires account):
## https://lists.exim.org/mailman3/postorius/lists/exim-users.lists.exim.org/
## unsubscribe (doesn't require an account):
## exim-users-unsubscribe@lists.exim.org
## Exim details at http://www.exim.org/
## Please use the Wiki with this list - http://wiki.exim.org/
Re: Show regex capture group in logs? [ In reply to ]
On 16/05/2023 21:08, Andrew C Aitchison via Exim-users wrote:
> Since it is just conditional logging of a header
> would a system filter be the place for this ?

The basic exim config can do it.

There's no good reason to take that filter cost (stepping back
one thread...)

--
Cheers,
Jeremy


--
## subscription configuration (requires account):
## https://lists.exim.org/mailman3/postorius/lists/exim-users.lists.exim.org/
## unsubscribe (doesn't require an account):
## exim-users-unsubscribe@lists.exim.org
## Exim details at http://www.exim.org/
## Please use the Wiki with this list - http://wiki.exim.org/
Re: Show regex capture group in logs? [ In reply to ]
On 2023-05-16 20:05, Slavko via Exim-users wrote:
> D?a 16. mája 2023 19:36:00 UTC používate? MRob via Exim-users
> <exim-users@lists.exim.org> napísal:
>> Hi, I want to capture part of a regex match (capture group) to put in
>> logs. This example using subject header works but is there a better
>> way?
>>
>> warn set acl_c_temp = ${if match{$h_Subject:}{<regex with capture
>> group>}{$1}{none}}
>> !condition = ${if eq{$acl_c_temp}{none}}
>> logwrite = Notice: subject match is: $acl_c_temp
>
> Empty variable is the same as undefined:
>
> set acl_m_temp = ${if match{$h_Subject:}{<regex>}{$1}}
> condition = ${if def:acl_m_temp}
> ...

Thanks Slavko!

> Note, use acl_m_... for that, one connection can transfer
> multiple unrelated messages, once acl_c_... will be set,
> it can remain in that state for other messages in the
> same connection (while not in this case, but anyway).
> The acl_m_... will be cleared for every message for you
> by exim itself.

I wasnt sure which is best. In this situation temp variable getting set
every time *even if set to undefined* correct? So I understand it should
not matter what variable to use, right?

If you do a few in a row it's not a problem right?

warn set acl_m_temp = ${if match{$h_Subject:}{<regex 1>}{$1}}
condition = ${if def:acl_m_temp}
...
warn set acl_m_temp = ${if match{$h_Subject:}{<regex 2>}{$1}}
condition = ${if def:acl_m_temp}
...
warn set acl_m_temp = ${if match{$h_Subject:}{<regex 3>}{$1}}
condition = ${if def:acl_m_temp}
...

--
## subscription configuration (requires account):
## https://lists.exim.org/mailman3/postorius/lists/exim-users.lists.exim.org/
## unsubscribe (doesn't require an account):
## exim-users-unsubscribe@lists.exim.org
## Exim details at http://www.exim.org/
## Please use the Wiki with this list - http://wiki.exim.org/
Re: Show regex capture group in logs? [ In reply to ]
On 2023-05-16 20:08, Andrew C Aitchison via Exim-users wrote:
> On Tue, 16 May 2023, MRob via Exim-users wrote:
>
>> Hi, I want to capture part of a regex match (capture group) to put in
>> logs. This example using subject header works but is there a better
>> way?
>>
>> warn set acl_c_temp = ${if match{$h_Subject:}{<regex with capture
>> group>}{$1}{none}}
>> !condition = ${if eq{$acl_c_temp}{none}}
>> logwrite = Notice: subject match is: $acl_c_temp
>>
>> This also risky because must be careful to make sure "none" could
>> never come from the regex match group
>>
>> PS: is there better local variable to use?
>
> Since it is just conditional logging of a header
> would a system filter be the place for this ?

Sorry Im a beginner so I don't know yet much about those or when it's
best to use them. Jeremy made it sound that your suggesting is an
unnecessary burden but if there's something youre suggesting I love to
learn! (note it's conditional logging of *part* of a header)

--
## subscription configuration (requires account):
## https://lists.exim.org/mailman3/postorius/lists/exim-users.lists.exim.org/
## unsubscribe (doesn't require an account):
## exim-users-unsubscribe@lists.exim.org
## Exim details at http://www.exim.org/
## Please use the Wiki with this list - http://wiki.exim.org/
Re: Show regex capture group in logs? [ In reply to ]
On 16/05/2023 22:05, MRob via Exim-users wrote:
> If you do a few in a row it's not a problem right?

Correct; new assignments just overwrite.
--
Cheers,
Jeremy


--
## subscription configuration (requires account):
## https://lists.exim.org/mailman3/postorius/lists/exim-users.lists.exim.org/
## unsubscribe (doesn't require an account):
## exim-users-unsubscribe@lists.exim.org
## Exim details at http://www.exim.org/
## Please use the Wiki with this list - http://wiki.exim.org/
Re: Show regex capture group in logs? [ In reply to ]
D?a 16. mája 2023 21:05:49 UTC používate? MRob via Exim-users <exim-users@lists.exim.org> napísal:

>I wasnt sure which is best. In this situation temp variable getting set every time *even if set to undefined* correct? So I understand it should not matter what variable to use, right?

Yes, you can (re)set variables to new (even empty) value
anywhere in ACL, the previous value is not took into
account, nor empty, nor non-empty, just the new one.

>If you do a few in a row it's not a problem right?

Yes and no, it depends on your ACLs. You must consider,
that not always are ACLs as simple, as in your examples and
often multiple (different) conditions can take into account
before value will be set, thus particular setting can be
skipped and variable remains its previous value. Sometime
it doesn't matter as condition willl be skipped too, but you
can test that value on different places, not just after it is set.
In other words, here is not one universal answer for that...

The acl_c (and acl_m too) are cleared when connection is
closed, thus on new connection are both empty (the "c"
is for connection).

The acl_m variables are cleared when new session (message)
starts, basically with new MAIL SMTP command (the "m"
is for message).

In all other cases it is your responsibility...

regards


--
Slavko
https://www.slavino.sk/

--
## subscription configuration (requires account):
## https://lists.exim.org/mailman3/postorius/lists/exim-users.lists.exim.org/
## unsubscribe (doesn't require an account):
## exim-users-unsubscribe@lists.exim.org
## Exim details at http://www.exim.org/
## Please use the Wiki with this list - http://wiki.exim.org/
Re: Show regex capture group in logs? [ In reply to ]
On Tue, 16 May 2023, MRob via Exim-users wrote:

> On 2023-05-16 20:08, Andrew C Aitchison via Exim-users wrote:
>> On Tue, 16 May 2023, MRob via Exim-users wrote:
>>
>>> Hi, I want to capture part of a regex match (capture group) to put in
>>> logs. This example using subject header works but is there a better way?
>>>
>>> warn set acl_c_temp = ${if match{$h_Subject:}{<regex with capture
>>> group>}{$1}{none}}
>>> !condition = ${if eq{$acl_c_temp}{none}}
>>> logwrite = Notice: subject match is: $acl_c_temp
>>>
>>> This also risky because must be careful to make sure "none" could never
>>> come from the regex match group
>>>
>>> PS: is there better local variable to use?
>>
>> Since it is just conditional logging of a header
>> would a system filter be the place for this ?
>
> Sorry Im a beginner so I don't know yet much about those or when it's best to
> use them. Jeremy made it sound that your suggesting is an unnecessary burden
> but if there's something youre suggesting I love to learn! (note it's
> conditional logging of *part* of a header)

Sorry, I missed that you are only logging *part* of a header.
Usually I find that when I do that sort of thing I don't have the piece of
information that would explain the incident.

A system filter is like a .forward, but applied to all mail,
not just that of one user.
Unless you already have one, you are better off doing this in the
main config.

--
Andrew C. Aitchison Kendal, UK
andrew@aitchison.me.uk

--
## subscription configuration (requires account):
## https://lists.exim.org/mailman3/postorius/lists/exim-users.lists.exim.org/
## unsubscribe (doesn't require an account):
## exim-users-unsubscribe@lists.exim.org
## Exim details at http://www.exim.org/
## Please use the Wiki with this list - http://wiki.exim.org/
Re: Show regex capture group in logs? [ In reply to ]
On 2023-05-16, MRob via Exim-users <exim-users@lists.exim.org> wrote:
> Hi, I want to capture part of a regex match (capture group) to put in
> logs. This example using subject header works but is there a better way?
>
> warn set acl_c_temp = ${if match{$h_Subject:}{<regex with capture
> group>}{$1}{none}}
> !condition = ${if eq{$acl_c_temp}{none}}
> logwrite = Notice: subject match is: $acl_c_temp
>
> This also risky because must be careful to make sure "none" could never
> come from the regex match group

probably better is

warn
set acl_m0 = ${if match{$h_Subject:}{<regex with capture group>}{1$1}{0}}
# if it matched acl_m0 starts with "1" else with "0"

condition = ${length_1:$acl_m0}
# condition understands "1" and "0" so no need for "${if..."

logwrite = Notice: subject match is: ${substr_1:$acl_m0}
# need to chop off the first charcter for printing.


> PS: is there better local variable to use?

I usually use an acl_m variable in message context, and if it's of
temporary use I use a numbered variable, but that's just a convention
I have adopted.


--
Jasen.
???????? ????? ???????

--
## subscription configuration (requires account):
## https://lists.exim.org/mailman3/postorius/lists/exim-users.lists.exim.org/
## unsubscribe (doesn't require an account):
## exim-users-unsubscribe@lists.exim.org
## Exim details at http://www.exim.org/
## Please use the Wiki with this list - http://wiki.exim.org/