remove some now-unnecessary explicit includes of autoconfig.h
[asterisk-bristuff.git] / funcs / func_realtime.c
blob9610c0c2a39aa877b3af9183be64b21e90cd27dc
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;
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 head = ast_load_realtime(args.family, args.fieldmatch, args.value, NULL);
79 if (!head) {
80 ast_module_user_remove(u);
81 return -1;
83 for (var = head; var; var = var->next)
84 resultslen += strlen(var->name) + strlen(var->value) + 2;
86 results = alloca(resultslen);
87 for (var = head; var; var = var->next)
88 ast_build_string(&results, &resultslen, "%s%s%s%s", var->name, args.delim2, var->value, args.delim1);
89 ast_copy_string(buf, results, len);
91 ast_module_user_remove(u);
93 return 0;
96 static int function_realtime_write(struct ast_channel *chan, char *cmd, char *data, const char *value)
98 struct ast_module_user *u;
99 int res = 0;
100 AST_DECLARE_APP_ARGS(args,
101 AST_APP_ARG(family);
102 AST_APP_ARG(fieldmatch);
103 AST_APP_ARG(value);
104 AST_APP_ARG(field);
107 if (ast_strlen_zero(data)) {
108 ast_log(LOG_WARNING, "Syntax: REALTIME(family|fieldmatch|value|newcol) - missing argument!\n");
109 return -1;
112 u = ast_module_user_add(chan);
114 AST_STANDARD_APP_ARGS(args, data);
116 res = ast_update_realtime(args.family, args.fieldmatch, args.value, args.field, (char *)value, NULL);
118 if (res < 0) {
119 ast_log(LOG_WARNING, "Failed to update. Check the debug log for possible data repository related entries.\n");
122 ast_module_user_remove(u);
124 return 0;
127 struct ast_custom_function realtime_function = {
128 .name = "REALTIME",
129 .synopsis = "RealTime Read/Write Functions",
130 .syntax = "REALTIME(family|fieldmatch[|value[|delim1[|delim2]]]) on read\n"
131 "REALTIME(family|fieldmatch|value|field) on write\n",
132 .desc = "This function will read or write values from/to a RealTime repository.\n"
133 "REALTIME(....) will read names/values from the repository, and \n"
134 "REALTIME(....)= will write a new value/field to the repository. On a\n"
135 "read, this function returns a delimited text string. The name/value \n"
136 "pairs are delimited by delim1, and the name and value are delimited \n"
137 "between each other with delim2. The default for delim1 is '=' and \n"
138 "the default for delim2 is '|'. If there is no match, NULL will be \n"
139 "returned by the function. On a write, this function will always \n"
140 "return NULL. \n",
141 .read = function_realtime_read,
142 .write = function_realtime_write,
145 static int unload_module(void)
147 int res = ast_custom_function_unregister(&realtime_function);
149 ast_module_user_hangup_all();
151 return res;
154 static int load_module(void)
156 int res = ast_custom_function_register(&realtime_function);
158 return res;
161 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Read/Write values from a RealTime repository");