44c3909fe9f24fba60007ad293d7946778f3dfb7
[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 "asterisk/lock.h"
33 #include "asterisk/file.h"
34 #include "asterisk/channel.h"
35 #include "asterisk/pbx.h"
36 #include "asterisk/module.h"
37 #include "asterisk/translate.h"
38 #include "asterisk/image.h"
39 #include "asterisk/app.h"
40
41 static char *app = "SendURL";
42
43 static char *synopsis = "Send a URL";
44
45 static char *descrip = 
46 "  SendURL(URL[,option]): Requests client go to URL (IAX2) or sends the \n"
47 "URL to the client (other channels).\n"
48 "Result is returned in the SENDURLSTATUS channel variable:\n"
49 "    SUCCESS       URL successfully sent to client\n"
50 "    FAILURE       Failed to send URL\n"
51 "    NOLOAD        Client failed to load URL (wait enabled)\n"
52 "    UNSUPPORTED   Channel does not support URL transport\n"
53 "\n"
54 "If the option 'w' is specified, execution will wait for an\n"
55 "acknowledgement that the URL has been loaded before continuing\n"
56 "\n"
57 "SendURL continues normally if the URL was sent correctly or if the channel\n"
58 "does not support HTML transport.  Otherwise, the channel is hung up.\n";
59
60 enum {
61         OPTION_WAIT = (1 << 0),
62 } option_flags;
63
64 AST_APP_OPTIONS(app_opts,{
65         AST_APP_OPTION('w', OPTION_WAIT),
66 });
67
68 static int sendurl_exec(struct ast_channel *chan, void *data)
69 {
70         int res = 0;
71         char *tmp;
72         struct ast_frame *f;
73         char *status = "FAILURE";
74         char *opts[0];
75         struct ast_flags flags;
76         AST_DECLARE_APP_ARGS(args,
77                 AST_APP_ARG(url);
78                 AST_APP_ARG(options);
79         );
80         
81         if (ast_strlen_zero(data)) {
82                 ast_log(LOG_WARNING, "SendURL requires an argument (URL)\n");
83                 pbx_builtin_setvar_helper(chan, "SENDURLSTATUS", status);
84                 return -1;
85         }
86
87         tmp = ast_strdupa(data);
88
89         AST_STANDARD_APP_ARGS(args, tmp);
90         if (args.argc == 2)
91                 ast_app_parse_options(app_opts, &flags, opts, args.options);
92         
93         if (!ast_channel_supports_html(chan)) {
94                 /* Does not support transport */
95                 pbx_builtin_setvar_helper(chan, "SENDURLSTATUS", "UNSUPPORTED");
96                 return 0;
97         }
98         res = ast_channel_sendurl(chan, args.url);
99         if (res == -1) {
100                 pbx_builtin_setvar_helper(chan, "SENDURLSTATUS", "FAILURE");
101                 return res;
102         }
103         status = "SUCCESS";
104         if (ast_test_flag(&flags, OPTION_WAIT)) {
105                 for(;;) {
106                         /* Wait for an event */
107                         res = ast_waitfor(chan, -1);
108                         if (res < 0) 
109                                 break;
110                         f = ast_read(chan);
111                         if (!f) {
112                                 res = -1;
113                                 status = "FAILURE";
114                                 break;
115                         }
116                         if (f->frametype == AST_FRAME_HTML) {
117                                 switch(f->subclass) {
118                                 case AST_HTML_LDCOMPLETE:
119                                         res = 0;
120                                         ast_frfree(f);
121                                         status = "NOLOAD";
122                                         goto out;
123                                         break;
124                                 case AST_HTML_NOSUPPORT:
125                                         /* Does not support transport */
126                                         status = "UNSUPPORTED";
127                                         res = 0;
128                                         ast_frfree(f);
129                                         goto out;
130                                         break;
131                                 default:
132                                         ast_log(LOG_WARNING, "Don't know what to do with HTML subclass %d\n", f->subclass);
133                                 };
134                         }
135                         ast_frfree(f);
136                 }
137         } 
138 out:    
139         pbx_builtin_setvar_helper(chan, "SENDURLSTATUS", status);
140         return res;
141 }
142
143 static int unload_module(void)
144 {
145         return ast_unregister_application(app);
146 }
147
148 static int load_module(void)
149 {
150         return ast_register_application(app, sendurl_exec, synopsis, descrip);
151 }
152
153 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Send URL Applications");