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>
30 #include <asterisk/utils.h>
34 /* set timeout to 0 for "standard" timeouts. Set timeout to -1 for
35 "ludicrous time" (essentially never times out) */
36 int ast_app_getdata(struct ast_channel *c, char *prompt, char *s, int maxlen, int timeout)
39 /* XXX Merge with full version? XXX */
41 res = ast_streamfile(c, prompt, c->language);
45 fto = c->pbx ? c->pbx->rtimeout * 1000 : 6000;
46 to = c->pbx ? c->pbx->dtimeout * 1000 : 2000;
48 if (timeout > 0) fto = to = timeout;
49 if (timeout < 0) fto = to = 1000000000;
50 res = ast_readstring(c, s, maxlen, to, fto, "#");
55 int ast_app_getdata_full(struct ast_channel *c, char *prompt, char *s, int maxlen, int timeout, int audiofd, int ctrlfd)
59 res = ast_streamfile(c, prompt, c->language);
65 if (timeout > 0) fto = to = timeout;
66 if (timeout < 0) fto = to = 1000000000;
67 res = ast_readstring_full(c, s, maxlen, to, fto, "#", audiofd, ctrlfd);
71 int ast_app_getvoice(struct ast_channel *c, char *dest, char *dstfmt, char *prompt, int silence, int maxsec)
74 struct ast_filestream *writer;
79 struct ast_dsp *sildet;
80 /* Play prompt if requested */
82 res = ast_streamfile(c, prompt, c->language);
85 res = ast_waitstream(c,"");
90 res = ast_set_read_format(c, AST_FORMAT_SLINEAR);
92 ast_log(LOG_WARNING, "Unable to set to linear mode, giving up\n");
95 sildet = ast_dsp_new();
97 ast_log(LOG_WARNING, "Unable to create silence detector :(\n");
100 writer = ast_writefile(dest, dstfmt, "Voice file", 0, 0, 0666);
102 ast_log(LOG_WARNING, "Unable to open file '%s' in format '%s' for writing\n", dest, dstfmt);
103 ast_dsp_free(sildet);
107 if ((res = ast_waitfor(c, 2000)) < 0) {
108 ast_log(LOG_NOTICE, "Waitfor failed while recording file '%s' format '%s'\n", dest, dstfmt);
114 ast_log(LOG_NOTICE, "Hungup while recording file '%s' format '%s'\n", dest, dstfmt);
117 if ((f->frametype == AST_FRAME_DTMF) && (f->subclass == '#')) {
118 /* Ended happily with DTMF */
121 } else if (f->frametype == AST_FRAME_VOICE) {
122 ast_dsp_silence(sildet, f, &total);
123 if (total > silence) {
124 /* Ended happily with silence */
128 totalms += f->samples / 8;
129 if (totalms > maxsec * 1000) {
130 /* Ended happily with too much stuff */
131 ast_log(LOG_NOTICE, "Constraining voice on '%s' to %d seconds\n", c->name, maxsec);
139 res = ast_set_read_format(c, rfmt);
141 ast_log(LOG_WARNING, "Unable to restore read format on '%s'\n", c->name);
142 ast_dsp_free(sildet);
143 ast_closestream(writer);
147 int ast_app_has_voicemail(const char *mailbox)
156 /* If no mailbox, return immediately */
157 if (ast_strlen_zero(mailbox))
159 if (strchr(mailbox, ',')) {
160 strncpy(tmp, mailbox, sizeof(tmp));
163 while((cur = strsep(&mb, ","))) {
164 if (!ast_strlen_zero(cur)) {
165 if (ast_app_has_voicemail(cur))
171 strncpy(tmp, mailbox, sizeof(tmp) - 1);
172 context = strchr(tmp, '@');
178 snprintf(fn, sizeof(fn), "%s/voicemail/%s/%s/INBOX", (char *)ast_config_AST_SPOOL_DIR, context, tmp);
182 while ((de = readdir(dir))) {
183 if (!strncasecmp(de->d_name, "msg", 3))
192 int ast_app_messagecount(const char *mailbox, int *newmsgs, int *oldmsgs)
205 /* If no mailbox, return immediately */
206 if (ast_strlen_zero(mailbox))
208 if (strchr(mailbox, ',')) {
210 strncpy(tmp, mailbox, sizeof(tmp));
213 while((cur = strsep(&mb, ", "))) {
214 if (!ast_strlen_zero(cur)) {
215 if (ast_app_messagecount(cur, newmsgs ? &tmpnew : NULL, oldmsgs ? &tmpold : NULL))
227 strncpy(tmp, mailbox, sizeof(tmp) - 1);
228 context = strchr(tmp, '@');
235 snprintf(fn, sizeof(fn), "%s/voicemail/%s/%s/INBOX", (char *)ast_config_AST_SPOOL_DIR, context, tmp);
238 while ((de = readdir(dir))) {
239 if ((strlen(de->d_name) > 3) && !strncasecmp(de->d_name, "msg", 3) &&
240 !strcasecmp(de->d_name + strlen(de->d_name) - 3, "txt"))
248 snprintf(fn, sizeof(fn), "%s/voicemail/%s/%s/Old", (char *)ast_config_AST_SPOOL_DIR, context, tmp);
251 while ((de = readdir(dir))) {
252 if ((strlen(de->d_name) > 3) && !strncasecmp(de->d_name, "msg", 3) &&
253 !strcasecmp(de->d_name + strlen(de->d_name) - 3, "txt"))