ARI: Add ability to raise arbitrary User Events
[asterisk/asterisk.git] / main / manager_channels.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2013, Digium, Inc.
5  *
6  * David M. Lee, II <dlee@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief The Asterisk Management Interface - AMI (channel event handling)
22  *
23  * \author David M. Lee, II <dlee@digium.com>
24  *
25  * AMI generated many per-channel and global-channel events by converting Stasis
26  * messages to AMI events. It makes sense to simply put them into a single file.
27  */
28
29 #include "asterisk.h"
30
31 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
32
33 #include "asterisk/callerid.h"
34 #include "asterisk/channel.h"
35 #include "asterisk/manager.h"
36 #include "asterisk/stasis_message_router.h"
37 #include "asterisk/pbx.h"
38 #include "asterisk/stasis_channels.h"
39
40 /*** DOCUMENTATION
41         <managerEvent language="en_US" name="Newchannel">
42                 <managerEventInstance class="EVENT_FLAG_CALL">
43                         <synopsis>Raised when a new channel is created.</synopsis>
44                         <syntax>
45                                 <channel_snapshot/>
46                         </syntax>
47                 </managerEventInstance>
48         </managerEvent>
49         <managerEvent language="en_US" name="Newstate">
50                 <managerEventInstance class="EVENT_FLAG_CALL">
51                         <synopsis>Raised when a channel's state changes.</synopsis>
52                         <syntax>
53                                 <channel_snapshot/>
54                         </syntax>
55                 </managerEventInstance>
56         </managerEvent>
57         <managerEvent language="en_US" name="Hangup">
58                 <managerEventInstance class="EVENT_FLAG_CALL">
59                         <synopsis>Raised when a channel is hung up.</synopsis>
60                         <syntax>
61                                 <channel_snapshot/>
62                                 <parameter name="Cause">
63                                         <para>A numeric cause code for why the channel was hung up.</para>
64                                 </parameter>
65                                 <parameter name="Cause-txt">
66                                         <para>A description of why the channel was hung up.</para>
67                                 </parameter>
68                         </syntax>
69                 </managerEventInstance>
70         </managerEvent>
71         <managerEvent language="en_US" name="HangupRequest">
72                 <managerEventInstance class="EVENT_FLAG_CALL">
73                         <synopsis>Raised when a hangup is requested.</synopsis>
74                         <syntax>
75                                 <channel_snapshot/>
76                                 <xi:include xpointer="xpointer(/docs/managerEvent[@name='Hangup']/managerEventInstance/syntax/parameter[@name='Cause'])" />
77                         </syntax>
78                 </managerEventInstance>
79         </managerEvent>
80         <managerEvent language="en_US" name="SoftHangupRequest">
81                 <managerEventInstance class="EVENT_FLAG_CALL">
82                         <synopsis>Raised when a soft hangup is requested with a specific cause code.</synopsis>
83                         <syntax>
84                                 <channel_snapshot/>
85                                 <xi:include xpointer="xpointer(/docs/managerEvent[@name='Hangup']/managerEventInstance/syntax/parameter[@name='Cause'])" />
86                         </syntax>
87                 </managerEventInstance>
88         </managerEvent>
89         <managerEvent language="en_US" name="NewExten">
90                 <managerEventInstance class="EVENT_FLAG_DIALPLAN">
91                         <synopsis>Raised when a channel enters a new context, extension, priority.</synopsis>
92                         <syntax>
93                                 <channel_snapshot/>
94                                 <parameter name="Extension">
95                                         <para>Deprecated in 12, but kept for
96                                         backward compatability. Please use
97                                         'Exten' instead.</para>
98                                 </parameter>
99                                 <parameter name="Application">
100                                         <para>The application about to be executed.</para>
101                                 </parameter>
102                                 <parameter name="AppData">
103                                         <para>The data to be passed to the application.</para>
104                                 </parameter>
105                         </syntax>
106                 </managerEventInstance>
107         </managerEvent>
108         <managerEvent language="en_US" name="NewCallerid">
109                 <managerEventInstance class="EVENT_FLAG_CALL">
110                         <synopsis>Raised when a channel receives new Caller ID information.</synopsis>
111                         <syntax>
112                                 <channel_snapshot/>
113                                 <parameter name="CID-CallingPres">
114                                         <para>A description of the Caller ID presentation.</para>
115                                 </parameter>
116                         </syntax>
117                 </managerEventInstance>
118         </managerEvent>
119         <managerEvent language="en_US" name="NewAccountCode">
120                 <managerEventInstance class="EVENT_FLAG_CALL">
121                         <synopsis>Raised when a Channel's AccountCode is changed.</synopsis>
122                         <syntax>
123                                 <channel_snapshot/>
124                                 <parameter name="OldAccountCode">
125                                         <para>The channel's previous account code</para>
126                                 </parameter>
127                         </syntax>
128                 </managerEventInstance>
129         </managerEvent>
130         <managerEvent language="en_US" name="DialBegin">
131                 <managerEventInstance class="EVENT_FLAG_CALL">
132                         <synopsis>Raised when a dial action has started.</synopsis>
133                         <syntax>
134                                 <channel_snapshot/>
135                                 <channel_snapshot prefix="Dest"/>
136                                 <parameter name="DialString">
137                                         <para>The non-technology specific device being dialed.</para>
138                                 </parameter>
139                         </syntax>
140                         <see-also>
141                                 <ref type="application">Dial</ref>
142                         </see-also>
143                 </managerEventInstance>
144         </managerEvent>
145         <managerEvent language="en_US" name="DialEnd">
146                 <managerEventInstance class="EVENT_FLAG_CALL">
147                         <synopsis>Raised when a dial action has completed.</synopsis>
148                         <syntax>
149                                 <channel_snapshot/>
150                                 <channel_snapshot prefix="Dest"/>
151                                 <parameter name="DialStatus">
152                                         <para>The result of the dial operation.</para>
153                                         <enumlist>
154                                                 <enum name="ABORT">
155                                                         <para>The call was aborted.</para>
156                                                 </enum>
157                                                 <enum name="ANSWER">
158                                                         <para>The caller answered.</para>
159                                                 </enum>
160                                                 <enum name="BUSY">
161                                                         <para>The caller was busy.</para>
162                                                 </enum>
163                                                 <enum name="CANCEL">
164                                                         <para>The caller cancelled the call.</para>
165                                                 </enum>
166                                                 <enum name="CHANUNAVAIL">
167                                                         <para>The requested channel is unavailable.</para>
168                                                 </enum>
169                                                 <enum name="CONGESTION">
170                                                         <para>The called party is congested.</para>
171                                                 </enum>
172                                                 <enum name="CONTINUE">
173                                                         <para>The dial completed, but the caller elected
174                                                         to continue in the dialplan.</para>
175                                                 </enum>
176                                                 <enum name="GOTO">
177                                                         <para>The dial completed, but the caller jumped to
178                                                         a dialplan location.</para>
179                                                         <para>If known, the location the caller is jumping
180                                                         to will be appended to the result following a
181                                                         ":".</para>
182                                                 </enum>
183                                                 <enum name="NOANSWER">
184                                                         <para>The called party failed to answer.</para>
185                                                 </enum>
186                                         </enumlist>
187                                 </parameter>
188                         </syntax>
189                         <see-also>
190                                 <ref type="application">Dial</ref>
191                         </see-also>
192                 </managerEventInstance>
193         </managerEvent>
194         <managerEvent language="en_US" name="Hold">
195                 <managerEventInstance class="EVENT_FLAG_CALL">
196                         <synopsis>Raised when a channel goes on hold.</synopsis>
197                         <syntax>
198                                 <channel_snapshot/>
199                                 <parameter name="MusicClass">
200                                         <para>The suggested MusicClass, if provided.</para>
201                                 </parameter>
202                         </syntax>
203                 </managerEventInstance>
204         </managerEvent>
205         <managerEvent language="en_US" name="Unhold">
206                 <managerEventInstance class="EVENT_FLAG_CALL">
207                         <synopsis>Raised when a channel goes off hold.</synopsis>
208                         <syntax>
209                                 <channel_snapshot/>
210                         </syntax>
211                 </managerEventInstance>
212         </managerEvent>
213         <managerEvent language="en_US" name="ChanSpyStart">
214                 <managerEventInstance class="EVENT_FLAG_CALL">
215                         <synopsis>Raised when one channel begins spying on another channel.</synopsis>
216                         <syntax>
217                                 <channel_snapshot prefix="Spyer"/>
218                                 <channel_snapshot prefix="Spyee"/>
219                         </syntax>
220                         <see-also>
221                                 <ref type="application">ChanSpyStop</ref>
222                         </see-also>
223                 </managerEventInstance>
224         </managerEvent>
225         <managerEvent language="en_US" name="ChanSpyStop">
226                 <managerEventInstance class="EVENT_FLAG_CALL">
227                         <synopsis>Raised when a channel has stopped spying.</synopsis>
228                         <syntax>
229                                 <channel_snapshot prefix="Spyer"/>
230                                 <channel_snapshot prefix="Spyee"/>
231                         </syntax>
232                         <see-also>
233                                 <ref type="application">ChanSpyStart</ref>
234                         </see-also>
235                 </managerEventInstance>
236         </managerEvent>
237         <managerEvent language="en_US" name="HangupHandlerRun">
238                 <managerEventInstance class="EVENT_FLAG_DIALPLAN">
239                         <synopsis>Raised when a hangup handler is about to be called.</synopsis>
240                         <syntax>
241                                 <channel_snapshot/>
242                                 <parameter name="Handler">
243                                         <para>Hangup handler parameter string passed to the Gosub application.</para>
244                                 </parameter>
245                         </syntax>
246                 </managerEventInstance>
247         </managerEvent>
248         <managerEvent language="en_US" name="HangupHandlerPop">
249                 <managerEventInstance class="EVENT_FLAG_DIALPLAN">
250                         <synopsis>
251                                 Raised when a hangup handler is removed from the handler stack
252                                 by the CHANNEL() function.
253                         </synopsis>
254                         <syntax>
255                                 <channel_snapshot/>
256                                 <xi:include xpointer="xpointer(/docs/managerEvent[@name='HangupHandlerRun']/managerEventInstance/syntax/parameter)" />
257                         </syntax>
258                         <see-also>
259                                 <ref type="managerEvent">HangupHandlerPush</ref>
260                                 <ref type="function">CHANNEL</ref>
261                         </see-also>
262                 </managerEventInstance>
263         </managerEvent>
264         <managerEvent language="en_US" name="HangupHandlerPush">
265                 <managerEventInstance class="EVENT_FLAG_DIALPLAN">
266                         <synopsis>
267                                 Raised when a hangup handler is added to the handler stack by
268                                 the CHANNEL() function.
269                         </synopsis>
270                         <syntax>
271                                 <channel_snapshot/>
272                                 <xi:include xpointer="xpointer(/docs/managerEvent[@name='HangupHandlerRun']/managerEventInstance/syntax/parameter)" />
273                         </syntax>
274                         <see-also>
275                                 <ref type="managerEvent">HangupHandlerPop</ref>
276                                 <ref type="function">CHANNEL</ref>
277                         </see-also>
278                 </managerEventInstance>
279         </managerEvent>
280         <managerEvent language="en_US" name="FAXStatus">
281                 <managerEventInstance class="EVENT_FLAG_CALL">
282                         <synopsis>
283                                 Raised periodically during a fax transmission.
284                         </synopsis>
285                         <syntax>
286                                 <channel_snapshot/>
287                                 <parameter name="Operation">
288                                         <enumlist>
289                                                 <enum name="gateway"/>
290                                                 <enum name="receive"/>
291                                                 <enum name="send"/>
292                                         </enumlist>
293                                 </parameter>
294                                 <parameter name="Status">
295                                         <para>A text message describing the current status of the fax</para>
296                                 </parameter>
297                                 <xi:include xpointer="xpointer(/docs/managerEvent[@name='ReceiveFAX']/managerEventInstance/syntax/parameter[@name='LocalStationID'])" />
298                                 <xi:include xpointer="xpointer(/docs/managerEvent[@name='ReceiveFAX']/managerEventInstance/syntax/parameter[@name='FileName'])" />
299                         </syntax>
300                 </managerEventInstance>
301         </managerEvent>
302         <managerEvent language="en_US" name="ReceiveFAX">
303                 <managerEventInstance class="EVENT_FLAG_CALL">
304                         <synopsis>
305                                 Raised when a receive fax operation has completed.
306                         </synopsis>
307                         <syntax>
308                                 <channel_snapshot/>
309                                 <parameter name="LocalStationID">
310                                         <para>The value of the <variable>LOCALSTATIONID</variable> channel variable</para>
311                                 </parameter>
312                                 <parameter name="RemoteStationID">
313                                         <para>The value of the <variable>REMOTESTATIONID</variable> channel variable</para>
314                                 </parameter>
315                                 <parameter name="PagesTransferred">
316                                         <para>The number of pages that have been transferred</para>
317                                 </parameter>
318                                 <parameter name="Resolution">
319                                         <para>The negotiated resolution</para>
320                                 </parameter>
321                                 <parameter name="TransferRate">
322                                         <para>The negotiated transfer rate</para>
323                                 </parameter>
324                                 <parameter name="FileName" multiple="yes">
325                                         <para>The files being affected by the fax operation</para>
326                                 </parameter>
327                         </syntax>
328                 </managerEventInstance>
329         </managerEvent>
330         <managerEvent language="en_US" name="SendFAX">
331                 <managerEventInstance class="EVENT_FLAG_CALL">
332                         <synopsis>
333                                 Raised when a send fax operation has completed.
334                         </synopsis>
335                         <syntax>
336                                 <channel_snapshot/>
337                                 <xi:include xpointer="xpointer(/docs/managerEvent[@name='ReceiveFAX']/managerEventInstance/syntax/parameter)" />
338                         </syntax>
339                 </managerEventInstance>
340         </managerEvent>
341         <managerEvent language="en_US" name="MusicOnHoldStart">
342                 <managerEventInstance class="EVENT_FLAG_CALL">
343                         <synopsis>Raised when music on hold has started on a channel.</synopsis>
344                         <syntax>
345                                 <channel_snapshot/>
346                                 <parameter name="Class">
347                                         <para>The class of music being played on the channel</para>
348                                 </parameter>
349                         </syntax>
350                         <see-also>
351                                 <ref type="managerEvent">MusicOnHoldStop</ref>
352                                 <ref type="application">MusicOnHold</ref>
353                         </see-also>
354                 </managerEventInstance>
355         </managerEvent>
356         <managerEvent language="en_US" name="MusicOnHoldStop">
357                 <managerEventInstance class="EVENT_FLAG_CALL">
358                         <synopsis>Raised when music on hold has stopped on a channel.</synopsis>
359                         <syntax>
360                                 <channel_snapshot/>
361                         </syntax>
362                         <see-also>
363                                 <ref type="managerEvent">MusicOnHoldStart</ref>
364                                 <ref type="application">StopMusicOnHold</ref>
365                         </see-also>
366                 </managerEventInstance>
367         </managerEvent>
368         <managerEvent language="en_US" name="MonitorStart">
369                 <managerEventInstance class="EVENT_FLAG_CALL">
370                         <synopsis>Raised when monitoring has started on a channel.</synopsis>
371                         <syntax>
372                                 <channel_snapshot/>
373                         </syntax>
374                         <see-also>
375                                 <ref type="managerEvent">MonitorStop</ref>
376                                 <ref type="application">Monitor</ref>
377                                 <ref type="manager">Monitor</ref>
378                         </see-also>
379                 </managerEventInstance>
380         </managerEvent>
381         <managerEvent language="en_US" name="MonitorStop">
382                 <managerEventInstance class="EVENT_FLAG_CALL">
383                 <synopsis>Raised when monitoring has stopped on a channel.</synopsis>
384                 <syntax>
385                         <channel_snapshot/>
386                 </syntax>
387                 <see-also>
388                         <ref type="managerEvent">MonitorStart</ref>
389                         <ref type="application">StopMonitor</ref>
390                         <ref type="manager">StopMonitor</ref>
391                 </see-also>
392                 </managerEventInstance>
393         </managerEvent>
394 ***/
395
396 /*! \brief The \ref stasis subscription returned by the forwarding of the channel topic
397  * to the manager topic
398  */
399 static struct stasis_forward *topic_forwarder;
400
401 struct ast_str *ast_manager_build_channel_state_string_prefix(
402                 const struct ast_channel_snapshot *snapshot,
403                 const char *prefix)
404 {
405         struct ast_str *out = ast_str_create(1024);
406         int res = 0;
407
408         if (!out) {
409                 return NULL;
410         }
411
412         if (snapshot->tech_properties & AST_CHAN_TP_INTERNAL) {
413                 ast_free(out);
414                 return NULL;
415         }
416
417         res = ast_str_set(&out, 0,
418                 "%sChannel: %s\r\n"
419                 "%sChannelState: %u\r\n"
420                 "%sChannelStateDesc: %s\r\n"
421                 "%sCallerIDNum: %s\r\n"
422                 "%sCallerIDName: %s\r\n"
423                 "%sConnectedLineNum: %s\r\n"
424                 "%sConnectedLineName: %s\r\n"
425                 "%sAccountCode: %s\r\n"
426                 "%sContext: %s\r\n"
427                 "%sExten: %s\r\n"
428                 "%sPriority: %d\r\n"
429                 "%sUniqueid: %s\r\n",
430                 prefix, snapshot->name,
431                 prefix, snapshot->state,
432                 prefix, ast_state2str(snapshot->state),
433                 prefix, S_OR(snapshot->caller_number, "<unknown>"),
434                 prefix, S_OR(snapshot->caller_name, "<unknown>"),
435                 prefix, S_OR(snapshot->connected_number, "<unknown>"),
436                 prefix, S_OR(snapshot->connected_name, "<unknown>"),
437                 prefix, snapshot->accountcode,
438                 prefix, snapshot->context,
439                 prefix, snapshot->exten,
440                 prefix, snapshot->priority,
441                 prefix, snapshot->uniqueid);
442
443         if (!res) {
444                 ast_free(out);
445                 return NULL;
446         }
447
448         if (snapshot->manager_vars) {
449                 struct ast_var_t *var;
450                 AST_LIST_TRAVERSE(snapshot->manager_vars, var, entries) {
451                         ast_str_append(&out, 0, "%sChanVariable: %s=%s\r\n",
452                                        prefix,
453                                        var->name, var->value);
454                 }
455         }
456
457         return out;
458 }
459
460 struct ast_str *ast_manager_build_channel_state_string(
461                 const struct ast_channel_snapshot *snapshot)
462 {
463         return ast_manager_build_channel_state_string_prefix(snapshot, "");
464 }
465
466 /*! \brief Typedef for callbacks that get called on channel snapshot updates */
467 typedef struct ast_manager_event_blob *(*channel_snapshot_monitor)(
468         struct ast_channel_snapshot *old_snapshot,
469         struct ast_channel_snapshot *new_snapshot);
470
471 /*! \brief Handle channel state changes */
472 static struct ast_manager_event_blob *channel_state_change(
473         struct ast_channel_snapshot *old_snapshot,
474         struct ast_channel_snapshot *new_snapshot)
475 {
476         int is_hungup, was_hungup;
477
478         if (!new_snapshot) {
479                 /* Ignore cache clearing events; we'll see the hangup first */
480                 return NULL;
481         }
482
483         /* The Newchannel, Newstate and Hangup events are closely related, in
484          * in that they are mutually exclusive, basically different flavors
485          * of a new channel state event.
486          */
487
488         if (!old_snapshot) {
489                 return ast_manager_event_blob_create(
490                         EVENT_FLAG_CALL, "Newchannel", NO_EXTRA_FIELDS);
491         }
492
493         was_hungup = ast_test_flag(&old_snapshot->flags, AST_FLAG_DEAD) ? 1 : 0;
494         is_hungup = ast_test_flag(&new_snapshot->flags, AST_FLAG_DEAD) ? 1 : 0;
495
496         if (!was_hungup && is_hungup) {
497                 return ast_manager_event_blob_create(
498                         EVENT_FLAG_CALL, "Hangup",
499                         "Cause: %d\r\n"
500                         "Cause-txt: %s\r\n",
501                         new_snapshot->hangupcause,
502                         ast_cause2str(new_snapshot->hangupcause));
503         }
504
505         if (old_snapshot->state != new_snapshot->state) {
506                 return ast_manager_event_blob_create(
507                         EVENT_FLAG_CALL, "Newstate", NO_EXTRA_FIELDS);
508         }
509
510         /* No event */
511         return NULL;
512 }
513
514 static struct ast_manager_event_blob *channel_newexten(
515         struct ast_channel_snapshot *old_snapshot,
516         struct ast_channel_snapshot *new_snapshot)
517 {
518         /* No Newexten event on cache clear */
519         if (!new_snapshot) {
520                 return NULL;
521         }
522
523         /* Empty application is not valid for a Newexten event */
524         if (ast_strlen_zero(new_snapshot->appl)) {
525                 return NULL;
526         }
527
528         /* Ignore any updates if we're hungup */
529         if (ast_test_flag(&new_snapshot->flags, AST_FLAG_DEAD)) {
530                 return NULL;
531         }
532
533         /* Ignore updates if the CEP is unchanged */
534         if (old_snapshot && ast_channel_snapshot_cep_equal(old_snapshot, new_snapshot)) {
535                 return NULL;
536         }
537
538         /* DEPRECATED: Extension field deprecated in 12; remove in 14 */
539         return ast_manager_event_blob_create(
540                 EVENT_FLAG_CALL, "Newexten",
541                 "Extension: %s\r\n"
542                 "Application: %s\r\n"
543                 "AppData: %s\r\n",
544                 new_snapshot->exten,
545                 new_snapshot->appl,
546                 new_snapshot->data);
547 }
548
549 static struct ast_manager_event_blob *channel_new_callerid(
550         struct ast_channel_snapshot *old_snapshot,
551         struct ast_channel_snapshot *new_snapshot)
552 {
553         /* No NewCallerid event on cache clear or first event */
554         if (!old_snapshot || !new_snapshot) {
555                 return NULL;
556         }
557
558         if (ast_channel_snapshot_caller_id_equal(old_snapshot, new_snapshot)) {
559                 return NULL;
560         }
561
562         return ast_manager_event_blob_create(
563                 EVENT_FLAG_CALL, "NewCallerid",
564                 "CID-CallingPres: %d (%s)\r\n",
565                 new_snapshot->caller_pres,
566                 ast_describe_caller_presentation(new_snapshot->caller_pres));
567 }
568
569 static struct ast_manager_event_blob *channel_new_accountcode(
570         struct ast_channel_snapshot *old_snapshot,
571         struct ast_channel_snapshot *new_snapshot)
572 {
573         if (!old_snapshot || !new_snapshot) {
574                 return NULL;
575         }
576
577         if (!strcmp(old_snapshot->accountcode, new_snapshot->accountcode)) {
578                 return NULL;
579         }
580
581         return ast_manager_event_blob_create(
582                 EVENT_FLAG_CALL, "NewAccountCode",
583                 "OldAccountCode: %s\r\n", old_snapshot->accountcode);
584 }
585
586 channel_snapshot_monitor channel_monitors[] = {
587         channel_state_change,
588         channel_newexten,
589         channel_new_callerid,
590         channel_new_accountcode
591 };
592
593 static void channel_snapshot_update(void *data, struct stasis_subscription *sub,
594                                     struct stasis_message *message)
595 {
596         RAII_VAR(struct ast_str *, channel_event_string, NULL, ast_free);
597         struct stasis_cache_update *update;
598         struct ast_channel_snapshot *old_snapshot;
599         struct ast_channel_snapshot *new_snapshot;
600         size_t i;
601
602         update = stasis_message_data(message);
603
604         ast_assert(ast_channel_snapshot_type() == update->type);
605
606         old_snapshot = stasis_message_data(update->old_snapshot);
607         new_snapshot = stasis_message_data(update->new_snapshot);
608
609         for (i = 0; i < ARRAY_LEN(channel_monitors); ++i) {
610                 RAII_VAR(struct ast_manager_event_blob *, ev, NULL, ao2_cleanup);
611                 ev = channel_monitors[i](old_snapshot, new_snapshot);
612
613                 if (!ev) {
614                         continue;
615                 }
616
617                 /* If we haven't already, build the channel event string */
618                 if (!channel_event_string) {
619                         channel_event_string =
620                                 ast_manager_build_channel_state_string(new_snapshot);
621                         if (!channel_event_string) {
622                                 return;
623                         }
624                 }
625
626                 manager_event(ev->event_flags, ev->manager_event, "%s%s",
627                         ast_str_buffer(channel_event_string),
628                         ev->extra_fields);
629         }
630 }
631
632 static void publish_basic_channel_event(const char *event, int class, struct ast_channel_snapshot *snapshot)
633 {
634         RAII_VAR(struct ast_str *, channel_event_string, NULL, ast_free);
635
636         channel_event_string = ast_manager_build_channel_state_string(snapshot);
637         if (!channel_event_string) {
638                 return;
639         }
640
641         manager_event(class, event,
642                 "%s",
643                 ast_str_buffer(channel_event_string));
644 }
645
646 static void channel_hangup_request_cb(void *data,
647         struct stasis_subscription *sub,
648         struct stasis_message *message)
649 {
650         struct ast_channel_blob *obj = stasis_message_data(message);
651         RAII_VAR(struct ast_str *, extra, NULL, ast_free);
652         RAII_VAR(struct ast_str *, channel_event_string, NULL, ast_free);
653         struct ast_json *cause;
654         int is_soft;
655         char *manager_event = "HangupRequest";
656
657         extra = ast_str_create(20);
658         if (!extra) {
659                 return;
660         }
661
662         channel_event_string = ast_manager_build_channel_state_string(obj->snapshot);
663
664         if (!channel_event_string) {
665                 return;
666         }
667
668         cause = ast_json_object_get(obj->blob, "cause");
669         if (cause) {
670                 ast_str_append(&extra, 0,
671                                "Cause: %jd\r\n",
672                                ast_json_integer_get(cause));
673         }
674
675         is_soft = ast_json_is_true(ast_json_object_get(obj->blob, "soft"));
676         if (is_soft) {
677                 manager_event = "SoftHangupRequest";
678         }
679
680         manager_event(EVENT_FLAG_CALL, manager_event,
681                       "%s%s",
682                       ast_str_buffer(channel_event_string),
683                       ast_str_buffer(extra));
684 }
685
686 static void channel_chanspy_stop_cb(void *data, struct stasis_subscription *sub,
687                 struct stasis_message *message)
688 {
689         RAII_VAR(struct ast_str *, spyer_channel_string, NULL, ast_free);
690         struct ast_channel_snapshot *spyer;
691         struct ast_multi_channel_blob *payload = stasis_message_data(message);
692
693         spyer = ast_multi_channel_blob_get_channel(payload, "spyer_channel");
694         if (!spyer) {
695                 ast_log(AST_LOG_WARNING, "Received ChanSpy Start event with no spyer channel!\n");
696                 return;
697         }
698
699         spyer_channel_string = ast_manager_build_channel_state_string_prefix(spyer, "Spyer");
700         if (!spyer_channel_string) {
701                 return;
702         }
703
704         manager_event(EVENT_FLAG_CALL, "ChanSpyStop",
705                       "%s",
706                       ast_str_buffer(spyer_channel_string));
707 }
708
709 static void channel_chanspy_start_cb(void *data, struct stasis_subscription *sub,
710                 struct stasis_message *message)
711 {
712         RAII_VAR(struct ast_str *, spyer_channel_string, NULL, ast_free);
713         RAII_VAR(struct ast_str *, spyee_channel_string, NULL, ast_free);
714         struct ast_channel_snapshot *spyer;
715         struct ast_channel_snapshot *spyee;
716         struct ast_multi_channel_blob *payload = stasis_message_data(message);
717
718         spyer = ast_multi_channel_blob_get_channel(payload, "spyer_channel");
719         if (!spyer) {
720                 ast_log(AST_LOG_WARNING, "Received ChanSpy Start event with no spyer channel!\n");
721                 return;
722         }
723         spyee = ast_multi_channel_blob_get_channel(payload, "spyee_channel");
724         if (!spyee) {
725                 ast_log(AST_LOG_WARNING, "Received ChanSpy Start event with no spyee channel!\n");
726                 return;
727         }
728
729         spyer_channel_string = ast_manager_build_channel_state_string_prefix(spyer, "Spyer");
730         if (!spyer_channel_string) {
731                 return;
732         }
733         spyee_channel_string = ast_manager_build_channel_state_string_prefix(spyee, "Spyee");
734         if (!spyee_channel_string) {
735                 return;
736         }
737
738         manager_event(EVENT_FLAG_CALL, "ChanSpyStart",
739                       "%s%s",
740                       ast_str_buffer(spyer_channel_string),
741                       ast_str_buffer(spyee_channel_string));
742 }
743
744 static void channel_dtmf_begin_cb(void *data, struct stasis_subscription *sub,
745         struct stasis_message *message)
746 {
747         struct ast_channel_blob *obj = stasis_message_data(message);
748         RAII_VAR(struct ast_str *, channel_event_string, NULL, ast_free);
749         const char *digit =
750                 ast_json_string_get(ast_json_object_get(obj->blob, "digit"));
751         const char *direction =
752                 ast_json_string_get(ast_json_object_get(obj->blob, "direction"));
753
754         channel_event_string = ast_manager_build_channel_state_string(obj->snapshot);
755
756         if (!channel_event_string) {
757                 return;
758         }
759
760         /*** DOCUMENTATION
761                 <managerEventInstance>
762                         <synopsis>Raised when a DTMF digit has started on a channel.</synopsis>
763                                 <syntax>
764                                         <channel_snapshot/>
765                                         <parameter name="Digit">
766                                                 <para>DTMF digit received or transmitted (0-9, A-E, # or *</para>
767                                         </parameter>
768                                         <parameter name="Direction">
769                                                 <enumlist>
770                                                         <enum name="Received"/>
771                                                         <enum name="Sent"/>
772                                                 </enumlist>
773                                         </parameter>
774                                 </syntax>
775                 </managerEventInstance>
776         ***/
777         manager_event(EVENT_FLAG_DTMF, "DTMFBegin",
778                 "%s"
779                 "Digit: %s\r\n"
780                 "Direction: %s\r\n",
781                 ast_str_buffer(channel_event_string),
782                 digit, direction);
783 }
784
785 static void channel_dtmf_end_cb(void *data, struct stasis_subscription *sub,
786         struct stasis_message *message)
787 {
788         struct ast_channel_blob *obj = stasis_message_data(message);
789         RAII_VAR(struct ast_str *, channel_event_string, NULL, ast_free);
790         const char *digit =
791                 ast_json_string_get(ast_json_object_get(obj->blob, "digit"));
792         const char *direction =
793                 ast_json_string_get(ast_json_object_get(obj->blob, "direction"));
794         long duration_ms =
795                 ast_json_integer_get(ast_json_object_get(obj->blob, "duration_ms"));
796
797         channel_event_string = ast_manager_build_channel_state_string(obj->snapshot);
798
799         if (!channel_event_string) {
800                 return;
801         }
802
803         /*** DOCUMENTATION
804                 <managerEventInstance>
805                         <synopsis>Raised when a DTMF digit has ended on a channel.</synopsis>
806                                 <syntax>
807                                         <channel_snapshot/>
808                                         <parameter name="Digit">
809                                                 <para>DTMF digit received or transmitted (0-9, A-E, # or *</para>
810                                         </parameter>
811                                         <parameter name="DurationMs">
812                                                 <para>Duration (in milliseconds) DTMF was sent/received</para>
813                                         </parameter>
814                                         <parameter name="Direction">
815                                                 <enumlist>
816                                                         <enum name="Received"/>
817                                                         <enum name="Sent"/>
818                                                 </enumlist>
819                                         </parameter>
820                                 </syntax>
821                 </managerEventInstance>
822         ***/
823         manager_event(EVENT_FLAG_DTMF, "DTMFEnd",
824                 "%s"
825                 "Digit: %s\r\n"
826                 "DurationMs: %ld\r\n"
827                 "Direction: %s\r\n",
828                 ast_str_buffer(channel_event_string),
829                 digit, duration_ms, direction);
830 }
831
832 static void channel_hangup_handler_cb(void *data, struct stasis_subscription *sub,
833                 struct stasis_message *message)
834 {
835         RAII_VAR(struct ast_str *, channel_event_string, NULL, ast_free);
836         struct ast_channel_blob *payload = stasis_message_data(message);
837         const char *action = ast_json_string_get(ast_json_object_get(payload->blob, "type"));
838         const char *handler = ast_json_string_get(ast_json_object_get(payload->blob, "handler"));
839         const char *event;
840
841         channel_event_string = ast_manager_build_channel_state_string(payload->snapshot);
842
843         if (!channel_event_string) {
844                 return;
845         }
846
847         if (!strcmp(action, "type")) {
848                 event = "HangupHandlerRun";
849         } else if (!strcmp(action, "type")) {
850                 event = "HangupHandlerPop";
851         } else if (!strcmp(action, "type")) {
852                 event = "HangupHandlerPush";
853         } else {
854                 return;
855         }
856         manager_event(EVENT_FLAG_DIALPLAN, event,
857                 "%s"
858                 "Handler: %s\r\n",
859                 ast_str_buffer(channel_event_string),
860                 handler);
861 }
862
863 static void channel_fax_cb(void *data, struct stasis_subscription *sub,
864                 struct stasis_message *message)
865 {
866         RAII_VAR(struct ast_str *, channel_event_string, NULL, ast_free);
867         RAII_VAR(struct ast_str *, event_buffer, ast_str_create(256), ast_free);
868         struct ast_channel_blob *payload = stasis_message_data(message);
869         const char *type = ast_json_string_get(ast_json_object_get(payload->blob, "type"));
870         struct ast_json *operation = ast_json_object_get(payload->blob, "operation");
871         struct ast_json *status = ast_json_object_get(payload->blob, "status");
872         struct ast_json *local_station_id = ast_json_object_get(payload->blob, "local_station_id");
873         struct ast_json *remote_station_id = ast_json_object_get(payload->blob, "remote_station_id");
874         struct ast_json *fax_pages = ast_json_object_get(payload->blob, "fax_pages");
875         struct ast_json *fax_resolution = ast_json_object_get(payload->blob, "fax_resolution");
876         struct ast_json *fax_bitrate = ast_json_object_get(payload->blob, "fax_bitrate");
877         struct ast_json *filenames = ast_json_object_get(payload->blob, "filenames");
878         const char *event;
879         size_t array_len;
880         size_t i;
881
882         if (!event_buffer) {
883                 return;
884         }
885
886         channel_event_string = ast_manager_build_channel_state_string(payload->snapshot);
887         if (!channel_event_string) {
888                 return;
889         }
890
891         if (!strcmp(type, "status")) {
892                 event = "FAXStatus";
893         } else if (!strcmp(type, "receive")) {
894                 event = "ReceiveFAX";
895         } else if (!strcmp(type, "send")) {
896                 event = "SendFAX";
897         } else {
898                 return;
899         }
900
901         if (operation) {
902                 ast_str_append(&event_buffer, 0, "Operation: %s\r\n", ast_json_string_get(operation));
903         }
904         if (status) {
905                 ast_str_append(&event_buffer, 0, "Status: %s\r\n", ast_json_string_get(status));
906         }
907         if (local_station_id) {
908                 ast_str_append(&event_buffer, 0, "LocalStationID: %s\r\n", ast_json_string_get(local_station_id));
909         }
910         if (remote_station_id) {
911                 ast_str_append(&event_buffer, 0, "RemoteStationID: %s\r\n", ast_json_string_get(remote_station_id));
912         }
913         if (fax_pages) {
914                 ast_str_append(&event_buffer, 0, "PagesTransferred: %s\r\n", ast_json_string_get(fax_pages));
915         }
916         if (fax_resolution) {
917                 ast_str_append(&event_buffer, 0, "Resolution: %s\r\n", ast_json_string_get(fax_resolution));
918         }
919         if (fax_bitrate) {
920                 ast_str_append(&event_buffer, 0, "TransferRate: %s\r\n", ast_json_string_get(fax_bitrate));
921         }
922         if (filenames) {
923                 array_len = ast_json_array_size(filenames);
924                 for (i = 0; i < array_len; i++) {
925                         ast_str_append(&event_buffer, 0, "FileName: %s\r\n", ast_json_string_get(ast_json_array_get(filenames, i)));
926                 }
927         }
928
929         manager_event(EVENT_FLAG_CALL, event,
930                 "%s"
931                 "%s",
932                 ast_str_buffer(channel_event_string),
933                 ast_str_buffer(event_buffer));
934 }
935
936 static void channel_moh_start_cb(void *data, struct stasis_subscription *sub,
937                 struct stasis_message *message)
938 {
939         struct ast_channel_blob *payload = stasis_message_data(message);
940         struct ast_json *blob = payload->blob;
941         RAII_VAR(struct ast_str *, channel_event_string, NULL, ast_free);
942
943         channel_event_string = ast_manager_build_channel_state_string(payload->snapshot);
944         if (!channel_event_string) {
945                 return;
946         }
947
948         manager_event(EVENT_FLAG_CALL, "MusicOnHoldStart",
949                 "%s"
950                 "Class: %s\r\n",
951                 ast_str_buffer(channel_event_string),
952                 ast_json_string_get(ast_json_object_get(blob, "class")));
953
954 }
955
956 static void channel_moh_stop_cb(void *data, struct stasis_subscription *sub,
957                 struct stasis_message *message)
958 {
959         struct ast_channel_blob *payload = stasis_message_data(message);
960
961         publish_basic_channel_event("MusicOnHoldStop", EVENT_FLAG_CALL, payload->snapshot);
962 }
963
964 static void channel_monitor_start_cb(void *data, struct stasis_subscription *sub,
965                 struct stasis_message *message)
966 {
967         struct ast_channel_blob *payload = stasis_message_data(message);
968
969         publish_basic_channel_event("MonitorStart", EVENT_FLAG_CALL, payload->snapshot);
970 }
971
972 static void channel_monitor_stop_cb(void *data, struct stasis_subscription *sub,
973                 struct stasis_message *message)
974 {
975         struct ast_channel_blob *payload = stasis_message_data(message);
976
977         publish_basic_channel_event("MonitorStop", EVENT_FLAG_CALL, payload->snapshot);
978 }
979
980 /*!
981  * \brief Callback processing messages for channel dialing
982  */
983 static void channel_dial_cb(void *data, struct stasis_subscription *sub,
984         struct stasis_message *message)
985 {
986         struct ast_multi_channel_blob *obj = stasis_message_data(message);
987         const char *dialstatus;
988         const char *dialstring;
989         struct ast_channel_snapshot *caller;
990         struct ast_channel_snapshot *peer;
991         RAII_VAR(struct ast_str *, caller_event_string, NULL, ast_free);
992         RAII_VAR(struct ast_str *, peer_event_string, NULL, ast_free);
993
994         caller = ast_multi_channel_blob_get_channel(obj, "caller");
995         peer = ast_multi_channel_blob_get_channel(obj, "peer");
996
997         /* Peer is required - otherwise, who are we dialing? */
998         ast_assert(peer != NULL);
999         peer_event_string = ast_manager_build_channel_state_string_prefix(peer, "Dest");
1000         if (!peer_event_string) {
1001                 return;
1002         }
1003
1004         if (caller && !(caller_event_string = ast_manager_build_channel_state_string(caller))) {
1005                 return;
1006         }
1007
1008         dialstatus = ast_json_string_get(ast_json_object_get(ast_multi_channel_blob_get_json(obj), "dialstatus"));
1009         dialstring = ast_json_string_get(ast_json_object_get(ast_multi_channel_blob_get_json(obj), "dialstring"));
1010         if (ast_strlen_zero(dialstatus)) {
1011                 manager_event(EVENT_FLAG_CALL, "DialBegin",
1012                                 "%s"
1013                                 "%s"
1014                                 "DialString: %s\r\n",
1015                                 caller_event_string ? ast_str_buffer(caller_event_string) : "",
1016                                 ast_str_buffer(peer_event_string),
1017                                 S_OR(dialstring, "unknown"));
1018         } else {
1019                 manager_event(EVENT_FLAG_CALL, "DialEnd",
1020                                 "%s"
1021                                 "%s"
1022                                 "DialStatus: %s\r\n",
1023                                 caller_event_string ? ast_str_buffer(caller_event_string) : "",
1024                                 ast_str_buffer(peer_event_string),
1025                                 S_OR(dialstatus, "unknown"));
1026         }
1027
1028 }
1029
1030 static void channel_hold_cb(void *data, struct stasis_subscription *sub,
1031         struct stasis_message *message)
1032 {
1033         struct ast_channel_blob *obj = stasis_message_data(message);
1034         const char *musicclass;
1035         RAII_VAR(struct ast_str *, musicclass_string, NULL, ast_free);
1036         RAII_VAR(struct ast_str *, channel_event_string, NULL, ast_free);
1037
1038         if (!(musicclass_string = ast_str_create(32))) {
1039                 return;
1040         }
1041
1042         channel_event_string = ast_manager_build_channel_state_string(obj->snapshot);
1043         if (!channel_event_string) {
1044                 return;
1045         }
1046
1047         if (obj->blob) {
1048                 musicclass = ast_json_string_get(ast_json_object_get(obj->blob, "musicclass"));
1049
1050                 if (!ast_strlen_zero(musicclass)) {
1051                         ast_str_set(&musicclass_string, 0, "MusicClass: %s\r\n", musicclass);
1052                 }
1053         }
1054
1055         manager_event(EVENT_FLAG_CALL, "Hold",
1056                 "%s"
1057                 "%s",
1058                 ast_str_buffer(channel_event_string),
1059                 ast_str_buffer(musicclass_string));
1060 }
1061
1062 static void channel_unhold_cb(void *data, struct stasis_subscription *sub,
1063         struct stasis_message *message)
1064 {
1065         struct ast_channel_blob *obj = stasis_message_data(message);
1066         RAII_VAR(struct ast_str *, channel_event_string, NULL, ast_free);
1067
1068         channel_event_string = ast_manager_build_channel_state_string(obj->snapshot);
1069         if (!channel_event_string) {
1070                 return;
1071         }
1072
1073         manager_event(EVENT_FLAG_CALL, "Unhold",
1074                 "%s",
1075                 ast_str_buffer(channel_event_string));
1076 }
1077
1078 static void manager_channels_shutdown(void)
1079 {
1080         stasis_forward_cancel(topic_forwarder);
1081         topic_forwarder = NULL;
1082 }
1083
1084 int manager_channels_init(void)
1085 {
1086         int ret = 0;
1087         struct stasis_topic *manager_topic;
1088         struct stasis_topic *channel_topic;
1089         struct stasis_message_router *message_router;
1090
1091         manager_topic = ast_manager_get_topic();
1092         if (!manager_topic) {
1093                 return -1;
1094         }
1095         message_router = ast_manager_get_message_router();
1096         if (!message_router) {
1097                 return -1;
1098         }
1099         channel_topic = ast_channel_topic_all_cached();
1100         if (!channel_topic) {
1101                 return -1;
1102         }
1103
1104         topic_forwarder = stasis_forward_all(channel_topic, manager_topic);
1105         if (!topic_forwarder) {
1106                 return -1;
1107         }
1108
1109         ast_register_atexit(manager_channels_shutdown);
1110
1111         ret |= stasis_message_router_add_cache_update(message_router,
1112                 ast_channel_snapshot_type(), channel_snapshot_update, NULL);
1113
1114         ret |= stasis_message_router_add(message_router,
1115                 ast_channel_dtmf_begin_type(), channel_dtmf_begin_cb, NULL);
1116
1117         ret |= stasis_message_router_add(message_router,
1118                 ast_channel_dtmf_end_type(), channel_dtmf_end_cb, NULL);
1119
1120         ret |= stasis_message_router_add(message_router,
1121                 ast_channel_hangup_request_type(), channel_hangup_request_cb,
1122                 NULL);
1123
1124         ret |= stasis_message_router_add(message_router,
1125                 ast_channel_dial_type(), channel_dial_cb, NULL);
1126
1127         ret |= stasis_message_router_add(message_router,
1128                 ast_channel_hold_type(), channel_hold_cb, NULL);
1129
1130         ret |= stasis_message_router_add(message_router,
1131                 ast_channel_unhold_type(), channel_unhold_cb, NULL);
1132
1133         ret |= stasis_message_router_add(message_router,
1134                 ast_channel_fax_type(), channel_fax_cb, NULL);
1135
1136         ret |= stasis_message_router_add(message_router,
1137                 ast_channel_chanspy_start_type(), channel_chanspy_start_cb,
1138                 NULL);
1139
1140         ret |= stasis_message_router_add(message_router,
1141                 ast_channel_chanspy_stop_type(), channel_chanspy_stop_cb, NULL);
1142
1143         ret |= stasis_message_router_add(message_router,
1144                 ast_channel_hangup_handler_type(), channel_hangup_handler_cb,
1145                 NULL);
1146
1147         ret |= stasis_message_router_add(message_router,
1148                 ast_channel_moh_start_type(), channel_moh_start_cb, NULL);
1149
1150         ret |= stasis_message_router_add(message_router,
1151                 ast_channel_moh_stop_type(), channel_moh_stop_cb, NULL);
1152
1153         ret |= stasis_message_router_add(message_router,
1154                 ast_channel_monitor_start_type(), channel_monitor_start_cb,
1155                 NULL);
1156
1157         ret |= stasis_message_router_add(message_router,
1158                 ast_channel_monitor_stop_type(), channel_monitor_stop_cb, NULL);
1159
1160         /* If somehow we failed to add any routes, just shut down the whole
1161          * thing and fail it.
1162          */
1163         if (ret) {
1164                 manager_channels_shutdown();
1165                 return -1;
1166         }
1167
1168         return 0;
1169 }
1170