Discussion:
[SR-Users] Text Operations on a Message
AliReza Khoshgoftar Monfared
2014-08-14 18:59:42 UTC
Permalink
Hi list,

How can I extract a specific part of my message body?
I know that "textops" has the function "search_body(re)" but it returns
True/False upon finding "re" in the body of the text, how can I return the
actual text that regular expression matches?

As an example, my message body is

rate is 172#012


I want to extract the number "172" out of this.


Thanks
Alireza
Alex Balashov
2014-08-14 23:40:46 UTC
Permalink
Post by AliReza Khoshgoftar Monfared
Hi list,
How can I extract a specific part of my message body?
I know that "textops" has the function "search_body(re)" but it returns
True/False upon finding "re" in the body of the text, how can I return
the actual text that regular expression matches?
As an example, my message body is
rate is 172#012
I want to extract the number "172" out of this.
There is no general means of accomplishing that. Your best bet is to
feed the message body to another language using one of the language
interface modules, such as Lua, Perl or Python, and parse stuff out of
it there.

For all of its features, Kamailio route script is not a general-purpose
programming runtime.
--
Alex Balashov - Principal
Evariste Systems LLC
Tel: +1-678-954-0670
Web: http://www.evaristesys.com/, http://www.alexbalashov.com/

Please be kind to the English language:

http://www.entrepreneur.com/article/232906
AliReza Khoshgoftar Monfared
2014-08-15 04:13:31 UTC
Permalink
Thanks very much Alex.
You are right. In general kamailio does not offer much functionality of a
general purpose language.
The other option for the task that I found was "s.rm" transformation.
Although it works the other way :) you need to match what needs to be
removed :)

Now a general question ( or request for a word of advice :) ): if you have
ultimate flexibility and want to send an integer from one proxy to another.
What functions ( modules ) are the best to append and recover it? ( i don't
care where it is appended, body or header, etc )
Post by AliReza Khoshgoftar Monfared
Hi list,
How can I extract a specific part of my message body?
I know that "textops" has the function "search_body(re)" but it returns
True/False upon finding "re" in the body of the text, how can I return
the actual text that regular expression matches?
As an example, my message body is
rate is 172#012
I want to extract the number "172" out of this.
There is no general means of accomplishing that. Your best bet is to feed
the message body to another language using one of the language interface
modules, such as Lua, Perl or Python, and parse stuff out of it there.
For all of its features, Kamailio route script is not a general-purpose
programming runtime.
--
Alex Balashov - Principal
Evariste Systems LLC
Tel: +1-678-954-0670
Web: http://www.evaristesys.com/, http://www.alexbalashov.com/
http://www.entrepreneur.com/article/232906
_______________________________________________
SIP Express Router (SER) and Kamailio (OpenSER) - sr-users mailing list
http://lists.sip-router.org/cgi-bin/mailman/listinfo/sr-users
Alex Balashov
2014-08-15 11:36:00 UTC
Permalink
Hello AliReza,
Post by AliReza Khoshgoftar Monfared
Now a general question ( or request for a word of advice :) ): if you
have ultimate flexibility and want to send an integer from one proxy to
another. What functions ( modules ) are the best to append and recover
it? ( i don't care where it is appended, body or header, etc )
If you just want to send such values around in standard SIP requests,
i.e. insert them to an INVITE, I would say that the most canonical and
widespread way to do this is via custom SIP headers, e.g.

On the sender:

append_hf("X-Alireza-Rate: 172\r\n");

On the receiver:

if(is_present_hf("X-Alireza-Rate")) {
xlog("L_INFO", "Extracted rate is $hdr(X-Alireza-Rate)\n");

# Don't pass this header further upstream.

remove_hf("X-Alireza-Rate");
}
--
Alex Balashov - Principal
Evariste Systems LLC
Tel: +1-678-954-0670
Web: http://www.evaristesys.com/, http://www.alexbalashov.com/

Please be kind to the English language:

http://www.entrepreneur.com/article/232906
AliReza Khoshgoftar Monfared
2014-08-15 18:43:23 UTC
Permalink
Thanks very much Alex and Hugh,

I think what Hugh mentioned is the best way to process text in Kamailio
without relying on an external tool and it's good to know it, and Alex's
suggestion is great for transferring a single number.

Cheers,
Alireza
Post by Alex Balashov
is_present_hf("X-Alireza-Rate")
Alex Balashov
2014-08-15 18:48:38 UTC
Permalink
Post by AliReza Khoshgoftar Monfared
Thanks very much Alex and Hugh,
I think what Hugh mentioned is the best way to process text in Kamailio
without relying on an external tool and it's good to know it, and Alex's
suggestion is great for transferring a single number.
It does not have to be a single number. A header can have any value you
like, e.g.

X-AliReza-Custom-Header: param1=x;param2=y;param3=z

Which you could then extract with string transformations[1]:

$var(param1) = $(hdr(X-AliReza-Custom-Header){s.select,0,;});
$var(param2) = $(hdr(X-AliReza-Custom-Header){s.select,1,;});
$var(param3) = $(hdr(X-AliReza-Custom-Header){s.select,2,;});

-- Alex

[1]
http://www.kamailio.org/wiki/cookbooks/4.1.x/transformations#sselect_index_separator
--
Alex Balashov - Principal
Evariste Systems LLC
Tel: +1-678-954-0670
Web: http://www.evaristesys.com/, http://www.alexbalashov.com/

Please be kind to the English language:

http://www.entrepreneur.com/article/232906
AliReza Khoshgoftar Monfared
2014-08-15 18:54:24 UTC
Permalink
Awesome, and I assume to recover the integer value, I need sth like, as
well:

$var(param1) = $(var(param1){s.int});

Thanks very much,
Alireza
Post by AliReza Khoshgoftar Monfared
Thanks very much Alex and Hugh,
Post by AliReza Khoshgoftar Monfared
I think what Hugh mentioned is the best way to process text in Kamailio
without relying on an external tool and it's good to know it, and Alex's
suggestion is great for transferring a single number.
It does not have to be a single number. A header can have any value you
like, e.g.
X-AliReza-Custom-Header: param1=x;param2=y;param3=z
$var(param1) = $(hdr(X-AliReza-Custom-Header){s.select,0,;});
$var(param2) = $(hdr(X-AliReza-Custom-Header){s.select,1,;});
$var(param3) = $(hdr(X-AliReza-Custom-Header){s.select,2,;});
-- Alex
[1] http://www.kamailio.org/wiki/cookbooks/4.1.x/
transformations#sselect_index_separator
--
Alex Balashov - Principal
Evariste Systems LLC
Tel: +1-678-954-0670
Web: http://www.evaristesys.com/, http://www.alexbalashov.com/
http://www.entrepreneur.com/article/232906
_______________________________________________
SIP Express Router (SER) and Kamailio (OpenSER) - sr-users mailing list
http://lists.sip-router.org/cgi-bin/mailman/listinfo/sr-users
Alex Balashov
2014-08-15 18:56:24 UTC
Permalink
Post by AliReza Khoshgoftar Monfared
Awesome, and I assume to recover the integer value, I need sth like, as
$var(param1) = $(var(param1){s.int <http://s.int>});
Yep. In fact, you can chain transformations:

$(hdr(X-AliReza-Custom-Header){s.select,0,;}{s.int});
--
Alex Balashov - Principal
Evariste Systems LLC
Tel: +1-678-954-0670
Web: http://www.evaristesys.com/, http://www.alexbalashov.com/

Please be kind to the English language:

http://www.entrepreneur.com/article/232906
AliReza Khoshgoftar Monfared
2014-08-15 18:58:03 UTC
Permalink
Awesome,

Thanks very much
Post by AliReza Khoshgoftar Monfared
Awesome, and I assume to recover the integer value, I need sth like, as
Post by AliReza Khoshgoftar Monfared
$var(param1) = $(var(param1){s.int <http://s.int>});
$(hdr(X-AliReza-Custom-Header){s.select,0,;}{s.int});
--
Alex Balashov - Principal
Evariste Systems LLC
Tel: +1-678-954-0670
Web: http://www.evaristesys.com/, http://www.alexbalashov.com/
http://www.entrepreneur.com/article/232906
_______________________________________________
SIP Express Router (SER) and Kamailio (OpenSER) - sr-users mailing list
http://lists.sip-router.org/cgi-bin/mailman/listinfo/sr-users
Alex Balashov
2014-08-15 19:00:28 UTC
Permalink
Sure thing.

The main issue you were having is access to the information you want in
the part of the message body where you want it.

Assuming you can get that and that it's formatted in a way that the
transformation functions in Kamailio can operate on it, i.e. split by
delimiter versus regex extraction, you can pass around anything any way
you like. :-)
--
Alex Balashov - Principal
Evariste Systems LLC
Tel: +1-678-954-0670
Web: http://www.evaristesys.com/, http://www.alexbalashov.com/

Please be kind to the English language:

http://www.entrepreneur.com/article/232906
Waite, Hugh
2014-08-15 09:43:13 UTC
Permalink
Hi,
The re.subst transformation can be used on variables to extract data. http://www.kamailio.org/wiki/cookbooks/devel/transformations#resubst_expression

$rb is the variable that contains the request body, but you can use $var or $avp variables if doing multiple operations.

$var(data) = (int)$(rb{re.subst,/^rate is ([0-9+).*$/\1});

Regards,
Hugh

From: sr-users-***@lists.sip-router.org [mailto:sr-users-***@lists.sip-router.org] On Behalf Of AliReza Khoshgoftar Monfared
Sent: 14 August 2014 20:00
To: Kamailio (SER) - Users Mailing List
Subject: [SR-Users] Text Operations on a Message

Hi list,

How can I extract a specific part of my message body?
I know that "textops" has the function "search_body(re)" but it returns True/False upon finding "re" in the body of the text, how can I return the actual text that regular expression matches?

As an example, my message body is

rate is 172#012

I want to extract the number "172" out of this.


Thanks
Alireza
________________________________
This e-mail and any attachment is for authorised use by the intended recipient(s) only. It may contain proprietary material, confidential information and/or be subject to legal privilege. It should not be copied, disclosed to, retained or used by, any other party. If you are not an intended recipient then please promptly delete this e-mail and any attachment and all copies and inform the sender. Thank you for understanding.
Waite, Hugh
2014-08-15 10:36:18 UTC
Permalink
I think my keyboard battery is failing, it keeps dropping characters
 Here’s a better example.

$var(data) = $(rb{re.subst,/^rate is ([0-9]+).*$/\1/});

Regards,
Hugh

From: sr-users-***@lists.sip-router.org [mailto:sr-users-***@lists.sip-router.org] On Behalf Of Waite, Hugh
Sent: 15 August 2014 10:43
To: Kamailio (SER) - Users Mailing List
Subject: Re: [SR-Users] Text Operations on a Message

Hi,
The re.subst transformation can be used on variables to extract data. http://www.kamailio.org/wiki/cookbooks/devel/transformations#resubst_expression

$rb is the variable that contains the request body, but you can use $var or $avp variables if doing multiple operations.

$var(data) = (int)$(rb{re.subst,/^rate is ([0-9+).*$/\1});

Regards,
Hugh

From: sr-users-***@lists.sip-router.org<mailto:sr-users-***@lists.sip-router.org> [mailto:sr-users-***@lists.sip-router.org] On Behalf Of AliReza Khoshgoftar Monfared
Sent: 14 August 2014 20:00
To: Kamailio (SER) - Users Mailing List
Subject: [SR-Users] Text Operations on a Message

Hi list,

How can I extract a specific part of my message body?
I know that "textops" has the function "search_body(re)" but it returns True/False upon finding "re" in the body of the text, how can I return the actual text that regular expression matches?

As an example, my message body is

rate is 172#012

I want to extract the number "172" out of this.


Thanks
Alireza
________________________________
This e-mail and any attachment is for authorised use by the intended recipient(s) only. It may contain proprietary material, confidential information and/or be subject to legal privilege. It should not be copied, disclosed to, retained or used by, any other party. If you are not an intended recipient then please promptly delete this e-mail and any attachment and all copies and inform the sender. Thank you for understanding.
________________________________
This e-mail and any attachment is for authorised use by the intended recipient(s) only. It may contain proprietary material, confidential information and/or be subject to legal privilege. It should not be copied, disclosed to, retained or used by, any other party. If you are not an intended recipient then please promptly delete this e-mail and any attachment and all copies and inform the sender. Thank you for understanding.
Loading...