Doxygen Updates Janitor Work
[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 /*! TRUE if we should set the EventName header to USER_DEFINED on user events. */
61 static unsigned char cel_show_user_def;
62
63 static struct ast_event_sub *event_sub;
64
65 static void manager_log(const struct ast_event *event, void *userdata)
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_cel_get_ama_flag_name(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) {
157                 ast_log(LOG_WARNING, "Failed to load configuration file. CEL manager Module not activated.\n");
158                 enablecel = 0;
159                 return -1;
160         }
161
162         while ((cat = ast_category_browse(cfg, cat))) {
163                 if (strcasecmp(cat, "manager")) {
164                         continue;
165                 }
166
167                 for (v = ast_variable_browse(cfg, cat); v; v = v->next) {
168                         if (!strcasecmp(v->name, "enabled")) {
169                                 newenablecel = ast_true(v->value) ? 1 : 0;
170                         } else if (!strcasecmp(v->name, "show_user_defined")) {
171                                 new_cel_show_user_def = ast_true(v->value) ? 1 : 0;
172                         } else {
173                                 ast_log(LOG_NOTICE, "Unknown option '%s' specified "
174                                                 "for cel_manager.\n", v->name);
175                         }
176                 }
177         }
178
179         ast_config_destroy(cfg);
180
181         cel_show_user_def = new_cel_show_user_def;
182         if (enablecel && !newenablecel) {
183                 if (event_sub) {
184                         event_sub = ast_event_unsubscribe(event_sub);
185                 }
186         } else if (!enablecel && newenablecel) {
187                 event_sub = ast_event_subscribe(AST_EVENT_CEL, manager_log, "Manager Event Logging", NULL, AST_EVENT_IE_END);
188                 if (!event_sub) {
189                         ast_log(LOG_ERROR, "Unable to register Asterisk Call Manager CEL handling\n");
190                 }
191         }
192         enablecel = newenablecel;
193
194         return 0;
195 }
196
197 static int unload_module(void)
198 {
199         if (event_sub) {
200                 event_sub = ast_event_unsubscribe(event_sub);
201         }
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         .load = load_module,
221         .unload = unload_module,
222         .reload = reload,
223         .load_pri = AST_MODPRI_CDR_DRIVER,
224 );