more license/copyright header updates (thanks Ian!)
[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[@context][:folder][|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_exec(struct ast_channel *chan, void *data)
69 {
70         int res=0;
71         struct localuser *u;
72         char vmpath[256], *temps, *input, *varname = NULL, *vmbox, *context = "default";
73         char *vmfolder;
74         DIR *vmdir;
75         struct dirent *vment;
76         int vmcount = 0;
77
78         if (!data) {
79                 ast_log(LOG_WARNING, "HasVoicemail requires an argument (vm-box[@context][:folder]|varname)\n");
80                 return -1;
81         }
82         LOCAL_USER_ADD(u);
83
84         input = ast_strdupa((char *)data);
85         if (input) {
86                 temps = input;
87                 if ((temps = strsep(&input, "|"))) {
88                         if (input && !ast_strlen_zero(input))
89                                 varname = input;
90                         input = temps;
91                 }
92                 if ((temps = strsep(&input, ":"))) {
93                         if (input && !ast_strlen_zero(input))
94                                 vmfolder = input;
95                         input = temps;
96                 }
97                 if ((vmbox = strsep(&input, "@")))
98                         if (input && !ast_strlen_zero(input))
99                                 context = input;
100                 if (!vmbox)
101                         vmbox = input;
102                 vmfolder = strchr(vmbox, '/');
103                 if (vmfolder) {
104                         *vmfolder = '\0';
105                         vmfolder++;
106                 } else
107                         vmfolder = "INBOX";
108                 snprintf(vmpath,sizeof(vmpath), "%s/voicemail/%s/%s/%s", (char *)ast_config_AST_SPOOL_DIR, context, vmbox, vmfolder);
109                 if (!(vmdir = opendir(vmpath))) {
110                         ast_log(LOG_NOTICE, "Voice mailbox %s at %s does not exist\n", vmbox, vmpath);
111                 } else {
112
113                         /* No matter what the format of VM, there will always be a .txt file for each message. */
114                         while ((vment = readdir(vmdir)))
115                                 if (!strncmp(vment->d_name + 7,".txt",4))
116                                         vmcount++;
117                         closedir(vmdir);
118                 }
119                 /* Set the count in the channel variable */
120                 if (varname) {
121                         char tmp[12];
122                         snprintf(tmp, sizeof(tmp), "%d", vmcount);
123                         pbx_builtin_setvar_helper(chan, varname, tmp);
124                 }
125
126                 if (vmcount > 0) {
127                         /* Branch to the next extension */
128                         if (!ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101)) 
129                                 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);
130                 }
131         } else {
132                 ast_log(LOG_ERROR, "Out of memory error\n");
133         }
134
135         LOCAL_USER_REMOVE(u);
136         return res;
137 }
138
139 int unload_module(void)
140 {
141         int res;
142         STANDARD_HANGUP_LOCALUSERS;
143         res = ast_unregister_application(app_hasvoicemail);
144         res |= ast_unregister_application(app_hasnewvoicemail);
145         return res;
146 }
147
148 int load_module(void)
149 {
150         int res;
151         res = ast_register_application(app_hasvoicemail, hasvoicemail_exec, hasvoicemail_synopsis, hasvoicemail_descrip);
152         res |= ast_register_application(app_hasnewvoicemail, hasvoicemail_exec, hasnewvoicemail_synopsis, hasnewvoicemail_descrip);
153         return res;
154 }
155
156 char *description(void)
157 {
158         return tdesc;
159 }
160
161 int usecount(void)
162 {
163         int res;
164         STANDARD_USECOUNT(res);
165         return res;
166 }
167
168 char *key()
169 {
170         return ASTERISK_GPL_KEY;
171 }