Minor formatting change to test a mantis change ...
[asterisk-bristuff.git] / cdr / cdr_manager.c
blob8b2ab215d4d180c13a10db2a6e52520d1646ea10
1 /*
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.
17 /*! \file
19 * \brief Asterisk Call Manager CDR records.
21 * See also
22 * \arg \ref AstCDR
23 * \arg \ref AstAMI
24 * \arg \ref Config_ami
25 * \ingroup cdr_drivers
28 #include "asterisk.h"
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
32 #include <time.h>
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;
49 struct ast_str *customfields;
51 static int manager_log(struct ast_cdr *cdr);
53 static int load_config(int reload)
55 char *cat = NULL;
56 struct ast_config *cfg;
57 struct ast_variable *v;
58 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
59 int newenablecdr = 0;
61 cfg = ast_config_load(CONF_FILE, config_flags);
62 if (cfg == CONFIG_STATUS_FILEUNCHANGED)
63 return 0;
65 if (reload && customfields) {
66 ast_free(customfields);
68 customfields = NULL;
70 if (!cfg) {
71 /* Standard configuration */
72 ast_log(LOG_WARNING, "Failed to load configuration file. Module not activated.\n");
73 if (enablecdr)
74 ast_cdr_unregister(name);
75 enablecdr = 0;
76 return 0;
79 while ( (cat = ast_category_browse(cfg, cat)) ) {
80 if (!strcasecmp(cat, "general")) {
81 v = ast_variable_browse(cfg, cat);
82 while (v) {
83 if (!strcasecmp(v->name, "enabled"))
84 newenablecdr = ast_true(v->value);
86 v = v->next;
88 } else if (!strcasecmp(cat, "mappings")) {
89 customfields = ast_str_create(CUSTOM_FIELDS_BUF_SIZE);
90 v = ast_variable_browse(cfg, cat);
91 while (v) {
92 if (customfields && !ast_strlen_zero(v->name) && !ast_strlen_zero(v->value)) {
93 if( (customfields->used + strlen(v->value) + strlen(v->name) + 14) < customfields->len) {
94 ast_str_append(&customfields, -1, "%s: ${CDR(%s)}\r\n", v->value, v->name);
95 ast_log(LOG_NOTICE, "Added mapping %s: ${CDR(%s)}\n", v->value, v->name);
96 } else {
97 ast_log(LOG_WARNING, "No more buffer space to add other custom fields\n");
98 break;
102 v = v->next;
107 ast_config_destroy(cfg);
109 if (enablecdr && !newenablecdr)
110 ast_cdr_unregister(name);
111 else if (!enablecdr && newenablecdr)
112 ast_cdr_register(name, "Asterisk Manager Interface CDR Backend", manager_log);
113 enablecdr = newenablecdr;
115 return 1;
118 static int manager_log(struct ast_cdr *cdr)
120 struct ast_tm timeresult;
121 char strStartTime[80] = "";
122 char strAnswerTime[80] = "";
123 char strEndTime[80] = "";
124 char buf[CUSTOM_FIELDS_BUF_SIZE];
125 struct ast_channel dummy;
127 if (!enablecdr)
128 return 0;
130 ast_localtime(&cdr->start, &timeresult, NULL);
131 ast_strftime(strStartTime, sizeof(strStartTime), DATE_FORMAT, &timeresult);
133 if (cdr->answer.tv_sec) {
134 ast_localtime(&cdr->answer, &timeresult, NULL);
135 ast_strftime(strAnswerTime, sizeof(strAnswerTime), DATE_FORMAT, &timeresult);
138 ast_localtime(&cdr->end, &timeresult, NULL);
139 ast_strftime(strEndTime, sizeof(strEndTime), DATE_FORMAT, &timeresult);
141 buf[0] = 0;
142 /* Custom fields handling */
143 if (customfields != NULL && customfields->used > 0) {
144 memset(&dummy, 0, sizeof(dummy));
145 dummy.cdr = cdr;
146 pbx_substitute_variables_helper(&dummy, customfields->str, buf, sizeof(buf) - 1);
149 manager_event(EVENT_FLAG_CDR, "Cdr",
150 "AccountCode: %s\r\n"
151 "Source: %s\r\n"
152 "Destination: %s\r\n"
153 "DestinationContext: %s\r\n"
154 "CallerID: %s\r\n"
155 "Channel: %s\r\n"
156 "DestinationChannel: %s\r\n"
157 "LastApplication: %s\r\n"
158 "LastData: %s\r\n"
159 "StartTime: %s\r\n"
160 "AnswerTime: %s\r\n"
161 "EndTime: %s\r\n"
162 "Duration: %ld\r\n"
163 "BillableSeconds: %ld\r\n"
164 "Disposition: %s\r\n"
165 "AMAFlags: %s\r\n"
166 "UniqueID: %s\r\n"
167 "UserField: %s\r\n"
168 "%s",
169 cdr->accountcode, cdr->src, cdr->dst, cdr->dcontext, cdr->clid, cdr->channel,
170 cdr->dstchannel, cdr->lastapp, cdr->lastdata, strStartTime, strAnswerTime, strEndTime,
171 cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition),
172 ast_cdr_flags2str(cdr->amaflags), cdr->uniqueid, cdr->userfield,buf);
174 return 0;
177 static int unload_module(void)
179 ast_cdr_unregister(name);
180 if (customfields)
181 ast_free(customfields);
183 return 0;
186 static int load_module(void)
188 /* Configuration file */
189 if (!load_config(0))
190 return AST_MODULE_LOAD_DECLINE;
192 return AST_MODULE_LOAD_SUCCESS;
195 static int reload(void)
197 return load_config(1);
200 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Asterisk Manager Interface CDR Backend",
201 .load = load_module,
202 .unload = unload_module,
203 .reload = reload,