[Freeswitch-users] extra headers to event message to locate user directory for voicemail
Daniel-Constantin Mierla
miconda at gmail.com
Fri Sep 21 00:53:24 MSD 2012
Hello,
I was trying to build user directory file dynamically via a lua script
at a dial-in event to listen to/leave a voice message, looking to pass
some values from particular SIP headers.
But the session object seems to be unavailable at that moment - next are
the relevant config snippets, the dialplan:
<extension name="vbox">
<condition field="destination_number" expression="^vb-(.+)$">
<action application="answer"/>
<action application="voicemail" data="default ${domain_name} $1"/>
</condition>
</extension>
and lua.conf:
<param name="xml-handler-script"
value="/usr/local/freeswitch/scripts/dp.lua"/>
<param name="xml-handler-bindings" value="directory"/>
The event message is like the one from:
http://wiki.freeswitch.org/wiki/Mod_lua/Serving_Configuration#when_being_called_.28by_another_extension.29
The Core-UUID is different that channel UUID, so I could not find any
API commands that could be used to get channel variables.
Do I miss any setting/command that should get me access to channel
variables or SIP headers inside the dp.lua?
My quick workaround was to patch to be able to add extra headers to the
internal event message (most of the time diving in the sources is the
fast way to get things done :-) ).
I am attaching the patch for the case there is no other solution and
someone else is interested - by now it adds extra headers only when
checking voicemail or recording a new message, but should be
straightforward to add to other places across voicemail module if it has
to (I can make the patch if one points other types of voicemail events
that can benefit of extra headers).
The patch adds a new channel variable, locate_event_extra_headers, that
can be set to a prefix of the channel variable names that have to be
passed as extra headers in the internal event message fired to locate
user directory. An example is in the patch comments.
If there is no solution to pass channel variables/SIP headers to the lua
scripts used for generating user directories, would it be a good feature
to add to devel branch? My particular need comes from using Kamailio as
the SIP server and when user is offline, the call is directed to
FreeSwitch for voicemail service. Also users can dial in to listed to
messages. I want to pass voicemail box id, pincode and email via SIP
headers, as I have them already loaded in Kamailio config script,
avoiding another database lookup from freeswitch lua script.
Cheers,
Daniel
--
Daniel-Constantin Mierla - http://www.asipto.com
http://twitter.com/#!/miconda - http://www.linkedin.com/in/miconda
Kamailio Advanced Training, Berlin, Nov 5-8, 2012 - http://asipto.com/u/kat
Kamailio Advanced Training, Miami, USA, Nov 12-14, 2012 - http://asipto.com/u/katu
-------------- next part --------------
>From 6aea6c27f78a7799b440a06be4c158f6f61bf601 Mon Sep 17 00:00:00 2001
From: Daniel-Constantin Mierla <miconda at gmail.com>
Date: Thu, 20 Sep 2012 22:05:12 +0200
Subject: [PATCH] mod_voicemail: allow passing extra headers in event message
to locate user directory
- channel variable 'locate_event_extra_headers' can be set to the prefix
of channel variable names that have to be added as extra headers to
the event message fired to locate the user directory
- the header name is variable name and the header body is variable value
- here is an example of adding the value of SIP header X-Data as event
message header X-Event-Data:
<action application="set" data="locate_event_extra_headers=X-Event-" />
<action application="set" data="X-Event-Data=${sip_h_X-Data}" />
- the headers are added for leave voice message and listen voice message
events
- the extra headers can be useful on building user directory
dinamically, e.g., via a Lua script
---
src/mod/applications/mod_voicemail/mod_voicemail.c | 30 ++++++++++++++++++++
1 file changed, 30 insertions(+)
diff --git a/src/mod/applications/mod_voicemail/mod_voicemail.c b/src/mod/applications/mod_voicemail/mod_voicemail.c
index 51677c5..9509ea7 100644
--- a/src/mod/applications/mod_voicemail/mod_voicemail.c
+++ b/src/mod/applications/mod_voicemail/mod_voicemail.c
@@ -161,6 +161,31 @@ struct vm_profile {
typedef struct vm_profile vm_profile_t;
+static int voicemail_event_message_add_extra_headers(switch_channel_t *channel, switch_event_t *event,
+ const char *prefix)
+{
+ switch_event_header_t *hi = NULL;
+ int n;
+
+ if(channel==NULL || event==NULL || prefix==NULL)
+ return 0;
+
+ n = 0;
+ if ((hi = switch_channel_variable_first(channel))) {
+ for (; hi; hi = hi->next) {
+ const char *name = (char *) hi->name;
+ char *value = (char *) hi->value;
+
+ if (!strncasecmp(name, prefix, strlen(prefix))) {
+ switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, name, value);
+ }
+ }
+ switch_channel_variable_last(channel);
+ }
+
+ return n;
+}
+
switch_cache_db_handle_t *vm_get_db_handle(vm_profile_t *profile)
{
switch_cache_db_connection_options_t options = { {0} };
@@ -2326,6 +2351,8 @@ static void voicemail_check_main(switch_core_session_t *session, vm_profile_t *p
switch_event_create(¶ms, SWITCH_EVENT_GENERAL);
switch_assert(params);
+ voicemail_event_message_add_extra_headers(channel, params,
+ switch_channel_get_variable(channel, "locate_event_extra_headers"));
switch_event_add_header_string(params, SWITCH_STACK_BOTTOM, "action", "voicemail-lookup");
switch_event_add_header_string(params, SWITCH_STACK_BOTTOM, "destination_number", caller_profile->destination_number);
switch_event_add_header_string(params, SWITCH_STACK_BOTTOM, "caller_id_number", caller_id_number);
@@ -3157,6 +3184,7 @@ static switch_status_t voicemail_inject(const char *data, switch_core_session_t
return status;
}
+
static switch_status_t voicemail_leave_main(switch_core_session_t *session, vm_profile_t *profile, const char *domain_name, const char *id)
{
switch_channel_t *channel = switch_core_session_get_channel(session);
@@ -3220,6 +3248,8 @@ static switch_status_t voicemail_leave_main(switch_core_session_t *session, vm_p
switch_event_create(&locate_params, SWITCH_EVENT_REQUEST_PARAMS);
switch_assert(locate_params);
switch_event_add_header_string(locate_params, SWITCH_STACK_BOTTOM, "action", "voicemail-lookup");
+ voicemail_event_message_add_extra_headers(channel, locate_params,
+ switch_channel_get_variable(channel, "locate_event_extra_headers"));
if (switch_xml_locate_user_merged("id", id, domain_name, switch_channel_get_variable(channel, "network_addr"),
&x_user, locate_params) == SWITCH_STATUS_SUCCESS) {
--
1.7.9.2
Join us at ClueCon 2011 Aug 9-11, 2011
More information about the FreeSWITCH-users
mailing list