OneTouchRecord: Make so Monitor/MixMonitor can be toggled/started/stopped.
[asterisk/asterisk.git] / bridges / bridge_builtin_features.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2009, Digium, Inc.
5  *
6  * Joshua Colp <jcolp@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 Built in bridging features
22  *
23  * \author Joshua Colp <jcolp@digium.com>
24  *
25  * \ingroup bridges
26  */
27
28 /*** MODULEINFO
29         <support_level>core</support_level>
30  ***/
31
32 #include "asterisk.h"
33
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <sys/types.h>
40 #include <sys/stat.h>
41
42 #include "asterisk/module.h"
43 #include "asterisk/channel.h"
44 #include "asterisk/bridging.h"
45 #include "asterisk/bridging_technology.h"
46 #include "asterisk/frame.h"
47 #include "asterisk/file.h"
48 #include "asterisk/app.h"
49 #include "asterisk/astobj2.h"
50 #include "asterisk/pbx.h"
51 #include "asterisk/parking.h"
52 #include "asterisk/features_config.h"
53 #include "asterisk/monitor.h"
54 #include "asterisk/mixmonitor.h"
55 #include "asterisk/audiohook.h"
56
57 /*!
58  * \brief Helper function that presents dialtone and grabs extension
59  *
60  * \retval 0 on success
61  * \retval -1 on failure
62  */
63 static int grab_transfer(struct ast_channel *chan, char *exten, size_t exten_len, const char *context)
64 {
65         int res;
66         int digit_timeout;
67         RAII_VAR(struct ast_features_xfer_config *, xfer_cfg, NULL, ao2_cleanup);
68
69         ast_channel_lock(chan);
70         xfer_cfg = ast_get_chan_features_xfer_config(chan);
71         if (!xfer_cfg) {
72                 ast_log(LOG_ERROR, "Unable to get transfer configuration\n");
73                 ast_channel_unlock(chan);
74                 return -1;
75         }
76         digit_timeout = xfer_cfg->transferdigittimeout;
77         ast_channel_unlock(chan);
78
79         /* Play the simple "transfer" prompt out and wait */
80         res = ast_stream_and_wait(chan, "pbx-transfer", AST_DIGIT_ANY);
81         ast_stopstream(chan);
82         if (res < 0) {
83                 /* Hangup or error */
84                 return -1;
85         }
86         if (res) {
87                 /* Store the DTMF digit that interrupted playback of the file. */
88                 exten[0] = res;
89         }
90
91         /* Drop to dialtone so they can enter the extension they want to transfer to */
92         res = ast_app_dtget(chan, context, exten, exten_len, exten_len - 1, digit_timeout);
93         if (res < 0) {
94                 /* Hangup or error */
95                 res = -1;
96         } else if (!res) {
97                 /* 0 for invalid extension dialed. */
98                 if (ast_strlen_zero(exten)) {
99                         ast_debug(1, "%s dialed no digits.\n", ast_channel_name(chan));
100                 } else {
101                         ast_debug(1, "%s dialed '%s@%s' does not exist.\n",
102                                 ast_channel_name(chan), exten, context);
103                 }
104                 ast_stream_and_wait(chan, "pbx-invalid", AST_DIGIT_NONE);
105                 res = -1;
106         } else {
107                 /* Dialed extension is valid. */
108                 res = 0;
109         }
110         return res;
111 }
112
113 static void copy_caller_data(struct ast_channel *dest, struct ast_channel *caller)
114 {
115         ast_channel_lock_both(caller, dest);
116         ast_connected_line_copy_from_caller(ast_channel_connected(dest), ast_channel_caller(caller));
117         ast_channel_inherit_variables(caller, dest);
118         ast_channel_datastore_inherit(caller, dest);
119         ast_channel_unlock(dest);
120         ast_channel_unlock(caller);
121 }
122
123 /*! \brief Helper function that creates an outgoing channel and returns it immediately */
124 static struct ast_channel *dial_transfer(struct ast_channel *caller, const char *exten, const char *context)
125 {
126         char destination[AST_MAX_EXTENSION + AST_MAX_CONTEXT + 1];
127         struct ast_channel *chan;
128         int cause;
129
130         /* Fill the variable with the extension and context we want to call */
131         snprintf(destination, sizeof(destination), "%s@%s", exten, context);
132
133         /* Now we request a local channel to prepare to call the destination */
134         chan = ast_request("Local", ast_channel_nativeformats(caller), caller, destination,
135                 &cause);
136         if (!chan) {
137                 return NULL;
138         }
139
140         /* Who is transferring the call. */
141         pbx_builtin_setvar_helper(chan, "TRANSFERERNAME", ast_channel_name(caller));
142
143         /* Before we actually dial out let's inherit appropriate information. */
144         copy_caller_data(chan, caller);
145
146         /* Since the above worked fine now we actually call it and return the channel */
147         if (ast_call(chan, destination, 0)) {
148                 ast_hangup(chan);
149                 return NULL;
150         }
151
152         return chan;
153 }
154
155 /*!
156  * \internal
157  * \brief Determine the transfer context to use.
158  * \since 12.0.0
159  *
160  * \param transferer Channel initiating the transfer.
161  * \param context User supplied context if available.  May be NULL.
162  *
163  * \return The context to use for the transfer.
164  */
165 static const char *get_transfer_context(struct ast_channel *transferer, const char *context)
166 {
167         if (!ast_strlen_zero(context)) {
168                 return context;
169         }
170         context = pbx_builtin_getvar_helper(transferer, "TRANSFER_CONTEXT");
171         if (!ast_strlen_zero(context)) {
172                 return context;
173         }
174         context = ast_channel_macrocontext(transferer);
175         if (!ast_strlen_zero(context)) {
176                 return context;
177         }
178         context = ast_channel_context(transferer);
179         if (!ast_strlen_zero(context)) {
180                 return context;
181         }
182         return "default";
183 }
184
185 static void blind_transfer_cb(struct ast_channel *new_channel, void *user_data,
186                 enum ast_transfer_type transfer_type)
187 {
188         struct ast_channel *transferer_channel = user_data;
189
190         if (transfer_type == AST_BRIDGE_TRANSFER_MULTI_PARTY) {
191                 copy_caller_data(new_channel, transferer_channel);
192         }
193 }
194
195 /*! \brief Internal built in feature for blind transfers */
196 static int feature_blind_transfer(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, void *hook_pvt)
197 {
198         char exten[AST_MAX_EXTENSION] = "";
199         struct ast_bridge_features_blind_transfer *blind_transfer = hook_pvt;
200         const char *context;
201         char *goto_on_blindxfr;
202
203         ast_bridge_channel_write_hold(bridge_channel, NULL);
204
205         ast_channel_lock(bridge_channel->chan);
206         context = ast_strdupa(get_transfer_context(bridge_channel->chan,
207                 blind_transfer ? blind_transfer->context : NULL));
208         goto_on_blindxfr = ast_strdupa(S_OR(pbx_builtin_getvar_helper(bridge_channel->chan,
209                 "GOTO_ON_BLINDXFR"), ""));
210         ast_channel_unlock(bridge_channel->chan);
211
212         /* Grab the extension to transfer to */
213         if (grab_transfer(bridge_channel->chan, exten, sizeof(exten), context)) {
214                 ast_bridge_channel_write_unhold(bridge_channel);
215                 return 0;
216         }
217
218         if (!ast_strlen_zero(goto_on_blindxfr)) {
219                 ast_debug(1, "After transfer, transferer %s goes to %s\n",
220                                 ast_channel_name(bridge_channel->chan), goto_on_blindxfr);
221                 ast_after_bridge_set_go_on(bridge_channel->chan, NULL, NULL, 0, goto_on_blindxfr);
222         }
223
224         if (ast_bridge_transfer_blind(0, bridge_channel->chan, exten, context, blind_transfer_cb,
225                         bridge_channel->chan) != AST_BRIDGE_TRANSFER_SUCCESS &&
226                         !ast_strlen_zero(goto_on_blindxfr)) {
227                 ast_after_bridge_goto_discard(bridge_channel->chan);
228         }
229
230         return 0;
231 }
232
233 /*! Attended transfer code */
234 enum atxfer_code {
235         /*! Party C hungup or other reason to abandon the transfer. */
236         ATXFER_INCOMPLETE,
237         /*! Transfer party C to party A. */
238         ATXFER_COMPLETE,
239         /*! Turn the transfer into a threeway call. */
240         ATXFER_THREEWAY,
241         /*! Hangup party C and return party B to the bridge. */
242         ATXFER_ABORT,
243 };
244
245 /*! \brief Attended transfer feature to complete transfer */
246 static int attended_transfer_complete(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, void *hook_pvt)
247 {
248         enum atxfer_code *transfer_code = hook_pvt;
249
250         *transfer_code = ATXFER_COMPLETE;
251         ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_HANGUP);
252         return 0;
253 }
254
255 /*! \brief Attended transfer feature to turn it into a threeway call */
256 static int attended_transfer_threeway(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, void *hook_pvt)
257 {
258         enum atxfer_code *transfer_code = hook_pvt;
259
260         *transfer_code = ATXFER_THREEWAY;
261         ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_HANGUP);
262         return 0;
263 }
264
265 /*! \brief Attended transfer feature to abort transfer */
266 static int attended_transfer_abort(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, void *hook_pvt)
267 {
268         enum atxfer_code *transfer_code = hook_pvt;
269
270         *transfer_code = ATXFER_ABORT;
271         ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_HANGUP);
272         return 0;
273 }
274
275 /*! \brief Internal built in feature for attended transfers */
276 static int feature_attended_transfer(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, void *hook_pvt)
277 {
278         char exten[AST_MAX_EXTENSION] = "";
279         struct ast_channel *peer;
280         struct ast_bridge *attended_bridge;
281         struct ast_bridge_features caller_features;
282         int xfer_failed;
283         struct ast_bridge_features_attended_transfer *attended_transfer = hook_pvt;
284         const char *complete_sound;
285         const char *context;
286         enum atxfer_code transfer_code = ATXFER_INCOMPLETE;
287         const char *atxfer_abort;
288         const char *atxfer_threeway;
289         const char *atxfer_complete;
290         const char *fail_sound;
291         RAII_VAR(struct ast_features_xfer_config *, xfer_cfg, NULL, ao2_cleanup);
292
293         ast_bridge_channel_write_hold(bridge_channel, NULL);
294
295         bridge = ast_bridge_channel_merge_inhibit(bridge_channel, +1);
296
297         ast_channel_lock(bridge_channel->chan);
298         context = ast_strdupa(get_transfer_context(bridge_channel->chan,
299                 attended_transfer ? attended_transfer->context : NULL));
300         xfer_cfg = ast_get_chan_features_xfer_config(bridge_channel->chan);
301         if (!xfer_cfg) {
302                 ast_log(LOG_ERROR, "Unable to get transfer configuration options\n");
303                 ast_channel_unlock(bridge_channel->chan);
304                 return 0;
305         }
306         if (attended_transfer) {
307                 atxfer_abort = ast_strdupa(S_OR(attended_transfer->abort, xfer_cfg->atxferabort));
308                 atxfer_threeway = ast_strdupa(S_OR(attended_transfer->threeway, xfer_cfg->atxferthreeway));
309                 atxfer_complete = ast_strdupa(S_OR(attended_transfer->complete, xfer_cfg->atxfercomplete));
310         } else {
311                 atxfer_abort = ast_strdupa(xfer_cfg->atxferabort);
312                 atxfer_threeway = ast_strdupa(xfer_cfg->atxferthreeway);
313                 atxfer_complete = ast_strdupa(xfer_cfg->atxfercomplete);
314         }
315         fail_sound = ast_strdupa(xfer_cfg->xferfailsound);
316         ast_channel_unlock(bridge_channel->chan);
317
318         /* Grab the extension to transfer to */
319         if (grab_transfer(bridge_channel->chan, exten, sizeof(exten), context)) {
320                 ast_bridge_merge_inhibit(bridge, -1);
321                 ao2_ref(bridge, -1);
322                 ast_bridge_channel_write_unhold(bridge_channel);
323                 return 0;
324         }
325
326         /* Get a channel that is the destination we wish to call */
327         peer = dial_transfer(bridge_channel->chan, exten, context);
328         if (!peer) {
329                 ast_bridge_merge_inhibit(bridge, -1);
330                 ao2_ref(bridge, -1);
331                 ast_stream_and_wait(bridge_channel->chan, fail_sound, AST_DIGIT_NONE);
332                 ast_bridge_channel_write_unhold(bridge_channel);
333                 return 0;
334         }
335
336 /* BUGBUG bridging API features does not support the features.conf atxfer bounce between C & B channels */
337         /* Setup a DTMF menu to control the transfer. */
338         if (ast_bridge_features_init(&caller_features)
339                 || ast_bridge_hangup_hook(&caller_features,
340                         attended_transfer_complete, &transfer_code, NULL, 0)
341                 || ast_bridge_dtmf_hook(&caller_features, atxfer_abort,
342                         attended_transfer_abort, &transfer_code, NULL, 0)
343                 || ast_bridge_dtmf_hook(&caller_features, atxfer_complete,
344                         attended_transfer_complete, &transfer_code, NULL, 0)
345                 || ast_bridge_dtmf_hook(&caller_features, atxfer_threeway,
346                         attended_transfer_threeway, &transfer_code, NULL, 0)) {
347                 ast_bridge_features_cleanup(&caller_features);
348                 ast_hangup(peer);
349                 ast_bridge_merge_inhibit(bridge, -1);
350                 ao2_ref(bridge, -1);
351                 ast_stream_and_wait(bridge_channel->chan, fail_sound, AST_DIGIT_NONE);
352                 ast_bridge_channel_write_unhold(bridge_channel);
353                 return 0;
354         }
355
356         /* Create a bridge to use to talk to the person we are calling */
357         attended_bridge = ast_bridge_base_new(AST_BRIDGE_CAPABILITY_1TO1MIX,
358                 AST_BRIDGE_FLAG_DISSOLVE_HANGUP);
359         if (!attended_bridge) {
360                 ast_bridge_features_cleanup(&caller_features);
361                 ast_hangup(peer);
362                 ast_bridge_merge_inhibit(bridge, -1);
363                 ao2_ref(bridge, -1);
364                 ast_stream_and_wait(bridge_channel->chan, fail_sound, AST_DIGIT_NONE);
365                 ast_bridge_channel_write_unhold(bridge_channel);
366                 return 0;
367         }
368         ast_bridge_merge_inhibit(attended_bridge, +1);
369
370         /* This is how this is going down, we are imparting the channel we called above into this bridge first */
371 /* BUGBUG we should impart the peer as an independent and move it to the original bridge. */
372         if (ast_bridge_impart(attended_bridge, peer, NULL, NULL, 0)) {
373                 ast_bridge_destroy(attended_bridge);
374                 ast_bridge_features_cleanup(&caller_features);
375                 ast_hangup(peer);
376                 ast_bridge_merge_inhibit(bridge, -1);
377                 ao2_ref(bridge, -1);
378                 ast_stream_and_wait(bridge_channel->chan, fail_sound, AST_DIGIT_NONE);
379                 ast_bridge_channel_write_unhold(bridge_channel);
380                 return 0;
381         }
382
383         /*
384          * For the caller we want to join the bridge in a blocking
385          * fashion so we don't spin around in this function doing
386          * nothing while waiting.
387          */
388         ast_bridge_join(attended_bridge, bridge_channel->chan, NULL, &caller_features, NULL, 0);
389
390 /*
391  * BUGBUG there is a small window where the channel does not point to the bridge_channel.
392  *
393  * This window is expected to go away when atxfer is redesigned
394  * to fully support existing functionality.  There will be one
395  * and only one ast_bridge_channel structure per channel.
396  */
397         /* Point the channel back to the original bridge and bridge_channel. */
398         ast_bridge_channel_lock(bridge_channel);
399         ast_channel_lock(bridge_channel->chan);
400         ast_channel_internal_bridge_channel_set(bridge_channel->chan, bridge_channel);
401         ast_channel_internal_bridge_set(bridge_channel->chan, bridge_channel->bridge);
402         ast_channel_unlock(bridge_channel->chan);
403         ast_bridge_channel_unlock(bridge_channel);
404
405         /* Wait for peer thread to exit bridge and die. */
406         if (!ast_autoservice_start(bridge_channel->chan)) {
407                 ast_bridge_depart(peer);
408                 ast_autoservice_stop(bridge_channel->chan);
409         } else {
410                 ast_bridge_depart(peer);
411         }
412
413         /* Now that all channels are out of it we can destroy the bridge and the feature structures */
414         ast_bridge_destroy(attended_bridge);
415         ast_bridge_features_cleanup(&caller_features);
416
417         /* Is there a courtesy sound to play to the peer? */
418         ast_channel_lock(bridge_channel->chan);
419         complete_sound = pbx_builtin_getvar_helper(bridge_channel->chan,
420                 "ATTENDED_TRANSFER_COMPLETE_SOUND");
421         if (!ast_strlen_zero(complete_sound)) {
422                 complete_sound = ast_strdupa(complete_sound);
423         } else {
424                 complete_sound = NULL;
425         }
426         ast_channel_unlock(bridge_channel->chan);
427         if (complete_sound) {
428                 pbx_builtin_setvar_helper(peer, "BRIDGE_PLAY_SOUND", complete_sound);
429         }
430
431         xfer_failed = -1;
432         switch (transfer_code) {
433         case ATXFER_INCOMPLETE:
434                 /* Peer hungup */
435                 break;
436         case ATXFER_COMPLETE:
437                 /* The peer takes our place in the bridge. */
438                 ast_bridge_channel_write_unhold(bridge_channel);
439                 ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_HANGUP);
440                 xfer_failed = ast_bridge_impart(bridge_channel->bridge, peer, bridge_channel->chan, NULL, 1);
441                 break;
442         case ATXFER_THREEWAY:
443                 /*
444                  * Transferer wants to convert to a threeway call.
445                  *
446                  * Just impart the peer onto the bridge and have us return to it
447                  * as normal.
448                  */
449                 ast_bridge_channel_write_unhold(bridge_channel);
450                 xfer_failed = ast_bridge_impart(bridge_channel->bridge, peer, NULL, NULL, 1);
451                 break;
452         case ATXFER_ABORT:
453                 /* Transferer decided not to transfer the call after all. */
454                 break;
455         }
456         ast_bridge_merge_inhibit(bridge, -1);
457         ao2_ref(bridge, -1);
458         if (xfer_failed) {
459                 ast_hangup(peer);
460                 if (!ast_check_hangup_locked(bridge_channel->chan)) {
461                         ast_stream_and_wait(bridge_channel->chan, fail_sound, AST_DIGIT_NONE);
462                 }
463                 ast_bridge_channel_write_unhold(bridge_channel);
464         }
465
466         return 0;
467 }
468
469 enum set_touch_variables_res {
470         SET_TOUCH_SUCCESS,
471         SET_TOUCH_UNSET,
472         SET_TOUCH_ALLOC_FAILURE,
473 };
474
475 static void set_touch_variable(enum set_touch_variables_res *res, struct ast_channel *chan, const char *var_name, char **touch)
476 {
477         const char *c_touch;
478
479         if (*res == SET_TOUCH_ALLOC_FAILURE) {
480                 return;
481         }
482         c_touch = pbx_builtin_getvar_helper(chan, var_name);
483         if (!ast_strlen_zero(c_touch)) {
484                 *touch = ast_strdup(c_touch);
485                 if (!*touch) {
486                         *res = SET_TOUCH_ALLOC_FAILURE;
487                 } else {
488                         *res = SET_TOUCH_SUCCESS;
489                 }
490         }
491 }
492
493 static enum set_touch_variables_res set_touch_variables(struct ast_channel *chan, int is_mixmonitor, char **touch_format, char **touch_monitor, char **touch_monitor_prefix)
494 {
495         enum set_touch_variables_res res = SET_TOUCH_UNSET;
496         const char *var_format;
497         const char *var_monitor;
498         const char *var_prefix;
499
500         SCOPED_CHANNELLOCK(lock, chan);
501
502         if (is_mixmonitor) {
503                 var_format = "TOUCH_MIXMONITOR_FORMAT";
504                 var_monitor = "TOUCH_MIXMONITOR";
505                 var_prefix = "TOUCH_MIXMONITOR_PREFIX";
506         } else {
507                 var_format = "TOUCH_MONITOR_FORMAT";
508                 var_monitor = "TOUCH_MONITOR";
509                 var_prefix = "TOUCH_MONITOR_PREFIX";
510         }
511         set_touch_variable(&res, chan, var_format, touch_format);
512         set_touch_variable(&res, chan, var_monitor, touch_monitor);
513         set_touch_variable(&res, chan, var_prefix, touch_monitor_prefix);
514
515         return res;
516 }
517
518 static void stop_automonitor(struct ast_bridge_channel *bridge_channel, struct ast_channel *peer_chan, struct ast_features_general_config *features_cfg, const char *stop_message)
519 {
520         ast_verb(3, "AutoMonitor used to stop recording call.\n");
521
522         ast_channel_lock(peer_chan);
523         if (ast_channel_monitor(peer_chan)) {
524                 if (ast_channel_monitor(peer_chan)->stop(peer_chan, 1)) {
525                         ast_verb(3, "Cannot stop AutoMonitor for %s\n", ast_channel_name(bridge_channel->chan));
526                         if (features_cfg && !(ast_strlen_zero(features_cfg->recordingfailsound))) {
527                                 ast_bridge_channel_queue_playfile(bridge_channel, NULL, features_cfg->recordingfailsound, NULL);
528                         }
529                         ast_channel_unlock(peer_chan);
530                         return;
531                 }
532         } else {
533                 /* Something else removed the Monitor before we got to it. */
534                 ast_channel_unlock(peer_chan);
535                 return;
536         }
537
538         ast_channel_unlock(peer_chan);
539
540         if (features_cfg && !(ast_strlen_zero(features_cfg->courtesytone))) {
541                 ast_bridge_channel_queue_playfile(bridge_channel, NULL, features_cfg->courtesytone, NULL);
542                 ast_bridge_channel_write_playfile(bridge_channel, NULL, features_cfg->courtesytone, NULL);
543         }
544
545         if (!ast_strlen_zero(stop_message)) {
546                 ast_bridge_channel_queue_playfile(bridge_channel, NULL, stop_message, NULL);
547                 ast_bridge_channel_write_playfile(bridge_channel, NULL, stop_message, NULL);
548         }
549 }
550
551 static void start_automonitor(struct ast_bridge_channel *bridge_channel, struct ast_channel *peer_chan, struct ast_features_general_config *features_cfg, const char *start_message)
552 {
553         char *touch_filename;
554         size_t len;
555         int x;
556         enum set_touch_variables_res set_touch_res;
557
558         RAII_VAR(char *, touch_format, NULL, ast_free);
559         RAII_VAR(char *, touch_monitor, NULL, ast_free);
560         RAII_VAR(char *, touch_monitor_prefix, NULL, ast_free);
561
562         set_touch_res = set_touch_variables(bridge_channel->chan, 0, &touch_format,
563                 &touch_monitor, &touch_monitor_prefix);
564         switch (set_touch_res) {
565         case SET_TOUCH_SUCCESS:
566                 break;
567         case SET_TOUCH_UNSET:
568                 set_touch_res = set_touch_variables(peer_chan, 0, &touch_format, &touch_monitor,
569                         &touch_monitor_prefix);
570                 if (set_touch_res == SET_TOUCH_ALLOC_FAILURE) {
571                         return;
572                 }
573                 break;
574         case SET_TOUCH_ALLOC_FAILURE:
575                 return;
576         }
577
578         if (!ast_strlen_zero(touch_monitor)) {
579                 len = strlen(touch_monitor) + 50;
580                 touch_filename = ast_alloca(len);
581                 snprintf(touch_filename, len, "%s-%ld-%s",
582                         S_OR(touch_monitor_prefix, "auto"),
583                         (long) time(NULL),
584                         touch_monitor);
585         } else {
586                 char *caller_chan_id;
587                 char *peer_chan_id;
588
589                 caller_chan_id = ast_strdupa(S_COR(ast_channel_caller(bridge_channel->chan)->id.number.valid,
590                         ast_channel_caller(bridge_channel->chan)->id.number.str, ast_channel_name(bridge_channel->chan)));
591                 peer_chan_id = ast_strdupa(S_COR(ast_channel_caller(peer_chan)->id.number.valid,
592                         ast_channel_caller(peer_chan)->id.number.str, ast_channel_name(peer_chan)));
593                 len = strlen(caller_chan_id) + strlen(peer_chan_id) + 50;
594                 touch_filename = ast_alloca(len);
595                 snprintf(touch_filename, len, "%s-%ld-%s-%s",
596                         S_OR(touch_monitor_prefix, "auto"),
597                         (long) time(NULL),
598                         caller_chan_id,
599                         peer_chan_id);
600         }
601
602         for (x = 0; x < strlen(touch_filename); x++) {
603                 if (touch_filename[x] == '/') {
604                         touch_filename[x] = '-';
605                 }
606         }
607
608         ast_verb(3, "AutoMonitor used to record call. Filename: %s\n", touch_filename);
609
610         if (ast_monitor_start(peer_chan, touch_format, touch_filename, 1, X_REC_IN | X_REC_OUT)) {
611                 ast_verb(3, "automon feature was tried by '%s' but monitor failed to start.\n",
612                         ast_channel_name(bridge_channel->chan));
613                 return;
614         }
615
616         if (features_cfg && !ast_strlen_zero(features_cfg->courtesytone)) {
617                 ast_bridge_channel_queue_playfile(bridge_channel, NULL, features_cfg->courtesytone, NULL);
618                 ast_bridge_channel_write_playfile(bridge_channel, NULL, features_cfg->courtesytone, NULL);
619         }
620
621         if (!ast_strlen_zero(start_message)) {
622                 ast_bridge_channel_queue_playfile(bridge_channel, NULL, start_message, NULL);
623                 ast_bridge_channel_write_playfile(bridge_channel, NULL, start_message, NULL);
624         }
625
626         pbx_builtin_setvar_helper(peer_chan, "TOUCH_MONITOR_OUTPUT", touch_filename);
627 }
628
629 static int feature_automonitor(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, void *hook_pvt)
630 {
631         const char *start_message;
632         const char *stop_message;
633         struct ast_bridge_features_automonitor *options = hook_pvt;
634         enum ast_bridge_features_monitor start_stop = options ? options->start_stop : AUTO_MONITOR_TOGGLE;
635         int is_monitoring;
636
637         RAII_VAR(struct ast_channel *, peer_chan, NULL, ast_channel_cleanup);
638         RAII_VAR(struct ast_features_general_config *, features_cfg, NULL, ao2_cleanup);
639
640         features_cfg = ast_get_chan_features_general_config(bridge_channel->chan);
641         peer_chan = ast_bridge_peer(bridge, bridge_channel->chan);
642
643         if (!peer_chan) {
644                 ast_verb(3, "Cannot start AutoMonitor for %s - can not determine peer in bridge.\n",
645                         ast_channel_name(bridge_channel->chan));
646                 if (features_cfg && !ast_strlen_zero(features_cfg->recordingfailsound)) {
647                         ast_bridge_channel_queue_playfile(bridge_channel, NULL, features_cfg->recordingfailsound, NULL);
648                 }
649                 return 0;
650         }
651
652         ast_channel_lock(bridge_channel->chan);
653         start_message = pbx_builtin_getvar_helper(bridge_channel->chan,
654                 "TOUCH_MONITOR_MESSAGE_START");
655         start_message = ast_strdupa(S_OR(start_message, ""));
656         stop_message = pbx_builtin_getvar_helper(bridge_channel->chan,
657                 "TOUCH_MONITOR_MESSAGE_STOP");
658         stop_message = ast_strdupa(S_OR(stop_message, ""));
659         ast_channel_unlock(bridge_channel->chan);
660
661         is_monitoring = ast_channel_monitor(peer_chan) != NULL;
662         switch (start_stop) {
663         case AUTO_MONITOR_TOGGLE:
664                 if (is_monitoring) {
665                         stop_automonitor(bridge_channel, peer_chan, features_cfg, stop_message);
666                 } else {
667                         start_automonitor(bridge_channel, peer_chan, features_cfg, start_message);
668                 }
669                 return 0;
670         case AUTO_MONITOR_START:
671                 if (!is_monitoring) {
672                         start_automonitor(bridge_channel, peer_chan, features_cfg, start_message);
673                         return 0;
674                 }
675                 ast_verb(3, "AutoMonitor already recording call.\n");
676                 break;
677         case AUTO_MONITOR_STOP:
678                 if (is_monitoring) {
679                         stop_automonitor(bridge_channel, peer_chan, features_cfg, stop_message);
680                         return 0;
681                 }
682                 ast_verb(3, "AutoMonitor already not recording call.\n");
683                 break;
684         }
685
686         /*
687          * Fake start/stop to invoker so will think it did something but
688          * was already in that mode.
689          */
690         if (features_cfg && !ast_strlen_zero(features_cfg->courtesytone)) {
691                 ast_bridge_channel_queue_playfile(bridge_channel, NULL, features_cfg->courtesytone, NULL);
692         }
693         if (is_monitoring) {
694                 if (!ast_strlen_zero(start_message)) {
695                         ast_bridge_channel_queue_playfile(bridge_channel, NULL, start_message, NULL);
696                 }
697         } else {
698                 if (!ast_strlen_zero(stop_message)) {
699                         ast_bridge_channel_queue_playfile(bridge_channel, NULL, stop_message, NULL);
700                 }
701         }
702         return 0;
703 }
704
705 static void stop_automixmonitor(struct ast_bridge_channel *bridge_channel, struct ast_channel *peer_chan, struct ast_features_general_config *features_cfg, const char *stop_message)
706 {
707         ast_verb(3, "AutoMixMonitor used to stop recording call.\n");
708
709         if (ast_stop_mixmonitor(peer_chan, NULL)) {
710                 ast_verb(3, "Failed to stop Mixmonitor for %s.\n", ast_channel_name(bridge_channel->chan));
711                 if (features_cfg && !(ast_strlen_zero(features_cfg->recordingfailsound))) {
712                         ast_bridge_channel_queue_playfile(bridge_channel, NULL, features_cfg->recordingfailsound, NULL);
713                 }
714                 return;
715         }
716
717         if (features_cfg && !ast_strlen_zero(features_cfg->courtesytone)) {
718                 ast_bridge_channel_queue_playfile(bridge_channel, NULL, features_cfg->courtesytone, NULL);
719                 ast_bridge_channel_write_playfile(bridge_channel, NULL, features_cfg->courtesytone, NULL);
720         }
721
722         if (!ast_strlen_zero(stop_message)) {
723                 ast_bridge_channel_queue_playfile(bridge_channel, NULL, stop_message, NULL);
724                 ast_bridge_channel_write_playfile(bridge_channel, NULL, stop_message, NULL);
725         }
726 }
727
728 static void start_automixmonitor(struct ast_bridge_channel *bridge_channel, struct ast_channel *peer_chan, struct ast_features_general_config *features_cfg, const char *start_message)
729 {
730         char *touch_filename;
731         size_t len;
732         int x;
733         enum set_touch_variables_res set_touch_res;
734
735         RAII_VAR(char *, touch_format, NULL, ast_free);
736         RAII_VAR(char *, touch_monitor, NULL, ast_free);
737         RAII_VAR(char *, touch_monitor_prefix, NULL, ast_free);
738
739         set_touch_res = set_touch_variables(bridge_channel->chan, 1, &touch_format,
740                 &touch_monitor, &touch_monitor_prefix);
741         switch (set_touch_res) {
742         case SET_TOUCH_SUCCESS:
743                 break;
744         case SET_TOUCH_UNSET:
745                 set_touch_res = set_touch_variables(peer_chan, 1, &touch_format, &touch_monitor,
746                         &touch_monitor_prefix);
747                 if (set_touch_res == SET_TOUCH_ALLOC_FAILURE) {
748                         return;
749                 }
750                 break;
751         case SET_TOUCH_ALLOC_FAILURE:
752                 return;
753         }
754
755         if (!ast_strlen_zero(touch_monitor)) {
756                 len = strlen(touch_monitor) + 50;
757                 touch_filename = ast_alloca(len);
758                 snprintf(touch_filename, len, "%s-%ld-%s.%s",
759                         S_OR(touch_monitor_prefix, "auto"),
760                         (long) time(NULL),
761                         touch_monitor,
762                         S_OR(touch_format, "wav"));
763         } else {
764                 char *caller_chan_id;
765                 char *peer_chan_id;
766
767                 caller_chan_id = ast_strdupa(S_COR(ast_channel_caller(bridge_channel->chan)->id.number.valid,
768                         ast_channel_caller(bridge_channel->chan)->id.number.str, ast_channel_name(bridge_channel->chan)));
769                 peer_chan_id = ast_strdupa(S_COR(ast_channel_caller(peer_chan)->id.number.valid,
770                         ast_channel_caller(peer_chan)->id.number.str, ast_channel_name(peer_chan)));
771                 len = strlen(caller_chan_id) + strlen(peer_chan_id) + 50;
772                 touch_filename = ast_alloca(len);
773                 snprintf(touch_filename, len, "%s-%ld-%s-%s.%s",
774                         S_OR(touch_monitor_prefix, "auto"),
775                         (long) time(NULL),
776                         caller_chan_id,
777                         peer_chan_id,
778                         S_OR(touch_format, "wav"));
779         }
780
781         for (x = 0; x < strlen(touch_filename); x++) {
782                 if (touch_filename[x] == '/') {
783                         touch_filename[x] = '-';
784                 }
785         }
786
787         ast_verb(3, "AutoMixMonitor used to record call. Filename: %s\n", touch_filename);
788
789         if (ast_start_mixmonitor(peer_chan, touch_filename, "b")) {
790                 ast_verb(3, "automixmon feature was tried by '%s' but mixmonitor failed to start.\n",
791                         ast_channel_name(bridge_channel->chan));
792
793                 if (features_cfg && !ast_strlen_zero(features_cfg->recordingfailsound)) {
794                         ast_bridge_channel_queue_playfile(bridge_channel, NULL, features_cfg->recordingfailsound, NULL);
795                 }
796                 return;
797         }
798
799         if (features_cfg && !ast_strlen_zero(features_cfg->courtesytone)) {
800                 ast_bridge_channel_queue_playfile(bridge_channel, NULL, features_cfg->courtesytone, NULL);
801                 ast_bridge_channel_write_playfile(bridge_channel, NULL, features_cfg->courtesytone, NULL);
802         }
803
804         if (!ast_strlen_zero(start_message)) {
805                 ast_bridge_channel_queue_playfile(bridge_channel, NULL, start_message, NULL);
806                 ast_bridge_channel_write_playfile(bridge_channel, NULL, start_message, NULL);
807         }
808
809         pbx_builtin_setvar_helper(peer_chan, "TOUCH_MIXMONITOR_OUTPUT", touch_filename);
810 }
811
812 static int feature_automixmonitor(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, void *hook_pvt)
813 {
814         static const char *mixmonitor_spy_type = "MixMonitor";
815         const char *stop_message;
816         const char *start_message;
817         struct ast_bridge_features_automixmonitor *options = hook_pvt;
818         enum ast_bridge_features_monitor start_stop = options ? options->start_stop : AUTO_MONITOR_TOGGLE;
819         int is_monitoring;
820
821         RAII_VAR(struct ast_channel *, peer_chan, NULL, ast_channel_cleanup);
822         RAII_VAR(struct ast_features_general_config *, features_cfg, NULL, ao2_cleanup);
823
824         features_cfg = ast_get_chan_features_general_config(bridge_channel->chan);
825         peer_chan = ast_bridge_peer(bridge, bridge_channel->chan);
826
827         if (!peer_chan) {
828                 ast_verb(3, "Cannot do AutoMixMonitor for %s - cannot determine peer in bridge.\n",
829                         ast_channel_name(bridge_channel->chan));
830                 if (features_cfg && !ast_strlen_zero(features_cfg->recordingfailsound)) {
831                         ast_bridge_channel_queue_playfile(bridge_channel, NULL, features_cfg->recordingfailsound, NULL);
832                 }
833                 return 0;
834         }
835
836         ast_channel_lock(bridge_channel->chan);
837         start_message = pbx_builtin_getvar_helper(bridge_channel->chan,
838                 "TOUCH_MIXMONITOR_MESSAGE_START");
839         start_message = ast_strdupa(S_OR(start_message, ""));
840         stop_message = pbx_builtin_getvar_helper(bridge_channel->chan,
841                 "TOUCH_MIXMONITOR_MESSAGE_STOP");
842         stop_message = ast_strdupa(S_OR(stop_message, ""));
843         ast_channel_unlock(bridge_channel->chan);
844
845         is_monitoring =
846                 0 < ast_channel_audiohook_count_by_source(peer_chan, mixmonitor_spy_type, AST_AUDIOHOOK_TYPE_SPY);
847         switch (start_stop) {
848         case AUTO_MONITOR_TOGGLE:
849                 if (is_monitoring) {
850                         stop_automixmonitor(bridge_channel, peer_chan, features_cfg, stop_message);
851                 } else {
852                         start_automixmonitor(bridge_channel, peer_chan, features_cfg, start_message);
853                 }
854                 return 0;
855         case AUTO_MONITOR_START:
856                 if (!is_monitoring) {
857                         start_automixmonitor(bridge_channel, peer_chan, features_cfg, start_message);
858                         return 0;
859                 }
860                 ast_verb(3, "AutoMixMonitor already recording call.\n");
861                 break;
862         case AUTO_MONITOR_STOP:
863                 if (is_monitoring) {
864                         stop_automixmonitor(bridge_channel, peer_chan, features_cfg, stop_message);
865                         return 0;
866                 }
867                 ast_verb(3, "AutoMixMonitor already not recording call.\n");
868                 break;
869         }
870
871         /*
872          * Fake start/stop to invoker so will think it did something but
873          * was already in that mode.
874          */
875         if (features_cfg && !ast_strlen_zero(features_cfg->courtesytone)) {
876                 ast_bridge_channel_queue_playfile(bridge_channel, NULL, features_cfg->courtesytone, NULL);
877         }
878         if (is_monitoring) {
879                 if (!ast_strlen_zero(start_message)) {
880                         ast_bridge_channel_queue_playfile(bridge_channel, NULL, start_message, NULL);
881                 }
882         } else {
883                 if (!ast_strlen_zero(stop_message)) {
884                         ast_bridge_channel_queue_playfile(bridge_channel, NULL, stop_message, NULL);
885                 }
886         }
887         return 0;
888 }
889
890 /*! \brief Internal built in feature for hangup */
891 static int feature_hangup(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, void *hook_pvt)
892 {
893         /*
894          * This is very simple, we simply change the state on the
895          * bridge_channel to force the channel out of the bridge and the
896          * core takes care of the rest.
897          */
898         ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_END);
899         return 0;
900 }
901
902 static int unload_module(void)
903 {
904         return 0;
905 }
906
907 static int load_module(void)
908 {
909         ast_bridge_features_register(AST_BRIDGE_BUILTIN_BLINDTRANSFER, feature_blind_transfer, NULL);
910         ast_bridge_features_register(AST_BRIDGE_BUILTIN_ATTENDEDTRANSFER, feature_attended_transfer, NULL);
911         ast_bridge_features_register(AST_BRIDGE_BUILTIN_HANGUP, feature_hangup, NULL);
912         ast_bridge_features_register(AST_BRIDGE_BUILTIN_AUTOMON, feature_automonitor, NULL);
913         ast_bridge_features_register(AST_BRIDGE_BUILTIN_AUTOMIXMON, feature_automixmonitor, NULL);
914
915         /* Bump up our reference count so we can't be unloaded */
916         ast_module_ref(ast_module_info->self);
917
918         return AST_MODULE_LOAD_SUCCESS;
919 }
920
921 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Built in bridging features");