PJPROJECT logging: Made easier to get available logging levels.
[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                 if (chan) {
483                         ast_poll_channel_add(chan, channel->owner);
484                 }
485                 ast_channel_publish_dial(async ? NULL : chan, channel->owner, channel->device, NULL);
486                 res = 1;
487                 ast_verb(3, "Called %s\n", numsubst);
488         }
489
490         return res;
491 }
492
493 /*! \brief Helper function that does the beginning dialing per dial structure */
494 static int begin_dial(struct ast_dial *dial, struct ast_channel *chan, int async)
495 {
496         struct ast_dial_channel *channel = NULL;
497         int success = 0;
498         char *predial_string = dial->options[AST_DIAL_OPTION_PREDIAL];
499
500         /* Iterate through channel list, requesting and calling each one */
501         AST_LIST_LOCK(&dial->channels);
502         AST_LIST_TRAVERSE(&dial->channels, channel, list) {
503                 success += begin_dial_channel(channel, chan, async, predial_string, NULL);
504         }
505         AST_LIST_UNLOCK(&dial->channels);
506
507         /* If number of failures matches the number of channels, then this truly failed */
508         return success;
509 }
510
511 /*! \brief Helper function to handle channels that have been call forwarded */
512 static int handle_call_forward(struct ast_dial *dial, struct ast_dial_channel *channel, struct ast_channel *chan)
513 {
514         struct ast_channel *original = channel->owner;
515         char *tmp = ast_strdupa(ast_channel_call_forward(channel->owner));
516         char *tech = "Local", *device = tmp, *stuff;
517         char *predial_string = dial->options[AST_DIAL_OPTION_PREDIAL];
518
519         /* If call forwarding is disabled just drop the original channel and don't attempt to dial the new one */
520         if (FIND_RELATIVE_OPTION(dial, channel, AST_DIAL_OPTION_DISABLE_CALL_FORWARDING)) {
521                 ast_hangup(original);
522                 channel->owner = NULL;
523                 return 0;
524         }
525
526         /* Figure out the new destination */
527         if ((stuff = strchr(tmp, '/'))) {
528                 *stuff++ = '\0';
529                 tech = tmp;
530                 device = stuff;
531         } else {
532                 const char *forward_context;
533                 char destination[AST_MAX_CONTEXT + AST_MAX_EXTENSION + 1];
534
535                 ast_channel_lock(original);
536                 forward_context = pbx_builtin_getvar_helper(original, "FORWARD_CONTEXT");
537                 snprintf(destination, sizeof(destination), "%s@%s", tmp, S_OR(forward_context, ast_channel_context(original)));
538                 ast_channel_unlock(original);
539                 device = ast_strdupa(destination);
540         }
541
542         /* Drop old destination information */
543         ast_free(channel->tech);
544         ast_free(channel->device);
545         ast_free(channel->assignedid1);
546         channel->assignedid1 = NULL;
547         ast_free(channel->assignedid2);
548         channel->assignedid2 = NULL;
549
550         /* Update the dial channel with the new destination information */
551         channel->tech = ast_strdup(tech);
552         channel->device = ast_strdup(device);
553         AST_LIST_UNLOCK(&dial->channels);
554
555         /* Drop the original channel */
556         channel->owner = NULL;
557
558         /* Finally give it a go... send it out into the world */
559         begin_dial_channel(channel, chan, chan ? 0 : 1, predial_string, original);
560
561         ast_channel_publish_dial_forward(chan, original, channel->owner, NULL, "CANCEL",
562                 ast_channel_call_forward(original));
563
564         ast_hangup(original);
565
566         return 0;
567 }
568
569 /*! \brief Helper function that finds the dialed channel based on owner */
570 static struct ast_dial_channel *find_relative_dial_channel(struct ast_dial *dial, struct ast_channel *owner)
571 {
572         struct ast_dial_channel *channel = NULL;
573
574         AST_LIST_LOCK(&dial->channels);
575         AST_LIST_TRAVERSE(&dial->channels, channel, list) {
576                 if (channel->owner == owner)
577                         break;
578         }
579         AST_LIST_UNLOCK(&dial->channels);
580
581         return channel;
582 }
583
584 static void set_state(struct ast_dial *dial, enum ast_dial_result state)
585 {
586         dial->state = state;
587
588         if (dial->state_callback)
589                 dial->state_callback(dial);
590 }
591
592 /*! \brief Helper function that handles frames */
593 static void handle_frame(struct ast_dial *dial, struct ast_dial_channel *channel, struct ast_frame *fr, struct ast_channel *chan)
594 {
595         if (fr->frametype == AST_FRAME_CONTROL) {
596                 switch (fr->subclass.integer) {
597                 case AST_CONTROL_ANSWER:
598                         if (chan) {
599                                 ast_verb(3, "%s answered %s\n", ast_channel_name(channel->owner), ast_channel_name(chan));
600                         } else {
601                                 ast_verb(3, "%s answered\n", ast_channel_name(channel->owner));
602                         }
603                         AST_LIST_LOCK(&dial->channels);
604                         AST_LIST_REMOVE(&dial->channels, channel, list);
605                         AST_LIST_INSERT_HEAD(&dial->channels, channel, list);
606                         AST_LIST_UNLOCK(&dial->channels);
607                         ast_channel_publish_dial(chan, channel->owner, channel->device, "ANSWER");
608                         set_state(dial, AST_DIAL_RESULT_ANSWERED);
609                         break;
610                 case AST_CONTROL_BUSY:
611                         ast_verb(3, "%s is busy\n", ast_channel_name(channel->owner));
612                         ast_channel_publish_dial(chan, channel->owner, channel->device, "BUSY");
613                         ast_hangup(channel->owner);
614                         channel->cause = AST_CAUSE_USER_BUSY;
615                         channel->owner = NULL;
616                         break;
617                 case AST_CONTROL_CONGESTION:
618                         ast_verb(3, "%s is circuit-busy\n", ast_channel_name(channel->owner));
619                         ast_channel_publish_dial(chan, channel->owner, channel->device, "CONGESTION");
620                         ast_hangup(channel->owner);
621                         channel->cause = AST_CAUSE_NORMAL_CIRCUIT_CONGESTION;
622                         channel->owner = NULL;
623                         break;
624                 case AST_CONTROL_INCOMPLETE:
625                         ast_verb(3, "%s dialed Incomplete extension %s\n", ast_channel_name(channel->owner), ast_channel_exten(channel->owner));
626                         if (chan) {
627                                 ast_indicate(chan, AST_CONTROL_INCOMPLETE);
628                         } else {
629                                 ast_hangup(channel->owner);
630                                 channel->cause = AST_CAUSE_UNALLOCATED;
631                                 channel->owner = NULL;
632                         }
633                         break;
634                 case AST_CONTROL_RINGING:
635                         ast_verb(3, "%s is ringing\n", ast_channel_name(channel->owner));
636                         ast_channel_publish_dial(chan, channel->owner, channel->device, "RINGING");
637                         if (chan && !dial->options[AST_DIAL_OPTION_MUSIC])
638                                 ast_indicate(chan, AST_CONTROL_RINGING);
639                         set_state(dial, AST_DIAL_RESULT_RINGING);
640                         break;
641                 case AST_CONTROL_PROGRESS:
642                         ast_channel_publish_dial(chan, channel->owner, channel->device, "PROGRESS");
643                         if (chan) {
644                                 ast_verb(3, "%s is making progress, passing it to %s\n", ast_channel_name(channel->owner), ast_channel_name(chan));
645                                 ast_indicate(chan, AST_CONTROL_PROGRESS);
646                         } else {
647                                 ast_verb(3, "%s is making progress\n", ast_channel_name(channel->owner));
648                         }
649                         set_state(dial, AST_DIAL_RESULT_PROGRESS);
650                         break;
651                 case AST_CONTROL_VIDUPDATE:
652                         if (!chan) {
653                                 break;
654                         }
655                         ast_verb(3, "%s requested a video update, passing it to %s\n", ast_channel_name(channel->owner), ast_channel_name(chan));
656                         ast_indicate(chan, AST_CONTROL_VIDUPDATE);
657                         break;
658                 case AST_CONTROL_SRCUPDATE:
659                         if (!chan) {
660                                 break;
661                         }
662                         ast_verb(3, "%s requested a source update, passing it to %s\n", ast_channel_name(channel->owner), ast_channel_name(chan));
663                         ast_indicate(chan, AST_CONTROL_SRCUPDATE);
664                         break;
665                 case AST_CONTROL_CONNECTED_LINE:
666                         if (!chan) {
667                                 break;
668                         }
669                         ast_verb(3, "%s connected line has changed, passing it to %s\n", ast_channel_name(channel->owner), ast_channel_name(chan));
670                         if (ast_channel_connected_line_sub(channel->owner, chan, fr, 1) &&
671                                 ast_channel_connected_line_macro(channel->owner, chan, fr, 1, 1)) {
672                                 ast_indicate_data(chan, AST_CONTROL_CONNECTED_LINE, fr->data.ptr, fr->datalen);
673                         }
674                         break;
675                 case AST_CONTROL_REDIRECTING:
676                         if (!chan) {
677                                 break;
678                         }
679                         ast_verb(3, "%s redirecting info has changed, passing it to %s\n", ast_channel_name(channel->owner), ast_channel_name(chan));
680                         if (ast_channel_redirecting_sub(channel->owner, chan, fr, 1) &&
681                                 ast_channel_redirecting_macro(channel->owner, chan, fr, 1, 1)) {
682                                 ast_indicate_data(chan, AST_CONTROL_REDIRECTING, fr->data.ptr, fr->datalen);
683                         }
684                         break;
685                 case AST_CONTROL_PROCEEDING:
686                         ast_channel_publish_dial(chan, channel->owner, channel->device, "PROCEEDING");
687                         if (chan) {
688                                 ast_verb(3, "%s is proceeding, passing it to %s\n", ast_channel_name(channel->owner), ast_channel_name(chan));
689                                 ast_indicate(chan, AST_CONTROL_PROCEEDING);
690                         } else {
691                                 ast_verb(3, "%s is proceeding\n", ast_channel_name(channel->owner));
692                         }
693                         set_state(dial, AST_DIAL_RESULT_PROCEEDING);
694                         break;
695                 case AST_CONTROL_HOLD:
696                         if (!chan) {
697                                 break;
698                         }
699                         ast_verb(3, "Call on %s placed on hold\n", ast_channel_name(chan));
700                         ast_indicate_data(chan, AST_CONTROL_HOLD, fr->data.ptr, fr->datalen);
701                         break;
702                 case AST_CONTROL_UNHOLD:
703                         if (!chan) {
704                                 break;
705                         }
706                         ast_verb(3, "Call on %s left from hold\n", ast_channel_name(chan));
707                         ast_indicate(chan, AST_CONTROL_UNHOLD);
708                         break;
709                 case AST_CONTROL_OFFHOOK:
710                 case AST_CONTROL_FLASH:
711                         break;
712                 case AST_CONTROL_PVT_CAUSE_CODE:
713                         if (chan) {
714                                 ast_indicate_data(chan, AST_CONTROL_PVT_CAUSE_CODE, fr->data.ptr, fr->datalen);
715                         }
716                         break;
717                 case -1:
718                         if (chan) {
719                                 /* Prod the channel */
720                                 ast_indicate(chan, -1);
721                         }
722                         break;
723                 default:
724                         break;
725                 }
726         }
727 }
728
729 /*! \brief Helper function to handle when a timeout occurs on dialing attempt */
730 static int handle_timeout_trip(struct ast_dial *dial, struct timeval start)
731 {
732         struct ast_dial_channel *channel = NULL;
733         int diff = ast_tvdiff_ms(ast_tvnow(), start), lowest_timeout = -1, new_timeout = -1;
734
735         /* If there is no difference yet return the dial timeout so we can go again, we were likely interrupted */
736         if (!diff) {
737                 return dial->timeout;
738         }
739
740         /* If the global dial timeout tripped switch the state to timeout so our channel loop will drop every channel */
741         if (diff >= dial->timeout) {
742                 set_state(dial, AST_DIAL_RESULT_TIMEOUT);
743                 new_timeout = 0;
744         }
745
746         /* Go through dropping out channels that have met their timeout */
747         AST_LIST_TRAVERSE(&dial->channels, channel, list) {
748                 if (dial->state == AST_DIAL_RESULT_TIMEOUT || diff >= channel->timeout) {
749                         ast_hangup(channel->owner);
750                         channel->cause = AST_CAUSE_NO_ANSWER;
751                         channel->owner = NULL;
752                 } else if ((lowest_timeout == -1) || (lowest_timeout > channel->timeout)) {
753                         lowest_timeout = channel->timeout;
754                 }
755         }
756
757         /* Calculate the new timeout using the lowest timeout found */
758         if (lowest_timeout >= 0)
759                 new_timeout = lowest_timeout - diff;
760
761         return new_timeout;
762 }
763
764 const char *ast_hangup_cause_to_dial_status(int hangup_cause)
765 {
766         switch(hangup_cause) {
767         case AST_CAUSE_BUSY:
768                 return "BUSY";
769         case AST_CAUSE_CONGESTION:
770                 return "CONGESTION";
771         case AST_CAUSE_NO_ROUTE_DESTINATION:
772         case AST_CAUSE_UNREGISTERED:
773                 return "CHANUNAVAIL";
774         case AST_CAUSE_NO_ANSWER:
775         default:
776                 return "NOANSWER";
777         }
778 }
779
780 /*! \brief Helper function that basically keeps tabs on dialing attempts */
781 static enum ast_dial_result monitor_dial(struct ast_dial *dial, struct ast_channel *chan)
782 {
783         int timeout = -1;
784         struct ast_channel *cs[AST_MAX_WATCHERS], *who = NULL;
785         struct ast_dial_channel *channel = NULL;
786         struct answer_exec_struct *answer_exec = NULL;
787         struct timeval start;
788
789         set_state(dial, AST_DIAL_RESULT_TRYING);
790
791         /* If the "always indicate ringing" option is set, change state to ringing and indicate to the owner if present */
792         if (dial->options[AST_DIAL_OPTION_RINGING]) {
793                 set_state(dial, AST_DIAL_RESULT_RINGING);
794                 if (chan)
795                         ast_indicate(chan, AST_CONTROL_RINGING);
796         } else if (chan && dial->options[AST_DIAL_OPTION_MUSIC] &&
797                 !ast_strlen_zero(dial->options[AST_DIAL_OPTION_MUSIC])) {
798                 char *original_moh = ast_strdupa(ast_channel_musicclass(chan));
799                 ast_indicate(chan, -1);
800                 ast_channel_musicclass_set(chan, dial->options[AST_DIAL_OPTION_MUSIC]);
801                 ast_moh_start(chan, dial->options[AST_DIAL_OPTION_MUSIC], NULL);
802                 ast_channel_musicclass_set(chan, original_moh);
803         }
804
805         /* Record start time for timeout purposes */
806         start = ast_tvnow();
807
808         /* We actually figured out the maximum timeout we can do as they were added, so we can directly access the info */
809         timeout = dial->actual_timeout;
810
811         /* Go into an infinite loop while we are trying */
812         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)) {
813                 int pos = 0, count = 0;
814                 struct ast_frame *fr = NULL;
815
816                 /* Set up channel structure array */
817                 pos = count = 0;
818                 if (chan)
819                         cs[pos++] = chan;
820
821                 /* Add channels we are attempting to dial */
822                 AST_LIST_LOCK(&dial->channels);
823                 AST_LIST_TRAVERSE(&dial->channels, channel, list) {
824                         if (channel->owner) {
825                                 cs[pos++] = channel->owner;
826                                 count++;
827                         }
828                 }
829                 AST_LIST_UNLOCK(&dial->channels);
830
831                 /* If we have no outbound channels in progress, switch state to unanswered and stop */
832                 if (!count) {
833                         set_state(dial, AST_DIAL_RESULT_UNANSWERED);
834                         break;
835                 }
836
837                 /* Just to be safe... */
838                 if (dial->thread == AST_PTHREADT_STOP)
839                         break;
840
841                 /* Wait for frames from channels */
842                 who = ast_waitfor_n(cs, pos, &timeout);
843
844                 /* Check to see if our thread is being canceled */
845                 if (dial->thread == AST_PTHREADT_STOP)
846                         break;
847
848                 /* If the timeout no longer exists OR if we got no channel it basically means the timeout was tripped, so handle it */
849                 if (!timeout || !who) {
850                         timeout = handle_timeout_trip(dial, start);
851                         continue;
852                 }
853
854                 /* Find relative dial channel */
855                 if (!chan || !IS_CALLER(chan, who))
856                         channel = find_relative_dial_channel(dial, who);
857
858                 /* See if this channel has been forwarded elsewhere */
859                 if (!ast_strlen_zero(ast_channel_call_forward(who))) {
860                         handle_call_forward(dial, channel, chan);
861                         continue;
862                 }
863
864                 /* Attempt to read in a frame */
865                 if (!(fr = ast_read(who))) {
866                         /* If this is the caller then we switch state to hangup and stop */
867                         if (chan && IS_CALLER(chan, who)) {
868                                 set_state(dial, AST_DIAL_RESULT_HANGUP);
869                                 break;
870                         }
871                         if (chan)
872                                 ast_poll_channel_del(chan, channel->owner);
873                         ast_channel_publish_dial(chan, who, channel->device, ast_hangup_cause_to_dial_status(ast_channel_hangupcause(who)));
874                         ast_hangup(who);
875                         channel->owner = NULL;
876                         continue;
877                 }
878
879                 /* Process the frame */
880                 handle_frame(dial, channel, fr, chan);
881
882                 /* Free the received frame and start all over */
883                 ast_frfree(fr);
884         }
885
886         /* Do post-processing from loop */
887         if (dial->state == AST_DIAL_RESULT_ANSWERED) {
888                 /* Hangup everything except that which answered */
889                 AST_LIST_LOCK(&dial->channels);
890                 AST_LIST_TRAVERSE(&dial->channels, channel, list) {
891                         if (!channel->owner || channel->owner == who)
892                                 continue;
893                         if (chan)
894                                 ast_poll_channel_del(chan, channel->owner);
895                         ast_channel_publish_dial(chan, channel->owner, channel->device, "CANCEL");
896                         ast_hangup(channel->owner);
897                         channel->cause = AST_CAUSE_ANSWERED_ELSEWHERE;
898                         channel->owner = NULL;
899                 }
900                 AST_LIST_UNLOCK(&dial->channels);
901                 /* If ANSWER_EXEC is enabled as an option, execute application on answered channel */
902                 if ((channel = find_relative_dial_channel(dial, who)) && (answer_exec = FIND_RELATIVE_OPTION(dial, channel, AST_DIAL_OPTION_ANSWER_EXEC))) {
903                         channel->is_running_app = 1;
904                         answer_exec_run(dial, channel, answer_exec->app, answer_exec->args);
905                         channel->is_running_app = 0;
906                 }
907
908                 if (chan && dial->options[AST_DIAL_OPTION_MUSIC] &&
909                         !ast_strlen_zero(dial->options[AST_DIAL_OPTION_MUSIC])) {
910                         ast_moh_stop(chan);
911                 }
912         } else if (dial->state == AST_DIAL_RESULT_HANGUP) {
913                 /* Hangup everything */
914                 AST_LIST_LOCK(&dial->channels);
915                 AST_LIST_TRAVERSE(&dial->channels, channel, list) {
916                         if (!channel->owner)
917                                 continue;
918                         if (chan)
919                                 ast_poll_channel_del(chan, channel->owner);
920                         ast_channel_publish_dial(chan, channel->owner, channel->device, "CANCEL");
921                         ast_hangup(channel->owner);
922                         channel->cause = AST_CAUSE_NORMAL_CLEARING;
923                         channel->owner = NULL;
924                 }
925                 AST_LIST_UNLOCK(&dial->channels);
926         }
927
928         if (dial->options[AST_DIAL_OPTION_SELF_DESTROY]) {
929                 enum ast_dial_result state = dial->state;
930
931                 ast_dial_destroy(dial);
932                 return state;
933         }
934
935         return dial->state;
936 }
937
938 /*! \brief Dial async thread function */
939 static void *async_dial(void *data)
940 {
941         struct ast_dial *dial = data;
942         if (dial->callid) {
943                 ast_callid_threadassoc_add(dial->callid);
944         }
945
946         /* This is really really simple... we basically pass monitor_dial a NULL owner and it changes it's behavior */
947         monitor_dial(dial, NULL);
948
949         return NULL;
950 }
951
952 /*! \brief Execute dialing synchronously or asynchronously
953  * \note Dials channels in a dial structure.
954  * \return Returns dial result code. (TRYING/INVALID/FAILED/ANSWERED/TIMEOUT/UNANSWERED).
955  */
956 enum ast_dial_result ast_dial_run(struct ast_dial *dial, struct ast_channel *chan, int async)
957 {
958         enum ast_dial_result res = AST_DIAL_RESULT_TRYING;
959
960         /* Ensure required arguments are passed */
961         if (!dial) {
962                 ast_debug(1, "invalid #1\n");
963                 return AST_DIAL_RESULT_INVALID;
964         }
965
966         /* If there are no channels to dial we can't very well try to dial them */
967         if (AST_LIST_EMPTY(&dial->channels)) {
968                 ast_debug(1, "invalid #2\n");
969                 return AST_DIAL_RESULT_INVALID;
970         }
971
972         /* Dial each requested channel */
973         if (!begin_dial(dial, chan, async))
974                 return AST_DIAL_RESULT_FAILED;
975
976         /* If we are running async spawn a thread and send it away... otherwise block here */
977         if (async) {
978                 /* reference be released at dial destruction if it isn't NULL */
979                 dial->callid = ast_read_threadstorage_callid();
980                 dial->state = AST_DIAL_RESULT_TRYING;
981                 /* Try to create a thread */
982                 if (ast_pthread_create(&dial->thread, NULL, async_dial, dial)) {
983                         /* Failed to create the thread - hangup all dialed channels and return failed */
984                         ast_dial_hangup(dial);
985                         res = AST_DIAL_RESULT_FAILED;
986                 }
987         } else {
988                 res = monitor_dial(dial, chan);
989         }
990
991         return res;
992 }
993
994 /*! \brief Return channel that answered
995  * \note Returns the Asterisk channel that answered
996  * \param dial Dialing structure
997  */
998 struct ast_channel *ast_dial_answered(struct ast_dial *dial)
999 {
1000         if (!dial)
1001                 return NULL;
1002
1003         return ((dial->state == AST_DIAL_RESULT_ANSWERED) ? AST_LIST_FIRST(&dial->channels)->owner : NULL);
1004 }
1005
1006 /*! \brief Steal the channel that answered
1007  * \note Returns the Asterisk channel that answered and removes it from the dialing structure
1008  * \param dial Dialing structure
1009  */
1010 struct ast_channel *ast_dial_answered_steal(struct ast_dial *dial)
1011 {
1012         struct ast_channel *chan = NULL;
1013
1014         if (!dial)
1015                 return NULL;
1016
1017         if (dial->state == AST_DIAL_RESULT_ANSWERED) {
1018                 chan = AST_LIST_FIRST(&dial->channels)->owner;
1019                 AST_LIST_FIRST(&dial->channels)->owner = NULL;
1020         }
1021
1022         return chan;
1023 }
1024
1025 /*! \brief Return state of dial
1026  * \note Returns the state of the dial attempt
1027  * \param dial Dialing structure
1028  */
1029 enum ast_dial_result ast_dial_state(struct ast_dial *dial)
1030 {
1031         return dial->state;
1032 }
1033
1034 /*! \brief Cancel async thread
1035  * \note Cancel a running async thread
1036  * \param dial Dialing structure
1037  */
1038 enum ast_dial_result ast_dial_join(struct ast_dial *dial)
1039 {
1040         pthread_t thread;
1041
1042         /* If the dial structure is not running in async, return failed */
1043         if (dial->thread == AST_PTHREADT_NULL)
1044                 return AST_DIAL_RESULT_FAILED;
1045
1046         /* Record thread */
1047         thread = dial->thread;
1048
1049         /* Boom, commence locking */
1050         ast_mutex_lock(&dial->lock);
1051
1052         /* Stop the thread */
1053         dial->thread = AST_PTHREADT_STOP;
1054
1055         /* If the answered channel is running an application we have to soft hangup it, can't just poke the thread */
1056         AST_LIST_LOCK(&dial->channels);
1057         if (AST_LIST_FIRST(&dial->channels)->is_running_app) {
1058                 struct ast_channel *chan = AST_LIST_FIRST(&dial->channels)->owner;
1059                 if (chan) {
1060                         ast_channel_lock(chan);
1061                         ast_softhangup(chan, AST_SOFTHANGUP_EXPLICIT);
1062                         ast_channel_unlock(chan);
1063                 }
1064         } else {
1065                 /* Now we signal it with SIGURG so it will break out of it's waitfor */
1066                 pthread_kill(thread, SIGURG);
1067         }
1068         AST_LIST_UNLOCK(&dial->channels);
1069
1070         /* Yay done with it */
1071         ast_mutex_unlock(&dial->lock);
1072
1073         /* Finally wait for the thread to exit */
1074         pthread_join(thread, NULL);
1075
1076         /* Yay thread is all gone */
1077         dial->thread = AST_PTHREADT_NULL;
1078
1079         return dial->state;
1080 }
1081
1082 /*! \brief Hangup channels
1083  * \note Hangup all active channels
1084  * \param dial Dialing structure
1085  */
1086 void ast_dial_hangup(struct ast_dial *dial)
1087 {
1088         struct ast_dial_channel *channel = NULL;
1089
1090         if (!dial)
1091                 return;
1092
1093         AST_LIST_LOCK(&dial->channels);
1094         AST_LIST_TRAVERSE(&dial->channels, channel, list) {
1095                 ast_hangup(channel->owner);
1096                 channel->owner = NULL;
1097         }
1098         AST_LIST_UNLOCK(&dial->channels);
1099
1100         return;
1101 }
1102
1103 /*! \brief Destroys a dialing structure
1104  * \note Destroys (free's) the given ast_dial structure
1105  * \param dial Dialing structure to free
1106  * \return Returns 0 on success, -1 on failure
1107  */
1108 int ast_dial_destroy(struct ast_dial *dial)
1109 {
1110         int i = 0;
1111         struct ast_dial_channel *channel = NULL;
1112
1113         if (!dial)
1114                 return -1;
1115
1116         /* Hangup and deallocate all the dialed channels */
1117         AST_LIST_LOCK(&dial->channels);
1118         AST_LIST_TRAVERSE_SAFE_BEGIN(&dial->channels, channel, list) {
1119                 /* Disable any enabled options */
1120                 for (i = 0; i < AST_DIAL_OPTION_MAX; i++) {
1121                         if (!channel->options[i])
1122                                 continue;
1123                         if (option_types[i].disable)
1124                                 option_types[i].disable(channel->options[i]);
1125                         channel->options[i] = NULL;
1126                 }
1127
1128                 /* Hang up channel if need be */
1129                 ast_hangup(channel->owner);
1130                 channel->owner = NULL;
1131
1132                 /* Free structure */
1133                 ast_free(channel->tech);
1134                 ast_free(channel->device);
1135                 ast_free(channel->assignedid1);
1136                 ast_free(channel->assignedid2);
1137
1138                 AST_LIST_REMOVE_CURRENT(list);
1139                 ast_free(channel);
1140         }
1141         AST_LIST_TRAVERSE_SAFE_END;
1142         AST_LIST_UNLOCK(&dial->channels);
1143
1144         /* Disable any enabled options globally */
1145         for (i = 0; i < AST_DIAL_OPTION_MAX; i++) {
1146                 if (!dial->options[i])
1147                         continue;
1148                 if (option_types[i].disable)
1149                         option_types[i].disable(dial->options[i]);
1150                 dial->options[i] = NULL;
1151         }
1152
1153         /* Lock be gone! */
1154         ast_mutex_destroy(&dial->lock);
1155
1156         /* Free structure */
1157         ast_free(dial);
1158
1159         return 0;
1160 }
1161
1162 /*! \brief Enables an option globally
1163  * \param dial Dial structure to enable option on
1164  * \param option Option to enable
1165  * \param data Data to pass to this option (not always needed)
1166  * \return Returns 0 on success, -1 on failure
1167  */
1168 int ast_dial_option_global_enable(struct ast_dial *dial, enum ast_dial_option option, void *data)
1169 {
1170         /* If the option is already enabled, return failure */
1171         if (dial->options[option])
1172                 return -1;
1173
1174         /* Execute enable callback if it exists, if not simply make sure the value is set */
1175         if (option_types[option].enable)
1176                 dial->options[option] = option_types[option].enable(data);
1177         else
1178                 dial->options[option] = (void*)1;
1179
1180         return 0;
1181 }
1182
1183 /*! \brief Helper function for finding a channel in a dial structure based on number
1184  */
1185 static struct ast_dial_channel *find_dial_channel(struct ast_dial *dial, int num)
1186 {
1187         struct ast_dial_channel *channel = AST_LIST_LAST(&dial->channels);
1188
1189         /* We can try to predict programmer behavior, the last channel they added is probably the one they wanted to modify */
1190         if (channel->num == num)
1191                 return channel;
1192
1193         /* Hrm not at the end... looking through the list it is! */
1194         AST_LIST_LOCK(&dial->channels);
1195         AST_LIST_TRAVERSE(&dial->channels, channel, list) {
1196                 if (channel->num == num)
1197                         break;
1198         }
1199         AST_LIST_UNLOCK(&dial->channels);
1200
1201         return channel;
1202 }
1203
1204 /*! \brief Enables an option per channel
1205  * \param dial Dial structure
1206  * \param num Channel number to enable option on
1207  * \param option Option to enable
1208  * \param data Data to pass to this option (not always needed)
1209  * \return Returns 0 on success, -1 on failure
1210  */
1211 int ast_dial_option_enable(struct ast_dial *dial, int num, enum ast_dial_option option, void *data)
1212 {
1213         struct ast_dial_channel *channel = NULL;
1214
1215         /* Ensure we have required arguments */
1216         if (!dial || AST_LIST_EMPTY(&dial->channels))
1217                 return -1;
1218
1219         if (!(channel = find_dial_channel(dial, num)))
1220                 return -1;
1221
1222         /* If the option is already enabled, return failure */
1223         if (channel->options[option])
1224                 return -1;
1225
1226         /* Execute enable callback if it exists, if not simply make sure the value is set */
1227         if (option_types[option].enable)
1228                 channel->options[option] = option_types[option].enable(data);
1229         else
1230                 channel->options[option] = (void*)1;
1231
1232         return 0;
1233 }
1234
1235 /*! \brief Disables an option globally
1236  * \param dial Dial structure to disable option on
1237  * \param option Option to disable
1238  * \return Returns 0 on success, -1 on failure
1239  */
1240 int ast_dial_option_global_disable(struct ast_dial *dial, enum ast_dial_option option)
1241 {
1242         /* If the option is not enabled, return failure */
1243         if (!dial->options[option]) {
1244                 return -1;
1245         }
1246
1247         /* Execute callback of option to disable if it exists */
1248         if (option_types[option].disable)
1249                 option_types[option].disable(dial->options[option]);
1250
1251         /* Finally disable option on the structure */
1252         dial->options[option] = NULL;
1253
1254         return 0;
1255 }
1256
1257 /*! \brief Disables an option per channel
1258  * \param dial Dial structure
1259  * \param num Channel number to disable option on
1260  * \param option Option to disable
1261  * \return Returns 0 on success, -1 on failure
1262  */
1263 int ast_dial_option_disable(struct ast_dial *dial, int num, enum ast_dial_option option)
1264 {
1265         struct ast_dial_channel *channel = NULL;
1266
1267         /* Ensure we have required arguments */
1268         if (!dial || AST_LIST_EMPTY(&dial->channels))
1269                 return -1;
1270
1271         if (!(channel = find_dial_channel(dial, num)))
1272                 return -1;
1273
1274         /* If the option is not enabled, return failure */
1275         if (!channel->options[option])
1276                 return -1;
1277
1278         /* Execute callback of option to disable it if it exists */
1279         if (option_types[option].disable)
1280                 option_types[option].disable(channel->options[option]);
1281
1282         /* Finally disable the option on the structure */
1283         channel->options[option] = NULL;
1284
1285         return 0;
1286 }
1287
1288 int ast_dial_reason(struct ast_dial *dial, int num)
1289 {
1290         struct ast_dial_channel *channel;
1291
1292         if (!dial || AST_LIST_EMPTY(&dial->channels) || !(channel = find_dial_channel(dial, num))) {
1293                 return -1;
1294         }
1295
1296         return channel->cause;
1297 }
1298
1299 struct ast_channel *ast_dial_get_channel(struct ast_dial *dial, int num)
1300 {
1301         struct ast_dial_channel *channel;
1302
1303         if (!dial || AST_LIST_EMPTY(&dial->channels) || !(channel = find_dial_channel(dial, num))) {
1304                 return NULL;
1305         }
1306
1307         return channel->owner;
1308 }
1309
1310 void ast_dial_set_state_callback(struct ast_dial *dial, ast_dial_state_callback callback)
1311 {
1312         dial->state_callback = callback;
1313 }
1314
1315 void ast_dial_set_user_data(struct ast_dial *dial, void *user_data)
1316 {
1317         dial->user_data = user_data;
1318 }
1319
1320 void *ast_dial_get_user_data(struct ast_dial *dial)
1321 {
1322         return dial->user_data;
1323 }
1324
1325 /*! \brief Set the maximum time (globally) allowed for trying to ring phones
1326  * \param dial The dial structure to apply the time limit to
1327  * \param timeout Maximum time allowed
1328  * \return nothing
1329  */
1330 void ast_dial_set_global_timeout(struct ast_dial *dial, int timeout)
1331 {
1332         dial->timeout = timeout;
1333
1334         if (dial->timeout > 0 && (dial->actual_timeout > dial->timeout || dial->actual_timeout == -1))
1335                 dial->actual_timeout = dial->timeout;
1336
1337         return;
1338 }
1339
1340 /*! \brief Set the maximum time (per channel) allowed for trying to ring the phone
1341  * \param dial The dial structure the channel belongs to
1342  * \param num Channel number to set timeout on
1343  * \param timeout Maximum time allowed
1344  * \return nothing
1345  */
1346 void ast_dial_set_timeout(struct ast_dial *dial, int num, int timeout)
1347 {
1348         struct ast_dial_channel *channel = NULL;
1349
1350         if (!(channel = find_dial_channel(dial, num)))
1351                 return;
1352
1353         channel->timeout = timeout;
1354
1355         if (channel->timeout > 0 && (dial->actual_timeout > channel->timeout || dial->actual_timeout == -1))
1356                 dial->actual_timeout = channel->timeout;
1357
1358         return;
1359 }