Version 0.1.0 from FTP
[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, Adtran Inc. and Linux Support Services, LLC
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");
85         if (!res) {
86                 res = ast_waitstream(chan, AST_DIGIT_ANY);
87                 if (!res) {
88                         res = ast_say_number(chan, msgcnt+1);
89                         if (!res) {
90                                 fn = get_fn(dir, msgcnt);
91                                 if (fn) {
92                                         res = ast_streamfile(chan, fn);
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, *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, chan->context, 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)) {
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                                                 writer = ast_writefile(fn, fmt, comment, O_EXCL, 1 /* check for other formats */, 0700);
186                                                 if (!writer && (errno != EEXIST))
187                                                         break;
188                                                 msgnum++;
189                                         } while(!writer && (msgnum < MAXMSG));
190                                         if (writer) {
191                                                 /* We need to reset these values */
192                                                 free(fmts);
193                                                 fmt = ast_variable_retrieve(cfg, "general", "format");
194                                                 fmts = strdup(fmt);
195                                                 strtok(fmts, "|");
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");
199                                                                 break;
200                                                         }
201                                                         sfmt[fmtcnt++] = strdup(fmt);
202                                                 }
203                                                 for (x=0;x<fmtcnt;x++) {
204                                                         others[x] = ast_writefile(fn, sfmt[x], comment, 0, 0, 0700);
205                                                         if (!others[x]) {
206                                                                 /* Ick, the other format didn't work, but be sure not
207                                                                    to leak memory here */
208                                                                 int y;
209                                                                 for(y=x+1;y < fmtcnt;y++)
210                                                                         free(sfmt[y]);
211                                                                 break;
212                                                         }
213                                                         free(sfmt[x]);
214                                                 }
215                                                 if (x == fmtcnt) {
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);
227                                                                         ast_frfree(f);
228                                                                         /* Exit on any error */
229                                                                         if (res) {
230                                                                                 ast_log(LOG_WARNING, "Error writing frame\n");
231                                                                                 break;
232                                                                         }
233                                                                 }
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);
238                                                                                 outmsg=2;
239                                                                                 break;
240                                                                         }
241                                                                 }
242                                                         }
243                                                         if (!f) {
244                                                                 if (option_verbose > 2) 
245                                                                         ast_verbose( VERBOSE_PREFIX_3 "User hung up\n");
246                                                                 res = -1;
247                                                                 outmsg=1;
248                                                         }
249                                                 } else {
250                                                         ast_log(LOG_WARNING, "Error creating writestream '%s', format '%s'\n", fn, sfmt[x]); 
251                                                         free(sfmt[x]);
252                                                 }
253                                                 ast_closestream(writer);
254                                                 for (x=0;x<fmtcnt;x++) {
255                                                         if (!others[x])
256                                                                 break;
257                                                         ast_closestream(others[x]);
258                                                 }
259                                                 if (outmsg) {
260                                                         if (outmsg > 1) {
261                                                                 /* Let them know it worked */
262                                                                 ast_streamfile(chan, "vm-msgsaved");
263                                                                 ast_waitstream(chan, "");
264                                                         }
265                                                         /* Send e-mail if applicable */
266                                                         if (email) 
267                                                                 sendmail(email, name, msgnum, ext);
268                                                 }
269                                         } else {
270                                                 if (msgnum < MAXMSG)
271                                                         ast_log(LOG_WARNING, "Error writing to mailbox %s\n", ext);
272                                                 else
273                                                         ast_log(LOG_WARNING, "Too many messages in mailbox %s\n", ext);
274                                         }
275                                         if (fn)
276                                                 free(fn);
277                                         free(fmts);
278                                 } else 
279                                         ast_log(LOG_WARNING, "No format to save messages in \n");
280                         }
281                 } else
282                         ast_log(LOG_WARNING, "Unable to playback instructions\n");
283                         
284                 free(dir);
285                 free(copy);
286         } else
287                 ast_log(LOG_WARNING, "No entry in voicemail config file for '%s'\n", ext);
288         ast_destroy(cfg);
289         /* Leave voicemail for someone */
290         return res;
291 }
292
293 static int vm_execmain(struct ast_channel *chan, void *data)
294 {
295         /* XXX This is, admittedly, some pretty horrendus code XXX */
296         int res=-1;
297         int valid = 0;
298         int curmsg = 0;
299         int maxmsg = 0;
300         int x;
301         char *fn, *nfn;
302         char d;
303         struct localuser *u;
304         char username[80];
305         char password[80], *copy;
306         int deleted[MAXMSG];
307         struct ast_config *cfg;
308         int state;
309         char *dir=NULL;
310         
311         LOCAL_USER_ADD(u);
312         cfg = ast_load(VOICEMAIL_CONFIG);
313         if (!cfg) {
314                 ast_log(LOG_WARNING, "No voicemail configuration\n");
315                 goto out;
316         }
317         if (ast_streamfile(chan, "vm-login"))
318                 goto out;
319         do {
320                 /* Prompt for, and read in the username */
321                 if (ast_readstring(chan, username, sizeof(username), 2000, 5000, "#"))
322                         goto out;
323                 if (ast_streamfile(chan, "vm-password"))
324                         goto out;
325                 if (ast_readstring(chan, password, sizeof(password), 2000, 5000, "#"))
326                         goto out;
327                 copy = ast_variable_retrieve(cfg, chan->context, username);
328                 if (copy) {
329                         copy = strdup(copy);
330                         strtok(copy, ",");
331                         if (!strcmp(password,copy))
332                                 valid++;
333                         else if (option_verbose > 2)
334                                 ast_verbose( VERBOSE_PREFIX_3 "Incorrect password '%s' for user '%s'\n", password, username);
335                         free(copy);
336                 } else if (option_verbose > 2)
337                         ast_verbose( VERBOSE_PREFIX_3 "No such user '%s' in config file\n", username);
338                 if (!valid) {
339                         if (ast_streamfile(chan, "vm-incorrect"))
340                                 break;
341                         if (ast_waitstream(chan, ""))
342                                 break;
343                 }
344         } while (!valid);
345         if (valid) {
346                 dir = get_dir(username, "INBOX");
347                 if (!dir) 
348                         goto out;
349
350                 deleted[0] = 0;
351                 /* Find out how many messages are there, mark all as
352                    not deleted. */
353                 do {
354                         fn = get_fn(dir, maxmsg);
355                         if ((res = ast_fileexists(fn, NULL))>0) {
356                                 maxmsg++;
357                                 deleted[maxmsg] = 0;
358                         }
359                         free(fn);
360                 } while(res > 0);
361                 if (ast_streamfile(chan, "vm-youhave"))
362                         goto out;
363                 if ((d=ast_waitstream(chan, AST_DIGIT_ANY)) < 0)
364                         goto out;
365                 ast_stopstream(chan);
366                 if (!d) {
367                         /* If they haven't interrupted us, play the message count */
368                         if (maxmsg > 0) {
369                                 if ((d = ast_say_number(chan, maxmsg)) < 0)
370                                         goto out;
371                         } else {
372                                 if (ast_streamfile(chan, "vm-no"))
373                                         goto out;
374                                 if ((d=ast_waitstream(chan, AST_DIGIT_ANY)) < 0)
375                                         goto out;
376                                 ast_stopstream(chan);
377                         }
378                         if (!d) {
379                                 /* And if they still haven't, give them the last word */
380                                 if (ast_streamfile(chan, ((maxmsg == 1) ? "vm-message" : "vm-messages")))
381                                         goto out;
382                                 if (ast_waitstream(chan, AST_DIGIT_ANY) < 0)
383                                         goto out;
384                                 ast_stopstream(chan);
385                         }
386                 }
387                 res = -1;
388                                 
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")) {
397                         for(;;) {
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);
407                                         }
408                                         if (!d)
409                                                 d = ast_waitfordigit(chan, COMMAND_TIMEOUT);
410                                 } else
411                                         d = ast_waitfordigit(chan, COMMAND_TIMEOUT);
412                                 if (d < 0)
413                                         goto out;
414 restart:
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. */
418                                         switch(state) {
419                                         case STATE_STARTING:
420                                                 if (ast_streamfile(chan, "vm-instructions"))
421                                                         goto out;
422                                                 break;
423                                         case STATE_MESSAGE:
424                                         case STATE_MESSAGE_PLAYING:
425                                                 if (ast_streamfile(chan, "vm-msginstruct"))
426                                                         goto out;
427                                                 break;
428                                         default:
429                                                 ast_log(LOG_WARNING, "What do I do when they timeout/* in state %d?\n", state);
430                                         }
431                                 } else {
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 */
435                                         switch(d) {
436                                         case '0':
437                                                 if (curmsg < maxmsg) {
438                                                         deleted[curmsg] = !deleted[curmsg];
439                                                         if (deleted[curmsg]) {
440                                                                 if (ast_streamfile(chan, "vm-deleted"))
441                                                                         goto out;
442                                                         } else {
443                                                                 if (ast_streamfile(chan, "vm-undeleted"))
444                                                                         goto out;
445                                                         }
446                                                 } else {
447                                                         if (ast_streamfile(chan, "vm-nomore"))
448                                                                 goto out;
449                                                 }
450                                                 break;
451                                         case '1':
452                                                 curmsg = 0;
453                                                 if (maxmsg > 0) {
454                                                         /* Yuck */
455                                                         if ((d = announce_message(chan, dir, curmsg)) > 0)
456                                                                 goto restart;
457                                                         else if (d < 0)
458                                                                 goto out;
459                                                 } else {
460                                                         if (ast_streamfile(chan, "vm-nomore"))
461                                                                 goto out;
462                                                 }
463                                                 state = STATE_MESSAGE_PLAYING;
464                                                 break;
465                                         case '4':
466                                                 if (curmsg > 0)
467                                                         curmsg--;
468                                                 /* Yuck */
469                                                 if ((d = announce_message(chan, dir, curmsg)) > 0)
470                                                         goto restart;
471                                                 else if (d < 0)
472                                                         goto out;
473                                                 state = STATE_MESSAGE_PLAYING;
474                                                 break;
475                                         case '5':
476                                                 if ((d = announce_message(chan, dir, curmsg)) > 0)
477                                                         goto restart;
478                                                 else if (d < 0)
479                                                         goto out;
480                                                 state = STATE_MESSAGE_PLAYING;
481                                                 break;
482                                         case '6':
483                                                 if (curmsg < maxmsg - 1) {
484                                                         curmsg++;
485                                                         if ((d = announce_message(chan, dir, curmsg)) > 0)
486                                                                 goto restart;
487                                                         else if (d < 0)
488                                                                 goto out;
489                                                 } else {
490                                                         if (ast_streamfile(chan, "vm-nomore"))
491                                                                 goto out;
492                                                 }
493                                                 state = STATE_MESSAGE_PLAYING;
494                                                 break;
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 */
497                                         case '#':
498                                                 if (ast_streamfile(chan, "vm-goodbye"))
499                                                         goto out;
500                                                 if (ast_waitstream(chan, ""))
501                                                         goto out;
502                                                 res = 0;
503                                                 goto out;
504                                                 break;
505                                         default:
506                                                 /* Double yuck */
507                                                 d = '*';
508                                                 goto restart;
509                                         }
510                                 }
511                         }
512                 }
513         }
514         
515 out:
516         ast_stopstream(chan);
517         if (maxmsg) {
518                 /* Get the deleted messages fixed */
519                 curmsg = -1;
520                 for (x=0;x<maxmsg;x++) {
521                         if (!deleted[x]) {
522                                 curmsg++;
523                                 fn = get_fn(dir, x);
524                                 nfn = get_fn(dir, curmsg);
525                                 if (strcmp(fn, nfn))
526                                         ast_filerename(fn, nfn, NULL);
527                                 free(fn);
528                                 free(nfn);
529                         }
530                 }
531                 for (x = curmsg + 1; x<maxmsg; x++) {
532                         fn = get_fn(dir, x);
533                         if (fn) {
534                                 ast_filedelete(fn, NULL);
535                                 free(fn);
536                         }
537                 }
538         }
539         if (dir)
540                 free(dir);
541         if (cfg)
542                 ast_destroy(cfg);
543         LOCAL_USER_REMOVE(u);
544         return res;
545 }
546
547 static int vm_exec(struct ast_channel *chan, void *data)
548 {
549         int res=0, silent=0;
550         struct localuser *u;
551         char *ext = (char *)data;
552         
553         if (!data) {
554                 ast_log(LOG_WARNING, "vm requires an argument (extension)\n");
555                 return -1;
556         }
557         LOCAL_USER_ADD(u);
558         if (*ext == 's') {
559                 silent++;
560                 ext++;
561         }
562         res = leave_voicemail(chan, ext, silent);
563         LOCAL_USER_REMOVE(u);
564         return res;
565 }
566
567 int unload_module(void)
568 {
569         int res;
570         STANDARD_HANGUP_LOCALUSERS;
571         res = ast_unregister_application(app);
572         res |= ast_unregister_application(app2);
573         return res;
574 }
575
576 int load_module(void)
577 {
578         int res;
579         res = ast_register_application(app, vm_exec);
580         if (!res)
581                 res = ast_register_application(app2, vm_execmain);
582         return res;
583 }
584
585 char *description(void)
586 {
587         return tdesc;
588 }
589
590 int usecount(void)
591 {
592         int res;
593         STANDARD_USECOUNT(res);
594         return res;
595 }