Version 0.1.1 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, 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");
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, 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)) {
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 (!strlen(username)) {
324                         res = 0;
325                         goto out;
326                 }
327                 if (ast_streamfile(chan, "vm-password"))
328                         goto out;
329                 if (ast_readstring(chan, password, sizeof(password), 2000, 5000, "#"))
330                         goto out;
331                 copy = ast_variable_retrieve(cfg, NULL, username);
332                 if (copy) {
333                         copy = strdup(copy);
334                         strtok(copy, ",");
335                         if (!strcmp(password,copy))
336                                 valid++;
337                         else if (option_verbose > 2)
338                                 ast_verbose( VERBOSE_PREFIX_3 "Incorrect password '%s' for user '%s'\n", password, username);
339                         free(copy);
340                 } else if (option_verbose > 2)
341                         ast_verbose( VERBOSE_PREFIX_3 "No such user '%s' in config file\n", username);
342                 if (!valid) {
343                         if (ast_streamfile(chan, "vm-incorrect"))
344                                 break;
345                         if (ast_waitstream(chan, ""))
346                                 break;
347                 }
348         } while (!valid);
349         if (valid) {
350                 dir = get_dir(username, "INBOX");
351                 if (!dir) 
352                         goto out;
353
354                 deleted[0] = 0;
355                 /* Find out how many messages are there, mark all as
356                    not deleted. */
357                 do {
358                         fn = get_fn(dir, maxmsg);
359                         if ((res = ast_fileexists(fn, NULL))>0) {
360                                 maxmsg++;
361                                 deleted[maxmsg] = 0;
362                         }
363                         free(fn);
364                 } while(res > 0);
365                 if (ast_streamfile(chan, "vm-youhave"))
366                         goto out;
367                 if ((d=ast_waitstream(chan, AST_DIGIT_ANY)) < 0)
368                         goto out;
369                 ast_stopstream(chan);
370                 if (!d) {
371                         /* If they haven't interrupted us, play the message count */
372                         if (maxmsg > 0) {
373                                 if ((d = ast_say_number(chan, maxmsg)) < 0)
374                                         goto out;
375                         } else {
376                                 if (ast_streamfile(chan, "vm-no"))
377                                         goto out;
378                                 if ((d=ast_waitstream(chan, AST_DIGIT_ANY)) < 0)
379                                         goto out;
380                                 ast_stopstream(chan);
381                         }
382                         if (!d) {
383                                 /* And if they still haven't, give them the last word */
384                                 if (ast_streamfile(chan, ((maxmsg == 1) ? "vm-message" : "vm-messages")))
385                                         goto out;
386                                 if (ast_waitstream(chan, AST_DIGIT_ANY) < 0)
387                                         goto out;
388                                 ast_stopstream(chan);
389                         }
390                 }
391                 res = -1;
392                                 
393 #define STATE_STARTING 1
394 #define STATE_MESSAGE 2
395 #define STATE_MESSAGE_PLAYING 3
396                 state = STATE_STARTING;
397                 ast_log(LOG_EVENT, "User '%s' logged in on channel '%s' with %d message(s).\n", username, chan->name, maxmsg);
398                 if (option_verbose > 2)
399                         ast_verbose( VERBOSE_PREFIX_3 "User '%s' logged in on channel %s with %d messages\n", username, chan->name, maxmsg);
400                 if (!ast_streamfile(chan, "vm-instructions")) {
401                         for(;;) {
402                                 if (chan->stream || (chan->trans && chan->trans->stream)) {
403                                         d = ast_waitstream(chan, AST_DIGIT_ANY);
404                                         ast_stopstream(chan);
405                                         if (!d && (state == STATE_MESSAGE_PLAYING)) {
406                                                 state  = STATE_MESSAGE;
407                                                 /* If it runs out playing a message, then give directions */
408                                                 if (!(d = ast_streamfile(chan, "vm-msginstruct")))
409                                                         d = ast_waitstream(chan, AST_DIGIT_ANY);
410                                                 ast_stopstream(chan);
411                                         }
412                                         if (!d)
413                                                 d = ast_waitfordigit(chan, COMMAND_TIMEOUT);
414                                 } else
415                                         d = ast_waitfordigit(chan, COMMAND_TIMEOUT);
416                                 if (d < 0)
417                                         goto out;
418 restart:
419                                 if (!d || (d == '*')) {
420                                         /* If they don't say anything, play back a message.  We'll decide which one is
421                                            best based up on where they are.  Ditto if they press the '*' key. */
422                                         switch(state) {
423                                         case STATE_STARTING:
424                                                 if (ast_streamfile(chan, "vm-instructions"))
425                                                         goto out;
426                                                 break;
427                                         case STATE_MESSAGE:
428                                         case STATE_MESSAGE_PLAYING:
429                                                 if (ast_streamfile(chan, "vm-msginstruct"))
430                                                         goto out;
431                                                 break;
432                                         default:
433                                                 ast_log(LOG_WARNING, "What do I do when they timeout/* in state %d?\n", state);
434                                         }
435                                 } else {
436                                         /* XXX Should we be command-compatible with Meridian mail?  Their system seems
437                                                very confusing, but also widely used XXX */
438                                         /* They've entered (or started to enter) a command */
439                                         switch(d) {
440                                         case '0':
441                                                 if (curmsg < maxmsg) {
442                                                         deleted[curmsg] = !deleted[curmsg];
443                                                         if (deleted[curmsg]) {
444                                                                 if (ast_streamfile(chan, "vm-deleted"))
445                                                                         goto out;
446                                                         } else {
447                                                                 if (ast_streamfile(chan, "vm-undeleted"))
448                                                                         goto out;
449                                                         }
450                                                 } else {
451                                                         if (ast_streamfile(chan, "vm-nomore"))
452                                                                 goto out;
453                                                 }
454                                                 break;
455                                         case '1':
456                                                 curmsg = 0;
457                                                 if (maxmsg > 0) {
458                                                         /* Yuck */
459                                                         if ((d = announce_message(chan, dir, curmsg)) > 0)
460                                                                 goto restart;
461                                                         else if (d < 0)
462                                                                 goto out;
463                                                 } else {
464                                                         if (ast_streamfile(chan, "vm-nomore"))
465                                                                 goto out;
466                                                 }
467                                                 state = STATE_MESSAGE_PLAYING;
468                                                 break;
469                                         case '4':
470                                                 if (curmsg > 0)
471                                                         curmsg--;
472                                                 /* Yuck */
473                                                 if ((d = announce_message(chan, dir, curmsg)) > 0)
474                                                         goto restart;
475                                                 else if (d < 0)
476                                                         goto out;
477                                                 state = STATE_MESSAGE_PLAYING;
478                                                 break;
479                                         case '5':
480                                                 if ((d = announce_message(chan, dir, curmsg)) > 0)
481                                                         goto restart;
482                                                 else if (d < 0)
483                                                         goto out;
484                                                 state = STATE_MESSAGE_PLAYING;
485                                                 break;
486                                         case '6':
487                                                 if (curmsg < maxmsg - 1) {
488                                                         curmsg++;
489                                                         if ((d = announce_message(chan, dir, curmsg)) > 0)
490                                                                 goto restart;
491                                                         else if (d < 0)
492                                                                 goto out;
493                                                 } else {
494                                                         if (ast_streamfile(chan, "vm-nomore"))
495                                                                 goto out;
496                                                 }
497                                                 state = STATE_MESSAGE_PLAYING;
498                                                 break;
499                                         /* XXX Message compose? It's easy!  Just read their # and, assuming it's in the config, 
500                                                call the routine as if it were called from the PBX proper XXX */
501                                         case '#':
502                                                 if (ast_streamfile(chan, "vm-goodbye"))
503                                                         goto out;
504                                                 if (ast_waitstream(chan, ""))
505                                                         goto out;
506                                                 res = 0;
507                                                 goto out;
508                                                 break;
509                                         default:
510                                                 /* Double yuck */
511                                                 d = '*';
512                                                 goto restart;
513                                         }
514                                 }
515                         }
516                 }
517         }
518         
519 out:
520         ast_stopstream(chan);
521         if (maxmsg) {
522                 /* Get the deleted messages fixed */
523                 curmsg = -1;
524                 for (x=0;x<maxmsg;x++) {
525                         if (!deleted[x]) {
526                                 curmsg++;
527                                 fn = get_fn(dir, x);
528                                 nfn = get_fn(dir, curmsg);
529                                 if (strcmp(fn, nfn))
530                                         ast_filerename(fn, nfn, NULL);
531                                 free(fn);
532                                 free(nfn);
533                         }
534                 }
535                 for (x = curmsg + 1; x<maxmsg; x++) {
536                         fn = get_fn(dir, x);
537                         if (fn) {
538                                 ast_filedelete(fn, NULL);
539                                 free(fn);
540                         }
541                 }
542         }
543         if (dir)
544                 free(dir);
545         if (cfg)
546                 ast_destroy(cfg);
547         LOCAL_USER_REMOVE(u);
548         return res;
549 }
550
551 static int vm_exec(struct ast_channel *chan, void *data)
552 {
553         int res=0, silent=0;
554         struct localuser *u;
555         char *ext = (char *)data;
556         
557         if (!data) {
558                 ast_log(LOG_WARNING, "vm requires an argument (extension)\n");
559                 return -1;
560         }
561         LOCAL_USER_ADD(u);
562         if (*ext == 's') {
563                 silent++;
564                 ext++;
565         }
566         res = leave_voicemail(chan, ext, silent);
567         LOCAL_USER_REMOVE(u);
568         return res;
569 }
570
571 int unload_module(void)
572 {
573         int res;
574         STANDARD_HANGUP_LOCALUSERS;
575         res = ast_unregister_application(app);
576         res |= ast_unregister_application(app2);
577         return res;
578 }
579
580 int load_module(void)
581 {
582         int res;
583         res = ast_register_application(app, vm_exec);
584         if (!res)
585                 res = ast_register_application(app2, vm_execmain);
586         return res;
587 }
588
589 char *description(void)
590 {
591         return tdesc;
592 }
593
594 int usecount(void)
595 {
596         int res;
597         STANDARD_USECOUNT(res);
598         return res;
599 }