| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 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 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
static char *app = "StoreDTMF"; |
| 80 |
|
| 81 |
|
| 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 |
|
| 104 |
static const struct ast_datastore_info dtmf_store_datastore = { |
| 105 |
.type = "dtmf_store", |
| 106 |
.destroy = datastore_destroy_cb |
| 107 |
}; |
| 108 |
|
| 109 |
|
| 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 |
|
| 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 |
|
| 163 |
if (event == AST_FRAMEHOOK_EVENT_READ && framedata->rx_var) { |
| 164 |
varname = framedata->rx_var; |
| 165 |
} else if (event == AST_FRAMEHOOK_EVENT_WRITE && framedata->tx_var) { |
| 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 |
|
| 176 |
return f; |
| 177 |
} |
| 178 |
|
| 179 |
pbx_substitute_variables_helper(chan, varnamesub, currentdata, 511); |
| 180 |
|
| 181 |
if (ast_strlen_zero(currentdata)) { |
| 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); |
| 188 |
return f; |
| 189 |
} else { |
| 190 |
char newdata[len + 2]; |
| 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 |
|
| 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"); |