add dialplan functions for Caller ID, language and timeouts (bug #4219, with mods)
[asterisk/asterisk.git] / funcs / func_timeout.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Channel timeout related dialplan functions
5  * 
6  * Copyright (C) 2005, Digium, Inc.
7  *
8  * This program is free software, distributed under the terms of
9  * the GNU General Public License
10  */
11
12 #include <stdlib.h>
13 #include <string.h>
14 #include <sys/types.h>
15
16 #include "asterisk/channel.h"
17 #include "asterisk/pbx.h"
18 #include "asterisk/logger.h"
19 #include "asterisk/utils.h"
20 #include "asterisk/app.h"
21 #include "asterisk/options.h"
22
23 static char *builtin_function_timeout_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len) 
24 {
25         time_t myt;
26
27         if (!data) {
28                 ast_log(LOG_ERROR, "Must specify type of timeout to get.");
29                 return NULL;
30         }
31         
32         switch(*data) {
33         case 'a':
34         case 'A':
35                 if (chan->whentohangup == 0) {
36                         ast_copy_string(buf, "0", len);
37                 } else {
38                         time(&myt);
39                         snprintf(buf, len, "%d", (int) (chan->whentohangup - myt));
40                 }
41                 break;
42
43         case 'r':
44         case 'R':
45                 if (chan->pbx) {
46                         snprintf(buf, len, "%d", chan->pbx->rtimeout);
47                 }
48                 break;
49
50         case 'd':
51         case 'D':
52                 if (chan->pbx) {
53                         snprintf(buf, len, "%d", chan->pbx->dtimeout);
54                 }
55                 break;
56
57         default:
58                 ast_log(LOG_ERROR, "Unknown timeout type specified.");
59                 break;
60         }
61
62         return buf;
63 }
64
65 static void builtin_function_timeout_write(struct ast_channel *chan, char *cmd, char *data, const char *value) 
66 {
67         int x;
68         char timestr[64];
69         struct tm myt;
70
71         if (!data) {
72                 ast_log(LOG_ERROR, "Must specify type of timeout to set.");
73                 return;
74         }
75         
76         if (!value)
77                 return;
78
79         x = atoi(value);
80
81         switch(*data) {
82         case 'a':
83         case 'A':
84                 ast_channel_setwhentohangup(chan, x);
85                 if (option_verbose > 2) {
86                         if (chan->whentohangup) {
87                                 strftime(timestr, sizeof(timestr), "%Y-%m-%d %H:%M:%S UTC", gmtime_r(&chan->whentohangup, &myt));
88                                 ast_verbose( VERBOSE_PREFIX_3 "Channel will hangup at %s.\n", timestr);
89                         } else {
90                                 ast_verbose( VERBOSE_PREFIX_3 "Channel hangup cancelled.\n");
91                         } 
92                 }
93                 break;
94
95         case 'r':
96         case 'R':
97                 if (chan->pbx) {
98                         chan->pbx->rtimeout = x;
99                         if (option_verbose > 2)
100                                 ast_verbose( VERBOSE_PREFIX_3 "Response timeout set to %d\n", chan->pbx->rtimeout);
101                 }
102                 break;
103
104         case 'd':
105         case 'D':
106                 if (chan->pbx) {
107                         chan->pbx->dtimeout = x;
108                         if (option_verbose > 2)
109                                 ast_verbose( VERBOSE_PREFIX_3 "Digit timeout set to %d\n", chan->pbx->dtimeout);
110                 }
111                 break;
112
113         default:
114                 ast_log(LOG_ERROR, "Unknown timeout type specified.");
115                 break;
116         }
117 }
118
119 #ifndef BUILTIN_FUNC
120 static
121 #endif
122 struct ast_custom_function timeout_function = {
123         .name = "TIMEOUT",
124         .synopsis = "Gets or sets timeouts on the channel.",
125         .syntax = "TIMEOUT(timeouttype)",
126         .desc = "Gets or sets various channel timeouts. The timeouts that can be\n"
127         "manipulated are:\n"
128         "\n"
129         "absolute: The absolute maximum amount of time permitted for a call.  A\n"
130         "          setting of 0 disables the timeout.\n"
131         "\n"
132         "digit:    The maximum amount of time permitted between digits when the\n"
133         "          user is typing in an extension.  When this timeout expires,\n"
134         "          after the user has started to type in an extension, the\n"
135         "          extension will be considered complete, and will be\n"
136         "          interpreted.  Note that if an extension typed in is valid,\n"
137         "          it will not have to timeout to be tested, so typically at\n"
138         "          the expiry of this timeout, the extension will be considered\n"
139         "          invalid (and thus control would be passed to the 'i'\n"
140         "          extension, or if it doesn't exist the call would be\n"
141         "          terminated).  The default timeout is 5 seconds.\n"
142         "\n"
143         "response: The maximum amount of time permitted after falling through a\n"
144         "          series of priorities for a channel in which the user may\n"
145         "          begin typing an extension.  If the user does not type an\n"
146         "          extension in this amount of time, control will pass to the\n"
147         "          't' extension if it exists, and if not the call would be\n"
148         "          terminated.  The default timeout is 10 seconds.\n",
149         .read = builtin_function_timeout_read,
150         .write = builtin_function_timeout_write,
151 };
152
153 /*
154 Local Variables:
155 mode: C
156 c-file-style: "linux"
157 indent-tabs-mode: nil
158 End:
159 */