Mercurial > pidgin
annotate src/html.c @ 1605:21db0d659049
[gaim-migrate @ 1615]
fun stuff
committer: Tailor Script <tailor@pidgin.im>
author | Eric Warmenhoven <eric@warmenhoven.org> |
---|---|
date | Sat, 17 Mar 2001 00:21:36 +0000 |
parents | b5783215b245 |
children | 00aef397a1fe |
rev | line source |
---|---|
1 | 1 /* |
2 * gaim | |
3 * | |
4 * Copyright (C) 1998-1999, Mark Spencer <markster@marko.net> | |
5 * | |
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. | |
10 * | |
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. | |
15 * | |
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 | |
19 * | |
20 */ | |
21 | |
349
b402a23f35df
[gaim-migrate @ 359]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
278
diff
changeset
|
22 #ifdef HAVE_CONFIG_H |
b402a23f35df
[gaim-migrate @ 359]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
278
diff
changeset
|
23 #include "../config.h" |
b402a23f35df
[gaim-migrate @ 359]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
278
diff
changeset
|
24 #endif |
1 | 25 #include <string.h> |
26 #include <stdio.h> | |
27 #include <stdlib.h> | |
28 #include <sys/time.h> | |
29 #include <unistd.h> | |
30 #include <gtk/gtk.h> | |
31 #include <gdk/gdkprivate.h> | |
32 #include <gdk/gdkx.h> | |
33 #include "gaim.h" | |
34 #include <sys/types.h> | |
35 #include <sys/socket.h> | |
36 #include <netdb.h> | |
37 #include <netinet/in.h> | |
278
29e1669b006b
[gaim-migrate @ 288]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
268
diff
changeset
|
38 #include <fcntl.h> |
29e1669b006b
[gaim-migrate @ 288]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
268
diff
changeset
|
39 #include <errno.h> |
1092
a930439f29b1
[gaim-migrate @ 1102]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1088
diff
changeset
|
40 #include "proxy.h" |
1 | 41 |
1250
b5783215b245
[gaim-migrate @ 1260]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1092
diff
changeset
|
42 gchar *strip_html(gchar *text) |
1 | 43 { |
44 int i, j; | |
45 int visible = 1; | |
46 gchar *text2 = g_malloc(strlen(text) + 1); | |
1250
b5783215b245
[gaim-migrate @ 1260]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1092
diff
changeset
|
47 |
1 | 48 strcpy(text2, text); |
1250
b5783215b245
[gaim-migrate @ 1260]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1092
diff
changeset
|
49 for (i = 0, j = 0; text2[i]; i++) { |
b5783215b245
[gaim-migrate @ 1260]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1092
diff
changeset
|
50 if (text2[i] == '<') { |
1 | 51 visible = 0; |
52 continue; | |
1250
b5783215b245
[gaim-migrate @ 1260]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1092
diff
changeset
|
53 } else if (text2[i] == '>') { |
1 | 54 visible = 1; |
55 continue; | |
56 } | |
1250
b5783215b245
[gaim-migrate @ 1260]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1092
diff
changeset
|
57 if (visible) { |
1 | 58 text2[j++] = text2[i]; |
59 } | |
60 } | |
61 text2[j] = '\0'; | |
62 return text2; | |
63 } | |
64 | |
65 struct g_url parse_url(char *url) | |
66 { | |
67 struct g_url test; | |
68 char scan_info[255]; | |
69 char port[5]; | |
70 int f; | |
71 | |
72 if (strstr(url, "http://")) | |
1250
b5783215b245
[gaim-migrate @ 1260]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1092
diff
changeset
|
73 g_snprintf(scan_info, sizeof(scan_info), |
b5783215b245
[gaim-migrate @ 1260]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1092
diff
changeset
|
74 "http://%%[A-Za-z0-9.]:%%[0-9]/%%[A-Za-z0-9.~_-/&%%?]"); |
1 | 75 else |
1250
b5783215b245
[gaim-migrate @ 1260]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1092
diff
changeset
|
76 g_snprintf(scan_info, sizeof(scan_info), |
b5783215b245
[gaim-migrate @ 1260]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1092
diff
changeset
|
77 "%%[A-Za-z0-9.]:%%[0-9]/%%[A-Za-z0-9.~_-/&%%?]"); |
1 | 78 f = sscanf(url, scan_info, test.address, port, test.page); |
79 if (f == 1) { | |
80 if (strstr(url, "http://")) | |
1250
b5783215b245
[gaim-migrate @ 1260]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1092
diff
changeset
|
81 g_snprintf(scan_info, sizeof(scan_info), |
b5783215b245
[gaim-migrate @ 1260]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1092
diff
changeset
|
82 "http://%%[A-Za-z0-9.]/%%[A-Za-z0-9.~_-/&%%?]"); |
1 | 83 else |
1250
b5783215b245
[gaim-migrate @ 1260]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1092
diff
changeset
|
84 g_snprintf(scan_info, sizeof(scan_info), |
b5783215b245
[gaim-migrate @ 1260]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1092
diff
changeset
|
85 "%%[A-Za-z0-9.]/%%[A-Za-z0-9.~_-/&%%?]"); |
b5783215b245
[gaim-migrate @ 1260]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1092
diff
changeset
|
86 f = sscanf(url, scan_info, test.address, test.page); |
b5783215b245
[gaim-migrate @ 1260]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1092
diff
changeset
|
87 g_snprintf(port, sizeof(test.port), "80"); |
b5783215b245
[gaim-migrate @ 1260]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1092
diff
changeset
|
88 port[2] = 0; |
1 | 89 } |
90 if (f == 1) { | |
91 if (strstr(url, "http://")) | |
92 g_snprintf(scan_info, sizeof(scan_info), "http://%%[A-Za-z0-9.]"); | |
1250
b5783215b245
[gaim-migrate @ 1260]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1092
diff
changeset
|
93 else |
b5783215b245
[gaim-migrate @ 1260]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1092
diff
changeset
|
94 g_snprintf(scan_info, sizeof(scan_info), "%%[A-Za-z0-9.]"); |
1 | 95 f = sscanf(url, scan_info, test.address); |
96 g_snprintf(test.page, sizeof(test.page), "%c", '\0'); | |
97 } | |
98 | |
99 sscanf(port, "%d", &test.port); | |
100 return test; | |
101 } | |
102 | |
1087
56c7ceb986a8
[gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
691
diff
changeset
|
103 char *grab_url(struct aim_user *user, char *url) |
1 | 104 { |
105 struct g_url website; | |
106 char *webdata = NULL; | |
1250
b5783215b245
[gaim-migrate @ 1260]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1092
diff
changeset
|
107 int sock; |
b5783215b245
[gaim-migrate @ 1260]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1092
diff
changeset
|
108 int len; |
268
f8a29745247c
[gaim-migrate @ 278]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
253
diff
changeset
|
109 int read_rv; |
1 | 110 int datalen = 0; |
111 char buf[256]; | |
112 char data; | |
1250
b5783215b245
[gaim-migrate @ 1260]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1092
diff
changeset
|
113 int startsaving = 0; |
b5783215b245
[gaim-migrate @ 1260]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1092
diff
changeset
|
114 GtkWidget *pw = NULL, *pbar = NULL, *label; |
1 | 115 |
1250
b5783215b245
[gaim-migrate @ 1260]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1092
diff
changeset
|
116 website = parse_url(url); |
1 | 117 |
1087
56c7ceb986a8
[gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
691
diff
changeset
|
118 if (user) { |
56c7ceb986a8
[gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
691
diff
changeset
|
119 if ((sock = proxy_connect(website.address, website.port, user->proto_opt[2], |
1250
b5783215b245
[gaim-migrate @ 1260]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1092
diff
changeset
|
120 atoi(user->proto_opt[3]), atoi(user->proto_opt[4]))) < 0) |
1087
56c7ceb986a8
[gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
691
diff
changeset
|
121 return g_strdup(_("g003: Error opening connection.\n")); |
56c7ceb986a8
[gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
691
diff
changeset
|
122 } else { |
1092
a930439f29b1
[gaim-migrate @ 1102]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1088
diff
changeset
|
123 if ((sock = proxy_connect(website.address, website.port, NULL, 0, -1)) < 0) |
1087
56c7ceb986a8
[gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
691
diff
changeset
|
124 return g_strdup(_("g003: Error opening connection.\n")); |
56c7ceb986a8
[gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
691
diff
changeset
|
125 } |
1 | 126 |
691
104a2659b358
[gaim-migrate @ 701]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
350
diff
changeset
|
127 g_snprintf(buf, sizeof(buf), "GET /%s HTTP/1.0\r\n\r\n", website.page); |
1250
b5783215b245
[gaim-migrate @ 1260]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1092
diff
changeset
|
128 debug_printf("Request: %s\n", buf); |
268
f8a29745247c
[gaim-migrate @ 278]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
253
diff
changeset
|
129 write(sock, buf, strlen(buf)); |
278
29e1669b006b
[gaim-migrate @ 288]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
268
diff
changeset
|
130 fcntl(sock, F_SETFL, O_NONBLOCK); |
1 | 131 |
1250
b5783215b245
[gaim-migrate @ 1260]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1092
diff
changeset
|
132 webdata = NULL; |
b5783215b245
[gaim-migrate @ 1260]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1092
diff
changeset
|
133 len = 0; |
b5783215b245
[gaim-migrate @ 1260]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1092
diff
changeset
|
134 |
268
f8a29745247c
[gaim-migrate @ 278]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
253
diff
changeset
|
135 /* |
f8a29745247c
[gaim-migrate @ 278]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
253
diff
changeset
|
136 * avoid fgetc(), it causes problems on solaris |
1250
b5783215b245
[gaim-migrate @ 1260]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1092
diff
changeset
|
137 while ((data = fgetc(sockfile)) != EOF) { |
b5783215b245
[gaim-migrate @ 1260]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1092
diff
changeset
|
138 */ |
268
f8a29745247c
[gaim-migrate @ 278]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
253
diff
changeset
|
139 /* read_rv will be 0 on EOF and < 0 on error, so this should be fine */ |
278
29e1669b006b
[gaim-migrate @ 288]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
268
diff
changeset
|
140 while ((read_rv = read(sock, &data, 1)) > 0 || errno == EWOULDBLOCK) { |
29e1669b006b
[gaim-migrate @ 288]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
268
diff
changeset
|
141 if (errno == EWOULDBLOCK) { |
29e1669b006b
[gaim-migrate @ 288]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
268
diff
changeset
|
142 errno = 0; |
29e1669b006b
[gaim-migrate @ 288]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
268
diff
changeset
|
143 continue; |
29e1669b006b
[gaim-migrate @ 288]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
268
diff
changeset
|
144 } |
29e1669b006b
[gaim-migrate @ 288]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
268
diff
changeset
|
145 |
1 | 146 if (!data) |
147 continue; | |
1250
b5783215b245
[gaim-migrate @ 1260]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1092
diff
changeset
|
148 |
1 | 149 if (!startsaving && data == '<') { |
150 #ifdef HAVE_STRSTR | |
151 char *cs = strstr(webdata, "Content-Length"); | |
152 if (cs) { | |
153 char tmpbuf[1024]; | |
154 sscanf(cs, "Content-Length: %d", &datalen); | |
155 | |
1250
b5783215b245
[gaim-migrate @ 1260]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1092
diff
changeset
|
156 g_snprintf(tmpbuf, 1024, _("Getting %d bytes from %s"), datalen, url); |
b5783215b245
[gaim-migrate @ 1260]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1092
diff
changeset
|
157 pw = gtk_dialog_new(); |
1 | 158 |
159 label = gtk_label_new(tmpbuf); | |
160 gtk_widget_show(label); | |
161 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(pw)->vbox), | |
162 label, FALSE, FALSE, 5); | |
1250
b5783215b245
[gaim-migrate @ 1260]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1092
diff
changeset
|
163 |
1 | 164 pbar = gtk_progress_bar_new(); |
165 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(pw)->action_area), | |
166 pbar, FALSE, FALSE, 5); | |
1250
b5783215b245
[gaim-migrate @ 1260]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1092
diff
changeset
|
167 gtk_widget_show(pbar); |
b5783215b245
[gaim-migrate @ 1260]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1092
diff
changeset
|
168 |
b5783215b245
[gaim-migrate @ 1260]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1092
diff
changeset
|
169 gtk_window_set_title(GTK_WINDOW(pw), _("Getting Data")); |
1 | 170 |
1250
b5783215b245
[gaim-migrate @ 1260]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1092
diff
changeset
|
171 gtk_widget_realize(pw); |
b5783215b245
[gaim-migrate @ 1260]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1092
diff
changeset
|
172 aol_icon(pw->window); |
b5783215b245
[gaim-migrate @ 1260]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1092
diff
changeset
|
173 |
b5783215b245
[gaim-migrate @ 1260]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1092
diff
changeset
|
174 gtk_widget_show(pw); |
b5783215b245
[gaim-migrate @ 1260]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1092
diff
changeset
|
175 } else |
1 | 176 datalen = 0; |
177 #else | |
178 datalen = 0; | |
179 #endif | |
180 g_free(webdata); | |
181 webdata = NULL; | |
182 len = 0; | |
183 startsaving = 1; | |
184 } | |
185 | |
186 len++; | |
187 webdata = g_realloc(webdata, len); | |
188 webdata[len - 1] = data; | |
189 | |
190 if (pbar) | |
1250
b5783215b245
[gaim-migrate @ 1260]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1092
diff
changeset
|
191 gtk_progress_bar_update(GTK_PROGRESS_BAR(pbar), ((100 * len) / datalen) / 100.0); |
b5783215b245
[gaim-migrate @ 1260]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1092
diff
changeset
|
192 |
1 | 193 while (gtk_events_pending()) |
194 gtk_main_iteration(); | |
195 } | |
196 | |
1250
b5783215b245
[gaim-migrate @ 1260]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1092
diff
changeset
|
197 webdata = g_realloc(webdata, len + 1); |
b5783215b245
[gaim-migrate @ 1260]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1092
diff
changeset
|
198 webdata[len] = 0; |
1 | 199 |
200 | |
1250
b5783215b245
[gaim-migrate @ 1260]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1092
diff
changeset
|
201 debug_printf(_("Receieved: '%s'\n"), webdata); |
1 | 202 |
1250
b5783215b245
[gaim-migrate @ 1260]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1092
diff
changeset
|
203 if (pw) |
b5783215b245
[gaim-migrate @ 1260]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1092
diff
changeset
|
204 gtk_widget_destroy(pw); |
1 | 205 |
206 close(sock); | |
207 return webdata; | |
208 } |