00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #include "asterisk.h"
00035
00036 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 328209 $")
00037
00038 #include <regex.h>
00039
00040 #include "asterisk/module.h"
00041 #include "asterisk/channel.h"
00042 #include "asterisk/pbx.h"
00043 #include "asterisk/utils.h"
00044 #include "asterisk/app.h"
00045 #include "asterisk/astdb.h"
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109 static int function_db_read(struct ast_channel *chan, const char *cmd,
00110 char *parse, char *buf, size_t len)
00111 {
00112 AST_DECLARE_APP_ARGS(args,
00113 AST_APP_ARG(family);
00114 AST_APP_ARG(key);
00115 );
00116
00117 buf[0] = '\0';
00118
00119 if (ast_strlen_zero(parse)) {
00120 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)\n");
00121 return -1;
00122 }
00123
00124 AST_NONSTANDARD_APP_ARGS(args, parse, '/');
00125
00126 if (args.argc < 2) {
00127 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)\n");
00128 return -1;
00129 }
00130
00131 if (ast_db_get(args.family, args.key, buf, len - 1)) {
00132 ast_debug(1, "DB: %s/%s not found in database.\n", args.family, args.key);
00133 } else {
00134 pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
00135 }
00136
00137 return 0;
00138 }
00139
00140 static int function_db_write(struct ast_channel *chan, const char *cmd, char *parse,
00141 const char *value)
00142 {
00143 AST_DECLARE_APP_ARGS(args,
00144 AST_APP_ARG(family);
00145 AST_APP_ARG(key);
00146 );
00147
00148 if (ast_strlen_zero(parse)) {
00149 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)=<value>\n");
00150 return -1;
00151 }
00152
00153 AST_NONSTANDARD_APP_ARGS(args, parse, '/');
00154
00155 if (args.argc < 2) {
00156 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)=value\n");
00157 return -1;
00158 }
00159
00160 if (ast_db_put(args.family, args.key, value)) {
00161 ast_log(LOG_WARNING, "DB: Error writing value to database.\n");
00162 }
00163
00164 return 0;
00165 }
00166
00167 static struct ast_custom_function db_function = {
00168 .name = "DB",
00169 .read = function_db_read,
00170 .write = function_db_write,
00171 };
00172
00173 static int function_db_exists(struct ast_channel *chan, const char *cmd,
00174 char *parse, char *buf, size_t len)
00175 {
00176 AST_DECLARE_APP_ARGS(args,
00177 AST_APP_ARG(family);
00178 AST_APP_ARG(key);
00179 );
00180
00181 buf[0] = '\0';
00182
00183 if (ast_strlen_zero(parse)) {
00184 ast_log(LOG_WARNING, "DB_EXISTS requires an argument, DB(<family>/<key>)\n");
00185 return -1;
00186 }
00187
00188 AST_NONSTANDARD_APP_ARGS(args, parse, '/');
00189
00190 if (args.argc < 2) {
00191 ast_log(LOG_WARNING, "DB_EXISTS requires an argument, DB(<family>/<key>)\n");
00192 return -1;
00193 }
00194
00195 if (ast_db_get(args.family, args.key, buf, len - 1)) {
00196 strcpy(buf, "0");
00197 } else {
00198 pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
00199 strcpy(buf, "1");
00200 }
00201
00202 return 0;
00203 }
00204
00205 static struct ast_custom_function db_exists_function = {
00206 .name = "DB_EXISTS",
00207 .read = function_db_exists,
00208 .read_max = 2,
00209 };
00210
00211 static int function_db_delete(struct ast_channel *chan, const char *cmd,
00212 char *parse, char *buf, size_t len)
00213 {
00214 AST_DECLARE_APP_ARGS(args,
00215 AST_APP_ARG(family);
00216 AST_APP_ARG(key);
00217 );
00218
00219 buf[0] = '\0';
00220
00221 if (ast_strlen_zero(parse)) {
00222 ast_log(LOG_WARNING, "DB_DELETE requires an argument, DB_DELETE(<family>/<key>)\n");
00223 return -1;
00224 }
00225
00226 AST_NONSTANDARD_APP_ARGS(args, parse, '/');
00227
00228 if (args.argc < 2) {
00229 ast_log(LOG_WARNING, "DB_DELETE requires an argument, DB_DELETE(<family>/<key>)\n");
00230 return -1;
00231 }
00232
00233 if (ast_db_get(args.family, args.key, buf, len - 1)) {
00234 ast_debug(1, "DB_DELETE: %s/%s not found in database.\n", args.family, args.key);
00235 } else {
00236 if (ast_db_del(args.family, args.key)) {
00237 ast_debug(1, "DB_DELETE: %s/%s could not be deleted from the database\n", args.family, args.key);
00238 }
00239 }
00240
00241 pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
00242
00243 return 0;
00244 }
00245
00246
00247 static struct ast_custom_function db_delete_function = {
00248 .name = "DB_DELETE",
00249 .read = function_db_delete,
00250 };
00251
00252 static int unload_module(void)
00253 {
00254 int res = 0;
00255
00256 res |= ast_custom_function_unregister(&db_function);
00257 res |= ast_custom_function_unregister(&db_exists_function);
00258 res |= ast_custom_function_unregister(&db_delete_function);
00259
00260 return res;
00261 }
00262
00263 static int load_module(void)
00264 {
00265 int res = 0;
00266
00267 res |= ast_custom_function_register(&db_function);
00268 res |= ast_custom_function_register(&db_exists_function);
00269 res |= ast_custom_function_register(&db_delete_function);
00270
00271 return res;
00272 }
00273
00274 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Database (astdb) related dialplan functions");