2 * Asterisk -- A telephony toolkit for Linux.
6 * Copyright (C) 1999, Mark Spencer
8 * Mark Spencer <markster@linux-support.net>
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License
23 #include <asterisk/channel.h>
24 #include <asterisk/pbx.h>
25 #include <asterisk/file.h>
26 #include <asterisk/app.h>
27 #include <asterisk/dsp.h>
28 #include <asterisk/logger.h>
29 #include <asterisk/options.h>
33 /* set timeout to 0 for "standard" timeouts. Set timeout to -1 for
34 "ludicrous time" (essentially never times out) */
35 int ast_app_getdata(struct ast_channel *c, char *prompt, char *s, int maxlen, int timeout)
38 /* XXX Merge with full version? XXX */
40 res = ast_streamfile(c, prompt, c->language);
44 fto = c->pbx ? c->pbx->rtimeout * 1000 : 6000;
45 to = c->pbx ? c->pbx->dtimeout * 1000 : 2000;
47 if (timeout > 0) fto = to = timeout;
48 if (timeout < 0) fto = to = 1000000000;
49 res = ast_readstring(c, s, maxlen, to, fto, "#");
54 int ast_app_getdata_full(struct ast_channel *c, char *prompt, char *s, int maxlen, int timeout, int audiofd, int ctrlfd)
58 res = ast_streamfile(c, prompt, c->language);
64 if (timeout > 0) fto = to = timeout;
65 if (timeout < 0) fto = to = 1000000000;
66 res = ast_readstring_full(c, s, maxlen, to, fto, "#", audiofd, ctrlfd);
70 int ast_app_getvoice(struct ast_channel *c, char *dest, char *dstfmt, char *prompt, int silence, int maxsec)
73 struct ast_filestream *writer;
78 struct ast_dsp *sildet;
79 /* Play prompt if requested */
81 res = ast_streamfile(c, prompt, c->language);
84 res = ast_waitstream(c,"");
89 res = ast_set_read_format(c, AST_FORMAT_SLINEAR);
91 ast_log(LOG_WARNING, "Unable to set to linear mode, giving up\n");
94 sildet = ast_dsp_new();
96 ast_log(LOG_WARNING, "Unable to create silence detector :(\n");
99 writer = ast_writefile(dest, dstfmt, "Voice file", 0, 0, 0666);
101 ast_log(LOG_WARNING, "Unable to open file '%s' in format '%s' for writing\n", dest, dstfmt);
102 ast_dsp_free(sildet);
106 if ((res = ast_waitfor(c, 2000)) < 0) {
107 ast_log(LOG_NOTICE, "Waitfor failed while recording file '%s' format '%s'\n", dest, dstfmt);
113 ast_log(LOG_NOTICE, "Hungup while recording file '%s' format '%s'\n", dest, dstfmt);
116 if ((f->frametype == AST_FRAME_DTMF) && (f->subclass == '#')) {
117 /* Ended happily with DTMF */
120 } else if (f->frametype == AST_FRAME_VOICE) {
121 ast_dsp_silence(sildet, f, &total);
122 if (total > silence) {
123 /* Ended happily with silence */
127 totalms += f->samples / 8;
128 if (totalms > maxsec * 1000) {
129 /* Ended happily with too much stuff */
130 ast_log(LOG_NOTICE, "Constraining voice on '%s' to %d seconds\n", c->name, maxsec);
138 res = ast_set_read_format(c, rfmt);
140 ast_log(LOG_WARNING, "Unable to restore read format on '%s'\n", c->name);
141 ast_dsp_free(sildet);
142 ast_closestream(writer);
146 int ast_app_has_voicemail(const char *mailbox)
155 /* If no mailbox, return immediately */
156 if (!strlen(mailbox))
158 if (strchr(mailbox, ',')) {
159 strncpy(tmp, mailbox, sizeof(tmp));
162 while((cur = strsep(&mb, ","))) {
164 if (ast_app_has_voicemail(cur))
170 strncpy(tmp, mailbox, sizeof(tmp) - 1);
171 context = strchr(tmp, '@');
177 snprintf(fn, sizeof(fn), "%s/voicemail/%s/%s/INBOX", (char *)ast_config_AST_SPOOL_DIR, context, tmp);
181 while ((de = readdir(dir))) {
182 if (!strncasecmp(de->d_name, "msg", 3))
191 int ast_app_messagecount(const char *mailbox, int *newmsgs, int *oldmsgs)
204 /* If no mailbox, return immediately */
205 if (!strlen(mailbox))
207 if (strchr(mailbox, ',')) {
209 strncpy(tmp, mailbox, sizeof(tmp));
212 while((cur = strsep(&mb, ", "))) {
214 if (ast_app_messagecount(cur, newmsgs ? &tmpnew : NULL, oldmsgs ? &tmpold : NULL))
226 strncpy(tmp, mailbox, sizeof(tmp) - 1);
227 context = strchr(tmp, '@');
234 snprintf(fn, sizeof(fn), "%s/voicemail/%s/%s/INBOX", (char *)ast_config_AST_SPOOL_DIR, context, tmp);
237 while ((de = readdir(dir))) {
238 if ((strlen(de->d_name) > 3) && !strncasecmp(de->d_name, "msg", 3) &&
239 !strcasecmp(de->d_name + strlen(de->d_name) - 3, "txt"))
247 snprintf(fn, sizeof(fn), "%s/voicemail/%s/%s/Old", (char *)ast_config_AST_SPOOL_DIR, context, tmp);
250 while ((de = readdir(dir))) {
251 if ((strlen(de->d_name) > 3) && !strncasecmp(de->d_name, "msg", 3) &&
252 !strcasecmp(de->d_name + strlen(de->d_name) - 3, "txt"))