Add HasVoicemail application (bug 1520)
[asterisk/asterisk.git] / apps / app_hasnewvoicemail.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * HasVoicemail application
5  * Changes Copyright (c) 2004 Todd Freeman <freeman@andrews.edu>
6  * 
7  * 95% based on HasNewVoicemail by:
8  * 
9  * Copyright (c) 2003 Tilghman Lesher.  All rights reserved.
10  * 
11  * Tilghman Lesher <asterisk-hasnewvoicemail-app@the-tilghman.com>
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
25  * EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <sys/types.h>
35 #include <asterisk/file.h>
36 #include <asterisk/logger.h>
37 #include <asterisk/channel.h>
38 #include <asterisk/pbx.h>
39 #include <asterisk/module.h>
40 #include <stdlib.h>
41 #include <unistd.h>
42 #include <string.h>
43 #include <stdlib.h>
44 #include <dirent.h>
45
46 #include <pthread.h>
47
48 #include "../astconf.h"
49
50 static char *tdesc = "Indicator for whether a voice mailbox has messages in a given folder.";
51 static char *app_hasvoicemail = "HasVoicemail";
52 static char *hasvoicemail_synopsis = "Conditionally branches to priority + 101";
53 static char *hasvoicemail_descrip =
54 "HasVoicemail(vmbox[@context][:folder][|varname])\n"
55 "  Branches to priority + 101, if there is voicemail in folder indicated."
56 "  Optionally sets <varname> to the number of messages in that folder."
57 "  Assumes folder of INBOX if not specified.\n";
58
59 static char *newtdesc = "Indicator for whether there are messages in INBOX.";
60 static char *app_hasnewvoicemail = "HasNewVoicemail";
61 static char *hasnewvoicemail_synopsis = "Conditionally branches to priority + 101";
62 static char *hasnewvoicemail_descrip =
63 "HasNewVoicemail(vmbox[@context][|varname])\n"
64 "  Branches to priority + 101, if there is voicemail in folder INBOX."
65 "  Optionally sets <varname> to the number of messages in that folder.\n";
66
67 STANDARD_LOCAL_USER;
68
69 LOCAL_USER_DECL;
70
71 static int hasvoicemail_exec(struct ast_channel *chan, void *data)
72 {
73         int res=0;
74         struct localuser *u;
75         char vmpath[256], *input, *varname = NULL, *vmbox, *vmfolder = "INBOX", *context = "default";
76         DIR *vmdir;
77         struct dirent *vment;
78         int vmcount = 0;
79
80         if (!data) {
81                 ast_log(LOG_WARNING, "HasVoicemail requires an argument (vm-box[@context][:folder]|varname)\n");
82                 return -1;
83         }
84         LOCAL_USER_ADD(u);
85
86         input = ast_strdupa((char *)data);
87         if (input) {
88                 if ((vmbox = strsep(&input,":")))
89                         if ((vmfolder = strsep(&input,"|")))
90                                 varname = input;
91                         else
92                                 vmfolder = input;
93                 else
94                         if ((vmbox = strsep(&input,"|")))
95                                 varname = input;
96                         else
97                                 vmbox = input;
98
99                 if (index(vmbox,'@')) {
100                         context = vmbox;
101                         vmbox = strsep(&context,"@");
102                 }
103
104                 snprintf(vmpath,sizeof(vmpath), "%s/voicemail/%s/%s/%s", (char *)ast_config_AST_SPOOL_DIR, context, vmbox, vmfolder);
105                 if (!(vmdir = opendir(vmpath))) {
106                         ast_log(LOG_NOTICE, "Voice mailbox %s at %s does not exist\n", vmbox, vmpath);
107                 } else {
108
109                         /* No matter what the format of VM, there will always be a .txt file for each message. */
110                         while ((vment = readdir(vmdir)))
111                                 if (!strncmp(vment->d_name + 7,".txt",4))
112                                         vmcount++;
113                         closedir(vmdir);
114                 }
115                 /* Set the count in the channel variable */
116                 if (varname) {
117                         char tmp[12];
118                         snprintf(tmp, sizeof(tmp) - 1, "%d", vmcount);
119                         pbx_builtin_setvar_helper(chan, varname, tmp);
120                 }
121
122                 if (vmcount > 0) {
123                         /* Branch to the next extension */
124                         if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->callerid)) {
125                                 chan->priority += 100;
126                         } else
127                                 ast_log(LOG_WARNING, "VM box %s@%s has new voicemail, but extension %s, priority %d doesn't exist\n", vmbox, context, chan->exten, chan->priority + 101);
128                 }
129         } else {
130                 ast_log(LOG_ERROR, "Out of memory error\n");
131         }
132
133         LOCAL_USER_REMOVE(u);
134         return res;
135 }
136
137 int unload_module(void)
138 {
139         int res;
140         STANDARD_HANGUP_LOCALUSERS;
141         res = ast_unregister_application(app_hasvoicemail);
142         res |= ast_unregister_application(app_hasnewvoicemail);
143         return res;
144 }
145
146 int load_module(void)
147 {
148         int res;
149         res = ast_register_application(app_hasvoicemail, hasvoicemail_exec, hasvoicemail_synopsis, hasvoicemail_descrip);
150         res |= ast_register_application(app_hasnewvoicemail, hasvoicemail_exec, hasnewvoicemail_synopsis, hasnewvoicemail_descrip);
151         return res;
152 }
153
154 char *description(void)
155 {
156         return tdesc;
157 }
158
159 int usecount(void)
160 {
161         int res;
162         STANDARD_USECOUNT(res);
163         return res;
164 }
165
166 char *key()
167 {
168         return ASTERISK_GPL_KEY;
169 }