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