2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2005-2006, BJ Weschke. All rights reserved.
6 * BJ Weschke <bweschke@btwtech.com>
8 * This code is released by the author with no restrictions on usage.
10 * See http://www.asterisk.org for more information about
11 * the Asterisk project. Please do not directly contact
12 * any of the maintainers of this project for assistance;
13 * the project provides a web site, mailing lists and IRC
14 * channels for your use.
20 * \brief REALTIME dialplan function
22 * \author BJ Weschke <bweschke@btwtech.com>
29 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
35 #include <sys/types.h>
37 #include "asterisk/file.h"
38 #include "asterisk/channel.h"
39 #include "asterisk/pbx.h"
40 #include "asterisk/options.h"
41 #include "asterisk/config.h"
42 #include "asterisk/module.h"
43 #include "asterisk/lock.h"
44 #include "asterisk/logger.h"
45 #include "asterisk/utils.h"
46 #include "asterisk/app.h"
48 static int function_realtime_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
50 struct ast_variable *var, *head;
54 AST_DECLARE_APP_ARGS(args,
56 AST_APP_ARG(fieldmatch);
62 if (ast_strlen_zero(data)) {
63 ast_log(LOG_WARNING, "Syntax: REALTIME(family,fieldmatch[,value[,delim1[,delim2]]]) - missing argument!\n");
67 AST_STANDARD_APP_ARGS(args, data);
74 head = ast_load_realtime_all(args.family, args.fieldmatch, args.value, NULL);
81 for (var = head; var; n++, var = var->next)
82 resultslen += strlen(var->name) + strlen(var->value);
83 /* add space for delimiters and final '\0' */
84 resultslen += n * (strlen(args.delim1) + strlen(args.delim2)) + 1;
86 out = ast_str_alloca(resultslen);
87 for (var = head; var; var = var->next)
88 ast_str_append(&out, 0, "%s%s%s%s", var->name, args.delim2, var->value, args.delim1);
89 ast_copy_string(buf, out->str, len);
94 static int function_realtime_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
97 AST_DECLARE_APP_ARGS(args,
99 AST_APP_ARG(fieldmatch);
104 if (ast_strlen_zero(data)) {
105 ast_log(LOG_WARNING, "Syntax: REALTIME(family,fieldmatch,value,newcol) - missing argument!\n");
109 AST_STANDARD_APP_ARGS(args, data);
111 res = ast_update_realtime(args.family, args.fieldmatch, args.value, args.field, (char *)value, NULL);
114 ast_log(LOG_WARNING, "Failed to update. Check the debug log for possible data repository related entries.\n");
120 struct ast_custom_function realtime_function = {
122 .synopsis = "RealTime Read/Write Functions",
123 .syntax = "REALTIME(family,fieldmatch[,value[,delim1[,delim2]]]) on read\n"
124 "REALTIME(family,fieldmatch,value,field) on write",
125 .desc = "This function will read or write values from/to a RealTime repository.\n"
126 "REALTIME(....) will read names/values from the repository, and \n"
127 "REALTIME(....)= will write a new value/field to the repository. On a\n"
128 "read, this function returns a delimited text string. The name/value \n"
129 "pairs are delimited by delim1, and the name and value are delimited \n"
130 "between each other with delim2. The default for delim1 is ',' and \n"
131 "the default for delim2 is '='. If there is no match, NULL will be \n"
132 "returned by the function. On a write, this function will always \n"
134 .read = function_realtime_read,
135 .write = function_realtime_write,
138 static int unload_module(void)
140 return ast_custom_function_unregister(&realtime_function);
143 static int load_module(void)
145 return ast_custom_function_register(&realtime_function);
148 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Read/Write values from a RealTime repository");