Dial API: Add a dial option to indicate the dialed channel will replace dialer
[asterisk/asterisk.git] / main / dial.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2007, 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 Dialing API
22  *
23  * \author Joshua Colp <jcolp@digium.com>
24  */
25
26 /*** MODULEINFO
27         <support_level>core</support_level>
28  ***/
29
30 #include "asterisk.h"
31
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33
34 #include <sys/time.h>
35 #include <signal.h>
36
37 #include "asterisk/channel.h"
38 #include "asterisk/utils.h"
39 #include "asterisk/lock.h"
40 #include "asterisk/linkedlists.h"
41 #include "asterisk/dial.h"
42 #include "asterisk/pbx.h"
43 #include "asterisk/musiconhold.h"
44 #include "asterisk/app.h"
45 #include "asterisk/causes.h"
46 #include "asterisk/stasis_channels.h"
47
48 /*! \brief Main dialing structure. Contains global options, channels being dialed, and more! */
49 struct ast_dial {
50         int num;                                           /*!< Current number to give to next dialed channel */
51         int timeout;                                       /*!< Maximum time allowed for dial attempts */
52         int actual_timeout;                                /*!< Actual timeout based on all factors (ie: channels) */
53         enum ast_dial_result state;                        /*!< Status of dial */
54         void *options[AST_DIAL_OPTION_MAX];                /*!< Global options */
55         ast_dial_state_callback state_callback;            /*!< Status callback */
56         void *user_data;                                   /*!< Attached user data */
57         AST_LIST_HEAD(, ast_dial_channel) channels; /*!< Channels being dialed */
58         pthread_t thread;                                  /*!< Thread (if running in async) */
59         struct ast_callid *callid;                         /*!< callid pointer (if running in async) */
60         ast_mutex_t lock;                                  /*! Lock to protect the thread information above */
61 };
62
63 /*! \brief Dialing channel structure. Contains per-channel dialing options, asterisk channel, and more! */
64 struct ast_dial_channel {
65         int num;                                /*!< Unique number for dialed channel */
66         int timeout;                            /*!< Maximum time allowed for attempt */
67         char *tech;                             /*!< Technology being dialed */
68         char *device;                           /*!< Device being dialed */
69         void *options[AST_DIAL_OPTION_MAX];     /*!< Channel specific options */
70         int cause;                              /*!< Cause code in case of failure */
71         unsigned int is_running_app:1;          /*!< Is this running an application? */
72         char *assignedid1;                              /*!< UniqueID to assign channel */
73         char *assignedid2;                              /*!< UniqueID to assign 2nd channel */
74         struct ast_channel *owner;              /*!< Asterisk channel */
75         AST_LIST_ENTRY(ast_dial_channel) list;  /*!< Linked list information */
76 };
77
78 /*! \brief Typedef for dial option enable */
79 typedef void *(*ast_dial_option_cb_enable)(void *data);
80
81 /*! \brief Typedef for dial option disable */
82 typedef int (*ast_dial_option_cb_disable)(void *data);
83
84 /*! \brief Structure for 'ANSWER_EXEC' option */
85 struct answer_exec_struct {
86         char app[AST_MAX_APP]; /*!< Application name */
87         char *args;            /*!< Application arguments */
88 };
89
90 /*! \brief Enable function for 'ANSWER_EXEC' option */
91 static void *answer_exec_enable(void *data)
92 {
93         struct answer_exec_struct *answer_exec = NULL;
94         char *app = ast_strdupa((char*)data), *args = NULL;
95
96         /* Not giving any data to this option is bad, mmmk? */
97         if (ast_strlen_zero(app))
98                 return NULL;
99
100         /* Create new data structure */
101         if (!(answer_exec = ast_calloc(1, sizeof(*answer_exec))))
102                 return NULL;
103
104         /* Parse out application and arguments */
105         if ((args = strchr(app, ','))) {
106                 *args++ = '\0';
107                 answer_exec->args = ast_strdup(args);
108         }
109
110         /* Copy application name */
111         ast_copy_string(answer_exec->app, app, sizeof(answer_exec->app));
112
113         return answer_exec;
114 }
115
116 /*! \brief Disable function for 'ANSWER_EXEC' option */
117 static int answer_exec_disable(void *data)
118 {
119         struct answer_exec_struct *answer_exec = data;
120
121         /* Make sure we have a value */
122         if (!answer_exec)
123                 return -1;
124
125         /* If arguments are present, free them too */
126         if (answer_exec->args)
127                 ast_free(answer_exec->args);
128
129         /* This is simple - just free the structure */
130         ast_free(answer_exec);
131
132         return 0;
133 }
134
135 static void *music_enable(void *data)
136 {
137         return ast_strdup(data);
138 }
139
140 static int music_disable(void *data)
141 {
142         if (!data)
143                 return -1;
144
145         ast_free(data);
146
147         return 0;
148 }
149
150 static void *predial_enable(void *data)
151 {
152         return ast_strdup(data);
153 }
154
155 static int predial_disable(void *data)
156 {
157         if (!data) {
158                 return -1;
159         }
160
161         ast_free(data);
162
163         return 0;
164 }
165
166 /*! \brief Application execution function for 'ANSWER_EXEC' option */
167 static void answer_exec_run(struct ast_dial *dial, struct ast_dial_channel *dial_channel, char *app, char *args)
168 {
169         struct ast_channel *chan = dial_channel->owner;
170         struct ast_app *ast_app = pbx_findapp(app);
171
172         /* If the application was not found, return immediately */
173         if (!ast_app)
174                 return;
175
176         /* All is well... execute the application */
177         pbx_exec(chan, ast_app, args);
178
179         /* If another thread is not taking over hang up the channel */
180         ast_mutex_lock(&dial->lock);
181         if (dial->thread != AST_PTHREADT_STOP) {
182                 ast_hangup(chan);
183                 dial_channel->owner = NULL;
184         }
185         ast_mutex_unlock(&dial->lock);
186
187         return;
188 }
189
190 struct ast_option_types {
191         enum ast_dial_option option;
192         ast_dial_option_cb_enable enable;
193         ast_dial_option_cb_disable disable;
194 };
195
196 /*!
197  * \brief Map options to respective handlers (enable/disable).
198  *
199  * \note This list MUST be perfectly kept in order with enum
200  * ast_dial_option, or else madness will happen.
201  */
202 static const struct ast_option_types option_types[] = {
203         { AST_DIAL_OPTION_RINGING, NULL, NULL },                                  /*!< Always indicate ringing to caller */
204         { AST_DIAL_OPTION_ANSWER_EXEC, answer_exec_enable, answer_exec_disable }, /*!< Execute application upon answer in async mode */
205         { AST_DIAL_OPTION_MUSIC, music_enable, music_disable },                   /*!< Play music to the caller instead of ringing */
206         { AST_DIAL_OPTION_DISABLE_CALL_FORWARDING, NULL, NULL },                  /*!< Disable call forwarding on channels */
207         { AST_DIAL_OPTION_PREDIAL, predial_enable, predial_disable },             /*!< Execute a subroutine on the outbound channels prior to dialing */
208         { AST_DIAL_OPTION_DIAL_REPLACES_SELF, NULL, NULL },                       /*!< The dial operation is a replacement for the requester */
209         { AST_DIAL_OPTION_MAX, NULL, NULL },                                      /*!< Terminator of list */
210 };
211
212 /*! \brief Maximum number of channels we can watch at a time */
213 #define AST_MAX_WATCHERS 256
214
215 /*! \brief Macro for finding the option structure to use on a dialed channel */
216 #define FIND_RELATIVE_OPTION(dial, dial_channel, ast_dial_option) (dial_channel->options[ast_dial_option] ? dial_channel->options[ast_dial_option] : dial->options[ast_dial_option])
217
218 /*! \brief Macro that determines whether a channel is the caller or not */
219 #define IS_CALLER(chan, owner) (chan == owner ? 1 : 0)
220
221 /*! \brief New dialing structure
222  * \note Create a dialing structure
223  * \return Returns a calloc'd ast_dial structure, NULL on failure
224  */
225 struct ast_dial *ast_dial_create(void)
226 {
227         struct ast_dial *dial = NULL;
228
229         /* Allocate new memory for structure */
230         if (!(dial = ast_calloc(1, sizeof(*dial))))
231                 return NULL;
232
233         /* Initialize list of channels */
234         AST_LIST_HEAD_INIT(&dial->channels);
235
236         /* Initialize thread to NULL */
237         dial->thread = AST_PTHREADT_NULL;
238
239         /* No timeout exists... yet */
240         dial->timeout = -1;
241         dial->actual_timeout = -1;
242
243         /* Can't forget about the lock */
244         ast_mutex_init(&dial->lock);
245
246         return dial;
247 }
248
249 /*! \brief Append a channel
250  * \note Appends a channel to a dialing structure
251  * \return Returns channel reference number on success, -1 on failure
252  */
253 int ast_dial_append(struct ast_dial *dial, const char *tech, const char *device, const struct ast_assigned_ids *assignedids)
254 {
255         struct ast_dial_channel *channel = NULL;
256
257         /* Make sure we have required arguments */
258         if (!dial || !tech || !device)
259                 return -1;
260
261         /* Allocate new memory for dialed channel structure */
262         if (!(channel = ast_calloc(1, sizeof(*channel))))
263                 return -1;
264
265         /* Record technology and device for when we actually dial */
266         channel->tech = ast_strdup(tech);
267         channel->device = ast_strdup(device);
268
269         /* Store the assigned id */
270         if (assignedids && !ast_strlen_zero(assignedids->uniqueid)) {
271                 channel->assignedid1 = ast_strdup(assignedids->uniqueid);
272
273                 if (!ast_strlen_zero(assignedids->uniqueid2)) {
274                         channel->assignedid2 = ast_strdup(assignedids->uniqueid2);
275                 }
276         }
277
278         /* Grab reference number from dial structure */
279         channel->num = ast_atomic_fetchadd_int(&dial->num, +1);
280
281         /* No timeout exists... yet */
282         channel->timeout = -1;
283
284         /* Insert into channels list */
285         AST_LIST_INSERT_TAIL(&dial->channels, channel, list);
286
287         return channel->num;
288 }
289
290 /*! \brief Helper function that requests all channels */
291 static int begin_dial_prerun(struct ast_dial_channel *channel, struct ast_channel *chan, struct ast_format_cap *cap, const char *predial_string)
292 {
293         char numsubst[AST_MAX_EXTENSION];
294         struct ast_format_cap *cap_all_audio = NULL;
295         struct ast_format_cap *cap_request;
296         struct ast_assigned_ids assignedids = {
297                 .uniqueid = channel->assignedid1,
298                 .uniqueid2 = channel->assignedid2,
299         };
300
301         /* Copy device string over */
302         ast_copy_string(numsubst, channel->device, sizeof(numsubst));
303
304         if (cap && ast_format_cap_count(cap)) {
305                 cap_request = cap;
306         } else if (chan) {
307                 cap_request = ast_channel_nativeformats(chan);
308         } else {
309                 cap_all_audio = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
310                 ast_format_cap_append_by_type(cap_all_audio, AST_MEDIA_TYPE_AUDIO);
311                 cap_request = cap_all_audio;
312         }
313
314         /* If we fail to create our owner channel bail out */
315         if (!(channel->owner = ast_request(channel->tech, cap_request, &assignedids, chan, numsubst, &channel->cause))) {
316                 ao2_cleanup(cap_all_audio);
317                 return -1;
318         }
319         cap_request = NULL;
320         ao2_cleanup(cap_all_audio);
321
322         if (chan) {
323                 ast_channel_lock_both(chan, channel->owner);
324         } else {
325                 ast_channel_lock(channel->owner);
326         }
327
328         ast_channel_stage_snapshot(channel->owner);
329
330         ast_channel_appl_set(channel->owner, "AppDial2");
331         ast_channel_data_set(channel->owner, "(Outgoing Line)");
332
333         memset(ast_channel_whentohangup(channel->owner), 0, sizeof(*ast_channel_whentohangup(channel->owner)));
334
335         /* Inherit everything from he who spawned this dial */
336         if (chan) {
337                 ast_channel_inherit_variables(chan, channel->owner);
338                 ast_channel_datastore_inherit(chan, channel->owner);
339
340                 /* Copy over callerid information */
341                 ast_party_redirecting_copy(ast_channel_redirecting(channel->owner), ast_channel_redirecting(chan));
342
343                 ast_channel_dialed(channel->owner)->transit_network_select = ast_channel_dialed(chan)->transit_network_select;
344
345                 ast_connected_line_copy_from_caller(ast_channel_connected(channel->owner), ast_channel_caller(chan));
346
347                 ast_channel_language_set(channel->owner, ast_channel_language(chan));
348                 if (channel->options[AST_DIAL_OPTION_DIAL_REPLACES_SELF]) {
349                         ast_channel_req_accountcodes(channel->owner, chan, AST_CHANNEL_REQUESTOR_REPLACEMENT);
350                 } else {
351                         ast_channel_req_accountcodes(channel->owner, chan, AST_CHANNEL_REQUESTOR_BRIDGE_PEER);
352                 }
353                 if (ast_strlen_zero(ast_channel_musicclass(channel->owner)))
354                         ast_channel_musicclass_set(channel->owner, ast_channel_musicclass(chan));
355
356                 ast_channel_adsicpe_set(channel->owner, ast_channel_adsicpe(chan));
357                 ast_channel_transfercapability_set(channel->owner, ast_channel_transfercapability(chan));
358                 ast_channel_unlock(chan);
359         }
360
361         ast_channel_stage_snapshot_done(channel->owner);
362         ast_channel_unlock(channel->owner);
363
364         if (!ast_strlen_zero(predial_string)) {
365                 const char *predial_callee = ast_app_expand_sub_args(chan, predial_string);
366                 if (!predial_callee) {
367                         ast_log(LOG_ERROR, "Could not expand subroutine arguments in predial request '%s'\n", predial_string);
368                 }
369                 ast_autoservice_start(chan);
370                 ast_pre_call(channel->owner, predial_callee);
371                 ast_autoservice_stop(chan);
372                 ast_free((char *) predial_callee);
373         }
374
375         return 0;
376 }
377
378 int ast_dial_prerun(struct ast_dial *dial, struct ast_channel *chan, struct ast_format_cap *cap)
379 {
380         struct ast_dial_channel *channel;
381         int res = -1;
382         char *predial_string = dial->options[AST_DIAL_OPTION_PREDIAL];
383
384         if (!ast_strlen_zero(predial_string)) {
385                 ast_replace_subargument_delimiter(predial_string);
386         }
387
388         AST_LIST_LOCK(&dial->channels);
389         AST_LIST_TRAVERSE(&dial->channels, channel, list) {
390                 if ((res = begin_dial_prerun(channel, chan, cap, predial_string))) {
391                         break;
392                 }
393         }
394         AST_LIST_UNLOCK(&dial->channels);
395
396         return res;
397 }
398
399 /*! \brief Helper function that does the beginning dialing per-appended channel */
400 static int begin_dial_channel(struct ast_dial_channel *channel, struct ast_channel *chan, int async, const char *predial_string)
401 {
402         char numsubst[AST_MAX_EXTENSION];
403         int res = 1;
404
405         /* If no owner channel exists yet execute pre-run */
406         if (!channel->owner && begin_dial_prerun(channel, chan, NULL, predial_string)) {
407                 return 0;
408         }
409
410         /* Copy device string over */
411         ast_copy_string(numsubst, channel->device, sizeof(numsubst));
412
413         /* Attempt to actually call this device */
414         if ((res = ast_call(channel->owner, numsubst, 0))) {
415                 res = 0;
416                 ast_hangup(channel->owner);
417                 channel->owner = NULL;
418         } else {
419                 if (chan) {
420                         ast_poll_channel_add(chan, channel->owner);
421                 }
422                 ast_channel_publish_dial(async ? NULL : chan, channel->owner, channel->device, NULL);
423                 res = 1;
424                 ast_verb(3, "Called %s\n", numsubst);
425         }
426
427         return res;
428 }
429
430 /*! \brief Helper function that does the beginning dialing per dial structure */
431 static int begin_dial(struct ast_dial *dial, struct ast_channel *chan, int async)
432 {
433         struct ast_dial_channel *channel = NULL;
434         int success = 0;
435         char *predial_string = dial->options[AST_DIAL_OPTION_PREDIAL];
436
437         if (!ast_strlen_zero(predial_string)) {
438                 ast_replace_subargument_delimiter(predial_string);
439         }
440
441         /* Iterate through channel list, requesting and calling each one */
442         AST_LIST_LOCK(&dial->channels);
443         AST_LIST_TRAVERSE(&dial->channels, channel, list) {
444                 success += begin_dial_channel(channel, chan, async, predial_string);
445         }
446         AST_LIST_UNLOCK(&dial->channels);
447
448         /* If number of failures matches the number of channels, then this truly failed */
449         return success;
450 }
451
452 /*! \brief Helper function to handle channels that have been call forwarded */
453 static int handle_call_forward(struct ast_dial *dial, struct ast_dial_channel *channel, struct ast_channel *chan)
454 {
455         struct ast_channel *original = channel->owner;
456         char *tmp = ast_strdupa(ast_channel_call_forward(channel->owner));
457         char *tech = "Local", *device = tmp, *stuff;
458         char *predial_string = dial->options[AST_DIAL_OPTION_PREDIAL];
459
460         if (!ast_strlen_zero(predial_string)) {
461                 ast_replace_subargument_delimiter(predial_string);
462         }
463
464         /* If call forwarding is disabled just drop the original channel and don't attempt to dial the new one */
465         if (FIND_RELATIVE_OPTION(dial, channel, AST_DIAL_OPTION_DISABLE_CALL_FORWARDING)) {
466                 ast_hangup(original);
467                 channel->owner = NULL;
468                 return 0;
469         }
470
471         /* Figure out the new destination */
472         if ((stuff = strchr(tmp, '/'))) {
473                 *stuff++ = '\0';
474                 tech = tmp;
475                 device = stuff;
476         } else {
477                 const char *forward_context;
478                 char destination[AST_MAX_CONTEXT + AST_MAX_EXTENSION + 1];
479
480                 ast_channel_lock(original);
481                 forward_context = pbx_builtin_getvar_helper(original, "FORWARD_CONTEXT");
482                 snprintf(destination, sizeof(destination), "%s@%s", tmp, S_OR(forward_context, ast_channel_context(original)));
483                 ast_channel_unlock(original);
484                 device = ast_strdupa(destination);
485         }
486
487         /* Drop old destination information */
488         ast_free(channel->tech);
489         ast_free(channel->device);
490         ast_free(channel->assignedid1);
491         channel->assignedid1 = NULL;
492         ast_free(channel->assignedid2);
493         channel->assignedid2 = NULL;
494
495         /* Update the dial channel with the new destination information */
496         channel->tech = ast_strdup(tech);
497         channel->device = ast_strdup(device);
498         AST_LIST_UNLOCK(&dial->channels);
499
500         /* Drop the original channel */
501         channel->owner = NULL;
502
503         /* Finally give it a go... send it out into the world */
504         begin_dial_channel(channel, chan, chan ? 0 : 1, predial_string);
505
506         ast_channel_publish_dial_forward(chan, original, channel->owner, NULL, "CANCEL",
507                 ast_channel_call_forward(original));
508
509         ast_hangup(original);
510
511         return 0;
512 }
513
514 /*! \brief Helper function that finds the dialed channel based on owner */
515 static struct ast_dial_channel *find_relative_dial_channel(struct ast_dial *dial, struct ast_channel *owner)
516 {
517         struct ast_dial_channel *channel = NULL;
518
519         AST_LIST_LOCK(&dial->channels);
520         AST_LIST_TRAVERSE(&dial->channels, channel, list) {
521                 if (channel->owner == owner)
522                         break;
523         }
524         AST_LIST_UNLOCK(&dial->channels);
525
526         return channel;
527 }
528
529 static void set_state(struct ast_dial *dial, enum ast_dial_result state)
530 {
531         dial->state = state;
532
533         if (dial->state_callback)
534                 dial->state_callback(dial);
535 }
536
537 /*! \brief Helper function that handles control frames WITH owner */
538 static void handle_frame(struct ast_dial *dial, struct ast_dial_channel *channel, struct ast_frame *fr, struct ast_channel *chan)
539 {
540         if (fr->frametype == AST_FRAME_CONTROL) {
541                 switch (fr->subclass.integer) {
542                 case AST_CONTROL_ANSWER:
543                         ast_verb(3, "%s answered %s\n", ast_channel_name(channel->owner), ast_channel_name(chan));
544                         AST_LIST_LOCK(&dial->channels);
545                         AST_LIST_REMOVE(&dial->channels, channel, list);
546                         AST_LIST_INSERT_HEAD(&dial->channels, channel, list);
547                         AST_LIST_UNLOCK(&dial->channels);
548                         ast_channel_publish_dial(chan, channel->owner, channel->device, "ANSWER");
549                         set_state(dial, AST_DIAL_RESULT_ANSWERED);
550                         break;
551                 case AST_CONTROL_BUSY:
552                         ast_verb(3, "%s is busy\n", ast_channel_name(channel->owner));
553                         ast_channel_publish_dial(chan, channel->owner, channel->device, "BUSY");
554                         ast_hangup(channel->owner);
555                         channel->cause = AST_CAUSE_USER_BUSY;
556                         channel->owner = NULL;
557                         break;
558                 case AST_CONTROL_CONGESTION:
559                         ast_verb(3, "%s is circuit-busy\n", ast_channel_name(channel->owner));
560                         ast_channel_publish_dial(chan, channel->owner, channel->device, "CONGESTION");
561                         ast_hangup(channel->owner);
562                         channel->cause = AST_CAUSE_NORMAL_CIRCUIT_CONGESTION;
563                         channel->owner = NULL;
564                         break;
565                 case AST_CONTROL_INCOMPLETE:
566                         ast_verb(3, "%s dialed Incomplete extension %s\n", ast_channel_name(channel->owner), ast_channel_exten(channel->owner));
567                         ast_indicate(chan, AST_CONTROL_INCOMPLETE);
568                         break;
569                 case AST_CONTROL_RINGING:
570                         ast_verb(3, "%s is ringing\n", ast_channel_name(channel->owner));
571                         if (!dial->options[AST_DIAL_OPTION_MUSIC])
572                                 ast_indicate(chan, AST_CONTROL_RINGING);
573                         set_state(dial, AST_DIAL_RESULT_RINGING);
574                         break;
575                 case AST_CONTROL_PROGRESS:
576                         ast_verb(3, "%s is making progress, passing it to %s\n", ast_channel_name(channel->owner), ast_channel_name(chan));
577                         ast_indicate(chan, AST_CONTROL_PROGRESS);
578                         set_state(dial, AST_DIAL_RESULT_PROGRESS);
579                         break;
580                 case AST_CONTROL_VIDUPDATE:
581                         ast_verb(3, "%s requested a video update, passing it to %s\n", ast_channel_name(channel->owner), ast_channel_name(chan));
582                         ast_indicate(chan, AST_CONTROL_VIDUPDATE);
583                         break;
584                 case AST_CONTROL_SRCUPDATE:
585                         ast_verb(3, "%s requested a source update, passing it to %s\n", ast_channel_name(channel->owner), ast_channel_name(chan));
586                         ast_indicate(chan, AST_CONTROL_SRCUPDATE);
587                         break;
588                 case AST_CONTROL_CONNECTED_LINE:
589                         ast_verb(3, "%s connected line has changed, passing it to %s\n", ast_channel_name(channel->owner), ast_channel_name(chan));
590                         if (ast_channel_connected_line_sub(channel->owner, chan, fr, 1) &&
591                                 ast_channel_connected_line_macro(channel->owner, chan, fr, 1, 1)) {
592                                 ast_indicate_data(chan, AST_CONTROL_CONNECTED_LINE, fr->data.ptr, fr->datalen);
593                         }
594                         break;
595                 case AST_CONTROL_REDIRECTING:
596                         ast_verb(3, "%s redirecting info has changed, passing it to %s\n", ast_channel_name(channel->owner), ast_channel_name(chan));
597                         if (ast_channel_redirecting_sub(channel->owner, chan, fr, 1) &&
598                                 ast_channel_redirecting_macro(channel->owner, chan, fr, 1, 1)) {
599                                 ast_indicate_data(chan, AST_CONTROL_REDIRECTING, fr->data.ptr, fr->datalen);
600                         }
601                         break;
602                 case AST_CONTROL_PROCEEDING:
603                         ast_verb(3, "%s is proceeding, passing it to %s\n", ast_channel_name(channel->owner), ast_channel_name(chan));
604                         ast_indicate(chan, AST_CONTROL_PROCEEDING);
605                         set_state(dial, AST_DIAL_RESULT_PROCEEDING);
606                         break;
607                 case AST_CONTROL_HOLD:
608                         ast_verb(3, "Call on %s placed on hold\n", ast_channel_name(chan));
609                         ast_indicate_data(chan, AST_CONTROL_HOLD, fr->data.ptr, fr->datalen);
610                         break;
611                 case AST_CONTROL_UNHOLD:
612                         ast_verb(3, "Call on %s left from hold\n", ast_channel_name(chan));
613                         ast_indicate(chan, AST_CONTROL_UNHOLD);
614                         break;
615                 case AST_CONTROL_OFFHOOK:
616                 case AST_CONTROL_FLASH:
617                         break;
618                 case AST_CONTROL_PVT_CAUSE_CODE:
619                         ast_indicate_data(chan, AST_CONTROL_PVT_CAUSE_CODE, fr->data.ptr, fr->datalen);
620                         break;
621                 case -1:
622                         /* Prod the channel */
623                         ast_indicate(chan, -1);
624                         break;
625                 default:
626                         break;
627                 }
628         }
629 }
630
631 /*! \brief Helper function that handles control frames WITHOUT owner */
632 static void handle_frame_ownerless(struct ast_dial *dial, struct ast_dial_channel *channel, struct ast_frame *fr)
633 {
634         /* If we have no owner we can only update the state of the dial structure, so only look at control frames */
635         if (fr->frametype != AST_FRAME_CONTROL)
636                 return;
637
638         switch (fr->subclass.integer) {
639         case AST_CONTROL_ANSWER:
640                 ast_verb(3, "%s answered\n", ast_channel_name(channel->owner));
641                 AST_LIST_LOCK(&dial->channels);
642                 AST_LIST_REMOVE(&dial->channels, channel, list);
643                 AST_LIST_INSERT_HEAD(&dial->channels, channel, list);
644                 AST_LIST_UNLOCK(&dial->channels);
645                 ast_channel_publish_dial(NULL, channel->owner, channel->device, "ANSWER");
646                 set_state(dial, AST_DIAL_RESULT_ANSWERED);
647                 break;
648         case AST_CONTROL_BUSY:
649                 ast_verb(3, "%s is busy\n", ast_channel_name(channel->owner));
650                 ast_channel_publish_dial(NULL, channel->owner, channel->device, "BUSY");
651                 ast_hangup(channel->owner);
652                 channel->cause = AST_CAUSE_USER_BUSY;
653                 channel->owner = NULL;
654                 break;
655         case AST_CONTROL_CONGESTION:
656                 ast_verb(3, "%s is circuit-busy\n", ast_channel_name(channel->owner));
657                 ast_channel_publish_dial(NULL, channel->owner, channel->device, "CONGESTION");
658                 ast_hangup(channel->owner);
659                 channel->cause = AST_CAUSE_NORMAL_CIRCUIT_CONGESTION;
660                 channel->owner = NULL;
661                 break;
662         case AST_CONTROL_INCOMPLETE:
663                 /*
664                  * Nothing to do but abort the call since we have no
665                  * controlling channel to ask for more digits.
666                  */
667                 ast_verb(3, "%s dialed Incomplete extension %s\n",
668                         ast_channel_name(channel->owner), ast_channel_exten(channel->owner));
669                 ast_hangup(channel->owner);
670                 channel->cause = AST_CAUSE_UNALLOCATED;
671                 channel->owner = NULL;
672                 break;
673         case AST_CONTROL_RINGING:
674                 ast_verb(3, "%s is ringing\n", ast_channel_name(channel->owner));
675                 set_state(dial, AST_DIAL_RESULT_RINGING);
676                 break;
677         case AST_CONTROL_PROGRESS:
678                 ast_verb(3, "%s is making progress\n", ast_channel_name(channel->owner));
679                 set_state(dial, AST_DIAL_RESULT_PROGRESS);
680                 break;
681         case AST_CONTROL_PROCEEDING:
682                 ast_verb(3, "%s is proceeding\n", ast_channel_name(channel->owner));
683                 set_state(dial, AST_DIAL_RESULT_PROCEEDING);
684                 break;
685         default:
686                 break;
687         }
688 }
689
690 /*! \brief Helper function to handle when a timeout occurs on dialing attempt */
691 static int handle_timeout_trip(struct ast_dial *dial, struct timeval start)
692 {
693         struct ast_dial_channel *channel = NULL;
694         int diff = ast_tvdiff_ms(ast_tvnow(), start), lowest_timeout = -1, new_timeout = -1;
695
696         /* If there is no difference yet return the dial timeout so we can go again, we were likely interrupted */
697         if (!diff) {
698                 return dial->timeout;
699         }
700
701         /* If the global dial timeout tripped switch the state to timeout so our channel loop will drop every channel */
702         if (diff >= dial->timeout) {
703                 set_state(dial, AST_DIAL_RESULT_TIMEOUT);
704                 new_timeout = 0;
705         }
706
707         /* Go through dropping out channels that have met their timeout */
708         AST_LIST_TRAVERSE(&dial->channels, channel, list) {
709                 if (dial->state == AST_DIAL_RESULT_TIMEOUT || diff >= channel->timeout) {
710                         ast_hangup(channel->owner);
711                         channel->cause = AST_CAUSE_NO_ANSWER;
712                         channel->owner = NULL;
713                 } else if ((lowest_timeout == -1) || (lowest_timeout > channel->timeout)) {
714                         lowest_timeout = channel->timeout;
715                 }
716         }
717
718         /* Calculate the new timeout using the lowest timeout found */
719         if (lowest_timeout >= 0)
720                 new_timeout = lowest_timeout - diff;
721
722         return new_timeout;
723 }
724
725 const char *ast_hangup_cause_to_dial_status(int hangup_cause)
726 {
727         switch(hangup_cause) {
728         case AST_CAUSE_BUSY:
729                 return "BUSY";
730         case AST_CAUSE_CONGESTION:
731                 return "CONGESTION";
732         case AST_CAUSE_NO_ROUTE_DESTINATION:
733         case AST_CAUSE_UNREGISTERED:
734                 return "CHANUNAVAIL";
735         case AST_CAUSE_NO_ANSWER:
736         default:
737                 return "NOANSWER";
738         }
739 }
740
741 /*! \brief Helper function that basically keeps tabs on dialing attempts */
742 static enum ast_dial_result monitor_dial(struct ast_dial *dial, struct ast_channel *chan)
743 {
744         int timeout = -1;
745         struct ast_channel *cs[AST_MAX_WATCHERS], *who = NULL;
746         struct ast_dial_channel *channel = NULL;
747         struct answer_exec_struct *answer_exec = NULL;
748         struct timeval start;
749
750         set_state(dial, AST_DIAL_RESULT_TRYING);
751
752         /* If the "always indicate ringing" option is set, change state to ringing and indicate to the owner if present */
753         if (dial->options[AST_DIAL_OPTION_RINGING]) {
754                 set_state(dial, AST_DIAL_RESULT_RINGING);
755                 if (chan)
756                         ast_indicate(chan, AST_CONTROL_RINGING);
757         } else if (chan && dial->options[AST_DIAL_OPTION_MUSIC] &&
758                 !ast_strlen_zero(dial->options[AST_DIAL_OPTION_MUSIC])) {
759                 char *original_moh = ast_strdupa(ast_channel_musicclass(chan));
760                 ast_indicate(chan, -1);
761                 ast_channel_musicclass_set(chan, dial->options[AST_DIAL_OPTION_MUSIC]);
762                 ast_moh_start(chan, dial->options[AST_DIAL_OPTION_MUSIC], NULL);
763                 ast_channel_musicclass_set(chan, original_moh);
764         }
765
766         /* Record start time for timeout purposes */
767         start = ast_tvnow();
768
769         /* We actually figured out the maximum timeout we can do as they were added, so we can directly access the info */
770         timeout = dial->actual_timeout;
771
772         /* Go into an infinite loop while we are trying */
773         while ((dial->state != AST_DIAL_RESULT_UNANSWERED) && (dial->state != AST_DIAL_RESULT_ANSWERED) && (dial->state != AST_DIAL_RESULT_HANGUP) && (dial->state != AST_DIAL_RESULT_TIMEOUT)) {
774                 int pos = 0, count = 0;
775                 struct ast_frame *fr = NULL;
776
777                 /* Set up channel structure array */
778                 pos = count = 0;
779                 if (chan)
780                         cs[pos++] = chan;
781
782                 /* Add channels we are attempting to dial */
783                 AST_LIST_LOCK(&dial->channels);
784                 AST_LIST_TRAVERSE(&dial->channels, channel, list) {
785                         if (channel->owner) {
786                                 cs[pos++] = channel->owner;
787                                 count++;
788                         }
789                 }
790                 AST_LIST_UNLOCK(&dial->channels);
791
792                 /* If we have no outbound channels in progress, switch state to unanswered and stop */
793                 if (!count) {
794                         set_state(dial, AST_DIAL_RESULT_UNANSWERED);
795                         break;
796                 }
797
798                 /* Just to be safe... */
799                 if (dial->thread == AST_PTHREADT_STOP)
800                         break;
801
802                 /* Wait for frames from channels */
803                 who = ast_waitfor_n(cs, pos, &timeout);
804
805                 /* Check to see if our thread is being canceled */
806                 if (dial->thread == AST_PTHREADT_STOP)
807                         break;
808
809                 /* If the timeout no longer exists OR if we got no channel it basically means the timeout was tripped, so handle it */
810                 if (!timeout || !who) {
811                         timeout = handle_timeout_trip(dial, start);
812                         continue;
813                 }
814
815                 /* Find relative dial channel */
816                 if (!chan || !IS_CALLER(chan, who))
817                         channel = find_relative_dial_channel(dial, who);
818
819                 /* See if this channel has been forwarded elsewhere */
820                 if (!ast_strlen_zero(ast_channel_call_forward(who))) {
821                         handle_call_forward(dial, channel, chan);
822                         continue;
823                 }
824
825                 /* Attempt to read in a frame */
826                 if (!(fr = ast_read(who))) {
827                         /* If this is the caller then we switch state to hangup and stop */
828                         if (chan && IS_CALLER(chan, who)) {
829                                 set_state(dial, AST_DIAL_RESULT_HANGUP);
830                                 break;
831                         }
832                         if (chan)
833                                 ast_poll_channel_del(chan, channel->owner);
834                         ast_channel_publish_dial(chan, who, channel->device, ast_hangup_cause_to_dial_status(ast_channel_hangupcause(who)));
835                         ast_hangup(who);
836                         channel->owner = NULL;
837                         continue;
838                 }
839
840                 /* Process the frame */
841                 if (chan)
842                         handle_frame(dial, channel, fr, chan);
843                 else
844                         handle_frame_ownerless(dial, channel, fr);
845
846                 /* Free the received frame and start all over */
847                 ast_frfree(fr);
848         }
849
850         /* Do post-processing from loop */
851         if (dial->state == AST_DIAL_RESULT_ANSWERED) {
852                 /* Hangup everything except that which answered */
853                 AST_LIST_LOCK(&dial->channels);
854                 AST_LIST_TRAVERSE(&dial->channels, channel, list) {
855                         if (!channel->owner || channel->owner == who)
856                                 continue;
857                         if (chan)
858                                 ast_poll_channel_del(chan, channel->owner);
859                         ast_channel_publish_dial(chan, channel->owner, channel->device, "CANCEL");
860                         ast_hangup(channel->owner);
861                         channel->cause = AST_CAUSE_ANSWERED_ELSEWHERE;
862                         channel->owner = NULL;
863                 }
864                 AST_LIST_UNLOCK(&dial->channels);
865                 /* If ANSWER_EXEC is enabled as an option, execute application on answered channel */
866                 if ((channel = find_relative_dial_channel(dial, who)) && (answer_exec = FIND_RELATIVE_OPTION(dial, channel, AST_DIAL_OPTION_ANSWER_EXEC))) {
867                         channel->is_running_app = 1;
868                         answer_exec_run(dial, channel, answer_exec->app, answer_exec->args);
869                         channel->is_running_app = 0;
870                 }
871
872                 if (chan && dial->options[AST_DIAL_OPTION_MUSIC] &&
873                         !ast_strlen_zero(dial->options[AST_DIAL_OPTION_MUSIC])) {
874                         ast_moh_stop(chan);
875                 }
876         } else if (dial->state == AST_DIAL_RESULT_HANGUP) {
877                 /* Hangup everything */
878                 AST_LIST_LOCK(&dial->channels);
879                 AST_LIST_TRAVERSE(&dial->channels, channel, list) {
880                         if (!channel->owner)
881                                 continue;
882                         if (chan)
883                                 ast_poll_channel_del(chan, channel->owner);
884                         ast_channel_publish_dial(chan, channel->owner, channel->device, "CANCEL");
885                         ast_hangup(channel->owner);
886                         channel->cause = AST_CAUSE_NORMAL_CLEARING;
887                         channel->owner = NULL;
888                 }
889                 AST_LIST_UNLOCK(&dial->channels);
890         }
891
892         return dial->state;
893 }
894
895 /*! \brief Dial async thread function */
896 static void *async_dial(void *data)
897 {
898         struct ast_dial *dial = data;
899         if (dial->callid) {
900                 ast_callid_threadassoc_add(dial->callid);
901         }
902
903         /* This is really really simple... we basically pass monitor_dial a NULL owner and it changes it's behavior */
904         monitor_dial(dial, NULL);
905
906         return NULL;
907 }
908
909 /*! \brief Execute dialing synchronously or asynchronously
910  * \note Dials channels in a dial structure.
911  * \return Returns dial result code. (TRYING/INVALID/FAILED/ANSWERED/TIMEOUT/UNANSWERED).
912  */
913 enum ast_dial_result ast_dial_run(struct ast_dial *dial, struct ast_channel *chan, int async)
914 {
915         enum ast_dial_result res = AST_DIAL_RESULT_TRYING;
916
917         /* Ensure required arguments are passed */
918         if (!dial) {
919                 ast_debug(1, "invalid #1\n");
920                 return AST_DIAL_RESULT_INVALID;
921         }
922
923         /* If there are no channels to dial we can't very well try to dial them */
924         if (AST_LIST_EMPTY(&dial->channels)) {
925                 ast_debug(1, "invalid #2\n");
926                 return AST_DIAL_RESULT_INVALID;
927         }
928
929         /* Dial each requested channel */
930         if (!begin_dial(dial, chan, async))
931                 return AST_DIAL_RESULT_FAILED;
932
933         /* If we are running async spawn a thread and send it away... otherwise block here */
934         if (async) {
935                 /* reference be released at dial destruction if it isn't NULL */
936                 dial->callid = ast_read_threadstorage_callid();
937                 dial->state = AST_DIAL_RESULT_TRYING;
938                 /* Try to create a thread */
939                 if (ast_pthread_create(&dial->thread, NULL, async_dial, dial)) {
940                         /* Failed to create the thread - hangup all dialed channels and return failed */
941                         ast_dial_hangup(dial);
942                         res = AST_DIAL_RESULT_FAILED;
943                 }
944         } else {
945                 res = monitor_dial(dial, chan);
946         }
947
948         return res;
949 }
950
951 /*! \brief Return channel that answered
952  * \note Returns the Asterisk channel that answered
953  * \param dial Dialing structure
954  */
955 struct ast_channel *ast_dial_answered(struct ast_dial *dial)
956 {
957         if (!dial)
958                 return NULL;
959
960         return ((dial->state == AST_DIAL_RESULT_ANSWERED) ? AST_LIST_FIRST(&dial->channels)->owner : NULL);
961 }
962
963 /*! \brief Steal the channel that answered
964  * \note Returns the Asterisk channel that answered and removes it from the dialing structure
965  * \param dial Dialing structure
966  */
967 struct ast_channel *ast_dial_answered_steal(struct ast_dial *dial)
968 {
969         struct ast_channel *chan = NULL;
970
971         if (!dial)
972                 return NULL;
973
974         if (dial->state == AST_DIAL_RESULT_ANSWERED) {
975                 chan = AST_LIST_FIRST(&dial->channels)->owner;
976                 AST_LIST_FIRST(&dial->channels)->owner = NULL;
977         }
978
979         return chan;
980 }
981
982 /*! \brief Return state of dial
983  * \note Returns the state of the dial attempt
984  * \param dial Dialing structure
985  */
986 enum ast_dial_result ast_dial_state(struct ast_dial *dial)
987 {
988         return dial->state;
989 }
990
991 /*! \brief Cancel async thread
992  * \note Cancel a running async thread
993  * \param dial Dialing structure
994  */
995 enum ast_dial_result ast_dial_join(struct ast_dial *dial)
996 {
997         pthread_t thread;
998
999         /* If the dial structure is not running in async, return failed */
1000         if (dial->thread == AST_PTHREADT_NULL)
1001                 return AST_DIAL_RESULT_FAILED;
1002
1003         /* Record thread */
1004         thread = dial->thread;
1005
1006         /* Boom, commence locking */
1007         ast_mutex_lock(&dial->lock);
1008
1009         /* Stop the thread */
1010         dial->thread = AST_PTHREADT_STOP;
1011
1012         /* If the answered channel is running an application we have to soft hangup it, can't just poke the thread */
1013         AST_LIST_LOCK(&dial->channels);
1014         if (AST_LIST_FIRST(&dial->channels)->is_running_app) {
1015                 struct ast_channel *chan = AST_LIST_FIRST(&dial->channels)->owner;
1016                 if (chan) {
1017                         ast_channel_lock(chan);
1018                         ast_softhangup(chan, AST_SOFTHANGUP_EXPLICIT);
1019                         ast_channel_unlock(chan);
1020                 }
1021         } else {
1022                 /* Now we signal it with SIGURG so it will break out of it's waitfor */
1023                 pthread_kill(thread, SIGURG);
1024         }
1025         AST_LIST_UNLOCK(&dial->channels);
1026
1027         /* Yay done with it */
1028         ast_mutex_unlock(&dial->lock);
1029
1030         /* Finally wait for the thread to exit */
1031         pthread_join(thread, NULL);
1032
1033         /* Yay thread is all gone */
1034         dial->thread = AST_PTHREADT_NULL;
1035
1036         return dial->state;
1037 }
1038
1039 /*! \brief Hangup channels
1040  * \note Hangup all active channels
1041  * \param dial Dialing structure
1042  */
1043 void ast_dial_hangup(struct ast_dial *dial)
1044 {
1045         struct ast_dial_channel *channel = NULL;
1046
1047         if (!dial)
1048                 return;
1049
1050         AST_LIST_LOCK(&dial->channels);
1051         AST_LIST_TRAVERSE(&dial->channels, channel, list) {
1052                 ast_hangup(channel->owner);
1053                 channel->owner = NULL;
1054         }
1055         AST_LIST_UNLOCK(&dial->channels);
1056
1057         return;
1058 }
1059
1060 /*! \brief Destroys a dialing structure
1061  * \note Destroys (free's) the given ast_dial structure
1062  * \param dial Dialing structure to free
1063  * \return Returns 0 on success, -1 on failure
1064  */
1065 int ast_dial_destroy(struct ast_dial *dial)
1066 {
1067         int i = 0;
1068         struct ast_dial_channel *channel = NULL;
1069
1070         if (!dial)
1071                 return -1;
1072
1073         /* Hangup and deallocate all the dialed channels */
1074         AST_LIST_LOCK(&dial->channels);
1075         AST_LIST_TRAVERSE_SAFE_BEGIN(&dial->channels, channel, list) {
1076                 /* Disable any enabled options */
1077                 for (i = 0; i < AST_DIAL_OPTION_MAX; i++) {
1078                         if (!channel->options[i])
1079                                 continue;
1080                         if (option_types[i].disable)
1081                                 option_types[i].disable(channel->options[i]);
1082                         channel->options[i] = NULL;
1083                 }
1084
1085                 /* Hang up channel if need be */
1086                 ast_hangup(channel->owner);
1087                 channel->owner = NULL;
1088
1089                 /* Free structure */
1090                 ast_free(channel->tech);
1091                 ast_free(channel->device);
1092                 ast_free(channel->assignedid1);
1093                 ast_free(channel->assignedid2);
1094
1095                 AST_LIST_REMOVE_CURRENT(list);
1096                 ast_free(channel);
1097         }
1098         AST_LIST_TRAVERSE_SAFE_END;
1099         AST_LIST_UNLOCK(&dial->channels);
1100
1101         /* Disable any enabled options globally */
1102         for (i = 0; i < AST_DIAL_OPTION_MAX; i++) {
1103                 if (!dial->options[i])
1104                         continue;
1105                 if (option_types[i].disable)
1106                         option_types[i].disable(dial->options[i]);
1107                 dial->options[i] = NULL;
1108         }
1109
1110         /* Lock be gone! */
1111         ast_mutex_destroy(&dial->lock);
1112
1113         /* Get rid of the reference to the ast_callid */
1114         if (dial->callid) {
1115                 ast_callid_unref(dial->callid);
1116         }
1117
1118         /* Free structure */
1119         ast_free(dial);
1120
1121         return 0;
1122 }
1123
1124 /*! \brief Enables an option globally
1125  * \param dial Dial structure to enable option on
1126  * \param option Option to enable
1127  * \param data Data to pass to this option (not always needed)
1128  * \return Returns 0 on success, -1 on failure
1129  */
1130 int ast_dial_option_global_enable(struct ast_dial *dial, enum ast_dial_option option, void *data)
1131 {
1132         /* If the option is already enabled, return failure */
1133         if (dial->options[option])
1134                 return -1;
1135
1136         /* Execute enable callback if it exists, if not simply make sure the value is set */
1137         if (option_types[option].enable)
1138                 dial->options[option] = option_types[option].enable(data);
1139         else
1140                 dial->options[option] = (void*)1;
1141
1142         return 0;
1143 }
1144
1145 /*! \brief Helper function for finding a channel in a dial structure based on number
1146  */
1147 static struct ast_dial_channel *find_dial_channel(struct ast_dial *dial, int num)
1148 {
1149         struct ast_dial_channel *channel = AST_LIST_LAST(&dial->channels);
1150
1151         /* We can try to predict programmer behavior, the last channel they added is probably the one they wanted to modify */
1152         if (channel->num == num)
1153                 return channel;
1154
1155         /* Hrm not at the end... looking through the list it is! */
1156         AST_LIST_LOCK(&dial->channels);
1157         AST_LIST_TRAVERSE(&dial->channels, channel, list) {
1158                 if (channel->num == num)
1159                         break;
1160         }
1161         AST_LIST_UNLOCK(&dial->channels);
1162
1163         return channel;
1164 }
1165
1166 /*! \brief Enables an option per channel
1167  * \param dial Dial structure
1168  * \param num Channel number to enable option on
1169  * \param option Option to enable
1170  * \param data Data to pass to this option (not always needed)
1171  * \return Returns 0 on success, -1 on failure
1172  */
1173 int ast_dial_option_enable(struct ast_dial *dial, int num, enum ast_dial_option option, void *data)
1174 {
1175         struct ast_dial_channel *channel = NULL;
1176
1177         /* Ensure we have required arguments */
1178         if (!dial || AST_LIST_EMPTY(&dial->channels))
1179                 return -1;
1180
1181         if (!(channel = find_dial_channel(dial, num)))
1182                 return -1;
1183
1184         /* If the option is already enabled, return failure */
1185         if (channel->options[option])
1186                 return -1;
1187
1188         /* Execute enable callback if it exists, if not simply make sure the value is set */
1189         if (option_types[option].enable)
1190                 channel->options[option] = option_types[option].enable(data);
1191         else
1192                 channel->options[option] = (void*)1;
1193
1194         return 0;
1195 }
1196
1197 /*! \brief Disables an option globally
1198  * \param dial Dial structure to disable option on
1199  * \param option Option to disable
1200  * \return Returns 0 on success, -1 on failure
1201  */
1202 int ast_dial_option_global_disable(struct ast_dial *dial, enum ast_dial_option option)
1203 {
1204         /* If the option is not enabled, return failure */
1205         if (!dial->options[option]) {
1206                 return -1;
1207         }
1208
1209         /* Execute callback of option to disable if it exists */
1210         if (option_types[option].disable)
1211                 option_types[option].disable(dial->options[option]);
1212
1213         /* Finally disable option on the structure */
1214         dial->options[option] = NULL;
1215
1216         return 0;
1217 }
1218
1219 /*! \brief Disables an option per channel
1220  * \param dial Dial structure
1221  * \param num Channel number to disable option on
1222  * \param option Option to disable
1223  * \return Returns 0 on success, -1 on failure
1224  */
1225 int ast_dial_option_disable(struct ast_dial *dial, int num, enum ast_dial_option option)
1226 {
1227         struct ast_dial_channel *channel = NULL;
1228
1229         /* Ensure we have required arguments */
1230         if (!dial || AST_LIST_EMPTY(&dial->channels))
1231                 return -1;
1232
1233         if (!(channel = find_dial_channel(dial, num)))
1234                 return -1;
1235
1236         /* If the option is not enabled, return failure */
1237         if (!channel->options[option])
1238                 return -1;
1239
1240         /* Execute callback of option to disable it if it exists */
1241         if (option_types[option].disable)
1242                 option_types[option].disable(channel->options[option]);
1243
1244         /* Finally disable the option on the structure */
1245         channel->options[option] = NULL;
1246
1247         return 0;
1248 }
1249
1250 int ast_dial_reason(struct ast_dial *dial, int num)
1251 {
1252         struct ast_dial_channel *channel;
1253
1254         if (!dial || AST_LIST_EMPTY(&dial->channels) || !(channel = find_dial_channel(dial, num))) {
1255                 return -1;
1256         }
1257
1258         return channel->cause;
1259 }
1260
1261 struct ast_channel *ast_dial_get_channel(struct ast_dial *dial, int num)
1262 {
1263         struct ast_dial_channel *channel;
1264
1265         if (!dial || AST_LIST_EMPTY(&dial->channels) || !(channel = find_dial_channel(dial, num))) {
1266                 return NULL;
1267         }
1268
1269         return channel->owner;
1270 }
1271
1272 void ast_dial_set_state_callback(struct ast_dial *dial, ast_dial_state_callback callback)
1273 {
1274         dial->state_callback = callback;
1275 }
1276
1277 void ast_dial_set_user_data(struct ast_dial *dial, void *user_data)
1278 {
1279         dial->user_data = user_data;
1280 }
1281
1282 void *ast_dial_get_user_data(struct ast_dial *dial)
1283 {
1284         return dial->user_data;
1285 }
1286
1287 /*! \brief Set the maximum time (globally) allowed for trying to ring phones
1288  * \param dial The dial structure to apply the time limit to
1289  * \param timeout Maximum time allowed
1290  * \return nothing
1291  */
1292 void ast_dial_set_global_timeout(struct ast_dial *dial, int timeout)
1293 {
1294         dial->timeout = timeout;
1295
1296         if (dial->timeout > 0 && (dial->actual_timeout > dial->timeout || dial->actual_timeout == -1))
1297                 dial->actual_timeout = dial->timeout;
1298
1299         return;
1300 }
1301
1302 /*! \brief Set the maximum time (per channel) allowed for trying to ring the phone
1303  * \param dial The dial structure the channel belongs to
1304  * \param num Channel number to set timeout on
1305  * \param timeout Maximum time allowed
1306  * \return nothing
1307  */
1308 void ast_dial_set_timeout(struct ast_dial *dial, int num, int timeout)
1309 {
1310         struct ast_dial_channel *channel = NULL;
1311
1312         if (!(channel = find_dial_channel(dial, num)))
1313                 return;
1314
1315         channel->timeout = timeout;
1316
1317         if (channel->timeout > 0 && (dial->actual_timeout > channel->timeout || dial->actual_timeout == -1))
1318                 dial->actual_timeout = channel->timeout;
1319
1320         return;
1321 }