2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2004 - 2005
6 * See http://www.asterisk.org for more information about
7 * the Asterisk project. Please do not directly contact
8 * any of the maintainers of this project for assistance;
9 * the project provides a web site, mailing lists and IRC
10 * channels for your use.
12 * This program is free software, distributed under the terms of
13 * the GNU General Public License Version 2. See the LICENSE file
14 * at the top of the source tree.
19 * \brief Asterisk Call Manager CDR records.
24 * \arg \ref Config_ami
25 * \ingroup cdr_drivers
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
34 #include "asterisk/channel.h"
35 #include "asterisk/cdr.h"
36 #include "asterisk/module.h"
37 #include "asterisk/utils.h"
38 #include "asterisk/manager.h"
39 #include "asterisk/config.h"
40 #include "asterisk/pbx.h"
42 #define DATE_FORMAT "%Y-%m-%d %T"
43 #define CONF_FILE "cdr_manager.conf"
44 #define CUSTOM_FIELDS_BUF_SIZE 1024
46 static char *name = "cdr_manager";
48 static int enablecdr = 0;
52 * \bug The handling of this variable is not thread-safe. Crashes are possible on reload.
54 static struct ast_str *customfields;
56 static int manager_log(struct ast_cdr *cdr);
58 static int load_config(int reload)
61 struct ast_config *cfg;
62 struct ast_variable *v;
63 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
66 cfg = ast_config_load(CONF_FILE, config_flags);
67 if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
71 if (cfg == CONFIG_STATUS_FILEINVALID) {
72 ast_log(LOG_ERROR, "Config file '%s' could not be parsed\n", CONF_FILE);
76 if (reload && customfields) {
77 ast_free(customfields);
82 /* Standard configuration */
83 ast_log(LOG_WARNING, "Failed to load configuration file. Module not activated.\n");
85 ast_cdr_unregister(name);
90 while ( (cat = ast_category_browse(cfg, cat)) ) {
91 if (!strcasecmp(cat, "general")) {
92 v = ast_variable_browse(cfg, cat);
94 if (!strcasecmp(v->name, "enabled"))
95 newenablecdr = ast_true(v->value);
99 } else if (!strcasecmp(cat, "mappings")) {
100 customfields = ast_str_create(CUSTOM_FIELDS_BUF_SIZE);
101 v = ast_variable_browse(cfg, cat);
103 if (customfields && !ast_strlen_zero(v->name) && !ast_strlen_zero(v->value)) {
104 if ((ast_str_strlen(customfields) + strlen(v->value) + strlen(v->name) + 14) < ast_str_size(customfields)) {
105 ast_str_append(&customfields, -1, "%s: ${CDR(%s)}\r\n", v->value, v->name);
106 ast_log(LOG_NOTICE, "Added mapping %s: ${CDR(%s)}\n", v->value, v->name);
108 ast_log(LOG_WARNING, "No more buffer space to add other custom fields\n");
118 ast_config_destroy(cfg);
120 if (enablecdr && !newenablecdr)
121 ast_cdr_unregister(name);
122 else if (!enablecdr && newenablecdr)
123 ast_cdr_register(name, "Asterisk Manager Interface CDR Backend", manager_log);
124 enablecdr = newenablecdr;
129 static int manager_log(struct ast_cdr *cdr)
131 struct ast_tm timeresult;
132 char strStartTime[80] = "";
133 char strAnswerTime[80] = "";
134 char strEndTime[80] = "";
135 char buf[CUSTOM_FIELDS_BUF_SIZE];
140 ast_localtime(&cdr->start, &timeresult, NULL);
141 ast_strftime(strStartTime, sizeof(strStartTime), DATE_FORMAT, &timeresult);
143 if (cdr->answer.tv_sec) {
144 ast_localtime(&cdr->answer, &timeresult, NULL);
145 ast_strftime(strAnswerTime, sizeof(strAnswerTime), DATE_FORMAT, &timeresult);
148 ast_localtime(&cdr->end, &timeresult, NULL);
149 ast_strftime(strEndTime, sizeof(strEndTime), DATE_FORMAT, &timeresult);
152 /* Custom fields handling */
153 if (customfields != NULL && ast_str_strlen(customfields)) {
154 struct ast_channel *dummy = ast_channel_alloc(0, 0, "", "", "", "", "", 0, "Substitution/%p", cdr);
156 ast_log(LOG_ERROR, "Unable to allocate channel for variable substitution.\n");
159 dummy->cdr = ast_cdr_dup(cdr);
160 pbx_substitute_variables_helper(dummy, ast_str_buffer(customfields), buf, sizeof(buf) - 1);
161 ast_channel_release(dummy);
164 manager_event(EVENT_FLAG_CDR, "Cdr",
165 "AccountCode: %s\r\n"
167 "Destination: %s\r\n"
168 "DestinationContext: %s\r\n"
171 "DestinationChannel: %s\r\n"
172 "LastApplication: %s\r\n"
178 "BillableSeconds: %ld\r\n"
179 "Disposition: %s\r\n"
184 cdr->accountcode, cdr->src, cdr->dst, cdr->dcontext, cdr->clid, cdr->channel,
185 cdr->dstchannel, cdr->lastapp, cdr->lastdata, strStartTime, strAnswerTime, strEndTime,
186 cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition),
187 ast_cdr_flags2str(cdr->amaflags), cdr->uniqueid, cdr->userfield,buf);
192 static int unload_module(void)
194 ast_cdr_unregister(name);
196 ast_free(customfields);
201 static int load_module(void)
203 if (load_config(0)) {
204 return AST_MODULE_LOAD_DECLINE;
207 return AST_MODULE_LOAD_SUCCESS;
210 static int reload(void)
212 return load_config(1);
215 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Asterisk Manager Interface CDR Backend",
217 .unload = unload_module,