ec5f9be3e7cdacce04ff1583bbc100a2ab4f0234
[asterisk/asterisk.git] / res / res_pjsip_one_touch_record_info.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2013, malleable, llc.
5  *
6  * Sean Bright <sean@malleable.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*** MODULEINFO
20          <depend>pjproject</depend>
21          <depend>res_pjsip</depend>
22          <depend>res_pjsip_session</depend>
23          <support_level>core</support_level>
24 ***/
25
26 #include "asterisk.h"
27
28 #include <pjsip.h>
29 #include <pjsip_ua.h>
30
31 #include "asterisk/features.h"
32 #include "asterisk/res_pjsip.h"
33 #include "asterisk/res_pjsip_session.h"
34 #include "asterisk/module.h"
35 #include "asterisk/features_config.h"
36
37 static void send_response(struct ast_sip_session *session, int code, struct pjsip_rx_data *rdata)
38 {
39         pjsip_tx_data *tdata;
40
41         if (pjsip_dlg_create_response(session->inv_session->dlg, rdata, code, NULL, &tdata) == PJ_SUCCESS) {
42                 struct pjsip_transaction *tsx = pjsip_rdata_get_tsx(rdata);
43
44                 pjsip_dlg_send_response(session->inv_session->dlg, tsx, tdata);
45         }
46 }
47
48 static int handle_incoming_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
49 {
50         static const pj_str_t rec_str = { "Record", 6 };
51         pjsip_generic_string_hdr *record;
52         int feature_res;
53         char feature_code[AST_FEATURE_MAX_LEN];
54         const char *feature;
55         char *digit;
56
57         record = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &rec_str, NULL);
58
59         /* If we don't have Record header, we have nothing to do */
60         if (!record) {
61                 return 0;
62         }
63
64         if (!pj_stricmp2(&record->hvalue, "on")) {
65                 feature = session->endpoint->info.recording.onfeature;
66         } else if (!pj_stricmp2(&record->hvalue, "off")) {
67                 feature = session->endpoint->info.recording.offfeature;
68         } else {
69                 /* Don't send response because another module may handle this */
70                 return 0;
71         }
72
73         if (!session->channel) {
74                 send_response(session, 481, rdata);
75                 return 1;
76         }
77
78         /* Is this endpoint configured with One Touch Recording? */
79         if (!session->endpoint->info.recording.enabled || ast_strlen_zero(feature)) {
80                 send_response(session, 403, rdata);
81                 return 1;
82         }
83
84         ast_channel_lock(session->channel);
85         feature_res = ast_get_feature(session->channel, feature, feature_code, sizeof(feature_code));
86         ast_channel_unlock(session->channel);
87
88         if (feature_res || ast_strlen_zero(feature_code)) {
89                 send_response(session, 403, rdata);
90                 return 1;
91         }
92
93         for (digit = feature_code; *digit; ++digit) {
94                 struct ast_frame f = { AST_FRAME_DTMF, .subclass.integer = *digit, .len = 100 };
95                 ast_queue_frame(session->channel, &f);
96         }
97
98         send_response(session, 200, rdata);
99
100         return 1;
101 }
102
103 static struct ast_sip_session_supplement info_supplement = {
104         .method = "INFO",
105         .priority = AST_SIP_SUPPLEMENT_PRIORITY_FIRST,
106         .incoming_request = handle_incoming_request,
107 };
108
109 static int load_module(void)
110 {
111         CHECK_PJSIP_SESSION_MODULE_LOADED();
112
113         if (ast_sip_session_register_supplement(&info_supplement)) {
114                 ast_log(LOG_ERROR, "Unable to register One Touch Recording supplement\n");
115                 return AST_MODULE_LOAD_DECLINE;
116         }
117
118         return AST_MODULE_LOAD_SUCCESS;
119 }
120
121 static int unload_module(void)
122 {
123         ast_sip_session_unregister_supplement(&info_supplement);
124         return 0;
125 }
126
127 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP INFO One Touch Recording Support",
128         .support_level = AST_MODULE_SUPPORT_CORE,
129         .load = load_module,
130         .unload = unload_module,
131         .load_pri = AST_MODPRI_APP_DEPEND,
132 );