OK heres what we have done
extensions_override_freepbx.conf
Fix to set the P-Preferred-Identityheader of the outbound call
[func-apply-sipheaders]
include => func-apply-sipheaders-custom
exten => s,1,Noop(Applying SIP Headers to channel ${CHANNEL} ${CALLERIDNUMINTERNAL} ${CALLERID(number)} ${CDR(outbound_cnum)} ${CALLERID(num)} ${CALLERID(all)})
exten => s,n,Set(localchan=${CUT(CHANNEL,/,2)})
exten => s,n,Set(DialMCEXT=${CUT(localchan,-,1)})
exten => s,n,Set(CHANNEL(hangup_handler_push)=app-missedcall-hangup,${DialMCEXT},1)
exten => s,n,Set(TECH=${CUT(CHANNEL,/,1)})
;we set the callerid length variable so we can check its external caller id
;as could be internatonal had to have flexability
exten => s,n,Set(PPI=${LEN(${CALLERIDNUMINTERNAL})})
exten => s,n,Noop(${PPI})
exten => s,n,Set(SIPHEADERKEYS=${HASHKEYS(SIPHEADERS)})
;We check that its PJSIP and the correct trunk and the callerid to set to a length longer than extensions before setting the PPI
exten => s,n,ExecIf($[“${TECH}” = “PJSIP” & “${DialMCEXT}” = “TRUNK-USERNAME” & ${PPI} > 9]?Set(PJSIP_HEADER(add,P-Preferred-Identity)=sip:${[CALLERIDNUMINTERNAL}@sv2.sip.vu](mailto:CALLERIDNUMINTERNAL}@sv2.sip.vu)))
exten => s,n,While($[“${SET(sipkey=${SHIFT(SIPHEADERKEYS)})}” != “”])
exten => s,n,Set(sipheader=${HASH(SIPHEADERS,${sipkey})})
exten => s,n,ExecIf($[“${sipheader}” = “unset” & “${TECH}” = “SIP”]?SIPRemoveHeader(${sipkey}:))
exten => s,n,ExecIf($[“${sipheader}” = “unset” & “${TECH}” = “PJSIP”]?Set(PJSIP_HEADER(remove,${sipkey})=))
exten => s,n,ExecIf($[“${sipheader}” != “unset” & “${sipkey}” = “Alert-Info” & ${REGEX(“^<[^>]>" ${sipheader})} != 1 & ${REGEX(“;info=” ${sipheader})} != 1]?Set(sipheader=<http://127.0.0.1>;info=${sipheader}))
exten => s,n,ExecIf($[“${sipheader}” != “unset” & “${sipkey}” = “Alert-Info” & ${REGEX("^<[^>]>” ${sipheader})} != 1]?Set(sipheader=<http://127.0.0.1>${sipheader}))
exten => s,n,ExecIf($[“${TECH}” = “SIP” & “${sipheader}” != “unset”]?SIPAddHeader(${sipkey}:${sipheader}))
exten => s,n,ExecIf($[“${TECH}” = “PJSIP” & “${sipheader}” != “unset”]?Set(PJSIP_HEADER(add,${sipkey})=${sipheader}))
exten => s,n,EndWhile
exten => s,n,Return()
;–== end of [func-apply-sipheaders] ==–;
extensions_custom.conf
;fix to get DDI from To header of PJSIP
[custom-get-did-from-sip]
;exten => _.,1,Goto(from-trunk,${CUT(CUT(SIP_HEADER(To),:,2),>,1)},1)
exten => _0X.,1,Goto(from-pstn,${CUT(CUT(PJSIP_HEADER(read,To),@,1),:,2)},1)
Other notes.
In Freepbx make sure that the trunks “Outbound CallerID” is to a valid DDI and that “CID Options” is set to “Block Foreign CIDs” which
sounds counter intuitive but important to preserve the PIA sip header as a valid “voipunlimited” number, You can also set the route caller ID to “overide extension” caller id
if you wish , this does not effect calls forwarding through the system
This code is primarily for passing callers callerID through the system to a remote party call center for example