Merged revisions 317474 via svnmerge from
[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 #include "asterisk.h"
33
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
35
36 #include "asterisk/channel.h"
37 #include "asterisk/cel.h"
38 #include "asterisk/module.h"
39 #include "asterisk/logger.h"
40 #include "asterisk/utils.h"
41 #include "asterisk/manager.h"
42 #include "asterisk/config.h"
43
44 static const char DATE_FORMAT[] = "%Y-%m-%d %T";
45
46 static const char CONF_FILE[] = "cel.conf";
47
48 static int enablecel;
49
50 static struct ast_event_sub *event_sub;
51
52 static void manager_log(const struct ast_event *event, void *userdata)
53 {
54         struct ast_tm timeresult;
55         char start_time[80] = "";
56         struct ast_cel_event_record record = {
57                 .version = AST_CEL_EVENT_RECORD_VERSION,
58         };
59
60         if (ast_cel_fill_record(event, &record)) {
61                 return;
62         }
63
64         if (!enablecel) {
65                 return;
66         }
67
68         ast_localtime(&record.event_time, &timeresult, NULL);
69         ast_strftime(start_time, sizeof(start_time), DATE_FORMAT, &timeresult);
70
71         manager_event(EVENT_FLAG_CALL, "CEL",
72                 "EventName: %s\r\n"
73                 "AccountCode: %s\r\n"
74                 "CallerIDnum: %s\r\n"
75                 "CallerIDname: %s\r\n"
76                 "CallerIDani: %s\r\n"
77                 "CallerIDrdnis: %s\r\n"
78                 "CallerIDdnid: %s\r\n"
79                 "Exten: %s\r\n"
80                 "Context: %s\r\n"
81                 "Channel: %s\r\n"
82                 "Application: %s\r\n"
83                 "AppData: %s\r\n"
84                 "EventTime: %s\r\n"
85                 "AMAFlags: %s\r\n"
86                 "UniqueID: %s\r\n"
87                 "LinkedID: %s\r\n"
88                 "Userfield: %s\r\n"
89                 "Peer: %s\r\n",
90                 record.event_name, record.account_code, record.caller_id_num,
91                 record.caller_id_name, record.caller_id_ani, record.caller_id_rdnis,
92                 record.caller_id_dnid, record.extension, record.context, record.channel_name,
93                 record.application_name, record.application_data, start_time,
94                 ast_cel_get_ama_flag_name(record.amaflag), record.unique_id, record.linked_id,
95                 record.user_field, record.peer);
96 }
97
98 static int load_config(int reload)
99 {
100         const char *cat = NULL;
101         struct ast_config *cfg;
102         struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
103         struct ast_variable *v;
104         int newenablecel = 0;
105
106         cfg = ast_config_load(CONF_FILE, config_flags);
107         if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
108                 return 0;
109         }
110
111         if (!cfg) {
112                 ast_log(LOG_WARNING, "Failed to load configuration file. CEL manager Module not activated.\n");
113                 enablecel = 0;
114                 return -1;
115         }
116
117         while ((cat = ast_category_browse(cfg, cat))) {
118                 if (strcasecmp(cat, "manager")) {
119                         continue;
120                 }
121
122                 for (v = ast_variable_browse(cfg, cat); v; v = v->next) {
123                         if (!strcasecmp(v->name, "enabled")) {
124                                 newenablecel = ast_true(v->value);
125                         } else {
126                                 ast_log(LOG_NOTICE, "Unknown option '%s' specified "
127                                                 "for cel_manager.\n", v->name);
128                         }
129                 }
130         }
131
132         ast_config_destroy(cfg);
133
134         if (enablecel && !newenablecel) {
135                 if (event_sub) {
136                         event_sub = ast_event_unsubscribe(event_sub);
137                 }
138         } else if (!enablecel && newenablecel) {
139                 event_sub = ast_event_subscribe(AST_EVENT_CEL, manager_log, "Manager Event Logging", NULL, AST_EVENT_IE_END);
140                 if (!event_sub) {
141                         ast_log(LOG_ERROR, "Unable to register Asterisk Call Manager CEL handling\n");
142                 }
143         }
144         enablecel = newenablecel;
145
146         return 0;
147 }
148
149 static int unload_module(void)
150 {
151         if (event_sub) {
152                 event_sub = ast_event_unsubscribe(event_sub);
153         }
154         return 0;
155 }
156
157 static int load_module(void)
158 {
159         if (load_config(0)) {
160                 return AST_MODULE_LOAD_DECLINE;
161         }
162
163         return AST_MODULE_LOAD_SUCCESS;
164 }
165
166 static int reload(void)
167 {
168         return load_config(1);
169 }
170
171 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Asterisk Manager Interface CEL Backend",
172         .load = load_module,
173         .unload = unload_module,
174         .reload = reload,
175         .load_pri = AST_MODPRI_CDR_DRIVER,
176 );