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