2 * Asterisk -- A telephony toolkit for Linux.
4 * Voicemail System (did you ever think it could be so easy?)
6 * Copyright (C) 1999, Adtran Inc. and Linux Support Services, LLC
8 * Mark Spencer <markster@linux-support.net>
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License
14 #include <asterisk/file.h>
15 #include <asterisk/logger.h>
16 #include <asterisk/channel.h>
17 #include <asterisk/pbx.h>
18 #include <asterisk/options.h>
19 #include <asterisk/config.h>
20 #include <asterisk/say.h>
21 #include <asterisk/module.h>
33 #include "../asterisk.h"
35 #define COMMAND_TIMEOUT 5000
37 #define VOICEMAIL_CONFIG "voicemail.conf"
38 #define ASTERISK_USERNAME "asterisk"
41 #define HOSTNAME_OVERRIDE "linux-support.net"
44 #define SENDMAIL "/usr/sbin/sendmail -t"
46 #define INTRO "vm-intro"
50 #define MAX_OTHER_FORMATS 10
52 #define VM_SPOOL_DIR AST_SPOOL_DIR "/vm"
55 static char *tdesc = "Comedian Mail (Voicemail System)";
58 static char *app = "VoiceMail";
60 /* Check mail, control, etc */
61 static char *app2 = "VoiceMailMain";
67 static char *get_dir(char *ext, char *mailbox)
69 char *tmp = malloc(strlen(ext) + strlen(VM_SPOOL_DIR) + 3 + strlen(mailbox));
70 sprintf(tmp, "%s/%s/%s", VM_SPOOL_DIR, ext, mailbox);
73 static char *get_fn(char *dir, int num)
75 char *tmp = malloc(strlen(dir) + 10);
76 sprintf(tmp, "%s/msg%04d", dir, num);
80 static int announce_message(struct ast_channel *chan, char *dir, int msgcnt)
84 res = ast_streamfile(chan, "vm-message");
86 res = ast_waitstream(chan, AST_DIGIT_ANY);
88 res = ast_say_number(chan, msgcnt+1);
90 fn = get_fn(dir, msgcnt);
92 res = ast_streamfile(chan, fn);
99 ast_log(LOG_WARNING, "Unable to announce message\n");
102 static int sendmail(char *email, char *name, int msgnum, char *mailbox)
109 p = popen(SENDMAIL, "w");
111 gethostname(host, sizeof(host));
114 strftime(date, sizeof(date), "%a, %d %b %Y %H:%M:%S %z", tm);
115 fprintf(p, "Date: %s\n", date);
116 fprintf(p, "Message-ID: <Asterisk-%d-%s-%d@%s>\n", msgnum, mailbox, getpid(), host);
117 fprintf(p, "From: Asterisk PBX <%s@%s>\n", ASTERISK_USERNAME,
118 #ifdef HOSTNAME_OVERRIDE
124 fprintf(p, "To: %s <%s>\n", name, email);
125 fprintf(p, "Subject: [PBX]: New message %d in mailbox %s\n\n", msgnum, mailbox);
126 strftime(date, sizeof(date), "%A, %B %d, %Y at %r", tm);
127 fprintf(p, "Dear %s:\n\n\tJust wanted to let you know you were just left a message (number %d)\n"
128 "in mailbox %s, on %s so you might\n"
129 "want to check it when you get a chance. Thanks!\n\n\t\t\t\t--Asterisk\n", name, msgnum, mailbox, date);
133 ast_log(LOG_WARNING, "Unable to launch '%s'\n", SENDMAIL);
139 static int leave_voicemail(struct ast_channel *chan, char *ext, int silent)
141 struct ast_config *cfg;
142 char *copy, *name, *passwd, *email, *dir, *fmt, *fmts, *fn=NULL;
144 struct ast_filestream *writer, *others[MAX_OTHER_FORMATS];
145 char *sfmt[MAX_OTHER_FORMATS];
146 int res = -1, fmtcnt=0, x;
151 cfg = ast_load(VOICEMAIL_CONFIG);
153 ast_log(LOG_WARNING, "No such configuration file %s\n", VOICEMAIL_CONFIG);
156 if ((copy = ast_variable_retrieve(cfg, chan->context, ext))) {
157 /* Make sure they have an entry in the config */
159 passwd = strtok(copy, ",");
160 name = strtok(NULL, ",");
161 email = strtok(NULL, ",");
162 dir = get_dir(ext, "");
163 /* It's easier just to try to make it than to check for its existence */
164 if (mkdir(dir, 0700) && (errno != EEXIST))
165 ast_log(LOG_WARNING, "mkdir '%s' failed: %s\n", dir, strerror(errno));
166 dir = get_dir(ext, "INBOX");
167 if (mkdir(dir, 0700) && (errno != EEXIST))
168 ast_log(LOG_WARNING, "mkdir '%s' failed: %s\n", dir, strerror(errno));
169 /* Stream an info message */
170 if (silent || !ast_streamfile(chan, INTRO)) {
171 /* Wait for the message to finish */
172 if (silent || !ast_waitstream(chan, "")) {
173 fmt = ast_variable_retrieve(cfg, "general", "format");
176 fmt = strtok(fmts, "|");
181 fn = get_fn(dir, msgnum);
182 snprintf(comment, sizeof(comment), "Voicemail from %s to %s (%s) on %s\n",
183 (chan->callerid ? chan->callerid : "Unknown"),
184 name, ext, chan->name);
185 writer = ast_writefile(fn, fmt, comment, O_EXCL, 1 /* check for other formats */, 0700);
186 if (!writer && (errno != EEXIST))
189 } while(!writer && (msgnum < MAXMSG));
191 /* We need to reset these values */
193 fmt = ast_variable_retrieve(cfg, "general", "format");
196 while((fmt = strtok(NULL, "|"))) {
197 if (fmtcnt > MAX_OTHER_FORMATS - 1) {
198 ast_log(LOG_WARNING, "Please increase MAX_OTHER_FORMATS in app_voicemail.c\n");
201 sfmt[fmtcnt++] = strdup(fmt);
203 for (x=0;x<fmtcnt;x++) {
204 others[x] = ast_writefile(fn, sfmt[x], comment, 0, 0, 0700);
206 /* Ick, the other format didn't work, but be sure not
207 to leak memory here */
209 for(y=x+1;y < fmtcnt;y++)
216 /* Loop forever, writing the packets we read to the writer(s), until
217 we read a # or get a hangup */
218 if (option_verbose > 2)
219 ast_verbose( VERBOSE_PREFIX_3 "Recording to %s\n", fn);
220 while((f = ast_read(chan))) {
221 if (f->frametype == AST_FRAME_VOICE) {
222 /* Write the primary format */
223 res = ast_writestream(writer, f);
224 /* And each of the others */
225 for (x=0;x<fmtcnt;x++)
226 res |= ast_writestream(others[x], f);
228 /* Exit on any error */
230 ast_log(LOG_WARNING, "Error writing frame\n");
234 if (f->frametype == AST_FRAME_DTMF) {
235 if (f->subclass == '#') {
236 if (option_verbose > 2)
237 ast_verbose( VERBOSE_PREFIX_3 "User ended message by pressing %c\n", f->subclass);
244 if (option_verbose > 2)
245 ast_verbose( VERBOSE_PREFIX_3 "User hung up\n");
250 ast_log(LOG_WARNING, "Error creating writestream '%s', format '%s'\n", fn, sfmt[x]);
253 ast_closestream(writer);
254 for (x=0;x<fmtcnt;x++) {
257 ast_closestream(others[x]);
261 /* Let them know it worked */
262 ast_streamfile(chan, "vm-msgsaved");
263 ast_waitstream(chan, "");
265 /* Send e-mail if applicable */
267 sendmail(email, name, msgnum, ext);
271 ast_log(LOG_WARNING, "Error writing to mailbox %s\n", ext);
273 ast_log(LOG_WARNING, "Too many messages in mailbox %s\n", ext);
279 ast_log(LOG_WARNING, "No format to save messages in \n");
282 ast_log(LOG_WARNING, "Unable to playback instructions\n");
287 ast_log(LOG_WARNING, "No entry in voicemail config file for '%s'\n", ext);
289 /* Leave voicemail for someone */
293 static int vm_execmain(struct ast_channel *chan, void *data)
295 /* XXX This is, admittedly, some pretty horrendus code XXX */
305 char password[80], *copy;
307 struct ast_config *cfg;
312 cfg = ast_load(VOICEMAIL_CONFIG);
314 ast_log(LOG_WARNING, "No voicemail configuration\n");
317 if (ast_streamfile(chan, "vm-login"))
320 /* Prompt for, and read in the username */
321 if (ast_readstring(chan, username, sizeof(username), 2000, 5000, "#"))
323 if (ast_streamfile(chan, "vm-password"))
325 if (ast_readstring(chan, password, sizeof(password), 2000, 5000, "#"))
327 copy = ast_variable_retrieve(cfg, chan->context, username);
331 if (!strcmp(password,copy))
333 else if (option_verbose > 2)
334 ast_verbose( VERBOSE_PREFIX_3 "Incorrect password '%s' for user '%s'\n", password, username);
336 } else if (option_verbose > 2)
337 ast_verbose( VERBOSE_PREFIX_3 "No such user '%s' in config file\n", username);
339 if (ast_streamfile(chan, "vm-incorrect"))
341 if (ast_waitstream(chan, ""))
346 dir = get_dir(username, "INBOX");
351 /* Find out how many messages are there, mark all as
354 fn = get_fn(dir, maxmsg);
355 if ((res = ast_fileexists(fn, NULL))>0) {
361 if (ast_streamfile(chan, "vm-youhave"))
363 if ((d=ast_waitstream(chan, AST_DIGIT_ANY)) < 0)
365 ast_stopstream(chan);
367 /* If they haven't interrupted us, play the message count */
369 if ((d = ast_say_number(chan, maxmsg)) < 0)
372 if (ast_streamfile(chan, "vm-no"))
374 if ((d=ast_waitstream(chan, AST_DIGIT_ANY)) < 0)
376 ast_stopstream(chan);
379 /* And if they still haven't, give them the last word */
380 if (ast_streamfile(chan, ((maxmsg == 1) ? "vm-message" : "vm-messages")))
382 if (ast_waitstream(chan, AST_DIGIT_ANY) < 0)
384 ast_stopstream(chan);
389 #define STATE_STARTING 1
390 #define STATE_MESSAGE 2
391 #define STATE_MESSAGE_PLAYING 3
392 state = STATE_STARTING;
393 ast_log(LOG_EVENT, "User '%s' logged in on channel '%s' with %d message(s).\n", username, chan->name, maxmsg);
394 if (option_verbose > 2)
395 ast_verbose( VERBOSE_PREFIX_3 "User '%s' logged in on channel %s with %d messages\n", username, chan->name, maxmsg);
396 if (!ast_streamfile(chan, "vm-instructions")) {
398 if (chan->stream || (chan->trans && chan->trans->stream)) {
399 d = ast_waitstream(chan, AST_DIGIT_ANY);
400 ast_stopstream(chan);
401 if (!d && (state == STATE_MESSAGE_PLAYING)) {
402 state = STATE_MESSAGE;
403 /* If it runs out playing a message, then give directions */
404 if (!(d = ast_streamfile(chan, "vm-msginstruct")))
405 d = ast_waitstream(chan, AST_DIGIT_ANY);
406 ast_stopstream(chan);
409 d = ast_waitfordigit(chan, COMMAND_TIMEOUT);
411 d = ast_waitfordigit(chan, COMMAND_TIMEOUT);
415 if (!d || (d == '*')) {
416 /* If they don't say anything, play back a message. We'll decide which one is
417 best based up on where they are. Ditto if they press the '*' key. */
420 if (ast_streamfile(chan, "vm-instructions"))
424 case STATE_MESSAGE_PLAYING:
425 if (ast_streamfile(chan, "vm-msginstruct"))
429 ast_log(LOG_WARNING, "What do I do when they timeout/* in state %d?\n", state);
432 /* XXX Should we be command-compatible with Meridian mail? Their system seems
433 very confusing, but also widely used XXX */
434 /* They've entered (or started to enter) a command */
437 if (curmsg < maxmsg) {
438 deleted[curmsg] = !deleted[curmsg];
439 if (deleted[curmsg]) {
440 if (ast_streamfile(chan, "vm-deleted"))
443 if (ast_streamfile(chan, "vm-undeleted"))
447 if (ast_streamfile(chan, "vm-nomore"))
455 if ((d = announce_message(chan, dir, curmsg)) > 0)
460 if (ast_streamfile(chan, "vm-nomore"))
463 state = STATE_MESSAGE_PLAYING;
469 if ((d = announce_message(chan, dir, curmsg)) > 0)
473 state = STATE_MESSAGE_PLAYING;
476 if ((d = announce_message(chan, dir, curmsg)) > 0)
480 state = STATE_MESSAGE_PLAYING;
483 if (curmsg < maxmsg - 1) {
485 if ((d = announce_message(chan, dir, curmsg)) > 0)
490 if (ast_streamfile(chan, "vm-nomore"))
493 state = STATE_MESSAGE_PLAYING;
495 /* XXX Message compose? It's easy! Just read their # and, assuming it's in the config,
496 call the routine as if it were called from the PBX proper XXX */
498 if (ast_streamfile(chan, "vm-goodbye"))
500 if (ast_waitstream(chan, ""))
516 ast_stopstream(chan);
518 /* Get the deleted messages fixed */
520 for (x=0;x<maxmsg;x++) {
524 nfn = get_fn(dir, curmsg);
526 ast_filerename(fn, nfn, NULL);
531 for (x = curmsg + 1; x<maxmsg; x++) {
534 ast_filedelete(fn, NULL);
543 LOCAL_USER_REMOVE(u);
547 static int vm_exec(struct ast_channel *chan, void *data)
551 char *ext = (char *)data;
554 ast_log(LOG_WARNING, "vm requires an argument (extension)\n");
562 res = leave_voicemail(chan, ext, silent);
563 LOCAL_USER_REMOVE(u);
567 int unload_module(void)
570 STANDARD_HANGUP_LOCALUSERS;
571 res = ast_unregister_application(app);
572 res |= ast_unregister_application(app2);
576 int load_module(void)
579 res = ast_register_application(app, vm_exec);
581 res = ast_register_application(app2, vm_execmain);
585 char *description(void)
593 STANDARD_USECOUNT(res);