Doxygen Updates - Title update
[asterisk/asterisk.git] / cdr / cdr_manager.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2004 - 2005
5  *
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.
11  *
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.
15  */
16
17 /*!
18  * \file
19  * \brief Asterisk Call Manager CDR records.
20  *
21  * See also
22  * \arg \ref AstCDR
23  * \arg \ref AstAMI
24  * \arg \ref Config_ami
25  * \ingroup cdr_drivers
26  */
27
28 /*! \li \ref cdr_manager.c uses the configuration file \ref cdr_manager.conf
29  * \addtogroup configuration_file Configuration Files
30  */
31
32 /*!
33  * \page cdr_manager.conf cdr_manager.conf
34  * \verbinclude cdr_manager.conf.sample
35  */
36
37 /*** MODULEINFO
38         <support_level>core</support_level>
39  ***/
40
41 #include "asterisk.h"
42
43 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
44
45 #include <time.h>
46
47 #include "asterisk/channel.h"
48 #include "asterisk/cdr.h"
49 #include "asterisk/module.h"
50 #include "asterisk/utils.h"
51 #include "asterisk/manager.h"
52 #include "asterisk/config.h"
53 #include "asterisk/pbx.h"
54
55 #define DATE_FORMAT     "%Y-%m-%d %T"
56 #define CONF_FILE       "cdr_manager.conf"
57 #define CUSTOM_FIELDS_BUF_SIZE 1024
58
59 static const char name[] = "cdr_manager";
60
61 static int enablecdr = 0;
62
63 static struct ast_str *customfields;
64 AST_RWLOCK_DEFINE_STATIC(customfields_lock);
65
66 static int manager_log(struct ast_cdr *cdr);
67
68 static int load_config(int reload)
69 {
70         char *cat = NULL;
71         struct ast_config *cfg;
72         struct ast_variable *v;
73         struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
74         int newenablecdr = 0;
75
76         cfg = ast_config_load(CONF_FILE, config_flags);
77         if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
78                 return 0;
79         }
80
81         if (cfg == CONFIG_STATUS_FILEINVALID) {
82                 ast_log(LOG_ERROR, "Config file '%s' could not be parsed\n", CONF_FILE);
83                 return -1;
84         }
85
86         if (!cfg) {
87                 /* Standard configuration */
88                 ast_log(LOG_WARNING, "Failed to load configuration file. Module not activated.\n");
89                 if (enablecdr)
90                         ast_cdr_unregister(name);
91                 enablecdr = 0;
92                 return -1;
93         }
94
95         if (reload) {
96                 ast_rwlock_wrlock(&customfields_lock);
97         }
98
99         if (reload && customfields) {
100                 ast_free(customfields);
101                 customfields = NULL;
102         }
103
104         while ( (cat = ast_category_browse(cfg, cat)) ) {
105                 if (!strcasecmp(cat, "general")) {
106                         v = ast_variable_browse(cfg, cat);
107                         while (v) {
108                                 if (!strcasecmp(v->name, "enabled"))
109                                         newenablecdr = ast_true(v->value);
110
111                                 v = v->next;
112                         }
113                 } else if (!strcasecmp(cat, "mappings")) {
114                         customfields = ast_str_create(CUSTOM_FIELDS_BUF_SIZE);
115                         v = ast_variable_browse(cfg, cat);
116                         while (v) {
117                                 if (customfields && !ast_strlen_zero(v->name) && !ast_strlen_zero(v->value)) {
118                                         if ((ast_str_strlen(customfields) + strlen(v->value) + strlen(v->name) + 14) < ast_str_size(customfields)) {
119                                                 ast_str_append(&customfields, -1, "%s: ${CDR(%s)}\r\n", v->value, v->name);
120                                                 ast_log(LOG_NOTICE, "Added mapping %s: ${CDR(%s)}\n", v->value, v->name);
121                                         } else {
122                                                 ast_log(LOG_WARNING, "No more buffer space to add other custom fields\n");
123                                                 break;
124                                         }
125
126                                 }
127                                 v = v->next;
128                         }
129                 }
130         }
131
132         if (reload) {
133                 ast_rwlock_unlock(&customfields_lock);
134         }
135
136         ast_config_destroy(cfg);
137
138         if (enablecdr && !newenablecdr)
139                 ast_cdr_unregister(name);
140         else if (!enablecdr && newenablecdr)
141                 ast_cdr_register(name, "Asterisk Manager Interface CDR Backend", manager_log);
142         enablecdr = newenablecdr;
143
144         return 0;
145 }
146
147 static int manager_log(struct ast_cdr *cdr)
148 {
149         struct ast_tm timeresult;
150         char strStartTime[80] = "";
151         char strAnswerTime[80] = "";
152         char strEndTime[80] = "";
153         char buf[CUSTOM_FIELDS_BUF_SIZE];
154
155         if (!enablecdr)
156                 return 0;
157
158         ast_localtime(&cdr->start, &timeresult, NULL);
159         ast_strftime(strStartTime, sizeof(strStartTime), DATE_FORMAT, &timeresult);
160
161         if (cdr->answer.tv_sec) {
162                 ast_localtime(&cdr->answer, &timeresult, NULL);
163                 ast_strftime(strAnswerTime, sizeof(strAnswerTime), DATE_FORMAT, &timeresult);
164         }
165
166         ast_localtime(&cdr->end, &timeresult, NULL);
167         ast_strftime(strEndTime, sizeof(strEndTime), DATE_FORMAT, &timeresult);
168
169         buf[0] = '\0';
170         ast_rwlock_rdlock(&customfields_lock);
171         if (customfields && ast_str_strlen(customfields)) {
172                 struct ast_channel *dummy = ast_dummy_channel_alloc();
173                 if (!dummy) {
174                         ast_log(LOG_ERROR, "Unable to allocate channel for variable substitution.\n");
175                         return 0;
176                 }
177                 ast_channel_cdr_set(dummy, ast_cdr_dup(cdr));
178                 pbx_substitute_variables_helper(dummy, ast_str_buffer(customfields), buf, sizeof(buf) - 1);
179                 ast_channel_unref(dummy);
180         }
181         ast_rwlock_unlock(&customfields_lock);
182
183         manager_event(EVENT_FLAG_CDR, "Cdr",
184             "AccountCode: %s\r\n"
185             "Source: %s\r\n"
186             "Destination: %s\r\n"
187             "DestinationContext: %s\r\n"
188             "CallerID: %s\r\n"
189             "Channel: %s\r\n"
190             "DestinationChannel: %s\r\n"
191             "LastApplication: %s\r\n"
192             "LastData: %s\r\n"
193             "StartTime: %s\r\n"
194             "AnswerTime: %s\r\n"
195             "EndTime: %s\r\n"
196             "Duration: %ld\r\n"
197             "BillableSeconds: %ld\r\n"
198             "Disposition: %s\r\n"
199             "AMAFlags: %s\r\n"
200             "UniqueID: %s\r\n"
201             "UserField: %s\r\n"
202             "%s",
203             cdr->accountcode, cdr->src, cdr->dst, cdr->dcontext, cdr->clid, cdr->channel,
204             cdr->dstchannel, cdr->lastapp, cdr->lastdata, strStartTime, strAnswerTime, strEndTime,
205             cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition),
206             ast_cdr_flags2str(cdr->amaflags), cdr->uniqueid, cdr->userfield,buf);
207
208         return 0;
209 }
210
211 static int unload_module(void)
212 {
213         ast_cdr_unregister(name);
214         if (customfields)
215                 ast_free(customfields);
216
217         return 0;
218 }
219
220 static int load_module(void)
221 {
222         if (load_config(0)) {
223                 return AST_MODULE_LOAD_DECLINE;
224         }
225
226         return AST_MODULE_LOAD_SUCCESS;
227 }
228
229 static int reload(void)
230 {
231         return load_config(1);
232 }
233
234 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Asterisk Manager Interface CDR Backend",
235                 .load = load_module,
236                 .unload = unload_module,
237                 .reload = reload,
238                 .load_pri = AST_MODPRI_CDR_DRIVER,
239                );