/* * CallWeaver -- An open source telephony toolkit. * * Copyright (C) 1999 - 2005, Digium, Inc. * Copyright (C) 2003, Jefferson Noxon * * Mark Spencer * Jefferson Noxon * * See http://www.callweaver.org for more information about * the CallWeaver project. Please do not directly contact * any of the maintainers of this project for assistance; * the project provides a web site, mailing lists and IRC * channels for your use. * * This program is free software, distributed under the terms of * the GNU General Public License Version 2. See the LICENSE file * at the top of the source tree. */ /*! \file * * \brief Database access functions * */ #ifdef HAVE_CONFIG_H #include "confdefs.h" #endif #include #include #include #include #include #include "callweaver.h" CALLWEAVER_FILE_VERSION("$HeadURL$", "$Revision$") #include "callweaver/options.h" #include "callweaver/file.h" #include "callweaver/logger.h" #include "callweaver/channel.h" #include "callweaver/pbx.h" #include "callweaver/module.h" #include "callweaver/callweaver_db.h" #include "callweaver/lock.h" static char *tdesc = "Database access functions for CallWeaver extension logic"; static void *g_app; static void *p_app; static void *d_app; static void *dt_app; static char *g_name = "DBget"; static char *p_name = "DBput"; static char *d_name = "DBdel"; static char *dt_name = "DBdelTree"; static char *g_synopsis = "Retrieve a value from the database"; static char *p_synopsis = "Store a value in the database"; static char *d_synopsis = "Delete a key from the database"; static char *dt_synopsis = "Delete a family or keytree from the database"; static char *g_syntax = "DBget(varname=family/key)"; static char *p_syntax = "DBput(family/key=value)"; static char *d_syntax = "DBdel(family/key)"; static char *dt_syntax = "DBdelTree(family[/keytree])"; static char *g_descrip = "Retrieves a value from the CallWeaver\n" "database and stores it in the given variable. Always returns 0.\n" "Sets DBSTATUS to SUCCESS if the key is found and FAIL on error.\n"; static char *p_descrip = "Stores the given value in the CallWeaver\n" "database. Always returns 0.\n" "Sets DBSTATUS to SUCCESS if the key is found and FAIL on error.\n"; static char *d_descrip = "Deletes a key from the CallWeaver database. Always\n" "returns 0.\n" "Sets DBSTATUS to SUCCESS if the key is found and FAIL on error.\n"; static char *dt_descrip = "Deletes a family or keytree from the CallWeaver\n" "database. Always returns 0.\n" "Sets DBSTATUS to SUCCESS if the key is found and FAIL on error.\n"; STANDARD_LOCAL_USER; LOCAL_USER_DECL; static int deltree_exec(struct cw_channel *chan, int argc, char **argv) { char *family, *keytree; struct localuser *u; if (argc != 1) { cw_log(LOG_ERROR, "Syntax: %s\n", dt_syntax); return -1; } LOCAL_USER_ADD(u); if (strchr(argv[0], '/')) { family = strsep(&argv[0], "/"); keytree = strsep(&argv[0], "\0"); if (!family || !keytree) { cw_log(LOG_DEBUG, "Ignoring; Syntax error in argument\n"); LOCAL_USER_REMOVE(u); return 0; } if (cw_strlen_zero(keytree)) keytree = 0; } else { family = argv[0]; keytree = 0; } if (option_verbose > 2) { if (keytree) cw_verbose(VERBOSE_PREFIX_3 "DBdeltree: family=%s, keytree=%s\n", family, keytree); else cw_verbose(VERBOSE_PREFIX_3 "DBdeltree: family=%s\n", family); } if (cw_db_deltree(family, keytree)) { if (option_verbose > 2) cw_verbose(VERBOSE_PREFIX_3 "DBdeltree: Error deleting key from database.\n"); pbx_builtin_setvar_helper(chan, "DBSTATUS", "FAIL"); } else { pbx_builtin_setvar_helper(chan, "DBSTATUS", "SUCCESS"); } LOCAL_USER_REMOVE(u); return 0; } static int del_exec(struct cw_channel *chan, int argc, char **argv) { char *family, *key; struct localuser *u; if (argc != 1) { cw_log (LOG_ERROR, "Syntax: %s\n", d_syntax); return -1; } LOCAL_USER_ADD(u); if (strchr(argv[0], '/')) { family = strsep(&argv[0], "/"); key = strsep(&argv[0], "\0"); if (!family || !key) { cw_log(LOG_DEBUG, "Ignoring; Syntax error in argument\n"); LOCAL_USER_REMOVE(u); return 0; } if (option_verbose > 2) cw_verbose(VERBOSE_PREFIX_3 "DBdel: family=%s, key=%s\n", family, key); if (cw_db_del(family, key)) { if (option_verbose > 2) cw_verbose(VERBOSE_PREFIX_3 "DBdel: Error deleting key from database.\n"); pbx_builtin_setvar_helper(chan, "DBSTATUS", "FAIL"); } else { pbx_builtin_setvar_helper(chan, "DBSTATUS", "SUCCESS"); } } else { cw_log(LOG_DEBUG, "Ignoring, no parameters\n"); } LOCAL_USER_REMOVE(u); return 0; } static int put_exec(struct cw_channel *chan, int argc, char **argv) { char *value, *family, *key; static int dep_warning = 0; struct localuser *u; if (!dep_warning) { cw_log(LOG_WARNING, "This application has been deprecated, please use the ${DB(family/key)} function instead.\n"); dep_warning = 1; } if (argc != 1) { cw_log(LOG_ERROR, "Syntax: %s\n", p_syntax); return -1; } LOCAL_USER_ADD(u); if (strchr(argv[0], '/') && strchr(argv[0], '=')) { family = strsep(&argv[0], "/"); key = strsep(&argv[0], "="); value = strsep(&argv[0], "\0"); if (!value || !family || !key) { cw_log(LOG_DEBUG, "Ignoring; Syntax error in argument\n"); LOCAL_USER_REMOVE(u); return 0; } if (option_verbose > 2) cw_verbose(VERBOSE_PREFIX_3 "DBput: family=%s, key=%s, value=%s\n", family, key, value); if (cw_db_put(family, key, value)) { if (option_verbose > 2) cw_verbose(VERBOSE_PREFIX_3 "DBput: Error writing value to database.\n"); pbx_builtin_setvar_helper(chan, "DBSTATUS", "FAIL"); } else { pbx_builtin_setvar_helper(chan, "DBSTATUS", "SUCCESS"); } } else { cw_log (LOG_DEBUG, "Ignoring, no parameters\n"); } LOCAL_USER_REMOVE(u); return 0; } static int get_exec(struct cw_channel *chan, int argc, char **argv) { char *varname, *family, *key; char dbresult[256]; static int dep_warning = 0; struct localuser *u; if (!dep_warning) { cw_log(LOG_WARNING, "This application has been deprecated, please use the ${DB(family/key)} function instead.\n"); dep_warning = 1; } if (argc != 1) { cw_log(LOG_ERROR, "Syntax: %s\n", g_syntax); return -1; } LOCAL_USER_ADD(u); if (strchr(argv[0], '=') && strchr(argv[0], '/')) { varname = strsep(&argv[0], "="); family = strsep(&argv[0], "/"); key = strsep(&argv[0], "\0"); if (!varname || !family || !key) { cw_log(LOG_DEBUG, "Ignoring; Syntax error in argument\n"); LOCAL_USER_REMOVE(u); return 0; } if (option_verbose > 2) cw_verbose(VERBOSE_PREFIX_3 "DBget: varname=%s, family=%s, key=%s\n", varname, family, key); if (!cw_db_get(family, key, dbresult, sizeof (dbresult) - 1)) { pbx_builtin_setvar_helper(chan, varname, dbresult); if (option_verbose > 2) cw_verbose(VERBOSE_PREFIX_3 "DBget: set variable %s to %s\n", varname, dbresult); pbx_builtin_setvar_helper(chan, "DBSTATUS", "SUCCESS"); } else { if (option_verbose > 2) cw_verbose(VERBOSE_PREFIX_3 "DBget: Value not found in database.\n"); pbx_builtin_setvar_helper(chan, "DBSTATUS", "FAIL"); } } else { cw_log(LOG_DEBUG, "Ignoring, no parameters\n"); } LOCAL_USER_REMOVE(u); return 0; } int unload_module(void) { int res = 0; STANDARD_HANGUP_LOCALUSERS; res |= cw_unregister_application(dt_app); res |= cw_unregister_application(d_app); res |= cw_unregister_application(p_app); res |= cw_unregister_application(g_app); return res; } int load_module(void) { g_app = cw_register_application(g_name, get_exec, g_synopsis, g_syntax, g_descrip); p_app = cw_register_application(p_name, put_exec, p_synopsis, p_syntax, p_descrip); d_app = cw_register_application(d_name, del_exec, d_synopsis, d_syntax, d_descrip); dt_app = cw_register_application(dt_name, deltree_exec, dt_synopsis, dt_syntax, dt_descrip); return 0; } char *description(void) { return tdesc; } int usecount(void) { int res; STANDARD_USECOUNT(res); return res; }