40 #include "asterisk/res_pjsip.h"
41 #include "asterisk/res_pjsip_session.h"
44 #define DATASTORE_NAME "call_feature_send_to_vm_datastore"
46 #define SEND_TO_VM_HEADER "PJSIP_HEADER(add,X-Digium-Call-Feature)"
47 #define SEND_TO_VM_HEADER_VALUE "feature_send_to_vm"
49 #define SEND_TO_VM_REDIRECT "REDIRECTING(reason)"
50 #define SEND_TO_VM_REDIRECT_VALUE "send_to_vm"
51 #define SEND_TO_VM_REDIRECT_QUOTED_VALUE "\"" SEND_TO_VM_REDIRECT_VALUE "\""
53 static void send_response(
struct ast_sip_session *session,
int code,
struct pjsip_rx_data *rdata)
57 if (pjsip_dlg_create_response(session->
inv_session->dlg, rdata, code, NULL, &tdata) == PJ_SUCCESS) {
58 struct pjsip_transaction *tsx = pjsip_rdata_get_tsx(rdata);
60 pjsip_dlg_send_response(session->
inv_session->dlg, tsx, tdata);
64 static void channel_cleanup_wrapper(
void *data)
71 .
type =
"REFER call feature info",
72 .destroy = channel_cleanup_wrapper,
75 static pjsip_param *get_diversion_reason(pjsip_fromto_hdr *hdr)
77 static const pj_str_t reason_str = {
"reason", 6 };
78 return pjsip_param_find(&hdr->other_param, &reason_str);
81 static pjsip_fromto_hdr *get_diversion_header(pjsip_rx_data *rdata)
83 static const pj_str_t from_str = {
"From", 4 };
84 static const pj_str_t diversion_str = {
"Diversion", 9 };
86 pjsip_generic_string_hdr *hdr;
89 if (!(hdr = pjsip_msg_find_hdr_by_name(
90 rdata->msg_info.msg, &diversion_str, NULL))) {
94 pj_strdup_with_null(rdata->tp_info.pool, &value, &hdr->hvalue);
97 return pjsip_parse_hdr(rdata->tp_info.pool, &from_str, value.ptr,
98 pj_strlen(&value), NULL);
101 static int has_diversion_reason(pjsip_rx_data *rdata)
104 pjsip_fromto_hdr *hdr = get_diversion_header(rdata);
109 reason = get_diversion_reason(hdr);
111 && (!pj_stricmp2(&reason->value, SEND_TO_VM_REDIRECT_QUOTED_VALUE)
112 || !pj_stricmp2(&reason->value, SEND_TO_VM_REDIRECT_VALUE));
115 static int has_call_feature(pjsip_rx_data *rdata)
117 static const pj_str_t call_feature_str = {
"X-Digium-Call-Feature", 21 };
119 pjsip_generic_string_hdr *hdr = pjsip_msg_find_hdr_by_name(
120 rdata->msg_info.msg, &call_feature_str, NULL);
122 return hdr && !pj_stricmp2(&hdr->hvalue, SEND_TO_VM_HEADER_VALUE);
125 static int handle_incoming_request(
struct ast_sip_session *session,
struct pjsip_rx_data *rdata)
136 has_feature = has_call_feature(rdata);
137 has_reason = has_diversion_reason(rdata);
138 if (!has_feature && !has_reason) {
149 ast_log(LOG_WARNING,
"%s (%s) attempted to transfer to voicemail, "
150 "but was not in a two party bridge.\n",
152 ast_channel_name(session->
channel));
153 send_response(session, 400, rdata);
157 sip_session_datastore = ast_sip_session_alloc_datastore(
158 &call_feature_info, DATASTORE_NAME);
159 if (!sip_session_datastore) {
161 send_response(session, 500, rdata);
165 sip_session_datastore->
data = other_party;
167 if (ast_sip_session_add_datastore(session, sip_session_datastore)) {
168 ao2_ref(sip_session_datastore, -1);
169 send_response(session, 500, rdata);
175 SEND_TO_VM_HEADER_VALUE);
180 SEND_TO_VM_REDIRECT_VALUE);
183 ao2_ref(sip_session_datastore, -1);
187 static void handle_outgoing_response(
struct ast_sip_session *session,
struct pjsip_tx_data *tdata)
189 pjsip_status_line status = tdata->msg->line.status;
191 ast_sip_session_get_datastore(session, DATASTORE_NAME);
194 if (!feature_datastore) {
199 ast_sip_session_remove_datastore(session, DATASTORE_NAME);
202 if (status.code >= 300) {
203 target_chan = feature_datastore->
data;
207 ao2_ref(feature_datastore, -1);
212 .incoming_request = handle_incoming_request,
213 .outgoing_response = handle_outgoing_response,
216 static int load_module(
void)
218 ast_sip_session_register_supplement(&refer_supplement);
223 static int unload_module(
void)
225 ast_sip_session_unregister_supplement(&refer_supplement);
229 AST_MODULE_INFO(
ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER,
"PJSIP REFER Send to Voicemail Support",
230 .support_level = AST_MODULE_SUPPORT_CORE,
232 .unload = unload_module,
234 .requires =
"res_pjsip,res_pjsip_session",
Main Channel structure associated with a channel.
struct ast_sip_endpoint * endpoint
Asterisk main include file. File version handling, generic pbx functions.
#define ast_channel_unref(c)
Decrease channel reference count.
Structure for a data store type.
Structure for a data store object.
struct pjsip_inv_session * inv_session
A structure describing a SIP session.
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
#define ast_channel_cleanup(c)
Cleanup a channel reference.
const char * ast_sorcery_object_get_id(const void *object)
Get the unique identifier of a sorcery object.
struct ast_channel * channel
Core PBX routines and definitions.
A supplement to SIP message processing.
int pbx_builtin_setvar_helper(struct ast_channel *chan, const char *name, const char *value)
Add a variable to the channel variable stack, removing the most recently set value for the same name...
struct ast_channel * ast_channel_bridge_peer(struct ast_channel *chan)
Get the channel's bridge peer only if the bridge is two-party.
#define ASTERISK_GPL_KEY
The text the key() function should return.
Asterisk module definitions.