2 * Asterisk -- An open source telephony toolkit.
4 * Changes Copyright (c) 2004 - 2006 Todd Freeman <freeman@andrews.edu>
6 * 95% based on HasNewVoicemail by:
8 * Copyright (c) 2003 Tilghman Lesher. All rights reserved.
10 * Tilghman Lesher <asterisk-hasnewvoicemail-app@the-tilghman.com>
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.
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.
25 * \brief HasVoicemail application
27 * \author Todd Freeman <freeman@andrews.edu>
29 * \note 95% based on HasNewVoicemail by
30 * Tilghman Lesher <asterisk-hasnewvoicemail-app@the-tilghman.com>
32 * \ingroup applications
37 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
44 #include <sys/types.h>
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"
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";
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"
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";
81 static int hasvoicemail_exec(struct ast_channel *chan, void *data)
83 struct ast_module_user *u;
84 char *input, *varname = NULL, *vmbox, *context = "default";
87 static int dep_warning = 0;
88 int priority_jump = 0;
90 AST_DECLARE_APP_ARGS(args,
97 ast_log(LOG_WARNING, "The applications HasVoicemail and HasNewVoicemail have been deprecated. Please use the VMCOUNT() function instead.\n");
102 ast_log(LOG_WARNING, "HasVoicemail requires an argument (vm-box[/folder][@context][|varname[|options]])\n");
106 u = ast_module_user_add(chan);
108 input = ast_strdupa(data);
110 AST_STANDARD_APP_ARGS(args, input);
112 vmbox = strsep(&args.vmbox, "@");
114 if (!ast_strlen_zero(args.vmbox))
115 context = args.vmbox;
117 vmfolder = strchr(vmbox, '/');
126 if (strchr(args.options, 'j'))
130 vmcount = ast_app_messagecount(context, vmbox, vmfolder);
131 /* Set the count in the channel variable */
133 snprintf(tmp, sizeof(tmp), "%d", vmcount);
134 pbx_builtin_setvar_helper(chan, varname, tmp);
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);
145 snprintf(tmp, sizeof(tmp), "%d", vmcount);
146 pbx_builtin_setvar_helper(chan, "HASVMSTATUS", tmp);
148 ast_module_user_remove(u);
153 static int acf_vmcount_exec(struct ast_channel *chan, char *cmd, char *argsstr, char *buf, size_t len)
155 struct ast_module_user *u;
157 AST_DECLARE_APP_ARGS(args,
162 u = ast_module_user_add(chan);
166 AST_STANDARD_APP_ARGS(args, argsstr);
168 if (strchr(args.vmbox, '@')) {
169 context = args.vmbox;
170 args.vmbox = strsep(&context, "@");
175 if (ast_strlen_zero(args.folder)) {
176 args.folder = "INBOX";
179 snprintf(buf, len, "%d", ast_app_messagecount(context, args.vmbox, args.folder));
181 ast_module_user_remove(u);
186 struct ast_custom_function acf_vmcount = {
188 .synopsis = "Counts the voicemail in a specified mailbox",
189 .syntax = "VMCOUNT(vmbox[@context][|folder])",
191 " context - defaults to \"default\"\n"
192 " folder - defaults to \"INBOX\"\n",
193 .read = acf_vmcount_exec,
196 static int unload_module(void)
200 res = ast_custom_function_unregister(&acf_vmcount);
201 res |= ast_unregister_application(app_hasvoicemail);
202 res |= ast_unregister_application(app_hasnewvoicemail);
204 ast_module_user_hangup_all();
209 static int load_module(void)
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);
220 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Indicator for whether a voice mailbox has messages in a given folder.");