Merged revisions 378322 via svnmerge from
[asterisk/asterisk.git] / funcs / func_vmcount.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (c) 2006 Tilghman Lesher.  All rights reserved.
5  * 
6  * Tilghman Lesher <asterisk-vmcount-func@the-tilghman.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief VMCOUNT dialplan function
22  *
23  * \author Tilghman Lesher <asterisk-vmcount-func@the-tilghman.com>
24  *
25  * \ingroup functions
26  */
27
28 /*** MODULEINFO
29         <support_level>core</support_level>
30  ***/
31
32 #include "asterisk.h"
33
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
35
36 #include <dirent.h>
37
38 #include "asterisk/file.h"
39 #include "asterisk/channel.h"
40 #include "asterisk/pbx.h"
41 #include "asterisk/module.h"
42 #include "asterisk/lock.h"
43 #include "asterisk/utils.h"
44 #include "asterisk/app.h"
45
46 /*** DOCUMENTATION
47         <function name="VMCOUNT" language="en_US">
48                 <synopsis>
49                         Count the voicemails in a specified mailbox.
50                 </synopsis>
51                 <syntax>
52                         <parameter name="vmbox" required="true" argsep="@">
53                                 <argument name="vmbox" required="true" />
54                                 <argument name="context" required="false">
55                                         <para>If not specified, defaults to <literal>default</literal>.</para>
56                                 </argument>
57                         </parameter>
58                         <parameter name="folder" required="false">
59                                 <para>If not specified, defaults to <literal>INBOX</literal></para>
60                         </parameter>
61                 </syntax>
62                 <description>
63                         <para>Count the number of voicemails in a specified mailbox, you could also specify 
64                         the <replaceable>context</replaceable> and the mailbox <replaceable>folder</replaceable>.</para>
65                         <para>Example: <literal>exten => s,1,Set(foo=${VMCOUNT(125)})</literal></para>
66                 </description>
67         </function>
68  ***/
69
70 static int acf_vmcount_exec(struct ast_channel *chan, const char *cmd, char *argsstr, char *buf, size_t len)
71 {
72         char *context;
73         AST_DECLARE_APP_ARGS(args,
74                 AST_APP_ARG(vmbox);
75                 AST_APP_ARG(folder);
76         );
77
78         buf[0] = '\0';
79
80         if (ast_strlen_zero(argsstr))
81                 return -1;
82
83         AST_STANDARD_APP_ARGS(args, argsstr);
84
85         if (strchr(args.vmbox, '@')) {
86                 context = args.vmbox;
87                 args.vmbox = strsep(&context, "@");
88         } else {
89                 context = "default";
90         }
91
92         if (ast_strlen_zero(args.folder)) {
93                 args.folder = "INBOX";
94         }
95
96         snprintf(buf, len, "%d", ast_app_messagecount(context, args.vmbox, args.folder));
97         
98         return 0;
99 }
100
101 static struct ast_custom_function acf_vmcount = {
102         .name = "VMCOUNT",
103         .read = acf_vmcount_exec,
104         .read_max = 12,
105 };
106
107 static int unload_module(void)
108 {
109         return ast_custom_function_unregister(&acf_vmcount);
110 }
111
112 static int load_module(void)
113 {
114         return ast_custom_function_register(&acf_vmcount);
115 }
116
117 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Indicator for whether a voice mailbox has messages in a given folder.");