f4696fcbb7a7612ab26ab3c00cb7c1e9ce639615
[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
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 || ast_opt_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 *argsstr, *context;
182         AST_DECLARE_APP_ARGS(args,
183                 AST_APP_ARG(vmbox);
184                 AST_APP_ARG(folder);
185         );
186
187         LOCAL_USER_ACF_ADD(u);
188
189         buf[0] = '\0';
190
191         argsstr = ast_strdupa(data);
192
193         AST_STANDARD_APP_ARGS(args, argsstr);
194
195         if (strchr(args.vmbox, '@')) {
196                 context = args.vmbox;
197                 args.vmbox = strsep(&context, "@");
198         } else {
199                 context = "default";
200         }
201
202         if (ast_strlen_zero(args.folder)) {
203                 args.folder = "INBOX";
204         }
205
206         snprintf(buf, len, "%d", hasvoicemail_internal(context, args.vmbox, args.folder));
207
208         LOCAL_USER_REMOVE(u);
209         
210         return buf;
211 }
212
213 struct ast_custom_function acf_vmcount = {
214         .name = "VMCOUNT",
215         .synopsis = "Counts the voicemail in a specified mailbox",
216         .syntax = "VMCOUNT(vmbox[@context][|folder])",
217         .desc =
218         "  context - defaults to \"default\"\n"
219         "  folder  - defaults to \"INBOX\"\n",
220         .read = acf_vmcount_exec,
221 };
222
223 int unload_module(void)
224 {
225         int res;
226         
227         res = ast_custom_function_unregister(&acf_vmcount);
228         res |= ast_unregister_application(app_hasvoicemail);
229         res |= ast_unregister_application(app_hasnewvoicemail);
230         
231         STANDARD_HANGUP_LOCALUSERS;
232
233         return res;
234 }
235
236 int load_module(void)
237 {
238         int res;
239
240         res = ast_custom_function_register(&acf_vmcount);
241         res |= ast_register_application(app_hasvoicemail, hasvoicemail_exec, hasvoicemail_synopsis, hasvoicemail_descrip);
242         res |= ast_register_application(app_hasnewvoicemail, hasvoicemail_exec, hasnewvoicemail_synopsis, hasnewvoicemail_descrip);
243
244         return res;
245 }
246
247 char *description(void)
248 {
249         return tdesc;
250 }
251
252 int usecount(void)
253 {
254         int res;
255         STANDARD_USECOUNT(res);
256         return res;
257 }
258
259 char *key()
260 {
261         return ASTERISK_GPL_KEY;
262 }