33204bbe9ce7cbd34ecd949eb333d9a625c55af5
[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 "asterisk.h"
36
37 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
38
39 #include <stdlib.h>
40 #include <stdio.h>
41 #include <string.h>
42 #include <unistd.h>
43 #include <dirent.h>
44 #include <sys/types.h>
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 *app_hasvoicemail = "HasVoicemail";
57 static char *hasvoicemail_synopsis = "Conditionally branches to priority + 101 with the right options set";
58 static char *hasvoicemail_descrip =
59 "HasVoicemail(vmbox[/folder][@context][|varname[|options]])\n"
60 "  Optionally sets <varname> to the number of messages in that folder."
61 "  Assumes folder of INBOX if not specified.\n"
62 "  The option string may contain zero or the following character:\n"
63 "       'j' -- jump to priority n+101, if there is voicemail in the folder indicated.\n"
64 "  This application sets the following channel variable upon completion:\n"
65 "       HASVMSTATUS             The result of the voicemail check returned as a text string as follows\n"
66 "               <# of messages in the folder, 0 for NONE>\n";
67
68 static char *app_hasnewvoicemail = "HasNewVoicemail";
69 static char *hasnewvoicemail_synopsis = "Conditionally branches to priority + 101 with the right options set";
70 static char *hasnewvoicemail_descrip =
71 "HasNewVoicemail(vmbox[/folder][@context][|varname[|options]])\n"
72 "Assumes folder 'INBOX' if folder is not specified. Optionally sets <varname> to the number of messages\n" 
73 "in that folder.\n"
74 "  The option string may contain zero of the following character:\n"
75 "       'j' -- jump to priority n+101, if there is new voicemail in folder 'folder' or INBOX\n"
76 "  This application sets the following channel variable upon completion:\n"
77 "       HASVMSTATUS             The result of the new voicemail check returned as a text string as follows\n"
78 "               <# of messages in the folder, 0 for NONE>\n";
79
80
81 static int hasvoicemail_exec(struct ast_channel *chan, void *data)
82 {
83         struct ast_module_user *u;
84         char *input, *varname = NULL, *vmbox, *context = "default";
85         char *vmfolder;
86         int vmcount = 0;
87         static int dep_warning = 0;
88         int priority_jump = 0;
89         char tmp[12];
90         AST_DECLARE_APP_ARGS(args,
91                 AST_APP_ARG(vmbox);
92                 AST_APP_ARG(varname);
93                 AST_APP_ARG(options);
94         );
95
96         if (!dep_warning) {
97                 ast_log(LOG_WARNING, "The applications HasVoicemail and HasNewVoicemail have been deprecated.  Please use the VMCOUNT() function instead.\n");
98                 dep_warning = 1;
99         }
100         
101         if (!data) {
102                 ast_log(LOG_WARNING, "HasVoicemail requires an argument (vm-box[/folder][@context][|varname[|options]])\n");
103                 return -1;
104         }
105
106         u = ast_module_user_add(chan);
107
108         input = ast_strdupa(data);
109
110         AST_STANDARD_APP_ARGS(args, input);
111
112         vmbox = strsep(&args.vmbox, "@");
113
114         if (!ast_strlen_zero(args.vmbox))
115                 context = args.vmbox;
116
117         vmfolder = strchr(vmbox, '/');
118         if (vmfolder) {
119                 *vmfolder = '\0';
120                 vmfolder++;
121         } else {
122                 vmfolder = "INBOX";
123         }
124
125         if (args.options) {
126                 if (strchr(args.options, 'j'))
127                         priority_jump = 1;
128         }
129
130         vmcount = ast_app_messagecount(context, vmbox, vmfolder);
131         /* Set the count in the channel variable */
132         if (varname) {
133                 snprintf(tmp, sizeof(tmp), "%d", vmcount);
134                 pbx_builtin_setvar_helper(chan, varname, tmp);
135         }
136
137         if (vmcount > 0) {
138                 /* Branch to the next extension */
139                 if (priority_jump || ast_opt_priority_jumping) {
140                         if (ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101)) 
141                                 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);
142                 }
143         }
144
145         snprintf(tmp, sizeof(tmp), "%d", vmcount);
146         pbx_builtin_setvar_helper(chan, "HASVMSTATUS", tmp);
147         
148         ast_module_user_remove(u);
149
150         return 0;
151 }
152
153 static int acf_vmcount_exec(struct ast_channel *chan, char *cmd, char *argsstr, char *buf, size_t len)
154 {
155         struct ast_module_user *u;
156         char *context;
157         AST_DECLARE_APP_ARGS(args,
158                 AST_APP_ARG(vmbox);
159                 AST_APP_ARG(folder);
160         );
161
162         u = ast_module_user_add(chan);
163
164         buf[0] = '\0';
165
166         AST_STANDARD_APP_ARGS(args, argsstr);
167
168         if (strchr(args.vmbox, '@')) {
169                 context = args.vmbox;
170                 args.vmbox = strsep(&context, "@");
171         } else {
172                 context = "default";
173         }
174
175         if (ast_strlen_zero(args.folder)) {
176                 args.folder = "INBOX";
177         }
178
179         snprintf(buf, len, "%d", ast_app_messagecount(context, args.vmbox, args.folder));
180
181         ast_module_user_remove(u);
182         
183         return 0;
184 }
185
186 struct ast_custom_function acf_vmcount = {
187         .name = "VMCOUNT",
188         .synopsis = "Counts the voicemail in a specified mailbox",
189         .syntax = "VMCOUNT(vmbox[@context][|folder])",
190         .desc =
191         "  context - defaults to \"default\"\n"
192         "  folder  - defaults to \"INBOX\"\n",
193         .read = acf_vmcount_exec,
194 };
195
196 static int unload_module(void)
197 {
198         int res;
199         
200         res = ast_custom_function_unregister(&acf_vmcount);
201         res |= ast_unregister_application(app_hasvoicemail);
202         res |= ast_unregister_application(app_hasnewvoicemail);
203         
204         ast_module_user_hangup_all();
205
206         return res;
207 }
208
209 static int load_module(void)
210 {
211         int res;
212
213         res = ast_custom_function_register(&acf_vmcount);
214         res |= ast_register_application(app_hasvoicemail, hasvoicemail_exec, hasvoicemail_synopsis, hasvoicemail_descrip);
215         res |= ast_register_application(app_hasnewvoicemail, hasvoicemail_exec, hasnewvoicemail_synopsis, hasnewvoicemail_descrip);
216
217         return res;
218 }
219
220 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Indicator for whether a voice mailbox has messages in a given folder.");