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