Using LUA Script to Allow SIP-Based Phones to Dial from the CUCM Corporate Directory

Posted by Eric Lavoie on Mar 29, 2017 7:01:00 AM

Using LUA Script.png

If you sync your Cisco Unified Communications Manager (CUCM) to an enterprise directory, such as Microsoft Active Directory, and the phone format is entered in a fancy “human-friendly” way, users with SIP-based phones will have some issues to dial.

In my last case with a customer, the phone number format in the corporate directory had been formatted as follows: +1 514-940-1480 x5661.

From SCCP phones and Jabber, this string is interpreted, and the resulting number is respectively +151494014805661 or +1514940148095661 (note the extra “9” digit between the main number and the extension).

With SIP-based phones, there is no parsing before the call is initiated with CUCM. At the time of writing, it is still unclear whether this is a bug or is in-line with the RFC.

Given the elusive answers provided by TAC on this matter, I decided to leverage the "SIP Normalization" feature of CUCM to workaround this situation and achieve my goal of allowing SIP-based phones to dial numbers from within the CUCM Corporate Directory.

Below is a typical INVITE sent by the phone when dialing "+1 514-940-1480 x5661" from the Corporate Directory in CUCM.


New Call-to-action


Spaces are replaced by "%20", hyphens and the letter x are sent as-is. Obviously, there is no way in CUCM to create a translation pattern which would match this string.

Via: SIP/2.0/TCP;branch=z9hG4bK7673093f
From: "Test phone" <>;tag=00a289fbb820000f2624e8cf-1f9deb31
To: <>
Call-ID: 00a289fb-b8200004-5f91f053-241493e7@
Max-Forwards: 70
With the help of a LUA script applied on a SIP Profile, which is in turn applied to the SIP-based phones, we have a resulting INVITE looking like this:
Via: SIP/2.0/TCP;branch=z9hG4bK7673093f
From: "Test phone" <>;tag=00a289fbb820000f2624e8cf-1f9deb31
To: <>
Call-ID: 00a289fb-b8200004-5f91f053-241493e7@
Max-Forwards: 70
As you can see, the number is now routable by CUCM. The LUA script I have developed to remove the unwanted characters can be found below. You can be as fancy as you want with adapting this script to your particular needs.

function M.inbound_INVITE(msg)
      --[[  Extract data from SIP INVITE  --]]
      local to = msg:getHeader("To")
      local method, ruri, ver = msg:getRequestLine()
      --[[  Isolate both side of "@" in variables  --]]
      local to_left_side, to_right_side = string.match(to, "<sip:(.*)@(.*)")
      local ruri_left_side, ruri_right_side = string.match(ruri, "sip:(.*)@(.*)")

      --[[  Search and replace for "To" Header and Request URI --]]
      to_left_side = string.gsub(to_left_side , "%%20", "")
      to_left_side = string.gsub(to_left_side , "-", "")
      to_left_side = string.gsub(to_left_side , "x", "")
      ruri_left_side = string.gsub(ruri_left_side , "%%20", "")
      ruri_left_side = string.gsub(ruri_left_side , "-", "")
      ruri_left_side = string.gsub(ruri_left_side , "x", "")

      --[[  If the left side of "@" contain only numbers and begin OR not begin
                  with "+", then update the "To" Header and Request URI  --]]
      if string.find(to_left_side, "^+?%d*$") then
            msg:modifyHeader("To", "<sip:" .. to_left_side .. "@" .. to_right_side)

      if string.find(ruri_left_side, "^+?%d*$") then
            msg:setRequestUri("sip:" .. ruri_left_side .. "@" .. ruri_right_side)
return M

If this blog was helpful

Subscribe to the Stack8 Blog

Topics: Cisco Unified Communications Manager, LUA script, SIP Profile, corporate directory, normalization script

Don’t miss out. Expert advice straight to your inbox!

Insightful tips, troubleshooting and solutions for your everyday Unified Communications challenges from our team of experts. You can look forward to:

  • Weekly UC tips;
  • Cisco Unified Communications insights;
  • UCCX - Contact Center insights;
  • Network and Security insights;
  • Cisco Release notes and Product reviews.

Posts by Topic

see all
Join us for free live demo

Recent Posts