title: FreeSWITCH dialplan to check if enduser is registered for WebRTC to SIP
body: <p>I am trying to use <a href="https://freeswitch.org/" rel="nofollow">FreesSWITCH</a> with the Mizu <a href="https://www.mizu-voip.com/Software/WebPhone.aspx" rel="nofollow">WebRTC to SIP client</a>.
  If the called user is registered to FreesSWITCH than the call should be routed to the user. Otherwise the call should be routed to outbound SIP server. </p>
  
  <p>The problem is that I am unable to find out from the FreesSWITCH dialplan if user is currently registered or not. I have the following in the default dialplan:</p>
  
  <pre><code>&lt;!-- If the user doesn't exist, forward to outbound SIP --&gt;
  &lt;extension name="check_user"&gt;
      &lt;condition field="${user_exists(id ${destination_number} $${domain})}" expression="false"&gt;
          &lt;action application="bridge" data="sofia/gateway/asterisk/${destination_number}"/&gt;
      &lt;/condition&gt;
  &lt;/extension&gt;
  
  &lt;!-- If the user is registered, forward the call to them --&gt;
  &lt;extension name="check_user_registered"&gt;
      &lt;condition field="${sofia_contact(profile/${destination_number}@$${domain})}" expression="(.+)"&gt;
          &lt;action application="bridge" data="$1" /&gt;
      &lt;/condition&gt;
  &lt;/extension&gt;
  
  &lt;!-- Otherwise forward to outbound SIP --&gt;
  &lt;extension name="outbound"&gt;
      &lt;condition field="destination_number" expression="^.*$"&gt;
          &lt;action application="bridge" data="sofia/gateway/asterisk/${destination_number}"/&gt;
      &lt;/condition&gt;
  &lt;/extension&gt;
  </code></pre>
  
  <p>However the "check_user_registered" rule seems to be passed even if the user is not actually registered from the Webphone, so the FreesSWITCH will go to send the call to the user, but then finds out that it is not registered and the call fails. (I think that WebRTC doesn't matter here, this would be the same for SIP to SIP calls).This is the relevant log:</p>
  
  <pre><code>Dialplan: sofia/internal/2233@81.12.74.77 Regex (PASS) [check_user_registered] ${sofia_contact(profile/${destination_number}@rtc.mizu-voip.com)}(error/user_not_registered) =~ /(.+)/ break=on-false
  Dialplan: sofia/internal/2233@81.12.74.77 Action bridge(error/user_not_registered)
  Freeswitch hangup with 480 Temporarily Unavailable / USER_NOT_REGISTERED
  </code></pre>
  
  <p>Do you know any reliable way to query from FreesSWITCH dialplan the user registered status?</p>