2 * Asterisk -- A telephony toolkit for Linux.
4 * Voicemail System (did you ever think it could be so easy?)
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
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", chan->language);
86 res = ast_waitstream(chan, AST_DIGIT_ANY);
88 res = ast_say_number(chan, msgcnt+1, chan->language);
90 fn = get_fn(dir, msgcnt);
92 res = ast_streamfile(chan, fn, chan->language);
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=NULL, *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, NULL, 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, chan->language)) {
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 if (ast_fileexists(fn, NULL, chan->language) > 0) {
189 writer = ast_writefile(fn, fmt, comment, O_EXCL, 1 /* check for other formats */, 0700);
193 } while(!writer && (msgnum < MAXMSG));
195 /* We need to reset these values */
197 fmt = ast_variable_retrieve(cfg, "general", "format");
200 while((fmt = strtok(NULL, "|"))) {
201 if (fmtcnt > MAX_OTHER_FORMATS - 1) {
202 ast_log(LOG_WARNING, "Please increase MAX_OTHER_FORMATS in app_voicemail.c\n");
205 sfmt[fmtcnt++] = strdup(fmt);
207 for (x=0;x<fmtcnt;x++) {
208 others[x] = ast_writefile(fn, sfmt[x], comment, 0, 0, 0700);
210 /* Ick, the other format didn't work, but be sure not
211 to leak memory here */
213 for(y=x+1;y < fmtcnt;y++)
220 /* Loop forever, writing the packets we read to the writer(s), until
221 we read a # or get a hangup */
222 if (option_verbose > 2)
223 ast_verbose( VERBOSE_PREFIX_3 "Recording to %s\n", fn);
224 while((f = ast_read(chan))) {
225 if (f->frametype == AST_FRAME_VOICE) {
226 /* Write the primary format */
227 res = ast_writestream(writer, f);
229 ast_log(LOG_WARNING, "Error writing primary frame\n");
232 /* And each of the others */
233 for (x=0;x<fmtcnt;x++) {
234 res |= ast_writestream(others[x], f);
237 /* Exit on any error */
239 ast_log(LOG_WARNING, "Error writing frame\n");
243 if (f->frametype == AST_FRAME_DTMF) {
244 if (f->subclass == '#') {
245 if (option_verbose > 2)
246 ast_verbose( VERBOSE_PREFIX_3 "User ended message by pressing %c\n", f->subclass);
253 if (option_verbose > 2)
254 ast_verbose( VERBOSE_PREFIX_3 "User hung up\n");
259 ast_log(LOG_WARNING, "Error creating writestream '%s', format '%s'\n", fn, sfmt[x]);
262 ast_closestream(writer);
263 for (x=0;x<fmtcnt;x++) {
266 ast_closestream(others[x]);
270 /* Let them know it worked */
271 ast_streamfile(chan, "vm-msgsaved", chan->language);
272 ast_waitstream(chan, "");
274 /* Send e-mail if applicable */
276 sendmail(email, name, msgnum, ext);
280 ast_log(LOG_WARNING, "Error writing to mailbox %s\n", ext);
282 ast_log(LOG_WARNING, "Too many messages in mailbox %s\n", ext);
288 ast_log(LOG_WARNING, "No format to save messages in \n");
291 ast_log(LOG_WARNING, "Unable to playback instructions\n");
296 ast_log(LOG_WARNING, "No entry in voicemail config file for '%s'\n", ext);
298 /* Leave voicemail for someone */
302 static int vm_execmain(struct ast_channel *chan, void *data)
304 /* XXX This is, admittedly, some pretty horrendus code XXX */
314 char password[80], *copy;
316 struct ast_config *cfg;
321 cfg = ast_load(VOICEMAIL_CONFIG);
323 ast_log(LOG_WARNING, "No voicemail configuration\n");
326 if (ast_streamfile(chan, "vm-login", chan->language)) {
327 ast_log(LOG_WARNING, "Couldn't stream login file\n");
331 /* Prompt for, and read in the username */
332 if (ast_readstring(chan, username, sizeof(username), 2000, 5000, "#")) {
333 ast_log(LOG_WARNING, "Couldn't read username\n");
336 if (!strlen(username)) {
337 if (option_verbose > 2)
338 ast_verbose(VERBOSE_PREFIX_3 "Username not entered\n");
342 if (ast_streamfile(chan, "vm-password", chan->language)) {
343 ast_log(LOG_WARNING, "Unable to stream password file\n");
346 if (ast_readstring(chan, password, sizeof(password), 2000, 5000, "#")) {
347 ast_log(LOG_WARNING, "Unable to read password\n");
350 copy = ast_variable_retrieve(cfg, NULL, username);
354 if (!strcmp(password,copy))
356 else if (option_verbose > 2)
357 ast_verbose( VERBOSE_PREFIX_3 "Incorrect password '%s' for user '%s'\n", password, username);
359 } else if (option_verbose > 2)
360 ast_verbose( VERBOSE_PREFIX_3 "No such user '%s' in config file\n", username);
362 if (ast_streamfile(chan, "vm-incorrect", chan->language))
364 if (ast_waitstream(chan, ""))
369 dir = get_dir(username, "INBOX");
374 /* Find out how many messages are there, mark all as
377 fn = get_fn(dir, maxmsg);
378 if ((res = ast_fileexists(fn, NULL, chan->language))>0) {
384 if (ast_streamfile(chan, "vm-youhave", chan->language))
386 if ((d=ast_waitstream(chan, AST_DIGIT_ANY)) < 0)
388 ast_stopstream(chan);
390 /* If they haven't interrupted us, play the message count */
392 if ((d = ast_say_number(chan, maxmsg, chan->language)) < 0)
395 if (ast_streamfile(chan, "vm-no", chan->language))
397 if ((d=ast_waitstream(chan, AST_DIGIT_ANY)) < 0)
399 ast_stopstream(chan);
402 /* And if they still haven't, give them the last word */
403 if (ast_streamfile(chan, ((maxmsg == 1) ? "vm-message" : "vm-messages"), chan->language))
405 if (ast_waitstream(chan, AST_DIGIT_ANY) < 0)
407 ast_stopstream(chan);
412 #define STATE_STARTING 1
413 #define STATE_MESSAGE 2
414 #define STATE_MESSAGE_PLAYING 3
415 state = STATE_STARTING;
416 ast_log(LOG_EVENT, "User '%s' logged in on channel '%s' with %d message(s).\n", username, chan->name, maxmsg);
417 if (option_verbose > 2)
418 ast_verbose( VERBOSE_PREFIX_3 "User '%s' logged in on channel %s with %d messages\n", username, chan->name, maxmsg);
419 if (!ast_streamfile(chan, "vm-instructions", chan->language)) {
421 if (chan->stream || (chan->trans && chan->trans->stream)) {
422 d = ast_waitstream(chan, AST_DIGIT_ANY);
423 ast_stopstream(chan);
424 if (!d && (state == STATE_MESSAGE_PLAYING)) {
425 state = STATE_MESSAGE;
426 /* If it runs out playing a message, then give directions */
427 if (!(d = ast_streamfile(chan, "vm-msginstruct", chan->language)))
428 d = ast_waitstream(chan, AST_DIGIT_ANY);
429 ast_stopstream(chan);
432 d = ast_waitfordigit(chan, COMMAND_TIMEOUT);
434 d = ast_waitfordigit(chan, COMMAND_TIMEOUT);
438 if (!d || (d == '*')) {
439 /* If they don't say anything, play back a message. We'll decide which one is
440 best based up on where they are. Ditto if they press the '*' key. */
443 if (ast_streamfile(chan, "vm-instructions", chan->language))
447 case STATE_MESSAGE_PLAYING:
448 if (ast_streamfile(chan, "vm-msginstruct", chan->language))
452 ast_log(LOG_WARNING, "What do I do when they timeout/* in state %d?\n", state);
455 /* XXX Should we be command-compatible with Meridian mail? Their system seems
456 very confusing, but also widely used XXX */
457 /* They've entered (or started to enter) a command */
460 if (curmsg < maxmsg) {
461 deleted[curmsg] = !deleted[curmsg];
462 if (deleted[curmsg]) {
463 if (ast_streamfile(chan, "vm-deleted", chan->language))
466 if (ast_streamfile(chan, "vm-undeleted", chan->language))
470 if (ast_streamfile(chan, "vm-nomore", chan->language))
478 if ((d = announce_message(chan, dir, curmsg)) > 0)
483 if (ast_streamfile(chan, "vm-nomore", chan->language))
486 state = STATE_MESSAGE_PLAYING;
492 if ((d = announce_message(chan, dir, curmsg)) > 0)
496 state = STATE_MESSAGE_PLAYING;
499 if ((d = announce_message(chan, dir, curmsg)) > 0)
503 state = STATE_MESSAGE_PLAYING;
506 if (curmsg < maxmsg - 1) {
508 if ((d = announce_message(chan, dir, curmsg)) > 0)
513 if (ast_streamfile(chan, "vm-nomore", chan->language))
516 state = STATE_MESSAGE_PLAYING;
518 /* XXX Message compose? It's easy! Just read their # and, assuming it's in the config,
519 call the routine as if it were called from the PBX proper XXX */
521 if (ast_streamfile(chan, "vm-goodbye", chan->language))
523 if (ast_waitstream(chan, ""))
539 ast_stopstream(chan);
541 /* Get the deleted messages fixed */
543 for (x=0;x<maxmsg;x++) {
547 nfn = get_fn(dir, curmsg);
549 ast_filerename(fn, nfn, NULL);
554 for (x = curmsg + 1; x<maxmsg; x++) {
557 ast_filedelete(fn, NULL);
566 LOCAL_USER_REMOVE(u);
570 static int vm_exec(struct ast_channel *chan, void *data)
574 char *ext = (char *)data;
577 ast_log(LOG_WARNING, "vm requires an argument (extension)\n");
585 res = leave_voicemail(chan, ext, silent);
586 LOCAL_USER_REMOVE(u);
590 int unload_module(void)
593 STANDARD_HANGUP_LOCALUSERS;
594 res = ast_unregister_application(app);
595 res |= ast_unregister_application(app2);
599 int load_module(void)
602 res = ast_register_application(app, vm_exec);
604 res = ast_register_application(app2, vm_execmain);
608 char *description(void)
616 STANDARD_USECOUNT(res);