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 #include <stdlib.h>
00028 #include <string.h>
00029 #include <sys/types.h>
00030 #include <regex.h>
00031
00032 #include "asterisk.h"
00033
00034
00035
00036 #include "asterisk/channel.h"
00037 #include "asterisk/pbx.h"
00038 #include "asterisk/logger.h"
00039 #include "asterisk/options.h"
00040 #include "asterisk/utils.h"
00041 #include "asterisk/app.h"
00042 #include "asterisk/astdb.h"
00043
00044 static char *function_db_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
00045 {
00046 int argc;
00047 char *args;
00048 char *argv[2];
00049 char *family;
00050 char *key;
00051
00052 if (ast_strlen_zero(data)) {
00053 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)\n");
00054 return buf;
00055 }
00056
00057 args = ast_strdupa(data);
00058 argc = ast_app_separate_args(args, '/', argv, sizeof(argv) / sizeof(argv[0]));
00059
00060 if (argc > 1) {
00061 family = argv[0];
00062 key = argv[1];
00063 } else {
00064 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)\n");
00065 return buf;
00066 }
00067
00068 if (ast_db_get(family, key, buf, len-1)) {
00069 ast_log(LOG_DEBUG, "DB: %s/%s not found in database.\n", family, key);
00070 } else
00071 pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
00072
00073
00074 return buf;
00075 }
00076
00077 static void function_db_write(struct ast_channel *chan, char *cmd, char *data, const char *value)
00078 {
00079 int argc;
00080 char *args;
00081 char *argv[2];
00082 char *family;
00083 char *key;
00084
00085 if (ast_strlen_zero(data)) {
00086 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)=<value>\n");
00087 return;
00088 }
00089
00090 args = ast_strdupa(data);
00091 argc = ast_app_separate_args(args, '/', argv, sizeof(argv) / sizeof(argv[0]));
00092
00093 if (argc > 1) {
00094 family = argv[0];
00095 key = argv[1];
00096 } else {
00097 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)=value\n");
00098 return;
00099 }
00100
00101 if (ast_db_put(family, key, (char*)value)) {
00102 ast_log(LOG_WARNING, "DB: Error writing value to database.\n");
00103 }
00104 }
00105
00106 #ifndef BUILTIN_FUNC
00107 static
00108 #endif
00109 struct ast_custom_function db_function = {
00110 .name = "DB",
00111 .synopsis = "Read or Write from/to the Asterisk database",
00112 .syntax = "DB(<family>/<key>)",
00113 .desc = "This function will read or write a value from/to the Asterisk database.\n"
00114 "DB(...) will read a value from the database, while DB(...)=value\n"
00115 "will write a value to the database. On a read, this function\n"
00116 "returns the value from the datase, or NULL if it does not exist.\n"
00117 "On a write, this function will always return NULL. Reading a database value\n"
00118 "will also set the variable DB_RESULT.\n",
00119 .read = function_db_read,
00120 .write = function_db_write,
00121 };
00122
00123 static char *function_db_exists(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
00124 {
00125 int argc;
00126 char *args;
00127 char *argv[2];
00128 char *family;
00129 char *key;
00130
00131 if (ast_strlen_zero(data)) {
00132 ast_log(LOG_WARNING, "DB_EXISTS requires an argument, DB(<family>/<key>)\n");
00133 return buf;
00134 }
00135
00136 args = ast_strdupa(data);
00137 argc = ast_app_separate_args(args, '/', argv, sizeof(argv) / sizeof(argv[0]));
00138
00139 if (argc > 1) {
00140 family = argv[0];
00141 key = argv[1];
00142 } else {
00143 ast_log(LOG_WARNING, "DB_EXISTS requires an argument, DB(<family>/<key>)\n");
00144 return buf;
00145 }
00146
00147 if (ast_db_get(family, key, buf, len-1))
00148 ast_copy_string(buf, "0", len);
00149 else {
00150 pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
00151 ast_copy_string(buf, "1", len);
00152 }
00153
00154 return buf;
00155 }
00156
00157 #ifndef BUILTIN_FUNC
00158 static
00159 #endif
00160 struct ast_custom_function db_exists_function = {
00161 .name = "DB_EXISTS",
00162 .synopsis = "Check to see if a key exists in the Asterisk database",
00163 .syntax = "DB_EXISTS(<family>/<key>)",
00164 .desc = "This function will check to see if a key exists in the Asterisk\n"
00165 "database. If it exists, the function will return \"1\". If not,\n"
00166 "it will return \"0\". Checking for existence of a database key will\n"
00167 "also set the variable DB_RESULT to the key's value if it exists.\n",
00168 .read = function_db_exists,
00169 };