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: 89512 $")
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 static int function_db_read(struct ast_channel *chan, const char *cmd,
00044 char *parse, char *buf, size_t len)
00045 {
00046 AST_DECLARE_APP_ARGS(args,
00047 AST_APP_ARG(family);
00048 AST_APP_ARG(key);
00049 );
00050
00051 buf[0] = '\0';
00052
00053 if (ast_strlen_zero(parse)) {
00054 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)\n");
00055 return -1;
00056 }
00057
00058 AST_NONSTANDARD_APP_ARGS(args, parse, '/');
00059
00060 if (args.argc < 2) {
00061 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)\n");
00062 return -1;
00063 }
00064
00065 if (ast_db_get(args.family, args.key, buf, len - 1)) {
00066 ast_debug(1, "DB: %s/%s not found in database.\n", args.family, args.key);
00067 } else
00068 pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
00069
00070 return 0;
00071 }
00072
00073 static int function_db_write(struct ast_channel *chan, const char *cmd, char *parse,
00074 const char *value)
00075 {
00076 AST_DECLARE_APP_ARGS(args,
00077 AST_APP_ARG(family);
00078 AST_APP_ARG(key);
00079 );
00080
00081 if (ast_strlen_zero(parse)) {
00082 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)=<value>\n");
00083 return -1;
00084 }
00085
00086 AST_NONSTANDARD_APP_ARGS(args, parse, '/');
00087
00088 if (args.argc < 2) {
00089 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)=value\n");
00090 return -1;
00091 }
00092
00093 if (ast_db_put(args.family, args.key, (char *) value))
00094 ast_log(LOG_WARNING, "DB: Error writing value to database.\n");
00095
00096 return 0;
00097 }
00098
00099 static struct ast_custom_function db_function = {
00100 .name = "DB",
00101 .synopsis = "Read from or write to the Asterisk database",
00102 .syntax = "DB(<family>/<key>)",
00103 .desc =
00104 "This function will read from or write a value to the Asterisk database. On a\n"
00105 "read, this function returns the corresponding value from the database, or blank\n"
00106 "if it does not exist. Reading a database value will also set the variable\n"
00107 "DB_RESULT. If you wish to find out if an entry exists, use the DB_EXISTS\n"
00108 "function.\n",
00109 .read = function_db_read,
00110 .write = function_db_write,
00111 };
00112
00113 static int function_db_exists(struct ast_channel *chan, const char *cmd,
00114 char *parse, char *buf, size_t len)
00115 {
00116 AST_DECLARE_APP_ARGS(args,
00117 AST_APP_ARG(family);
00118 AST_APP_ARG(key);
00119 );
00120
00121 buf[0] = '\0';
00122
00123 if (ast_strlen_zero(parse)) {
00124 ast_log(LOG_WARNING, "DB_EXISTS requires an argument, DB(<family>/<key>)\n");
00125 return -1;
00126 }
00127
00128 AST_NONSTANDARD_APP_ARGS(args, parse, '/');
00129
00130 if (args.argc < 2) {
00131 ast_log(LOG_WARNING, "DB_EXISTS requires an argument, DB(<family>/<key>)\n");
00132 return -1;
00133 }
00134
00135 if (ast_db_get(args.family, args.key, buf, len - 1))
00136 strcpy(buf, "0");
00137 else {
00138 pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
00139 strcpy(buf, "1");
00140 }
00141
00142 return 0;
00143 }
00144
00145 static struct ast_custom_function db_exists_function = {
00146 .name = "DB_EXISTS",
00147 .synopsis = "Check to see if a key exists in the Asterisk database",
00148 .syntax = "DB_EXISTS(<family>/<key>)",
00149 .desc =
00150 "This function will check to see if a key exists in the Asterisk\n"
00151 "database. If it exists, the function will return \"1\". If not,\n"
00152 "it will return \"0\". Checking for existence of a database key will\n"
00153 "also set the variable DB_RESULT to the key's value if it exists.\n",
00154 .read = function_db_exists,
00155 };
00156
00157 static int function_db_delete(struct ast_channel *chan, const char *cmd,
00158 char *parse, char *buf, size_t len)
00159 {
00160 AST_DECLARE_APP_ARGS(args,
00161 AST_APP_ARG(family);
00162 AST_APP_ARG(key);
00163 );
00164
00165 buf[0] = '\0';
00166
00167 if (ast_strlen_zero(parse)) {
00168 ast_log(LOG_WARNING, "DB_DELETE requires an argument, DB_DELETE(<family>/<key>)\n");
00169 return -1;
00170 }
00171
00172 AST_NONSTANDARD_APP_ARGS(args, parse, '/');
00173
00174 if (args.argc < 2) {
00175 ast_log(LOG_WARNING, "DB_DELETE requires an argument, DB_DELETE(<family>/<key>)\n");
00176 return -1;
00177 }
00178
00179 if (ast_db_get(args.family, args.key, buf, len - 1)) {
00180 ast_debug(1, "DB_DELETE: %s/%s not found in database.\n", args.family, args.key);
00181 } else {
00182 if (ast_db_del(args.family, args.key)) {
00183 ast_debug(1, "DB_DELETE: %s/%s could not be deleted from the database\n", args.family, args.key);
00184 }
00185 }
00186 pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
00187
00188 return 0;
00189 }
00190
00191
00192 static struct ast_custom_function db_delete_function = {
00193 .name = "DB_DELETE",
00194 .synopsis = "Return a value from the database and delete it",
00195 .syntax = "DB_DELETE(<family>/<key>)",
00196 .desc =
00197 "This function will retrieve a value from the Asterisk database\n"
00198 " and then remove that key from the database. DB_RESULT\n"
00199 "will be set to the key's value if it exists.\n",
00200 .read = function_db_delete,
00201 };
00202
00203 static int unload_module(void)
00204 {
00205 int res = 0;
00206
00207 res |= ast_custom_function_unregister(&db_function);
00208 res |= ast_custom_function_unregister(&db_exists_function);
00209 res |= ast_custom_function_unregister(&db_delete_function);
00210
00211 return res;
00212 }
00213
00214 static int load_module(void)
00215 {
00216 int res = 0;
00217
00218 res |= ast_custom_function_register(&db_function);
00219 res |= ast_custom_function_register(&db_exists_function);
00220 res |= ast_custom_function_register(&db_delete_function);
00221
00222 return res;
00223 }
00224
00225 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Database (astdb) related dialplan functions");