Merged revisions 80539 via svnmerge from
[asterisk/asterisk.git] / funcs / func_timeout.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2006, Digium, Inc.
5  *
6  * Mark Spencer <markster@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 Channel timeout related dialplan functions
22  *
23  * \author Mark Spencer <markster@digium.com> 
24  * \ingroup functions
25  */
26
27 #include "asterisk.h"
28
29 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
30
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <sys/types.h>
35
36 #include "asterisk/module.h"
37 #include "asterisk/channel.h"
38 #include "asterisk/pbx.h"
39 #include "asterisk/logger.h"
40 #include "asterisk/utils.h"
41 #include "asterisk/app.h"
42 #include "asterisk/options.h"
43
44 static int timeout_read(struct ast_channel *chan, const char *cmd, char *data,
45                         char *buf, size_t len)
46 {
47         time_t myt;
48
49         if (!chan)
50                 return -1;
51
52         if (!data) {
53                 ast_log(LOG_ERROR, "Must specify type of timeout to get.\n");
54                 return -1;
55         }
56
57         switch (*data) {
58         case 'a':
59         case 'A':
60                 if (chan->whentohangup == 0) {
61                         ast_copy_string(buf, "0", len);
62                 } else {
63                         time(&myt);
64                         snprintf(buf, len, "%d", (int) (chan->whentohangup - myt));
65                 }
66                 break;
67
68         case 'r':
69         case 'R':
70                 if (chan->pbx) {
71                         snprintf(buf, len, "%d", chan->pbx->rtimeout);
72                 }
73                 break;
74
75         case 'd':
76         case 'D':
77                 if (chan->pbx) {
78                         snprintf(buf, len, "%d", chan->pbx->dtimeout);
79                 }
80                 break;
81
82         default:
83                 ast_log(LOG_ERROR, "Unknown timeout type specified.\n");
84                 break;
85         }
86
87         return 0;
88 }
89
90 static int timeout_write(struct ast_channel *chan, const char *cmd, char *data,
91                          const char *value)
92 {
93         float f;
94         int x;
95         char timestr[64];
96         struct ast_tm myt;
97
98         if (!chan)
99                 return -1;
100
101         if (!data) {
102                 ast_log(LOG_ERROR, "Must specify type of timeout to set.\n");
103                 return -1;
104         }
105
106         if (!value)
107                 return -1;
108
109         f = atof(value);
110         if (f < 0)
111                 f = 1.0;
112         x = (int) (f * 1000);
113
114         switch (*data) {
115         case 'a':
116         case 'A':
117                 ast_channel_setwhentohangup(chan, x);
118                         if (chan->whentohangup) {
119                                 struct timeval tv = { chan->whentohangup, 0 };
120                                 ast_strftime(timestr, sizeof(timestr), "%Y-%m-%d %H:%M:%S.%3q %Z",
121                                         ast_localtime(&tv, &myt, NULL));
122                         ast_verb(3, "Channel will hangup at %s.\n", timestr);
123                         } else {
124                         ast_verb(3, "Channel hangup cancelled.\n");
125                         }
126                 break;
127
128         case 'r':
129         case 'R':
130                 if (chan->pbx) {
131                         chan->pbx->rtimeout = x;
132                         ast_verb(3, "Response timeout set to %d\n", chan->pbx->rtimeout);
133                 }
134                 break;
135
136         case 'd':
137         case 'D':
138                 if (chan->pbx) {
139                         chan->pbx->dtimeout = x;
140                         ast_verb(3, "Digit timeout set to %d\n", chan->pbx->dtimeout);
141                 }
142                 break;
143
144         default:
145                 ast_log(LOG_ERROR, "Unknown timeout type specified.\n");
146                 break;
147         }
148
149         return 0;
150 }
151
152 static struct ast_custom_function timeout_function = {
153         .name = "TIMEOUT",
154         .synopsis = "Gets or sets timeouts on the channel. Timeout values are in seconds.",
155         .syntax = "TIMEOUT(timeouttype)",
156         .desc =
157                 "Gets or sets various channel timeouts. The timeouts that can be\n"
158                 "manipulated are:\n" "\n"
159                 "absolute: The absolute maximum amount of time permitted for a call.  A\n"
160                 "          setting of 0 disables the timeout.\n" "\n"
161                 "digit:    The maximum amount of time permitted between digits when the\n"
162                 "          user is typing in an extension.  When this timeout expires,\n"
163                 "          after the user has started to type in an extension, the\n"
164                 "          extension will be considered complete, and will be\n"
165                 "          interpreted.  Note that if an extension typed in is valid,\n"
166                 "          it will not have to timeout to be tested, so typically at\n"
167                 "          the expiry of this timeout, the extension will be considered\n"
168                 "          invalid (and thus control would be passed to the 'i'\n"
169                 "          extension, or if it doesn't exist the call would be\n"
170                 "          terminated).  The default timeout is 5 seconds.\n" "\n"
171                 "response: The maximum amount of time permitted after falling through a\n"
172                 "          series of priorities for a channel in which the user may\n"
173                 "          begin typing an extension.  If the user does not type an\n"
174                 "          extension in this amount of time, control will pass to the\n"
175                 "          't' extension if it exists, and if not the call would be\n"
176                 "          terminated.  The default timeout is 10 seconds.\n",
177         .read = timeout_read,
178         .write = timeout_write,
179 };
180
181 static int unload_module(void)
182 {
183         return ast_custom_function_unregister(&timeout_function);
184 }
185
186 static int load_module(void)
187 {
188         return ast_custom_function_register(&timeout_function);
189 }
190
191 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Channel timeout dialplan functions");