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