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/logger.h"
38 #include "asterisk/utils.h"
39 #include "asterisk/manager.h"
40 #include "asterisk/config.h"
41 #include "asterisk/pbx.h"
43 #define DATE_FORMAT "%Y-%m-%d %T"
44 #define CONF_FILE "cdr_manager.conf"
45 #define CUSTOM_FIELDS_BUF_SIZE 1024
47 static char *name = "cdr_manager";
49 static int enablecdr = 0;
50 struct ast_str *customfields;
52 static int manager_log(struct ast_cdr *cdr);
54 static int load_config(int reload)
57 struct ast_config *cfg;
58 struct ast_variable *v;
59 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
62 cfg = ast_config_load(CONF_FILE, config_flags);
63 if (cfg == CONFIG_STATUS_FILEUNCHANGED)
66 if (reload && customfields) {
67 ast_free(customfields);
72 /* Standard configuration */
73 ast_log(LOG_WARNING, "Failed to load configuration file. Module not activated.\n");
75 ast_cdr_unregister(name);
80 while ( (cat = ast_category_browse(cfg, cat)) ) {
81 if (!strcasecmp(cat, "general")) {
82 v = ast_variable_browse(cfg, cat);
84 if (!strcasecmp(v->name, "enabled"))
85 newenablecdr = ast_true(v->value);
89 } else if (!strcasecmp(cat, "mappings")) {
90 customfields = ast_str_create(CUSTOM_FIELDS_BUF_SIZE);
91 v = ast_variable_browse(cfg, cat);
93 if (customfields && !ast_strlen_zero(v->name) && !ast_strlen_zero(v->value)) {
94 if( (customfields->used + strlen(v->value) + strlen(v->name) + 14) < customfields->len) {
95 ast_str_append(&customfields, -1, "%s: ${CDR(%s)}\r\n", v->value, v->name);
96 ast_log(LOG_NOTICE, "Added mapping %s: ${CDR(%s)}\n", v->value, v->name);
98 ast_log(LOG_WARNING, "No more buffer space to add other custom fields\n");
108 ast_config_destroy(cfg);
110 if (enablecdr && !newenablecdr)
111 ast_cdr_unregister(name);
112 else if (!enablecdr && newenablecdr)
113 ast_cdr_register(name, "Asterisk Manager Interface CDR Backend", manager_log);
114 enablecdr = newenablecdr;
119 static int manager_log(struct ast_cdr *cdr)
121 struct ast_tm timeresult;
122 char strStartTime[80] = "";
123 char strAnswerTime[80] = "";
124 char strEndTime[80] = "";
125 char buf[CUSTOM_FIELDS_BUF_SIZE];
126 struct ast_channel dummy;
131 ast_localtime(&cdr->start, &timeresult, NULL);
132 ast_strftime(strStartTime, sizeof(strStartTime), DATE_FORMAT, &timeresult);
134 if (cdr->answer.tv_sec) {
135 ast_localtime(&cdr->answer, &timeresult, NULL);
136 ast_strftime(strAnswerTime, sizeof(strAnswerTime), DATE_FORMAT, &timeresult);
139 ast_localtime(&cdr->end, &timeresult, NULL);
140 ast_strftime(strEndTime, sizeof(strEndTime), DATE_FORMAT, &timeresult);
143 /* Custom fields handling */
144 if (customfields != NULL && customfields->used > 0) {
145 memset(&dummy, 0, sizeof(dummy));
147 pbx_substitute_variables_helper(&dummy, customfields->str, buf, sizeof(buf) - 1);
150 manager_event(EVENT_FLAG_CALL, "Cdr",
151 "AccountCode: %s\r\n"
153 "Destination: %s\r\n"
154 "DestinationContext: %s\r\n"
157 "DestinationChannel: %s\r\n"
158 "LastApplication: %s\r\n"
164 "BillableSeconds: %ld\r\n"
165 "Disposition: %s\r\n"
170 cdr->accountcode, cdr->src, cdr->dst, cdr->dcontext, cdr->clid, cdr->channel,
171 cdr->dstchannel, cdr->lastapp, cdr->lastdata, strStartTime, strAnswerTime, strEndTime,
172 cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition),
173 ast_cdr_flags2str(cdr->amaflags), cdr->uniqueid, cdr->userfield,buf);
178 static int unload_module(void)
180 ast_cdr_unregister(name);
182 ast_free(customfields);
187 static int load_module(void)
189 /* Configuration file */
191 return AST_MODULE_LOAD_DECLINE;
193 return AST_MODULE_LOAD_SUCCESS;
196 static int reload(void)
198 return load_config(1);
201 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Asterisk Manager Interface CDR Backend",
203 .unload = unload_module,