Start untangling header inclusion in a way that does not affect
[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/logger.h"
38 #include "asterisk/utils.h"
39 #include "asterisk/manager.h"
40 #include "asterisk/config.h"
41 #include "asterisk/pbx.h"
42
43 #define DATE_FORMAT     "%Y-%m-%d %T"
44 #define CONF_FILE       "cdr_manager.conf"
45 #define CUSTOM_FIELDS_BUF_SIZE 1024
46
47 static char *name = "cdr_manager";
48
49 static int enablecdr = 0;
50 struct ast_str *customfields;
51
52 static int manager_log(struct ast_cdr *cdr);
53
54 static int load_config(int reload)
55 {
56         char *cat = NULL;
57         struct ast_config *cfg;
58         struct ast_variable *v;
59         struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
60         int newenablecdr = 0;
61
62         cfg = ast_config_load(CONF_FILE, config_flags);
63         if (cfg == CONFIG_STATUS_FILEUNCHANGED)
64                 return 0;
65
66         if (reload && customfields) {
67                 ast_free(customfields);
68         }
69         customfields = NULL;
70
71         if (!cfg) {
72                 /* Standard configuration */
73                 ast_log(LOG_WARNING, "Failed to load configuration file. Module not activated.\n");
74                 if (enablecdr)
75                         ast_cdr_unregister(name);
76                 enablecdr = 0;
77                 return 0;
78         }
79         
80         while ( (cat = ast_category_browse(cfg, cat)) ) {
81                 if (!strcasecmp(cat, "general")) {
82                         v = ast_variable_browse(cfg, cat);
83                         while (v) {
84                                 if (!strcasecmp(v->name, "enabled"))
85                                         newenablecdr = ast_true(v->value);
86                                 
87                                 v = v->next;
88                         }
89                 } else if (!strcasecmp(cat, "mappings")) {
90                         customfields = ast_str_create(CUSTOM_FIELDS_BUF_SIZE);
91                         v = ast_variable_browse(cfg, cat);
92                         while (v) {
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);
97                                         } else {
98                                                 ast_log(LOG_WARNING, "No more buffer space to add other custom fields\n");
99                                                 break;
100                                         }
101                                         
102                                 }
103                                 v = v->next;
104                         }
105                 }
106         }
107         
108         ast_config_destroy(cfg);
109
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;
115
116         return 1;
117 }
118
119 static int manager_log(struct ast_cdr *cdr)
120 {
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;
127
128         if (!enablecdr)
129                 return 0;
130
131         ast_localtime(&cdr->start, &timeresult, NULL);
132         ast_strftime(strStartTime, sizeof(strStartTime), DATE_FORMAT, &timeresult);
133         
134         if (cdr->answer.tv_sec) {
135                 ast_localtime(&cdr->answer, &timeresult, NULL);
136                 ast_strftime(strAnswerTime, sizeof(strAnswerTime), DATE_FORMAT, &timeresult);
137         }
138
139         ast_localtime(&cdr->end, &timeresult, NULL);
140         ast_strftime(strEndTime, sizeof(strEndTime), DATE_FORMAT, &timeresult);
141
142         buf[0] = 0;
143         /* Custom fields handling */
144         if (customfields != NULL && customfields->used > 0) {
145                 memset(&dummy, 0, sizeof(dummy));
146                 dummy.cdr = cdr;
147                 pbx_substitute_variables_helper(&dummy, customfields->str, buf, sizeof(buf) - 1);
148         }
149
150         manager_event(EVENT_FLAG_CALL, "Cdr",
151             "AccountCode: %s\r\n"
152             "Source: %s\r\n"
153             "Destination: %s\r\n"
154             "DestinationContext: %s\r\n"
155             "CallerID: %s\r\n"
156             "Channel: %s\r\n"
157             "DestinationChannel: %s\r\n"
158             "LastApplication: %s\r\n"
159             "LastData: %s\r\n"
160             "StartTime: %s\r\n"
161             "AnswerTime: %s\r\n"
162             "EndTime: %s\r\n"
163             "Duration: %ld\r\n"
164             "BillableSeconds: %ld\r\n"
165             "Disposition: %s\r\n"
166             "AMAFlags: %s\r\n"
167             "UniqueID: %s\r\n"
168             "UserField: %s\r\n"
169             "%s",
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);
174
175         return 0;
176 }
177
178 static int unload_module(void)
179 {
180         ast_cdr_unregister(name);
181         if (customfields)
182                 ast_free(customfields);
183
184         return 0;
185 }
186
187 static int load_module(void)
188 {
189         /* Configuration file */
190         if (!load_config(0))
191                 return AST_MODULE_LOAD_DECLINE;
192
193         return AST_MODULE_LOAD_SUCCESS;
194 }
195
196 static int reload(void)
197 {
198         return load_config(1);
199 }
200
201 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Asterisk Manager Interface CDR Backend",
202                 .load = load_module,
203                 .unload = unload_module,
204                 .reload = reload,
205                );