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