Index: funcs/func_realtime.c =================================================================== --- funcs/func_realtime.c (revision 147591) +++ funcs/func_realtime.c (working copy) @@ -37,6 +37,28 @@ #include "asterisk/utils.h" #include "asterisk/app.h" +AST_THREADSTORAGE(buf1); +AST_THREADSTORAGE(buf2); +AST_THREADSTORAGE(buf3); + +static char *hash_escape(struct ast_str **str, const char *value) +{ + int len; + ast_str_reset(*str); + + if ((*str)->len < (len = strlen(value) * 2 + 1)) { + ast_str_make_space(str, len); + } + for (; *value; value++) { + if (*value == ',' || *value == '\\') { + (*str)->str[(*str)->used++] = '\\'; + } + (*str)->str[(*str)->used++] = *value; + } + (*str)->str[(*str)->used] = '\0'; + return (*str)->str; +} + static int function_realtime_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len) { struct ast_variable *var, *head; @@ -103,7 +125,7 @@ ); if (ast_strlen_zero(data)) { - ast_log(LOG_WARNING, "Syntax: REALTIME(family,fieldmatch,value,newcol) - missing argument!\n"); + ast_log(LOG_WARNING, "Syntax: %s(family,fieldmatch,value,newcol) - missing argument!\n", cmd); return -1; } @@ -124,6 +146,70 @@ return 0; } +static int realtimefield_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len) +{ + struct ast_variable *var, *head; + struct ast_str *escapebuf = ast_str_thread_get(&buf1, 16); + struct ast_str *fields = ast_str_thread_get(&buf2, 16); + struct ast_str *values = ast_str_thread_get(&buf3, 16); + int first; + AST_DECLARE_APP_ARGS(args, + AST_APP_ARG(family); + AST_APP_ARG(fieldmatch); + AST_APP_ARG(value); + AST_APP_ARG(fieldname); + ); + + if (ast_strlen_zero(data)) { + ast_log(LOG_WARNING, "Syntax: %s(family,fieldmatch,value,fieldname) - missing argument!\n", cmd); + return -1; + } + + AST_STANDARD_APP_ARGS(args, data); + + if (args.argc < 4) { + ast_log(LOG_WARNING, "Syntax: %s(family,fieldmatch,value,fieldname) - missing argument!\n", cmd); + return -1; + } + + if (chan) { + ast_autoservice_start(chan); + } + + if (!(head = ast_load_realtime_all(args.family, args.fieldmatch, args.value, SENTINEL))) { + if (chan) { + ast_autoservice_stop(chan); + } + return -1; + } + + ast_str_reset(fields); + ast_str_reset(values); + + for (var = head; var; var = var->next) { + if (!strcmp(cmd, "REALTIME_FIELD")) { + if (!strcasecmp(var->name, args.fieldname)) { + ast_copy_string(buf, var->value, len); + break; + } + } else if (!strcmp(cmd, "REALTIME_HASH")) { + ast_str_append(&fields, 0, "%s%s", first ? "" : ",", hash_escape(&escapebuf, var->name)); + ast_str_append(&values, 0, "%s%s", first ? "" : ",", hash_escape(&escapebuf, var->value)); + first = 0; + } + } + ast_variables_destroy(head); + + pbx_builtin_setvar_helper(chan, "~ODBCFIELDS~", fields->str); + ast_copy_string(buf, values->str, len); + + if (chan) { + ast_autoservice_stop(chan); + } + + return 0; +} + static int function_realtime_store(struct ast_channel *chan, const char *cmd, char *data, const char *value) { int res = 0; @@ -248,6 +334,27 @@ .write = function_realtime_write, }; +struct ast_custom_function realtimefield_function = { + .name = "REALTIME_FIELD", + .synopsis = "RealTime Read/Write Functions", + .syntax = "REALTIME_FIELD(family,fieldmatch,value,field)", + .desc = +"This function will read or write values from/to a RealTime repository.\n" +"On a read, this function returns a single value.\n", + .read = realtimefield_read, + .write = function_realtime_write, +}; + +struct ast_custom_function realtimehash_function = { + .name = "REALTIME_HASH", + .synopsis = "RealTime Read/Write Functions", + .syntax = "REALTIME_HASH(family,fieldmatch,value)", + .desc = +"This function will read values from a RealTime repository and return them\n" +"in a format suitable to be assigned to a HASH function.\n", + .read = realtimefield_read, +}; + struct ast_custom_function realtime_store_function = { .name = "REALTIME_STORE", .synopsis = "RealTime Store Function", @@ -274,6 +381,8 @@ res |= ast_custom_function_unregister(&realtime_function); res |= ast_custom_function_unregister(&realtime_store_function); res |= ast_custom_function_unregister(&realtime_destroy_function); + res |= ast_custom_function_unregister(&realtimefield_function); + res |= ast_custom_function_unregister(&realtimehash_function); return res; } @@ -283,6 +392,8 @@ res |= ast_custom_function_register(&realtime_function); res |= ast_custom_function_register(&realtime_store_function); res |= ast_custom_function_register(&realtime_destroy_function); + res |= ast_custom_function_register(&realtimefield_function); + res |= ast_custom_function_register(&realtimehash_function); return res; }