f03b51d60e6272dff7cb8291752963283e5f4195
[asterisk/asterisk.git] / main / cel.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2007 - 2009, Digium, Inc.
5  *
6  * See http://www.asterisk.org for more information about
7  * the Asterisk project. Please do not directly contact
8  * any of the maintainers of this project for assistance;
9  * the project provides a web site, mailing lists and IRC
10  * channels for your use.
11  *
12  * This program is free software, distributed under the terms of
13  * the GNU General Public License Version 2. See the LICENSE file
14  * at the top of the source tree.
15  */
16
17 /*!
18  * \file
19  *
20  * \brief Channel Event Logging API
21  *
22  * \author Steve Murphy <murf@digium.com>
23  * \author Russell Bryant <russell@digium.com>
24  *
25  * \todo Do thorough testing of all transfer methods to ensure that BLINDTRANSFER,
26  *       ATTENDEDTRANSFER, BRIDGE_START, and BRIDGE_END events are all reported
27  *       as expected.
28  */
29
30 #include "asterisk.h"
31
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33
34 #include "asterisk/_private.h"
35
36 #include "asterisk/channel.h"
37 #include "asterisk/pbx.h"
38 #include "asterisk/cel.h"
39 #include "asterisk/logger.h"
40 #include "asterisk/linkedlists.h"
41 #include "asterisk/utils.h"
42 #include "asterisk/config.h"
43 #include "asterisk/cli.h"
44 #include "asterisk/astobj2.h"
45
46 /*! Is the CEL subsystem enabled ? */
47 static unsigned char cel_enabled;
48
49 /*! \brief CEL is off by default */
50 static const unsigned char CEL_ENALBED_DEFAULT = 0;
51
52 /*! 
53  * \brief which events we want to track 
54  *
55  * \note bit field, up to 64 events
56  */
57 static int64_t eventset;
58
59 /*!
60  * \brief Maximum possible CEL event IDs
61  * \note This limit is currently imposed by the eventset definition
62  */
63 #define CEL_MAX_EVENT_IDS 64
64
65 /*! 
66  * \brief Track no events by default.
67  */
68 static const int64_t CEL_DEFAULT_EVENTS = 0;
69
70 /*!
71  * \brief Number of buckets for the appset container
72  */
73 static const int NUM_APP_BUCKETS = 97;
74
75 /*!
76  * \brief Container of Asterisk application names
77  *
78  * The apps in this container are the applications that were specified
79  * in the configuration as applications that CEL events should be generated
80  * for when they start and end on a channel.
81  */
82 static struct ao2_container *appset;
83
84 /*!
85  * \brief Configured date format for event timestamps
86  */
87 static char cel_dateformat[256];
88
89 /*!
90  * \brief Map of ast_cel_event_type to strings
91  */
92 static const char * const cel_event_types[CEL_MAX_EVENT_IDS] = {
93         [0]                        = "ALL",
94         [AST_CEL_CHANNEL_START]    = "CHAN_START",
95         [AST_CEL_CHANNEL_END]      = "CHAN_END",
96         [AST_CEL_ANSWER]           = "ANSWER",
97         [AST_CEL_HANGUP]           = "HANGUP",
98         [AST_CEL_APP_START]        = "APP_START",
99         [AST_CEL_APP_END]          = "APP_END",
100         [AST_CEL_BRIDGE_START]     = "BRIDGE_START",
101         [AST_CEL_BRIDGE_END]       = "BRIDGE_END",
102         [AST_CEL_BRIDGE_UPDATE]    = "BRIDGE_UPDATE",
103         [AST_CEL_CONF_START]       = "CONF_START",
104         [AST_CEL_CONF_END]         = "CONF_END",
105         [AST_CEL_PARK_START]       = "PARK_START",
106         [AST_CEL_PARK_END]         = "PARK_END",
107         [AST_CEL_TRANSFER]         = "TRANSFER",
108         [AST_CEL_USER_DEFINED]     = "USER_DEFINED",
109         [AST_CEL_CONF_ENTER]       = "CONF_ENTER",
110         [AST_CEL_CONF_EXIT]        = "CONF_EXIT",
111         [AST_CEL_BLINDTRANSFER]    = "BLINDTRANSFER",
112         [AST_CEL_ATTENDEDTRANSFER] = "ATTENDEDTRANSFER",
113         [AST_CEL_PICKUP]           = "PICKUP",
114         [AST_CEL_FORWARD]          = "FORWARD",
115         [AST_CEL_3WAY_START]       = "3WAY_START",
116         [AST_CEL_3WAY_END]         = "3WAY_END",
117         [AST_CEL_HOOKFLASH]        = "HOOKFLASH",
118         [AST_CEL_LINKEDID_END]     = "LINKEDID_END",
119 };
120
121 /*!
122  * \brief Map of ast_cel_ama_flags to strings
123  */
124 static const char * const cel_ama_flags[AST_CEL_AMA_FLAG_TOTAL] = {
125         [AST_CEL_AMA_FLAG_NONE]          = "NONE",
126         [AST_CEL_AMA_FLAG_OMIT]          = "OMIT",
127         [AST_CEL_AMA_FLAG_BILLING]       = "BILLING",
128         [AST_CEL_AMA_FLAG_DOCUMENTATION] = "DOCUMENTATION",
129 };
130
131 unsigned int ast_cel_check_enabled(void)
132 {
133         return cel_enabled;
134 }
135
136 static int print_app(void *obj, void *arg, int flags)
137 {
138         struct ast_cli_args *a = arg;
139
140         ast_cli(a->fd, "CEL Tracking Application: %s\n", (const char *) obj);
141
142         return 0;
143 }
144
145 static void print_cel_sub(const struct ast_event *event, void *data)
146 {
147         struct ast_cli_args *a = data;
148
149         ast_cli(a->fd, "CEL Event Subscriber: %s\n",
150                         ast_event_get_ie_str(event, AST_EVENT_IE_DESCRIPTION));
151 }
152
153 static char *handle_cli_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
154 {
155         unsigned int i;
156         struct ast_event_sub *sub;
157
158         switch (cmd) {
159         case CLI_INIT:
160                 e->command = "cel show status";
161                 e->usage =
162                         "Usage: cel show status\n"
163                         "       Displays the Channel Event Logging system status.\n";
164                 return NULL;
165         case CLI_GENERATE:
166                 return NULL;
167         case CLI_HANDLER:
168                 break;
169         }
170
171         if (a->argc > 3) {
172                 return CLI_SHOWUSAGE;
173         }
174
175         ast_cli(a->fd, "CEL Logging: %s\n", cel_enabled ? "Enabled" : "Disabled");
176
177         if (!cel_enabled) {
178                 return CLI_SUCCESS;
179         }
180
181         for (i = 0; i < (sizeof(eventset) * 8); i++) {
182                 const char *name;
183
184                 if (!(eventset & ((int64_t) 1 << i))) {
185                         continue;
186                 }
187
188                 name = ast_cel_get_type_name(i);
189                 if (strcasecmp(name, "Unknown")) {
190                         ast_cli(a->fd, "CEL Tracking Event: %s\n", name);
191                 }
192         }
193
194         ao2_callback(appset, OBJ_NODATA, print_app, a);
195
196         if (!(sub = ast_event_subscribe_new(AST_EVENT_SUB, print_cel_sub, a))) {
197                 return CLI_FAILURE;
198         }
199         ast_event_sub_append_ie_uint(sub, AST_EVENT_IE_EVENTTYPE, AST_EVENT_CEL);
200         ast_event_report_subs(sub);
201         ast_event_sub_destroy(sub);
202         sub = NULL;
203
204         return CLI_SUCCESS;
205 }
206
207 static struct ast_cli_entry cli_status = AST_CLI_DEFINE(handle_cli_status, "Display the CEL status");
208
209 enum ast_cel_event_type ast_cel_str_to_event_type(const char *name)
210 {
211         unsigned int i;
212
213         for (i = 0; i < ARRAY_LEN(cel_event_types); i++) {
214                 if (!cel_event_types[i]) {
215                         continue;
216                 }
217
218                 if (!strcasecmp(name, cel_event_types[i])) {
219                         return i;
220                 }
221         }
222
223         return -1;
224 }
225
226 static int ast_cel_track_event(enum ast_cel_event_type et)
227 {
228         return (eventset & ((int64_t) 1 << et));
229 }
230
231 static void parse_events(const char *val)
232 {
233         char *events = ast_strdupa(val);
234         char *cur_event;
235
236         while ((cur_event = strsep(&events, ","))) {
237                 enum ast_cel_event_type event_type;
238
239                 cur_event = ast_strip(cur_event);
240                 if (ast_strlen_zero(cur_event)) {
241                         continue;
242                 }
243
244                 event_type = ast_cel_str_to_event_type(cur_event);
245
246                 if (event_type == 0) {
247                         /* All events */
248                         eventset = (int64_t) -1;
249                 } else if (event_type == -1) {
250                         ast_log(LOG_WARNING, "Unknown event name '%s'\n",
251                                         cur_event);
252                 } else {
253                         eventset |= ((int64_t) 1 << event_type);
254                 }
255         }
256 }
257
258 static void parse_apps(const char *val)
259 {
260         char *apps = ast_strdupa(val);
261         char *cur_app;
262
263         if (!ast_cel_track_event(AST_CEL_APP_START) && !ast_cel_track_event(AST_CEL_APP_END)) {
264                 ast_log(LOG_WARNING, "An apps= config line, but not tracking APP events\n");
265                 return;
266         }
267
268         while ((cur_app = strsep(&apps, ","))) {
269                 char *app;
270
271                 cur_app = ast_strip(cur_app);
272                 if (ast_strlen_zero(cur_app)) {
273                         continue;
274                 }
275
276                 if (!(app = ao2_alloc(strlen(cur_app) + 1, NULL))) {
277                         continue;
278                 }
279                 strcpy(app, cur_app);
280
281                 ao2_link(appset, app);
282                 ao2_ref(app, -1);
283                 app = NULL;
284         }
285 }
286
287 AST_MUTEX_DEFINE_STATIC(reload_lock);
288
289 static int do_reload(void)
290 {
291         struct ast_config *config;
292         const char *enabled_value;
293         const char *val;
294         int res = 0;
295         struct ast_flags config_flags = { 0, };
296         const char *s;
297
298         ast_mutex_lock(&reload_lock);
299
300         /* Reset all settings before reloading configuration */
301         cel_enabled = CEL_ENALBED_DEFAULT;
302         eventset = CEL_DEFAULT_EVENTS;
303         *cel_dateformat = '\0';
304         ao2_callback(appset, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, NULL, NULL);
305
306         config = ast_config_load2("cel.conf", "cel", config_flags);
307
308         if (config == CONFIG_STATUS_FILEMISSING) {
309                 config = NULL;
310                 goto return_cleanup;
311         }
312
313         if ((enabled_value = ast_variable_retrieve(config, "general", "enable"))) {
314                 cel_enabled = ast_true(enabled_value);
315         }
316
317         if (!cel_enabled) {
318                 goto return_cleanup;
319         }
320
321         /* get the date format for logging */
322         if ((s = ast_variable_retrieve(config, "general", "dateformat"))) {
323                 ast_copy_string(cel_dateformat, s, sizeof(cel_dateformat));
324         }
325
326         if ((val = ast_variable_retrieve(config, "general", "events"))) {
327                 parse_events(val);
328         }
329
330         if ((val = ast_variable_retrieve(config, "general", "apps"))) {
331                 parse_apps(val);
332         }
333
334 return_cleanup:
335         ast_verb(3, "CEL logging %sabled.\n", cel_enabled ? "en" : "dis");
336
337         ast_mutex_unlock(&reload_lock);
338
339         if (config) {
340                 ast_config_destroy(config);
341         }
342
343         return res;
344 }
345
346 const char *ast_cel_get_type_name(enum ast_cel_event_type type)
347 {
348         return S_OR(cel_event_types[type], "Unknown");
349 }
350
351 const char *ast_cel_get_ama_flag_name(enum ast_cel_ama_flag flag)
352 {
353         if (flag < 0 || flag >= ARRAY_LEN(cel_ama_flags)) {
354                 ast_log(LOG_WARNING, "Invalid AMA flag: %d\n", flag);
355                 return "Unknown";
356         }
357
358         return S_OR(cel_ama_flags[flag], "Unknown");
359 }
360
361 /* called whenever a channel is destroyed or a linkedid is changed to
362  * potentially emit a CEL_LINKEDID_END event */
363
364 struct channel_find_data {
365         const struct ast_channel *chan;
366         const char *linkedid;
367 };
368
369 static int linkedid_match(void *obj, void *arg, void *data, int flags)
370 {
371         struct ast_channel *c = obj;
372         struct channel_find_data *find_dat = data;
373         int res;
374
375         ast_channel_lock(c);
376         res = (c != find_dat->chan && c->linkedid && !strcmp(find_dat->linkedid, c->linkedid));
377         ast_channel_unlock(c);
378
379         return res ? CMP_MATCH | CMP_STOP : 0;
380 }
381
382 void ast_cel_check_retire_linkedid(struct ast_channel *chan)
383 {
384         const char *linkedid = chan->linkedid;
385         struct channel_find_data find_dat;
386
387         /* make sure we need to do all this work */
388
389         if (!ast_strlen_zero(linkedid) && ast_cel_track_event(AST_CEL_LINKEDID_END)) {
390                 struct ast_channel *tmp = NULL;
391                 find_dat.chan = chan;
392                 find_dat.linkedid = linkedid;
393                 if ((tmp = ast_channel_callback(linkedid_match, NULL, &find_dat, 0))) {
394                         tmp = ast_channel_unref(tmp);
395                 } else {
396                         ast_cel_report_event(chan, AST_CEL_LINKEDID_END, NULL, NULL, NULL);
397                 }
398         }
399 }
400
401 struct ast_channel *ast_cel_fabricate_channel_from_event(const struct ast_event *event)
402 {
403         struct varshead *headp;
404         struct ast_var_t *newvariable;
405         char timebuf[30];
406         struct ast_channel *tchan;
407         struct ast_cel_event_record record = {
408                 .version = AST_CEL_EVENT_RECORD_VERSION,
409         };
410
411         /* do not call ast_channel_alloc because this is not really a real channel */
412         if (!(tchan = ast_dummy_channel_alloc())) {
413                 return NULL;
414         }
415
416         headp = &tchan->varshead;
417
418         /* first, get the variables from the event */
419         if (ast_cel_fill_record(event, &record)) {
420                 ast_channel_unref(tchan);
421                 return NULL;
422         }
423
424         /* next, fill the channel with their data */
425         if ((newvariable = ast_var_assign("eventtype", record.event_name))) {
426                 AST_LIST_INSERT_HEAD(headp, newvariable, entries);
427         }
428
429         if (ast_strlen_zero(cel_dateformat)) {
430                 snprintf(timebuf, sizeof(timebuf), "%ld.%06ld", (long) record.event_time.tv_sec,
431                                 (long) record.event_time.tv_usec);
432         } else {
433                 struct ast_tm tm;
434                 ast_localtime(&record.event_time, &tm, NULL);
435                 ast_strftime(timebuf, sizeof(timebuf), cel_dateformat, &tm);
436         }
437
438         if ((newvariable = ast_var_assign("eventtime", timebuf))) {
439                 AST_LIST_INSERT_HEAD(headp, newvariable, entries);
440         }
441
442         if ((newvariable = ast_var_assign("eventextra", record.extra))) {
443                 AST_LIST_INSERT_HEAD(headp, newvariable, entries);
444         }
445
446         tchan->caller.id.name.valid = 1;
447         tchan->caller.id.name.str = ast_strdup(record.caller_id_name);
448         tchan->caller.id.number.valid = 1;
449         tchan->caller.id.number.str = ast_strdup(record.caller_id_num);
450         tchan->caller.ani.number.valid = 1;
451         tchan->caller.ani.number.str = ast_strdup(record.caller_id_ani);
452         tchan->redirecting.from.number.valid = 1;
453         tchan->redirecting.from.number.str = ast_strdup(record.caller_id_rdnis);
454         tchan->dialed.number.str = ast_strdup(record.caller_id_dnid);
455
456         ast_copy_string(tchan->exten, record.extension, sizeof(tchan->exten));
457         ast_copy_string(tchan->context, record.context, sizeof(tchan->context));
458         ast_channel_name_set(tchan, record.channel_name);
459         ast_string_field_set(tchan, uniqueid, record.unique_id);
460         ast_string_field_set(tchan, linkedid, record.linked_id);
461         ast_string_field_set(tchan, accountcode, record.account_code);
462         ast_string_field_set(tchan, peeraccount, record.peer_account);
463         ast_string_field_set(tchan, userfield, record.user_field);
464
465         pbx_builtin_setvar_helper(tchan, "BRIDGEPEER", record.peer);
466
467         tchan->appl = ast_strdup(record.application_name);
468         tchan->data = ast_strdup(record.application_data);
469         tchan->amaflags = record.amaflag;
470
471         return tchan;
472 }
473
474 int ast_cel_report_event(struct ast_channel *chan, enum ast_cel_event_type event_type,
475                 const char *userdefevname, const char *extra, struct ast_channel *peer2)
476 {
477         struct timeval eventtime;
478         struct ast_event *ev;
479         const char *peername = "";
480         struct ast_channel *peer;
481
482         ast_channel_lock(chan);
483         peer = ast_bridged_channel(chan);
484         if (peer) {
485                 ast_channel_ref(peer);
486         }
487         ast_channel_unlock(chan);
488
489         /* Make sure a reload is not occurring while we're checking to see if this
490          * is an event that we care about.  We could lose an important event in this
491          * process otherwise. */
492         ast_mutex_lock(&reload_lock);
493
494         if (!cel_enabled || !ast_cel_track_event(event_type)) {
495                 ast_mutex_unlock(&reload_lock);
496                 if (peer) {
497                         ast_channel_unref(peer);
498                 }
499                 return 0;
500         }
501
502         if (event_type == AST_CEL_APP_START || event_type == AST_CEL_APP_END) {
503                 char *app;
504                 if (!(app = ao2_find(appset, (char *) chan->appl, OBJ_POINTER))) {
505                         ast_mutex_unlock(&reload_lock);
506                         if (peer) {
507                                 ast_channel_unref(peer);
508                         }
509                         return 0;
510                 }
511                 ao2_ref(app, -1);
512         }
513
514         ast_mutex_unlock(&reload_lock);
515
516         if (peer) {
517                 ast_channel_lock(peer);
518                 peername = ast_strdupa(ast_channel_name(peer));
519                 ast_channel_unlock(peer);
520         } else if (peer2) {
521                 ast_channel_lock(peer2);
522                 peername = ast_strdupa(ast_channel_name(peer2));
523                 ast_channel_unlock(peer2);
524         }
525
526         if (!userdefevname) {
527                 userdefevname = "";
528         }
529
530         if (!extra) {
531                 extra = "";
532         }
533
534         eventtime = ast_tvnow();
535
536         ast_channel_lock(chan);
537
538         ev = ast_event_new(AST_EVENT_CEL,
539                 AST_EVENT_IE_CEL_EVENT_TYPE, AST_EVENT_IE_PLTYPE_UINT, event_type,
540                 AST_EVENT_IE_CEL_EVENT_TIME, AST_EVENT_IE_PLTYPE_UINT, eventtime.tv_sec,
541                 AST_EVENT_IE_CEL_EVENT_TIME_USEC, AST_EVENT_IE_PLTYPE_UINT, eventtime.tv_usec,
542                 AST_EVENT_IE_CEL_USEREVENT_NAME, AST_EVENT_IE_PLTYPE_STR, userdefevname,
543                 AST_EVENT_IE_CEL_CIDNAME, AST_EVENT_IE_PLTYPE_STR,
544                         S_COR(chan->caller.id.name.valid, chan->caller.id.name.str, ""),
545                 AST_EVENT_IE_CEL_CIDNUM, AST_EVENT_IE_PLTYPE_STR,
546                         S_COR(chan->caller.id.number.valid, chan->caller.id.number.str, ""),
547                 AST_EVENT_IE_CEL_CIDANI, AST_EVENT_IE_PLTYPE_STR,
548                         S_COR(chan->caller.ani.number.valid, chan->caller.ani.number.str, ""),
549                 AST_EVENT_IE_CEL_CIDRDNIS, AST_EVENT_IE_PLTYPE_STR,
550                         S_COR(chan->redirecting.from.number.valid, chan->redirecting.from.number.str, ""),
551                 AST_EVENT_IE_CEL_CIDDNID, AST_EVENT_IE_PLTYPE_STR,
552                         S_OR(chan->dialed.number.str, ""),
553                 AST_EVENT_IE_CEL_EXTEN, AST_EVENT_IE_PLTYPE_STR, chan->exten,
554                 AST_EVENT_IE_CEL_CONTEXT, AST_EVENT_IE_PLTYPE_STR, chan->context,
555                 AST_EVENT_IE_CEL_CHANNAME, AST_EVENT_IE_PLTYPE_STR, ast_channel_name(chan),
556                 AST_EVENT_IE_CEL_APPNAME, AST_EVENT_IE_PLTYPE_STR, S_OR(chan->appl, ""),
557                 AST_EVENT_IE_CEL_APPDATA, AST_EVENT_IE_PLTYPE_STR, S_OR(chan->data, ""),
558                 AST_EVENT_IE_CEL_AMAFLAGS, AST_EVENT_IE_PLTYPE_UINT, chan->amaflags,
559                 AST_EVENT_IE_CEL_ACCTCODE, AST_EVENT_IE_PLTYPE_STR, chan->accountcode,
560                 AST_EVENT_IE_CEL_PEERACCT, AST_EVENT_IE_PLTYPE_STR, chan->peeraccount,
561                 AST_EVENT_IE_CEL_UNIQUEID, AST_EVENT_IE_PLTYPE_STR, chan->uniqueid,
562                 AST_EVENT_IE_CEL_LINKEDID, AST_EVENT_IE_PLTYPE_STR, chan->linkedid,
563                 AST_EVENT_IE_CEL_USERFIELD, AST_EVENT_IE_PLTYPE_STR, chan->userfield,
564                 AST_EVENT_IE_CEL_EXTRA, AST_EVENT_IE_PLTYPE_STR, extra,
565                 AST_EVENT_IE_CEL_PEER, AST_EVENT_IE_PLTYPE_STR, peername,
566                 AST_EVENT_IE_END);
567
568         ast_channel_unlock(chan);
569
570         if (peer) {
571                 peer = ast_channel_unref(peer);
572         }
573
574         if (ev && ast_event_queue(ev)) {
575                 ast_event_destroy(ev);
576                 return -1;
577         }
578
579         return 0;
580 }
581
582 int ast_cel_fill_record(const struct ast_event *e, struct ast_cel_event_record *r)
583 {
584         if (r->version != AST_CEL_EVENT_RECORD_VERSION) {
585                 ast_log(LOG_ERROR, "Module ABI mismatch for ast_cel_event_record.  "
586                                 "Please ensure all modules were compiled for "
587                                 "this version of Asterisk.\n");
588                 return -1;
589         }
590
591         r->event_type = ast_event_get_ie_uint(e, AST_EVENT_IE_CEL_EVENT_TYPE);
592
593         r->event_time.tv_sec = ast_event_get_ie_uint(e, AST_EVENT_IE_CEL_EVENT_TIME);
594         r->event_time.tv_usec = ast_event_get_ie_uint(e, AST_EVENT_IE_CEL_EVENT_TIME_USEC);
595
596         r->user_defined_name = "";
597
598         if (r->event_type == AST_CEL_USER_DEFINED) {
599                 r->user_defined_name = ast_event_get_ie_str(e, AST_EVENT_IE_CEL_USEREVENT_NAME);
600                 r->event_name = r->user_defined_name;
601         } else {
602                 r->event_name = ast_cel_get_type_name(r->event_type);
603         }
604
605         r->caller_id_name   = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_CIDNAME), "");
606         r->caller_id_num    = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_CIDNUM), "");
607         r->caller_id_ani    = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_CIDANI), "");
608         r->caller_id_rdnis  = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_CIDRDNIS), "");
609         r->caller_id_dnid   = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_CIDDNID), "");
610         r->extension        = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_EXTEN), "");
611         r->context          = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_CONTEXT), "");
612         r->channel_name     = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_CHANNAME), "");
613         r->application_name = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_APPNAME), "");
614         r->application_data = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_APPDATA), "");
615         r->account_code     = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_ACCTCODE), "");
616         r->peer_account     = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_ACCTCODE), "");
617         r->unique_id        = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_UNIQUEID), "");
618         r->linked_id        = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_LINKEDID), "");
619         r->amaflag          = ast_event_get_ie_uint(e, AST_EVENT_IE_CEL_AMAFLAGS);
620         r->user_field       = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_USERFIELD), "");
621         r->peer             = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_PEER), "");
622         r->extra            = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_EXTRA), "");
623
624         return 0;
625 }
626
627 static int app_hash(const void *obj, const int flags)
628 {
629         return ast_str_case_hash((const char *) obj);
630 }
631
632 static int app_cmp(void *obj, void *arg, int flags)
633 {
634         const char *app1 = obj, *app2 = arg;
635
636         return !strcasecmp(app1, app2) ? CMP_MATCH | CMP_STOP : 0;
637 }
638
639 static void ast_cel_engine_term(void)
640 {
641         if (appset) {
642                 ao2_ref(appset, -1);
643                 appset = NULL;
644         }
645 }
646
647 int ast_cel_engine_init(void)
648 {
649         if (!(appset = ao2_container_alloc(NUM_APP_BUCKETS, app_hash, app_cmp))) {
650                 return -1;
651         }
652
653         if (do_reload()) {
654                 ao2_ref(appset, -1);
655                 appset = NULL;
656                 return -1;
657         }
658
659         if (ast_cli_register(&cli_status)) {
660                 ao2_ref(appset, -1);
661                 appset = NULL;
662                 return -1;
663         }
664
665         ast_register_atexit(ast_cel_engine_term);
666
667         return 0;
668 }
669
670 int ast_cel_engine_reload(void)
671 {
672         return do_reload();
673 }
674