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 #include "asterisk.h"
00031
00032 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 191140 $")
00033
00034 #include <regex.h>
00035
00036 #include "asterisk/module.h"
00037 #include "asterisk/channel.h"
00038 #include "asterisk/pbx.h"
00039 #include "asterisk/utils.h"
00040 #include "asterisk/app.h"
00041 #include "asterisk/astdb.h"
00042
00043
00044
00045
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 static int function_db_read(struct ast_channel *chan, const char *cmd,
00106 char *parse, char *buf, size_t len)
00107 {
00108 AST_DECLARE_APP_ARGS(args,
00109 AST_APP_ARG(family);
00110 AST_APP_ARG(key);
00111 );
00112
00113 buf[0] = '\0';
00114
00115 if (ast_strlen_zero(parse)) {
00116 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)\n");
00117 return -1;
00118 }
00119
00120 AST_NONSTANDARD_APP_ARGS(args, parse, '/');
00121
00122 if (args.argc < 2) {
00123 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)\n");
00124 return -1;
00125 }
00126
00127 if (ast_db_get(args.family, args.key, buf, len - 1)) {
00128 ast_debug(1, "DB: %s/%s not found in database.\n", args.family, args.key);
00129 } else {
00130 pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
00131 }
00132
00133 return 0;
00134 }
00135
00136 static int function_db_write(struct ast_channel *chan, const char *cmd, char *parse,
00137 const char *value)
00138 {
00139 AST_DECLARE_APP_ARGS(args,
00140 AST_APP_ARG(family);
00141 AST_APP_ARG(key);
00142 );
00143
00144 if (ast_strlen_zero(parse)) {
00145 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)=<value>\n");
00146 return -1;
00147 }
00148
00149 AST_NONSTANDARD_APP_ARGS(args, parse, '/');
00150
00151 if (args.argc < 2) {
00152 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)=value\n");
00153 return -1;
00154 }
00155
00156 if (ast_db_put(args.family, args.key, value)) {
00157 ast_log(LOG_WARNING, "DB: Error writing value to database.\n");
00158 }
00159
00160 return 0;
00161 }
00162
00163 static struct ast_custom_function db_function = {
00164 .name = "DB",
00165 .read = function_db_read,
00166 .write = function_db_write,
00167 };
00168
00169 static int function_db_exists(struct ast_channel *chan, const char *cmd,
00170 char *parse, char *buf, size_t len)
00171 {
00172 AST_DECLARE_APP_ARGS(args,
00173 AST_APP_ARG(family);
00174 AST_APP_ARG(key);
00175 );
00176
00177 buf[0] = '\0';
00178
00179 if (ast_strlen_zero(parse)) {
00180 ast_log(LOG_WARNING, "DB_EXISTS requires an argument, DB(<family>/<key>)\n");
00181 return -1;
00182 }
00183
00184 AST_NONSTANDARD_APP_ARGS(args, parse, '/');
00185
00186 if (args.argc < 2) {
00187 ast_log(LOG_WARNING, "DB_EXISTS requires an argument, DB(<family>/<key>)\n");
00188 return -1;
00189 }
00190
00191 if (ast_db_get(args.family, args.key, buf, len - 1)) {
00192 strcpy(buf, "0");
00193 } else {
00194 pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
00195 strcpy(buf, "1");
00196 }
00197
00198 return 0;
00199 }
00200
00201 static struct ast_custom_function db_exists_function = {
00202 .name = "DB_EXISTS",
00203 .read = function_db_exists,
00204 .read_max = 2,
00205 };
00206
00207 static int function_db_delete(struct ast_channel *chan, const char *cmd,
00208 char *parse, char *buf, size_t len)
00209 {
00210 AST_DECLARE_APP_ARGS(args,
00211 AST_APP_ARG(family);
00212 AST_APP_ARG(key);
00213 );
00214
00215 buf[0] = '\0';
00216
00217 if (ast_strlen_zero(parse)) {
00218 ast_log(LOG_WARNING, "DB_DELETE requires an argument, DB_DELETE(<family>/<key>)\n");
00219 return -1;
00220 }
00221
00222 AST_NONSTANDARD_APP_ARGS(args, parse, '/');
00223
00224 if (args.argc < 2) {
00225 ast_log(LOG_WARNING, "DB_DELETE requires an argument, DB_DELETE(<family>/<key>)\n");
00226 return -1;
00227 }
00228
00229 if (ast_db_get(args.family, args.key, buf, len - 1)) {
00230 ast_debug(1, "DB_DELETE: %s/%s not found in database.\n", args.family, args.key);
00231 } else {
00232 if (ast_db_del(args.family, args.key)) {
00233 ast_debug(1, "DB_DELETE: %s/%s could not be deleted from the database\n", args.family, args.key);
00234 }
00235 }
00236
00237 pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
00238
00239 return 0;
00240 }
00241
00242
00243 static struct ast_custom_function db_delete_function = {
00244 .name = "DB_DELETE",
00245 .read = function_db_delete,
00246 };
00247
00248 static int unload_module(void)
00249 {
00250 int res = 0;
00251
00252 res |= ast_custom_function_unregister(&db_function);
00253 res |= ast_custom_function_unregister(&db_exists_function);
00254 res |= ast_custom_function_unregister(&db_delete_function);
00255
00256 return res;
00257 }
00258
00259 static int load_module(void)
00260 {
00261 int res = 0;
00262
00263 res |= ast_custom_function_register(&db_function);
00264 res |= ast_custom_function_register(&db_exists_function);
00265 res |= ast_custom_function_register(&db_delete_function);
00266
00267 return res;
00268 }
00269
00270 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Database (astdb) related dialplan functions");