Fix HasVoicemail so it can parse all arguments correctly (bug 2559)
[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 <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 "../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 *app_hasnewvoicemail = "HasNewVoicemail";
60 static char *hasnewvoicemail_synopsis = "Conditionally branches to priority + 101";
61 static char *hasnewvoicemail_descrip =
62 "HasNewVoicemail(vmbox[@context][|varname])\n"
63 "  Branches to priority + 101, if there is voicemail in folder INBOX."
64 "  Optionally sets <varname> to the number of messages in that folder.\n";
65
66 STANDARD_LOCAL_USER;
67
68 LOCAL_USER_DECL;
69
70 static int hasvoicemail_exec(struct ast_channel *chan, void *data)
71 {
72         int res=0;
73         struct localuser *u;
74         char vmpath[256], *temps, *input, *varname = NULL, *vmbox, *vmfolder = "INBOX", *context = "default";
75         DIR *vmdir;
76         struct dirent *vment;
77         int vmcount = 0;
78
79         if (!data) {
80                 ast_log(LOG_WARNING, "HasVoicemail requires an argument (vm-box[@context][:folder]|varname)\n");
81                 return -1;
82         }
83         LOCAL_USER_ADD(u);
84
85         input = ast_strdupa((char *)data);
86         if (input) {
87                 temps = input;
88                 if ((temps = strsep(&input, "|"))) {
89                         if (input && !ast_strlen_zero(input))
90                                 varname = input;
91                         input = temps;
92                 }
93                 if ((temps = strsep(&input, ":"))) {
94                         if (input && !ast_strlen_zero(input))
95                                 vmfolder = input;
96                         input = temps;
97                 }
98                 if ((vmbox = strsep(&input, "@")))
99                         if (input && !ast_strlen_zero(input))
100                                 context = input;
101                 if (!vmbox)
102                         vmbox = input;
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), "%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->cid.cid_num)) {
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 }