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