revert my pass through the tree to remove checks of the result of ast_strdupa
[asterisk/asterisk.git] / apps / app_hasnewvoicemail.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Changes Copyright (c) 2004 - 2005 Todd Freeman <freeman@andrews.edu>
5  * 
6  * 95% based on HasNewVoicemail by:
7  * 
8  * Copyright (c) 2003 Tilghman Lesher.  All rights reserved.
9  * 
10  * Tilghman Lesher <asterisk-hasnewvoicemail-app@the-tilghman.com>
11  *
12  * See http://www.asterisk.org for more information about
13  * the Asterisk project. Please do not directly contact
14  * any of the maintainers of this project for assistance;
15  * the project provides a web site, mailing lists and IRC
16  * channels for your use.
17  *
18  * This program is free software, distributed under the terms of
19  * the GNU General Public License Version 2. See the LICENSE file
20  * at the top of the source tree.
21  */
22
23 /*! \file
24  *
25  * \brief HasVoicemail application
26  *
27  * \author Todd Freeman <freeman@andrews.edu>
28  *
29  * \note 95% based on HasNewVoicemail by
30  * Tilghman Lesher <asterisk-hasnewvoicemail-app@the-tilghman.com>
31  *
32  * \ingroup applications
33  */
34
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <string.h>
38 #include <unistd.h>
39 #include <dirent.h>
40 #include <sys/types.h>
41
42 #include "asterisk.h"
43
44 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
45
46 #include "asterisk/file.h"
47 #include "asterisk/logger.h"
48 #include "asterisk/channel.h"
49 #include "asterisk/pbx.h"
50 #include "asterisk/module.h"
51 #include "asterisk/lock.h"
52 #include "asterisk/utils.h"
53 #include "asterisk/app.h"
54 #include "asterisk/options.h"
55
56 static char *tdesc = "Indicator for whether a voice mailbox has messages in a given folder.";
57 static char *app_hasvoicemail = "HasVoicemail";
58 static char *hasvoicemail_synopsis = "Conditionally branches to priority + 101 with the right options set";
59 static char *hasvoicemail_descrip =
60 "HasVoicemail(vmbox[/folder][@context][|varname[|options]])\n"
61 "  Optionally sets <varname> to the number of messages in that folder."
62 "  Assumes folder of INBOX if not specified.\n"
63 "  The option string may contain zero or the following character:\n"
64 "       'j' -- jump to priority n+101, if there is voicemail in the folder indicated.\n"
65 "  This application sets the following channel variable upon completion:\n"
66 "       HASVMSTATUS             The result of the voicemail check returned as a text string as follows\n"
67 "               <# of messages in the folder, 0 for NONE>\n";
68
69 static char *app_hasnewvoicemail = "HasNewVoicemail";
70 static char *hasnewvoicemail_synopsis = "Conditionally branches to priority + 101 with the right options set";
71 static char *hasnewvoicemail_descrip =
72 "HasNewVoicemail(vmbox[/folder][@context][|varname[|options]])\n"
73 "Assumes folder 'INBOX' if folder is not specified. Optionally sets <varname> to the number of messages\n" 
74 "in that folder.\n"
75 "  The option string may contain zero of the following character:\n"
76 "       'j' -- jump to priority n+101, if there is new voicemail in folder 'folder' or INBOX\n"
77 "  This application sets the following channel variable upon completion:\n"
78 "       HASVMSTATUS             The result of the new voicemail check returned as a text string as follows\n"
79 "               <# of messages in the folder, 0 for NONE>\n";
80
81 STANDARD_LOCAL_USER;
82
83 LOCAL_USER_DECL;
84
85 static int hasvoicemail_internal(char *context, char *box, char *folder)
86 {
87         char vmpath[256];
88         DIR *vmdir;
89         struct dirent *vment;
90         int count=0;
91
92         snprintf(vmpath,sizeof(vmpath), "%s/voicemail/%s/%s/%s", (char *)ast_config_AST_SPOOL_DIR, context, box, folder);
93         if ((vmdir = opendir(vmpath))) {
94                 /* No matter what the format of VM, there will always be a .txt file for each message. */
95                 while ((vment = readdir(vmdir))) {
96                         if (!strncmp(vment->d_name + 7, ".txt", 4)) {
97                                 count++;
98                         }
99                 }
100                 closedir(vmdir);
101         }
102         return count;
103 }
104
105 static int hasvoicemail_exec(struct ast_channel *chan, void *data)
106 {
107         struct localuser *u;
108         char *input, *varname = NULL, *vmbox, *context = "default";
109         char *vmfolder;
110         int vmcount = 0;
111         static int dep_warning = 0;
112         int priority_jump = 0;
113         char tmp[12];
114         AST_DECLARE_APP_ARGS(args,
115                 AST_APP_ARG(vmbox);
116                 AST_APP_ARG(varname);
117                 AST_APP_ARG(options);
118         );
119
120         if (!dep_warning) {
121                 ast_log(LOG_WARNING, "The applications HasVoicemail and HasNewVoicemail have been deprecated.  Please use the VMCOUNT() function instead.\n");
122                 dep_warning = 1;
123         }
124         
125         if (!data) {
126                 ast_log(LOG_WARNING, "HasVoicemail requires an argument (vm-box[/folder][@context][|varname[|options]])\n");
127                 return -1;
128         }
129
130         LOCAL_USER_ADD(u);
131
132         input = ast_strdupa((char *)data);
133         if (! input) {
134                 ast_log(LOG_ERROR, "Out of memory error\n");
135                 LOCAL_USER_REMOVE(u);
136                 return -1;
137         }
138
139         AST_STANDARD_APP_ARGS(args, input);
140
141         if ((vmbox = strsep(&args.vmbox, "@")))
142                 if (!ast_strlen_zero(args.vmbox))
143                         context = args.vmbox;
144         if (!vmbox)
145                 vmbox = args.vmbox;
146
147         vmfolder = strchr(vmbox, '/');
148         if (vmfolder) {
149                 *vmfolder = '\0';
150                 vmfolder++;
151         } else {
152                 vmfolder = "INBOX";
153         }
154
155         if (args.options) {
156                 if (strchr(args.options, 'j'))
157                         priority_jump = 1;
158         }
159
160         vmcount = hasvoicemail_internal(context, vmbox, vmfolder);
161         /* Set the count in the channel variable */
162         if (varname) {
163                 snprintf(tmp, sizeof(tmp), "%d", vmcount);
164                 pbx_builtin_setvar_helper(chan, varname, tmp);
165         }
166
167         if (vmcount > 0) {
168                 /* Branch to the next extension */
169                 if (priority_jump || ast_opt_priority_jumping) {
170                         if (ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101)) 
171                                 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);
172                 }
173         }
174
175         snprintf(tmp, sizeof(tmp), "%d", vmcount);
176         pbx_builtin_setvar_helper(chan, "HASVMSTATUS", tmp);
177         
178         LOCAL_USER_REMOVE(u);
179
180         return 0;
181 }
182
183 static char *acf_vmcount_exec(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
184 {
185         struct localuser *u;
186         char *argsstr, *context;
187         AST_DECLARE_APP_ARGS(args,
188                 AST_APP_ARG(vmbox);
189                 AST_APP_ARG(folder);
190         );
191
192         LOCAL_USER_ACF_ADD(u);
193
194         buf[0] = '\0';
195
196         argsstr = ast_strdupa(data);
197         if (!argsstr) {
198                 ast_log(LOG_ERROR, "Out of memory");
199                 LOCAL_USER_REMOVE(u);
200                 return buf;
201         }
202
203         AST_STANDARD_APP_ARGS(args, argsstr);
204
205         if (strchr(args.vmbox, '@')) {
206                 context = args.vmbox;
207                 args.vmbox = strsep(&context, "@");
208         } else {
209                 context = "default";
210         }
211
212         if (ast_strlen_zero(args.folder)) {
213                 args.folder = "INBOX";
214         }
215
216         snprintf(buf, len, "%d", hasvoicemail_internal(context, args.vmbox, args.folder));
217
218         LOCAL_USER_REMOVE(u);
219         
220         return buf;
221 }
222
223 struct ast_custom_function acf_vmcount = {
224         .name = "VMCOUNT",
225         .synopsis = "Counts the voicemail in a specified mailbox",
226         .syntax = "VMCOUNT(vmbox[@context][|folder])",
227         .desc =
228         "  context - defaults to \"default\"\n"
229         "  folder  - defaults to \"INBOX\"\n",
230         .read = acf_vmcount_exec,
231 };
232
233 int unload_module(void)
234 {
235         int res;
236         
237         res = ast_custom_function_unregister(&acf_vmcount);
238         res |= ast_unregister_application(app_hasvoicemail);
239         res |= ast_unregister_application(app_hasnewvoicemail);
240         
241         STANDARD_HANGUP_LOCALUSERS;
242
243         return res;
244 }
245
246 int load_module(void)
247 {
248         int res;
249
250         res = ast_custom_function_register(&acf_vmcount);
251         res |= ast_register_application(app_hasvoicemail, hasvoicemail_exec, hasvoicemail_synopsis, hasvoicemail_descrip);
252         res |= ast_register_application(app_hasnewvoicemail, hasvoicemail_exec, hasnewvoicemail_synopsis, hasnewvoicemail_descrip);
253
254         return res;
255 }
256
257 char *description(void)
258 {
259         return tdesc;
260 }
261
262 int usecount(void)
263 {
264         int res;
265         STANDARD_USECOUNT(res);
266         return res;
267 }
268
269 char *key()
270 {
271         return ASTERISK_GPL_KEY;
272 }