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