Migrate a large number of AMI events over to Stasis-Core
[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                                 <parameter name="Channel">
46                                 </parameter>
47                                 <parameter name="ChannelState">
48                                         <para>A numeric code for the channel's current state, related to ChannelStateDesc</para>
49                                 </parameter>
50                                 <parameter name="ChannelStateDesc">
51                                         <enumlist>
52                                                 <enum name="Down"/>
53                                                 <enum name="Rsrvd"/>
54                                                 <enum name="OffHook"/>
55                                                 <enum name="Dialing"/>
56                                                 <enum name="Ring"/>
57                                                 <enum name="Ringing"/>
58                                                 <enum name="Up"/>
59                                                 <enum name="Busy"/>
60                                                 <enum name="Dialing Offhook"/>
61                                                 <enum name="Pre-ring"/>
62                                                 <enum name="Unknown"/>
63                                         </enumlist>
64                                 </parameter>
65                                 <parameter name="CallerIDNum">
66                                 </parameter>
67                                 <parameter name="CallerIDName">
68                                 </parameter>
69                                 <parameter name="ConnectedLineNum">
70                                 </parameter>
71                                 <parameter name="ConnectedLineName">
72                                 </parameter>
73                                 <parameter name="AccountCode">
74                                 </parameter>
75                                 <parameter name="Context">
76                                 </parameter>
77                                 <parameter name="Exten">
78                                 </parameter>
79                                 <parameter name="Priority">
80                                 </parameter>
81                                 <parameter name="Uniqueid">
82                                 </parameter>
83                         </syntax>
84                 </managerEventInstance>
85         </managerEvent>
86         <managerEvent language="en_US" name="Newstate">
87                 <managerEventInstance class="EVENT_FLAG_CALL">
88                         <synopsis>Raised when a channel's state changes.</synopsis>
89                         <syntax>
90                                 <xi:include xpointer="xpointer(/docs/managerEvent[@name='Newchannel']/managerEventInstance/syntax/parameter)" />
91                         </syntax>
92                 </managerEventInstance>
93         </managerEvent>
94         <managerEvent language="en_US" name="Hangup">
95                 <managerEventInstance class="EVENT_FLAG_CALL">
96                         <synopsis>Raised when a channel is hung up.</synopsis>
97                         <syntax>
98                                 <xi:include xpointer="xpointer(/docs/managerEvent[@name='Newchannel']/managerEventInstance/syntax/parameter)" />
99                                 <parameter name="Cause">
100                                         <para>A numeric cause code for why the channel was hung up.</para>
101                                 </parameter>
102                                 <parameter name="Cause-txt">
103                                         <para>A description of why the channel was hung up.</para>
104                                 </parameter>
105                         </syntax>
106                 </managerEventInstance>
107         </managerEvent>
108         <managerEvent language="en_US" name="HangupRequest">
109                 <managerEventInstance class="EVENT_FLAG_CALL">
110                         <synopsis>Raised when a hangup is requested.</synopsis>
111                         <syntax>
112                                 <xi:include xpointer="xpointer(/docs/managerEvent[@name='Newchannel']/managerEventInstance/syntax/parameter)" />
113                                 <xi:include xpointer="xpointer(/docs/managerEvent[@name='Hangup']/managerEventInstance/syntax/parameter[@name='Cause'])" />
114                         </syntax>
115                 </managerEventInstance>
116         </managerEvent>
117         <managerEvent language="en_US" name="SoftHangupRequest">
118                 <managerEventInstance class="EVENT_FLAG_CALL">
119                         <synopsis>Raised when a soft hangup is requested with a specific cause code.</synopsis>
120                         <syntax>
121                                 <xi:include xpointer="xpointer(/docs/managerEvent[@name='Newchannel']/managerEventInstance/syntax/parameter)" />
122                                 <xi:include xpointer="xpointer(/docs/managerEvent[@name='Hangup']/managerEventInstance/syntax/parameter[@name='Cause'])" />
123                         </syntax>
124                 </managerEventInstance>
125         </managerEvent>
126         <managerEvent language="en_US" name="NewExten">
127                 <managerEventInstance class="EVENT_FLAG_DIALPLAN">
128                         <synopsis>Raised when a channel enters a new context, extension, priority.</synopsis>
129                         <syntax>
130                                 <xi:include xpointer="xpointer(/docs/managerEvent[@name='Newchannel']/managerEventInstance/syntax/parameter)" />
131                                 <parameter name="Extension">
132                                         <para>Deprecated in 12, but kept for
133                                         backward compatability. Please use
134                                         'Exten' instead.</para>
135                                 </parameter>
136                                 <parameter name="Application">
137                                         <para>The application about to be executed.</para>
138                                 </parameter>
139                                 <parameter name="AppData">
140                                         <para>The data to be passed to the application.</para>
141                                 </parameter>
142                         </syntax>
143                 </managerEventInstance>
144         </managerEvent>
145         <managerEvent language="en_US" name="NewCallerid">
146                 <managerEventInstance class="EVENT_FLAG_CALL">
147                         <synopsis>Raised when a channel receives new Caller ID information.</synopsis>
148                         <syntax>
149                                 <xi:include xpointer="xpointer(/docs/managerEvent[@name='Newchannel']/managerEventInstance/syntax/parameter)" />
150                                 <parameter name="CID-CallingPres">
151                                         <para>A description of the Caller ID presentation.</para>
152                                 </parameter>
153                         </syntax>
154                 </managerEventInstance>
155         </managerEvent>
156         <managerEvent language="en_US" name="DialBegin">
157                 <managerEventInstance class="EVENT_FLAG_CALL">
158                         <synopsis>Raised when a dial action has started.</synopsis>
159                         <syntax>
160                                 <xi:include xpointer="xpointer(/docs/managerEvent[@name='Newchannel']/managerEventInstance/syntax/parameter)" />
161                                 <parameter name="DestChannel">
162                                 </parameter>
163                                 <parameter name="DestChannelState">
164                                         <para>A numeric code for the channel's current state, related to DestChannelStateDesc</para>
165                                 </parameter>
166                                 <parameter name="DestChannelStateDesc">
167                                         <enumlist>
168                                                 <enum name="Down"/>
169                                                 <enum name="Rsrvd"/>
170                                                 <enum name="OffHook"/>
171                                                 <enum name="Dialing"/>
172                                                 <enum name="Ring"/>
173                                                 <enum name="Ringing"/>
174                                                 <enum name="Up"/>
175                                                 <enum name="Busy"/>
176                                                 <enum name="Dialing Offhook"/>
177                                                 <enum name="Pre-ring"/>
178                                                 <enum name="Unknown"/>
179                                         </enumlist>
180                                 </parameter>
181                                 <parameter name="DestCallerIDNum">
182                                 </parameter>
183                                 <parameter name="DestCallerIDName">
184                                 </parameter>
185                                 <parameter name="DestConnectedLineNum">
186                                 </parameter>
187                                 <parameter name="DestConnectedLineName">
188                                 </parameter>
189                                 <parameter name="DestAccountCode">
190                                 </parameter>
191                                 <parameter name="DestContext">
192                                 </parameter>
193                                 <parameter name="DestExten">
194                                 </parameter>
195                                 <parameter name="DestPriority">
196                                 </parameter>
197                                 <parameter name="DestUniqueid">
198                                 </parameter>
199                                 <parameter name="DialString">
200                                         <para>The non-technology specific device being dialed.</para>
201                                 </parameter>
202                         </syntax>
203                         <see-also>
204                                 <ref type="application">Dial</ref>
205                         </see-also>
206                 </managerEventInstance>
207         </managerEvent>
208         <managerEvent language="en_US" name="DialEnd">
209                 <managerEventInstance class="EVENT_FLAG_CALL">
210                         <synopsis>Raised when a dial action has completed.</synopsis>
211                         <syntax>
212                                 <xi:include xpointer="xpointer(/docs/managerEvent[@name='Newchannel']/managerEventInstance/syntax/parameter)" />
213                                 <xi:include xpointer="xpointer(/docs/managerEvent[@name='DialBegin']/managerEventInstance/syntax/parameter[contains(@name, 'Dest')])" />
214                                 <parameter name="DialStatus">
215                                         <para>The result of the dial operation.</para>
216                                         <enumlist>
217                                                 <enum name="ANSWER" />
218                                                 <enum name="BUSY" />
219                                                 <enum name="CANCEL" />
220                                                 <enum name="CHANUNAVAIL" />
221                                                 <enum name="CONGESTION" />
222                                                 <enum name="NOANSWER" />
223                                         </enumlist>
224                                 </parameter>
225                         </syntax>
226                         <see-also>
227                                 <ref type="application">Dial</ref>
228                         </see-also>
229                 </managerEventInstance>
230         </managerEvent>
231         <managerEvent language="en_US" name="ChanSpyStart">
232                 <managerEventInstance class="EVENT_FLAG_CALL">
233                         <synopsis>Raised when one channel begins spying on another channel.</synopsis>
234                         <syntax>
235                                 <parameter name="SpyerChannel">
236                                         <para>The channel performing the spying.</para>
237                                 </parameter>
238                                 <parameter name="SpyerChannelState">
239                                         <para>A numeric code for the channel's current state, related to SpyerChannelStateDesc</para>
240                                 </parameter>
241                                 <parameter name="SpyerChannelStateDesc">
242                                         <enumlist>
243                                                 <enum name="Down"/>
244                                                 <enum name="Rsrvd"/>
245                                                 <enum name="OffHook"/>
246                                                 <enum name="Dialing"/>
247                                                 <enum name="Ring"/>
248                                                 <enum name="Ringing"/>
249                                                 <enum name="Up"/>
250                                                 <enum name="Busy"/>
251                                                 <enum name="Dialing Offhook"/>
252                                                 <enum name="Pre-ring"/>
253                                                 <enum name="Unknown"/>
254                                         </enumlist>
255                                 </parameter>
256                                 <parameter name="SpyerCallerIDNum">
257                                 </parameter>
258                                 <parameter name="SpyerCallerIDName">
259                                 </parameter>
260                                 <parameter name="SpyerConnectedLineNum">
261                                 </parameter>
262                                 <parameter name="SpyerConnectedLineName">
263                                 </parameter>
264                                 <parameter name="SpyerAccountCode">
265                                 </parameter>
266                                 <parameter name="SpyerContext">
267                                 </parameter>
268                                 <parameter name="SpyerExten">
269                                 </parameter>
270                                 <parameter name="SpyerPriority">
271                                 </parameter>
272                                 <parameter name="SpyerUniqueid">
273                                 </parameter>
274                                 <parameter name="SpyeeChannel">
275                                         <para>The channel being spied upon.</para>
276                                 </parameter>
277                                 <parameter name="SpyeeChannelState">
278                                         <para>A numeric code for the channel's current state, related to SpyeeChannelStateDesc</para>
279                                 </parameter>
280                                 <parameter name="SpyeeChannelStateDesc">
281                                         <enumlist>
282                                                 <enum name="Down"/>
283                                                 <enum name="Rsrvd"/>
284                                                 <enum name="OffHook"/>
285                                                 <enum name="Dialing"/>
286                                                 <enum name="Ring"/>
287                                                 <enum name="Ringing"/>
288                                                 <enum name="Up"/>
289                                                 <enum name="Busy"/>
290                                                 <enum name="Dialing Offhook"/>
291                                                 <enum name="Pre-ring"/>
292                                                 <enum name="Unknown"/>
293                                         </enumlist>
294                                 </parameter>
295                                 <parameter name="SpyeeCallerIDNum">
296                                 </parameter>
297                                 <parameter name="SpyeeCallerIDName">
298                                 </parameter>
299                                 <parameter name="SpyeeConnectedLineNum">
300                                 </parameter>
301                                 <parameter name="SpyeeConnectedLineName">
302                                 </parameter>
303                                 <parameter name="SpyeeAccountCode">
304                                 </parameter>
305                                 <parameter name="SpyeeContext">
306                                 </parameter>
307                                 <parameter name="SpyeeExten">
308                                 </parameter>
309                                 <parameter name="SpyeePriority">
310                                 </parameter>
311                                 <parameter name="SpyeeUniqueid">
312                                 </parameter>
313                         </syntax>
314                         <see-also>
315                                 <ref type="application">ChanSpyStop</ref>
316                         </see-also>
317                 </managerEventInstance>
318         </managerEvent>
319         <managerEvent language="en_US" name="ChanSpyStop">
320                 <managerEventInstance class="EVENT_FLAG_CALL">
321                         <synopsis>Raised when a channel has stopped spying.</synopsis>
322                         <syntax>
323                                 <xi:include xpointer="xpointer(/docs/managerEvent[@name='ChanSpyStart']/managerEventInstance/syntax/parameter[contains(@name, 'Spyer')])" />
324                         </syntax>
325                         <see-also>
326                                 <ref type="application">ChanSpyStart</ref>
327                         </see-also>
328                 </managerEventInstance>
329         </managerEvent>
330         <managerEvent language="en_US" name="HangupHandlerRun">
331                 <managerEventInstance class="EVENT_FLAG_DIALPLAN">
332                         <synopsis>Raised when a hangup handler is about to be called.</synopsis>
333                         <syntax>
334                                 <xi:include xpointer="xpointer(/docs/managerEvent[@name='Newchannel']/managerEventInstance/syntax/parameter)" />
335                                 <parameter name="Handler">
336                                         <para>Hangup handler parameter string passed to the Gosub application.</para>
337                                 </parameter>
338                         </syntax>
339                 </managerEventInstance>
340         </managerEvent>
341         <managerEvent language="en_US" name="HangupHandlerPop">
342                 <managerEventInstance class="EVENT_FLAG_DIALPLAN">
343                         <synopsis>
344                                 Raised when a hangup handler is removed from the handler stack
345                                 by the CHANNEL() function.
346                         </synopsis>
347                         <syntax>
348                                 <xi:include xpointer="xpointer(/docs/managerEvent[@name='HangupHandlerRun']/managerEventInstance/syntax/parameter)" />
349                         </syntax>
350                         <see-also>
351                                 <ref type="managerEvent">HangupHandlerPush</ref>
352                                 <ref type="function">CHANNEL</ref>
353                         </see-also>
354                 </managerEventInstance>
355         </managerEvent>
356         <managerEvent language="en_US" name="HangupHandlerPush">
357                 <managerEventInstance class="EVENT_FLAG_DIALPLAN">
358                         <synopsis>
359                                 Raised when a hangup handler is added to the handler stack by
360                                 the CHANNEL() function.
361                         </synopsis>
362                         <syntax>
363                                 <xi:include xpointer="xpointer(/docs/managerEvent[@name='HangupHandlerRun']/managerEventInstance/syntax/parameter)" />
364                         </syntax>
365                         <see-also>
366                                 <ref type="managerEvent">HangupHandlerPop</ref>
367                                 <ref type="function">CHANNEL</ref>
368                         </see-also>
369                 </managerEventInstance>
370         </managerEvent>
371         <managerEvent language="en_US" name="FAXStatus">
372                 <managerEventInstance class="EVENT_FLAG_CALL">
373                         <synopsis>
374                                 Raised periodically during a fax transmission.
375                         </synopsis>
376                         <syntax>
377                                 <xi:include xpointer="xpointer(/docs/managerEvent[@name='Newchannel']/managerEventInstance/syntax/parameter)" />
378                                 <parameter name="Operation">
379                                         <enumlist>
380                                                 <enum name="gateway"/>
381                                                 <enum name="receive"/>
382                                                 <enum name="send"/>
383                                         </enumlist>
384                                 </parameter>
385                                 <parameter name="Status">
386                                         <para>A text message describing the current status of the fax</para>
387                                 </parameter>
388                                 <xi:include xpointer="xpointer(/docs/managerEvent[@name='ReceiveFAX']/managerEventInstance/syntax/parameter[@name='LocalStationID'])" />
389                                 <xi:include xpointer="xpointer(/docs/managerEvent[@name='ReceiveFAX']/managerEventInstance/syntax/parameter[@name='FileName'])" />
390                         </syntax>
391                 </managerEventInstance>
392         </managerEvent>
393         <managerEvent language="en_US" name="ReceiveFAX">
394                 <managerEventInstance class="EVENT_FLAG_CALL">
395                         <synopsis>
396                                 Raised when a receive fax operation has completed.
397                         </synopsis>
398                         <syntax>
399                                 <xi:include xpointer="xpointer(/docs/managerEvent[@name='Newchannel']/managerEventInstance/syntax/parameter)" />
400                                 <parameter name="LocalStationID">
401                                         <para>The value of the <variable>LOCALSTATIONID</variable> channel variable</para>
402                                 </parameter>
403                                 <parameter name="RemoteStationID">
404                                         <para>The value of the <variable>REMOTESTATIONID</variable> channel variable</para>
405                                 </parameter>
406                                 <parameter name="PagesTransferred">
407                                         <para>The number of pages that have been transferred</para>
408                                 </parameter>
409                                 <parameter name="Resolution">
410                                         <para>The negotiated resolution</para>
411                                 </parameter>
412                                 <parameter name="TransferRate">
413                                         <para>The negotiated transfer rate</para>
414                                 </parameter>
415                                 <parameter name="FileName" multiple="yes">
416                                         <para>The files being affected by the fax operation</para>
417                                 </parameter>
418                         </syntax>
419                 </managerEventInstance>
420         </managerEvent>
421         <managerEvent language="en_US" name="SendFAX">
422                 <managerEventInstance class="EVENT_FLAG_CALL">
423                         <synopsis>
424                                 Raised when a send fax operation has completed.
425                         </synopsis>
426                         <syntax>
427                                 <xi:include xpointer="xpointer(/docs/managerEvent[@name='Newchannel']/managerEventInstance/syntax/parameter)" />
428                                 <xi:include xpointer="xpointer(/docs/managerEvent[@name='ReceiveFAX']/managerEventInstance/syntax/parameter)" />
429                         </syntax>
430                 </managerEventInstance>
431         </managerEvent>
432         <managerEvent language="en_US" name="MusicOnHoldStart">
433                 <managerEventInstance class="EVENT_FLAG_CALL">
434                         <synopsis>Raised when music on hold has started on a channel.</synopsis>
435                         <syntax>
436                                 <xi:include xpointer="xpointer(/docs/managerEvent[@name='Newchannel']/managerEventInstance/syntax/parameter)" />
437                                 <parameter name="Class">
438                                         <para>The class of music being played on the channel</para>
439                                 </parameter>
440                         </syntax>
441                         <see-also>
442                                 <ref type="managerEvent">MusicOnHoldStop</ref>
443                                 <ref type="application">MusicOnHold</ref>
444                         </see-also>
445                 </managerEventInstance>
446         </managerEvent>
447         <managerEvent language="en_US" name="MusicOnHoldStop">
448                 <managerEventInstance class="EVENT_FLAG_CALL">
449                         <synopsis>Raised when music on hold has stopped on a channel.</synopsis>
450                         <syntax>
451                                 <xi:include xpointer="xpointer(/docs/managerEvent[@name='Newchannel']/managerEventInstance/syntax/parameter)" />
452                         </syntax>
453                         <see-also>
454                                 <ref type="managerEvent">MusicOnHoldStart</ref>
455                                 <ref type="application">StopMusicOnHold</ref>
456                         </see-also>
457                 </managerEventInstance>
458         </managerEvent>
459         <managerEvent language="en_US" name="MonitorStart">
460                 <managerEventInstance class="EVENT_FLAG_CALL">
461                         <synopsis>Raised when monitoring has started on a channel.</synopsis>
462                         <syntax>
463                                 <xi:include xpointer="xpointer(/docs/managerEvent[@name='Newchannel']/managerEventInstance/syntax/parameter)" />
464                         </syntax>
465                         <see-also>
466                                 <ref type="managerEvent">MonitorStop</ref>
467                                 <ref type="application">Monitor</ref>
468                                 <ref type="manager">Monitor</ref>
469                         </see-also>
470                 </managerEventInstance>
471         </managerEvent>
472         <managerEvent language="en_US" name="MonitorStop">
473                 <managerEventInstance class="EVENT_FLAG_CALL">
474                 <synopsis>Raised when monitoring has stopped on a channel.</synopsis>
475                 <syntax>
476                         <xi:include xpointer="xpointer(/docs/managerEvent[@name='Newchannel']/managerEventInstance/syntax/parameter)" />
477                 </syntax>
478                 <see-also>
479                         <ref type="managerEvent">MonitorStart</ref>
480                         <ref type="application">StopMonitor</ref>
481                         <ref type="manager">StopMonitor</ref>
482                 </see-also>
483                 </managerEventInstance>
484         </managerEvent>
485 ***/
486
487 /*! \brief The \ref stasis subscription returned by the forwarding of the channel topic
488  * to the manager topic
489  */
490 static struct stasis_subscription *topic_forwarder;
491
492 struct ast_str *ast_manager_build_channel_state_string_prefix(
493                 const struct ast_channel_snapshot *snapshot,
494                 const char *prefix)
495 {
496         struct ast_str *out = ast_str_create(1024);
497         int res = 0;
498         if (!out) {
499                 return NULL;
500         }
501         res = ast_str_set(&out, 0,
502                 "%sChannel: %s\r\n"
503                 "%sChannelState: %d\r\n"
504                 "%sChannelStateDesc: %s\r\n"
505                 "%sCallerIDNum: %s\r\n"
506                 "%sCallerIDName: %s\r\n"
507                 "%sConnectedLineNum: %s\r\n"
508                 "%sConnectedLineName: %s\r\n"
509                 "%sAccountCode: %s\r\n"
510                 "%sContext: %s\r\n"
511                 "%sExten: %s\r\n"
512                 "%sPriority: %d\r\n"
513                 "%sUniqueid: %s\r\n",
514                 prefix, snapshot->name,
515                 prefix, snapshot->state,
516                 prefix, ast_state2str(snapshot->state),
517                 prefix, S_OR(snapshot->caller_number, "<unknown>"),
518                 prefix, S_OR(snapshot->caller_name, "<unknown>"),
519                 prefix, S_OR(snapshot->connected_number, "<unknown>"),
520                 prefix, S_OR(snapshot->connected_name, "<unknown>"),
521                 prefix, snapshot->accountcode,
522                 prefix, snapshot->context,
523                 prefix, snapshot->exten,
524                 prefix, snapshot->priority,
525                 prefix, snapshot->uniqueid);
526
527         if (!res) {
528                 return NULL;
529         }
530
531         if (snapshot->manager_vars) {
532                 struct ast_var_t *var;
533                 AST_LIST_TRAVERSE(snapshot->manager_vars, var, entries) {
534                         ast_str_append(&out, 0, "%sChanVariable: %s=%s\r\n",
535                                        prefix,
536                                        var->name, var->value);
537                 }
538         }
539
540         return out;
541 }
542
543 struct ast_str *ast_manager_build_channel_state_string(
544                 const struct ast_channel_snapshot *snapshot)
545 {
546         return ast_manager_build_channel_state_string_prefix(snapshot, "");
547 }
548
549 /*! \brief Typedef for callbacks that get called on channel snapshot updates */
550 typedef struct ast_manager_event_blob *(*channel_snapshot_monitor)(
551         struct ast_channel_snapshot *old_snapshot,
552         struct ast_channel_snapshot *new_snapshot);
553
554 /*! \brief Handle channel state changes */
555 static struct ast_manager_event_blob *channel_state_change(
556         struct ast_channel_snapshot *old_snapshot,
557         struct ast_channel_snapshot *new_snapshot)
558 {
559         int is_hungup, was_hungup;
560
561         if (!new_snapshot) {
562                 /* Ignore cache clearing events; we'll see the hangup first */
563                 return NULL;
564         }
565
566         /* The Newchannel, Newstate and Hangup events are closely related, in
567          * in that they are mutually exclusive, basically different flavors
568          * of a new channel state event.
569          */
570
571         if (!old_snapshot) {
572                 return ast_manager_event_blob_create(
573                         EVENT_FLAG_CALL, "Newchannel", NO_EXTRA_FIELDS);
574         }
575
576         was_hungup = ast_test_flag(&old_snapshot->flags, AST_FLAG_ZOMBIE) ? 1 : 0;
577         is_hungup = ast_test_flag(&new_snapshot->flags, AST_FLAG_ZOMBIE) ? 1 : 0;
578
579         if (!was_hungup && is_hungup) {
580                 return ast_manager_event_blob_create(
581                         EVENT_FLAG_CALL, "Hangup",
582                         "Cause: %d\r\n"
583                         "Cause-txt: %s\r\n",
584                         new_snapshot->hangupcause,
585                         ast_cause2str(new_snapshot->hangupcause));
586         }
587
588         if (old_snapshot->state != new_snapshot->state) {
589                 return ast_manager_event_blob_create(
590                         EVENT_FLAG_CALL, "Newstate", NO_EXTRA_FIELDS);
591         }
592
593         /* No event */
594         return NULL;
595 }
596
597 static struct ast_manager_event_blob *channel_newexten(
598         struct ast_channel_snapshot *old_snapshot,
599         struct ast_channel_snapshot *new_snapshot)
600 {
601         /* No Newexten event on cache clear */
602         if (!new_snapshot) {
603                 return NULL;
604         }
605
606         /* Empty application is not valid for a Newexten event */
607         if (ast_strlen_zero(new_snapshot->appl)) {
608                 return NULL;
609         }
610
611         if (old_snapshot && ast_channel_snapshot_cep_equal(old_snapshot, new_snapshot)) {
612                 return NULL;
613         }
614
615         /* DEPRECATED: Extension field deprecated in 12; remove in 14 */
616         return ast_manager_event_blob_create(
617                 EVENT_FLAG_CALL, "Newexten",
618                 "Extension: %s\r\n"
619                 "Application: %s\r\n"
620                 "AppData: %s\r\n",
621                 new_snapshot->exten,
622                 new_snapshot->appl,
623                 new_snapshot->data);
624 }
625
626 static struct ast_manager_event_blob *channel_new_callerid(
627         struct ast_channel_snapshot *old_snapshot,
628         struct ast_channel_snapshot *new_snapshot)
629 {
630         /* No NewCallerid event on cache clear or first event */
631         if (!old_snapshot || !new_snapshot) {
632                 return NULL;
633         }
634
635         if (ast_channel_snapshot_caller_id_equal(old_snapshot, new_snapshot)) {
636                 return NULL;
637         }
638
639         return ast_manager_event_blob_create(
640                 EVENT_FLAG_CALL, "NewCallerid",
641                 "CID-CallingPres: %d (%s)\r\n",
642                 new_snapshot->caller_pres,
643                 ast_describe_caller_presentation(new_snapshot->caller_pres));
644 }
645
646 channel_snapshot_monitor channel_monitors[] = {
647         channel_state_change,
648         channel_newexten,
649         channel_new_callerid
650 };
651
652 static void channel_snapshot_update(void *data, struct stasis_subscription *sub,
653                                     struct stasis_topic *topic,
654                                     struct stasis_message *message)
655 {
656         RAII_VAR(struct ast_str *, channel_event_string, NULL, ast_free);
657         struct stasis_cache_update *update;
658         struct ast_channel_snapshot *old_snapshot;
659         struct ast_channel_snapshot *new_snapshot;
660         size_t i;
661
662         update = stasis_message_data(message);
663
664         if (ast_channel_snapshot_type() != update->type) {
665                 return;
666         }
667
668         old_snapshot = stasis_message_data(update->old_snapshot);
669         new_snapshot = stasis_message_data(update->new_snapshot);
670
671         for (i = 0; i < ARRAY_LEN(channel_monitors); ++i) {
672                 RAII_VAR(struct ast_manager_event_blob *, ev, NULL, ao2_cleanup);
673                 ev = channel_monitors[i](old_snapshot, new_snapshot);
674
675                 if (!ev) {
676                         continue;
677                 }
678
679                 /* If we haven't already, build the channel event string */
680                 if (!channel_event_string) {
681                         channel_event_string =
682                                 ast_manager_build_channel_state_string(new_snapshot);
683                         if (!channel_event_string) {
684                                 return;
685                         }
686                 }
687
688                 manager_event(ev->event_flags, ev->manager_event, "%s%s",
689                         ast_str_buffer(channel_event_string),
690                         ev->extra_fields);
691         }
692 }
693
694 static void channel_varset_cb(void *data, struct stasis_subscription *sub,
695         struct stasis_topic *topic, struct stasis_message *message)
696 {
697         struct ast_channel_blob *obj = stasis_message_data(message);
698         RAII_VAR(struct ast_str *, channel_event_string, NULL, ast_free);
699         const char *variable = ast_json_string_get(ast_json_object_get(obj->blob, "variable"));
700         const char *value = ast_json_string_get(ast_json_object_get(obj->blob, "value"));
701
702         if (obj->snapshot) {
703                 channel_event_string = ast_manager_build_channel_state_string(obj->snapshot);
704         } else {
705                 channel_event_string = ast_str_create(35);
706                 ast_str_set(&channel_event_string, 0,
707                             "Channel: none\r\n"
708                             "Uniqueid: none\r\n");
709         }
710
711         if (!channel_event_string) {
712                 return;
713         }
714
715         /*** DOCUMENTATION
716                 <managerEventInstance>
717                         <synopsis>Raised when a variable is set to a particular value.</synopsis>
718                         <syntax>
719                                 <xi:include xpointer="xpointer(/docs/managerEvent[@name='Newchannel']/managerEventInstance/syntax/parameter)" />
720                                 <parameter name="Variable">
721                                         <para>The variable being set.</para>
722                                 </parameter>
723                                 <parameter name="Value">
724                                         <para>The new value of the variable.</para>
725                                 </parameter>
726                         </syntax>
727                 </managerEventInstance>
728         ***/
729         manager_event(EVENT_FLAG_DIALPLAN, "VarSet",
730                       "%s"
731                       "Variable: %s\r\n"
732                       "Value: %s\r\n",
733                       ast_str_buffer(channel_event_string),
734                       variable, value);
735 }
736
737 static int userevent_exclusion_cb(const char *key)
738 {
739         if (!strcmp("type", key)) {
740                 return 1;
741         }
742         if (!strcmp("eventname", key)) {
743                 return 1;
744         }
745         return 0;
746 }
747
748 static void channel_user_event_cb(void *data, struct stasis_subscription *sub,
749         struct stasis_topic *topic, 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         RAII_VAR(struct ast_str *, body, NULL, ast_free);
754         const char *eventname;
755
756         eventname = ast_json_string_get(ast_json_object_get(obj->blob, "eventname"));
757         body = ast_manager_str_from_json_object(obj->blob, userevent_exclusion_cb);
758         channel_event_string = ast_manager_build_channel_state_string(obj->snapshot);
759
760         if (!channel_event_string || !body) {
761                 return;
762         }
763
764         /*** DOCUMENTATION
765                 <managerEventInstance>
766                         <synopsis>A user defined event raised from the dialplan.</synopsis>
767                         <syntax>
768                                 <xi:include xpointer="xpointer(/docs/managerEvent[@name='Newchannel']/managerEventInstance/syntax/parameter)" />
769                                 <parameter name="UserEvent">
770                                         <para>The event name, as specified in the dialplan.</para>
771                                 </parameter>
772                         </syntax>
773                         <see-also>
774                                 <ref type="application">UserEvent</ref>
775                         </see-also>
776                 </managerEventInstance>
777         ***/
778         manager_event(EVENT_FLAG_USER, "UserEvent",
779                       "%s"
780                       "UserEvent: %s\r\n"
781                       "%s",
782                       ast_str_buffer(channel_event_string), eventname, ast_str_buffer(body));
783 }
784
785 static void publish_basic_channel_event(const char *event, int class, struct ast_channel_snapshot *snapshot)
786 {
787         RAII_VAR(struct ast_str *, channel_event_string, NULL, ast_free);
788
789         channel_event_string = ast_manager_build_channel_state_string(snapshot);
790         if (!channel_event_string) {
791                 return;
792         }
793
794         manager_event(class, event,
795                 "%s",
796                 ast_str_buffer(channel_event_string));
797 }
798
799 static void channel_hangup_request_cb(void *data,
800         struct stasis_subscription *sub, struct stasis_topic *topic,
801         struct stasis_message *message)
802 {
803         struct ast_channel_blob *obj = stasis_message_data(message);
804         RAII_VAR(struct ast_str *, extra, NULL, ast_free);
805         RAII_VAR(struct ast_str *, channel_event_string, NULL, ast_free);
806         struct ast_json *cause;
807         int is_soft;
808         char *manager_event = "HangupRequest";
809
810         extra = ast_str_create(20);
811         if (!extra) {
812                 return;
813         }
814
815         channel_event_string = ast_manager_build_channel_state_string(obj->snapshot);
816
817         if (!channel_event_string) {
818                 return;
819         }
820
821         cause = ast_json_object_get(obj->blob, "cause");
822         if (cause) {
823                 ast_str_append(&extra, 0,
824                                "Cause: %jd\r\n",
825                                ast_json_integer_get(cause));
826         }
827
828         is_soft = ast_json_is_true(ast_json_object_get(obj->blob, "soft"));
829         if (is_soft) {
830                 manager_event = "SoftHangupRequest";
831         }
832
833         manager_event(EVENT_FLAG_CALL, manager_event,
834                       "%s%s",
835                       ast_str_buffer(channel_event_string),
836                       ast_str_buffer(extra));
837 }
838
839 static void channel_chanspy_stop_cb(void *data, struct stasis_subscription *sub,
840                 struct stasis_topic *topic, struct stasis_message *message)
841 {
842         RAII_VAR(struct ast_str *, spyer_channel_string, NULL, ast_free);
843         RAII_VAR(struct ast_channel_snapshot *, spyer, NULL, ao2_cleanup);
844         struct ast_multi_channel_blob *payload = stasis_message_data(message);
845
846         spyer = ast_multi_channel_blob_get_channel(payload, "spyer_channel");
847         if (!spyer) {
848                 ast_log(AST_LOG_WARNING, "Received ChanSpy Start event with no spyer channel!\n");
849                 return;
850         }
851
852         spyer_channel_string = ast_manager_build_channel_state_string_prefix(spyer, "Spyer");
853         if (!spyer_channel_string) {
854                 return;
855         }
856
857         manager_event(EVENT_FLAG_CALL, "ChanSpyStop",
858                       "%s",
859                       ast_str_buffer(spyer_channel_string));
860 }
861
862 static void channel_chanspy_start_cb(void *data, struct stasis_subscription *sub,
863                 struct stasis_topic *topic, struct stasis_message *message)
864 {
865         RAII_VAR(struct ast_str *, spyer_channel_string, NULL, ast_free);
866         RAII_VAR(struct ast_str *, spyee_channel_string, NULL, ast_free);
867         RAII_VAR(struct ast_channel_snapshot *, spyer, NULL, ao2_cleanup);
868         RAII_VAR(struct ast_channel_snapshot *, spyee, NULL, ao2_cleanup);
869         struct ast_multi_channel_blob *payload = stasis_message_data(message);
870
871         spyer = ast_multi_channel_blob_get_channel(payload, "spyer_channel");
872         if (!spyer) {
873                 ast_log(AST_LOG_WARNING, "Received ChanSpy Start event with no spyer channel!\n");
874                 return;
875         }
876         spyee = ast_multi_channel_blob_get_channel(payload, "spyee_channel");
877         if (!spyee) {
878                 ast_log(AST_LOG_WARNING, "Received ChanSpy Start event with no spyee channel!\n");
879                 return;
880         }
881
882         spyer_channel_string = ast_manager_build_channel_state_string_prefix(spyer, "Spyer");
883         if (!spyer_channel_string) {
884                 return;
885         }
886         spyee_channel_string = ast_manager_build_channel_state_string_prefix(spyee, "Spyee");
887         if (!spyee_channel_string) {
888                 return;
889         }
890
891         manager_event(EVENT_FLAG_CALL, "ChanSpyStart",
892                       "%s%s",
893                       ast_str_buffer(spyer_channel_string),
894                       ast_str_buffer(spyee_channel_string));
895 }
896
897 static void channel_dtmf_begin_cb(void *data, struct stasis_subscription *sub,
898         struct stasis_topic *topic, struct stasis_message *message)
899 {
900         struct ast_channel_blob *obj = stasis_message_data(message);
901         RAII_VAR(struct ast_str *, channel_event_string, NULL, ast_free);
902         const char *digit =
903                 ast_json_string_get(ast_json_object_get(obj->blob, "digit"));
904         const char *direction =
905                 ast_json_string_get(ast_json_object_get(obj->blob, "direction"));
906
907         channel_event_string = ast_manager_build_channel_state_string(obj->snapshot);
908
909         if (!channel_event_string) {
910                 return;
911         }
912
913         /*** DOCUMENTATION
914                 <managerEventInstance>
915                         <synopsis>Raised when a DTMF digit has started on a channel.</synopsis>
916                                 <syntax>
917                                         <xi:include xpointer="xpointer(/docs/managerEvent[@name='Newchannel']/managerEventInstance/syntax/parameter)" />
918                                         <parameter name="Digit">
919                                                 <para>DTMF digit received or transmitted (0-9, A-E, # or *</para>
920                                         </parameter>
921                                         <parameter name="Direction">
922                                                 <enumlist>
923                                                         <enum name="Received"/>
924                                                         <enum name="Sent"/>
925                                                 </enumlist>
926                                         </parameter>
927                                 </syntax>
928                 </managerEventInstance>
929         ***/
930         manager_event(EVENT_FLAG_DTMF, "DTMFBegin",
931                 "%s"
932                 "Digit: %s\r\n"
933                 "Direction: %s\r\n",
934                 ast_str_buffer(channel_event_string),
935                 digit, direction);
936 }
937
938 static void channel_dtmf_end_cb(void *data, struct stasis_subscription *sub,
939         struct stasis_topic *topic, struct stasis_message *message)
940 {
941         struct ast_channel_blob *obj = stasis_message_data(message);
942         RAII_VAR(struct ast_str *, channel_event_string, NULL, ast_free);
943         const char *digit =
944                 ast_json_string_get(ast_json_object_get(obj->blob, "digit"));
945         const char *direction =
946                 ast_json_string_get(ast_json_object_get(obj->blob, "direction"));
947         long duration_ms =
948                 ast_json_integer_get(ast_json_object_get(obj->blob, "duration_ms"));
949
950         channel_event_string = ast_manager_build_channel_state_string(obj->snapshot);
951
952         if (!channel_event_string) {
953                 return;
954         }
955
956         /*** DOCUMENTATION
957                 <managerEventInstance>
958                         <synopsis>Raised when a DTMF digit has ended on a channel.</synopsis>
959                                 <syntax>
960                                         <xi:include xpointer="xpointer(/docs/managerEvent[@name='Newchannel']/managerEventInstance/syntax/parameter)" />
961                                         <parameter name="Digit">
962                                                 <para>DTMF digit received or transmitted (0-9, A-E, # or *</para>
963                                         </parameter>
964                                         <parameter name="DurationMs">
965                                                 <para>Duration (in milliseconds) DTMF was sent/received</para>
966                                         </parameter>
967                                         <parameter name="Direction">
968                                                 <enumlist>
969                                                         <enum name="Received"/>
970                                                         <enum name="Sent"/>
971                                                 </enumlist>
972                                         </parameter>
973                                 </syntax>
974                 </managerEventInstance>
975         ***/
976         manager_event(EVENT_FLAG_DTMF, "DTMFEnd",
977                 "%s"
978                 "Digit: %s\r\n"
979                 "DurationMs: %ld\r\n"
980                 "Direction: %s\r\n",
981                 ast_str_buffer(channel_event_string),
982                 digit, duration_ms, direction);
983 }
984
985 static void channel_hangup_handler_cb(void *data, struct stasis_subscription *sub,
986                 struct stasis_topic *topic, struct stasis_message *message)
987 {
988         RAII_VAR(struct ast_str *, channel_event_string, NULL, ast_free);
989         struct ast_channel_blob *payload = stasis_message_data(message);
990         const char *action = ast_json_string_get(ast_json_object_get(payload->blob, "type"));
991         const char *handler = ast_json_string_get(ast_json_object_get(payload->blob, "handler"));
992         const char *event;
993
994         channel_event_string = ast_manager_build_channel_state_string(payload->snapshot);
995
996         if (!channel_event_string) {
997                 return;
998         }
999
1000         if (!strcmp(action, "type")) {
1001                 event = "HangupHandlerRun";
1002         } else if (!strcmp(action, "type")) {
1003                 event = "HangupHandlerPop";
1004         } else if (!strcmp(action, "type")) {
1005                 event = "HangupHandlerPush";
1006         } else {
1007                 return;
1008         }
1009         manager_event(EVENT_FLAG_DIALPLAN, event,
1010                 "%s"
1011                 "Handler: %s\r\n",
1012                 ast_str_buffer(channel_event_string),
1013                 handler);
1014 }
1015
1016 static void channel_fax_cb(void *data, struct stasis_subscription *sub,
1017                 struct stasis_topic *topic, struct stasis_message *message)
1018 {
1019         RAII_VAR(struct ast_str *, channel_event_string, NULL, ast_free);
1020         RAII_VAR(struct ast_str *, event_buffer, ast_str_create(256), ast_free);
1021         struct ast_channel_blob *payload = stasis_message_data(message);
1022         const char *type = ast_json_string_get(ast_json_object_get(payload->blob, "type"));
1023         struct ast_json *operation = ast_json_object_get(payload->blob, "operation");
1024         struct ast_json *status = ast_json_object_get(payload->blob, "status");
1025         struct ast_json *local_station_id = ast_json_object_get(payload->blob, "local_station_id");
1026         struct ast_json *remote_station_id = ast_json_object_get(payload->blob, "remote_station_id");
1027         struct ast_json *fax_pages = ast_json_object_get(payload->blob, "fax_pages");
1028         struct ast_json *fax_resolution = ast_json_object_get(payload->blob, "fax_resolution");
1029         struct ast_json *fax_bitrate = ast_json_object_get(payload->blob, "fax_bitrate");
1030         struct ast_json *filenames = ast_json_object_get(payload->blob, "filenames");
1031         const char *event;
1032         size_t array_len;
1033         size_t i;
1034
1035         if (!event_buffer) {
1036                 return;
1037         }
1038
1039         channel_event_string = ast_manager_build_channel_state_string(payload->snapshot);
1040         if (!channel_event_string) {
1041                 return;
1042         }
1043
1044         if (!strcmp(type, "status")) {
1045                 event = "FAXStatus";
1046         } else if (!strcmp(type, "receive")) {
1047                 event = "ReceiveFAX";
1048         } else if (!strcmp(type, "send")) {
1049                 event = "SendFAX";
1050         } else {
1051                 return;
1052         }
1053
1054         if (operation) {
1055                 ast_str_append(&event_buffer, 0, "Operation: %s\r\n", ast_json_string_get(operation));
1056         }
1057         if (status) {
1058                 ast_str_append(&event_buffer, 0, "Status: %s\r\n", ast_json_string_get(status));
1059         }
1060         if (local_station_id) {
1061                 ast_str_append(&event_buffer, 0, "LocalStationID: %s\r\n", ast_json_string_get(local_station_id));
1062         }
1063         if (remote_station_id) {
1064                 ast_str_append(&event_buffer, 0, "RemoteStationID: %s\r\n", ast_json_string_get(remote_station_id));
1065         }
1066         if (fax_pages) {
1067                 ast_str_append(&event_buffer, 0, "PagesTransferred: %s\r\n", ast_json_string_get(fax_pages));
1068         }
1069         if (fax_resolution) {
1070                 ast_str_append(&event_buffer, 0, "Resolution: %s\r\n", ast_json_string_get(fax_resolution));
1071         }
1072         if (fax_bitrate) {
1073                 ast_str_append(&event_buffer, 0, "TransferRate: %s\r\n", ast_json_string_get(fax_bitrate));
1074         }
1075         if (filenames) {
1076                 array_len = ast_json_array_size(filenames);
1077                 for (i = 0; i < array_len; i++) {
1078                         ast_str_append(&event_buffer, 0, "FileName: %s\r\n", ast_json_string_get(ast_json_array_get(filenames, i)));
1079                 }
1080         }
1081
1082         manager_event(EVENT_FLAG_CALL, event,
1083                 "%s"
1084                 "%s",
1085                 ast_str_buffer(channel_event_string),
1086                 ast_str_buffer(event_buffer));
1087 }
1088
1089 static void channel_moh_start_cb(void *data, struct stasis_subscription *sub,
1090                 struct stasis_topic *topic, struct stasis_message *message)
1091 {
1092         struct ast_channel_blob *payload = stasis_message_data(message);
1093         struct ast_json *blob = payload->blob;
1094         RAII_VAR(struct ast_str *, channel_event_string, NULL, ast_free);
1095
1096         channel_event_string = ast_manager_build_channel_state_string(payload->snapshot);
1097         if (!channel_event_string) {
1098                 return;
1099         }
1100
1101         manager_event(EVENT_FLAG_CALL, "MusicOnHoldStart",
1102                 "%s"
1103                 "Class: %s\r\n",
1104                 ast_str_buffer(channel_event_string),
1105                 ast_json_string_get(ast_json_object_get(blob, "class")));
1106
1107 }
1108
1109 static void channel_moh_stop_cb(void *data, struct stasis_subscription *sub,
1110                 struct stasis_topic *topic, struct stasis_message *message)
1111 {
1112         struct ast_channel_blob *payload = stasis_message_data(message);
1113
1114         publish_basic_channel_event("MusicOnHoldStop", EVENT_FLAG_CALL, payload->snapshot);
1115 }
1116
1117 static void channel_monitor_start_cb(void *data, struct stasis_subscription *sub,
1118                 struct stasis_topic *topic, struct stasis_message *message)
1119 {
1120         struct ast_channel_blob *payload = stasis_message_data(message);
1121
1122         publish_basic_channel_event("MonitorStart", EVENT_FLAG_CALL, payload->snapshot);
1123 }
1124
1125 static void channel_monitor_stop_cb(void *data, struct stasis_subscription *sub,
1126                 struct stasis_topic *topic, struct stasis_message *message)
1127 {
1128         struct ast_channel_blob *payload = stasis_message_data(message);
1129
1130         publish_basic_channel_event("MonitorStop", EVENT_FLAG_CALL, payload->snapshot);
1131 }
1132
1133 /*!
1134  * \brief Callback processing messages for channel dialing
1135  */
1136 static void channel_dial_cb(void *data, struct stasis_subscription *sub,
1137         struct stasis_topic *topic, struct stasis_message *message)
1138 {
1139         struct ast_multi_channel_blob *obj = stasis_message_data(message);
1140         const char *dialstatus;
1141         const char *dialstring;
1142         struct ast_channel_snapshot *caller;
1143         struct ast_channel_snapshot *peer;
1144         RAII_VAR(struct ast_str *, caller_event_string, NULL, ast_free);
1145         RAII_VAR(struct ast_str *, peer_event_string, NULL, ast_free);
1146
1147         caller = ast_multi_channel_blob_get_channel(obj, "caller");
1148         peer = ast_multi_channel_blob_get_channel(obj, "peer");
1149
1150         /* Peer is required - otherwise, who are we dialing? */
1151         ast_assert(peer != NULL);
1152         peer_event_string = ast_manager_build_channel_state_string_prefix(peer, "Dest");
1153         if (!peer_event_string) {
1154                 return;
1155         }
1156
1157         if (caller && !(caller_event_string = ast_manager_build_channel_state_string(caller))) {
1158                 return;
1159         }
1160
1161         dialstatus = ast_json_string_get(ast_json_object_get(ast_multi_channel_blob_get_json(obj), "dialstatus"));
1162         dialstring = ast_json_string_get(ast_json_object_get(ast_multi_channel_blob_get_json(obj), "dialstring"));
1163         if (ast_strlen_zero(dialstatus)) {
1164                 manager_event(EVENT_FLAG_CALL, "DialBegin",
1165                                 "%s"
1166                                 "%s"
1167                                 "DialString: %s\r\n",
1168                                 caller_event_string ? ast_str_buffer(caller_event_string) : "",
1169                                 ast_str_buffer(peer_event_string),
1170                                 S_OR(dialstring, "unknown"));
1171         } else {
1172                 manager_event(EVENT_FLAG_CALL, "DialEnd",
1173                                 "%s"
1174                                 "%s"
1175                                 "DialStatus: %s\r\n",
1176                                 caller_event_string ? ast_str_buffer(caller_event_string) : "",
1177                                 ast_str_buffer(peer_event_string),
1178                                 S_OR(dialstatus, "unknown"));
1179         }
1180
1181 }
1182
1183 static void manager_channels_shutdown(void)
1184 {
1185         stasis_unsubscribe(topic_forwarder);
1186         topic_forwarder = NULL;
1187 }
1188
1189 int manager_channels_init(void)
1190 {
1191         int ret = 0;
1192         struct stasis_topic *manager_topic;
1193         struct stasis_topic *channel_topic;
1194         struct stasis_message_router *message_router;
1195
1196         manager_topic = ast_manager_get_topic();
1197         if (!manager_topic) {
1198                 return -1;
1199         }
1200         message_router = ast_manager_get_message_router();
1201         if (!message_router) {
1202                 return -1;
1203         }
1204         channel_topic = stasis_caching_get_topic(ast_channel_topic_all_cached());
1205         if (!channel_topic) {
1206                 return -1;
1207         }
1208
1209         topic_forwarder = stasis_forward_all(channel_topic, manager_topic);
1210         if (!topic_forwarder) {
1211                 return -1;
1212         }
1213
1214         ast_register_atexit(manager_channels_shutdown);
1215
1216         ret |= stasis_message_router_add(message_router,
1217                                          stasis_cache_update_type(),
1218                                          channel_snapshot_update,
1219                                          NULL);
1220
1221         ret |= stasis_message_router_add(message_router,
1222                                          ast_channel_varset_type(),
1223                                          channel_varset_cb,
1224                                          NULL);
1225
1226         ret |= stasis_message_router_add(message_router,
1227                                          ast_channel_user_event_type(),
1228                                          channel_user_event_cb,
1229                                          NULL);
1230
1231         ret |= stasis_message_router_add(message_router,
1232                                          ast_channel_dtmf_begin_type(),
1233                                          channel_dtmf_begin_cb,
1234                                          NULL);
1235
1236         ret |= stasis_message_router_add(message_router,
1237                                          ast_channel_dtmf_end_type(),
1238                                          channel_dtmf_end_cb,
1239                                          NULL);
1240
1241         ret |= stasis_message_router_add(message_router,
1242                                          ast_channel_hangup_request_type(),
1243                                          channel_hangup_request_cb,
1244                                          NULL);
1245
1246         ret |= stasis_message_router_add(message_router,
1247                                          ast_channel_dial_type(),
1248                                          channel_dial_cb,
1249                                          NULL);
1250
1251         ret |= stasis_message_router_add(message_router,
1252                                          ast_channel_fax_type(),
1253                                          channel_fax_cb,
1254                                          NULL);
1255
1256         ret |= stasis_message_router_add(message_router,
1257                                          ast_channel_chanspy_start_type(),
1258                                          channel_chanspy_start_cb,
1259                                          NULL);
1260
1261         ret |= stasis_message_router_add(message_router,
1262                                          ast_channel_chanspy_stop_type(),
1263                                          channel_chanspy_stop_cb,
1264                                          NULL);
1265
1266         ret |= stasis_message_router_add(message_router,
1267                                          ast_channel_hangup_handler_type(),
1268                                          channel_hangup_handler_cb,
1269                                          NULL);
1270
1271         ret |= stasis_message_router_add(message_router,
1272                                          ast_channel_moh_start_type(),
1273                                          channel_moh_start_cb,
1274                                          NULL);
1275
1276         ret |= stasis_message_router_add(message_router,
1277                                          ast_channel_moh_stop_type(),
1278                                          channel_moh_stop_cb,
1279                                          NULL);
1280
1281         ret |= stasis_message_router_add(message_router,
1282                                          ast_channel_monitor_start_type(),
1283                                          channel_monitor_start_cb,
1284                                          NULL);
1285
1286         ret |= stasis_message_router_add(message_router,
1287                                          ast_channel_monitor_stop_type(),
1288                                          channel_monitor_stop_cb,
1289                                          NULL);
1290
1291         /* If somehow we failed to add any routes, just shut down the whole
1292          * thing and fail it.
1293          */
1294         if (ret) {
1295                 manager_channels_shutdown();
1296                 return -1;
1297         }
1298
1299         return 0;
1300 }
1301