Version 0.3.0 from FTP
[asterisk/asterisk.git] / apps / app_url.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * App to transmit a URL
5  * 
6  * Copyright (C) 1999, Mark Spencer
7  *
8  * Mark Spencer <markster@linux-support.net>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  */
13  
14 #include <asterisk/lock.h>
15 #include <asterisk/file.h>
16 #include <asterisk/logger.h>
17 #include <asterisk/channel.h>
18 #include <asterisk/pbx.h>
19 #include <asterisk/module.h>
20 #include <asterisk/translate.h>
21 #include <asterisk/image.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <pthread.h>
25
26 static char *tdesc = "Send URL Applications";
27
28 static char *app = "SendURL";
29
30 static char *synopsis = "Send a URL";
31
32 static char *descrip = 
33 "  SendURL(URL[|option]): Requests client go to URL.  If the client\n"
34 "does not support html transport, and  there  exists  a  step  with\n"
35 "priority  n + 101,  then  execution  will  continue  at that step.\n"
36 "Otherwise, execution will continue at  the  next  priority  level.\n"
37 "SendURL only returns 0  if  the  URL  was  sent  correctly  or  if\n"
38 "the channel  does  not  support HTML transport,  and -1 otherwise.\n"
39 "If the option 'wait' is  specified,  execution  will  wait  for an\n"
40 "acknowledgement that  the  URL  has  been loaded before continuing\n"
41 "and will return -1 if the peer is unable to load the URL\n";
42
43 STANDARD_LOCAL_USER;
44
45 LOCAL_USER_DECL;
46
47 static int sendurl_exec(struct ast_channel *chan, void *data)
48 {
49         int res = 0;
50         struct localuser *u;
51         char tmp[256];
52         char *options;
53         int option_wait=0;
54         struct ast_frame *f;
55         char *stringp=NULL;
56         if (!data || !strlen((char *)data)) {
57                 ast_log(LOG_WARNING, "SendURL requires an argument (URL)\n");
58                 return -1;
59         }
60         strncpy(tmp, (char *)data, sizeof(tmp)-1);
61         stringp=tmp;
62         strsep(&stringp, "|");
63         options = strsep(&stringp, "|");
64         if (options && !strcasecmp(options, "wait"))
65                 option_wait = 1;
66         LOCAL_USER_ADD(u);
67         if (!ast_channel_supports_html(chan)) {
68                 /* Does not support transport */
69                 if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->callerid))
70                         chan->priority += 100;
71                 LOCAL_USER_REMOVE(u);
72                 return 0;
73         }
74         res = ast_channel_sendurl(chan, tmp);
75         if (res > -1) {
76                 if (option_wait) {
77                         for(;;) {
78                                 /* Wait for an event */
79                                 res = ast_waitfor(chan, -1);
80                                 if (res < 0) 
81                                         break;
82                                 f = ast_read(chan);
83                                 if (!f) {
84                                         res = -1;
85                                         break;
86                                 }
87                                 if (f->frametype == AST_FRAME_HTML) {
88                                         switch(f->subclass) {
89                                         case AST_HTML_LDCOMPLETE:
90                                                 res = 0;
91                                                 ast_frfree(f);
92                                                 goto out;
93                                                 break;
94                                         case AST_HTML_NOSUPPORT:
95                                                 /* Does not support transport */
96                                                 if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->callerid))
97                                                         chan->priority += 100;
98                                                 res = 0;
99                                                 goto out;
100                                                 break;
101                                         default:
102                                                 ast_log(LOG_WARNING, "Don't know what to do with HTML subclass %d\n", f->subclass);
103                                         };
104                                 }
105                                 ast_frfree(f);
106                         }
107                 }
108         }
109 out:    
110         LOCAL_USER_REMOVE(u);
111         return res;
112 }
113
114 int unload_module(void)
115 {
116         STANDARD_HANGUP_LOCALUSERS;
117         return ast_unregister_application(app);
118 }
119
120 int load_module(void)
121 {
122         return ast_register_application(app, sendurl_exec, synopsis, descrip);
123 }
124
125 char *description(void)
126 {
127         return tdesc;
128 }
129
130 int usecount(void)
131 {
132         int res;
133         STANDARD_USECOUNT(res);
134         return res;
135 }
136
137 char *key()
138 {
139         return ASTERISK_GPL_KEY;
140 }