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