merge new_loader_completion branch, including (at least):
[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        Clien 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 "and will return -1 if the peer is unable to load the URL\n"
62 "\n"
63 "Old behaviour (deprecated): \n"
64 " If the client does not support Asterisk \"html\" transport, \n"
65 " and there exists a step with priority n + 101, then execution will\n"
66 " continue at that step.\n"
67 " Otherwise, execution will continue at the next priority level.\n"
68 " SendURL only returns 0 if the URL was sent correctly  or if\n"
69 " the channel does not support HTML transport, and -1 otherwise.\n";
70
71
72 static int sendurl_exec(struct ast_channel *chan, void *data)
73 {
74         int res = 0;
75         struct ast_module_user *u;
76         char *tmp;
77         char *options;
78         int local_option_wait=0;
79         int local_option_jump = 0;
80         struct ast_frame *f;
81         char *stringp=NULL;
82         char *status = "FAILURE";
83         
84         if (ast_strlen_zero(data)) {
85                 ast_log(LOG_WARNING, "SendURL requires an argument (URL)\n");
86                 pbx_builtin_setvar_helper(chan, "SENDURLSTATUS", status);
87                 return -1;
88         }
89
90         u = ast_module_user_add(chan);
91
92         tmp = ast_strdupa(data);
93
94         stringp=tmp;
95         strsep(&stringp, "|");
96         options = strsep(&stringp, "|");
97         if (options && !strcasecmp(options, "wait"))
98                 local_option_wait = 1;
99         if (options && !strcasecmp(options, "j"))
100                 local_option_jump = 1;
101         
102         if (!ast_channel_supports_html(chan)) {
103                 /* Does not support transport */
104                 if (local_option_jump || ast_opt_priority_jumping)
105                          ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101);
106                 pbx_builtin_setvar_helper(chan, "SENDURLSTATUS", "UNSUPPORTED");
107                 ast_module_user_remove(u);
108                 return 0;
109         }
110         res = ast_channel_sendurl(chan, tmp);
111         if (res == -1) {
112                 pbx_builtin_setvar_helper(chan, "SENDURLSTATUS", "FAILURE");
113                 ast_module_user_remove(u);
114                 return res;
115         }
116         status = "SUCCESS";
117         if (local_option_wait) {
118                 for(;;) {
119                         /* Wait for an event */
120                         res = ast_waitfor(chan, -1);
121                         if (res < 0) 
122                                 break;
123                         f = ast_read(chan);
124                         if (!f) {
125                                 res = -1;
126                                 status = "FAILURE";
127                                 break;
128                         }
129                         if (f->frametype == AST_FRAME_HTML) {
130                                 switch(f->subclass) {
131                                 case AST_HTML_LDCOMPLETE:
132                                         res = 0;
133                                         ast_frfree(f);
134                                         status = "NOLOAD";
135                                         goto out;
136                                         break;
137                                 case AST_HTML_NOSUPPORT:
138                                         /* Does not support transport */
139                                         status ="UNSUPPORTED";
140                                         if (local_option_jump || ast_opt_priority_jumping)
141                                                 ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101);
142                                         res = 0;
143                                         ast_frfree(f);
144                                         goto out;
145                                         break;
146                                 default:
147                                         ast_log(LOG_WARNING, "Don't know what to do with HTML subclass %d\n", f->subclass);
148                                 };
149                         }
150                         ast_frfree(f);
151                 }
152         } 
153 out:    
154         pbx_builtin_setvar_helper(chan, "SENDURLSTATUS", status);
155         ast_module_user_remove(u);
156         return res;
157 }
158
159 static int unload_module(void)
160 {
161         int res;
162
163         res = ast_unregister_application(app);
164         
165         ast_module_user_hangup_all();
166
167         return res;     
168 }
169
170 static int load_module(void)
171 {
172         return ast_register_application(app, sendurl_exec, synopsis, descrip);
173 }
174
175 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Send URL Applications");