Mercurial > pidgin.yaz
annotate src/html.c @ 6124:16fd7811effd
[gaim-migrate @ 6598]
Remove the f2-toggles-timestamps patch because it makes highlighting
act ungood when timestamps are off. Another day, perhaps.
committer: Tailor Script <tailor@pidgin.im>
author | Mark Doliner <mark@kingant.net> |
---|---|
date | Mon, 14 Jul 2003 23:05:21 +0000 |
parents | 11bedb793a44 |
children | 3de23c9ca1e4 |
rev | line source |
---|---|
1 | 1 /* |
2 * gaim | |
3 * | |
4 * Copyright (C) 1998-1999, Mark Spencer <markster@marko.net> | |
5176 | 5 * 2003, Nathan Walp <faceprint@faceprint.com> |
1 | 6 * |
7 * This program is free software; you can redistribute it and/or modify | |
8 * it under the terms of the GNU General Public License as published by | |
9 * the Free Software Foundation; either version 2 of the License, or | |
10 * (at your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, | |
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 * GNU General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU General Public License | |
18 * along with this program; if not, write to the Free Software | |
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
20 * | |
21 */ | |
5872
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5681
diff
changeset
|
22 #include "internal.h" |
3630 | 23 |
5872
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5681
diff
changeset
|
24 #include "debug.h" |
6115
11bedb793a44
[gaim-migrate @ 6578]
Christian Hammond <chipx86@chipx86.com>
parents:
5940
diff
changeset
|
25 #include "html.h" |
1092
a930439f29b1
[gaim-migrate @ 1102]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1088
diff
changeset
|
26 #include "proxy.h" |
1 | 27 |
5872
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5681
diff
changeset
|
28 #include "gaim.h" |
3717
988485669631
[gaim-migrate @ 3850]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
3630
diff
changeset
|
29 |
4359
5fb47ec9bfe4
[gaim-migrate @ 4625]
Christian Hammond <chipx86@chipx86.com>
parents:
4335
diff
changeset
|
30 gchar *strip_html(const gchar *text) |
1 | 31 { |
1883
060161a5d5f8
[gaim-migrate @ 1893]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1881
diff
changeset
|
32 int i, j, k; |
1 | 33 int visible = 1; |
1883
060161a5d5f8
[gaim-migrate @ 1893]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1881
diff
changeset
|
34 gchar *text2 = g_strdup(text); |
1250
b5783215b245
[gaim-migrate @ 1260]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1092
diff
changeset
|
35 |
4757 | 36 if(!text) |
37 return NULL; | |
4503 | 38 |
1250
b5783215b245
[gaim-migrate @ 1260]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1092
diff
changeset
|
39 for (i = 0, j = 0; text2[i]; i++) { |
b5783215b245
[gaim-migrate @ 1260]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1092
diff
changeset
|
40 if (text2[i] == '<') { |
1883
060161a5d5f8
[gaim-migrate @ 1893]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1881
diff
changeset
|
41 k = i + 1; |
4777 | 42 if(g_ascii_isspace(text2[k])) { |
43 visible = 1; | |
44 } else { | |
45 while (text2[k]) { | |
46 if (text2[k] == '<') { | |
47 visible = 1; | |
48 break; | |
49 } | |
50 if (text2[k] == '>') { | |
51 visible = 0; | |
52 break; | |
53 } | |
54 k++; | |
1883
060161a5d5f8
[gaim-migrate @ 1893]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1881
diff
changeset
|
55 } |
060161a5d5f8
[gaim-migrate @ 1893]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1881
diff
changeset
|
56 } |
060161a5d5f8
[gaim-migrate @ 1893]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1881
diff
changeset
|
57 } else if (text2[i] == '>' && !visible) { |
1 | 58 visible = 1; |
59 continue; | |
60 } | |
4473 | 61 if (text2[i] == '&' && strncasecmp(text2+i,""",6) == 0) { |
62 text2[j++] = '\"'; | |
63 i = i+5; | |
64 continue; | |
65 } | |
1250
b5783215b245
[gaim-migrate @ 1260]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1092
diff
changeset
|
66 if (visible) { |
1 | 67 text2[j++] = text2[i]; |
68 } | |
69 } | |
70 text2[j] = '\0'; | |
71 return text2; | |
72 } | |
73 | |
3630 | 74 struct g_url *parse_url(char *url) |
1 | 75 { |
5512 | 76 struct g_url *test = g_new0(struct g_url, 1); |
1 | 77 char scan_info[255]; |
78 char port[5]; | |
79 int f; | |
5501
36d2c875a822
[gaim-migrate @ 5900]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
5211
diff
changeset
|
80 char* turl; |
36d2c875a822
[gaim-migrate @ 5900]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
5211
diff
changeset
|
81 /* hyphen at end includes it in control set */ |
36d2c875a822
[gaim-migrate @ 5900]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
5211
diff
changeset
|
82 char addr_ctrl[] = "A-Za-z0-9.-"; |
36d2c875a822
[gaim-migrate @ 5900]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
5211
diff
changeset
|
83 char port_ctrl[] = "0-9"; |
36d2c875a822
[gaim-migrate @ 5900]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
5211
diff
changeset
|
84 char page_ctrl[] = "A-Za-z0-9.~_/&%%?=+^-"; |
1 | 85 |
5501
36d2c875a822
[gaim-migrate @ 5900]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
5211
diff
changeset
|
86 if((turl=strstr(url, "http://")) || (turl=strstr(url, "HTTP://"))) |
36d2c875a822
[gaim-migrate @ 5900]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
5211
diff
changeset
|
87 url=turl+=7; |
36d2c875a822
[gaim-migrate @ 5900]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
5211
diff
changeset
|
88 |
36d2c875a822
[gaim-migrate @ 5900]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
5211
diff
changeset
|
89 snprintf(scan_info, sizeof(scan_info), |
36d2c875a822
[gaim-migrate @ 5900]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
5211
diff
changeset
|
90 "%%[%s]:%%[%s]/%%[%s]", |
36d2c875a822
[gaim-migrate @ 5900]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
5211
diff
changeset
|
91 addr_ctrl, port_ctrl, page_ctrl); |
36d2c875a822
[gaim-migrate @ 5900]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
5211
diff
changeset
|
92 |
2541
8229710b343b
[gaim-migrate @ 2554]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2417
diff
changeset
|
93 f = sscanf(url, scan_info, test->address, port, test->page); |
1 | 94 if (f == 1) { |
5501
36d2c875a822
[gaim-migrate @ 5900]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
5211
diff
changeset
|
95 snprintf(scan_info, sizeof(scan_info), |
36d2c875a822
[gaim-migrate @ 5900]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
5211
diff
changeset
|
96 "%%[%s]/%%[%s]", |
36d2c875a822
[gaim-migrate @ 5900]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
5211
diff
changeset
|
97 addr_ctrl, page_ctrl); |
2541
8229710b343b
[gaim-migrate @ 2554]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2417
diff
changeset
|
98 f = sscanf(url, scan_info, test->address, test->page); |
5501
36d2c875a822
[gaim-migrate @ 5900]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
5211
diff
changeset
|
99 snprintf(port, sizeof(port), "80"); |
1 | 100 } |
5501
36d2c875a822
[gaim-migrate @ 5900]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
5211
diff
changeset
|
101 if (f == 1) |
5512 | 102 test->page[0] = '\0'; |
1 | 103 |
2541
8229710b343b
[gaim-migrate @ 2554]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2417
diff
changeset
|
104 sscanf(port, "%d", &test->port); |
1 | 105 return test; |
106 } | |
107 | |
1840
00aef397a1fe
[gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
108 struct grab_url_data { |
4322 | 109 void (* callback)(gpointer, char *, unsigned long); |
1840
00aef397a1fe
[gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
110 gpointer data; |
2541
8229710b343b
[gaim-migrate @ 2554]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2417
diff
changeset
|
111 struct g_url *website; |
1840
00aef397a1fe
[gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
112 char *url; |
2584
34812d648f72
[gaim-migrate @ 2597]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2541
diff
changeset
|
113 gboolean full; |
2369
117e9f0950b6
[gaim-migrate @ 2382]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2090
diff
changeset
|
114 |
117e9f0950b6
[gaim-migrate @ 2382]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2090
diff
changeset
|
115 int inpa; |
117e9f0950b6
[gaim-migrate @ 2382]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2090
diff
changeset
|
116 |
117e9f0950b6
[gaim-migrate @ 2382]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2090
diff
changeset
|
117 gboolean sentreq; |
2584
34812d648f72
[gaim-migrate @ 2597]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2541
diff
changeset
|
118 gboolean newline; |
34812d648f72
[gaim-migrate @ 2597]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2541
diff
changeset
|
119 gboolean startsaving; |
2369
117e9f0950b6
[gaim-migrate @ 2382]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2090
diff
changeset
|
120 char *webdata; |
4322 | 121 unsigned long len; |
4331
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
122 unsigned long data_len; |
1840
00aef397a1fe
[gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
123 }; |
00aef397a1fe
[gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
124 |
4331
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
125 static gboolean |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
126 parse_redirect(const char *data, size_t data_len, gint sock, |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
127 struct grab_url_data *gunk) |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
128 { |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
129 gchar *s; |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
130 |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
131 if ((s = g_strstr_len(data, data_len, "Location: ")) != NULL) { |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
132 gchar *new_url, *end; |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
133 int len; |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
134 |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
135 s += strlen("Location: "); |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
136 end = strchr(s, '\r'); |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
137 |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
138 /* Just in case :) */ |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
139 if (end == NULL) |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
140 end = strchr(s, '\n'); |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
141 |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
142 len = end - s; |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
143 |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
144 new_url = g_malloc(len + 1); |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
145 strncpy(new_url, s, len); |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
146 new_url[len] = '\0'; |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
147 |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
148 /* Close the existing stuff. */ |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
149 gaim_input_remove(gunk->inpa); |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
150 close(sock); |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
151 |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
152 /* Try again, with this new location. */ |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
153 grab_url(new_url, gunk->full, gunk->callback, |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
154 gunk->data); |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
155 |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
156 /* Free up. */ |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
157 g_free(new_url); |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
158 g_free(gunk->webdata); |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
159 g_free(gunk->website); |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
160 g_free(gunk->url); |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
161 g_free(gunk); |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
162 |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
163 return TRUE; |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
164 } |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
165 |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
166 return FALSE; |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
167 } |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
168 |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
169 static size_t |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
170 parse_content_len(const char *data, size_t data_len) |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
171 { |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
172 size_t content_len = 0; |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
173 |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
174 sscanf(data, "Content-Length: %d", &content_len); |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
175 |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
176 return content_len; |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
177 } |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
178 |
2369
117e9f0950b6
[gaim-migrate @ 2382]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2090
diff
changeset
|
179 static void grab_url_callback(gpointer dat, gint sock, GaimInputCondition cond) |
1840
00aef397a1fe
[gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
180 { |
00aef397a1fe
[gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
181 struct grab_url_data *gunk = dat; |
1 | 182 char data; |
183 | |
1840
00aef397a1fe
[gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
184 if (sock == -1) { |
4322 | 185 gunk->callback(gunk->data, NULL, 0); |
2541
8229710b343b
[gaim-migrate @ 2554]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2417
diff
changeset
|
186 g_free(gunk->website); |
1840
00aef397a1fe
[gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
187 g_free(gunk->url); |
00aef397a1fe
[gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
188 g_free(gunk); |
00aef397a1fe
[gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
189 return; |
1087
56c7ceb986a8
[gaim-migrate @ 1097]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
691
diff
changeset
|
190 } |
1 | 191 |
2369
117e9f0950b6
[gaim-migrate @ 2382]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2090
diff
changeset
|
192 if (!gunk->sentreq) { |
117e9f0950b6
[gaim-migrate @ 2382]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2090
diff
changeset
|
193 char buf[256]; |
3717
988485669631
[gaim-migrate @ 3850]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
3630
diff
changeset
|
194 |
2584
34812d648f72
[gaim-migrate @ 2597]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2541
diff
changeset
|
195 g_snprintf(buf, sizeof(buf), "GET %s%s HTTP/1.0\r\n\r\n", gunk->full ? "" : "/", |
34812d648f72
[gaim-migrate @ 2597]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2541
diff
changeset
|
196 gunk->full ? gunk->url : gunk->website->page); |
5211
0241d6b6702d
[gaim-migrate @ 5581]
Christian Hammond <chipx86@chipx86.com>
parents:
5176
diff
changeset
|
197 |
0241d6b6702d
[gaim-migrate @ 5581]
Christian Hammond <chipx86@chipx86.com>
parents:
5176
diff
changeset
|
198 gaim_debug(GAIM_DEBUG_MISC, "grab_url_callback", |
0241d6b6702d
[gaim-migrate @ 5581]
Christian Hammond <chipx86@chipx86.com>
parents:
5176
diff
changeset
|
199 "Request: %s\n", buf); |
3717
988485669631
[gaim-migrate @ 3850]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
3630
diff
changeset
|
200 |
2369
117e9f0950b6
[gaim-migrate @ 2382]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2090
diff
changeset
|
201 write(sock, buf, strlen(buf)); |
117e9f0950b6
[gaim-migrate @ 2382]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2090
diff
changeset
|
202 fcntl(sock, F_SETFL, O_NONBLOCK); |
117e9f0950b6
[gaim-migrate @ 2382]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2090
diff
changeset
|
203 gunk->sentreq = TRUE; |
117e9f0950b6
[gaim-migrate @ 2382]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2090
diff
changeset
|
204 gunk->inpa = gaim_input_add(sock, GAIM_INPUT_READ, grab_url_callback, dat); |
4331
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
205 gunk->data_len = 4096; |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
206 gunk->webdata = g_malloc(gunk->data_len); |
2369
117e9f0950b6
[gaim-migrate @ 2382]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2090
diff
changeset
|
207 return; |
117e9f0950b6
[gaim-migrate @ 2382]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2090
diff
changeset
|
208 } |
1 | 209 |
2369
117e9f0950b6
[gaim-migrate @ 2382]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2090
diff
changeset
|
210 if (read(sock, &data, 1) > 0 || errno == EWOULDBLOCK) { |
278
29e1669b006b
[gaim-migrate @ 288]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
268
diff
changeset
|
211 if (errno == EWOULDBLOCK) { |
29e1669b006b
[gaim-migrate @ 288]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
268
diff
changeset
|
212 errno = 0; |
2369
117e9f0950b6
[gaim-migrate @ 2382]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2090
diff
changeset
|
213 return; |
117e9f0950b6
[gaim-migrate @ 2382]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2090
diff
changeset
|
214 } |
4331
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
215 |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
216 gunk->len++; |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
217 |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
218 if (gunk->len == gunk->data_len + 1) { |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
219 gunk->data_len += (gunk->data_len) / 2; |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
220 |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
221 gunk->webdata = g_realloc(gunk->webdata, gunk->data_len); |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
222 } |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
223 |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
224 gunk->webdata[gunk->len - 1] = data; |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
225 |
2584
34812d648f72
[gaim-migrate @ 2597]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2541
diff
changeset
|
226 if (!gunk->startsaving) { |
34812d648f72
[gaim-migrate @ 2597]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2541
diff
changeset
|
227 if (data == '\r') |
34812d648f72
[gaim-migrate @ 2597]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2541
diff
changeset
|
228 return; |
34812d648f72
[gaim-migrate @ 2597]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2541
diff
changeset
|
229 if (data == '\n') { |
4331
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
230 if (gunk->newline) { |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
231 size_t content_len; |
2584
34812d648f72
[gaim-migrate @ 2597]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2541
diff
changeset
|
232 gunk->startsaving = TRUE; |
4331
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
233 |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
234 /* See if we can find a redirect. */ |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
235 if (parse_redirect(gunk->webdata, gunk->len, sock, gunk)) |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
236 return; |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
237 |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
238 /* No redirect. See if we can find a content length. */ |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
239 content_len = parse_content_len(gunk->webdata, gunk->len); |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
240 |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
241 if (content_len == 0) { |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
242 /* We'll stick with an initial 8192 */ |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
243 content_len = 8192; |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
244 } |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
245 |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
246 /* Out with the old... */ |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
247 gunk->len = 0; |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
248 g_free(gunk->webdata); |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
249 gunk->webdata = NULL; |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
250 |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
251 /* In with the new. */ |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
252 gunk->data_len = content_len; |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
253 gunk->webdata = g_malloc(gunk->data_len); |
bbd7b12986a8
[gaim-migrate @ 4595]
Christian Hammond <chipx86@chipx86.com>
parents:
4322
diff
changeset
|
254 } |
2584
34812d648f72
[gaim-migrate @ 2597]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2541
diff
changeset
|
255 else |
34812d648f72
[gaim-migrate @ 2597]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2541
diff
changeset
|
256 gunk->newline = TRUE; |
34812d648f72
[gaim-migrate @ 2597]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2541
diff
changeset
|
257 return; |
34812d648f72
[gaim-migrate @ 2597]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2541
diff
changeset
|
258 } |
34812d648f72
[gaim-migrate @ 2597]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2541
diff
changeset
|
259 gunk->newline = FALSE; |
278
29e1669b006b
[gaim-migrate @ 288]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
268
diff
changeset
|
260 } |
2369
117e9f0950b6
[gaim-migrate @ 2382]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2090
diff
changeset
|
261 } else if (errno != ETIMEDOUT) { |
117e9f0950b6
[gaim-migrate @ 2382]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2090
diff
changeset
|
262 gunk->webdata = g_realloc(gunk->webdata, gunk->len + 1); |
117e9f0950b6
[gaim-migrate @ 2382]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2090
diff
changeset
|
263 gunk->webdata[gunk->len] = 0; |
1250
b5783215b245
[gaim-migrate @ 1260]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1092
diff
changeset
|
264 |
5211
0241d6b6702d
[gaim-migrate @ 5581]
Christian Hammond <chipx86@chipx86.com>
parents:
5176
diff
changeset
|
265 gaim_debug(GAIM_DEBUG_MISC, "grab_url_callback", |
0241d6b6702d
[gaim-migrate @ 5581]
Christian Hammond <chipx86@chipx86.com>
parents:
5176
diff
changeset
|
266 "Received: '%s'\n", gunk->webdata); |
1250
b5783215b245
[gaim-migrate @ 1260]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1092
diff
changeset
|
267 |
2369
117e9f0950b6
[gaim-migrate @ 2382]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2090
diff
changeset
|
268 gaim_input_remove(gunk->inpa); |
117e9f0950b6
[gaim-migrate @ 2382]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2090
diff
changeset
|
269 close(sock); |
4322 | 270 gunk->callback(gunk->data, gunk->webdata, gunk->len); |
2369
117e9f0950b6
[gaim-migrate @ 2382]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2090
diff
changeset
|
271 if (gunk->webdata) |
117e9f0950b6
[gaim-migrate @ 2382]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2090
diff
changeset
|
272 g_free(gunk->webdata); |
2541
8229710b343b
[gaim-migrate @ 2554]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2417
diff
changeset
|
273 g_free(gunk->website); |
2369
117e9f0950b6
[gaim-migrate @ 2382]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2090
diff
changeset
|
274 g_free(gunk->url); |
117e9f0950b6
[gaim-migrate @ 2382]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2090
diff
changeset
|
275 g_free(gunk); |
117e9f0950b6
[gaim-migrate @ 2382]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2090
diff
changeset
|
276 } else { |
117e9f0950b6
[gaim-migrate @ 2382]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2090
diff
changeset
|
277 gaim_input_remove(gunk->inpa); |
117e9f0950b6
[gaim-migrate @ 2382]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2090
diff
changeset
|
278 close(sock); |
4322 | 279 gunk->callback(gunk->data, NULL, 0); |
2369
117e9f0950b6
[gaim-migrate @ 2382]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2090
diff
changeset
|
280 if (gunk->webdata) |
117e9f0950b6
[gaim-migrate @ 2382]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2090
diff
changeset
|
281 g_free(gunk->webdata); |
2541
8229710b343b
[gaim-migrate @ 2554]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2417
diff
changeset
|
282 g_free(gunk->website); |
2369
117e9f0950b6
[gaim-migrate @ 2382]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2090
diff
changeset
|
283 g_free(gunk->url); |
117e9f0950b6
[gaim-migrate @ 2382]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2090
diff
changeset
|
284 g_free(gunk); |
1 | 285 } |
286 } | |
1840
00aef397a1fe
[gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
287 |
4322 | 288 void grab_url(char *url, gboolean full, void callback(gpointer, char *, unsigned long), gpointer data) |
1840
00aef397a1fe
[gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
289 { |
00aef397a1fe
[gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
290 int sock; |
00aef397a1fe
[gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
291 struct grab_url_data *gunk = g_new0(struct grab_url_data, 1); |
00aef397a1fe
[gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
292 |
00aef397a1fe
[gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
293 gunk->callback = callback; |
00aef397a1fe
[gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
294 gunk->data = data; |
00aef397a1fe
[gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
295 gunk->url = g_strdup(url); |
00aef397a1fe
[gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
296 gunk->website = parse_url(url); |
2584
34812d648f72
[gaim-migrate @ 2597]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2541
diff
changeset
|
297 gunk->full = full; |
1840
00aef397a1fe
[gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
298 |
5681
46d7ad0dfa26
[gaim-migrate @ 6100]
Christian Hammond <chipx86@chipx86.com>
parents:
5512
diff
changeset
|
299 if ((sock = gaim_proxy_connect(NULL, gunk->website->address, |
46d7ad0dfa26
[gaim-migrate @ 6100]
Christian Hammond <chipx86@chipx86.com>
parents:
5512
diff
changeset
|
300 gunk->website->port, grab_url_callback, |
46d7ad0dfa26
[gaim-migrate @ 6100]
Christian Hammond <chipx86@chipx86.com>
parents:
5512
diff
changeset
|
301 gunk)) < 0) { |
2541
8229710b343b
[gaim-migrate @ 2554]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2417
diff
changeset
|
302 g_free(gunk->website); |
1881
a02584b98823
[gaim-migrate @ 1891]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1843
diff
changeset
|
303 g_free(gunk->url); |
a02584b98823
[gaim-migrate @ 1891]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1843
diff
changeset
|
304 g_free(gunk); |
4322 | 305 callback(data, g_strdup(_("g003: Error opening connection.\n")), 0); |
1840
00aef397a1fe
[gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
306 } |
00aef397a1fe
[gaim-migrate @ 1850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
307 } |
5093 | 308 |
5104 | 309 struct gaim_parse_tag { |
310 char *src_tag; | |
311 char *dest_tag; | |
312 }; | |
313 | |
5093 | 314 #define ALLOW_TAG_ALT(x, y) if(!g_ascii_strncasecmp(c, "<" x " ", strlen("<" x " "))) { \ |
5176 | 315 const char *o = c + strlen("<" x); \ |
5141 | 316 const char *p = NULL, *q = NULL, *r = NULL; \ |
5176 | 317 GString *innards = g_string_new(""); \ |
318 while(o && *o) { \ | |
5141 | 319 if(!q && (*o == '\"' || *o == '\'') ) { \ |
320 q = o; \ | |
321 } else if(q) { \ | |
322 if(*o == *q) { \ | |
5176 | 323 char *unescaped = g_strndup(q+1, o-q-1); \ |
324 char *escaped = g_markup_escape_text(unescaped, -1); \ | |
325 g_string_append_printf(innards, "%c%s%c", *q, escaped, *q); \ | |
5940 | 326 g_free(unescaped); \ |
327 g_free(escaped); \ | |
5141 | 328 q = NULL; \ |
329 } else if(*c == '\\') { \ | |
330 o++; \ | |
331 } \ | |
332 } else if(*o == '<') { \ | |
333 r = o; \ | |
334 } else if(*o == '>') { \ | |
335 p = o; \ | |
336 break; \ | |
5176 | 337 } else { \ |
338 innards = g_string_append_c(innards, *o); \ | |
5141 | 339 } \ |
340 o++; \ | |
341 } \ | |
342 if(p && !r) { \ | |
5104 | 343 if(*(p-1) != '/') { \ |
344 struct gaim_parse_tag *pt = g_new0(struct gaim_parse_tag, 1); \ | |
345 pt->src_tag = x; \ | |
346 pt->dest_tag = y; \ | |
347 tags = g_list_prepend(tags, pt); \ | |
348 } \ | |
5093 | 349 xhtml = g_string_append(xhtml, "<" y); \ |
350 c += strlen("<" x ); \ | |
5176 | 351 xhtml = g_string_append(xhtml, innards->str); \ |
352 xhtml = g_string_append_c(xhtml, '>'); \ | |
5093 | 353 c = p + 1; \ |
354 } else { \ | |
355 xhtml = g_string_append(xhtml, "<"); \ | |
5110 | 356 plain = g_string_append_c(plain, '<'); \ |
5176 | 357 c++; \ |
5093 | 358 } \ |
5176 | 359 g_string_free(innards, TRUE); \ |
5093 | 360 continue; \ |
361 } \ | |
362 if(!g_ascii_strncasecmp(c, "<" x, strlen("<" x)) && \ | |
363 (*(c+strlen("<" x)) == '>' || \ | |
364 !g_ascii_strncasecmp(c+strlen("<" x), "/>", 2))) { \ | |
365 xhtml = g_string_append(xhtml, "<" y); \ | |
366 c += strlen("<" x); \ | |
5104 | 367 if(*c != '/') { \ |
368 struct gaim_parse_tag *pt = g_new0(struct gaim_parse_tag, 1); \ | |
369 pt->src_tag = x; \ | |
370 pt->dest_tag = y; \ | |
371 tags = g_list_prepend(tags, pt); \ | |
5110 | 372 xhtml = g_string_append_c(xhtml, '>'); \ |
373 } else { \ | |
374 xhtml = g_string_append(xhtml, "/>");\ | |
5104 | 375 } \ |
5110 | 376 c = strchr(c, '>') + 1; \ |
5093 | 377 continue; \ |
378 } | |
379 #define ALLOW_TAG(x) ALLOW_TAG_ALT(x, x) | |
380 | |
5110 | 381 void html_to_xhtml(const char *html, char **xhtml_out, char **plain_out) { |
5093 | 382 GString *xhtml = g_string_new(""); |
5110 | 383 GString *plain = g_string_new(""); |
5093 | 384 GList *tags = NULL, *tag; |
5141 | 385 const char *c = html; |
5176 | 386 |
387 while(c && *c) { | |
5141 | 388 if(*c == '<') { |
5093 | 389 if(*(c+1) == '/') { /* closing tag */ |
390 tag = tags; | |
391 while(tag) { | |
5104 | 392 struct gaim_parse_tag *pt = tag->data; |
393 if(!g_ascii_strncasecmp((c+2), pt->src_tag, strlen(pt->src_tag)) && *(c+strlen(pt->src_tag)+2) == '>') { | |
394 c += strlen(pt->src_tag) + 3; | |
5093 | 395 break; |
396 } | |
397 tag = tag->next; | |
398 } | |
399 if(tag) { | |
400 while(tags) { | |
5104 | 401 struct gaim_parse_tag *pt = tags->data; |
402 g_string_append_printf(xhtml, "</%s>", pt->dest_tag); | |
5093 | 403 if(tags == tag) |
404 break; | |
5104 | 405 tags = g_list_remove(tags, pt); |
406 g_free(pt); | |
5093 | 407 } |
5104 | 408 g_free(tag->data); |
5093 | 409 tags = g_list_remove(tags, tag->data); |
410 } else { | |
411 /* we tried to close a tag we never opened! escape it | |
412 * and move on */ | |
413 xhtml = g_string_append(xhtml, "<"); | |
5110 | 414 plain = g_string_append_c(plain, '<'); |
5093 | 415 c++; |
416 } | |
417 } else { /* opening tag */ | |
418 ALLOW_TAG("a"); | |
5101 | 419 ALLOW_TAG_ALT("b", "strong"); |
5093 | 420 ALLOW_TAG("blockquote"); |
5101 | 421 ALLOW_TAG_ALT("bold", "strong"); |
5093 | 422 ALLOW_TAG("cite"); |
423 ALLOW_TAG("div"); | |
424 ALLOW_TAG("em"); | |
425 ALLOW_TAG("h1"); | |
426 ALLOW_TAG("h2"); | |
427 ALLOW_TAG("h3"); | |
428 ALLOW_TAG("h4"); | |
429 ALLOW_TAG("h5"); | |
430 ALLOW_TAG("h6"); | |
431 ALLOW_TAG("html"); | |
5101 | 432 ALLOW_TAG_ALT("i", "em"); |
433 ALLOW_TAG_ALT("italic", "em"); | |
5093 | 434 ALLOW_TAG("li"); |
435 ALLOW_TAG("ol"); | |
436 ALLOW_TAG("p"); | |
437 ALLOW_TAG("pre"); | |
438 ALLOW_TAG("q"); | |
439 ALLOW_TAG("span"); | |
440 ALLOW_TAG("strong"); | |
441 ALLOW_TAG("ul"); | |
442 | |
5174 | 443 /* we skip <HR> because it's not legal in XHTML-IM. However, |
444 * we still want to send something sensible, so we put a | |
445 * linebreak in its place. <BR> also needs special handling | |
446 * because putting a </BR> to close it would just be dumb. */ | |
447 if((!g_ascii_strncasecmp(c, "<br", 3) | |
448 || !g_ascii_strncasecmp(c, "<hr", 3)) | |
449 && (*(c+3) == '>' || | |
450 !g_ascii_strncasecmp(c+3, "/>", 2) || | |
451 !g_ascii_strncasecmp(c+3, " />", 3))) { | |
452 c = strchr(c, '>') + 1; | |
453 xhtml = g_string_append(xhtml, "<br/>"); | |
454 if(*c != '\n') | |
455 plain = g_string_append_c(plain, '\n'); | |
456 continue; | |
457 } | |
458 if(!g_ascii_strncasecmp(c, "<u>", 3) || !g_ascii_strncasecmp(c, "<underline>", strlen("<underline>"))) { | |
5104 | 459 struct gaim_parse_tag *pt = g_new0(struct gaim_parse_tag, 1); |
460 pt->src_tag = *(c+2) == '>' ? "u" : "underline"; | |
461 pt->dest_tag = "span"; | |
462 tags = g_list_prepend(tags, pt); | |
463 c = strchr(c, '>') + 1; | |
464 xhtml = g_string_append(xhtml, "<span style='text-decoration: underline;'>"); | |
465 continue; | |
466 } | |
5174 | 467 if(!g_ascii_strncasecmp(c, "<s>", 3) || !g_ascii_strncasecmp(c, "<strike>", strlen("<strike>"))) { |
5104 | 468 struct gaim_parse_tag *pt = g_new0(struct gaim_parse_tag, 1); |
469 pt->src_tag = *(c+2) == '>' ? "s" : "strike"; | |
470 pt->dest_tag = "span"; | |
471 tags = g_list_prepend(tags, pt); | |
472 c = strchr(c, '>') + 1; | |
473 xhtml = g_string_append(xhtml, "<span style='text-decoration: line-through;'>"); | |
474 continue; | |
475 } | |
476 if(!g_ascii_strncasecmp(c, "<sub>", 5)) { | |
477 struct gaim_parse_tag *pt = g_new0(struct gaim_parse_tag, 1); | |
478 pt->src_tag = "sub"; | |
479 pt->dest_tag = "span"; | |
480 tags = g_list_prepend(tags, pt); | |
481 c = strchr(c, '>') + 1; | |
482 xhtml = g_string_append(xhtml, "<span style='vertical-align:sub;'>"); | |
483 continue; | |
484 } | |
485 if(!g_ascii_strncasecmp(c, "<sup>", 5)) { | |
486 struct gaim_parse_tag *pt = g_new0(struct gaim_parse_tag, 1); | |
487 pt->src_tag = "sup"; | |
488 pt->dest_tag = "span"; | |
489 tags = g_list_prepend(tags, pt); | |
490 c = strchr(c, '>') + 1; | |
491 xhtml = g_string_append(xhtml, "<span style='vertical-align:super;'>"); | |
492 continue; | |
493 } | |
5107 | 494 if(!g_ascii_strncasecmp(c, "<font", 5) && (*(c+5) == '>' || *(c+5) == ' ')) { |
495 const char *p = c; | |
496 GString *style = g_string_new(""); | |
497 struct gaim_parse_tag *pt; | |
498 while(*p && *p != '>') { | |
499 if(!g_ascii_strncasecmp(p, "color=", strlen("color="))) { | |
500 const char *q = p + strlen("color="); | |
501 GString *color = g_string_new(""); | |
502 if(*q == '\'' || *q == '\"') | |
503 q++; | |
504 while(*q && *q != '\"' && *q != '\'' && *q != ' ') { | |
505 color = g_string_append_c(color, *q); | |
506 q++; | |
507 } | |
508 g_string_append_printf(style, "color: %s; ", color->str); | |
509 g_string_free(color, TRUE); | |
510 p = q; | |
511 } else if(!g_ascii_strncasecmp(p, "face=", strlen("face="))) { | |
512 const char *q = p + strlen("face="); | |
513 gboolean space_allowed = FALSE; | |
514 GString *face = g_string_new(""); | |
515 if(*q == '\'' || *q == '\"') { | |
516 space_allowed = TRUE; | |
517 q++; | |
518 } | |
519 while(*q && *q != '\"' && *q != '\'' && (space_allowed || *q != ' ')) { | |
520 face = g_string_append_c(face, *q); | |
521 q++; | |
522 } | |
523 g_string_append_printf(style, "font-family: %s; ", face->str); | |
524 g_string_free(face, TRUE); | |
525 p = q; | |
526 } else if(!g_ascii_strncasecmp(p, "size=", strlen("size="))) { | |
527 const char *q = p + strlen("size="); | |
528 int sz; | |
529 const char *size = "medium"; | |
530 if(*q == '\'' || *q == '\"') | |
531 q++; | |
532 sz = atoi(q); | |
533 if(sz < 3) | |
534 size = "smaller"; | |
535 else if(sz > 3) | |
536 size = "larger"; | |
537 g_string_append_printf(style, "font-size: %s; ", size); | |
538 p = q; | |
539 } | |
540 p++; | |
541 } | |
542 c = strchr(c, '>') + 1; | |
543 pt = g_new0(struct gaim_parse_tag, 1); | |
544 pt->src_tag = "font"; | |
545 pt->dest_tag = "span"; | |
546 tags = g_list_prepend(tags, pt); | |
547 xhtml = g_string_append(xhtml, "<span"); | |
548 if(style->len) | |
549 g_string_append_printf(xhtml, " style='%s'", style->str); | |
550 xhtml = g_string_append_c(xhtml, '>'); | |
551 g_string_free(style, TRUE); | |
552 continue; | |
553 } | |
554 if(!g_ascii_strncasecmp(c, "<body ", 6)) { | |
555 const char *p = c; | |
556 gboolean did_something = FALSE; | |
557 while(*p && *p != '>') { | |
558 if(!g_ascii_strncasecmp(p, "bgcolor=", strlen("bgcolor="))) { | |
559 const char *q = p + strlen("bgcolor="); | |
560 struct gaim_parse_tag *pt = g_new0(struct gaim_parse_tag, 1); | |
561 GString *color = g_string_new(""); | |
562 if(*q == '\'' || *q == '\"') | |
563 q++; | |
564 while(*q && *q != '\"' && *q != '\'' && *q != ' ') { | |
565 color = g_string_append_c(color, *q); | |
566 q++; | |
567 } | |
568 g_string_append_printf(xhtml, "<span style='background: %s;'>", color->str); | |
569 g_string_free(color, TRUE); | |
570 c = strchr(c, '>') + 1; | |
571 pt->src_tag = "body"; | |
572 pt->dest_tag = "span"; | |
573 tags = g_list_prepend(tags, pt); | |
574 did_something = TRUE; | |
575 break; | |
576 } | |
577 p++; | |
578 } | |
579 if(did_something) continue; | |
580 } | |
581 /* this has to come after the special case for bgcolor */ | |
582 ALLOW_TAG("body"); | |
5093 | 583 if(!g_ascii_strncasecmp(c, "<!--", strlen("<!--"))) { |
584 char *p = strstr(c + strlen("<!--"), "-->"); | |
585 if(p) { | |
586 xhtml = g_string_append(xhtml, "<!--"); | |
587 c += strlen("<!--"); | |
588 continue; | |
589 } | |
590 } | |
591 | |
592 xhtml = g_string_append(xhtml, "<"); | |
5110 | 593 plain = g_string_append_c(plain, '<'); |
5093 | 594 c++; |
595 } | |
596 } else { | |
597 xhtml = g_string_append_c(xhtml, *c); | |
5110 | 598 plain = g_string_append_c(plain, *c); |
5093 | 599 c++; |
600 } | |
601 } | |
602 tag = tags; | |
603 while(tag) { | |
604 g_string_append_printf(xhtml, "</%s>", (char *)tag->data); | |
605 tag = tag->next; | |
606 } | |
607 g_list_free(tags); | |
5110 | 608 if(xhtml_out) |
609 *xhtml_out = g_strdup(xhtml->str); | |
610 if(plain_out) | |
611 *plain_out = g_strdup(plain->str); | |
5093 | 612 g_string_free(xhtml, TRUE); |
5110 | 613 g_string_free(plain, TRUE); |
5093 | 614 } |