Let's also include aclocal.m4
[asterisk-bristuff.git] / funcs / func_timeout.c
blob5b935605ae5d0ede2e2c98ca2b2c39419b3ec306
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 (!chan)
49 return -1;
51 if (!data) {
52 ast_log(LOG_ERROR, "Must specify type of timeout to get.\n");
53 return -1;
56 switch (*data) {
57 case 'a':
58 case 'A':
59 if (chan->whentohangup == 0) {
60 ast_copy_string(buf, "0", len);
61 } else {
62 time(&myt);
63 snprintf(buf, len, "%d", (int) (chan->whentohangup - myt));
65 break;
67 case 'r':
68 case 'R':
69 if (chan->pbx) {
70 snprintf(buf, len, "%d", chan->pbx->rtimeout);
72 break;
74 case 'd':
75 case 'D':
76 if (chan->pbx) {
77 snprintf(buf, len, "%d", chan->pbx->dtimeout);
79 break;
81 default:
82 ast_log(LOG_ERROR, "Unknown timeout type specified.\n");
83 return -1;
86 return 0;
89 static int timeout_write(struct ast_channel *chan, char *cmd, char *data,
90 const char *value)
92 int x;
93 char timestr[64];
94 struct tm myt;
96 if (!chan)
97 return -1;
99 if (!data) {
100 ast_log(LOG_ERROR, "Must specify type of timeout to set.\n");
101 return -1;
104 if (!value)
105 return -1;
107 x = atoi(value);
108 if (x < 0)
109 x = 0;
111 switch (*data) {
112 case 'a':
113 case 'A':
114 ast_channel_setwhentohangup(chan, x);
115 if (option_verbose > 2) {
116 if (chan->whentohangup) {
117 strftime(timestr, sizeof(timestr), "%Y-%m-%d %H:%M:%S UTC",
118 gmtime_r(&chan->whentohangup, &myt));
119 ast_verbose(VERBOSE_PREFIX_3 "Channel will hangup at %s.\n",
120 timestr);
121 } else {
122 ast_verbose(VERBOSE_PREFIX_3 "Channel hangup cancelled.\n");
125 break;
127 case 'r':
128 case 'R':
129 if (chan->pbx) {
130 chan->pbx->rtimeout = x;
131 if (option_verbose > 2)
132 ast_verbose(VERBOSE_PREFIX_3 "Response timeout set to %d\n",
133 chan->pbx->rtimeout);
135 break;
137 case 'd':
138 case 'D':
139 if (chan->pbx) {
140 chan->pbx->dtimeout = x;
141 if (option_verbose > 2)
142 ast_verbose(VERBOSE_PREFIX_3 "Digit timeout set to %d\n",
143 chan->pbx->dtimeout);
145 break;
147 default:
148 ast_log(LOG_ERROR, "Unknown timeout type specified.\n");
149 break;
152 return 0;
155 static struct ast_custom_function timeout_function = {
156 .name = "TIMEOUT",
157 .synopsis = "Gets or sets timeouts on the channel.",
158 .syntax = "TIMEOUT(timeouttype)",
159 .desc =
160 "Gets or sets various channel timeouts. The timeouts that can be\n"
161 "manipulated are:\n" "\n"
162 "absolute: The absolute maximum amount of time permitted for a call. A\n"
163 " setting of 0 disables the timeout.\n" "\n"
164 "digit: The maximum amount of time permitted between digits when the\n"
165 " user is typing in an extension. When this timeout expires,\n"
166 " after the user has started to type in an extension, the\n"
167 " extension will be considered complete, and will be\n"
168 " interpreted. Note that if an extension typed in is valid,\n"
169 " it will not have to timeout to be tested, so typically at\n"
170 " the expiry of this timeout, the extension will be considered\n"
171 " invalid (and thus control would be passed to the 'i'\n"
172 " extension, or if it doesn't exist the call would be\n"
173 " terminated). The default timeout is 5 seconds.\n" "\n"
174 "response: The maximum amount of time permitted after falling through a\n"
175 " series of priorities for a channel in which the user may\n"
176 " begin typing an extension. If the user does not type an\n"
177 " extension in this amount of time, control will pass to the\n"
178 " 't' extension if it exists, and if not the call would be\n"
179 " terminated. The default timeout is 10 seconds.\n",
180 .read = timeout_read,
181 .write = timeout_write,
184 static int unload_module(void)
186 return ast_custom_function_unregister(&timeout_function);
189 static int load_module(void)
191 return ast_custom_function_register(&timeout_function);
194 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Channel timeout dialplan functions");