a1f295a32602aa6fcd6e4d82137c8e13ca9c2fb3
[asterisk/asterisk.git] / apps / app_url.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief App to transmit a URL
22  *
23  * \author Mark Spencer <markster@digium.com>
24  * 
25  * \ingroup applications
26  */
27  
28 #include "asterisk.h"
29
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
31
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <string.h>
35
36 #include "asterisk/lock.h"
37 #include "asterisk/file.h"
38 #include "asterisk/logger.h"
39 #include "asterisk/channel.h"
40 #include "asterisk/pbx.h"
41 #include "asterisk/module.h"
42 #include "asterisk/translate.h"
43 #include "asterisk/image.h"
44 #include "asterisk/options.h"
45
46 static char *app = "SendURL";
47
48 static char *synopsis = "Send a URL";
49
50 static char *descrip = 
51 "  SendURL(URL[|option]): Requests client go to URL (IAX2) or sends the \n"
52 "URL to the client (other channels).\n"
53 "Result is returned in the SENDURLSTATUS channel variable:\n"
54 "    SUCCESS       URL successfully sent to client\n"
55 "    FAILURE       Failed to send URL\n"
56 "    NOLOAD        Client failed to load URL (wait enabled)\n"
57 "    UNSUPPORTED   Channel does not support URL transport\n"
58 "\n"
59 "If the option 'wait' is specified, execution will wait for an\n"
60 "acknowledgement that the URL has been loaded before continuing\n"
61 "\n"
62 "If jumping is specified as an option (the 'j' flag), the client does not\n"
63 "support Asterisk \"html\" transport, and there exists a step with priority\n"
64 "n + 101, then execution will continue at that step.\n"
65 "\n"
66 "SendURL continues normally if the URL was sent correctly or if the channel\n"
67 "does not support HTML transport.  Otherwise, the channel is hung up.\n";
68
69
70 static int sendurl_exec(struct ast_channel *chan, void *data)
71 {
72         int res = 0;
73         struct ast_module_user *u;
74         char *tmp;
75         char *options;
76         int local_option_wait=0;
77         int local_option_jump = 0;
78         struct ast_frame *f;
79         char *stringp=NULL;
80         char *status = "FAILURE";
81         
82         if (ast_strlen_zero(data)) {
83                 ast_log(LOG_WARNING, "SendURL requires an argument (URL)\n");
84                 pbx_builtin_setvar_helper(chan, "SENDURLSTATUS", status);
85                 return -1;
86         }
87
88         u = ast_module_user_add(chan);
89
90         tmp = ast_strdupa(data);
91
92         stringp=tmp;
93         strsep(&stringp, "|");
94         options = strsep(&stringp, "|");
95         if (options && !strcasecmp(options, "wait"))
96                 local_option_wait = 1;
97         if (options && !strcasecmp(options, "j"))
98                 local_option_jump = 1;
99         
100         if (!ast_channel_supports_html(chan)) {
101                 /* Does not support transport */
102                 if (local_option_jump || ast_opt_priority_jumping)
103                          ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101);
104                 pbx_builtin_setvar_helper(chan, "SENDURLSTATUS", "UNSUPPORTED");
105                 ast_module_user_remove(u);
106                 return 0;
107         }
108         res = ast_channel_sendurl(chan, tmp);
109         if (res == -1) {
110                 pbx_builtin_setvar_helper(chan, "SENDURLSTATUS", "FAILURE");
111                 ast_module_user_remove(u);
112                 return res;
113         }
114         status = "SUCCESS";
115         if (local_option_wait) {
116                 for(;;) {
117                         /* Wait for an event */
118                         res = ast_waitfor(chan, -1);
119                         if (res < 0) 
120                                 break;
121                         f = ast_read(chan);
122                         if (!f) {
123                                 res = -1;
124                                 status = "FAILURE";
125                                 break;
126                         }
127                         if (f->frametype == AST_FRAME_HTML) {
128                                 switch(f->subclass) {
129                                 case AST_HTML_LDCOMPLETE:
130                                         res = 0;
131                                         ast_frfree(f);
132                                         status = "NOLOAD";
133                                         goto out;
134                                         break;
135                                 case AST_HTML_NOSUPPORT:
136                                         /* Does not support transport */
137                                         status ="UNSUPPORTED";
138                                         if (local_option_jump || ast_opt_priority_jumping)
139                                                 ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101);
140                                         res = 0;
141                                         ast_frfree(f);
142                                         goto out;
143                                         break;
144                                 default:
145                                         ast_log(LOG_WARNING, "Don't know what to do with HTML subclass %d\n", f->subclass);
146                                 };
147                         }
148                         ast_frfree(f);
149                 }
150         } 
151 out:    
152         pbx_builtin_setvar_helper(chan, "SENDURLSTATUS", status);
153         ast_module_user_remove(u);
154         return res;
155 }
156
157 static int unload_module(void)
158 {
159         int res;
160
161         res = ast_unregister_application(app);
162         
163         ast_module_user_hangup_all();
164
165         return res;     
166 }
167
168 static int load_module(void)
169 {
170         return ast_register_application(app, sendurl_exec, synopsis, descrip);
171 }
172
173 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Send URL Applications");