Use find_user for existsmailbox
[asterisk/asterisk.git] / apps / app_hasnewvoicemail.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * HasVoicemail application
5  * Changes Copyright (c) 2004 Todd Freeman <freeman@andrews.edu>
6  * 
7  * 95% based on HasNewVoicemail by:
8  * 
9  * Copyright (c) 2003 Tilghman Lesher.  All rights reserved.
10  * 
11  * Tilghman Lesher <asterisk-hasnewvoicemail-app@the-tilghman.com>
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
25  * EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <sys/types.h>
35 #include <asterisk/file.h>
36 #include <asterisk/logger.h>
37 #include <asterisk/channel.h>
38 #include <asterisk/pbx.h>
39 #include <asterisk/module.h>
40 #include <stdlib.h>
41 #include <unistd.h>
42 #include <string.h>
43 #include <stdlib.h>
44 #include <dirent.h>
45
46 #include <pthread.h>
47
48 #include "../astconf.h"
49
50 static char *tdesc = "Indicator for whether a voice mailbox has messages in a given folder.";
51 static char *app_hasvoicemail = "HasVoicemail";
52 static char *hasvoicemail_synopsis = "Conditionally branches to priority + 101";
53 static char *hasvoicemail_descrip =
54 "HasVoicemail(vmbox[@context][:folder][|varname])\n"
55 "  Branches to priority + 101, if there is voicemail in folder indicated."
56 "  Optionally sets <varname> to the number of messages in that folder."
57 "  Assumes folder of INBOX if not specified.\n";
58
59 static char *app_hasnewvoicemail = "HasNewVoicemail";
60 static char *hasnewvoicemail_synopsis = "Conditionally branches to priority + 101";
61 static char *hasnewvoicemail_descrip =
62 "HasNewVoicemail(vmbox[@context][|varname])\n"
63 "  Branches to priority + 101, if there is voicemail in folder INBOX."
64 "  Optionally sets <varname> to the number of messages in that folder.\n";
65
66 STANDARD_LOCAL_USER;
67
68 LOCAL_USER_DECL;
69
70 static int hasvoicemail_exec(struct ast_channel *chan, void *data)
71 {
72         int res=0;
73         struct localuser *u;
74         char vmpath[256], *input, *varname = NULL, *vmbox, *vmfolder = "INBOX", *context = "default";
75         DIR *vmdir;
76         struct dirent *vment;
77         int vmcount = 0;
78
79         if (!data) {
80                 ast_log(LOG_WARNING, "HasVoicemail requires an argument (vm-box[@context][:folder]|varname)\n");
81                 return -1;
82         }
83         LOCAL_USER_ADD(u);
84
85         input = ast_strdupa((char *)data);
86         if (input) {
87                 if ((vmbox = strsep(&input,":")))
88                         if ((vmfolder = strsep(&input,"|")))
89                                 varname = input;
90                         else
91                                 vmfolder = input;
92                 else
93                         if ((vmbox = strsep(&input,"|")))
94                                 varname = input;
95                         else
96                                 vmbox = input;
97
98                 if (index(vmbox,'@')) {
99                         context = vmbox;
100                         vmbox = strsep(&context,"@");
101                 }
102
103                 snprintf(vmpath,sizeof(vmpath), "%s/voicemail/%s/%s/%s", (char *)ast_config_AST_SPOOL_DIR, context, vmbox, vmfolder);
104                 if (!(vmdir = opendir(vmpath))) {
105                         ast_log(LOG_NOTICE, "Voice mailbox %s at %s does not exist\n", vmbox, vmpath);
106                 } else {
107
108                         /* No matter what the format of VM, there will always be a .txt file for each message. */
109                         while ((vment = readdir(vmdir)))
110                                 if (!strncmp(vment->d_name + 7,".txt",4))
111                                         vmcount++;
112                         closedir(vmdir);
113                 }
114                 /* Set the count in the channel variable */
115                 if (varname) {
116                         char tmp[12];
117                         snprintf(tmp, sizeof(tmp) - 1, "%d", vmcount);
118                         pbx_builtin_setvar_helper(chan, varname, tmp);
119                 }
120
121                 if (vmcount > 0) {
122                         /* Branch to the next extension */
123                         if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->callerid)) {
124                                 chan->priority += 100;
125                         } else
126                                 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);
127                 }
128         } else {
129                 ast_log(LOG_ERROR, "Out of memory error\n");
130         }
131
132         LOCAL_USER_REMOVE(u);
133         return res;
134 }
135
136 int unload_module(void)
137 {
138         int res;
139         STANDARD_HANGUP_LOCALUSERS;
140         res = ast_unregister_application(app_hasvoicemail);
141         res |= ast_unregister_application(app_hasnewvoicemail);
142         return res;
143 }
144
145 int load_module(void)
146 {
147         int res;
148         res = ast_register_application(app_hasvoicemail, hasvoicemail_exec, hasvoicemail_synopsis, hasvoicemail_descrip);
149         res |= ast_register_application(app_hasnewvoicemail, hasvoicemail_exec, hasnewvoicemail_synopsis, hasnewvoicemail_descrip);
150         return res;
151 }
152
153 char *description(void)
154 {
155         return tdesc;
156 }
157
158 int usecount(void)
159 {
160         int res;
161         STANDARD_USECOUNT(res);
162         return res;
163 }
164
165 char *key()
166 {
167         return ASTERISK_GPL_KEY;
168 }