49f826d9eaf339df9701278730532af826d9674f
[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 /*! \file
18  *
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 #include "asterisk.h"
29
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
31
32 #include <time.h>
33
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"
41
42 #define DATE_FORMAT     "%Y-%m-%d %T"
43 #define CONF_FILE       "cdr_manager.conf"
44 #define CUSTOM_FIELDS_BUF_SIZE 1024
45
46 static char *name = "cdr_manager";
47
48 static int enablecdr = 0;
49 static struct ast_str *customfields;
50
51 static int manager_log(struct ast_cdr *cdr);
52
53 static int load_config(int reload)
54 {
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;
60
61         cfg = ast_config_load(CONF_FILE, config_flags);
62         if (cfg == CONFIG_STATUS_FILEUNCHANGED)
63                 return 0;
64
65         if (cfg == CONFIG_STATUS_FILEINVALID) {
66                 ast_log(LOG_ERROR, "Config file '%s' could not be parsed\n", CONF_FILE);
67                 return 1;
68         }
69
70         if (reload && customfields) {
71                 ast_free(customfields);
72         }
73         customfields = NULL;
74
75         if (!cfg) {
76                 /* Standard configuration */
77                 ast_log(LOG_WARNING, "Failed to load configuration file. Module not activated.\n");
78                 if (enablecdr)
79                         ast_cdr_unregister(name);
80                 enablecdr = 0;
81                 return 0;
82         }
83
84         while ( (cat = ast_category_browse(cfg, cat)) ) {
85                 if (!strcasecmp(cat, "general")) {
86                         v = ast_variable_browse(cfg, cat);
87                         while (v) {
88                                 if (!strcasecmp(v->name, "enabled"))
89                                         newenablecdr = ast_true(v->value);
90
91                                 v = v->next;
92                         }
93                 } else if (!strcasecmp(cat, "mappings")) {
94                         customfields = ast_str_create(CUSTOM_FIELDS_BUF_SIZE);
95                         v = ast_variable_browse(cfg, cat);
96                         while (v) {
97                                 if (customfields && !ast_strlen_zero(v->name) && !ast_strlen_zero(v->value)) {
98                                         if ((ast_str_strlen(customfields) + strlen(v->value) + strlen(v->name) + 14) < ast_str_size(customfields)) {
99                                                 ast_str_append(&customfields, -1, "%s: ${CDR(%s)}\r\n", v->value, v->name);
100                                                 ast_log(LOG_NOTICE, "Added mapping %s: ${CDR(%s)}\n", v->value, v->name);
101                                         } else {
102                                                 ast_log(LOG_WARNING, "No more buffer space to add other custom fields\n");
103                                                 break;
104                                         }
105
106                                 }
107                                 v = v->next;
108                         }
109                 }
110         }
111
112         ast_config_destroy(cfg);
113
114         if (enablecdr && !newenablecdr)
115                 ast_cdr_unregister(name);
116         else if (!enablecdr && newenablecdr)
117                 ast_cdr_register(name, "Asterisk Manager Interface CDR Backend", manager_log);
118         enablecdr = newenablecdr;
119
120         return 1;
121 }
122
123 static int manager_log(struct ast_cdr *cdr)
124 {
125         struct ast_tm timeresult;
126         char strStartTime[80] = "";
127         char strAnswerTime[80] = "";
128         char strEndTime[80] = "";
129         char buf[CUSTOM_FIELDS_BUF_SIZE];
130
131         if (!enablecdr)
132                 return 0;
133
134         ast_localtime(&cdr->start, &timeresult, NULL);
135         ast_strftime(strStartTime, sizeof(strStartTime), DATE_FORMAT, &timeresult);
136
137         if (cdr->answer.tv_sec) {
138                 ast_localtime(&cdr->answer, &timeresult, NULL);
139                 ast_strftime(strAnswerTime, sizeof(strAnswerTime), DATE_FORMAT, &timeresult);
140         }
141
142         ast_localtime(&cdr->end, &timeresult, NULL);
143         ast_strftime(strEndTime, sizeof(strEndTime), DATE_FORMAT, &timeresult);
144
145         buf[0] = 0;
146         /* Custom fields handling */
147         if (customfields != NULL && ast_str_strlen(customfields)) {
148                 struct ast_channel *dummy = ast_channel_alloc(0, 0, "", "", "", "", "", 0, "Substitution/%p", cdr);
149                 if (!dummy) {
150                         ast_log(LOG_ERROR, "Unable to allocate channel for variable substitution.\n");
151                         return 0;
152                 }
153                 dummy->cdr = ast_cdr_dup(cdr);
154                 pbx_substitute_variables_helper(dummy, ast_str_buffer(customfields), buf, sizeof(buf) - 1);
155                 ast_channel_release(dummy);
156         }
157
158         manager_event(EVENT_FLAG_CDR, "Cdr",
159             "AccountCode: %s\r\n"
160             "Source: %s\r\n"
161             "Destination: %s\r\n"
162             "DestinationContext: %s\r\n"
163             "CallerID: %s\r\n"
164             "Channel: %s\r\n"
165             "DestinationChannel: %s\r\n"
166             "LastApplication: %s\r\n"
167             "LastData: %s\r\n"
168             "StartTime: %s\r\n"
169             "AnswerTime: %s\r\n"
170             "EndTime: %s\r\n"
171             "Duration: %ld\r\n"
172             "BillableSeconds: %ld\r\n"
173             "Disposition: %s\r\n"
174             "AMAFlags: %s\r\n"
175             "UniqueID: %s\r\n"
176             "UserField: %s\r\n"
177             "%s",
178             cdr->accountcode, cdr->src, cdr->dst, cdr->dcontext, cdr->clid, cdr->channel,
179             cdr->dstchannel, cdr->lastapp, cdr->lastdata, strStartTime, strAnswerTime, strEndTime,
180             cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition),
181             ast_cdr_flags2str(cdr->amaflags), cdr->uniqueid, cdr->userfield,buf);
182
183         return 0;
184 }
185
186 static int unload_module(void)
187 {
188         ast_cdr_unregister(name);
189         if (customfields)
190                 ast_free(customfields);
191
192         return 0;
193 }
194
195 static int load_module(void)
196 {
197         /* Configuration file */
198         if (!load_config(0))
199                 return AST_MODULE_LOAD_DECLINE;
200
201         return AST_MODULE_LOAD_SUCCESS;
202 }
203
204 static int reload(void)
205 {
206         return load_config(1);
207 }
208
209 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Asterisk Manager Interface CDR Backend",
210                 .load = load_module,
211                 .unload = unload_module,
212                 .reload = reload,
213                );