6e3dd87a372d837d41281899ada07a4a92e07330
[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 <sys/types.h>
33 #include <strings.h>
34 #include <unistd.h>
35 #include <time.h>
36
37 #include "asterisk/channel.h"
38 #include "asterisk/cdr.h"
39 #include "asterisk/module.h"
40 #include "asterisk/logger.h"
41 #include "asterisk/utils.h"
42 #include "asterisk/manager.h"
43 #include "asterisk/config.h"
44 #include "asterisk/pbx.h"
45
46 #define DATE_FORMAT     "%Y-%m-%d %T"
47 #define CONF_FILE       "cdr_manager.conf"
48 #define CUSTOM_FIELDS_BUF_SIZE 1024
49
50 static char *name = "cdr_manager";
51
52 static int enablecdr = 0;
53 struct ast_str *customfields;
54
55 static int manager_log(struct ast_cdr *cdr);
56
57 static int load_config(int reload)
58 {
59         char *cat = NULL;
60         struct ast_config *cfg;
61         struct ast_variable *v;
62         struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
63         int newenablecdr = 0;
64
65         cfg = ast_config_load(CONF_FILE, config_flags);
66         if (cfg == CONFIG_STATUS_FILEUNCHANGED)
67                 return 0;
68
69         if (reload && customfields) {
70                 ast_free(customfields);
71         }
72         customfields = NULL;
73
74         if (!cfg) {
75                 /* Standard configuration */
76                 ast_log(LOG_WARNING, "Failed to load configuration file. Module not activated.\n");
77                 if (enablecdr)
78                         ast_cdr_unregister(name);
79                 enablecdr = 0;
80                 return 0;
81         }
82         
83         while ( (cat = ast_category_browse(cfg, cat)) ) {
84                 if (!strcasecmp(cat, "general")) {
85                         v = ast_variable_browse(cfg, cat);
86                         while (v) {
87                                 if (!strcasecmp(v->name, "enabled"))
88                                         newenablecdr = ast_true(v->value);
89                                 
90                                 v = v->next;
91                         }
92                 } else if (!strcasecmp(cat, "mappings")) {
93                         customfields = ast_str_create(CUSTOM_FIELDS_BUF_SIZE);
94                         v = ast_variable_browse(cfg, cat);
95                         while (v) {
96                                 if (customfields && !ast_strlen_zero(v->name) && !ast_strlen_zero(v->value)) {
97                                         if( (customfields->used + strlen(v->value) + strlen(v->name) + 14) < customfields->len) {
98                                                 ast_str_append(&customfields, -1, "%s: ${CDR(%s)}\r\n", v->value, v->name);
99                                                 ast_log(LOG_NOTICE, "Added mapping %s: ${CDR(%s)}\n", v->value, v->name);
100                                         } else {
101                                                 ast_log(LOG_WARNING, "No more buffer space to add other custom fields\n");
102                                                 break;
103                                         }
104                                         
105                                 }
106                                 v = v->next;
107                         }
108                 }
109         }
110         
111         ast_config_destroy(cfg);
112
113         if (enablecdr && !newenablecdr)
114                 ast_cdr_unregister(name);
115         else if (!enablecdr && newenablecdr)
116                 ast_cdr_register(name, "Asterisk Manager Interface CDR Backend", manager_log);
117         enablecdr = newenablecdr;
118
119         return 1;
120 }
121
122 static int manager_log(struct ast_cdr *cdr)
123 {
124         struct ast_tm timeresult;
125         char strStartTime[80] = "";
126         char strAnswerTime[80] = "";
127         char strEndTime[80] = "";
128         char buf[CUSTOM_FIELDS_BUF_SIZE];
129         struct ast_channel dummy;
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         /* Custom fields handling */
146         memset(buf, 0 , sizeof(buf));
147         if (customfields != NULL && customfields->used > 0) {
148                 memset(&dummy, 0, sizeof(dummy));
149                 dummy.cdr = cdr;
150                 pbx_substitute_variables_helper(&dummy, customfields->str, buf, sizeof(buf) - 1);
151         }
152
153         manager_event(EVENT_FLAG_CALL, "Cdr",
154             "AccountCode: %s\r\n"
155             "Source: %s\r\n"
156             "Destination: %s\r\n"
157             "DestinationContext: %s\r\n"
158             "CallerID: %s\r\n"
159             "Channel: %s\r\n"
160             "DestinationChannel: %s\r\n"
161             "LastApplication: %s\r\n"
162             "LastData: %s\r\n"
163             "StartTime: %s\r\n"
164             "AnswerTime: %s\r\n"
165             "EndTime: %s\r\n"
166             "Duration: %ld\r\n"
167             "BillableSeconds: %ld\r\n"
168             "Disposition: %s\r\n"
169             "AMAFlags: %s\r\n"
170             "UniqueID: %s\r\n"
171             "UserField: %s\r\n"
172             "%s",
173             cdr->accountcode, cdr->src, cdr->dst, cdr->dcontext, cdr->clid, cdr->channel,
174             cdr->dstchannel, cdr->lastapp, cdr->lastdata, strStartTime, strAnswerTime, strEndTime,
175             cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition),
176             ast_cdr_flags2str(cdr->amaflags), cdr->uniqueid, cdr->userfield,buf);
177
178         return 0;
179 }
180
181 static int unload_module(void)
182 {
183         ast_cdr_unregister(name);
184         if (customfields)
185                 ast_free(customfields);
186
187         return 0;
188 }
189
190 static int load_module(void)
191 {
192         /* Configuration file */
193         if (!load_config(0))
194                 return AST_MODULE_LOAD_DECLINE;
195
196         return AST_MODULE_LOAD_SUCCESS;
197 }
198
199 static int reload(void)
200 {
201         return load_config(1);
202 }
203
204 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Asterisk Manager Interface CDR Backend",
205                 .load = load_module,
206                 .unload = unload_module,
207                 .reload = reload,
208                );