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