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