5ae35b21d63eaa4af0dabd58997120ac29b69121
[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_ZOMBIE) ? 1 : 0;
607         is_hungup = ast_test_flag(&new_snapshot->flags, AST_FLAG_ZOMBIE) ? 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         if (old_snapshot && ast_channel_snapshot_cep_equal(old_snapshot, new_snapshot)
642                 && !strcmp(old_snapshot->appl, new_snapshot->appl)) {
643                 return NULL;
644         }
645
646         /* DEPRECATED: Extension field deprecated in 12; remove in 14 */
647         return ast_manager_event_blob_create(
648                 EVENT_FLAG_CALL, "Newexten",
649                 "Extension: %s\r\n"
650                 "Application: %s\r\n"
651                 "AppData: %s\r\n",
652                 new_snapshot->exten,
653                 new_snapshot->appl,
654                 new_snapshot->data);
655 }
656
657 static struct ast_manager_event_blob *channel_new_callerid(
658         struct ast_channel_snapshot *old_snapshot,
659         struct ast_channel_snapshot *new_snapshot)
660 {
661         /* No NewCallerid event on cache clear or first event */
662         if (!old_snapshot || !new_snapshot) {
663                 return NULL;
664         }
665
666         if (ast_channel_snapshot_caller_id_equal(old_snapshot, new_snapshot)) {
667                 return NULL;
668         }
669
670         return ast_manager_event_blob_create(
671                 EVENT_FLAG_CALL, "NewCallerid",
672                 "CID-CallingPres: %d (%s)\r\n",
673                 new_snapshot->caller_pres,
674                 ast_describe_caller_presentation(new_snapshot->caller_pres));
675 }
676
677 static struct ast_manager_event_blob *channel_new_accountcode(
678         struct ast_channel_snapshot *old_snapshot,
679         struct ast_channel_snapshot *new_snapshot)
680 {
681         if (!old_snapshot || !new_snapshot) {
682                 return NULL;
683         }
684
685         if (!strcmp(old_snapshot->accountcode, new_snapshot->accountcode)) {
686                 return NULL;
687         }
688
689         return ast_manager_event_blob_create(
690                 EVENT_FLAG_CALL, "NewAccountCode",
691                 "OldAccountCode: %s\r\n", old_snapshot->accountcode);
692 }
693
694 channel_snapshot_monitor channel_monitors[] = {
695         channel_state_change,
696         channel_newexten,
697         channel_new_callerid,
698         channel_new_accountcode
699 };
700
701 static void channel_snapshot_update(void *data, struct stasis_subscription *sub,
702                                     struct stasis_topic *topic,
703                                     struct stasis_message *message)
704 {
705         RAII_VAR(struct ast_str *, channel_event_string, NULL, ast_free);
706         struct stasis_cache_update *update;
707         struct ast_channel_snapshot *old_snapshot;
708         struct ast_channel_snapshot *new_snapshot;
709         size_t i;
710
711         update = stasis_message_data(message);
712
713         if (ast_channel_snapshot_type() != update->type) {
714                 return;
715         }
716
717         old_snapshot = stasis_message_data(update->old_snapshot);
718         new_snapshot = stasis_message_data(update->new_snapshot);
719
720         for (i = 0; i < ARRAY_LEN(channel_monitors); ++i) {
721                 RAII_VAR(struct ast_manager_event_blob *, ev, NULL, ao2_cleanup);
722                 ev = channel_monitors[i](old_snapshot, new_snapshot);
723
724                 if (!ev) {
725                         continue;
726                 }
727
728                 /* If we haven't already, build the channel event string */
729                 if (!channel_event_string) {
730                         channel_event_string =
731                                 ast_manager_build_channel_state_string(new_snapshot);
732                         if (!channel_event_string) {
733                                 return;
734                         }
735                 }
736
737                 manager_event(ev->event_flags, ev->manager_event, "%s%s",
738                         ast_str_buffer(channel_event_string),
739                         ev->extra_fields);
740         }
741 }
742
743 static int userevent_exclusion_cb(const char *key)
744 {
745         if (!strcmp("type", key)) {
746                 return 1;
747         }
748         if (!strcmp("eventname", key)) {
749                 return 1;
750         }
751         return 0;
752 }
753
754 static void channel_user_event_cb(void *data, struct stasis_subscription *sub,
755         struct stasis_topic *topic, struct stasis_message *message)
756 {
757         struct ast_channel_blob *obj = stasis_message_data(message);
758         RAII_VAR(struct ast_str *, channel_event_string, NULL, ast_free);
759         RAII_VAR(struct ast_str *, body, NULL, ast_free);
760         const char *eventname;
761
762         eventname = ast_json_string_get(ast_json_object_get(obj->blob, "eventname"));
763         body = ast_manager_str_from_json_object(obj->blob, userevent_exclusion_cb);
764         channel_event_string = ast_manager_build_channel_state_string(obj->snapshot);
765
766         if (!channel_event_string || !body) {
767                 return;
768         }
769
770         /*** DOCUMENTATION
771                 <managerEventInstance>
772                         <synopsis>A user defined event raised from the dialplan.</synopsis>
773                         <syntax>
774                                 <xi:include xpointer="xpointer(/docs/managerEvent[@name='Newchannel']/managerEventInstance/syntax/parameter)" />
775                                 <parameter name="UserEvent">
776                                         <para>The event name, as specified in the dialplan.</para>
777                                 </parameter>
778                         </syntax>
779                         <see-also>
780                                 <ref type="application">UserEvent</ref>
781                         </see-also>
782                 </managerEventInstance>
783         ***/
784         manager_event(EVENT_FLAG_USER, "UserEvent",
785                       "%s"
786                       "UserEvent: %s\r\n"
787                       "%s",
788                       ast_str_buffer(channel_event_string), eventname, ast_str_buffer(body));
789 }
790
791 static void publish_basic_channel_event(const char *event, int class, struct ast_channel_snapshot *snapshot)
792 {
793         RAII_VAR(struct ast_str *, channel_event_string, NULL, ast_free);
794
795         channel_event_string = ast_manager_build_channel_state_string(snapshot);
796         if (!channel_event_string) {
797                 return;
798         }
799
800         manager_event(class, event,
801                 "%s",
802                 ast_str_buffer(channel_event_string));
803 }
804
805 static void channel_hangup_request_cb(void *data,
806         struct stasis_subscription *sub, struct stasis_topic *topic,
807         struct stasis_message *message)
808 {
809         struct ast_channel_blob *obj = stasis_message_data(message);
810         RAII_VAR(struct ast_str *, extra, NULL, ast_free);
811         RAII_VAR(struct ast_str *, channel_event_string, NULL, ast_free);
812         struct ast_json *cause;
813         int is_soft;
814         char *manager_event = "HangupRequest";
815
816         extra = ast_str_create(20);
817         if (!extra) {
818                 return;
819         }
820
821         channel_event_string = ast_manager_build_channel_state_string(obj->snapshot);
822
823         if (!channel_event_string) {
824                 return;
825         }
826
827         cause = ast_json_object_get(obj->blob, "cause");
828         if (cause) {
829                 ast_str_append(&extra, 0,
830                                "Cause: %jd\r\n",
831                                ast_json_integer_get(cause));
832         }
833
834         is_soft = ast_json_is_true(ast_json_object_get(obj->blob, "soft"));
835         if (is_soft) {
836                 manager_event = "SoftHangupRequest";
837         }
838
839         manager_event(EVENT_FLAG_CALL, manager_event,
840                       "%s%s",
841                       ast_str_buffer(channel_event_string),
842                       ast_str_buffer(extra));
843 }
844
845 static void channel_chanspy_stop_cb(void *data, struct stasis_subscription *sub,
846                 struct stasis_topic *topic, struct stasis_message *message)
847 {
848         RAII_VAR(struct ast_str *, spyer_channel_string, NULL, ast_free);
849         struct ast_channel_snapshot *spyer;
850         struct ast_multi_channel_blob *payload = stasis_message_data(message);
851
852         spyer = ast_multi_channel_blob_get_channel(payload, "spyer_channel");
853         if (!spyer) {
854                 ast_log(AST_LOG_WARNING, "Received ChanSpy Start event with no spyer 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
863         manager_event(EVENT_FLAG_CALL, "ChanSpyStop",
864                       "%s",
865                       ast_str_buffer(spyer_channel_string));
866 }
867
868 static void channel_chanspy_start_cb(void *data, struct stasis_subscription *sub,
869                 struct stasis_topic *topic, struct stasis_message *message)
870 {
871         RAII_VAR(struct ast_str *, spyer_channel_string, NULL, ast_free);
872         RAII_VAR(struct ast_str *, spyee_channel_string, NULL, ast_free);
873         struct ast_channel_snapshot *spyer;
874         struct ast_channel_snapshot *spyee;
875         struct ast_multi_channel_blob *payload = stasis_message_data(message);
876
877         spyer = ast_multi_channel_blob_get_channel(payload, "spyer_channel");
878         if (!spyer) {
879                 ast_log(AST_LOG_WARNING, "Received ChanSpy Start event with no spyer channel!\n");
880                 return;
881         }
882         spyee = ast_multi_channel_blob_get_channel(payload, "spyee_channel");
883         if (!spyee) {
884                 ast_log(AST_LOG_WARNING, "Received ChanSpy Start event with no spyee channel!\n");
885                 return;
886         }
887
888         spyer_channel_string = ast_manager_build_channel_state_string_prefix(spyer, "Spyer");
889         if (!spyer_channel_string) {
890                 return;
891         }
892         spyee_channel_string = ast_manager_build_channel_state_string_prefix(spyee, "Spyee");
893         if (!spyee_channel_string) {
894                 return;
895         }
896
897         manager_event(EVENT_FLAG_CALL, "ChanSpyStart",
898                       "%s%s",
899                       ast_str_buffer(spyer_channel_string),
900                       ast_str_buffer(spyee_channel_string));
901 }
902
903 static void channel_dtmf_begin_cb(void *data, struct stasis_subscription *sub,
904         struct stasis_topic *topic, struct stasis_message *message)
905 {
906         struct ast_channel_blob *obj = stasis_message_data(message);
907         RAII_VAR(struct ast_str *, channel_event_string, NULL, ast_free);
908         const char *digit =
909                 ast_json_string_get(ast_json_object_get(obj->blob, "digit"));
910         const char *direction =
911                 ast_json_string_get(ast_json_object_get(obj->blob, "direction"));
912
913         channel_event_string = ast_manager_build_channel_state_string(obj->snapshot);
914
915         if (!channel_event_string) {
916                 return;
917         }
918
919         /*** DOCUMENTATION
920                 <managerEventInstance>
921                         <synopsis>Raised when a DTMF digit has started on a channel.</synopsis>
922                                 <syntax>
923                                         <xi:include xpointer="xpointer(/docs/managerEvent[@name='Newchannel']/managerEventInstance/syntax/parameter)" />
924                                         <parameter name="Digit">
925                                                 <para>DTMF digit received or transmitted (0-9, A-E, # or *</para>
926                                         </parameter>
927                                         <parameter name="Direction">
928                                                 <enumlist>
929                                                         <enum name="Received"/>
930                                                         <enum name="Sent"/>
931                                                 </enumlist>
932                                         </parameter>
933                                 </syntax>
934                 </managerEventInstance>
935         ***/
936         manager_event(EVENT_FLAG_DTMF, "DTMFBegin",
937                 "%s"
938                 "Digit: %s\r\n"
939                 "Direction: %s\r\n",
940                 ast_str_buffer(channel_event_string),
941                 digit, direction);
942 }
943
944 static void channel_dtmf_end_cb(void *data, struct stasis_subscription *sub,
945         struct stasis_topic *topic, struct stasis_message *message)
946 {
947         struct ast_channel_blob *obj = stasis_message_data(message);
948         RAII_VAR(struct ast_str *, channel_event_string, NULL, ast_free);
949         const char *digit =
950                 ast_json_string_get(ast_json_object_get(obj->blob, "digit"));
951         const char *direction =
952                 ast_json_string_get(ast_json_object_get(obj->blob, "direction"));
953         long duration_ms =
954                 ast_json_integer_get(ast_json_object_get(obj->blob, "duration_ms"));
955
956         channel_event_string = ast_manager_build_channel_state_string(obj->snapshot);
957
958         if (!channel_event_string) {
959                 return;
960         }
961
962         /*** DOCUMENTATION
963                 <managerEventInstance>
964                         <synopsis>Raised when a DTMF digit has ended on a channel.</synopsis>
965                                 <syntax>
966                                         <xi:include xpointer="xpointer(/docs/managerEvent[@name='Newchannel']/managerEventInstance/syntax/parameter)" />
967                                         <parameter name="Digit">
968                                                 <para>DTMF digit received or transmitted (0-9, A-E, # or *</para>
969                                         </parameter>
970                                         <parameter name="DurationMs">
971                                                 <para>Duration (in milliseconds) DTMF was sent/received</para>
972                                         </parameter>
973                                         <parameter name="Direction">
974                                                 <enumlist>
975                                                         <enum name="Received"/>
976                                                         <enum name="Sent"/>
977                                                 </enumlist>
978                                         </parameter>
979                                 </syntax>
980                 </managerEventInstance>
981         ***/
982         manager_event(EVENT_FLAG_DTMF, "DTMFEnd",
983                 "%s"
984                 "Digit: %s\r\n"
985                 "DurationMs: %ld\r\n"
986                 "Direction: %s\r\n",
987                 ast_str_buffer(channel_event_string),
988                 digit, duration_ms, direction);
989 }
990
991 static void channel_hangup_handler_cb(void *data, struct stasis_subscription *sub,
992                 struct stasis_topic *topic, struct stasis_message *message)
993 {
994         RAII_VAR(struct ast_str *, channel_event_string, NULL, ast_free);
995         struct ast_channel_blob *payload = stasis_message_data(message);
996         const char *action = ast_json_string_get(ast_json_object_get(payload->blob, "type"));
997         const char *handler = ast_json_string_get(ast_json_object_get(payload->blob, "handler"));
998         const char *event;
999
1000         channel_event_string = ast_manager_build_channel_state_string(payload->snapshot);
1001
1002         if (!channel_event_string) {
1003                 return;
1004         }
1005
1006         if (!strcmp(action, "type")) {
1007                 event = "HangupHandlerRun";
1008         } else if (!strcmp(action, "type")) {
1009                 event = "HangupHandlerPop";
1010         } else if (!strcmp(action, "type")) {
1011                 event = "HangupHandlerPush";
1012         } else {
1013                 return;
1014         }
1015         manager_event(EVENT_FLAG_DIALPLAN, event,
1016                 "%s"
1017                 "Handler: %s\r\n",
1018                 ast_str_buffer(channel_event_string),
1019                 handler);
1020 }
1021
1022 static void channel_fax_cb(void *data, struct stasis_subscription *sub,
1023                 struct stasis_topic *topic, struct stasis_message *message)
1024 {
1025         RAII_VAR(struct ast_str *, channel_event_string, NULL, ast_free);
1026         RAII_VAR(struct ast_str *, event_buffer, ast_str_create(256), ast_free);
1027         struct ast_channel_blob *payload = stasis_message_data(message);
1028         const char *type = ast_json_string_get(ast_json_object_get(payload->blob, "type"));
1029         struct ast_json *operation = ast_json_object_get(payload->blob, "operation");
1030         struct ast_json *status = ast_json_object_get(payload->blob, "status");
1031         struct ast_json *local_station_id = ast_json_object_get(payload->blob, "local_station_id");
1032         struct ast_json *remote_station_id = ast_json_object_get(payload->blob, "remote_station_id");
1033         struct ast_json *fax_pages = ast_json_object_get(payload->blob, "fax_pages");
1034         struct ast_json *fax_resolution = ast_json_object_get(payload->blob, "fax_resolution");
1035         struct ast_json *fax_bitrate = ast_json_object_get(payload->blob, "fax_bitrate");
1036         struct ast_json *filenames = ast_json_object_get(payload->blob, "filenames");
1037         const char *event;
1038         size_t array_len;
1039         size_t i;
1040
1041         if (!event_buffer) {
1042                 return;
1043         }
1044
1045         channel_event_string = ast_manager_build_channel_state_string(payload->snapshot);
1046         if (!channel_event_string) {
1047                 return;
1048         }
1049
1050         if (!strcmp(type, "status")) {
1051                 event = "FAXStatus";
1052         } else if (!strcmp(type, "receive")) {
1053                 event = "ReceiveFAX";
1054         } else if (!strcmp(type, "send")) {
1055                 event = "SendFAX";
1056         } else {
1057                 return;
1058         }
1059
1060         if (operation) {
1061                 ast_str_append(&event_buffer, 0, "Operation: %s\r\n", ast_json_string_get(operation));
1062         }
1063         if (status) {
1064                 ast_str_append(&event_buffer, 0, "Status: %s\r\n", ast_json_string_get(status));
1065         }
1066         if (local_station_id) {
1067                 ast_str_append(&event_buffer, 0, "LocalStationID: %s\r\n", ast_json_string_get(local_station_id));
1068         }
1069         if (remote_station_id) {
1070                 ast_str_append(&event_buffer, 0, "RemoteStationID: %s\r\n", ast_json_string_get(remote_station_id));
1071         }
1072         if (fax_pages) {
1073                 ast_str_append(&event_buffer, 0, "PagesTransferred: %s\r\n", ast_json_string_get(fax_pages));
1074         }
1075         if (fax_resolution) {
1076                 ast_str_append(&event_buffer, 0, "Resolution: %s\r\n", ast_json_string_get(fax_resolution));
1077         }
1078         if (fax_bitrate) {
1079                 ast_str_append(&event_buffer, 0, "TransferRate: %s\r\n", ast_json_string_get(fax_bitrate));
1080         }
1081         if (filenames) {
1082                 array_len = ast_json_array_size(filenames);
1083                 for (i = 0; i < array_len; i++) {
1084                         ast_str_append(&event_buffer, 0, "FileName: %s\r\n", ast_json_string_get(ast_json_array_get(filenames, i)));
1085                 }
1086         }
1087
1088         manager_event(EVENT_FLAG_CALL, event,
1089                 "%s"
1090                 "%s",
1091                 ast_str_buffer(channel_event_string),
1092                 ast_str_buffer(event_buffer));
1093 }
1094
1095 static void channel_moh_start_cb(void *data, struct stasis_subscription *sub,
1096                 struct stasis_topic *topic, struct stasis_message *message)
1097 {
1098         struct ast_channel_blob *payload = stasis_message_data(message);
1099         struct ast_json *blob = payload->blob;
1100         RAII_VAR(struct ast_str *, channel_event_string, NULL, ast_free);
1101
1102         channel_event_string = ast_manager_build_channel_state_string(payload->snapshot);
1103         if (!channel_event_string) {
1104                 return;
1105         }
1106
1107         manager_event(EVENT_FLAG_CALL, "MusicOnHoldStart",
1108                 "%s"
1109                 "Class: %s\r\n",
1110                 ast_str_buffer(channel_event_string),
1111                 ast_json_string_get(ast_json_object_get(blob, "class")));
1112
1113 }
1114
1115 static void channel_moh_stop_cb(void *data, struct stasis_subscription *sub,
1116                 struct stasis_topic *topic, struct stasis_message *message)
1117 {
1118         struct ast_channel_blob *payload = stasis_message_data(message);
1119
1120         publish_basic_channel_event("MusicOnHoldStop", EVENT_FLAG_CALL, payload->snapshot);
1121 }
1122
1123 static void channel_monitor_start_cb(void *data, struct stasis_subscription *sub,
1124                 struct stasis_topic *topic, struct stasis_message *message)
1125 {
1126         struct ast_channel_blob *payload = stasis_message_data(message);
1127
1128         publish_basic_channel_event("MonitorStart", EVENT_FLAG_CALL, payload->snapshot);
1129 }
1130
1131 static void channel_monitor_stop_cb(void *data, struct stasis_subscription *sub,
1132                 struct stasis_topic *topic, struct stasis_message *message)
1133 {
1134         struct ast_channel_blob *payload = stasis_message_data(message);
1135
1136         publish_basic_channel_event("MonitorStop", EVENT_FLAG_CALL, payload->snapshot);
1137 }
1138
1139 /*!
1140  * \brief Callback processing messages for channel dialing
1141  */
1142 static void channel_dial_cb(void *data, struct stasis_subscription *sub,
1143         struct stasis_topic *topic, struct stasis_message *message)
1144 {
1145         struct ast_multi_channel_blob *obj = stasis_message_data(message);
1146         const char *dialstatus;
1147         const char *dialstring;
1148         struct ast_channel_snapshot *caller;
1149         struct ast_channel_snapshot *peer;
1150         RAII_VAR(struct ast_str *, caller_event_string, NULL, ast_free);
1151         RAII_VAR(struct ast_str *, peer_event_string, NULL, ast_free);
1152
1153         caller = ast_multi_channel_blob_get_channel(obj, "caller");
1154         peer = ast_multi_channel_blob_get_channel(obj, "peer");
1155
1156         /* Peer is required - otherwise, who are we dialing? */
1157         ast_assert(peer != NULL);
1158         peer_event_string = ast_manager_build_channel_state_string_prefix(peer, "Dest");
1159         if (!peer_event_string) {
1160                 return;
1161         }
1162
1163         if (caller && !(caller_event_string = ast_manager_build_channel_state_string(caller))) {
1164                 return;
1165         }
1166
1167         dialstatus = ast_json_string_get(ast_json_object_get(ast_multi_channel_blob_get_json(obj), "dialstatus"));
1168         dialstring = ast_json_string_get(ast_json_object_get(ast_multi_channel_blob_get_json(obj), "dialstring"));
1169         if (ast_strlen_zero(dialstatus)) {
1170                 manager_event(EVENT_FLAG_CALL, "DialBegin",
1171                                 "%s"
1172                                 "%s"
1173                                 "DialString: %s\r\n",
1174                                 caller_event_string ? ast_str_buffer(caller_event_string) : "",
1175                                 ast_str_buffer(peer_event_string),
1176                                 S_OR(dialstring, "unknown"));
1177         } else {
1178                 manager_event(EVENT_FLAG_CALL, "DialEnd",
1179                                 "%s"
1180                                 "%s"
1181                                 "DialStatus: %s\r\n",
1182                                 caller_event_string ? ast_str_buffer(caller_event_string) : "",
1183                                 ast_str_buffer(peer_event_string),
1184                                 S_OR(dialstatus, "unknown"));
1185         }
1186
1187 }
1188
1189 static void channel_hold_cb(void *data, struct stasis_subscription *sub,
1190         struct stasis_topic *topic, struct stasis_message *message)
1191 {
1192         struct ast_channel_blob *obj = stasis_message_data(message);
1193         const char *musicclass;
1194         RAII_VAR(struct ast_str *, musicclass_string, NULL, ast_free);
1195         RAII_VAR(struct ast_str *, channel_event_string, NULL, ast_free);
1196
1197         if (!(musicclass_string = ast_str_create(32))) {
1198                 return;
1199         }
1200
1201         channel_event_string = ast_manager_build_channel_state_string(obj->snapshot);
1202
1203         if (obj->blob) {
1204                 musicclass = ast_json_string_get(ast_json_object_get(obj->blob, "musicclass"));
1205
1206                 if (!ast_strlen_zero(musicclass)) {
1207                         ast_str_set(&musicclass_string, 0, "MusicClass: %s\r\n", musicclass);
1208                 }
1209         }
1210
1211         manager_event(EVENT_FLAG_CALL, "Hold",
1212                 "%s"
1213                 "%s",
1214                 ast_str_buffer(channel_event_string),
1215                 ast_str_buffer(musicclass_string));
1216 }
1217
1218 static void channel_unhold_cb(void *data, struct stasis_subscription *sub,
1219         struct stasis_topic *topic, struct stasis_message *message)
1220 {
1221         struct ast_channel_blob *obj = stasis_message_data(message);
1222         RAII_VAR(struct ast_str *, channel_event_string, NULL, ast_free);
1223
1224         channel_event_string = ast_manager_build_channel_state_string(obj->snapshot);
1225
1226         manager_event(EVENT_FLAG_CALL, "Unhold",
1227                 "%s",
1228                 ast_str_buffer(channel_event_string));
1229 }
1230
1231 static void manager_channels_shutdown(void)
1232 {
1233         stasis_unsubscribe(topic_forwarder);
1234         topic_forwarder = NULL;
1235 }
1236
1237 int manager_channels_init(void)
1238 {
1239         int ret = 0;
1240         struct stasis_topic *manager_topic;
1241         struct stasis_topic *channel_topic;
1242         struct stasis_message_router *message_router;
1243
1244         manager_topic = ast_manager_get_topic();
1245         if (!manager_topic) {
1246                 return -1;
1247         }
1248         message_router = ast_manager_get_message_router();
1249         if (!message_router) {
1250                 return -1;
1251         }
1252         channel_topic = stasis_caching_get_topic(ast_channel_topic_all_cached());
1253         if (!channel_topic) {
1254                 return -1;
1255         }
1256
1257         topic_forwarder = stasis_forward_all(channel_topic, manager_topic);
1258         if (!topic_forwarder) {
1259                 return -1;
1260         }
1261
1262         ast_register_atexit(manager_channels_shutdown);
1263
1264         ret |= stasis_message_router_add(message_router,
1265                                          stasis_cache_update_type(),
1266                                          channel_snapshot_update,
1267                                          NULL);
1268
1269         ret |= stasis_message_router_add(message_router,
1270                                          ast_channel_user_event_type(),
1271                                          channel_user_event_cb,
1272                                          NULL);
1273
1274         ret |= stasis_message_router_add(message_router,
1275                                          ast_channel_dtmf_begin_type(),
1276                                          channel_dtmf_begin_cb,
1277                                          NULL);
1278
1279         ret |= stasis_message_router_add(message_router,
1280                                          ast_channel_dtmf_end_type(),
1281                                          channel_dtmf_end_cb,
1282                                          NULL);
1283
1284         ret |= stasis_message_router_add(message_router,
1285                                          ast_channel_hangup_request_type(),
1286                                          channel_hangup_request_cb,
1287                                          NULL);
1288
1289         ret |= stasis_message_router_add(message_router,
1290                                          ast_channel_dial_type(),
1291                                          channel_dial_cb,
1292                                          NULL);
1293
1294         ret |= stasis_message_router_add(message_router,
1295                                          ast_channel_hold_type(),
1296                                          channel_hold_cb,
1297                                          NULL);
1298
1299         ret |= stasis_message_router_add(message_router,
1300                                          ast_channel_unhold_type(),
1301                                          channel_unhold_cb,
1302                                          NULL);
1303
1304         ret |= stasis_message_router_add(message_router,
1305                                          ast_channel_fax_type(),
1306                                          channel_fax_cb,
1307                                          NULL);
1308
1309         ret |= stasis_message_router_add(message_router,
1310                                          ast_channel_chanspy_start_type(),
1311                                          channel_chanspy_start_cb,
1312                                          NULL);
1313
1314         ret |= stasis_message_router_add(message_router,
1315                                          ast_channel_chanspy_stop_type(),
1316                                          channel_chanspy_stop_cb,
1317                                          NULL);
1318
1319         ret |= stasis_message_router_add(message_router,
1320                                          ast_channel_hangup_handler_type(),
1321                                          channel_hangup_handler_cb,
1322                                          NULL);
1323
1324         ret |= stasis_message_router_add(message_router,
1325                                          ast_channel_moh_start_type(),
1326                                          channel_moh_start_cb,
1327                                          NULL);
1328
1329         ret |= stasis_message_router_add(message_router,
1330                                          ast_channel_moh_stop_type(),
1331                                          channel_moh_stop_cb,
1332                                          NULL);
1333
1334         ret |= stasis_message_router_add(message_router,
1335                                          ast_channel_monitor_start_type(),
1336                                          channel_monitor_start_cb,
1337                                          NULL);
1338
1339         ret |= stasis_message_router_add(message_router,
1340                                          ast_channel_monitor_stop_type(),
1341                                          channel_monitor_stop_cb,
1342                                          NULL);
1343
1344         /* If somehow we failed to add any routes, just shut down the whole
1345          * thing and fail it.
1346          */
1347         if (ret) {
1348                 manager_channels_shutdown();
1349                 return -1;
1350         }
1351
1352         return 0;
1353 }
1354