convert a bunch of apps to use ast_goto_if_exists() (issue #5138)
[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 <stdlib.h>
36 #include <unistd.h>
37 #include <string.h>
38 #include <dirent.h>
39
40 #include "asterisk.h"
41
42 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
43
44 #include "asterisk/file.h"
45 #include "asterisk/logger.h"
46 #include "asterisk/channel.h"
47 #include "asterisk/pbx.h"
48 #include "asterisk/module.h"
49 #include "asterisk/lock.h"
50 #include "asterisk/utils.h"
51
52 static char *tdesc = "Indicator for whether a voice mailbox has messages in a given folder.";
53 static char *app_hasvoicemail = "HasVoicemail";
54 static char *hasvoicemail_synopsis = "Conditionally branches to priority + 101";
55 static char *hasvoicemail_descrip =
56 "HasVoicemail(vmbox[@context][:folder][|varname])\n"
57 "  Branches to priority + 101, if there is voicemail in folder indicated."
58 "  Optionally sets <varname> to the number of messages in that folder."
59 "  Assumes folder of INBOX if not specified.\n";
60
61 static char *app_hasnewvoicemail = "HasNewVoicemail";
62 static char *hasnewvoicemail_synopsis = "Conditionally branches to priority + 101";
63 static char *hasnewvoicemail_descrip =
64 "HasNewVoicemail(vmbox[/folder][@context][|varname])\n"
65 "  Branches to priority + 101, if there is voicemail in folder 'folder' or INBOX.\n"
66 "if folder is not specified. Optionally sets <varname> to the number of messages\n" 
67 "in that folder.\n";
68
69 STANDARD_LOCAL_USER;
70
71 LOCAL_USER_DECL;
72
73 static int hasvoicemail_exec(struct ast_channel *chan, void *data)
74 {
75         int res=0;
76         struct localuser *u;
77         char vmpath[256], *temps, *input, *varname = NULL, *vmbox, *context = "default";
78         char *vmfolder;
79         DIR *vmdir;
80         struct dirent *vment;
81         int vmcount = 0;
82
83         if (!data) {
84                 ast_log(LOG_WARNING, "HasVoicemail requires an argument (vm-box[@context][:folder]|varname)\n");
85                 return -1;
86         }
87         LOCAL_USER_ADD(u);
88
89         input = ast_strdupa((char *)data);
90         if (input) {
91                 temps = input;
92                 if ((temps = strsep(&input, "|"))) {
93                         if (input && !ast_strlen_zero(input))
94                                 varname = input;
95                         input = temps;
96                 }
97                 if ((temps = strsep(&input, ":"))) {
98                         if (input && !ast_strlen_zero(input))
99                                 vmfolder = input;
100                         input = temps;
101                 }
102                 if ((vmbox = strsep(&input, "@")))
103                         if (input && !ast_strlen_zero(input))
104                                 context = input;
105                 if (!vmbox)
106                         vmbox = input;
107                 vmfolder = strchr(vmbox, '/');
108                 if (vmfolder) {
109                         *vmfolder = '\0';
110                         vmfolder++;
111                 } else
112                         vmfolder = "INBOX";
113                 snprintf(vmpath,sizeof(vmpath), "%s/voicemail/%s/%s/%s", (char *)ast_config_AST_SPOOL_DIR, context, vmbox, vmfolder);
114                 if (!(vmdir = opendir(vmpath))) {
115                         ast_log(LOG_NOTICE, "Voice mailbox %s at %s does not exist\n", vmbox, vmpath);
116                 } else {
117
118                         /* No matter what the format of VM, there will always be a .txt file for each message. */
119                         while ((vment = readdir(vmdir)))
120                                 if (!strncmp(vment->d_name + 7,".txt",4))
121                                         vmcount++;
122                         closedir(vmdir);
123                 }
124                 /* Set the count in the channel variable */
125                 if (varname) {
126                         char tmp[12];
127                         snprintf(tmp, sizeof(tmp), "%d", vmcount);
128                         pbx_builtin_setvar_helper(chan, varname, tmp);
129                 }
130
131                 if (vmcount > 0) {
132                         /* Branch to the next extension */
133                         if (!ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101)) 
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 }