d5dd70176a449459de4b32a78754d99b79905503
[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 /*** MODULEINFO
29         <support_level>extended</support_level>
30  ***/
31  
32 #include "asterisk.h"
33
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
35
36 #include "asterisk/pbx.h"
37 #include "asterisk/module.h"
38 #include "asterisk/app.h"
39 #include "asterisk/channel.h"
40
41 /*** DOCUMENTATION
42         <application name="SendURL" language="en_US">
43                 <synopsis>
44                         Send a URL.
45                 </synopsis>
46                 <syntax>
47                         <parameter name="URL" required="true" />
48                         <parameter name="option">
49                                 <optionlist>
50                                         <option name="w">
51                                                 <para>Execution will wait for an acknowledgement that the
52                                                 URL has been loaded before continuing.</para>
53                                         </option>
54                                 </optionlist>
55                         </parameter>
56                 </syntax>
57                 <description>
58                         <para>Requests client go to <replaceable>URL</replaceable> (IAX2) or sends the
59                         URL to the client (other channels).</para>
60                         <para>Result is returned in the <variable>SENDURLSTATUS</variable> channel variable:</para>
61                         <variablelist>
62                                 <variable name="SENDURLSTATUS">
63                                         <value name="SUCCESS">
64                                                 URL successfully sent to client.
65                                         </value>
66                                         <value name="FAILURE">
67                                                 Failed to send URL.
68                                         </value>
69                                         <value name="NOLOAD">
70                                                 Client failed to load URL (wait enabled).
71                                         </value>
72                                         <value name="UNSUPPORTED">
73                                                 Channel does not support URL transport.
74                                         </value>
75                                 </variable>
76                         </variablelist>
77                         <para>SendURL continues normally if the URL was sent correctly or if the channel
78                         does not support HTML transport.  Otherwise, the channel is hung up.</para>
79                 </description>
80                 <see-also>
81                         <ref type="application">SendImage</ref>
82                         <ref type="application">SendText</ref>
83                 </see-also>
84         </application>
85  ***/
86
87 static char *app = "SendURL";
88
89 enum option_flags {
90         OPTION_WAIT = (1 << 0),
91 };
92
93 AST_APP_OPTIONS(app_opts,{
94         AST_APP_OPTION('w', OPTION_WAIT),
95 });
96
97 static int sendurl_exec(struct ast_channel *chan, const char *data)
98 {
99         int res = 0;
100         char *tmp;
101         struct ast_frame *f;
102         char *status = "FAILURE";
103         char *opts[0];
104         struct ast_flags flags = { 0 };
105         AST_DECLARE_APP_ARGS(args,
106                 AST_APP_ARG(url);
107                 AST_APP_ARG(options);
108         );
109         
110         if (ast_strlen_zero(data)) {
111                 ast_log(LOG_WARNING, "SendURL requires an argument (URL)\n");
112                 pbx_builtin_setvar_helper(chan, "SENDURLSTATUS", status);
113                 return -1;
114         }
115
116         tmp = ast_strdupa(data);
117
118         AST_STANDARD_APP_ARGS(args, tmp);
119         if (args.argc == 2)
120                 ast_app_parse_options(app_opts, &flags, opts, args.options);
121         
122         if (!ast_channel_supports_html(chan)) {
123                 /* Does not support transport */
124                 pbx_builtin_setvar_helper(chan, "SENDURLSTATUS", "UNSUPPORTED");
125                 return 0;
126         }
127         res = ast_channel_sendurl(chan, args.url);
128         if (res == -1) {
129                 pbx_builtin_setvar_helper(chan, "SENDURLSTATUS", "FAILURE");
130                 return res;
131         }
132         status = "SUCCESS";
133         if (ast_test_flag(&flags, OPTION_WAIT)) {
134                 for(;;) {
135                         /* Wait for an event */
136                         res = ast_waitfor(chan, -1);
137                         if (res < 0) 
138                                 break;
139                         f = ast_read(chan);
140                         if (!f) {
141                                 res = -1;
142                                 status = "FAILURE";
143                                 break;
144                         }
145                         if (f->frametype == AST_FRAME_HTML) {
146                                 switch (f->subclass.integer) {
147                                 case AST_HTML_LDCOMPLETE:
148                                         res = 0;
149                                         ast_frfree(f);
150                                         status = "NOLOAD";
151                                         goto out;
152                                         break;
153                                 case AST_HTML_NOSUPPORT:
154                                         /* Does not support transport */
155                                         status = "UNSUPPORTED";
156                                         res = 0;
157                                         ast_frfree(f);
158                                         goto out;
159                                         break;
160                                 default:
161                                         ast_log(LOG_WARNING, "Don't know what to do with HTML subclass %d\n", f->subclass.integer);
162                                 };
163                         }
164                         ast_frfree(f);
165                 }
166         } 
167 out:    
168         pbx_builtin_setvar_helper(chan, "SENDURLSTATUS", status);
169         return res;
170 }
171
172 static int unload_module(void)
173 {
174         return ast_unregister_application(app);
175 }
176
177 static int load_module(void)
178 {
179         return ast_register_application_xml(app, sendurl_exec);
180 }
181
182 AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Send URL Applications");
183