b44fb218b7a7d819f758604fa1402ed95dd890b3
[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  * \brief App to transmit a URL
21  * 
22  * \ingroup applications
23  */
24  
25 #include <string.h>
26 #include <stdlib.h>
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/logger.h"
35 #include "asterisk/channel.h"
36 #include "asterisk/pbx.h"
37 #include "asterisk/module.h"
38 #include "asterisk/translate.h"
39 #include "asterisk/image.h"
40 #include "asterisk/options.h"
41
42 static char *tdesc = "Send URL Applications";
43
44 static char *app = "SendURL";
45
46 static char *synopsis = "Send a URL";
47
48 static char *descrip = 
49 "  SendURL(URL[|option]): Requests client go to URL (IAX2) or sends the \n"
50 "URL to the client (other channels).\n"
51 "Result is returned in the SENDURLSTATUS channel variable:\n"
52 "    SUCCESS       URL successfully sent to client\n"
53 "    FAILURE       Failed to send URL\n"
54 "    NOLOAD        Clien failed to load URL (wait enabled)\n"
55 "    UNSUPPORTED   Channel does not support URL transport\n"
56 "\n"
57 "If the option 'wait' is specified, execution will wait for an\n"
58 "acknowledgement that the URL has been loaded before continuing\n"
59 "and will return -1 if the peer is unable to load the URL\n"
60 "\n"
61 "Old behaviour (deprecated): \n"
62 " If the client does not support Asterisk \"html\" transport, \n"
63 " and there exists a step with priority n + 101, then execution will\n"
64 " continue at that step.\n"
65 " Otherwise, execution will continue at the next priority level.\n"
66 " SendURL only returns 0 if the URL was sent correctly  or if\n"
67 " the channel does not support HTML transport, and -1 otherwise.\n";
68
69 STANDARD_LOCAL_USER;
70
71 LOCAL_USER_DECL;
72
73 static int sendurl_exec(struct ast_channel *chan, void *data)
74 {
75         int res = 0;
76         struct localuser *u;
77         char *tmp;
78         char *options;
79         int local_option_wait=0;
80         int local_option_jump = 0;
81         struct ast_frame *f;
82         char *stringp=NULL;
83         char *status = "FAILURE";
84         
85         if (ast_strlen_zero(data)) {
86                 ast_log(LOG_WARNING, "SendURL requires an argument (URL)\n");
87                 pbx_builtin_setvar_helper(chan, "SENDURLSTATUS", status);
88                 return -1;
89         }
90
91         LOCAL_USER_ADD(u);
92
93         tmp = ast_strdupa(data);
94         if (!tmp) {
95                 ast_log(LOG_ERROR, "Out of memory\n");
96                 LOCAL_USER_REMOVE(u);
97                 return -1;
98         }
99
100         stringp=tmp;
101         strsep(&stringp, "|");
102         options = strsep(&stringp, "|");
103         if (options && !strcasecmp(options, "wait"))
104                 local_option_wait = 1;
105         if (options && !strcasecmp(options, "j"))
106                 local_option_jump = 1;
107         
108         if (!ast_channel_supports_html(chan)) {
109                 /* Does not support transport */
110                 if (local_option_jump || option_priority_jumping)
111                          ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101);
112                 pbx_builtin_setvar_helper(chan, "SENDURLSTATUS", "UNSUPPORTED");
113                 LOCAL_USER_REMOVE(u);
114                 return 0;
115         }
116         res = ast_channel_sendurl(chan, tmp);
117         if (res == -1) {
118                 pbx_builtin_setvar_helper(chan, "SENDURLSTATUS", "FAILURE");
119                 LOCAL_USER_REMOVE(u);
120                 return res;
121         }
122         status = "SUCCESS";
123         if (local_option_wait) {
124                 for(;;) {
125                         /* Wait for an event */
126                         res = ast_waitfor(chan, -1);
127                         if (res < 0) 
128                                 break;
129                         f = ast_read(chan);
130                         if (!f) {
131                                 res = -1;
132                                 status = "FAILURE";
133                                 break;
134                         }
135                         if (f->frametype == AST_FRAME_HTML) {
136                                 switch(f->subclass) {
137                                 case AST_HTML_LDCOMPLETE:
138                                         res = 0;
139                                         ast_frfree(f);
140                                         status = "NOLOAD";
141                                         goto out;
142                                         break;
143                                 case AST_HTML_NOSUPPORT:
144                                         /* Does not support transport */
145                                         status ="UNSUPPORTED";
146                                         if (local_option_jump || option_priority_jumping)
147                                                 ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101);
148                                         res = 0;
149                                         goto out;
150                                         break;
151                                 default:
152                                         ast_log(LOG_WARNING, "Don't know what to do with HTML subclass %d\n", f->subclass);
153                                 };
154                         }
155                         ast_frfree(f);
156                 }
157         } 
158 out:    
159         pbx_builtin_setvar_helper(chan, "SENDURLSTATUS", status);
160         LOCAL_USER_REMOVE(u);
161         return res;
162 }
163
164 int unload_module(void)
165 {
166         int res;
167
168         res = ast_unregister_application(app);
169         
170         STANDARD_HANGUP_LOCALUSERS;
171
172         return res;     
173 }
174
175 int load_module(void)
176 {
177         return ast_register_application(app, sendurl_exec, synopsis, descrip);
178 }
179
180 char *description(void)
181 {
182         return tdesc;
183 }
184
185 int usecount(void)
186 {
187         int res;
188         STANDARD_USECOUNT(res);
189         return res;
190 }
191
192 char *key()
193 {
194         return ASTERISK_GPL_KEY;
195 }