more license/copyright header updates (thanks Ian!)
[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  *
19  * Asterisk Call Manager CDR records.
20  * 
21  */
22
23 #include <sys/types.h>
24 #include <strings.h>
25 #include <unistd.h>
26 #include <time.h>
27
28 #include "asterisk.h"
29
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
31
32 #include "asterisk/channel.h"
33 #include "asterisk/cdr.h"
34 #include "asterisk/module.h"
35 #include "asterisk/logger.h"
36 #include "asterisk/utils.h"
37 #include "asterisk/manager.h"
38 #include "asterisk/config.h"
39
40 #define DATE_FORMAT     "%Y-%m-%d %T"
41 #define CONF_FILE       "cdr_manager.conf"
42
43 static char *desc = "Asterisk Call Manager CDR Backend";
44 static char *name = "cdr_manager";
45
46 static int enablecdr = 0;
47
48 static void loadconfigurationfile(void)
49 {
50         char *cat;
51         struct ast_config *cfg;
52         struct ast_variable *v;
53         
54         cfg = ast_config_load(CONF_FILE);
55         if (!cfg) {
56                 /* Standard configuration */
57                 enablecdr = 0;
58                 return;
59         }
60         
61         cat = ast_category_browse(cfg, NULL);
62         while (cat) {
63                 if (!strcasecmp(cat, "general")) {
64                         v = ast_variable_browse(cfg, cat);
65                         while (v) {
66                                 if (!strcasecmp(v->name, "enabled")) {
67                                         enablecdr = ast_true(v->value);
68                                 }
69                                 
70                                 v = v->next;
71                         }
72                 }
73         
74                 /* Next category */
75                 cat = ast_category_browse(cfg, cat);
76         }
77         
78         ast_config_destroy(cfg);
79 }
80
81 static int manager_log(struct ast_cdr *cdr)
82 {
83         time_t t;
84         struct tm timeresult;
85         char strStartTime[80] = "";
86         char strAnswerTime[80] = "";
87         char strEndTime[80] = "";
88         
89         if (!enablecdr)
90                 return 0;
91
92         t = cdr->start.tv_sec;
93         localtime_r(&t, &timeresult);
94         strftime(strStartTime, sizeof(strStartTime), DATE_FORMAT, &timeresult);
95         
96         if (cdr->answer.tv_sec) {
97                 t = cdr->answer.tv_sec;
98                 localtime_r(&t, &timeresult);
99                 strftime(strAnswerTime, sizeof(strAnswerTime), DATE_FORMAT, &timeresult);
100         }
101
102         t = cdr->end.tv_sec;
103         localtime_r(&t, &timeresult);
104         strftime(strEndTime, sizeof(strEndTime), DATE_FORMAT, &timeresult);
105
106         manager_event(EVENT_FLAG_CALL, "Cdr",
107             "AccountCode: %s\r\n"
108             "Source: %s\r\n"
109             "Destination: %s\r\n"
110             "DestinationContext: %s\r\n"
111             "CallerID: %s\r\n"
112             "Channel: %s\r\n"
113             "DestinationChannel: %s\r\n"
114             "LastApplication: %s\r\n"
115             "LastData: %s\r\n"
116             "StartTime: %s\r\n"
117             "AnswerTime: %s\r\n"
118             "EndTime: %s\r\n"
119             "Duration: %d\r\n"
120             "BillableSeconds: %d\r\n"
121             "Disposition: %s\r\n"
122             "AMAFlags: %s\r\n"
123             "UniqueID: %s\r\n"
124             "UserField: %s\r\n",
125             cdr->accountcode, cdr->src, cdr->dst, cdr->dcontext, cdr->clid, cdr->channel,
126             cdr->dstchannel, cdr->lastapp, cdr->lastdata, strStartTime, strAnswerTime, strEndTime,
127             cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition), 
128             ast_cdr_flags2str(cdr->amaflags), cdr->uniqueid, cdr->userfield);
129                 
130         return 0;
131 }
132
133 char *description(void)
134 {
135         return desc;
136 }
137
138 int unload_module(void)
139 {
140         ast_cdr_unregister(name);
141         return 0;
142 }
143
144 int load_module(void)
145 {
146         int res;
147
148         /* Configuration file */
149         loadconfigurationfile();
150         
151         res = ast_cdr_register(name, desc, manager_log);
152         if (res) {
153                 ast_log(LOG_ERROR, "Unable to register Asterisk Call Manager CDR handling\n");
154         }
155         
156         return res;
157 }
158
159 int reload(void)
160 {
161         loadconfigurationfile();
162         return 0;
163 }
164
165 int usecount(void)
166 {
167         return 0;
168 }
169
170 char *key()
171 {
172         return ASTERISK_GPL_KEY;
173 }