<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[Freeswitch-trunk][14391] </title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<div id="header">FreeSWITCH Subversion</div>
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://fisheye.freeswitch.org/changelog/FreeSWITCH?cs=14391">14391</a></dd>
<dt>Author</dt> <dd>cseket</dd>
<dt>Date</dt> <dd>2009-07-27 15:26:00 -0500 (Mon, 27 Jul 2009)</dd>
</dl>

<h3>Log Message</h3>
<pre>hunt dialplan before channel init, not general enough to submit on jira, but it would be nice to avoid duplicate routing logic, that already is in dialplan</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#freeswitchbranchescseketsrcmodendpointsmod_sofiamod_sofiac">freeswitch/branches/cseket/src/mod/endpoints/mod_sofia/mod_sofia.c</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="freeswitchbranchescseketsrcmodendpointsmod_sofiamod_sofiac"></a>
<div class="modfile"><h4>Modified: freeswitch/branches/cseket/src/mod/endpoints/mod_sofia/mod_sofia.c (14390 => 14391)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/branches/cseket/src/mod/endpoints/mod_sofia/mod_sofia.c        2009-07-27 20:18:20 UTC (rev 14390)
+++ freeswitch/branches/cseket/src/mod/endpoints/mod_sofia/mod_sofia.c        2009-07-27 20:26:00 UTC (rev 14391)
</span><span class="lines">@@ -35,6 +35,7 @@
</span><span class="cx"> 
</span><span class="cx"> /* Best viewed in a 160 x 60 VT100 Terminal or so the line below at least fits across your screen*/
</span><span class="cx"> /*************************************************************************************************************************************************************/
</span><ins>+
</ins><span class="cx"> #include &quot;mod_sofia.h&quot;
</span><span class="cx"> #include &quot;sofia-sip/sip_extra.h&quot;
</span><span class="cx"> SWITCH_MODULE_LOAD_FUNCTION(mod_sofia_load);
</span><span class="lines">@@ -142,6 +143,8 @@
</span><span class="cx">                 sofia_clear_flag_locked(tech_pvt, TFLAG_SIP_HOLD);
</span><span class="cx">         }
</span><span class="cx"> 
</span><ins>+        //        switch_channel_set_state(switch_core_session_get_channel(session), CS_INIT);
+
</ins><span class="cx">         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, &quot;%s SOFIA RESET\n&quot;, switch_channel_get_name(switch_core_session_get_channel(session)));
</span><span class="cx"> 
</span><span class="cx">         return SWITCH_STATUS_SUCCESS;
</span><span class="lines">@@ -153,10 +156,14 @@
</span><span class="cx">         private_object_t *tech_pvt = switch_core_session_get_private(session);
</span><span class="cx">         switch_assert(tech_pvt != NULL);
</span><span class="cx"> 
</span><ins>+        //        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;HIBERNATE???\n&quot;);
+
</ins><span class="cx">         if (!sofia_test_flag(tech_pvt, TFLAG_HOLD_LOCK)) {
</span><span class="cx">                 sofia_clear_flag_locked(tech_pvt, TFLAG_SIP_HOLD);
</span><span class="cx">         }
</span><span class="cx"> 
</span><ins>+        //        switch_channel_set_state(switch_core_session_get_channel(session), CS_INIT);
+
</ins><span class="cx">         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, &quot;%s SOFIA HIBERNATE\n&quot;, switch_channel_get_name(switch_core_session_get_channel(session)));
</span><span class="cx"> 
</span><span class="cx">         return SWITCH_STATUS_SUCCESS;
</span><span class="lines">@@ -2532,6 +2539,94 @@
</span><span class="cx">         return SWITCH_STATUS_SUCCESS;
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+
+
+switch_status_t prehunt_dialplan(switch_core_session_t *session, switch_event_t *var_event, switch_caller_profile_t *outbound_profile, switch_caller_profile_t *hunt_profile, char **endpoint_name)
+{
+        switch_dialplan_interface_t *dialplan_interface = NULL;
+        switch_caller_extension_t *extension = NULL;
+        switch_caller_profile_t *new_profile = NULL;
+        char *dparg = NULL;
+        char *expanded_data = NULL;
+        switch_channel_t *channel = switch_core_session_get_channel(session);
+        char *data, *ext, *dp, *context;
+        char *app;
+
+
+        if (!(dialplan_interface = switch_loadable_module_get_dialplan_interface(hunt_profile-&gt;dialplan))) {
+                return SWITCH_STATUS_FALSE;
+        }
+                                        
+        extension = dialplan_interface-&gt;hunt_function(session, dparg, hunt_profile);
+                                        
+        if (extension) {
+                while (extension-&gt;current_application) {
+                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, &quot;application: %s data: %s\n&quot;, extension-&gt;current_application-&gt;application_name, extension-&gt;current_application-&gt;application_data);
+
+                        app = extension-&gt;current_application-&gt;application_name;
+
+                        expanded_data = switch_channel_expand_variables(channel, extension-&gt;current_application-&gt;application_data);
+                        data = switch_core_strdup(switch_core_session_get_pool(session), expanded_data);
+                        if (expanded_data != extension-&gt;current_application-&gt;application_data) {
+                                switch_safe_free(expanded_data);
+                        }
+
+                        if (!strcasecmp(app, &quot;execute_extension&quot;)) {
+                                new_profile = switch_caller_profile_clone(session, hunt_profile);
+
+                                ext = data;
+                                dp = NULL;
+                                context = NULL;
+
+                                if ((dp = strchr(ext, ' '))) {
+                                        *dp++ = '\0';
+                                        if ((context = strchr(dp, ' '))) {
+                                                *context++ = '\0';
+                                        }
+                                }
+                                new_profile-&gt;destination_number = ext;
+                                new_profile-&gt;dialplan = dp;
+                                new_profile-&gt;context = context;
+
+                                prehunt_dialplan(session, var_event, outbound_profile, new_profile, endpoint_name);
+                                
+                        } else if (!strcasecmp(app, &quot;set&quot;) || !strcasecmp(app, &quot;export&quot;)) {
+                                char *var = data;
+                                char *value = NULL;
+                                if ((value = strchr(var, '='))) {
+                                        *value++ = '\0';
+                                }
+
+                                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, &quot;Set var: '%s' value: '%s'\n&quot;, var, value);
+                                switch_channel_set_variable(channel, var, value);
+                        } else if (!strcasecmp(app, &quot;bridge&quot;)) {
+                                char *dest_to;
+                                char *p = data;
+                                
+                                if ((dest_to = strchr(data, '/'))) {
+                                        *dest_to++ = '\0';
+                                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, &quot;Rewrite destination_number: %s\n&quot;, dest_to);
+                                        outbound_profile-&gt;destination_number = switch_core_strdup(outbound_profile-&gt;pool, dest_to);
+                                }
+
+                                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, &quot;data: %s\n&quot;, data);
+                                if ((p = strchr(data, '}'))) {
+                                        *p++ = '\0';
+                                        *endpoint_name = switch_core_strdup(switch_core_session_get_pool(session), p);
+                                } else {
+                                        *endpoint_name = switch_core_strdup(switch_core_session_get_pool(session), data);
+                                }
+                                return SWITCH_STATUS_SUCCESS;
+                        }
+                        extension-&gt;current_application = extension-&gt;current_application-&gt;next;
+
+                }
+        }
+
+        return SWITCH_STATUS_SUCCESS;
+
+}
+
</ins><span class="cx"> static switch_call_cause_t sofia_outgoing_channel(switch_core_session_t *session, switch_event_t *var_event,
</span><span class="cx">                                                                                                   switch_caller_profile_t *outbound_profile, switch_core_session_t **new_session,
</span><span class="cx">                                                                                                   switch_memory_pool_t **pool, switch_originate_flag_t flags)
</span><span class="lines">@@ -2545,6 +2640,7 @@
</span><span class="cx">         switch_channel_t *nchannel;
</span><span class="cx">         char *host = NULL, *dest_to = NULL;
</span><span class="cx">         const char *hval = NULL;
</span><ins>+        char *prehunt_dp = NULL;
</ins><span class="cx"> 
</span><span class="cx">         *new_session = NULL;
</span><span class="cx"> 
</span><span class="lines">@@ -2553,21 +2649,60 @@
</span><span class="cx">                 goto error;
</span><span class="cx">         }
</span><span class="cx"> 
</span><ins>+        
+        nchannel = switch_core_session_get_channel(nsession);
+        caller_profile = switch_caller_profile_clone(nsession, outbound_profile);
+        switch_channel_set_caller_profile(nchannel, caller_profile);
+
+        // DEBUG
+        if (var_event-&gt;headers) {
+                switch_event_header_t *event_header = NULL;
+        for (event_header = var_event-&gt;headers; event_header; event_header = event_header-&gt;next) {
+            switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, &quot;Header info: %s =&gt; %s\n&quot;, event_header-&gt;name, event_header-&gt;value);
+        }
+        }
+
+        if ((prehunt_dp = switch_event_get_header(var_event, &quot;sofia_prehunt_dialplan&quot;))) {
+                switch_caller_profile_t *hunt_profile = NULL;
+                char *context = switch_event_get_header(var_event, &quot;sofia_prehunt_context&quot;);
+                const char *caller_id_name, *caller_id_number;
+                char *endpoint_name;
+
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, &quot;Pre-Hunt Diaplan\n&quot;);
+                
+                hunt_profile = switch_caller_profile_clone(nsession, caller_profile);
+
+                hunt_profile-&gt;dialplan = switch_core_sprintf(hunt_profile-&gt;pool, prehunt_dp);
+                if (context) {
+                        hunt_profile-&gt;context = switch_core_sprintf(hunt_profile-&gt;pool, context);
+                }
+                
+                prehunt_dialplan(nsession, var_event, outbound_profile, hunt_profile, &amp;endpoint_name);
+
+                if ((caller_id_name = switch_channel_get_variable(nchannel, &quot;effective_caller_id_name&quot;))) {
+                        caller_profile-&gt;caller_id_name = switch_core_sprintf(caller_profile-&gt;pool, caller_id_name);
+                        outbound_profile-&gt;caller_id_name = switch_core_sprintf(outbound_profile-&gt;pool, caller_id_name);
+                }
+                if ((caller_id_number = switch_channel_get_variable(nchannel, &quot;effective_caller_id_number&quot;))) {
+                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, &quot;caller id number: %s\n&quot;, caller_id_number);
+                        caller_profile-&gt;caller_id_number = switch_core_sprintf(caller_profile-&gt;pool, caller_id_number);
+                        outbound_profile-&gt;caller_id_number = switch_core_sprintf(outbound_profile-&gt;pool, caller_id_number);
+                }
+        }
+
</ins><span class="cx">         if (!(tech_pvt = (struct private_object *) switch_core_session_alloc(nsession, sizeof(*tech_pvt)))) {
</span><span class="cx">                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, &quot;Error Creating Session\n&quot;);
</span><span class="cx">                 goto error;
</span><span class="cx">         }
</span><span class="cx">         switch_mutex_init(&amp;tech_pvt-&gt;flag_mutex, SWITCH_MUTEX_NESTED, switch_core_session_get_pool(nsession));
</span><span class="cx">         switch_mutex_init(&amp;tech_pvt-&gt;sofia_mutex, SWITCH_MUTEX_NESTED, switch_core_session_get_pool(nsession));
</span><del>-
</del><ins>+        
</ins><span class="cx">         data = switch_core_session_strdup(nsession, outbound_profile-&gt;destination_number);
</span><span class="cx">         if ((dest_to = strchr(data, '^'))) {
</span><span class="cx">                 *dest_to++ = '\0';
</span><span class="cx">         }
</span><span class="cx">         profile_name = data;
</span><span class="cx"> 
</span><del>-        nchannel = switch_core_session_get_channel(nsession);
-
</del><span class="cx">         if ((hval = switch_event_get_header(var_event, &quot;sip_invite_to_uri&quot;))) {
</span><span class="cx">                 dest_to = switch_core_session_strdup(nsession, hval);
</span><span class="cx">         }
</span><span class="lines">@@ -2752,15 +2887,13 @@
</span><span class="cx">         }
</span><span class="cx">         switch_channel_set_variable(nchannel, &quot;sip_destination_url&quot;, tech_pvt-&gt;dest);
</span><span class="cx"> 
</span><del>-        caller_profile = switch_caller_profile_clone(nsession, outbound_profile);
</del><span class="cx">         caller_profile-&gt;destination_number = switch_core_strdup(caller_profile-&gt;pool, dest);
</span><del>-        switch_channel_set_caller_profile(nchannel, caller_profile);
</del><span class="cx">         switch_channel_set_flag(nchannel, CF_OUTBOUND);
</span><span class="cx">         sofia_set_flag_locked(tech_pvt, TFLAG_OUTBOUND);
</span><span class="cx">         sofia_clear_flag_locked(tech_pvt, TFLAG_LATE_NEGOTIATION);
</span><span class="cx">         if (switch_channel_get_state(nchannel) == CS_NEW) {
</span><span class="cx">                 switch_channel_set_state(nchannel, CS_INIT);
</span><del>-        }
</del><ins>+        } 
</ins><span class="cx">         tech_pvt-&gt;caller_profile = caller_profile;
</span><span class="cx">         *new_session = nsession;
</span><span class="cx">         cause = SWITCH_CAUSE_SUCCESS;
</span></span></pre>
</div>
</div>
<div id="footer">See you at ClueCon</div>

</body>
</html>