Huge callerid rework (might break H.323, others)
[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-2004, Digium, Inc.
7  *
8  * Mark Spencer <markster@digium.com>
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
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         char *stringp=NULL;
55         if (!data || !strlen((char *)data)) {
56                 ast_log(LOG_WARNING, "SendURL requires an argument (URL)\n");
57                 return -1;
58         }
59         strncpy(tmp, (char *)data, sizeof(tmp)-1);
60         stringp=tmp;
61         strsep(&stringp, "|");
62         options = strsep(&stringp, "|");
63         if (options && !strcasecmp(options, "wait"))
64                 option_wait = 1;
65         LOCAL_USER_ADD(u);
66         if (!ast_channel_supports_html(chan)) {
67                 /* Does not support transport */
68                 if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->cid.cid_num))
69                         chan->priority += 100;
70                 LOCAL_USER_REMOVE(u);
71                 return 0;
72         }
73         res = ast_channel_sendurl(chan, tmp);
74         if (res > -1) {
75                 if (option_wait) {
76                         for(;;) {
77                                 /* Wait for an event */
78                                 res = ast_waitfor(chan, -1);
79                                 if (res < 0) 
80                                         break;
81                                 f = ast_read(chan);
82                                 if (!f) {
83                                         res = -1;
84                                         break;
85                                 }
86                                 if (f->frametype == AST_FRAME_HTML) {
87                                         switch(f->subclass) {
88                                         case AST_HTML_LDCOMPLETE:
89                                                 res = 0;
90                                                 ast_frfree(f);
91                                                 goto out;
92                                                 break;
93                                         case AST_HTML_NOSUPPORT:
94                                                 /* Does not support transport */
95                                                 if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->cid.cid_num))
96                                                         chan->priority += 100;
97                                                 res = 0;
98                                                 goto out;
99                                                 break;
100                                         default:
101                                                 ast_log(LOG_WARNING, "Don't know what to do with HTML subclass %d\n", f->subclass);
102                                         };
103                                 }
104                                 ast_frfree(f);
105                         }
106                 }
107         }
108 out:    
109         LOCAL_USER_REMOVE(u);
110         return res;
111 }
112
113 int unload_module(void)
114 {
115         STANDARD_HANGUP_LOCALUSERS;
116         return ast_unregister_application(app);
117 }
118
119 int load_module(void)
120 {
121         return ast_register_application(app, sendurl_exec, synopsis, descrip);
122 }
123
124 char *description(void)
125 {
126         return tdesc;
127 }
128
129 int usecount(void)
130 {
131         int res;
132         STANDARD_USECOUNT(res);
133         return res;
134 }
135
136 char *key()
137 {
138         return ASTERISK_GPL_KEY;
139 }