Fix a memory leak in func_curl. Every thread that used this function leaked
[asterisk-bristuff.git] / apps / app_hasnewvoicemail.c
blob8f3d33504b72cb9513b2793a818e90749add9631
1 /*
2 * Asterisk -- An open source telephony toolkit.
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>
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.
23 /*! \file
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
35 #include "asterisk.h"
37 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
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>
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"
67 "\nThis application has been deprecated in favor of the VMCOUNT() function\n";
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 "\nThis application has been deprecated in favor of the VMCOUNT() function\n";
83 static int hasvoicemail_exec(struct ast_channel *chan, void *data)
85 struct ast_module_user *u;
86 char *input, *varname = NULL, *vmbox, *context = "default";
87 char *vmfolder;
88 int vmcount = 0;
89 static int dep_warning = 0;
90 int priority_jump = 0;
91 char tmp[12];
92 AST_DECLARE_APP_ARGS(args,
93 AST_APP_ARG(vmbox);
94 AST_APP_ARG(varname);
95 AST_APP_ARG(options);
98 if (!dep_warning) {
99 ast_log(LOG_WARNING, "The applications HasVoicemail and HasNewVoicemail have been deprecated. Please use the VMCOUNT() function instead.\n");
100 dep_warning = 1;
103 if (!data) {
104 ast_log(LOG_WARNING, "HasVoicemail requires an argument (vm-box[/folder][@context][|varname[|options]])\n");
105 return -1;
108 u = ast_module_user_add(chan);
110 input = ast_strdupa(data);
112 AST_STANDARD_APP_ARGS(args, input);
114 vmbox = strsep(&args.vmbox, "@");
116 if (!ast_strlen_zero(args.vmbox))
117 context = args.vmbox;
119 vmfolder = strchr(vmbox, '/');
120 if (vmfolder) {
121 *vmfolder = '\0';
122 vmfolder++;
123 } else {
124 vmfolder = "INBOX";
127 if (args.options) {
128 if (strchr(args.options, 'j'))
129 priority_jump = 1;
132 vmcount = ast_app_messagecount(context, vmbox, vmfolder);
133 /* Set the count in the channel variable */
134 if (varname) {
135 snprintf(tmp, sizeof(tmp), "%d", vmcount);
136 pbx_builtin_setvar_helper(chan, varname, tmp);
139 if (vmcount > 0) {
140 /* Branch to the next extension */
141 if (priority_jump || ast_opt_priority_jumping) {
142 if (ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101))
143 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);
147 snprintf(tmp, sizeof(tmp), "%d", vmcount);
148 pbx_builtin_setvar_helper(chan, "HASVMSTATUS", tmp);
150 ast_module_user_remove(u);
152 return 0;
155 static int acf_vmcount_exec(struct ast_channel *chan, char *cmd, char *argsstr, char *buf, size_t len)
157 struct ast_module_user *u;
158 char *context;
159 AST_DECLARE_APP_ARGS(args,
160 AST_APP_ARG(vmbox);
161 AST_APP_ARG(folder);
164 if (ast_strlen_zero(argsstr))
165 return -1;
167 u = ast_module_user_add(chan);
169 buf[0] = '\0';
171 AST_STANDARD_APP_ARGS(args, argsstr);
173 if (strchr(args.vmbox, '@')) {
174 context = args.vmbox;
175 args.vmbox = strsep(&context, "@");
176 } else {
177 context = "default";
180 if (ast_strlen_zero(args.folder)) {
181 args.folder = "INBOX";
184 snprintf(buf, len, "%d", ast_app_messagecount(context, args.vmbox, args.folder));
186 ast_module_user_remove(u);
188 return 0;
191 struct ast_custom_function acf_vmcount = {
192 .name = "VMCOUNT",
193 .synopsis = "Counts the voicemail in a specified mailbox",
194 .syntax = "VMCOUNT(vmbox[@context][|folder])",
195 .desc =
196 " context - defaults to \"default\"\n"
197 " folder - defaults to \"INBOX\"\n",
198 .read = acf_vmcount_exec,
201 static int unload_module(void)
203 int res;
205 res = ast_custom_function_unregister(&acf_vmcount);
206 res |= ast_unregister_application(app_hasvoicemail);
207 res |= ast_unregister_application(app_hasnewvoicemail);
209 ast_module_user_hangup_all();
211 return res;
214 static int load_module(void)
216 int res;
218 res = ast_custom_function_register(&acf_vmcount);
219 res |= ast_register_application(app_hasvoicemail, hasvoicemail_exec, hasvoicemail_synopsis, hasvoicemail_descrip);
220 res |= ast_register_application(app_hasnewvoicemail, hasvoicemail_exec, hasnewvoicemail_synopsis, hasnewvoicemail_descrip);
222 return res;
225 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Indicator for whether a voice mailbox has messages in a given folder.");