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