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
;
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
);
77 head
= ast_load_realtime(args
.family
, args
.fieldmatch
, args
.value
, NULL
);
80 ast_module_user_remove(u
);
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
);
96 static int function_realtime_write(struct ast_channel
*chan
, char *cmd
, char *data
, const char *value
)
98 struct ast_module_user
*u
;
100 AST_DECLARE_APP_ARGS(args
,
102 AST_APP_ARG(fieldmatch
);
107 if (ast_strlen_zero(data
)) {
108 ast_log(LOG_WARNING
, "Syntax: REALTIME(family|fieldmatch|value|newcol) - missing argument!\n");
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
);
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
);
127 struct ast_custom_function realtime_function
= {
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"
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();
154 static int load_module(void)
156 int res
= ast_custom_function_register(&realtime_function
);
161 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY
, "Read/Write values from a RealTime repository");