939587ff77b970b6722a50be723f0e9773307381
[asterisk/asterisk.git] / apps / app_voicemail.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Voicemail System (did you ever think it could be so easy?)
5  * 
6  * Copyright (C) 1999, Mark Spencer
7  *
8  * Mark Spencer <markster@linux-support.net>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  */
13
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>
22 #include <stdlib.h>
23 #include <errno.h>
24 #include <unistd.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <sys/time.h>
29 #include <sys/stat.h>
30 #include <time.h>
31
32 #include <pthread.h>
33 #include "../asterisk.h"
34
35 #define COMMAND_TIMEOUT 5000
36
37 #define VOICEMAIL_CONFIG "voicemail.conf"
38 #define ASTERISK_USERNAME "asterisk"
39
40 /*
41 #define HOSTNAME_OVERRIDE "linux-support.net"
42 */
43
44 #define SENDMAIL "/usr/sbin/sendmail -t"
45
46 #define INTRO "vm-intro"
47
48 #define MAXMSG 100
49
50 #define MAX_OTHER_FORMATS 10
51
52 #define VM_SPOOL_DIR AST_SPOOL_DIR "/vm"
53
54
55 static char *tdesc = "Comedian Mail (Voicemail System)";
56
57 /* Leave a message */
58 static char *app = "VoiceMail";
59
60 /* Check mail, control, etc */
61 static char *app2 = "VoiceMailMain";
62
63 STANDARD_LOCAL_USER;
64
65 LOCAL_USER_DECL;
66
67 static char *get_dir(char *ext, char *mailbox)
68 {
69         char *tmp = malloc(strlen(ext) + strlen(VM_SPOOL_DIR) + 3 + strlen(mailbox));
70         sprintf(tmp, "%s/%s/%s", VM_SPOOL_DIR, ext, mailbox);
71         return tmp;
72 }
73 static char *get_fn(char *dir, int num)
74 {
75         char *tmp = malloc(strlen(dir) + 10);
76         sprintf(tmp, "%s/msg%04d", dir, num);
77         return tmp;
78 }
79
80 static int announce_message(struct ast_channel *chan, char *dir, int msgcnt)
81 {
82         char *fn;
83         int res;
84         res = ast_streamfile(chan, "vm-message", chan->language);
85         if (!res) {
86                 res = ast_waitstream(chan, AST_DIGIT_ANY);
87                 if (!res) {
88                         res = ast_say_number(chan, msgcnt+1, chan->language);
89                         if (!res) {
90                                 fn = get_fn(dir, msgcnt);
91                                 if (fn) {
92                                         res = ast_streamfile(chan, fn, chan->language);
93                                         free(fn);
94                                 }
95                         }
96                 }
97         }
98         if (res < 0)
99                 ast_log(LOG_WARNING, "Unable to announce message\n");
100         return res;
101 }
102 static int sendmail(char *email, char *name, int msgnum, char *mailbox)
103 {
104         FILE *p;
105         char date[256];
106         char host[256];
107         time_t t;
108         struct tm *tm;
109         p = popen(SENDMAIL, "w");
110         if (p) {
111                 gethostname(host, sizeof(host));
112                 time(&t);
113                 tm = localtime(&t);
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
119                                 HOSTNAME_OVERRIDE
120 #else
121                                 host
122 #endif
123                                 );
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);
130                 fprintf(p, ".\n");
131                 pclose(p);
132         } else {
133                 ast_log(LOG_WARNING, "Unable to launch '%s'\n", SENDMAIL);
134                 return -1;
135         }
136         return 0;
137 }
138
139 static int leave_voicemail(struct ast_channel *chan, char *ext, int silent)
140 {
141         struct ast_config *cfg;
142         char *copy, *name, *passwd, *email, *dir, *fmt, *fmts, *fn=NULL;
143         char comment[256];
144         struct ast_filestream *writer=NULL, *others[MAX_OTHER_FORMATS];
145         char *sfmt[MAX_OTHER_FORMATS];
146         int res = -1, fmtcnt=0, x;
147         int msgnum;
148         int outmsg=0;
149         struct ast_frame *f;
150         
151         cfg = ast_load(VOICEMAIL_CONFIG);
152         if (!cfg) {
153                 ast_log(LOG_WARNING, "No such configuration file %s\n", VOICEMAIL_CONFIG);
154                 return -1;
155         }
156         if ((copy = ast_variable_retrieve(cfg, NULL, ext))) {
157                 /* Make sure they have an entry in the config */
158                 copy = strdup(copy);
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");
174                                 if (fmt) {
175                                         fmts = strdup(fmt);
176                                         fmt = strtok(fmts, "|");
177                                         msgnum = 0;
178                                         do {
179                                                 if (fn)
180                                                         free(fn);
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) {
186                                                         msgnum++;
187                                                         continue;
188                                                 }
189                                                 writer = ast_writefile(fn, fmt, comment, O_EXCL, 1 /* check for other formats */, 0700);
190                                                 if (!writer)
191                                                         break;
192                                                 msgnum++;
193                                         } while(!writer && (msgnum < MAXMSG));
194                                         if (writer) {
195                                                 /* We need to reset these values */
196                                                 free(fmts);
197                                                 fmt = ast_variable_retrieve(cfg, "general", "format");
198                                                 fmts = strdup(fmt);
199                                                 strtok(fmts, "|");
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");
203                                                                 break;
204                                                         }
205                                                         sfmt[fmtcnt++] = strdup(fmt);
206                                                 }
207                                                 for (x=0;x<fmtcnt;x++) {
208                                                         others[x] = ast_writefile(fn, sfmt[x], comment, 0, 0, 0700);
209                                                         if (!others[x]) {
210                                                                 /* Ick, the other format didn't work, but be sure not
211                                                                    to leak memory here */
212                                                                 int y;
213                                                                 for(y=x+1;y < fmtcnt;y++)
214                                                                         free(sfmt[y]);
215                                                                 break;
216                                                         }
217                                                         free(sfmt[x]);
218                                                 }
219                                                 if (x == fmtcnt) {
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);
228                                                                         if (res) {
229                                                                                 ast_log(LOG_WARNING, "Error writing primary frame\n");
230                                                                                 break;
231                                                                         }
232                                                                         /* And each of the others */
233                                                                         for (x=0;x<fmtcnt;x++) {
234                                                                                 res |= ast_writestream(others[x], f);
235                                                                         }
236                                                                         ast_frfree(f);
237                                                                         /* Exit on any error */
238                                                                         if (res) {
239                                                                                 ast_log(LOG_WARNING, "Error writing frame\n");
240                                                                                 break;
241                                                                         }
242                                                                 }
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);
247                                                                                 outmsg=2;
248                                                                                 break;
249                                                                         }
250                                                                 }
251                                                         }
252                                                         if (!f) {
253                                                                 if (option_verbose > 2) 
254                                                                         ast_verbose( VERBOSE_PREFIX_3 "User hung up\n");
255                                                                 res = -1;
256                                                                 outmsg=1;
257                                                         }
258                                                 } else {
259                                                         ast_log(LOG_WARNING, "Error creating writestream '%s', format '%s'\n", fn, sfmt[x]); 
260                                                         free(sfmt[x]);
261                                                 }
262                                                 ast_closestream(writer);
263                                                 for (x=0;x<fmtcnt;x++) {
264                                                         if (!others[x])
265                                                                 break;
266                                                         ast_closestream(others[x]);
267                                                 }
268                                                 if (outmsg) {
269                                                         if (outmsg > 1) {
270                                                                 /* Let them know it worked */
271                                                                 ast_streamfile(chan, "vm-msgsaved", chan->language);
272                                                                 ast_waitstream(chan, "");
273                                                         }
274                                                         /* Send e-mail if applicable */
275                                                         if (email) 
276                                                                 sendmail(email, name, msgnum, ext);
277                                                 }
278                                         } else {
279                                                 if (msgnum < MAXMSG)
280                                                         ast_log(LOG_WARNING, "Error writing to mailbox %s\n", ext);
281                                                 else
282                                                         ast_log(LOG_WARNING, "Too many messages in mailbox %s\n", ext);
283                                         }
284                                         if (fn)
285                                                 free(fn);
286                                         free(fmts);
287                                 } else 
288                                         ast_log(LOG_WARNING, "No format to save messages in \n");
289                         }
290                 } else
291                         ast_log(LOG_WARNING, "Unable to playback instructions\n");
292                         
293                 free(dir);
294                 free(copy);
295         } else
296                 ast_log(LOG_WARNING, "No entry in voicemail config file for '%s'\n", ext);
297         ast_destroy(cfg);
298         /* Leave voicemail for someone */
299         return res;
300 }
301
302 static int vm_execmain(struct ast_channel *chan, void *data)
303 {
304         /* XXX This is, admittedly, some pretty horrendus code XXX */
305         int res=-1;
306         int valid = 0;
307         int curmsg = 0;
308         int maxmsg = 0;
309         int x;
310         char *fn, *nfn;
311         char d;
312         struct localuser *u;
313         char username[80];
314         char password[80], *copy;
315         int deleted[MAXMSG];
316         struct ast_config *cfg;
317         int state;
318         char *dir=NULL;
319         
320         LOCAL_USER_ADD(u);
321         cfg = ast_load(VOICEMAIL_CONFIG);
322         if (!cfg) {
323                 ast_log(LOG_WARNING, "No voicemail configuration\n");
324                 goto out;
325         }
326         if (ast_streamfile(chan, "vm-login", chan->language)) {
327                 ast_log(LOG_WARNING, "Couldn't stream login file\n");
328                 goto out;
329         }
330         do {
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");
334                         goto out;
335                 }                       
336                 if (!strlen(username)) {
337                         if (option_verbose > 2)
338                                 ast_verbose(VERBOSE_PREFIX_3 "Username not entered\n");
339                         res = 0;
340                         goto out;
341                 }
342                 if (ast_streamfile(chan, "vm-password", chan->language)) {
343                         ast_log(LOG_WARNING, "Unable to stream password file\n");
344                         goto out;
345                 }
346                 if (ast_readstring(chan, password, sizeof(password), 2000, 5000, "#")) {
347                         ast_log(LOG_WARNING, "Unable to read password\n");
348                         goto out;
349                 }
350                 copy = ast_variable_retrieve(cfg, NULL, username);
351                 if (copy) {
352                         copy = strdup(copy);
353                         strtok(copy, ",");
354                         if (!strcmp(password,copy))
355                                 valid++;
356                         else if (option_verbose > 2)
357                                 ast_verbose( VERBOSE_PREFIX_3 "Incorrect password '%s' for user '%s'\n", password, username);
358                         free(copy);
359                 } else if (option_verbose > 2)
360                         ast_verbose( VERBOSE_PREFIX_3 "No such user '%s' in config file\n", username);
361                 if (!valid) {
362                         if (ast_streamfile(chan, "vm-incorrect", chan->language))
363                                 break;
364                         if (ast_waitstream(chan, ""))
365                                 break;
366                 }
367         } while (!valid);
368         if (valid) {
369                 dir = get_dir(username, "INBOX");
370                 if (!dir) 
371                         goto out;
372
373                 deleted[0] = 0;
374                 /* Find out how many messages are there, mark all as
375                    not deleted. */
376                 do {
377                         fn = get_fn(dir, maxmsg);
378                         if ((res = ast_fileexists(fn, NULL, chan->language))>0) {
379                                 maxmsg++;
380                                 deleted[maxmsg] = 0;
381                         }
382                         free(fn);
383                 } while(res > 0);
384                 if (ast_streamfile(chan, "vm-youhave", chan->language))
385                         goto out;
386                 if ((d=ast_waitstream(chan, AST_DIGIT_ANY)) < 0)
387                         goto out;
388                 ast_stopstream(chan);
389                 if (!d) {
390                         /* If they haven't interrupted us, play the message count */
391                         if (maxmsg > 0) {
392                                 if ((d = ast_say_number(chan, maxmsg, chan->language)) < 0)
393                                         goto out;
394                         } else {
395                                 if (ast_streamfile(chan, "vm-no", chan->language))
396                                         goto out;
397                                 if ((d=ast_waitstream(chan, AST_DIGIT_ANY)) < 0)
398                                         goto out;
399                                 ast_stopstream(chan);
400                         }
401                         if (!d) {
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))
404                                         goto out;
405                                 if (ast_waitstream(chan, AST_DIGIT_ANY) < 0)
406                                         goto out;
407                                 ast_stopstream(chan);
408                         }
409                 }
410                 res = -1;
411                                 
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)) {
420                         for(;;) {
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);
430                                         }
431                                         if (!d)
432                                                 d = ast_waitfordigit(chan, COMMAND_TIMEOUT);
433                                 } else
434                                         d = ast_waitfordigit(chan, COMMAND_TIMEOUT);
435                                 if (d < 0)
436                                         goto out;
437 restart:
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. */
441                                         switch(state) {
442                                         case STATE_STARTING:
443                                                 if (ast_streamfile(chan, "vm-instructions", chan->language))
444                                                         goto out;
445                                                 break;
446                                         case STATE_MESSAGE:
447                                         case STATE_MESSAGE_PLAYING:
448                                                 if (ast_streamfile(chan, "vm-msginstruct", chan->language))
449                                                         goto out;
450                                                 break;
451                                         default:
452                                                 ast_log(LOG_WARNING, "What do I do when they timeout/* in state %d?\n", state);
453                                         }
454                                 } else {
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 */
458                                         switch(d) {
459                                         case '0':
460                                                 if (curmsg < maxmsg) {
461                                                         deleted[curmsg] = !deleted[curmsg];
462                                                         if (deleted[curmsg]) {
463                                                                 if (ast_streamfile(chan, "vm-deleted", chan->language))
464                                                                         goto out;
465                                                         } else {
466                                                                 if (ast_streamfile(chan, "vm-undeleted", chan->language))
467                                                                         goto out;
468                                                         }
469                                                 } else {
470                                                         if (ast_streamfile(chan, "vm-nomore", chan->language))
471                                                                 goto out;
472                                                 }
473                                                 break;
474                                         case '1':
475                                                 curmsg = 0;
476                                                 if (maxmsg > 0) {
477                                                         /* Yuck */
478                                                         if ((d = announce_message(chan, dir, curmsg)) > 0)
479                                                                 goto restart;
480                                                         else if (d < 0)
481                                                                 goto out;
482                                                 } else {
483                                                         if (ast_streamfile(chan, "vm-nomore", chan->language))
484                                                                 goto out;
485                                                 }
486                                                 state = STATE_MESSAGE_PLAYING;
487                                                 break;
488                                         case '4':
489                                                 if (curmsg > 0)
490                                                         curmsg--;
491                                                 /* Yuck */
492                                                 if ((d = announce_message(chan, dir, curmsg)) > 0)
493                                                         goto restart;
494                                                 else if (d < 0)
495                                                         goto out;
496                                                 state = STATE_MESSAGE_PLAYING;
497                                                 break;
498                                         case '5':
499                                                 if ((d = announce_message(chan, dir, curmsg)) > 0)
500                                                         goto restart;
501                                                 else if (d < 0)
502                                                         goto out;
503                                                 state = STATE_MESSAGE_PLAYING;
504                                                 break;
505                                         case '6':
506                                                 if (curmsg < maxmsg - 1) {
507                                                         curmsg++;
508                                                         if ((d = announce_message(chan, dir, curmsg)) > 0)
509                                                                 goto restart;
510                                                         else if (d < 0)
511                                                                 goto out;
512                                                 } else {
513                                                         if (ast_streamfile(chan, "vm-nomore", chan->language))
514                                                                 goto out;
515                                                 }
516                                                 state = STATE_MESSAGE_PLAYING;
517                                                 break;
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 */
520                                         case '#':
521                                                 if (ast_streamfile(chan, "vm-goodbye", chan->language))
522                                                         goto out;
523                                                 if (ast_waitstream(chan, ""))
524                                                         goto out;
525                                                 res = 0;
526                                                 goto out;
527                                                 break;
528                                         default:
529                                                 /* Double yuck */
530                                                 d = '*';
531                                                 goto restart;
532                                         }
533                                 }
534                         }
535                 }
536         }
537         
538 out:
539         ast_stopstream(chan);
540         if (maxmsg) {
541                 /* Get the deleted messages fixed */
542                 curmsg = -1;
543                 for (x=0;x<maxmsg;x++) {
544                         if (!deleted[x]) {
545                                 curmsg++;
546                                 fn = get_fn(dir, x);
547                                 nfn = get_fn(dir, curmsg);
548                                 if (strcmp(fn, nfn))
549                                         ast_filerename(fn, nfn, NULL);
550                                 free(fn);
551                                 free(nfn);
552                         }
553                 }
554                 for (x = curmsg + 1; x<maxmsg; x++) {
555                         fn = get_fn(dir, x);
556                         if (fn) {
557                                 ast_filedelete(fn, NULL);
558                                 free(fn);
559                         }
560                 }
561         }
562         if (dir)
563                 free(dir);
564         if (cfg)
565                 ast_destroy(cfg);
566         LOCAL_USER_REMOVE(u);
567         return res;
568 }
569
570 static int vm_exec(struct ast_channel *chan, void *data)
571 {
572         int res=0, silent=0;
573         struct localuser *u;
574         char *ext = (char *)data;
575         
576         if (!data) {
577                 ast_log(LOG_WARNING, "vm requires an argument (extension)\n");
578                 return -1;
579         }
580         LOCAL_USER_ADD(u);
581         if (*ext == 's') {
582                 silent++;
583                 ext++;
584         }
585         res = leave_voicemail(chan, ext, silent);
586         LOCAL_USER_REMOVE(u);
587         return res;
588 }
589
590 int unload_module(void)
591 {
592         int res;
593         STANDARD_HANGUP_LOCALUSERS;
594         res = ast_unregister_application(app);
595         res |= ast_unregister_application(app2);
596         return res;
597 }
598
599 int load_module(void)
600 {
601         int res;
602         res = ast_register_application(app, vm_exec);
603         if (!res)
604                 res = ast_register_application(app2, vm_execmain);
605         return res;
606 }
607
608 char *description(void)
609 {
610         return tdesc;
611 }
612
613 int usecount(void)
614 {
615         int res;
616         STANDARD_USECOUNT(res);
617         return res;
618 }