2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2007, Digium, Inc.
6 * Mark Spencer <markster@digium.com> and others.
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief Custom SQLite3 CDR records.
23 * \author Adapted by Alejandro Rios <alejandro.rios@avatar.com.co> and
24 * Russell Bryant <russell@digium.com> from
25 * cdr_mysql_custom by Edward Eastman <ed@dm3.co.uk>,
26 * and cdr_sqlite by Holger Schurig <hs4233@mail.mn-solutions.de>
29 * \arg See also \ref AstCDR
32 * \ingroup cdr_drivers
36 <depend>sqlite3</depend>
41 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
49 #include <sys/types.h>
52 #include "asterisk/channel.h"
53 #include "asterisk/cdr.h"
54 #include "asterisk/module.h"
55 #include "asterisk/config.h"
56 #include "asterisk/pbx.h"
57 #include "asterisk/logger.h"
58 #include "asterisk/utils.h"
59 #include "asterisk/cli.h"
60 #include "asterisk/options.h"
62 AST_MUTEX_DEFINE_STATIC(lock);
64 static const char config_file[] = "cdr_sqlite3_custom.conf";
66 static char *desc = "Customizable SQLite3 CDR Backend";
67 static char *name = "cdr_sqlite3_custom";
68 static sqlite3 *db = NULL;
70 static char table[80];
71 static char columns[1024];
72 static char values[1024];
74 static int load_config(int reload)
76 struct ast_config *cfg;
77 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
78 struct ast_variable *mappingvar;
81 if (!(cfg = ast_config_load(config_file, config_flags))) {
83 ast_log(LOG_WARNING, "%s: Failed to reload configuration file.\n", name);
86 "%s: Failed to load configuration file. Module not activated.\n",
90 } else if (cfg == CONFIG_STATUS_FILEUNCHANGED)
93 ast_mutex_lock(&lock);
95 if (!(mappingvar = ast_variable_browse(cfg, "master"))) {
96 /* nothing configured */
97 ast_config_destroy(cfg);
101 /* Mapping must have a table name */
102 tmp = ast_variable_retrieve(cfg, "master", "table");
103 if (!ast_strlen_zero(tmp))
104 ast_copy_string(table, tmp, sizeof(table));
106 ast_log(LOG_WARNING, "%s: Table name not specified. Assuming cdr.\n", name);
107 strcpy(table, "cdr");
110 tmp = ast_variable_retrieve(cfg, "master", "columns");
111 if (!ast_strlen_zero(tmp))
112 ast_copy_string(columns, tmp, sizeof(columns));
114 ast_log(LOG_WARNING, "%s: Column names not specified. Module not loaded.\n",
116 ast_config_destroy(cfg);
120 tmp = ast_variable_retrieve(cfg, "master", "values");
121 if (!ast_strlen_zero(tmp))
122 ast_copy_string(values, tmp, sizeof(values));
124 ast_log(LOG_WARNING, "%s: Values not specified. Module not loaded.\n", name);
125 ast_config_destroy(cfg);
129 ast_mutex_unlock(&lock);
131 ast_config_destroy(cfg);
136 /* assumues 'to' buffer is at least strlen(from) * 2 + 1 bytes */
137 static int do_escape(char *to, const char *from)
141 for (; *from; from++) {
142 if (*from == '\'' || *from == '\\')
151 static int sqlite3_log(struct ast_cdr *cdr)
156 struct ast_channel dummy = { 0, };
159 { /* Make it obvious that only sql_cmd should be used outside of this block */
161 char sql_insert_cmd[2048] = "";
162 sql_tmp_cmd = sqlite3_mprintf("INSERT INTO %q (%q) VALUES (%q)", table, columns, values);
164 pbx_substitute_variables_helper(&dummy, sql_tmp_cmd, sql_insert_cmd, sizeof(sql_insert_cmd) - 1);
165 sqlite3_free(sql_tmp_cmd);
166 sql_cmd = alloca(strlen(sql_insert_cmd) * 2 + 1);
167 do_escape(sql_cmd, sql_insert_cmd);
170 ast_mutex_lock(&lock);
172 for (count = 0; count < 5; count++) {
173 res = sqlite3_exec(db, sql_cmd, NULL, NULL, &zErr);
174 if (res != SQLITE_BUSY && res != SQLITE_LOCKED)
180 ast_log(LOG_ERROR, "%s: %s. sentence: %s.\n", name, zErr, sql_cmd);
184 ast_mutex_unlock(&lock);
189 static int unload_module(void)
194 ast_cdr_unregister(name);
199 static int load_module(void)
206 if (!load_config(0)) {
207 res = ast_cdr_register(name, desc, sqlite3_log);
209 ast_log(LOG_ERROR, "%s: Unable to register custom SQLite3 CDR handling\n", name);
210 return AST_MODULE_LOAD_DECLINE;
214 /* is the database there? */
215 snprintf(fn, sizeof(fn), "%s/master.db", ast_config_AST_LOG_DIR);
216 res = sqlite3_open(fn, &db);
218 ast_log(LOG_ERROR, "%s: Could not open database %s.\n", name, fn);
220 return AST_MODULE_LOAD_DECLINE;
223 /* is the table there? */
224 sql_cmd = sqlite3_mprintf("SELECT COUNT(AcctId) FROM %q;", table);
225 res = sqlite3_exec(db, sql_cmd, NULL, NULL, NULL);
226 sqlite3_free(sql_cmd);
228 sql_cmd = sqlite3_mprintf("CREATE TABLE %q (AcctId INTEGER PRIMARY KEY,%q)", table, columns);
229 res = sqlite3_exec(db, sql_cmd, NULL, NULL, &zErr);
230 sqlite3_free(sql_cmd);
232 ast_log(LOG_WARNING, "%s: %s.\n", name, zErr);
238 ast_log(LOG_ERROR, "%s: Unable to create table '%s': %s.\n", name, table, zErr);
242 return AST_MODULE_LOAD_DECLINE;
249 static int reload(void)
251 return load_config(1);
254 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "SQLite3 Custom CDR Module",
256 .unload = unload_module,