Merged revisions 37441-37442 via svnmerge from
[asterisk-bristuff.git] / funcs / func_timeout.c
blob116aef8029c7c3c0db8828a4114c0c0f75b241c4
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2006, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
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.
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.
19 /*! \file
21 * \brief Channel timeout related dialplan functions
23 * \author Mark Spencer <markster@digium.com>
26 #include "asterisk.h"
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <sys/types.h>
35 #include "asterisk/module.h"
36 #include "asterisk/channel.h"
37 #include "asterisk/pbx.h"
38 #include "asterisk/logger.h"
39 #include "asterisk/utils.h"
40 #include "asterisk/app.h"
41 #include "asterisk/options.h"
43 static int timeout_read(struct ast_channel *chan, char *cmd, char *data,
44 char *buf, size_t len)
46 time_t myt;
48 if (!data) {
49 ast_log(LOG_ERROR, "Must specify type of timeout to get.");
50 return -1;
53 switch (*data) {
54 case 'a':
55 case 'A':
56 if (chan->whentohangup == 0) {
57 ast_copy_string(buf, "0", len);
58 } else {
59 time(&myt);
60 snprintf(buf, len, "%d", (int) (chan->whentohangup - myt));
62 break;
64 case 'r':
65 case 'R':
66 if (chan->pbx) {
67 snprintf(buf, len, "%d", chan->pbx->rtimeout);
69 break;
71 case 'd':
72 case 'D':
73 if (chan->pbx) {
74 snprintf(buf, len, "%d", chan->pbx->dtimeout);
76 break;
78 default:
79 ast_log(LOG_ERROR, "Unknown timeout type specified.");
80 break;
83 return 0;
86 static int timeout_write(struct ast_channel *chan, char *cmd, char *data,
87 const char *value)
89 int x;
90 char timestr[64];
91 struct tm myt;
93 if (!data) {
94 ast_log(LOG_ERROR, "Must specify type of timeout to set.");
95 return -1;
98 if (!value)
99 return -1;
101 x = atoi(value);
103 switch (*data) {
104 case 'a':
105 case 'A':
106 ast_channel_setwhentohangup(chan, x);
107 if (option_verbose > 2) {
108 if (chan->whentohangup) {
109 strftime(timestr, sizeof(timestr), "%Y-%m-%d %H:%M:%S UTC",
110 gmtime_r(&chan->whentohangup, &myt));
111 ast_verbose(VERBOSE_PREFIX_3 "Channel will hangup at %s.\n",
112 timestr);
113 } else {
114 ast_verbose(VERBOSE_PREFIX_3 "Channel hangup cancelled.\n");
117 break;
119 case 'r':
120 case 'R':
121 if (chan->pbx) {
122 chan->pbx->rtimeout = x;
123 if (option_verbose > 2)
124 ast_verbose(VERBOSE_PREFIX_3 "Response timeout set to %d\n",
125 chan->pbx->rtimeout);
127 break;
129 case 'd':
130 case 'D':
131 if (chan->pbx) {
132 chan->pbx->dtimeout = x;
133 if (option_verbose > 2)
134 ast_verbose(VERBOSE_PREFIX_3 "Digit timeout set to %d\n",
135 chan->pbx->dtimeout);
137 break;
139 default:
140 ast_log(LOG_ERROR, "Unknown timeout type specified.");
141 break;
144 return 0;
147 static struct ast_custom_function timeout_function = {
148 .name = "TIMEOUT",
149 .synopsis = "Gets or sets timeouts on the channel.",
150 .syntax = "TIMEOUT(timeouttype)",
151 .desc =
152 "Gets or sets various channel timeouts. The timeouts that can be\n"
153 "manipulated are:\n" "\n"
154 "absolute: The absolute maximum amount of time permitted for a call. A\n"
155 " setting of 0 disables the timeout.\n" "\n"
156 "digit: The maximum amount of time permitted between digits when the\n"
157 " user is typing in an extension. When this timeout expires,\n"
158 " after the user has started to type in an extension, the\n"
159 " extension will be considered complete, and will be\n"
160 " interpreted. Note that if an extension typed in is valid,\n"
161 " it will not have to timeout to be tested, so typically at\n"
162 " the expiry of this timeout, the extension will be considered\n"
163 " invalid (and thus control would be passed to the 'i'\n"
164 " extension, or if it doesn't exist the call would be\n"
165 " terminated). The default timeout is 5 seconds.\n" "\n"
166 "response: The maximum amount of time permitted after falling through a\n"
167 " series of priorities for a channel in which the user may\n"
168 " begin typing an extension. If the user does not type an\n"
169 " extension in this amount of time, control will pass to the\n"
170 " 't' extension if it exists, and if not the call would be\n"
171 " terminated. The default timeout is 10 seconds.\n",
172 .read = timeout_read,
173 .write = timeout_write,
176 static char *tdesc = "Channel timeout dialplan functions";
178 static int unload_module(void *mod)
180 return ast_custom_function_unregister(&timeout_function);
183 static int load_module(void *mod)
185 return ast_custom_function_register(&timeout_function);
188 static const char *description(void)
190 return tdesc;
194 static const char *key(void)
196 return ASTERISK_GPL_KEY;
198 STD_MOD(MOD_1 | NO_USECOUNT, NULL, NULL, NULL);