| 1 | /* |
| 2 | * Asterisk -- An open source telephony toolkit. |
| 3 | * |
| 4 | * Copyright (c) 2006 Tilghman Lesher. All rights reserved. |
| 5 | * |
| 6 | * Tilghman Lesher <[email protected]> |
| 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 <[email protected]> |
| 24 | * |
| 25 | * \ingroup functions |
| 26 | */ |
| 27 | |
| 28 | /*** MODULEINFO |
| 29 | <support_level>core</support_level> |
| 30 | ***/ |
| 31 | |
| 32 | #include "asterisk.h" |
| 33 | |
| 34 | #include <dirent.h> |
| 35 | |
| 36 | #include "asterisk/file.h" |
| 37 | #include "asterisk/channel.h" |
| 38 | #include "asterisk/pbx.h" |
| 39 | #include "asterisk/module.h" |
| 40 | #include "asterisk/lock.h" |
| 41 | #include "asterisk/utils.h" |
| 42 | #include "asterisk/app.h" |
| 43 | |
| 44 | /*** DOCUMENTATION |
| 45 | <function name="VMCOUNT" language="en_US"> |
| 46 | <since> |
| 47 | <version>1.2.0</version> |
| 48 | </since> |
| 49 | <synopsis> |
| 50 | Count the voicemails in a specified mailbox or mailboxes. |
| 51 | </synopsis> |
| 52 | <syntax> |
| 53 | <parameter name="vmbox" required="true" argsep="&"> |
| 54 | <para>A mailbox or list of mailboxes</para> |
| 55 | </parameter> |
| 56 | <parameter name="folder" required="false"> |
| 57 | <para>If not specified, defaults to <literal>INBOX</literal></para> |
| 58 | </parameter> |
| 59 | </syntax> |
| 60 | <description> |
| 61 | <para>Count the number of voicemails in a specified mailbox, you could also specify |
| 62 | the mailbox <replaceable>folder</replaceable>.</para> |
| 63 | <example title="Mailbox folder count"> |
| 64 | exten => s,1,Set(foo=${VMCOUNT(125@default)}) |
| 65 | </example> |
| 66 | <para>An ampersand-separated list of mailboxes may be specified to count voicemails in |
| 67 | multiple mailboxes. If a folder is specified, this will apply to all mailboxes specified.</para> |
| 68 | <example title="Multiple mailbox inbox count"> |
| 69 | same => n,NoOp(${VMCOUNT(1234@default&1235@default&1236@default,INBOX)}) |
| 70 | </example> |
| 71 | </description> |
| 72 | </function> |
| 73 | ***/ |
| 74 | |
| 75 | static int acf_vmcount_exec(struct ast_channel *chan, const char *cmd, char *argsstr, char *buf, size_t len) |
| 76 | { |
| 77 | int total = 0; |
| 78 | char *mailbox = NULL; |
| 79 | AST_DECLARE_APP_ARGS(args, |
| 80 | AST_APP_ARG(vmbox); |
| 81 | AST_APP_ARG(folder); |
| 82 | ); |
| 83 | |
| 84 | buf[0] = '\0'; |
| 85 | |
| 86 | if (ast_strlen_zero(argsstr)) |
| 87 | return -1; |
| 88 | |
| 89 | AST_STANDARD_APP_ARGS(args, argsstr); |
| 90 | |
| 91 | if (ast_strlen_zero(args.vmbox)) { |
| 92 | return -1; |
| 93 | } |
| 94 | |
| 95 | if (ast_strlen_zero(args.folder)) { |
| 96 | args.folder = "INBOX"; |
| 97 | } |
| 98 | |
| 99 | while ((mailbox = strsep(&args.vmbox, "&"))) { |
| 100 | int c; |
| 101 | if (ast_strlen_zero(mailbox)) { |
| 102 | continue; |
| 103 | } |
| 104 | c = ast_app_messagecount(mailbox, args.folder); |
| 105 | total += (c > 0 ? c : 0); |
| 106 | } |
| 107 | snprintf(buf, len, "%d", total); |
| 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | static struct ast_custom_function acf_vmcount = { |
| 113 | .name = "VMCOUNT", |
| 114 | .read = acf_vmcount_exec, |
| 115 | .read_max = 12, |
| 116 | }; |
| 117 | |
| 118 | static int unload_module(void) |
| 119 | { |
| 120 | return ast_custom_function_unregister(&acf_vmcount); |
| 121 | } |
| 122 | |
| 123 | static int load_module(void) |
| 124 | { |
| 125 | return ast_custom_function_register(&acf_vmcount); |
| 126 | } |
| 127 | |
| 128 | AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Indicator for whether a voice mailbox has messages in a given folder."); |