major dialplan functions update
[asterisk/asterisk.git] / apps / app_hasnewvoicemail.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Changes Copyright (c) 2004 - 2006 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         if (!(input = ast_strdupa(data))) {
133                 LOCAL_USER_REMOVE(u);
134                 return -1;
135         }
136
137         AST_STANDARD_APP_ARGS(args, input);
138
139         if ((vmbox = strsep(&args.vmbox, "@")))
140                 if (!ast_strlen_zero(args.vmbox))
141                         context = args.vmbox;
142         if (!vmbox)
143                 vmbox = args.vmbox;
144
145         vmfolder = strchr(vmbox, '/');
146         if (vmfolder) {
147                 *vmfolder = '\0';
148                 vmfolder++;
149         } else {
150                 vmfolder = "INBOX";
151         }
152
153         if (args.options) {
154                 if (strchr(args.options, 'j'))
155                         priority_jump = 1;
156         }
157
158         vmcount = hasvoicemail_internal(context, vmbox, vmfolder);
159         /* Set the count in the channel variable */
160         if (varname) {
161                 snprintf(tmp, sizeof(tmp), "%d", vmcount);
162                 pbx_builtin_setvar_helper(chan, varname, tmp);
163         }
164
165         if (vmcount > 0) {
166                 /* Branch to the next extension */
167                 if (priority_jump || ast_opt_priority_jumping) {
168                         if (ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101)) 
169                                 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);
170                 }
171         }
172
173         snprintf(tmp, sizeof(tmp), "%d", vmcount);
174         pbx_builtin_setvar_helper(chan, "HASVMSTATUS", tmp);
175         
176         LOCAL_USER_REMOVE(u);
177
178         return 0;
179 }
180
181 int acf_vmcount_exec(struct ast_channel *chan, char *cmd, char *argsstr, char *buf, size_t len)
182 {
183         struct localuser *u;
184         char *context;
185         AST_DECLARE_APP_ARGS(args,
186                 AST_APP_ARG(vmbox);
187                 AST_APP_ARG(folder);
188         );
189
190         LOCAL_USER_ACF_ADD(u);
191
192         buf[0] = '\0';
193
194         AST_STANDARD_APP_ARGS(args, argsstr);
195
196         if (strchr(args.vmbox, '@')) {
197                 context = args.vmbox;
198                 args.vmbox = strsep(&context, "@");
199         } else {
200                 context = "default";
201         }
202
203         if (ast_strlen_zero(args.folder)) {
204                 args.folder = "INBOX";
205         }
206
207         snprintf(buf, len, "%d", hasvoicemail_internal(context, args.vmbox, args.folder));
208
209         LOCAL_USER_REMOVE(u);
210         
211         return 0;
212 }
213
214 struct ast_custom_function acf_vmcount = {
215         .name = "VMCOUNT",
216         .synopsis = "Counts the voicemail in a specified mailbox",
217         .syntax = "VMCOUNT(vmbox[@context][|folder])",
218         .desc =
219         "  context - defaults to \"default\"\n"
220         "  folder  - defaults to \"INBOX\"\n",
221         .read = acf_vmcount_exec,
222 };
223
224 int unload_module(void)
225 {
226         int res;
227         
228         res = ast_custom_function_unregister(&acf_vmcount);
229         res |= ast_unregister_application(app_hasvoicemail);
230         res |= ast_unregister_application(app_hasnewvoicemail);
231         
232         STANDARD_HANGUP_LOCALUSERS;
233
234         return res;
235 }
236
237 int load_module(void)
238 {
239         int res;
240
241         res = ast_custom_function_register(&acf_vmcount);
242         res |= ast_register_application(app_hasvoicemail, hasvoicemail_exec, hasvoicemail_synopsis, hasvoicemail_descrip);
243         res |= ast_register_application(app_hasnewvoicemail, hasvoicemail_exec, hasnewvoicemail_synopsis, hasnewvoicemail_descrip);
244
245         return res;
246 }
247
248 char *description(void)
249 {
250         return tdesc;
251 }
252
253 int usecount(void)
254 {
255         int res;
256         STANDARD_USECOUNT(res);
257         return res;
258 }
259
260 char *key()
261 {
262         return ASTERISK_GPL_KEY;
263 }