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