support configurable batch posting of CDRs (off by default) (bug #3883)
[asterisk/asterisk.git] / cdr / cdr_manager.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Asterisk Call Manager CDR records.
5  * 
6  * This program is free software, distributed under the terms of
7  * the GNU General Public License.
8  *
9  */
10
11 #include <sys/types.h>
12 #include "asterisk/channel.h"
13 #include "asterisk/cdr.h"
14 #include "asterisk/module.h"
15 #include "asterisk/logger.h"
16 #include "asterisk/utils.h"
17 #include "asterisk/manager.h"
18 #include "asterisk/config.h"
19 #include "asterisk.h"
20 #include "astconf.h"
21 #include <strings.h>
22 #include <unistd.h>
23 #include <time.h>
24
25 #define DATE_FORMAT     "%Y-%m-%d %T"
26 #define CONF_FILE       "cdr_manager.conf"
27
28 static char *desc = "Asterisk Call Manager CDR Backend";
29 static char *name = "cdr_manager";
30
31 static int enablecdr = 0;
32
33 static void loadconfigurationfile(void)
34 {
35         char *cat;
36         struct ast_config *cfg;
37         struct ast_variable *v;
38         
39         cfg = ast_config_load(CONF_FILE);
40         if (!cfg) {
41                 /* Standard configuration */
42                 enablecdr = 0;
43                 return;
44         }
45         
46         cat = ast_category_browse(cfg, NULL);
47         while (cat) {
48                 if (!strcasecmp(cat, "general")) {
49                         v = ast_variable_browse(cfg, cat);
50                         while (v) {
51                                 if (!strcasecmp(v->name, "enabled")) {
52                                         enablecdr = ast_true(v->value);
53                                 }
54                                 
55                                 v = v->next;
56                         }
57                 }
58         
59                 /* Next category */
60                 cat = ast_category_browse(cfg, cat);
61         }
62         
63         ast_config_destroy(cfg);
64 }
65
66 static int manager_log(struct ast_cdr *cdr)
67 {
68         time_t t;
69         struct tm timeresult;
70         char strStartTime[80] = "";
71         char strAnswerTime[80] = "";
72         char strEndTime[80] = "";
73         
74         if (!enablecdr)
75                 return 0;
76
77         t = cdr->start.tv_sec;
78         localtime_r(&t, &timeresult);
79         strftime(strStartTime, sizeof(strStartTime), DATE_FORMAT, &timeresult);
80         
81         if (cdr->answer.tv_sec) {
82                 t = cdr->answer.tv_sec;
83                 localtime_r(&t, &timeresult);
84                 strftime(strAnswerTime, sizeof(strAnswerTime), DATE_FORMAT, &timeresult);
85         }
86
87         t = cdr->end.tv_sec;
88         localtime_r(&t, &timeresult);
89         strftime(strEndTime, sizeof(strEndTime), DATE_FORMAT, &timeresult);
90
91         manager_event(EVENT_FLAG_CALL, "Cdr",
92             "AccountCode: %s\r\n"
93             "Source: %s\r\n"
94             "Destination: %s\r\n"
95             "DestinationContext: %s\r\n"
96             "CallerID: %s\r\n"
97             "Channel: %s\r\n"
98             "DestinationChannel: %s\r\n"
99             "LastApplication: %s\r\n"
100             "LastData: %s\r\n"
101             "StartTime: %s\r\n"
102             "AnswerTime: %s\r\n"
103             "EndTime: %s\r\n"
104             "Duration: %d\r\n"
105             "BillableSeconds: %d\r\n"
106             "Disposition: %s\r\n"
107             "AMAFlags: %s\r\n"
108             "UniqueID: %s\r\n"
109             "UserField: %s\r\n",
110             cdr->accountcode, cdr->src, cdr->dst, cdr->dcontext, cdr->clid, cdr->channel,
111             cdr->dstchannel, cdr->lastapp, cdr->lastdata, strStartTime, strAnswerTime, strEndTime,
112             cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition), 
113             ast_cdr_flags2str(cdr->amaflags), cdr->uniqueid, cdr->userfield);
114                 
115         return 0;
116 }
117
118 char *description(void)
119 {
120         return desc;
121 }
122
123 int unload_module(void)
124 {
125         ast_cdr_unregister(name);
126         return 0;
127 }
128
129 int load_module(void)
130 {
131         int res;
132
133         /* Configuration file */
134         loadconfigurationfile();
135         
136         res = ast_cdr_register(name, desc, manager_log);
137         if (res) {
138                 ast_log(LOG_ERROR, "Unable to register Asterisk Call Manager CDR handling\n");
139         }
140         
141         return res;
142 }
143
144 int reload(void)
145 {
146         loadconfigurationfile();
147         return 0;
148 }
149
150 int usecount(void)
151 {
152         return 0;
153 }
154
155 char *key()
156 {
157         return ASTERISK_GPL_KEY;
158 }