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