f1b055b71cdbb6083a62584779647487f8c44ae7
[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 #include "../astconf.h"
46
47 static char *tdesc = "Indicator for whether a voice mailbox has new messages.";
48 static char *app_hasnewvoicemail = "HasNewVoicemail";
49 static char *hasnewvoicemail_synopsis = "Conditionally branches to priority + 101";
50 static char *hasnewvoicemail_descrip =
51 "HasNewVoicemail(vmbox[@context][|varname])\n"
52 "  Branches to priority + 101, if there is new voicemail"
53 "  Optionally sets <varname> to the number of new messages.\n";
54
55 STANDARD_LOCAL_USER;
56
57 LOCAL_USER_DECL;
58
59 static int hasnewvoicemail_exec(struct ast_channel *chan, void *data)
60 {
61         int res=0;
62         struct localuser *u;
63         char vmpath[256], *input, *varname = NULL, *vmbox, *context = "default";
64         DIR *vmdir;
65         struct dirent *vment;
66         int vmcount = 0;
67
68         if (!data) {
69                 ast_log(LOG_WARNING, "HasNewVoicemail requires an argument (vm-box[@context]|varname)\n");
70                 return -1;
71         }
72         LOCAL_USER_ADD(u);
73
74         input = ast_strdupa((char *)data);
75         if (input) {
76                 if ((vmbox = strsep(&input,"|")))
77                         varname = input;
78                 else
79                         vmbox = input;
80
81                 if (index(vmbox,'@')) {
82                         context = vmbox;
83                         vmbox = strsep(&context,"@");
84                 }
85
86                 snprintf(vmpath,sizeof(vmpath), "%s/voicemail/%s/%s/INBOX", (char *)ast_config_AST_SPOOL_DIR, context, vmbox);
87                 if (!(vmdir = opendir(vmpath))) {
88                         ast_log(LOG_NOTICE, "Voice mailbox %s at %s does not exist\n", vmbox, vmpath);
89                 } else {
90
91                         /* No matter what the format of VM, there will always be a .txt file for each message. */
92                         while ((vment = readdir(vmdir)))
93                                 if (!strncmp(vment->d_name + 7,".txt",4))
94                                         vmcount++;
95                         closedir(vmdir);
96                 }
97                 /* Set the count in the channel variable */
98                 if (varname) {
99                         char tmp[12];
100                         snprintf(tmp, sizeof(tmp) - 1, "%d", vmcount);
101                         pbx_builtin_setvar_helper(chan, varname, tmp);
102                 }
103
104                 if (vmcount > 0) {
105                         /* Branch to the next extension */
106                         if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->callerid)) {
107                                 chan->priority += 100;
108                         } else
109                                 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);
110                 }
111         } else {
112                 ast_log(LOG_ERROR, "Out of memory error\n");
113         }
114
115         LOCAL_USER_REMOVE(u);
116         return res;
117 }
118
119 int unload_module(void)
120 {
121         STANDARD_HANGUP_LOCALUSERS;
122         return ast_unregister_application(app_hasnewvoicemail);
123 }
124
125 int load_module(void)
126 {
127         return ast_register_application(app_hasnewvoicemail, hasnewvoicemail_exec, hasnewvoicemail_synopsis, hasnewvoicemail_descrip);
128 }
129
130 char *description(void)
131 {
132         return tdesc;
133 }
134
135 int usecount(void)
136 {
137         int res;
138         STANDARD_USECOUNT(res);
139         return res;
140 }
141
142 char *key()
143 {
144         return ASTERISK_GPL_KEY;
145 }