Add missing module dependencies to various res_sip* modules
[asterisk/asterisk.git] / res / res_sip_dtmf_info.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2013, Digium, Inc.
5  *
6  * Jason Parker <jparker@digium.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_sip</depend>
22         <depend>res_sip_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/res_sip.h"
32 #include "asterisk/res_sip_session.h"
33 #include "asterisk/module.h"
34
35 static int dtmf_info_incoming_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
36 {
37         int res = 0;
38         pjsip_msg_body *body = rdata->msg_info.msg->body;
39
40         pjsip_tx_data *tdata;
41
42         char buf[body->len];
43         char *cur = buf;
44         char *line;
45
46         char event = '\0';
47         unsigned int duration = 0;
48
49         if (pj_strcmp2(&body->content_type.type, "application") ||
50             pj_strcmp2(&body->content_type.subtype, "dtmf-relay")) {
51                 return 0;
52         }
53
54         body->print_body(body, buf, body->len);
55
56         while ((line = strsep(&cur, "\r\n"))) {
57                 char *c;
58
59                 if (!(c = strchr(line, '='))) {
60                         continue;
61                 }
62                 *c++ = '\0';
63
64                 c = ast_skip_blanks(c);
65
66                 if (!strcasecmp(line, "signal")) {
67                         if (c[0] == '!' || c[0] == '*' || c[0] == '#' ||
68                             ('0' <= c[0] && c[0] <= '9') ||
69                             ('A' <= c[0] && c[0] <= 'D') ||
70                             ('a' <= c[0] && c[0] <= 'd')) {
71                                 event = c[0];
72                         } else {
73                                 ast_log(LOG_ERROR, "Invalid DTMF event signal in INFO message.\n");
74                                 res = -1;
75                                 break;
76                         }
77                 } else if (!strcasecmp(line, "duration")) {
78                         sscanf(c, "%30u", &duration);
79                 }
80         }
81
82         if (!duration) {
83                 duration = 100;
84         }
85
86         if (event == '!') {
87                 struct ast_frame f = { AST_FRAME_CONTROL, { AST_CONTROL_FLASH, } };
88
89                 ast_queue_frame(session->channel, &f);
90         } else if (event != '\0') {
91                 struct ast_frame f = { AST_FRAME_DTMF, };
92                 f.len = duration;
93                 f.subclass.integer = event;
94
95                 ast_queue_frame(session->channel, &f);
96         } else {
97                 res = -1;
98         }
99
100         if (pjsip_dlg_create_response(session->inv_session->dlg, rdata, !res ? 200 : 500, NULL, &tdata) == PJ_SUCCESS) {
101                 struct pjsip_transaction *tsx = pjsip_rdata_get_tsx(rdata);
102
103                 pjsip_dlg_send_response(session->inv_session->dlg, tsx, tdata);
104         }
105
106         return res;
107 }
108
109 static struct ast_sip_session_supplement dtmf_info_supplement = {
110         .method = "INFO",
111         .incoming_request = dtmf_info_incoming_request,
112 };
113
114 static int load_module(void)
115 {
116         ast_sip_session_register_supplement(&dtmf_info_supplement);
117         return AST_MODULE_LOAD_SUCCESS;
118 }
119
120 static int unload_module(void)
121 {
122         ast_sip_session_unregister_supplement(&dtmf_info_supplement);
123         return 0;
124 }
125
126 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "SIP DTMF INFO Support",
127                 .load = load_module,
128                 .unload = unload_module,
129                 .load_pri = AST_MODPRI_APP_DEPEND,
130                );