f24d5492ad28bfe2e10518056201acd70fc57115
[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 "SendURL continues normally if the URL was sent correctly or if the channel\n"
63 "does not support HTML transport.  Otherwise, the channel is hung up.\n";
64
65
66 static int sendurl_exec(struct ast_channel *chan, void *data)
67 {
68         int res = 0;
69         struct ast_module_user *u;
70         char *tmp;
71         char *options;
72         int local_option_wait=0;
73         struct ast_frame *f;
74         char *stringp=NULL;
75         char *status = "FAILURE";
76         
77         if (ast_strlen_zero(data)) {
78                 ast_log(LOG_WARNING, "SendURL requires an argument (URL)\n");
79                 pbx_builtin_setvar_helper(chan, "SENDURLSTATUS", status);
80                 return -1;
81         }
82
83         u = ast_module_user_add(chan);
84
85         tmp = ast_strdupa(data);
86
87         stringp=tmp;
88         strsep(&stringp, "|");
89         options = strsep(&stringp, "|");
90         if (options && !strcasecmp(options, "wait"))
91                 local_option_wait = 1;
92         
93         if (!ast_channel_supports_html(chan)) {
94                 /* Does not support transport */
95                 pbx_builtin_setvar_helper(chan, "SENDURLSTATUS", "UNSUPPORTED");
96                 ast_module_user_remove(u);
97                 return 0;
98         }
99         res = ast_channel_sendurl(chan, tmp);
100         if (res == -1) {
101                 pbx_builtin_setvar_helper(chan, "SENDURLSTATUS", "FAILURE");
102                 ast_module_user_remove(u);
103                 return res;
104         }
105         status = "SUCCESS";
106         if (local_option_wait) {
107                 for(;;) {
108                         /* Wait for an event */
109                         res = ast_waitfor(chan, -1);
110                         if (res < 0) 
111                                 break;
112                         f = ast_read(chan);
113                         if (!f) {
114                                 res = -1;
115                                 status = "FAILURE";
116                                 break;
117                         }
118                         if (f->frametype == AST_FRAME_HTML) {
119                                 switch(f->subclass) {
120                                 case AST_HTML_LDCOMPLETE:
121                                         res = 0;
122                                         ast_frfree(f);
123                                         status = "NOLOAD";
124                                         goto out;
125                                         break;
126                                 case AST_HTML_NOSUPPORT:
127                                         /* Does not support transport */
128                                         status ="UNSUPPORTED";
129                                         res = 0;
130                                         ast_frfree(f);
131                                         goto out;
132                                         break;
133                                 default:
134                                         ast_log(LOG_WARNING, "Don't know what to do with HTML subclass %d\n", f->subclass);
135                                 };
136                         }
137                         ast_frfree(f);
138                 }
139         } 
140 out:    
141         pbx_builtin_setvar_helper(chan, "SENDURLSTATUS", status);
142         ast_module_user_remove(u);
143         return res;
144 }
145
146 static int unload_module(void)
147 {
148         return ast_unregister_application(app);
149 }
150
151 static int load_module(void)
152 {
153         return ast_register_application(app, sendurl_exec, synopsis, descrip);
154 }
155
156 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Send URL Applications");