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