83f8349138075453a7586b8bcc7f762dfb82541e
[asterisk/asterisk.git] / apps / app_hasnewvoicemail.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Changes Copyright (c) 2004 - 2005 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 /*
24  *
25  * HasVoicemail application
26  *
27  */
28
29 #include <sys/types.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <string.h>
33 #include <dirent.h>
34
35 #include "asterisk.h"
36
37 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
38
39 #include "asterisk/file.h"
40 #include "asterisk/logger.h"
41 #include "asterisk/channel.h"
42 #include "asterisk/pbx.h"
43 #include "asterisk/module.h"
44 #include "asterisk/lock.h"
45 #include "asterisk/utils.h"
46
47 static char *tdesc = "Indicator for whether a voice mailbox has messages in a given folder.";
48 static char *app_hasvoicemail = "HasVoicemail";
49 static char *hasvoicemail_synopsis = "Conditionally branches to priority + 101";
50 static char *hasvoicemail_descrip =
51 "HasVoicemail(vmbox[/folder][@context][|varname])\n"
52 "  Branches to priority + 101, if there is voicemail in folder indicated."
53 "  Optionally sets <varname> to the number of messages in that folder."
54 "  Assumes folder of INBOX if not specified.\n";
55
56 static char *app_hasnewvoicemail = "HasNewVoicemail";
57 static char *hasnewvoicemail_synopsis = "Conditionally branches to priority + 101";
58 static char *hasnewvoicemail_descrip =
59 "HasNewVoicemail(vmbox[/folder][@context][|varname])\n"
60 "  Branches to priority + 101, if there is voicemail in folder 'folder' or INBOX.\n"
61 "if folder is not specified. Optionally sets <varname> to the number of messages\n" 
62 "in that folder.\n";
63
64 STANDARD_LOCAL_USER;
65
66 LOCAL_USER_DECL;
67
68 static int hasvoicemail_internal(char *context, char *box, char *folder)
69 {
70         char vmpath[256];
71         DIR *vmdir;
72         struct dirent *vment;
73         int count=0;
74
75         snprintf(vmpath,sizeof(vmpath), "%s/voicemail/%s/%s/%s", (char *)ast_config_AST_SPOOL_DIR, context, box, folder);
76         if ((vmdir = opendir(vmpath))) {
77                 /* No matter what the format of VM, there will always be a .txt file for each message. */
78                 while ((vment = readdir(vmdir))) {
79                         if (!strncmp(vment->d_name + 7, ".txt", 4)) {
80                                 count++;
81                                 break;
82                         }
83                 }
84                 closedir(vmdir);
85         }
86         return count;
87 }
88
89 static int hasvoicemail_exec(struct ast_channel *chan, void *data)
90 {
91         struct localuser *u;
92         char *temps, *input, *varname = NULL, *vmbox, *context = "default";
93         char *vmfolder;
94         int vmcount = 0;
95         static int dep_warning = 0;
96
97         if (!data) {
98                 ast_log(LOG_WARNING, "HasVoicemail requires an argument (vm-box[/folder][@context]|varname)\n");
99                 return -1;
100         }
101
102         if (!dep_warning) {
103                 ast_log(LOG_WARNING, "The applications HasVoicemail and HasNewVoicemail have been deprecated.  Please use the VMCOUNT() function instead.\n");
104                 dep_warning = 1;
105         }
106
107         input = ast_strdupa((char *)data);
108         if (! input) {
109                 ast_log(LOG_ERROR, "Out of memory error\n");
110                 return -1;
111         }
112
113         LOCAL_USER_ADD(u);
114
115         temps = input;
116         if ((temps = strsep(&input, "|"))) {
117                 if (input && !ast_strlen_zero(input))
118                         varname = input;
119                 input = temps;
120         }
121
122         if ((vmbox = strsep(&input, "@")))
123                 if (input && !ast_strlen_zero(input))
124                         context = input;
125         if (!vmbox)
126                 vmbox = input;
127
128         vmfolder = strchr(vmbox, '/');
129         if (vmfolder) {
130                 *vmfolder = '\0';
131                 vmfolder++;
132         } else {
133                 vmfolder = "INBOX";
134         }
135
136         vmcount = hasvoicemail_internal(context, vmbox, vmfolder);
137         /* Set the count in the channel variable */
138         if (varname) {
139                 char tmp[12];
140                 snprintf(tmp, sizeof(tmp), "%d", vmcount);
141                 pbx_builtin_setvar_helper(chan, varname, tmp);
142         }
143
144         if (vmcount > 0) {
145                 /* Branch to the next extension */
146                 if (!ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101)) 
147                         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);
148         }
149
150         LOCAL_USER_REMOVE(u);
151         return 0;
152 }
153
154 static char *acf_vmcount_exec(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
155 {
156         struct localuser *u;
157         char *args, *context, *box, *folder;
158
159         LOCAL_USER_ACF_ADD(u);
160
161         args = ast_strdupa(data);
162         if (!args) {
163                 ast_log(LOG_ERROR, "Out of memory");
164                 LOCAL_USER_REMOVE(u);
165                 return "";
166         }
167
168         box = strsep(&args, "|");
169         if (strchr(box, '@')) {
170                 context = box;
171                 box = strsep(&context, "@");
172         } else {
173                 context = "default";
174         }
175
176         if (args) {
177                 folder = args;
178         } else {
179                 folder = "INBOX";
180         }
181
182         snprintf(buf, len, "%d", hasvoicemail_internal(context, box, folder));
183         LOCAL_USER_REMOVE(u);
184         return buf;
185 }
186
187 struct ast_custom_function acf_vmcount = {
188         .name = "VMCOUNT",
189         .synopsis = "Counts the voicemail in a specified mailbox",
190         .syntax = "VMCOUNT(vmbox[@context][|folder])",
191         .desc =
192 "  context - defaults to \"default\"\n"
193 "  folder  - defaults to \"INBOX\"\n",
194         .read = acf_vmcount_exec,
195 };
196
197 int unload_module(void)
198 {
199         int res;
200         STANDARD_HANGUP_LOCALUSERS;
201         res = ast_custom_function_unregister(&acf_vmcount);
202         res |= ast_unregister_application(app_hasvoicemail);
203         res |= ast_unregister_application(app_hasnewvoicemail);
204         return res;
205 }
206
207 int load_module(void)
208 {
209         int res;
210         res = ast_custom_function_register(&acf_vmcount);
211         res |= ast_register_application(app_hasvoicemail, hasvoicemail_exec, hasvoicemail_synopsis, hasvoicemail_descrip);
212         res |= ast_register_application(app_hasnewvoicemail, hasvoicemail_exec, hasnewvoicemail_synopsis, hasnewvoicemail_descrip);
213         return res;
214 }
215
216 char *description(void)
217 {
218         return tdesc;
219 }
220
221 int usecount(void)
222 {
223         int res;
224         STANDARD_USECOUNT(res);
225         return res;
226 }
227
228 char *key()
229 {
230         return ASTERISK_GPL_KEY;
231 }