aboutsummaryrefslogtreecommitdiffstats
path: root/funcs/func_timeout.c
blob: 1da11e54a60066e04314bef9d937fa0b5a52648d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
 * Asterisk -- An open source telephony toolkit.
 *
 * Copyright (C) 1999 - 2006, Digium, Inc.
 *
 * Mark Spencer <markster@digium.com>
 *
 * See http://www.asterisk.org for more information about
 * the Asterisk project. Please do not directly contact
 * any of the maintainers of this project for assistance;
 * the project provides a web site, mailing lists and IRC
 * channels for your use.
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License Version 2. See the LICENSE file
 * at the top of the source tree.
 */

/*! \file
 *
 * \brief Channel timeout related dialplan functions
 *
 * \author Mark Spencer <markster@digium.com> 
 * \ingroup functions
 */

#include "asterisk.h"

ASTERISK_FILE_VERSION(__FILE__, "$Revision$")

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>

#include "asterisk/module.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/logger.h"
#include "asterisk/utils.h"
#include "asterisk/app.h"
#include "asterisk/options.h"

static int timeout_read(struct ast_channel *chan, const char *cmd, char *data,
			char *buf, size_t len)
{
	time_t myt;

	if (!chan)
		return -1;

	if (!data) {
		ast_log(LOG_ERROR, "Must specify type of timeout to get.\n");
		return -1;
	}

	switch (*data) {
	case 'a':
	case 'A':
		if (chan->whentohangup == 0) {
			ast_copy_string(buf, "0", len);
		} else {
			time(&myt);
			snprintf(buf, len, "%d", (int) (chan->whentohangup - myt));
		}
		break;

	case 'r':
	case 'R':
		if (chan->pbx) {
			snprintf(buf, len, "%d", chan->pbx->rtimeout);
		}
		break;

	case 'd':
	case 'D':
		if (chan->pbx) {
			snprintf(buf, len, "%d", chan->pbx->dtimeout);
		}
		break;

	default:
		ast_log(LOG_ERROR, "Unknown timeout type specified.\n");
		break;
	}

	return 0;
}

static int timeout_write(struct ast_channel *chan, const char *cmd, char *data,
			 const char *value)
{
	int x;
	char timestr[64];
	struct tm myt;

	if (!chan)
		return -1;

	if (!data) {
		ast_log(LOG_ERROR, "Must specify type of timeout to set.\n");
		return -1;
	}

	if (!value)
		return -1;

	x = atoi(value);

	switch (*data) {
	case 'a':
	case 'A':
		ast_channel_setwhentohangup(chan, x);
		if (option_verbose > 2) {
			if (chan->whentohangup) {
				strftime(timestr, sizeof(timestr), "%Y-%m-%d %H:%M:%S UTC",
					 gmtime_r(&chan->whentohangup, &myt));
				ast_verbose(VERBOSE_PREFIX_3 "Channel will hangup at %s.\n",
					    timestr);
			} else {
				ast_verbose(VERBOSE_PREFIX_3 "Channel hangup cancelled.\n");
			}
		}
		break;

	case 'r':
	case 'R':
		if (chan->pbx) {
			chan->pbx->rtimeout = x;
			if (option_verbose > 2)
				ast_verbose(VERBOSE_PREFIX_3 "Response timeout set to %d\n",
					    chan->pbx->rtimeout);
		}
		break;

	case 'd':
	case 'D':
		if (chan->pbx) {
			chan->pbx->dtimeout = x;
			if (option_verbose > 2)
				ast_verbose(VERBOSE_PREFIX_3 "Digit timeout set to %d\n",
					    chan->pbx->dtimeout);
		}
		break;

	default:
		ast_log(LOG_ERROR, "Unknown timeout type specified.\n");
		break;
	}

	return 0;
}

static struct ast_custom_function timeout_function = {
	.name = "TIMEOUT",
	.synopsis = "Gets or sets timeouts on the channel. Timeout values are in seconds.",
	.syntax = "TIMEOUT(timeouttype)",
	.desc =
		"Gets or sets various channel timeouts. The timeouts that can be\n"
		"manipulated are:\n" "\n"
		"absolute: The absolute maximum amount of time permitted for a call.  A\n"
		"	   setting of 0 disables the timeout.\n" "\n"
		"digit:    The maximum amount of time permitted between digits when the\n"
		"          user is typing in an extension.  When this timeout expires,\n"
		"          after the user has started to type in an extension, the\n"
		"          extension will be considered complete, and will be\n"
		"          interpreted.  Note that if an extension typed in is valid,\n"
		"          it will not have to timeout to be tested, so typically at\n"
		"          the expiry of this timeout, the extension will be considered\n"
		"          invalid (and thus control would be passed to the 'i'\n"
		"          extension, or if it doesn't exist the call would be\n"
		"          terminated).  The default timeout is 5 seconds.\n" "\n"
		"response: The maximum amount of time permitted after falling through a\n"
		"	   series of priorities for a channel in which the user may\n"
		"	   begin typing an extension.  If the user does not type an\n"
		"	   extension in this amount of time, control will pass to the\n"
		"	   't' extension if it exists, and if not the call would be\n"
		"	   terminated.  The default timeout is 10 seconds.\n",
	.read = timeout_read,
	.write = timeout_write,
};

static int unload_module(void)
{
	return ast_custom_function_unregister(&timeout_function);
}

static int load_module(void)
{
	return ast_custom_function_register(&timeout_function);
}

AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Channel timeout dialplan functions");