Add module support level to ast_module_info structure. Print it in CLI "module show" .
[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 /*! \brief AMI CEL is off by default */
53 #define CEL_AMI_ENABLED_DEFAULT         0
54
55 static int enablecel;
56
57 /*! \brief show_user_def is off by default */
58 #define CEL_SHOW_USERDEF_DEFAULT        0
59
60 #define MANAGER_BACKEND_NAME "Manager Event Logging"
61
62 /*! TRUE if we should set the EventName header to USER_DEFINED on user events. */
63 static unsigned char cel_show_user_def;
64
65 static void manager_log(struct ast_event *event)
66 {
67         struct ast_tm timeresult;
68         char start_time[80] = "";
69         char user_defined_header[160];
70         const char *event_name;
71         struct ast_cel_event_record record = {
72                 .version = AST_CEL_EVENT_RECORD_VERSION,
73         };
74
75         if (!enablecel) {
76                 return;
77         }
78
79         if (ast_cel_fill_record(event, &record)) {
80                 return;
81         }
82
83         ast_localtime(&record.event_time, &timeresult, NULL);
84         ast_strftime(start_time, sizeof(start_time), DATE_FORMAT, &timeresult);
85
86         event_name = record.event_name;
87         user_defined_header[0] = '\0';
88         if (record.event_type == AST_CEL_USER_DEFINED) {
89                 if (cel_show_user_def) {
90                         snprintf(user_defined_header, sizeof(user_defined_header),
91                                 "UserDefType: %s\r\n", record.user_defined_name);
92                 } else {
93                         event_name = record.user_defined_name;
94                 }
95         }
96
97         manager_event(EVENT_FLAG_CALL, "CEL",
98                 "EventName: %s\r\n"
99                 "AccountCode: %s\r\n"
100                 "CallerIDnum: %s\r\n"
101                 "CallerIDname: %s\r\n"
102                 "CallerIDani: %s\r\n"
103                 "CallerIDrdnis: %s\r\n"
104                 "CallerIDdnid: %s\r\n"
105                 "Exten: %s\r\n"
106                 "Context: %s\r\n"
107                 "Channel: %s\r\n"
108                 "Application: %s\r\n"
109                 "AppData: %s\r\n"
110                 "EventTime: %s\r\n"
111                 "AMAFlags: %s\r\n"
112                 "UniqueID: %s\r\n"
113                 "LinkedID: %s\r\n"
114                 "Userfield: %s\r\n"
115                 "Peer: %s\r\n"
116                 "PeerAccount: %s\r\n"
117                 "%s"
118                 "Extra: %s\r\n",
119                 event_name,
120                 record.account_code,
121                 record.caller_id_num,
122                 record.caller_id_name,
123                 record.caller_id_ani,
124                 record.caller_id_rdnis,
125                 record.caller_id_dnid,
126                 record.extension,
127                 record.context,
128                 record.channel_name,
129                 record.application_name,
130                 record.application_data,
131                 start_time,
132                 ast_channel_amaflags2string(record.amaflag),
133                 record.unique_id,
134                 record.linked_id,
135                 record.user_field,
136                 record.peer,
137                 record.peer_account,
138                 user_defined_header,
139                 record.extra);
140 }
141
142 static int load_config(int reload)
143 {
144         const char *cat = NULL;
145         struct ast_config *cfg;
146         struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
147         struct ast_variable *v;
148         int newenablecel = CEL_AMI_ENABLED_DEFAULT;
149         int new_cel_show_user_def = CEL_SHOW_USERDEF_DEFAULT;
150
151         cfg = ast_config_load(CONF_FILE, config_flags);
152         if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
153                 return 0;
154         }
155
156         if (cfg == CONFIG_STATUS_FILEINVALID) {
157                 ast_log(LOG_WARNING, "Configuration file '%s' is invalid. CEL manager Module not activated.\n",
158                         CONF_FILE);
159                 enablecel = 0;
160                 return -1;
161         } else if (!cfg) {
162                 ast_log(LOG_WARNING, "Failed to load configuration file. CEL manager Module not activated.\n");
163                 enablecel = 0;
164                 return -1;
165         }
166
167         while ((cat = ast_category_browse(cfg, cat))) {
168                 if (strcasecmp(cat, "manager")) {
169                         continue;
170                 }
171
172                 for (v = ast_variable_browse(cfg, cat); v; v = v->next) {
173                         if (!strcasecmp(v->name, "enabled")) {
174                                 newenablecel = ast_true(v->value) ? 1 : 0;
175                         } else if (!strcasecmp(v->name, "show_user_defined")) {
176                                 new_cel_show_user_def = ast_true(v->value) ? 1 : 0;
177                         } else {
178                                 ast_log(LOG_NOTICE, "Unknown option '%s' specified "
179                                                 "for cel_manager.\n", v->name);
180                         }
181                 }
182         }
183
184         ast_config_destroy(cfg);
185
186         cel_show_user_def = new_cel_show_user_def;
187         if (enablecel && !newenablecel) {
188                 ast_cel_backend_unregister(MANAGER_BACKEND_NAME);
189         } else if (!enablecel && newenablecel) {
190                 if (ast_cel_backend_register(MANAGER_BACKEND_NAME, manager_log)) {
191                         ast_log(LOG_ERROR, "Unable to register Asterisk Call Manager CEL handling\n");
192                 }
193         }
194         enablecel = newenablecel;
195
196         return 0;
197 }
198
199 static int unload_module(void)
200 {
201         ast_cel_backend_unregister(MANAGER_BACKEND_NAME);
202         return 0;
203 }
204
205 static int load_module(void)
206 {
207         if (load_config(0)) {
208                 return AST_MODULE_LOAD_DECLINE;
209         }
210
211         return AST_MODULE_LOAD_SUCCESS;
212 }
213
214 static int reload(void)
215 {
216         return load_config(1);
217 }
218
219 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Asterisk Manager Interface CEL Backend",
220         .support_level = AST_MODULE_SUPPORT_CORE,
221         .load = load_module,
222         .unload = unload_module,
223         .reload = reload,
224         .load_pri = AST_MODPRI_CDR_DRIVER,
225 );