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