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