4 * Copyright (C) 1998-1999, Mark Spencer <markster@marko.net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 #include <sys/socket.h>
34 #include <netinet/in.h>
37 #include <sys/types.h>
47 gchar
*strip_html(gchar
*text
)
51 gchar
*text2
= g_strdup(text
);
53 for (i
= 0, j
= 0; text2
[i
]; i
++) {
54 if (text2
[i
] == '<') {
57 if (text2
[k
] == '<') {
61 if (text2
[k
] == '>') {
67 } else if (text2
[i
] == '>' && !visible
) {
72 text2
[j
++] = text2
[i
];
79 struct g_url
*parse_url(char *url
)
81 struct g_url
*test
= g_new0(struct g_url
, 1);
86 if (strstr(url
, "http://"))
87 g_snprintf(scan_info
, sizeof(scan_info
),
88 "http://%%[A-Za-z0-9.]:%%[0-9]/%%[A-Za-z0-9.~_-/&%%?=+]");
90 g_snprintf(scan_info
, sizeof(scan_info
),
91 "%%[A-Za-z0-9.]:%%[0-9]/%%[A-Za-z0-9.~_-/&%%?=+^]");
92 f
= sscanf(url
, scan_info
, test
->address
, port
, test
->page
);
94 if (strstr(url
, "http://"))
95 g_snprintf(scan_info
, sizeof(scan_info
),
96 "http://%%[A-Za-z0-9.]/%%[A-Za-z0-9.~_-/&%%?=+^]");
98 g_snprintf(scan_info
, sizeof(scan_info
),
99 "%%[A-Za-z0-9.]/%%[A-Za-z0-9.~_-/&%%?=+^]");
100 f
= sscanf(url
, scan_info
, test
->address
, test
->page
);
101 g_snprintf(port
, sizeof(test
->port
), "80");
105 if (strstr(url
, "http://"))
106 g_snprintf(scan_info
, sizeof(scan_info
), "http://%%[A-Za-z0-9.]");
108 g_snprintf(scan_info
, sizeof(scan_info
), "%%[A-Za-z0-9.]");
109 f
= sscanf(url
, scan_info
, test
->address
);
110 g_snprintf(test
->page
, sizeof(test
->page
), "%c", '\0');
113 sscanf(port
, "%d", &test
->port
);
117 struct grab_url_data
{
118 void (* callback
)(gpointer
, char *);
120 struct g_url
*website
;
128 gboolean startsaving
;
133 static void grab_url_callback(gpointer dat
, gint sock
, GaimInputCondition cond
)
135 struct grab_url_data
*gunk
= dat
;
139 gunk
->callback(gunk
->data
, NULL
);
140 g_free(gunk
->website
);
146 if (!gunk
->sentreq
) {
149 g_snprintf(buf
, sizeof(buf
), "GET %s%s HTTP/1.0\r\n\r\n", gunk
->full
? "" : "/",
150 gunk
->full
? gunk
->url
: gunk
->website
->page
);
151 debug_printf("Request: %s\n", buf
);
153 write(sock
, buf
, strlen(buf
));
154 fcntl(sock
, F_SETFL
, O_NONBLOCK
);
155 gunk
->sentreq
= TRUE
;
156 gunk
->inpa
= gaim_input_add(sock
, GAIM_INPUT_READ
, grab_url_callback
, dat
);
160 if (read(sock
, &data
, 1) > 0 || errno
== EWOULDBLOCK
) {
161 if (errno
== EWOULDBLOCK
) {
165 if (!gunk
->startsaving
) {
170 gunk
->startsaving
= TRUE
;
172 gunk
->newline
= TRUE
;
175 gunk
->newline
= FALSE
;
178 gunk
->webdata
= g_realloc(gunk
->webdata
, gunk
->len
);
179 gunk
->webdata
[gunk
->len
- 1] = data
;
181 } else if (errno
!= ETIMEDOUT
) {
182 gunk
->webdata
= g_realloc(gunk
->webdata
, gunk
->len
+ 1);
183 gunk
->webdata
[gunk
->len
] = 0;
185 debug_printf(_("Received: '%s'\n"), gunk
->webdata
);
187 gaim_input_remove(gunk
->inpa
);
189 gunk
->callback(gunk
->data
, gunk
->webdata
);
191 g_free(gunk
->webdata
);
192 g_free(gunk
->website
);
196 gaim_input_remove(gunk
->inpa
);
198 gunk
->callback(gunk
->data
, NULL
);
200 g_free(gunk
->webdata
);
201 g_free(gunk
->website
);
207 void grab_url(char *url
, gboolean full
, void callback(gpointer
, char *), gpointer data
)
210 struct grab_url_data
*gunk
= g_new0(struct grab_url_data
, 1);
212 gunk
->callback
= callback
;
214 gunk
->url
= g_strdup(url
);
215 gunk
->website
= parse_url(url
);
218 if ((sock
= proxy_connect(gunk
->website
->address
, gunk
->website
->port
,
219 grab_url_callback
, gunk
)) < 0) {
220 g_free(gunk
->website
);
223 callback(data
, g_strdup(_("g003: Error opening connection.\n")));