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