Debian Codegraph @

apps/app_dtmfstore.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2021, Naveen Albert
5 *
6 * Naveen Albert <[email protected]>
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 /*! \file
20 *
21 * \brief Technology independent asynchronous DTMF collection
22 *
23 * \author Naveen Albert <[email protected]>
24 *
25 * \ingroup functions
26 *
27 */
28
29 /*** MODULEINFO
30 <support_level>extended</support_level>
31 ***/
32
33 #include "asterisk.h"
34
35 #include "asterisk/module.h"
36 #include "asterisk/channel.h"
37 #include "asterisk/pbx.h"
38 #include "asterisk/framehook.h"
39 #include "asterisk/app.h"
40 #include "asterisk/conversions.h"
41
42 /*** DOCUMENTATION
43 <application name="StoreDTMF" language="en_US">
44 <since>
45 <version>16.20.0</version>
46 <version>18.6.0</version>
47 </since>
48 <synopsis>
49 Stores DTMF digits transmitted or received on a channel.
50 </synopsis>
51 <syntax>
52 <parameter name="direction" required="true">
53 <para>Must be <literal>TX</literal> or <literal>RX</literal> to
54 store digits, or <literal>remove</literal> to disable.</para>
55 </parameter>
56 </syntax>
57 <description>
58 <para>The StoreDTMF function can be used to obtain digits sent in the
59 <literal>TX</literal> or <literal>RX</literal> direction of any channel.</para>
60 <para>The arguments are:</para>
61 <para><replaceable>var_name</replaceable>: Name of variable to which to append
62 digits.</para>
63 <para><replaceable>max_digits</replaceable>: The maximum number of digits to
64 store in the variable. Defaults to 0 (no maximum). After reading <literal>
65 maximum</literal> digits, no more digits will be stored.</para>
66 <example title="Store digits in CDR variable">
67 same => n,StoreDTMF(TX,CDR(digits))
68 </example>
69 <example title="Store up to 24 digits">
70 same => n,StoreDTMF(RX,testvar,24)
71 </example>
72 <example title="Disable digit collection">
73 same => n,StoreDTMF(remove)
74 </example>
75 </description>
76 </application>
77 ***/
78
79 static char *app = "StoreDTMF";
80
81 /*! \brief Private data structure used with the function's datastore */
82 struct dtmf_store_data {
83 int framehook_id;
84 char *rx_var;
85 char *tx_var;
86 int maxdigits;
87 };
88
89 static void datastore_destroy_cb(void *data) {
90 struct dtmf_store_data *d;
91 d = data;
92 if (d) {
93 if (d->rx_var) {
94 ast_free(d->rx_var);
95 }
96 if (d->tx_var) {
97 ast_free(d->tx_var);
98 }
99 ast_free(data);
100 }
101 }
102
103 /*! \brief The channel datastore the function uses to store state */
104 static const struct ast_datastore_info dtmf_store_datastore = {
105 .type = "dtmf_store",
106 .destroy = datastore_destroy_cb
107 };
108
109 /*! \internal \brief Store digits tx/rx on the channel */
110 static int remove_dtmf_store(struct ast_channel *chan)
111 {
112 struct ast_datastore *datastore = NULL;
113 struct dtmf_store_data *data;
114 SCOPED_CHANNELLOCK(chan_lock, chan);
115
116 datastore = ast_channel_datastore_find(chan, &dtmf_store_datastore, NULL);
117 if (!datastore) {
118 ast_log(AST_LOG_WARNING, "Cannot remove StoreDTMF from %s: StoreDTMF not currently enabled\n",
119 ast_channel_name(chan));
120 return -1;
121 }
122 data = datastore->data;
123
124 if (ast_framehook_detach(chan, data->framehook_id)) {
125 ast_log(AST_LOG_WARNING, "Failed to remove StoreDTMF framehook from channel %s\n",
126 ast_channel_name(chan));
127 return -1;
128 }
129
130 if (ast_channel_datastore_remove(chan, datastore)) {
131 ast_log(AST_LOG_WARNING, "Failed to remove StoreDTMF datastore from channel %s\n",
132 ast_channel_name(chan));
133 return -1;
134 }
135 ast_datastore_free(datastore);
136
137 return 0;
138 }
139
140 /*! \brief Frame hook that is called to intercept digit/undigit */
141 static struct ast_frame *dtmf_store_framehook(struct ast_channel *chan,
142 struct ast_frame *f, enum ast_framehook_event event, void *data)
143 {
144 char currentdata[512];
145 char varnamesub[64];
146 char *varname = NULL;
147 struct dtmf_store_data *framedata = data;
148 int len;
149
150 if (!f || !framedata) {
151 return f;
152 }
153
154 if ((event != AST_FRAMEHOOK_EVENT_WRITE) && (event != AST_FRAMEHOOK_EVENT_READ)) {
155 return f;
156 }
157
158 if (f->frametype != AST_FRAME_DTMF_END) {
159 return f;
160 }
161
162 /* If this is DTMF then store the digits */
163 if (event == AST_FRAMEHOOK_EVENT_READ && framedata->rx_var) { /* coming from source */
164 varname = framedata->rx_var;
165 } else if (event == AST_FRAMEHOOK_EVENT_WRITE && framedata->tx_var) { /* going to source */
166 varname = framedata->tx_var;
167 }
168
169 if (!varname) {
170 return f;
171 }
172
173 len = snprintf(varnamesub, sizeof(varnamesub), "${%s}", varname);
174 if (len >= sizeof(varnamesub)) {
175 /* Not enough room, bail out */
176 return f;
177 }
178
179 pbx_substitute_variables_helper(chan, varnamesub, currentdata, 511);
180 /* pbx_builtin_getvar_helper works for regular vars but not CDR vars */
181 if (ast_strlen_zero(currentdata)) { /* var doesn't exist yet */
182 ast_debug(3, "Creating new digit store: %s\n", varname);
183 }
184 len = strlen(currentdata);
185 if (framedata->maxdigits > 0 && len >= framedata->maxdigits) {
186 ast_debug(3, "Reached digit limit: %d\n", framedata->maxdigits);
187 remove_dtmf_store(chan); /* reached max digit count, stop now */
188 return f;
189 } else {
190 char newdata[len + 2]; /* one more char + terminator */
191 if (len > 0) {
192 ast_copy_string(newdata, currentdata, len + 2);
193 }
194 newdata[len] = (unsigned) f->subclass.integer;
195 newdata[len + 1] = '\0';
196 ast_debug(3, "Appending to digit store: now %s\n", newdata);
197 pbx_builtin_setvar_helper(chan, varname, newdata);
198 }
199 return f;
200 }
201
202 /*! \internal \brief Enable digit interception on the channel */
203 static int dtmfstore_exec(struct ast_channel *chan, const char *appdata)
204 {
205 struct ast_datastore *datastore;
206 struct dtmf_store_data *data;
207 static struct ast_framehook_interface digit_framehook_interface = {
208 .version = AST_FRAMEHOOK_INTERFACE_VERSION,
209 .event_cb = dtmf_store_framehook,
210 .disable_inheritance = 1,
211 };
212 char *parse = ast_strdupa(appdata);
213 AST_DECLARE_APP_ARGS(args,
214 AST_APP_ARG(direction);
215 AST_APP_ARG(varname);
216 AST_APP_ARG(maxdigits);
217 );
218 SCOPED_CHANNELLOCK(chan_lock, chan);
219 AST_STANDARD_APP_ARGS(args, parse);
220
221 if (ast_strlen_zero(appdata)) {
222 ast_log(AST_LOG_WARNING, "StoreDTMF requires an argument\n");
223 return -1;
224 }
225
226 if (!strcasecmp(args.direction, "remove")) {
227 return remove_dtmf_store(chan);
228 }
229
230 datastore = ast_channel_datastore_find(chan, &dtmf_store_datastore, NULL);
231 if (datastore) {
232 ast_log(AST_LOG_WARNING, "StoreDTMF already set on '%s'\n",
233 ast_channel_name(chan));
234 return 0;
235 }
236
237 datastore = ast_datastore_alloc(&dtmf_store_datastore, NULL);
238 if (!datastore) {
239 return -1;
240 }
241
242 data = ast_calloc(1, sizeof(*data));
243 if (!data) {
244 ast_datastore_free(datastore);
245 return -1;
246 }
247
248 digit_framehook_interface.data = data;
249
250 data->rx_var = NULL;
251 data->tx_var = NULL;
252 data->maxdigits = 0;
253
254 if (!strcasecmp(args.direction, "tx")) {
255 data->tx_var = ast_strdup(args.varname);
256 } else if (!strcasecmp(args.direction, "rx")) {
257 data->rx_var = ast_strdup(args.varname);
258 } else {
259 ast_log(LOG_ERROR, "Direction must be either RX or TX\n");
260 return -1;
261 }
262
263 if (!ast_strlen_zero(args.maxdigits)) {
264 if (ast_str_to_int(args.maxdigits,&(data->maxdigits))) {
265 ast_log(LOG_ERROR, "Invalid integer: %s\n", args.maxdigits);
266 return -1;
267 }
268 if (data->maxdigits < 0) {
269 ast_log(LOG_ERROR, "Invalid natural number: %d\n", data->maxdigits);
270 return -1;
271 } else if (data->maxdigits == 0) {
272 ast_log(LOG_WARNING, "No maximum digit count set\n");
273 }
274 }
275
276 data->framehook_id = ast_framehook_attach(chan, &digit_framehook_interface);
277 if (data->framehook_id < 0) {
278 ast_log(AST_LOG_WARNING, "Failed to attach StoreDTMF framehook to '%s'\n",
279 ast_channel_name(chan));
280 ast_datastore_free(datastore);
281 ast_free(data);
282 return -1;
283 }
284 datastore->data = data;
285
286 ast_channel_datastore_add(chan, datastore);
287
288 return 0;
289 }
290
291 static int unload_module(void)
292 {
293 return ast_unregister_application(app);
294 }
295
296 static int load_module(void)
297 {
298 return ast_register_application_xml(app, dtmfstore_exec);
299 }
300
301 AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Technology independent async DTMF storage");