major Makefile and build process improvements, including removal of all hardcoded...
[asterisk/asterisk.git] / apps / app_hasnewvoicemail.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * HasVoicemail application
5  * Changes Copyright (c) 2004 - 2005 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 "asterisk/lock.h"
41 #include "asterisk/utils.h"
42 #include <stdlib.h>
43 #include <unistd.h>
44 #include <string.h>
45 #include <stdlib.h>
46 #include <dirent.h>
47
48 #include "asterisk.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 *app_hasnewvoicemail = "HasNewVoicemail";
60 static char *hasnewvoicemail_synopsis = "Conditionally branches to priority + 101";
61 static char *hasnewvoicemail_descrip =
62 "HasNewVoicemail(vmbox[/folder][@context][|varname])\n"
63 "  Branches to priority + 101, if there is voicemail in folder 'folder' or INBOX.\n"
64 "if folder is not specified. Optionally sets <varname> to the number of messages\n" 
65 "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], *temps, *input, *varname = NULL, *vmbox, *context = "default";
76         char *vmfolder;
77         DIR *vmdir;
78         struct dirent *vment;
79         int vmcount = 0;
80
81         if (!data) {
82                 ast_log(LOG_WARNING, "HasVoicemail requires an argument (vm-box[@context][:folder]|varname)\n");
83                 return -1;
84         }
85         LOCAL_USER_ADD(u);
86
87         input = ast_strdupa((char *)data);
88         if (input) {
89                 temps = input;
90                 if ((temps = strsep(&input, "|"))) {
91                         if (input && !ast_strlen_zero(input))
92                                 varname = input;
93                         input = temps;
94                 }
95                 if ((temps = strsep(&input, ":"))) {
96                         if (input && !ast_strlen_zero(input))
97                                 vmfolder = input;
98                         input = temps;
99                 }
100                 if ((vmbox = strsep(&input, "@")))
101                         if (input && !ast_strlen_zero(input))
102                                 context = input;
103                 if (!vmbox)
104                         vmbox = input;
105                 vmfolder = strchr(vmbox, '/');
106                 if (vmfolder) {
107                         *vmfolder = '\0';
108                         vmfolder++;
109                 } else
110                         vmfolder = "INBOX";
111                 snprintf(vmpath,sizeof(vmpath), "%s/voicemail/%s/%s/%s", (char *)ast_config_AST_SPOOL_DIR, context, vmbox, vmfolder);
112                 if (!(vmdir = opendir(vmpath))) {
113                         ast_log(LOG_NOTICE, "Voice mailbox %s at %s does not exist\n", vmbox, vmpath);
114                 } else {
115
116                         /* No matter what the format of VM, there will always be a .txt file for each message. */
117                         while ((vment = readdir(vmdir)))
118                                 if (!strncmp(vment->d_name + 7,".txt",4))
119                                         vmcount++;
120                         closedir(vmdir);
121                 }
122                 /* Set the count in the channel variable */
123                 if (varname) {
124                         char tmp[12];
125                         snprintf(tmp, sizeof(tmp), "%d", vmcount);
126                         pbx_builtin_setvar_helper(chan, varname, tmp);
127                 }
128
129                 if (vmcount > 0) {
130                         /* Branch to the next extension */
131                         if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->cid.cid_num)) {
132                                 chan->priority += 100;
133                         } else
134                                 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);
135                 }
136         } else {
137                 ast_log(LOG_ERROR, "Out of memory error\n");
138         }
139
140         LOCAL_USER_REMOVE(u);
141         return res;
142 }
143
144 int unload_module(void)
145 {
146         int res;
147         STANDARD_HANGUP_LOCALUSERS;
148         res = ast_unregister_application(app_hasvoicemail);
149         res |= ast_unregister_application(app_hasnewvoicemail);
150         return res;
151 }
152
153 int load_module(void)
154 {
155         int res;
156         res = ast_register_application(app_hasvoicemail, hasvoicemail_exec, hasvoicemail_synopsis, hasvoicemail_descrip);
157         res |= ast_register_application(app_hasnewvoicemail, hasvoicemail_exec, hasnewvoicemail_synopsis, hasnewvoicemail_descrip);
158         return res;
159 }
160
161 char *description(void)
162 {
163         return tdesc;
164 }
165
166 int usecount(void)
167 {
168         int res;
169         STANDARD_USECOUNT(res);
170         return res;
171 }
172
173 char *key()
174 {
175         return ASTERISK_GPL_KEY;
176 }