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
31 #include <sys/types.h>
32 #include <sys/socket.h>
34 #include <netinet/in.h>
45 gchar
*strip_html(gchar
*text
)
49 gchar
*text2
= g_strdup(text
);
51 for (i
= 0, j
= 0; text2
[i
]; i
++) {
52 if (text2
[i
] == '<') {
55 if (text2
[k
] == '<') {
59 if (text2
[k
] == '>') {
65 } else if (text2
[i
] == '>' && !visible
) {
70 text2
[j
++] = text2
[i
];
77 static struct g_url
*parse_url(char *url
)
79 struct g_url
*test
= g_new0(struct g_url
, 1);
84 if (strstr(url
, "http://"))
85 g_snprintf(scan_info
, sizeof(scan_info
),
86 "http://%%[A-Za-z0-9.]:%%[0-9]/%%[A-Za-z0-9.~_-/&%%?=+]");
88 g_snprintf(scan_info
, sizeof(scan_info
),
89 "%%[A-Za-z0-9.]:%%[0-9]/%%[A-Za-z0-9.~_-/&%%?=+^]");
90 f
= sscanf(url
, scan_info
, test
->address
, port
, test
->page
);
92 if (strstr(url
, "http://"))
93 g_snprintf(scan_info
, sizeof(scan_info
),
94 "http://%%[A-Za-z0-9.]/%%[A-Za-z0-9.~_-/&%%?=+^]");
96 g_snprintf(scan_info
, sizeof(scan_info
),
97 "%%[A-Za-z0-9.]/%%[A-Za-z0-9.~_-/&%%?=+^]");
98 f
= sscanf(url
, scan_info
, test
->address
, test
->page
);
99 g_snprintf(port
, sizeof(test
->port
), "80");
103 if (strstr(url
, "http://"))
104 g_snprintf(scan_info
, sizeof(scan_info
), "http://%%[A-Za-z0-9.]");
106 g_snprintf(scan_info
, sizeof(scan_info
), "%%[A-Za-z0-9.]");
107 f
= sscanf(url
, scan_info
, test
->address
);
108 g_snprintf(test
->page
, sizeof(test
->page
), "%c", '\0');
111 sscanf(port
, "%d", &test
->port
);
115 struct grab_url_data
{
116 void (* callback
)(gpointer
, char *);
118 struct g_url
*website
;
126 gboolean startsaving
;
131 static void grab_url_callback(gpointer dat
, gint sock
, GaimInputCondition cond
)
133 struct grab_url_data
*gunk
= dat
;
137 gunk
->callback(gunk
->data
, NULL
);
138 g_free(gunk
->website
);
144 if (!gunk
->sentreq
) {
146 g_snprintf(buf
, sizeof(buf
), "GET %s%s HTTP/1.0\r\n\r\n", gunk
->full
? "" : "/",
147 gunk
->full
? gunk
->url
: gunk
->website
->page
);
148 debug_printf("Request: %s\n", buf
);
149 write(sock
, buf
, strlen(buf
));
150 fcntl(sock
, F_SETFL
, O_NONBLOCK
);
151 gunk
->sentreq
= TRUE
;
152 gunk
->inpa
= gaim_input_add(sock
, GAIM_INPUT_READ
, grab_url_callback
, dat
);
156 if (read(sock
, &data
, 1) > 0 || errno
== EWOULDBLOCK
) {
157 if (errno
== EWOULDBLOCK
) {
162 if (!gunk
->startsaving
) {
167 gunk
->startsaving
= TRUE
;
169 gunk
->newline
= TRUE
;
172 gunk
->newline
= FALSE
;
175 gunk
->webdata
= g_realloc(gunk
->webdata
, gunk
->len
);
176 gunk
->webdata
[gunk
->len
- 1] = data
;
178 } else if (errno
!= ETIMEDOUT
) {
180 gunk
->webdata
= g_realloc(gunk
->webdata
, gunk
->len
+ 1);
181 gunk
->webdata
[gunk
->len
] = 0;
183 debug_printf(_("Receieved: '%s'\n"), gunk
->webdata
);
185 gaim_input_remove(gunk
->inpa
);
187 gunk
->callback(gunk
->data
, gunk
->webdata
);
189 g_free(gunk
->webdata
);
190 g_free(gunk
->website
);
194 gaim_input_remove(gunk
->inpa
);
196 gunk
->callback(gunk
->data
, NULL
);
198 g_free(gunk
->webdata
);
199 g_free(gunk
->website
);
205 void grab_url(char *url
, gboolean full
, void callback(gpointer
, char *), gpointer data
)
208 struct grab_url_data
*gunk
= g_new0(struct grab_url_data
, 1);
210 gunk
->callback
= callback
;
212 gunk
->url
= g_strdup(url
);
213 gunk
->website
= parse_url(url
);
216 if ((sock
= proxy_connect(gunk
->website
->address
, gunk
->website
->port
,
217 grab_url_callback
, gunk
)) < 0) {
218 g_free(gunk
->website
);
221 callback(data
, g_strdup(_("g003: Error opening connection.\n")));