Merge rgagnon's pedantic string checks (apps a-m, bug #2035)
[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 <stdlib.h>
42 #include <unistd.h>
43 #include <string.h>
44 #include <stdlib.h>
45 #include <dirent.h>
46
47 #include "../astconf.h"
48
49 static char *tdesc = "Indicator for whether a voice mailbox has messages in a given folder.";
50 static char *app_hasvoicemail = "HasVoicemail";
51 static char *hasvoicemail_synopsis = "Conditionally branches to priority + 101";
52 static char *hasvoicemail_descrip =
53 "HasVoicemail(vmbox[@context][:folder][|varname])\n"
54 "  Branches to priority + 101, if there is voicemail in folder indicated."
55 "  Optionally sets <varname> to the number of messages in that folder."
56 "  Assumes folder of INBOX if not specified.\n";
57
58 static char *app_hasnewvoicemail = "HasNewVoicemail";
59 static char *hasnewvoicemail_synopsis = "Conditionally branches to priority + 101";
60 static char *hasnewvoicemail_descrip =
61 "HasNewVoicemail(vmbox[@context][|varname])\n"
62 "  Branches to priority + 101, if there is voicemail in folder INBOX."
63 "  Optionally sets <varname> to the number of messages in that folder.\n";
64
65 STANDARD_LOCAL_USER;
66
67 LOCAL_USER_DECL;
68
69 static int hasvoicemail_exec(struct ast_channel *chan, void *data)
70 {
71         int res=0;
72         struct localuser *u;
73         char vmpath[256], *input, *varname = NULL, *vmbox, *vmfolder = "INBOX", *context = "default";
74         DIR *vmdir;
75         struct dirent *vment;
76         int vmcount = 0;
77
78         if (!data) {
79                 ast_log(LOG_WARNING, "HasVoicemail requires an argument (vm-box[@context][:folder]|varname)\n");
80                 return -1;
81         }
82         LOCAL_USER_ADD(u);
83
84         input = ast_strdupa((char *)data);
85         if (input) {
86                 if ((vmbox = strsep(&input,":")))
87                         if ((vmfolder = strsep(&input,"|")))
88                                 varname = input;
89                         else
90                                 vmfolder = input;
91                 else
92                         if ((vmbox = strsep(&input,"|")))
93                                 varname = input;
94                         else
95                                 vmbox = input;
96
97                 if (index(vmbox,'@')) {
98                         context = vmbox;
99                         vmbox = strsep(&context,"@");
100                 }
101
102                 snprintf(vmpath,sizeof(vmpath), "%s/voicemail/%s/%s/%s", (char *)ast_config_AST_SPOOL_DIR, context, vmbox, vmfolder);
103                 if (!(vmdir = opendir(vmpath))) {
104                         ast_log(LOG_NOTICE, "Voice mailbox %s at %s does not exist\n", vmbox, vmpath);
105                 } else {
106
107                         /* No matter what the format of VM, there will always be a .txt file for each message. */
108                         while ((vment = readdir(vmdir)))
109                                 if (!strncmp(vment->d_name + 7,".txt",4))
110                                         vmcount++;
111                         closedir(vmdir);
112                 }
113                 /* Set the count in the channel variable */
114                 if (varname) {
115                         char tmp[12];
116                         snprintf(tmp, sizeof(tmp), "%d", vmcount);
117                         pbx_builtin_setvar_helper(chan, varname, tmp);
118                 }
119
120                 if (vmcount > 0) {
121                         /* Branch to the next extension */
122                         if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->callerid)) {
123                                 chan->priority += 100;
124                         } else
125                                 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);
126                 }
127         } else {
128                 ast_log(LOG_ERROR, "Out of memory error\n");
129         }
130
131         LOCAL_USER_REMOVE(u);
132         return res;
133 }
134
135 int unload_module(void)
136 {
137         int res;
138         STANDARD_HANGUP_LOCALUSERS;
139         res = ast_unregister_application(app_hasvoicemail);
140         res |= ast_unregister_application(app_hasnewvoicemail);
141         return res;
142 }
143
144 int load_module(void)
145 {
146         int res;
147         res = ast_register_application(app_hasvoicemail, hasvoicemail_exec, hasvoicemail_synopsis, hasvoicemail_descrip);
148         res |= ast_register_application(app_hasnewvoicemail, hasvoicemail_exec, hasnewvoicemail_synopsis, hasnewvoicemail_descrip);
149         return res;
150 }
151
152 char *description(void)
153 {
154         return tdesc;
155 }
156
157 int usecount(void)
158 {
159         int res;
160         STANDARD_USECOUNT(res);
161         return res;
162 }
163
164 char *key()
165 {
166         return ASTERISK_GPL_KEY;
167 }