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
, 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
,
56 AST_APP_ARG(fieldmatch
);
63 if (ast_strlen_zero(data
)) {
64 ast_log(LOG_WARNING
, "Syntax: REALTIME(family|fieldmatch[|value[|delim1[|delim2]]]) - missing argument!\n");
68 u
= ast_module_user_add(chan
);
70 AST_STANDARD_APP_ARGS(args
, data
);
78 ast_autoservice_start(chan
);
80 head
= ast_load_realtime(args
.family
, args
.fieldmatch
, args
.value
, NULL
);
83 ast_module_user_remove(u
);
85 ast_autoservice_stop(chan
);
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
);
99 ast_autoservice_stop(chan
);
104 static int function_realtime_write(struct ast_channel
*chan
, char *cmd
, char *data
, const char *value
)
106 struct ast_module_user
*u
= NULL
;
108 AST_DECLARE_APP_ARGS(args
,
110 AST_APP_ARG(fieldmatch
);
115 if (ast_strlen_zero(data
)) {
116 ast_log(LOG_WARNING
, "Syntax: REALTIME(family|fieldmatch|value|newcol) - missing argument!\n");
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
);
130 ast_log(LOG_WARNING
, "Failed to update. Check the debug log for possible data repository related entries.\n");
134 ast_module_user_remove(u
);
135 ast_autoservice_stop(chan
);
141 struct ast_custom_function realtime_function
= {
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"
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();
168 static int load_module(void)
170 int res
= ast_custom_function_register(&realtime_function
);
175 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY
, "Read/Write values from a RealTime repository");