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