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