551b861d058dbb51724bb91a62ff71021daf9b6d
[asterisk/asterisk.git] / cel / cel_manager.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2008 - 2009, Digium, Inc.
5  *
6  * Steve Murphy <murf@digium.com>
7  * who freely borrowed code from the cdr equivalents
8  *     (see cdr/cdr_manager.c)
9  *
10  * See http://www.asterisk.org for more information about
11  * the Asterisk project. Please do not directly contact
12  * any of the maintainers of this project for assistance;
13  * the project provides a web site, mailing lists and IRC
14  * channels for your use.
15  *
16  * This program is free software, distributed under the terms of
17  * the GNU General Public License Version 2. See the LICENSE file
18  * at the top of the source tree.
19  */
20
21 /*! \file
22  *
23  * \brief Asterisk Channel Event records.
24  *
25  * See also
26  * \arg \ref AstCDR
27  * \arg \ref AstAMI
28  * \arg \ref Config_ami
29  * \ingroup cel_drivers
30  */
31
32 /*** MODULEINFO
33         <support_level>core</support_level>
34  ***/
35
36 #include "asterisk.h"
37
38 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
39
40 #include "asterisk/channel.h"
41 #include "asterisk/cel.h"
42 #include "asterisk/module.h"
43 #include "asterisk/logger.h"
44 #include "asterisk/utils.h"
45 #include "asterisk/manager.h"
46 #include "asterisk/config.h"
47
48 static const char DATE_FORMAT[] = "%Y-%m-%d %T";
49
50 static const char CONF_FILE[] = "cel.conf";
51
52 static int enablecel;
53
54 static struct ast_event_sub *event_sub;
55
56 static void manager_log(const struct ast_event *event, void *userdata)
57 {
58         struct ast_tm timeresult;
59         char start_time[80] = "";
60         struct ast_cel_event_record record = {
61                 .version = AST_CEL_EVENT_RECORD_VERSION,
62         };
63
64         if (ast_cel_fill_record(event, &record)) {
65                 return;
66         }
67
68         if (!enablecel) {
69                 return;
70         }
71
72         ast_localtime(&record.event_time, &timeresult, NULL);
73         ast_strftime(start_time, sizeof(start_time), DATE_FORMAT, &timeresult);
74
75         manager_event(EVENT_FLAG_CALL, "CEL",
76                 "EventName: %s\r\n"
77                 "AccountCode: %s\r\n"
78                 "CallerIDnum: %s\r\n"
79                 "CallerIDname: %s\r\n"
80                 "CallerIDani: %s\r\n"
81                 "CallerIDrdnis: %s\r\n"
82                 "CallerIDdnid: %s\r\n"
83                 "Exten: %s\r\n"
84                 "Context: %s\r\n"
85                 "Channel: %s\r\n"
86                 "Application: %s\r\n"
87                 "AppData: %s\r\n"
88                 "EventTime: %s\r\n"
89                 "AMAFlags: %s\r\n"
90                 "UniqueID: %s\r\n"
91                 "LinkedID: %s\r\n"
92                 "Userfield: %s\r\n"
93                 "Peer: %s\r\n",
94                 record.event_name, record.account_code, record.caller_id_num,
95                 record.caller_id_name, record.caller_id_ani, record.caller_id_rdnis,
96                 record.caller_id_dnid, record.extension, record.context, record.channel_name,
97                 record.application_name, record.application_data, start_time,
98                 ast_cel_get_ama_flag_name(record.amaflag), record.unique_id, record.linked_id,
99                 record.user_field, record.peer);
100 }
101
102 static int load_config(int reload)
103 {
104         const char *cat = NULL;
105         struct ast_config *cfg;
106         struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
107         struct ast_variable *v;
108         int newenablecel = 0;
109
110         cfg = ast_config_load(CONF_FILE, config_flags);
111         if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
112                 return 0;
113         }
114
115         if (!cfg) {
116                 ast_log(LOG_WARNING, "Failed to load configuration file. CEL manager Module not activated.\n");
117                 enablecel = 0;
118                 return -1;
119         }
120
121         while ((cat = ast_category_browse(cfg, cat))) {
122                 if (strcasecmp(cat, "manager")) {
123                         continue;
124                 }
125
126                 for (v = ast_variable_browse(cfg, cat); v; v = v->next) {
127                         if (!strcasecmp(v->name, "enabled")) {
128                                 newenablecel = ast_true(v->value);
129                         } else {
130                                 ast_log(LOG_NOTICE, "Unknown option '%s' specified "
131                                                 "for cel_manager.\n", v->name);
132                         }
133                 }
134         }
135
136         ast_config_destroy(cfg);
137
138         if (enablecel && !newenablecel) {
139                 if (event_sub) {
140                         event_sub = ast_event_unsubscribe(event_sub);
141                 }
142         } else if (!enablecel && newenablecel) {
143                 event_sub = ast_event_subscribe(AST_EVENT_CEL, manager_log, "Manager Event Logging", NULL, AST_EVENT_IE_END);
144                 if (!event_sub) {
145                         ast_log(LOG_ERROR, "Unable to register Asterisk Call Manager CEL handling\n");
146                 }
147         }
148         enablecel = newenablecel;
149
150         return 0;
151 }
152
153 static int unload_module(void)
154 {
155         if (event_sub) {
156                 event_sub = ast_event_unsubscribe(event_sub);
157         }
158         return 0;
159 }
160
161 static int load_module(void)
162 {
163         if (load_config(0)) {
164                 return AST_MODULE_LOAD_DECLINE;
165         }
166
167         return AST_MODULE_LOAD_SUCCESS;
168 }
169
170 static int reload(void)
171 {
172         return load_config(1);
173 }
174
175 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Asterisk Manager Interface CEL Backend",
176         .load = load_module,
177         .unload = unload_module,
178         .reload = reload,
179         .load_pri = AST_MODPRI_CDR_DRIVER,
180 );