Removing registrar_expire from basic-pbx config
[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          <support_level>core</support_level>
23 ***/
24
25 #include "asterisk.h"
26
27 #include <pjsip.h>
28 #include <pjsip_ua.h>
29
30 #include "asterisk/features.h"
31 #include "asterisk/res_pjsip.h"
32 #include "asterisk/res_pjsip_session.h"
33 #include "asterisk/module.h"
34 #include "asterisk/features_config.h"
35
36 static void send_response(struct ast_sip_session *session, int code, struct pjsip_rx_data *rdata)
37 {
38         pjsip_tx_data *tdata;
39
40         if (pjsip_dlg_create_response(session->inv_session->dlg, rdata, code, NULL, &tdata) == PJ_SUCCESS) {
41                 struct pjsip_transaction *tsx = pjsip_rdata_get_tsx(rdata);
42
43                 pjsip_dlg_send_response(session->inv_session->dlg, tsx, tdata);
44         }
45 }
46
47 static int handle_incoming_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
48 {
49         static const pj_str_t rec_str = { "Record", 6 };
50         pjsip_generic_string_hdr *record;
51         int feature_res;
52         char feature_code[AST_FEATURE_MAX_LEN];
53         const char *feature;
54         char *digit;
55
56         record = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &rec_str, NULL);
57
58         /* If we don't have Record header, we have nothing to do */
59         if (!record) {
60                 return 0;
61         }
62
63         if (!pj_stricmp2(&record->hvalue, "on")) {
64                 feature = session->endpoint->info.recording.onfeature;
65         } else if (!pj_stricmp2(&record->hvalue, "off")) {
66                 feature = session->endpoint->info.recording.offfeature;
67         } else {
68                 /* Don't send response because another module may handle this */
69                 return 0;
70         }
71
72         if (!session->channel) {
73                 send_response(session, 481, rdata);
74                 return 1;
75         }
76
77         /* Is this endpoint configured with One Touch Recording? */
78         if (!session->endpoint->info.recording.enabled || ast_strlen_zero(feature)) {
79                 send_response(session, 403, rdata);
80                 return 1;
81         }
82
83         ast_channel_lock(session->channel);
84         feature_res = ast_get_feature(session->channel, feature, feature_code, sizeof(feature_code));
85         ast_channel_unlock(session->channel);
86
87         if (feature_res || ast_strlen_zero(feature_code)) {
88                 send_response(session, 403, rdata);
89                 return 1;
90         }
91
92         for (digit = feature_code; *digit; ++digit) {
93                 struct ast_frame f = { AST_FRAME_DTMF, .subclass.integer = *digit, .len = 100 };
94                 ast_queue_frame(session->channel, &f);
95         }
96
97         send_response(session, 200, rdata);
98
99         return 1;
100 }
101
102 static struct ast_sip_session_supplement info_supplement = {
103         .method = "INFO",
104         .priority = AST_SIP_SUPPLEMENT_PRIORITY_FIRST,
105         .incoming_request = handle_incoming_request,
106 };
107
108 static int load_module(void)
109 {
110         ast_sip_session_register_supplement(&info_supplement);
111
112         return AST_MODULE_LOAD_SUCCESS;
113 }
114
115 static int unload_module(void)
116 {
117         ast_sip_session_unregister_supplement(&info_supplement);
118         return 0;
119 }
120
121 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP INFO One Touch Recording Support",
122         .support_level = AST_MODULE_SUPPORT_CORE,
123         .load = load_module,
124         .unload = unload_module,
125         .load_pri = AST_MODPRI_APP_DEPEND,
126         .requires = "res_pjsip,res_pjsip_session",
127 );