Let's also include aclocal.m4
[asterisk-bristuff.git] / funcs / func_realtime.c
blob6941b224ecd28598802038a339b68009f014b584
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2005-2006, BJ Weschke. All rights reserved.
5 *
6 * BJ Weschke <bweschke@btwtech.com>
7 *
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.
18 /*! \file
20 * \brief REALTIME dialplan function
22 * \author BJ Weschke <bweschke@btwtech.com>
24 * \ingroup functions
27 #include "asterisk.h"
29 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include <string.h>
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, char *cmd, char *data, char *buf, size_t len)
50 struct ast_variable *var, *head;
51 struct ast_module_user *u;
52 char *results, *result_begin;
53 size_t resultslen = 0;
54 AST_DECLARE_APP_ARGS(args,
55 AST_APP_ARG(family);
56 AST_APP_ARG(fieldmatch);
57 AST_APP_ARG(value);
58 AST_APP_ARG(delim1);
59 AST_APP_ARG(delim2);
63 if (ast_strlen_zero(data)) {
64 ast_log(LOG_WARNING, "Syntax: REALTIME(family|fieldmatch[|value[|delim1[|delim2]]]) - missing argument!\n");
65 return -1;
68 u = ast_module_user_add(chan);
70 AST_STANDARD_APP_ARGS(args, data);
72 if (!args.delim1)
73 args.delim1 = "|";
74 if (!args.delim2)
75 args.delim2 = "=";
77 if (chan)
78 ast_autoservice_start(chan);
80 head = ast_load_realtime(args.family, args.fieldmatch, args.value, NULL);
82 if (!head) {
83 ast_module_user_remove(u);
84 if (chan)
85 ast_autoservice_stop(chan);
86 return -1;
88 for (var = head; var; var = var->next)
89 resultslen += strlen(var->name) + strlen(var->value) + strlen(args.delim1) + strlen(args.delim2);
91 result_begin = results = alloca(resultslen);
92 for (var = head; var; var = var->next)
93 ast_build_string(&results, &resultslen, "%s%s%s%s", var->name, args.delim2, var->value, args.delim1);
94 ast_copy_string(buf, result_begin, len);
96 ast_module_user_remove(u);
98 if (chan)
99 ast_autoservice_stop(chan);
101 return 0;
104 static int function_realtime_write(struct ast_channel *chan, char *cmd, char *data, const char *value)
106 struct ast_module_user *u = NULL;
107 int res = 0;
108 AST_DECLARE_APP_ARGS(args,
109 AST_APP_ARG(family);
110 AST_APP_ARG(fieldmatch);
111 AST_APP_ARG(value);
112 AST_APP_ARG(field);
115 if (ast_strlen_zero(data)) {
116 ast_log(LOG_WARNING, "Syntax: REALTIME(family|fieldmatch|value|newcol) - missing argument!\n");
117 return -1;
120 if (chan) {
121 ast_autoservice_start(chan);
122 u = ast_module_user_add(chan);
125 AST_STANDARD_APP_ARGS(args, data);
127 res = ast_update_realtime(args.family, args.fieldmatch, args.value, args.field, (char *)value, NULL);
129 if (res < 0) {
130 ast_log(LOG_WARNING, "Failed to update. Check the debug log for possible data repository related entries.\n");
133 if (chan) {
134 ast_module_user_remove(u);
135 ast_autoservice_stop(chan);
138 return 0;
141 struct ast_custom_function realtime_function = {
142 .name = "REALTIME",
143 .synopsis = "RealTime Read/Write Functions",
144 .syntax = "REALTIME(family|fieldmatch[|value[|delim1[|delim2]]]) on read\n"
145 "REALTIME(family|fieldmatch|value|field) on write\n",
146 .desc = "This function will read or write values from/to a RealTime repository.\n"
147 "REALTIME(....) will read names/values from the repository, and \n"
148 "REALTIME(....)= will write a new value/field to the repository. On a\n"
149 "read, this function returns a delimited text string. The name/value \n"
150 "pairs are delimited by delim1, and the name and value are delimited \n"
151 "between each other with delim2. The default for delim1 is '|' and \n"
152 "the default for delim2 is '='. If there is no match, NULL will be \n"
153 "returned by the function. On a write, this function will always \n"
154 "return NULL. \n",
155 .read = function_realtime_read,
156 .write = function_realtime_write,
159 static int unload_module(void)
161 int res = ast_custom_function_unregister(&realtime_function);
163 ast_module_user_hangup_all();
165 return res;
168 static int load_module(void)
170 int res = ast_custom_function_register(&realtime_function);
172 return res;
175 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Read/Write values from a RealTime repository");