Mercurial > pidgin.yaz
annotate src/util.c @ 12912:5f3d5f30590a
[gaim-migrate @ 15265]
SF Patch #1407968 from Sadrul
Fixes SF Bug #1407678
Essentially, if you reordered tabs in the window, the gestures would move
you to the wrong tab. The plugin's concept of the ordering didn't update.
committer: Tailor Script <tailor@pidgin.im>
author | Richard Laager <rlaager@wiktel.com> |
---|---|
date | Tue, 17 Jan 2006 15:43:55 +0000 |
parents | 5e338dda872f |
children | 248b8b39c671 |
rev | line source |
---|---|
1 | 1 /* |
7094
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2 * @file util.h Utility Functions |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3 * @ingroup core |
1 | 4 * |
8046 | 5 * Gaim is the legal property of its developers, whose names are too numerous |
6 * to list here. Please refer to the COPYRIGHT file distributed with this | |
7 * source distribution. | |
1 | 8 * |
9 * This program is free software; you can redistribute it and/or modify | |
10 * it under the terms of the GNU General Public License as published by | |
11 * the Free Software Foundation; either version 2 of the License, or | |
12 * (at your option) any later version. | |
13 * | |
14 * This program is distributed in the hope that it will be useful, | |
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 * GNU General Public License for more details. | |
18 * | |
19 * You should have received a copy of the GNU General Public License | |
20 * along with this program; if not, write to the Free Software | |
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
22 */ | |
5872
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5826
diff
changeset
|
23 #include "internal.h" |
3630 | 24 |
5872
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5826
diff
changeset
|
25 #include "conversation.h" |
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5826
diff
changeset
|
26 #include "debug.h" |
10425 | 27 #include "notify.h" |
1575
427e1409917c
[gaim-migrate @ 1585]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1560
diff
changeset
|
28 #include "prpl.h" |
5545
7a64114641c3
[gaim-migrate @ 5946]
Christian Hammond <chipx86@chipx86.com>
parents:
5532
diff
changeset
|
29 #include "prefs.h" |
7094
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
30 #include "util.h" |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
31 |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
32 typedef struct |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
33 { |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
34 void (*callback)(void *, const char *, size_t); |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
35 void *user_data; |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
36 |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
37 struct |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
38 { |
9227
9171e528d7e5
[gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents:
9222
diff
changeset
|
39 char *user; |
9171e528d7e5
[gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents:
9222
diff
changeset
|
40 char *passwd; |
7094
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
41 char *address; |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
42 int port; |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
43 char *page; |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
44 |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
45 } website; |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
46 |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
47 char *url; |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
48 gboolean full; |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
49 char *user_agent; |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
50 gboolean http11; |
12887
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
51 char *request; |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
52 gboolean include_headers; |
7094
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
53 |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
54 int inpa; |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
55 |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
56 gboolean sentreq; |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
57 gboolean startsaving; |
9240 | 58 gboolean has_explicit_data_len; |
7094
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
59 char *webdata; |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
60 unsigned long len; |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
61 unsigned long data_len; |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
62 |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
63 } GaimFetchUrlData; |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
64 |
8596 | 65 static char custom_home_dir[MAXPATHLEN]; |
3630 | 66 static char home_dir[MAXPATHLEN]; |
67 | |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
68 /************************************************************************** |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
69 * Base16 Functions |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
70 **************************************************************************/ |
11127 | 71 gchar * |
11137 | 72 gaim_base16_encode(const guchar *data, gsize len) |
1 | 73 { |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
74 int i; |
11127 | 75 gchar *ascii = NULL; |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
76 |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
77 g_return_val_if_fail(data != NULL, NULL); |
11127 | 78 g_return_val_if_fail(len > 0, NULL); |
79 | |
80 ascii = g_malloc(len * 2 + 1); | |
81 | |
82 for (i = 0; i < len; i++) | |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
83 snprintf(&ascii[i * 2], 3, "%02hhx", data[i]); |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
84 |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
85 return ascii; |
1 | 86 } |
87 | |
11137 | 88 guchar * |
11127 | 89 gaim_base16_decode(const char *str, gsize *ret_len) |
1 | 90 { |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
91 int len, i, accumulator = 0; |
11137 | 92 guchar *data; |
11127 | 93 |
94 g_return_val_if_fail(str != NULL, NULL); | |
95 | |
96 len = strlen(str); | |
97 | |
98 g_return_val_if_fail(strlen(str) > 0, 0); | |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
99 g_return_val_if_fail(len % 2 > 0, 0); |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
100 |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
101 data = g_malloc(len / 2); |
1 | 102 |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
103 for (i = 0; i < len; i++) |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
104 { |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
105 if ((i % 2) == 0) |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
106 accumulator = 0; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
107 else |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
108 accumulator <<= 4; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
109 |
11127 | 110 if (isdigit(str[i])) |
111 accumulator |= str[i] - 48; | |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
112 else |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
113 { |
11127 | 114 switch(str[i]) |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
115 { |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
116 case 'a': case 'A': accumulator |= 10; break; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
117 case 'b': case 'B': accumulator |= 11; break; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
118 case 'c': case 'C': accumulator |= 12; break; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
119 case 'd': case 'D': accumulator |= 13; break; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
120 case 'e': case 'E': accumulator |= 14; break; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
121 case 'f': case 'F': accumulator |= 15; break; |
1826
7f889cdfa03e
[gaim-migrate @ 1836]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1815
diff
changeset
|
122 } |
1252
46c09828e929
[gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
123 } |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
124 |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
125 if (i % 2) |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
126 data[(i - 1) / 2] = accumulator; |
1252
46c09828e929
[gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
127 } |
1 | 128 |
11127 | 129 if (ret_len != NULL) |
130 *ret_len = len / 2; | |
131 | |
132 return data; | |
1 | 133 } |
134 | |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
135 /************************************************************************** |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
136 * Base64 Functions |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
137 **************************************************************************/ |
4888
912294585edf
[gaim-migrate @ 5218]
Christian Hammond <chipx86@chipx86.com>
parents:
4853
diff
changeset
|
138 static const char alphabet[] = |
912294585edf
[gaim-migrate @ 5218]
Christian Hammond <chipx86@chipx86.com>
parents:
4853
diff
changeset
|
139 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" |
912294585edf
[gaim-migrate @ 5218]
Christian Hammond <chipx86@chipx86.com>
parents:
4853
diff
changeset
|
140 "0123456789+/"; |
1252
46c09828e929
[gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
141 |
8929 | 142 static const char xdigits[] = |
143 "0123456789abcdef"; | |
144 | |
11127 | 145 gchar * |
11137 | 146 gaim_base64_encode(const guchar *data, gsize len) |
1 | 147 { |
6872 | 148 char *out, *rv; |
1252
46c09828e929
[gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
149 |
11127 | 150 g_return_val_if_fail(data != NULL, NULL); |
151 g_return_val_if_fail(len > 0, NULL); | |
152 | |
153 rv = out = g_malloc(((len/3)+1)*4 + 1); | |
154 | |
155 for (; len >= 3; len -= 3) | |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
156 { |
11127 | 157 *out++ = alphabet[data[0] >> 2]; |
158 *out++ = alphabet[((data[0] << 4) & 0x30) | (data[1] >> 4)]; | |
159 *out++ = alphabet[((data[1] << 2) & 0x3c) | (data[2] >> 6)]; | |
160 *out++ = alphabet[data[2] & 0x3f]; | |
161 data += 3; | |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
162 } |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
163 |
11127 | 164 if (len > 0) |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
165 { |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
166 unsigned char fragment; |
6872 | 167 |
11127 | 168 *out++ = alphabet[data[0] >> 2]; |
169 fragment = (data[0] << 4) & 0x30; | |
170 | |
171 if (len > 1) | |
172 fragment |= data[1] >> 4; | |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
173 |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
174 *out++ = alphabet[fragment]; |
11127 | 175 *out++ = (len < 2) ? '=' : alphabet[(data[1] << 2) & 0x3c]; |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
176 *out++ = '='; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
177 } |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
178 |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
179 *out = '\0'; |
5426 | 180 |
181 return rv; | |
1 | 182 } |
183 | |
11137 | 184 guchar * |
11127 | 185 gaim_base64_decode(const char *str, gsize *ret_len) |
1 | 186 { |
11137 | 187 guchar *out = NULL; |
1252
46c09828e929
[gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
188 char tmp = 0; |
1920
5bed3bc833b5
[gaim-migrate @ 1930]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1878
diff
changeset
|
189 const char *c; |
1252
46c09828e929
[gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
190 gint32 tmp2 = 0; |
46c09828e929
[gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
191 int len = 0, n = 0; |
46c09828e929
[gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
192 |
11127 | 193 g_return_val_if_fail(str != NULL, NULL); |
194 | |
195 c = str; | |
1 | 196 |
1252
46c09828e929
[gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
197 while (*c) { |
46c09828e929
[gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
198 if (*c >= 'A' && *c <= 'Z') { |
46c09828e929
[gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
199 tmp = *c - 'A'; |
46c09828e929
[gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
200 } else if (*c >= 'a' && *c <= 'z') { |
46c09828e929
[gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
201 tmp = 26 + (*c - 'a'); |
46c09828e929
[gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
202 } else if (*c >= '0' && *c <= 57) { |
46c09828e929
[gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
203 tmp = 52 + (*c - '0'); |
46c09828e929
[gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
204 } else if (*c == '+') { |
46c09828e929
[gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
205 tmp = 62; |
46c09828e929
[gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
206 } else if (*c == '/') { |
46c09828e929
[gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
207 tmp = 63; |
5289 | 208 } else if (*c == '\r' || *c == '\n') { |
209 c++; | |
210 continue; | |
1252
46c09828e929
[gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
211 } else if (*c == '=') { |
46c09828e929
[gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
212 if (n == 3) { |
46c09828e929
[gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
213 out = g_realloc(out, len + 2); |
11137 | 214 out[len] = (guchar)(tmp2 >> 10) & 0xff; |
1252
46c09828e929
[gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
215 len++; |
11137 | 216 out[len] = (guchar)(tmp2 >> 2) & 0xff; |
1252
46c09828e929
[gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
217 len++; |
46c09828e929
[gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
218 } else if (n == 2) { |
46c09828e929
[gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
219 out = g_realloc(out, len + 1); |
11137 | 220 out[len] = (guchar)(tmp2 >> 4) & 0xff; |
1252
46c09828e929
[gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
221 len++; |
46c09828e929
[gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
222 } |
46c09828e929
[gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
223 break; |
46c09828e929
[gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
224 } |
46c09828e929
[gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
225 tmp2 = ((tmp2 << 6) | (tmp & 0xff)); |
46c09828e929
[gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
226 n++; |
46c09828e929
[gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
227 if (n == 4) { |
46c09828e929
[gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
228 out = g_realloc(out, len + 3); |
11137 | 229 out[len] = (guchar)((tmp2 >> 16) & 0xff); |
1252
46c09828e929
[gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
230 len++; |
11137 | 231 out[len] = (guchar)((tmp2 >> 8) & 0xff); |
1252
46c09828e929
[gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
232 len++; |
11137 | 233 out[len] = (guchar)(tmp2 & 0xff); |
1252
46c09828e929
[gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
234 len++; |
46c09828e929
[gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
235 tmp2 = 0; |
46c09828e929
[gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
236 n = 0; |
46c09828e929
[gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
237 } |
46c09828e929
[gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
238 c++; |
46c09828e929
[gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
239 } |
46c09828e929
[gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
240 |
46c09828e929
[gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
241 out = g_realloc(out, len + 1); |
46c09828e929
[gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
242 out[len] = 0; |
46c09828e929
[gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
243 |
11127 | 244 if (ret_len != NULL) |
245 *ret_len = len; | |
246 | |
247 return out; | |
1 | 248 } |
249 | |
7679 | 250 /************************************************************************** |
11132 | 251 * Quoted Printable Functions (see RFC 2045). |
7679 | 252 **************************************************************************/ |
11137 | 253 guchar * |
11132 | 254 gaim_quotedp_decode(const char *str, gsize *ret_len) |
7679 | 255 { |
8125 | 256 char *n, *new; |
257 const char *end, *p; | |
7722 | 258 |
8005
3bdfb4308d10
[gaim-migrate @ 8682]
Christian Hammond <chipx86@chipx86.com>
parents:
7956
diff
changeset
|
259 n = new = g_malloc(strlen (str) + 1); |
8125 | 260 end = str + strlen(str); |
7679 | 261 |
8125 | 262 for (p = str; p < end; p++, n++) { |
7679 | 263 if (*p == '=') { |
8929 | 264 if (p[1] == '\r' && p[2] == '\n') { /* 5.1 #5 */ |
265 n -= 1; | |
266 p += 2; | |
267 } else if (p[1] == '\n') { /* fuzzy case for 5.1 #5 */ | |
268 n -= 1; | |
269 p += 1; | |
270 } else if (p[1] && p[2]) { | |
271 char *nibble1 = strchr(xdigits, tolower(p[1])); | |
272 char *nibble2 = strchr(xdigits, tolower(p[2])); | |
273 if (nibble1 && nibble2) { /* 5.1 #1 */ | |
274 *n = ((nibble1 - xdigits) << 4) | (nibble2 - xdigits); | |
275 p += 2; | |
276 } else { /* This should never happen */ | |
277 *n = *p; | |
278 } | |
279 } else { /* This should never happen */ | |
280 *n = *p; | |
281 } | |
7679 | 282 } |
283 else if (*p == '_') | |
284 *n = ' '; | |
285 else | |
286 *n = *p; | |
287 } | |
288 | |
289 *n = '\0'; | |
290 | |
11132 | 291 if (ret_len != NULL) |
7679 | 292 *ret_len = n - new; |
7722 | 293 |
7679 | 294 /* Resize to take less space */ |
295 /* new = realloc(new, n - new); */ | |
296 | |
11137 | 297 return (guchar *)new; |
7679 | 298 } |
299 | |
300 /************************************************************************** | |
301 * MIME Functions | |
302 **************************************************************************/ | |
303 char * | |
304 gaim_mime_decode_field(const char *str) | |
305 { | |
7858 | 306 /* |
8958 | 307 * This is wing's version, partially based on revo/shx's version |
308 * See RFC2047 [which apparently obsoletes RFC1342] | |
7858 | 309 */ |
8958 | 310 typedef enum { |
311 state_start, state_equal1, state_question1, | |
312 state_charset, state_question2, | |
313 state_encoding, state_question3, | |
314 state_encoded_text, state_question4, state_equal2 = state_start | |
315 } encoded_word_state_t; | |
316 encoded_word_state_t state = state_start; | |
7858 | 317 const char *cur, *mark; |
8958 | 318 const char *charset0 = NULL, *encoding0 = NULL, *encoded_text0 = NULL; |
7858 | 319 char *n, *new; |
320 | |
8976 | 321 /* token can be any CHAR (supposedly ISO8859-1/ISO2022), not just ASCII */ |
8958 | 322 #define token_char_p(c) \ |
323 (c != ' ' && !iscntrl(c) && !strchr("()<>@,;:\"/[]?.=", c)) | |
324 | |
325 /* But encoded-text must be ASCII; alas, isascii() may not exist */ | |
326 #define encoded_text_char_p(c) \ | |
327 ((c & 0x80) == 0 && c != '?' && c != ' ' && isgraph(c)) | |
328 | |
329 #define RECOVER_MARKED_TEXT strncpy(n, mark, cur - mark + 1); \ | |
330 n += cur - mark + 1 | |
331 | |
8976 | 332 g_return_val_if_fail(str != NULL, NULL); |
333 | |
334 /* NOTE: Assuming that we need just strlen(str)+1 *may* be wrong. | |
335 * It would be wrong if one byte (in some unknown encoding) could | |
336 * expand to >=4 bytes of UTF-8; I don't know if there are such things. | |
337 */ | |
7858 | 338 n = new = g_malloc(strlen(str) + 1); |
339 | |
340 /* Here we will be looking for encoded words and if they seem to be | |
341 * valid then decode them. | |
342 * They are of this form: =?charset?encoding?text?= | |
343 */ | |
344 | |
8958 | 345 for (cur = str, mark = NULL; *cur; cur += 1) { |
346 switch (state) { | |
347 case state_equal1: | |
348 if (*cur == '?') { | |
349 state = state_question1; | |
350 } else { | |
351 RECOVER_MARKED_TEXT; | |
352 state = state_start; | |
353 } | |
354 break; | |
355 case state_question1: | |
356 if (token_char_p(*cur)) { | |
357 charset0 = cur; | |
358 state = state_charset; | |
359 } else { /* This should never happen */ | |
360 RECOVER_MARKED_TEXT; | |
361 state = state_start; | |
362 } | |
363 break; | |
364 case state_charset: | |
7858 | 365 if (*cur == '?') { |
8958 | 366 state = state_question2; |
8976 | 367 } else if (!token_char_p(*cur)) { /* This should never happen */ |
8958 | 368 RECOVER_MARKED_TEXT; |
369 state = state_start; | |
370 } | |
371 break; | |
372 case state_question2: | |
373 if (token_char_p(*cur)) { | |
374 encoding0 = cur; | |
375 state = state_encoding; | |
376 } else { /* This should never happen */ | |
377 RECOVER_MARKED_TEXT; | |
378 state = state_start; | |
379 } | |
380 break; | |
381 case state_encoding: | |
382 if (*cur == '?') { | |
383 state = state_question3; | |
8976 | 384 } else if (!token_char_p(*cur)) { /* This should never happen */ |
8958 | 385 RECOVER_MARKED_TEXT; |
386 state = state_start; | |
387 } | |
388 break; | |
389 case state_question3: | |
390 if (encoded_text_char_p(*cur)) { | |
391 encoded_text0 = cur; | |
392 state = state_encoded_text; | |
8976 | 393 } else if (*cur == '?') { /* empty string */ |
394 encoded_text0 = cur; | |
395 state = state_question4; | |
8958 | 396 } else { /* This should never happen */ |
397 RECOVER_MARKED_TEXT; | |
398 state = state_start; | |
7858 | 399 } |
8958 | 400 break; |
401 case state_encoded_text: | |
402 if (*cur == '?') { | |
403 state = state_question4; | |
404 } else if (!encoded_text_char_p(*cur)) { | |
405 RECOVER_MARKED_TEXT; | |
406 state = state_start; | |
407 } | |
408 break; | |
409 case state_question4: | |
410 if (*cur == '=') { /* Got the whole encoded-word */ | |
411 char *charset = g_strndup(charset0, encoding0 - charset0 - 1); | |
412 char *encoding = g_strndup(encoding0, encoded_text0 - encoding0 - 1); | |
413 char *encoded_text = g_strndup(encoded_text0, cur - encoded_text0 - 1); | |
11137 | 414 guchar *decoded = NULL; |
11127 | 415 gsize dec_len; |
8958 | 416 if (g_ascii_strcasecmp(encoding, "Q") == 0) |
11132 | 417 decoded = gaim_quotedp_decode(encoded_text, &dec_len); |
8958 | 418 else if (g_ascii_strcasecmp(encoding, "B") == 0) |
11127 | 419 decoded = gaim_base64_decode(encoded_text, &dec_len); |
8958 | 420 else |
421 decoded = NULL; | |
422 if (decoded) { | |
423 gsize len; | |
11132 | 424 char *converted = g_convert((const gchar *)decoded, dec_len, "utf-8", charset, NULL, &len, NULL); |
8958 | 425 |
426 if (converted) { | |
427 n = strncpy(n, converted, len) + len; | |
428 g_free(converted); | |
429 } | |
430 g_free(decoded); | |
7858 | 431 } |
8958 | 432 g_free(charset); |
433 g_free(encoding); | |
434 g_free(encoded_text); | |
435 state = state_equal2; /* Restart the FSM */ | |
436 } else { /* This should never happen */ | |
437 RECOVER_MARKED_TEXT; | |
438 state = state_start; | |
7858 | 439 } |
8958 | 440 break; |
441 default: | |
442 if (*cur == '=') { | |
443 mark = cur; | |
444 state = state_equal1; | |
445 } else { | |
446 /* Some unencoded text. */ | |
447 *n = *cur; | |
448 n += 1; | |
449 } | |
450 break; | |
451 } /* switch */ | |
452 } /* for */ | |
453 | |
454 if (state != state_start) { | |
455 RECOVER_MARKED_TEXT; | |
7858 | 456 } |
457 *n = '\0'; | |
458 | |
459 return new; | |
7840 | 460 } |
7824 | 461 |
7679 | 462 |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
463 /************************************************************************** |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
464 * Date/Time Functions |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
465 **************************************************************************/ |
7162 | 466 const char * |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
467 gaim_date(void) |
1 | 468 { |
469 static char date[80]; | |
470 time_t tme; | |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
471 |
1 | 472 time(&tme); |
473 strftime(date, sizeof(date), "%H:%M:%S", localtime(&tme)); | |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
474 |
1 | 475 return date; |
476 } | |
477 | |
7162 | 478 const char * |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
479 gaim_date_full(void) |
1252
46c09828e929
[gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
480 { |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
481 char *date; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
482 time_t tme; |
1252
46c09828e929
[gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1250
diff
changeset
|
483 |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
484 time(&tme); |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
485 date = ctime(&tme); |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
486 date[strlen(date) - 1] = '\0'; |
1100
f168625b63fe
[gaim-migrate @ 1110]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1087
diff
changeset
|
487 |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
488 return date; |
3630 | 489 } |
490 | |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
491 time_t |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
492 gaim_time_build(int year, int month, int day, int hour, int min, int sec) |
1805
caa605e70917
[gaim-migrate @ 1815]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1753
diff
changeset
|
493 { |
caa605e70917
[gaim-migrate @ 1815]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1753
diff
changeset
|
494 struct tm tm; |
caa605e70917
[gaim-migrate @ 1815]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1753
diff
changeset
|
495 |
caa605e70917
[gaim-migrate @ 1815]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1753
diff
changeset
|
496 tm.tm_year = year - 1900; |
caa605e70917
[gaim-migrate @ 1815]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1753
diff
changeset
|
497 tm.tm_mon = month - 1; |
caa605e70917
[gaim-migrate @ 1815]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1753
diff
changeset
|
498 tm.tm_mday = day; |
caa605e70917
[gaim-migrate @ 1815]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1753
diff
changeset
|
499 tm.tm_hour = hour; |
caa605e70917
[gaim-migrate @ 1815]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1753
diff
changeset
|
500 tm.tm_min = min; |
caa605e70917
[gaim-migrate @ 1815]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1753
diff
changeset
|
501 tm.tm_sec = sec >= 0 ? sec : time(NULL) % 60; |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
502 |
1805
caa605e70917
[gaim-migrate @ 1815]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1753
diff
changeset
|
503 return mktime(&tm); |
caa605e70917
[gaim-migrate @ 1815]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1753
diff
changeset
|
504 } |
3230 | 505 |
8577 | 506 time_t |
507 gaim_str_to_time(const char *timestamp, gboolean utc) | |
508 { | |
9722 | 509 time_t retval = 0; |
9716 | 510 struct tm *t; |
511 char buf[32]; | |
512 char *c; | |
513 int tzoff = 0; | |
514 | |
515 time(&retval); | |
516 t = localtime(&retval); | |
517 | |
518 snprintf(buf, sizeof(buf), "%s", timestamp); | |
519 c = buf; | |
520 | |
521 /* 4 digit year */ | |
522 if (!sscanf(c, "%04d", &t->tm_year)) return 0; | |
523 c += 4; | |
524 if (*c == '-') | |
525 c++; | |
526 | |
527 t->tm_year -= 1900; | |
8577 | 528 |
529 /* 2 digit month */ | |
9716 | 530 if (!sscanf(c, "%02d", &t->tm_mon)) return 0; |
531 c += 2; | |
532 if (*c == '-') | |
8577 | 533 c++; |
534 | |
9716 | 535 t->tm_mon -= 1; |
536 | |
537 /* 2 digit day */ | |
538 if (!sscanf(c, "%02d", &t->tm_mday)) return 0; | |
539 c += 2; | |
540 if (*c == 'T' || *c == '.') { /* we have more than a date, keep going */ | |
9725 | 541 c++; /* skip the "T" */ |
542 | |
543 /* 2 digit hour */ | |
544 if (sscanf(c, "%02d:%02d:%02d", &t->tm_hour, &t->tm_min, &t->tm_sec) == 3 || | |
545 sscanf(c, "%02d%02d%02d", &t->tm_hour, &t->tm_min, &t->tm_sec) == 3) { | |
546 int tzhrs, tzmins; | |
547 c += 8; | |
548 if (*c == '.') /* dealing with precision we don't care about */ | |
549 c += 4; | |
550 if ((*c == '+' || *c == '-') && | |
551 sscanf(c+1, "%02d:%02d", &tzhrs, &tzmins)) { | |
552 tzoff = tzhrs*60*60 + tzmins*60; | |
553 if (*c == '+') | |
554 tzoff *= -1; | |
9716 | 555 } |
556 | |
10876 | 557 t->tm_isdst = -1; |
558 | |
9716 | 559 if (tzoff || utc) { |
10987
a8a7730db73c
[gaim-migrate @ 12824]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10948
diff
changeset
|
560 #ifdef _WIN32 |
a8a7730db73c
[gaim-migrate @ 12824]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10948
diff
changeset
|
561 TIME_ZONE_INFORMATION tzi; |
a8a7730db73c
[gaim-migrate @ 12824]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10948
diff
changeset
|
562 DWORD ret; |
a8a7730db73c
[gaim-migrate @ 12824]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10948
diff
changeset
|
563 if ((ret = GetTimeZoneInformation(&tzi)) |
a8a7730db73c
[gaim-migrate @ 12824]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10948
diff
changeset
|
564 != TIME_ZONE_ID_INVALID) { |
a8a7730db73c
[gaim-migrate @ 12824]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10948
diff
changeset
|
565 tzoff -= tzi.Bias * 60; |
a8a7730db73c
[gaim-migrate @ 12824]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10948
diff
changeset
|
566 if (ret == TIME_ZONE_ID_DAYLIGHT) { |
a8a7730db73c
[gaim-migrate @ 12824]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10948
diff
changeset
|
567 tzoff -= tzi.DaylightBias * 60; |
a8a7730db73c
[gaim-migrate @ 12824]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10948
diff
changeset
|
568 } |
a8a7730db73c
[gaim-migrate @ 12824]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10948
diff
changeset
|
569 } |
a8a7730db73c
[gaim-migrate @ 12824]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10948
diff
changeset
|
570 #else |
8577 | 571 #ifdef HAVE_TM_GMTOFF |
9716 | 572 tzoff += t->tm_gmtoff; |
8577 | 573 #else |
9716 | 574 # ifdef HAVE_TIMEZONE |
575 tzset(); /* making sure */ | |
576 tzoff -= timezone; | |
10876 | 577 t->tm_isdst = 0; /* I think this might fix it */ |
9716 | 578 # endif |
8577 | 579 #endif |
10987
a8a7730db73c
[gaim-migrate @ 12824]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10948
diff
changeset
|
580 #endif /* _WIN32 */ |
8577 | 581 } |
9716 | 582 } |
583 } | |
584 | |
585 retval = mktime(t); | |
586 retval += tzoff; | |
587 | |
588 return retval; | |
8577 | 589 } |
590 | |
10636 | 591 size_t gaim_strftime(char *s, size_t max, const char *format, const struct tm *tm) |
592 { | |
593 return strftime(s, max, format, tm); | |
594 } | |
8577 | 595 |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
596 /************************************************************************** |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
597 * Markup Functions |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
598 **************************************************************************/ |
7094
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
599 gboolean |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
600 gaim_markup_find_tag(const char *needle, const char *haystack, |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
601 const char **start, const char **end, GData **attributes) |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
602 { |
6982 | 603 GData *attribs; |
604 const char *cur = haystack; | |
605 char *name = NULL; | |
606 gboolean found = FALSE; | |
607 gboolean in_tag = FALSE; | |
608 gboolean in_attr = FALSE; | |
7094
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
609 const char *in_quotes = NULL; |
7804 | 610 size_t needlelen; |
6982 | 611 |
7804 | 612 g_return_val_if_fail( needle != NULL, FALSE); |
613 g_return_val_if_fail( *needle != '\0', FALSE); | |
614 g_return_val_if_fail( haystack != NULL, FALSE); | |
615 g_return_val_if_fail( *haystack != '\0', FALSE); | |
616 g_return_val_if_fail( start != NULL, FALSE); | |
617 g_return_val_if_fail( end != NULL, FALSE); | |
618 g_return_val_if_fail(attributes != NULL, FALSE); | |
619 | |
620 needlelen = strlen(needle); | |
6982 | 621 g_datalist_init(&attribs); |
622 | |
623 while (*cur && !found) { | |
624 if (in_tag) { | |
625 if (in_quotes) { | |
626 const char *close = cur; | |
627 | |
7078 | 628 while (*close && *close != *in_quotes) |
6982 | 629 close++; |
630 | |
631 /* if we got the close quote, store the value and carry on from * | |
632 * after it. if we ran to the end of the string, point to the NULL * | |
633 * and we're outta here */ | |
634 if (*close) { | |
635 /* only store a value if we have an attribute name */ | |
636 if (name) { | |
637 size_t len = close - cur; | |
638 char *val = g_strndup(cur, len); | |
639 | |
640 g_datalist_set_data_full(&attribs, name, val, g_free); | |
641 g_free(name); | |
642 name = NULL; | |
643 } | |
644 | |
7078 | 645 in_quotes = NULL; |
6982 | 646 cur = close + 1; |
647 } else { | |
648 cur = close; | |
649 } | |
650 } else if (in_attr) { | |
651 const char *close = cur; | |
652 | |
7078 | 653 while (*close && *close != '>' && *close != '"' && |
654 *close != '\'' && *close != ' ' && *close != '=') | |
6982 | 655 close++; |
656 | |
657 /* if we got the equals, store the name of the attribute. if we got | |
658 * the quote, save the attribute and go straight to quote mode. | |
659 * otherwise the tag closed or we reached the end of the string, | |
660 * so we can get outta here */ | |
661 switch (*close) { | |
662 case '"': | |
7078 | 663 case '\'': |
664 in_quotes = close; | |
6982 | 665 case '=': |
666 { | |
667 size_t len = close - cur; | |
668 | |
669 /* don't store a blank attribute name */ | |
670 if (len) { | |
671 if (name) | |
672 g_free(name); | |
673 name = g_ascii_strdown(cur, len); | |
674 } | |
675 | |
676 in_attr = FALSE; | |
677 cur = close + 1; | |
678 break; | |
679 } | |
680 case ' ': | |
681 case '>': | |
682 in_attr = FALSE; | |
683 default: | |
684 cur = close; | |
685 break; | |
686 } | |
687 } else { | |
688 switch (*cur) { | |
689 case ' ': | |
690 /* swallow extra spaces inside tag */ | |
691 while (*cur && *cur == ' ') cur++; | |
692 in_attr = TRUE; | |
693 break; | |
694 case '>': | |
695 found = TRUE; | |
696 *end = cur; | |
697 break; | |
698 case '"': | |
7078 | 699 case '\'': |
700 in_quotes = cur; | |
6982 | 701 default: |
702 cur++; | |
703 break; | |
704 } | |
705 } | |
706 } else { | |
707 /* if we hit a < followed by the name of our tag... */ | |
708 if (*cur == '<' && !g_ascii_strncasecmp(cur + 1, needle, needlelen)) { | |
709 *start = cur; | |
710 cur = cur + needlelen + 1; | |
711 | |
712 /* if we're pointing at a space or a >, we found the right tag. if * | |
713 * we're not, we've found a longer tag, so we need to skip to the * | |
714 * >, but not being distracted by >s inside quotes. */ | |
715 if (*cur == ' ' || *cur == '>') { | |
716 in_tag = TRUE; | |
717 } else { | |
7804 | 718 while (*cur && *cur != '"' && *cur != '\'' && *cur != '>') { |
6982 | 719 if (*cur == '"') { |
720 cur++; | |
721 while (*cur && *cur != '"') | |
722 cur++; | |
7804 | 723 } else if (*cur == '\'') { |
724 cur++; | |
725 while (*cur && *cur != '\'') | |
726 cur++; | |
6982 | 727 } else { |
728 cur++; | |
729 } | |
730 } | |
731 } | |
732 } else { | |
733 cur++; | |
734 } | |
735 } | |
736 } | |
737 | |
738 /* clean up any attribute name from a premature termination */ | |
739 if (name) | |
740 g_free(name); | |
741 | |
742 if (found) { | |
743 *attributes = attribs; | |
744 } else { | |
745 *start = NULL; | |
746 *end = NULL; | |
747 *attributes = NULL; | |
748 } | |
749 | |
750 return found; | |
751 } | |
7094
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
752 |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
753 gboolean |
7675 | 754 gaim_markup_extract_info_field(const char *str, int len, GString *dest, |
7095
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
755 const char *start_token, int skip, |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
756 const char *end_token, char check_value, |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
757 const char *no_value_token, |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
758 const char *display_name, gboolean is_link, |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
759 const char *link_prefix) |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
760 { |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
761 const char *p, *q; |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
762 |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
763 g_return_val_if_fail(str != NULL, FALSE); |
7675 | 764 g_return_val_if_fail(dest != NULL, FALSE); |
7095
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
765 g_return_val_if_fail(start_token != NULL, FALSE); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
766 g_return_val_if_fail(end_token != NULL, FALSE); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
767 g_return_val_if_fail(display_name != NULL, FALSE); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
768 |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
769 p = strstr(str, start_token); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
770 |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
771 if (p == NULL) |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
772 return FALSE; |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
773 |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
774 p += strlen(start_token) + skip; |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
775 |
7675 | 776 if (p >= str + len) |
777 return FALSE; | |
778 | |
7095
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
779 if (check_value != '\0' && *p == check_value) |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
780 return FALSE; |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
781 |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
782 q = strstr(p, end_token); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
783 |
9161 | 784 /* Trim leading blanks */ |
785 while (*p != '\n' && g_ascii_isspace(*p)) { | |
786 p += 1; | |
787 } | |
788 | |
789 /* Trim trailing blanks */ | |
790 while (q > p && g_ascii_isspace(*(q - 1))) { | |
791 q -= 1; | |
792 } | |
793 | |
794 /* Don't bother with null strings */ | |
795 if (p == q) | |
796 return FALSE; | |
797 | |
7095
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
798 if (q != NULL && (!no_value_token || |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
799 (no_value_token && strncmp(p, no_value_token, |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
800 strlen(no_value_token))))) |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
801 { |
9220 | 802 g_string_append_printf(dest, _("<b>%s:</b> "), display_name); |
7095
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
803 |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
804 if (is_link) |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
805 { |
7675 | 806 g_string_append(dest, "<br><a href=\""); |
7095
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
807 |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
808 if (link_prefix) |
7675 | 809 g_string_append(dest, link_prefix); |
7095
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
810 |
7675 | 811 g_string_append_len(dest, p, q - p); |
812 g_string_append(dest, "\">"); | |
7095
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
813 |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
814 if (link_prefix) |
7675 | 815 g_string_append(dest, link_prefix); |
7095
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
816 |
7675 | 817 g_string_append_len(dest, p, q - p); |
818 g_string_append(dest, "</a>"); | |
7095
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
819 } |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
820 else |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
821 { |
7675 | 822 g_string_append_len(dest, p, q - p); |
7095
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
823 } |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
824 |
7675 | 825 g_string_append(dest, "<br>\n"); |
7095
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
826 |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
827 return TRUE; |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
828 } |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
829 |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
830 return FALSE; |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
831 } |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
832 |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
833 struct gaim_parse_tag { |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
834 char *src_tag; |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
835 char *dest_tag; |
8064 | 836 gboolean ignore; |
7095
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
837 }; |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
838 |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
839 #define ALLOW_TAG_ALT(x, y) if(!g_ascii_strncasecmp(c, "<" x " ", strlen("<" x " "))) { \ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
840 const char *o = c + strlen("<" x); \ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
841 const char *p = NULL, *q = NULL, *r = NULL; \ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
842 GString *innards = g_string_new(""); \ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
843 while(o && *o) { \ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
844 if(!q && (*o == '\"' || *o == '\'') ) { \ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
845 q = o; \ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
846 } else if(q) { \ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
847 if(*o == *q) { \ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
848 char *unescaped = g_strndup(q+1, o-q-1); \ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
849 char *escaped = g_markup_escape_text(unescaped, -1); \ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
850 g_string_append_printf(innards, "%c%s%c", *q, escaped, *q); \ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
851 g_free(unescaped); \ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
852 g_free(escaped); \ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
853 q = NULL; \ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
854 } else if(*c == '\\') { \ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
855 o++; \ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
856 } \ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
857 } else if(*o == '<') { \ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
858 r = o; \ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
859 } else if(*o == '>') { \ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
860 p = o; \ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
861 break; \ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
862 } else { \ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
863 innards = g_string_append_c(innards, *o); \ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
864 } \ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
865 o++; \ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
866 } \ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
867 if(p && !r) { \ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
868 if(*(p-1) != '/') { \ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
869 struct gaim_parse_tag *pt = g_new0(struct gaim_parse_tag, 1); \ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
870 pt->src_tag = x; \ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
871 pt->dest_tag = y; \ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
872 tags = g_list_prepend(tags, pt); \ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
873 } \ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
874 xhtml = g_string_append(xhtml, "<" y); \ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
875 c += strlen("<" x ); \ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
876 xhtml = g_string_append(xhtml, innards->str); \ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
877 xhtml = g_string_append_c(xhtml, '>'); \ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
878 c = p + 1; \ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
879 } else { \ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
880 xhtml = g_string_append(xhtml, "<"); \ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
881 plain = g_string_append_c(plain, '<'); \ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
882 c++; \ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
883 } \ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
884 g_string_free(innards, TRUE); \ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
885 continue; \ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
886 } \ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
887 if(!g_ascii_strncasecmp(c, "<" x, strlen("<" x)) && \ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
888 (*(c+strlen("<" x)) == '>' || \ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
889 !g_ascii_strncasecmp(c+strlen("<" x), "/>", 2))) { \ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
890 xhtml = g_string_append(xhtml, "<" y); \ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
891 c += strlen("<" x); \ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
892 if(*c != '/') { \ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
893 struct gaim_parse_tag *pt = g_new0(struct gaim_parse_tag, 1); \ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
894 pt->src_tag = x; \ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
895 pt->dest_tag = y; \ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
896 tags = g_list_prepend(tags, pt); \ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
897 xhtml = g_string_append_c(xhtml, '>'); \ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
898 } else { \ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
899 xhtml = g_string_append(xhtml, "/>");\ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
900 } \ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
901 c = strchr(c, '>') + 1; \ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
902 continue; \ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
903 } |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
904 #define ALLOW_TAG(x) ALLOW_TAG_ALT(x, x) |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
905 void |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
906 gaim_markup_html_to_xhtml(const char *html, char **xhtml_out, |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
907 char **plain_out) |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
908 { |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
909 GString *xhtml = g_string_new(""); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
910 GString *plain = g_string_new(""); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
911 GList *tags = NULL, *tag; |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
912 const char *c = html; |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
913 |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
914 while(c && *c) { |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
915 if(*c == '<') { |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
916 if(*(c+1) == '/') { /* closing tag */ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
917 tag = tags; |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
918 while(tag) { |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
919 struct gaim_parse_tag *pt = tag->data; |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
920 if(!g_ascii_strncasecmp((c+2), pt->src_tag, strlen(pt->src_tag)) && *(c+strlen(pt->src_tag)+2) == '>') { |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
921 c += strlen(pt->src_tag) + 3; |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
922 break; |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
923 } |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
924 tag = tag->next; |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
925 } |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
926 if(tag) { |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
927 while(tags) { |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
928 struct gaim_parse_tag *pt = tags->data; |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
929 g_string_append_printf(xhtml, "</%s>", pt->dest_tag); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
930 if(tags == tag) |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
931 break; |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
932 tags = g_list_remove(tags, pt); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
933 g_free(pt); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
934 } |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
935 g_free(tag->data); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
936 tags = g_list_remove(tags, tag->data); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
937 } else { |
8534 | 938 /* a closing tag we weren't expecting... |
939 * we'll let it slide, if it's really a tag...if it's | |
940 * just a </ we'll escape it properly */ | |
941 const char *end = c+2; | |
942 while(*end && g_ascii_isalpha(*end)) | |
943 end++; | |
944 if(*end == '>') { | |
945 c = end+1; | |
946 } else { | |
947 xhtml = g_string_append(xhtml, "<"); | |
948 plain = g_string_append_c(plain, '<'); | |
949 c++; | |
950 } | |
7095
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
951 } |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
952 } else { /* opening tag */ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
953 ALLOW_TAG("a"); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
954 ALLOW_TAG_ALT("b", "strong"); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
955 ALLOW_TAG("blockquote"); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
956 ALLOW_TAG_ALT("bold", "strong"); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
957 ALLOW_TAG("cite"); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
958 ALLOW_TAG("div"); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
959 ALLOW_TAG("em"); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
960 ALLOW_TAG("h1"); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
961 ALLOW_TAG("h2"); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
962 ALLOW_TAG("h3"); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
963 ALLOW_TAG("h4"); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
964 ALLOW_TAG("h5"); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
965 ALLOW_TAG("h6"); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
966 /* we only allow html to start the message */ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
967 if(c == html) |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
968 ALLOW_TAG("html"); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
969 ALLOW_TAG_ALT("i", "em"); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
970 ALLOW_TAG_ALT("italic", "em"); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
971 ALLOW_TAG("li"); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
972 ALLOW_TAG("ol"); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
973 ALLOW_TAG("p"); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
974 ALLOW_TAG("pre"); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
975 ALLOW_TAG("q"); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
976 ALLOW_TAG("span"); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
977 ALLOW_TAG("strong"); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
978 ALLOW_TAG("ul"); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
979 |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
980 /* we skip <HR> because it's not legal in XHTML-IM. However, |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
981 * we still want to send something sensible, so we put a |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
982 * linebreak in its place. <BR> also needs special handling |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
983 * because putting a </BR> to close it would just be dumb. */ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
984 if((!g_ascii_strncasecmp(c, "<br", 3) |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
985 || !g_ascii_strncasecmp(c, "<hr", 3)) |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
986 && (*(c+3) == '>' || |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
987 !g_ascii_strncasecmp(c+3, "/>", 2) || |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
988 !g_ascii_strncasecmp(c+3, " />", 3))) { |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
989 c = strchr(c, '>') + 1; |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
990 xhtml = g_string_append(xhtml, "<br/>"); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
991 if(*c != '\n') |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
992 plain = g_string_append_c(plain, '\n'); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
993 continue; |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
994 } |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
995 if(!g_ascii_strncasecmp(c, "<u>", 3) || !g_ascii_strncasecmp(c, "<underline>", strlen("<underline>"))) { |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
996 struct gaim_parse_tag *pt = g_new0(struct gaim_parse_tag, 1); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
997 pt->src_tag = *(c+2) == '>' ? "u" : "underline"; |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
998 pt->dest_tag = "span"; |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
999 tags = g_list_prepend(tags, pt); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1000 c = strchr(c, '>') + 1; |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1001 xhtml = g_string_append(xhtml, "<span style='text-decoration: underline;'>"); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1002 continue; |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1003 } |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1004 if(!g_ascii_strncasecmp(c, "<s>", 3) || !g_ascii_strncasecmp(c, "<strike>", strlen("<strike>"))) { |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1005 struct gaim_parse_tag *pt = g_new0(struct gaim_parse_tag, 1); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1006 pt->src_tag = *(c+2) == '>' ? "s" : "strike"; |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1007 pt->dest_tag = "span"; |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1008 tags = g_list_prepend(tags, pt); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1009 c = strchr(c, '>') + 1; |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1010 xhtml = g_string_append(xhtml, "<span style='text-decoration: line-through;'>"); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1011 continue; |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1012 } |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1013 if(!g_ascii_strncasecmp(c, "<sub>", 5)) { |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1014 struct gaim_parse_tag *pt = g_new0(struct gaim_parse_tag, 1); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1015 pt->src_tag = "sub"; |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1016 pt->dest_tag = "span"; |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1017 tags = g_list_prepend(tags, pt); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1018 c = strchr(c, '>') + 1; |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1019 xhtml = g_string_append(xhtml, "<span style='vertical-align:sub;'>"); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1020 continue; |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1021 } |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1022 if(!g_ascii_strncasecmp(c, "<sup>", 5)) { |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1023 struct gaim_parse_tag *pt = g_new0(struct gaim_parse_tag, 1); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1024 pt->src_tag = "sup"; |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1025 pt->dest_tag = "span"; |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1026 tags = g_list_prepend(tags, pt); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1027 c = strchr(c, '>') + 1; |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1028 xhtml = g_string_append(xhtml, "<span style='vertical-align:super;'>"); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1029 continue; |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1030 } |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1031 if(!g_ascii_strncasecmp(c, "<font", 5) && (*(c+5) == '>' || *(c+5) == ' ')) { |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1032 const char *p = c; |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1033 GString *style = g_string_new(""); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1034 struct gaim_parse_tag *pt; |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1035 while(*p && *p != '>') { |
8064 | 1036 if(!g_ascii_strncasecmp(p, "back=", strlen("back="))) { |
1037 const char *q = p + strlen("back="); | |
1038 GString *color = g_string_new(""); | |
1039 if(*q == '\'' || *q == '\"') | |
1040 q++; | |
1041 while(*q && *q != '\"' && *q != '\'' && *q != ' ') { | |
1042 color = g_string_append_c(color, *q); | |
1043 q++; | |
1044 } | |
1045 g_string_append_printf(style, "background: %s; ", color->str); | |
1046 g_string_free(color, TRUE); | |
1047 p = q; | |
1048 } else if(!g_ascii_strncasecmp(p, "color=", strlen("color="))) { | |
7095
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1049 const char *q = p + strlen("color="); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1050 GString *color = g_string_new(""); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1051 if(*q == '\'' || *q == '\"') |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1052 q++; |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1053 while(*q && *q != '\"' && *q != '\'' && *q != ' ') { |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1054 color = g_string_append_c(color, *q); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1055 q++; |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1056 } |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1057 g_string_append_printf(style, "color: %s; ", color->str); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1058 g_string_free(color, TRUE); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1059 p = q; |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1060 } else if(!g_ascii_strncasecmp(p, "face=", strlen("face="))) { |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1061 const char *q = p + strlen("face="); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1062 gboolean space_allowed = FALSE; |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1063 GString *face = g_string_new(""); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1064 if(*q == '\'' || *q == '\"') { |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1065 space_allowed = TRUE; |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1066 q++; |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1067 } |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1068 while(*q && *q != '\"' && *q != '\'' && (space_allowed || *q != ' ')) { |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1069 face = g_string_append_c(face, *q); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1070 q++; |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1071 } |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1072 g_string_append_printf(style, "font-family: %s; ", face->str); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1073 g_string_free(face, TRUE); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1074 p = q; |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1075 } else if(!g_ascii_strncasecmp(p, "size=", strlen("size="))) { |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1076 const char *q = p + strlen("size="); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1077 int sz; |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1078 const char *size = "medium"; |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1079 if(*q == '\'' || *q == '\"') |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1080 q++; |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1081 sz = atoi(q); |
8686 | 1082 switch (sz) |
1083 { | |
1084 case 1: | |
1085 size = "xx-small"; | |
1086 break; | |
1087 case 2: | |
1088 size = "x-small"; | |
1089 break; | |
1090 case 3: | |
1091 size = "small"; | |
1092 break; | |
1093 case 4: | |
1094 size = "medium"; | |
1095 break; | |
1096 case 5: | |
1097 size = "large"; | |
1098 break; | |
1099 case 6: | |
1100 size = "x-large"; | |
1101 break; | |
1102 case 7: | |
1103 size = "xx-large"; | |
1104 break; | |
1105 default: | |
1106 break; | |
1107 } | |
7095
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1108 g_string_append_printf(style, "font-size: %s; ", size); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1109 p = q; |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1110 } |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1111 p++; |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1112 } |
10593 | 1113 if ((c = strchr(c, '>')) != NULL) |
1114 c++; | |
1115 else | |
1116 c = p; | |
8064 | 1117 pt = g_new0(struct gaim_parse_tag, 1); |
1118 pt->src_tag = "font"; | |
1119 pt->dest_tag = "span"; | |
1120 tags = g_list_prepend(tags, pt); | |
1121 if(style->len) | |
8026 | 1122 g_string_append_printf(xhtml, "<span style='%s'>", style->str); |
8064 | 1123 else |
1124 pt->ignore = TRUE; | |
7095
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1125 g_string_free(style, TRUE); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1126 continue; |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1127 } |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1128 if(!g_ascii_strncasecmp(c, "<body ", 6)) { |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1129 const char *p = c; |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1130 gboolean did_something = FALSE; |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1131 while(*p && *p != '>') { |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1132 if(!g_ascii_strncasecmp(p, "bgcolor=", strlen("bgcolor="))) { |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1133 const char *q = p + strlen("bgcolor="); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1134 struct gaim_parse_tag *pt = g_new0(struct gaim_parse_tag, 1); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1135 GString *color = g_string_new(""); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1136 if(*q == '\'' || *q == '\"') |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1137 q++; |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1138 while(*q && *q != '\"' && *q != '\'' && *q != ' ') { |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1139 color = g_string_append_c(color, *q); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1140 q++; |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1141 } |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1142 g_string_append_printf(xhtml, "<span style='background: %s;'>", color->str); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1143 g_string_free(color, TRUE); |
10623 | 1144 if ((c = strchr(c, '>')) != NULL) |
1145 c++; | |
1146 else | |
1147 c = p; | |
7095
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1148 pt->src_tag = "body"; |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1149 pt->dest_tag = "span"; |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1150 tags = g_list_prepend(tags, pt); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1151 did_something = TRUE; |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1152 break; |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1153 } |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1154 p++; |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1155 } |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1156 if(did_something) continue; |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1157 } |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1158 /* this has to come after the special case for bgcolor */ |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1159 ALLOW_TAG("body"); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1160 if(!g_ascii_strncasecmp(c, "<!--", strlen("<!--"))) { |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1161 char *p = strstr(c + strlen("<!--"), "-->"); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1162 if(p) { |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1163 xhtml = g_string_append(xhtml, "<!--"); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1164 c += strlen("<!--"); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1165 continue; |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1166 } |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1167 } |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1168 |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1169 xhtml = g_string_append(xhtml, "<"); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1170 plain = g_string_append_c(plain, '<'); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1171 c++; |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1172 } |
7287 | 1173 } else if(*c == '&') { |
1174 char buf[7]; | |
1175 char *pln; | |
1176 int len = 1; | |
1177 guint pound; | |
1178 if(!g_ascii_strncasecmp(c, "&", 5)) { | |
1179 pln = "&"; | |
1180 len = 5; | |
1181 } else if(!g_ascii_strncasecmp(c, "<", 4)) { | |
1182 pln = "<"; | |
1183 len = 4; | |
1184 } else if(!g_ascii_strncasecmp(c, ">", 4)) { | |
1185 pln = ">"; | |
1186 len = 4; | |
1187 } else if(!g_ascii_strncasecmp(c, " ", 6)) { | |
1188 pln = " "; | |
1189 len = 6; | |
1190 } else if(!g_ascii_strncasecmp(c, "©", 6)) { | |
1191 pln = "©"; | |
1192 len = 6; | |
1193 } else if(!g_ascii_strncasecmp(c, """, 6)) { | |
1194 pln = "\""; | |
1195 len = 6; | |
1196 } else if(!g_ascii_strncasecmp(c, "®", 5)) { | |
1197 pln = "®"; | |
1198 len = 5; | |
1199 } else if(!g_ascii_strncasecmp(c, "'", 6)) { | |
1200 pln = "\'"; | |
1201 len = 6; | |
1202 } else if(*(c+1) == '#' && (sscanf(c, "&#%u;", £) == 1) && | |
1203 pound != 0 && *(c+3+(gint)log10(pound)) == ';') { | |
1204 int buflen = g_unichar_to_utf8((gunichar)pound, buf); | |
1205 buf[buflen] = '\0'; | |
1206 pln = buf; | |
1207 | |
1208 | |
1209 len = 2; | |
1210 while(isdigit((gint) c [len])) len++; | |
1211 if(c [len] == ';') len++; | |
1212 } else { | |
1213 len = 1; | |
1214 g_snprintf(buf, sizeof(buf), "%c", *c); | |
1215 pln = buf; | |
1216 } | |
1217 xhtml = g_string_append_len(xhtml, c, len); | |
1218 plain = g_string_append(plain, pln); | |
1219 c += len; | |
7095
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1220 } else { |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1221 xhtml = g_string_append_c(xhtml, *c); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1222 plain = g_string_append_c(plain, *c); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1223 c++; |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1224 } |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1225 } |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1226 tag = tags; |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1227 while(tag) { |
8064 | 1228 struct gaim_parse_tag *pt = tag->data; |
1229 if(!pt->ignore) | |
1230 g_string_append_printf(xhtml, "</%s>", pt->dest_tag); | |
7095
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1231 tag = tag->next; |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1232 } |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1233 g_list_free(tags); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1234 if(xhtml_out) |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1235 *xhtml_out = g_strdup(xhtml->str); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1236 if(plain_out) |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1237 *plain_out = g_strdup(plain->str); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1238 g_string_free(xhtml, TRUE); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1239 g_string_free(plain, TRUE); |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1240 } |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1241 |
9161 | 1242 /* The following are probably reasonable changes: |
1243 * - \n should be converted to a normal space | |
1244 * - in addition to <br>, <p> and <div> etc. should also be converted into \n | |
9241 | 1245 * - We want to turn </td>#whitespace<td> sequences into a single tab |
1246 * - We want to turn <td> into a single tab (for msn profile "parsing") | |
9161 | 1247 * - We want to turn </tr>#whitespace<tr> sequences into a single \n |
9241 | 1248 * - <script>...</script> and <style>...</style> should be completely removed |
9161 | 1249 */ |
1250 | |
7095
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1251 char * |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1252 gaim_markup_strip_html(const char *str) |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1253 { |
7103 | 1254 int i, j, k; |
1255 gboolean visible = TRUE; | |
9161 | 1256 gboolean closing_td_p = FALSE; |
7103 | 1257 gchar *str2; |
9241 | 1258 const gchar *cdata_close_tag = NULL; |
12620
13599d978a31
[gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents:
12483
diff
changeset
|
1259 gchar *href = NULL; |
13599d978a31
[gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents:
12483
diff
changeset
|
1260 int href_st = 0; |
7103 | 1261 |
7110 | 1262 if(!str) |
1263 return NULL; | |
7103 | 1264 |
1265 str2 = g_strdup(str); | |
1266 | |
1267 for (i = 0, j = 0; str2[i]; i++) | |
1268 { | |
1269 if (str2[i] == '<') | |
1270 { | |
9241 | 1271 if (cdata_close_tag) |
9161 | 1272 { |
9241 | 1273 /* Note: Don't even assume any other tag is a tag in CDATA */ |
1274 if (strncasecmp(str2 + i, cdata_close_tag, | |
1275 strlen(cdata_close_tag)) == 0) | |
1276 { | |
1277 i += strlen(cdata_close_tag) - 1; | |
1278 cdata_close_tag = NULL; | |
1279 } | |
1280 continue; | |
1281 } | |
1282 else if (strncasecmp(str2 + i, "<td", 3) == 0 && closing_td_p) | |
1283 { | |
1284 str2[j++] = '\t'; | |
9161 | 1285 visible = TRUE; |
1286 } | |
1287 else if (strncasecmp(str2 + i, "</td>", 5) == 0) | |
8517 | 1288 { |
9161 | 1289 closing_td_p = TRUE; |
1290 visible = FALSE; | |
1291 } | |
1292 else | |
1293 { | |
1294 closing_td_p = FALSE; | |
1295 visible = TRUE; | |
8517 | 1296 } |
1297 | |
7103 | 1298 k = i + 1; |
1299 | |
1300 if(g_ascii_isspace(str2[k])) | |
1301 visible = TRUE; | |
9170 | 1302 else if (str2[k]) |
7103 | 1303 { |
9161 | 1304 /* Scan until we end the tag either implicitly (closed start |
1305 * tag) or explicitly, using a sloppy method (i.e., < or > | |
1306 * inside quoted attributes will screw us up) | |
1307 */ | |
1308 while (str2[k] && str2[k] != '<' && str2[k] != '>') | |
7103 | 1309 { |
1310 k++; | |
1311 } | |
9241 | 1312 |
12620
13599d978a31
[gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents:
12483
diff
changeset
|
1313 /* If we've got an <a> tag with an href, save the address |
13599d978a31
[gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents:
12483
diff
changeset
|
1314 * to print later. */ |
13599d978a31
[gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents:
12483
diff
changeset
|
1315 if (strncasecmp(str2 + i, "<a", 2) == 0 && |
13599d978a31
[gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents:
12483
diff
changeset
|
1316 g_ascii_isspace(str2[i+2])) |
13599d978a31
[gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents:
12483
diff
changeset
|
1317 { |
13599d978a31
[gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents:
12483
diff
changeset
|
1318 int st; /* start of href, inclusive [ */ |
13599d978a31
[gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents:
12483
diff
changeset
|
1319 int end; /* end of href, exclusive ) */ |
13599d978a31
[gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents:
12483
diff
changeset
|
1320 char delim = ' '; |
13599d978a31
[gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents:
12483
diff
changeset
|
1321 /* Find start of href */ |
13599d978a31
[gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents:
12483
diff
changeset
|
1322 for (st = i + 3; st < k; st++) |
13599d978a31
[gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents:
12483
diff
changeset
|
1323 { |
13599d978a31
[gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents:
12483
diff
changeset
|
1324 if (strncasecmp(str2+st, "href=", 5) == 0) |
13599d978a31
[gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents:
12483
diff
changeset
|
1325 { |
13599d978a31
[gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents:
12483
diff
changeset
|
1326 st += 5; |
13599d978a31
[gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents:
12483
diff
changeset
|
1327 if (str2[st] == '"') |
13599d978a31
[gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents:
12483
diff
changeset
|
1328 { |
13599d978a31
[gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents:
12483
diff
changeset
|
1329 delim = '"'; |
13599d978a31
[gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents:
12483
diff
changeset
|
1330 st++; |
13599d978a31
[gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents:
12483
diff
changeset
|
1331 } |
13599d978a31
[gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents:
12483
diff
changeset
|
1332 break; |
13599d978a31
[gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents:
12483
diff
changeset
|
1333 } |
13599d978a31
[gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents:
12483
diff
changeset
|
1334 } |
13599d978a31
[gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents:
12483
diff
changeset
|
1335 /* find end of address */ |
13599d978a31
[gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents:
12483
diff
changeset
|
1336 for (end = st; end < k && str2[end] != delim; end++) |
13599d978a31
[gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents:
12483
diff
changeset
|
1337 { |
13599d978a31
[gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents:
12483
diff
changeset
|
1338 /* All the work is done in the loop construct above. */ |
13599d978a31
[gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents:
12483
diff
changeset
|
1339 } |
13599d978a31
[gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents:
12483
diff
changeset
|
1340 |
13599d978a31
[gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents:
12483
diff
changeset
|
1341 /* If there's an address, save it. If there was |
13599d978a31
[gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents:
12483
diff
changeset
|
1342 * already one saved, kill it. */ |
13599d978a31
[gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents:
12483
diff
changeset
|
1343 if (st < k) |
13599d978a31
[gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents:
12483
diff
changeset
|
1344 { |
13599d978a31
[gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents:
12483
diff
changeset
|
1345 char *tmp; |
13599d978a31
[gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents:
12483
diff
changeset
|
1346 g_free(href); |
13599d978a31
[gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents:
12483
diff
changeset
|
1347 tmp = g_strndup(str2 + st, end - st); |
13599d978a31
[gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents:
12483
diff
changeset
|
1348 href = gaim_unescape_html(tmp); |
13599d978a31
[gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents:
12483
diff
changeset
|
1349 g_free(tmp); |
13599d978a31
[gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents:
12483
diff
changeset
|
1350 href_st = j; |
13599d978a31
[gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents:
12483
diff
changeset
|
1351 } |
13599d978a31
[gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents:
12483
diff
changeset
|
1352 } |
13599d978a31
[gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents:
12483
diff
changeset
|
1353 |
13599d978a31
[gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents:
12483
diff
changeset
|
1354 /* Replace </a> with an ascii representation of the |
13599d978a31
[gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents:
12483
diff
changeset
|
1355 * address the link was pointing to. */ |
13599d978a31
[gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents:
12483
diff
changeset
|
1356 else if (href != NULL && strncasecmp(str2 + i, "</a>", 4) == 0) |
13599d978a31
[gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents:
12483
diff
changeset
|
1357 { |
13599d978a31
[gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents:
12483
diff
changeset
|
1358 |
13599d978a31
[gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents:
12483
diff
changeset
|
1359 size_t hrlen = strlen(href); |
13599d978a31
[gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents:
12483
diff
changeset
|
1360 |
13599d978a31
[gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents:
12483
diff
changeset
|
1361 /* Only insert the href if it's different from the CDATA. */ |
12640
de306144dc8a
[gaim-migrate @ 14976]
Richard Laager <rlaager@wiktel.com>
parents:
12628
diff
changeset
|
1362 if ((hrlen != j - href_st || |
de306144dc8a
[gaim-migrate @ 14976]
Richard Laager <rlaager@wiktel.com>
parents:
12628
diff
changeset
|
1363 strncmp(str2 + href_st, href, hrlen)) && |
de306144dc8a
[gaim-migrate @ 14976]
Richard Laager <rlaager@wiktel.com>
parents:
12628
diff
changeset
|
1364 (hrlen != j - href_st + 7 || /* 7 == strlen("http://") */ |
de306144dc8a
[gaim-migrate @ 14976]
Richard Laager <rlaager@wiktel.com>
parents:
12628
diff
changeset
|
1365 strncmp(str2 + href_st, href + 7, hrlen - 7))) |
12620
13599d978a31
[gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents:
12483
diff
changeset
|
1366 { |
13599d978a31
[gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents:
12483
diff
changeset
|
1367 str2[j++] = ' '; |
12628
26647ddcc9c5
[gaim-migrate @ 14964]
Richard Laager <rlaager@wiktel.com>
parents:
12622
diff
changeset
|
1368 str2[j++] = '('; |
12620
13599d978a31
[gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents:
12483
diff
changeset
|
1369 g_memmove(str2 + j, href, hrlen); |
13599d978a31
[gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents:
12483
diff
changeset
|
1370 j += hrlen; |
12628
26647ddcc9c5
[gaim-migrate @ 14964]
Richard Laager <rlaager@wiktel.com>
parents:
12622
diff
changeset
|
1371 str2[j++] = ')'; |
12620
13599d978a31
[gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents:
12483
diff
changeset
|
1372 g_free(href); |
13599d978a31
[gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents:
12483
diff
changeset
|
1373 href = NULL; |
13599d978a31
[gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents:
12483
diff
changeset
|
1374 } |
13599d978a31
[gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents:
12483
diff
changeset
|
1375 } |
13599d978a31
[gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents:
12483
diff
changeset
|
1376 |
9161 | 1377 /* Check for tags which should be mapped to newline */ |
12620
13599d978a31
[gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents:
12483
diff
changeset
|
1378 else if (strncasecmp(str2 + i, "<p>", 3) == 0 |
9161 | 1379 || strncasecmp(str2 + i, "<tr", 3) == 0 |
1380 || strncasecmp(str2 + i, "<br", 3) == 0 | |
1381 || strncasecmp(str2 + i, "<li", 3) == 0 | |
1382 || strncasecmp(str2 + i, "<div", 4) == 0 | |
1383 || strncasecmp(str2 + i, "</table>", 8) == 0) | |
1384 { | |
1385 str2[j++] = '\n'; | |
1386 } | |
9241 | 1387 /* Check for tags which begin CDATA and need to be closed */ |
1388 #if 0 /* FIXME.. option is end tag optional, we can't handle this right now */ | |
1389 else if (strncasecmp(str2 + i, "<option", 7) == 0) | |
1390 { | |
1391 /* FIXME: We should not do this if the OPTION is SELECT'd */ | |
1392 cdata_close_tag = "</option>"; | |
1393 } | |
1394 #endif | |
1395 else if (strncasecmp(str2 + i, "<script", 7) == 0) | |
1396 { | |
1397 cdata_close_tag = "</script>"; | |
1398 } | |
1399 else if (strncasecmp(str2 + i, "<style", 6) == 0) | |
1400 { | |
1401 cdata_close_tag = "</style>"; | |
1402 } | |
9161 | 1403 /* Update the index and continue checking after the tag */ |
10731 | 1404 i = (str2[k] == '<' || str2[k] == '\0')? k - 1: k; |
9161 | 1405 continue; |
7103 | 1406 } |
1407 } | |
9241 | 1408 else if (cdata_close_tag) |
1409 { | |
1410 continue; | |
1411 } | |
9161 | 1412 else if (!g_ascii_isspace(str2[i])) |
7103 | 1413 { |
1414 visible = TRUE; | |
1415 } | |
1416 | |
9273 | 1417 /* XXX: This sucks. We need to be un-escaping all entities, which |
1418 * includes these, as well as the &#num; ones */ | |
1419 | |
7103 | 1420 if (str2[i] == '&' && strncasecmp(str2 + i, """, 6) == 0) |
1421 { | |
1422 str2[j++] = '\"'; | |
1423 i = i + 5; | |
1424 continue; | |
1425 } | |
1426 | |
8517 | 1427 if (str2[i] == '&' && strncasecmp(str2 + i, "&", 5) == 0) |
1428 { | |
1429 str2[j++] = '&'; | |
1430 i = i + 4; | |
1431 continue; | |
1432 } | |
1433 | |
1434 if (str2[i] == '&' && strncasecmp(str2 + i, "<", 4) == 0) | |
1435 { | |
1436 str2[j++] = '<'; | |
1437 i = i + 3; | |
1438 continue; | |
1439 } | |
1440 | |
1441 if (str2[i] == '&' && strncasecmp(str2 + i, ">", 4) == 0) | |
1442 { | |
1443 str2[j++] = '>'; | |
1444 i = i + 3; | |
1445 continue; | |
1446 } | |
1447 | |
9273 | 1448 if (str2[i] == '&' && strncasecmp(str2 + i, "'", 6) == 0) |
1449 { | |
1450 str2[j++] = '\''; | |
1451 i = i + 5; | |
1452 continue; | |
1453 } | |
1454 | |
7103 | 1455 if (visible) |
9161 | 1456 str2[j++] = g_ascii_isspace(str2[i])? ' ': str2[i]; |
7103 | 1457 } |
1458 | |
12620
13599d978a31
[gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents:
12483
diff
changeset
|
1459 g_free(href); |
13599d978a31
[gaim-migrate @ 14956]
Richard Laager <rlaager@wiktel.com>
parents:
12483
diff
changeset
|
1460 |
7103 | 1461 str2[j] = '\0'; |
1462 | |
1463 return str2; | |
7095
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1464 } |
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
1465 |
10461 | 1466 static gboolean |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
1467 badchar(char c) |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
1468 { |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
1469 switch (c) { |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
1470 case ' ': |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
1471 case ',': |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
1472 case '\0': |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
1473 case '\n': |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
1474 case '<': |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
1475 case '>': |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
1476 case '"': |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
1477 case '\'': |
10461 | 1478 return TRUE; |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
1479 default: |
10461 | 1480 return FALSE; |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
1481 } |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
1482 } |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
1483 |
10461 | 1484 static gboolean |
1485 badentity(const char *c) | |
1486 { | |
1487 if (!g_ascii_strncasecmp(c, "<", 4) || | |
1488 !g_ascii_strncasecmp(c, ">", 4) || | |
1489 !g_ascii_strncasecmp(c, """, 6)) { | |
1490 return TRUE; | |
1491 } | |
1492 return FALSE; | |
1493 } | |
1494 | |
7107
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1495 char * |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1496 gaim_markup_linkify(const char *text) |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1497 { |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1498 const char *c, *t, *q = NULL; |
10902 | 1499 char *tmp, *tmpurlbuf, *url_buf; |
9222 | 1500 gunichar g; |
8538 | 1501 gboolean inside_html = FALSE; |
9222 | 1502 int inside_paren = 0; |
7107
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1503 GString *ret = g_string_new(""); |
10372
618a330c0260
[gaim-migrate @ 11595]
Luke Schierer <lschiere@pidgin.im>
parents:
10332
diff
changeset
|
1504 /* Assumes you have a buffer able to carry at least BUF_LEN * 2 bytes */ |
7107
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1505 |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1506 c = text; |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1507 while (*c) { |
9222 | 1508 |
1509 if(*c == '(' && !inside_html) { | |
1510 inside_paren++; | |
1511 ret = g_string_append_c(ret, *c); | |
1512 c++; | |
1513 } | |
1514 | |
8538 | 1515 if(inside_html) { |
1516 if(*c == '>') { | |
1517 inside_html = FALSE; | |
1518 } else if(!q && (*c == '\"' || *c == '\'')) { | |
1519 q = c; | |
1520 } else if(q) { | |
1521 if(*c == *q) | |
1522 q = NULL; | |
1523 } | |
1524 } else if(*c == '<') { | |
1525 inside_html = TRUE; | |
9077 | 1526 if (!g_ascii_strncasecmp(c, "<A", 2)) { |
1527 while (1) { | |
1528 if (!g_ascii_strncasecmp(c, "/A>", 3)) { | |
1529 inside_html = FALSE; | |
1530 break; | |
1531 } | |
1532 ret = g_string_append_c(ret, *c); | |
1533 c++; | |
1534 if (!(*c)) | |
1535 break; | |
7107
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1536 } |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1537 } |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1538 } else if ((*c=='h') && (!g_ascii_strncasecmp(c, "http://", 7) || |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1539 (!g_ascii_strncasecmp(c, "https://", 8)))) { |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1540 t = c; |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1541 while (1) { |
10461 | 1542 if (badchar(*t) || badentity(t)) { |
7107
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1543 |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1544 if (*(t) == ',' && (*(t + 1) != ' ')) { |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1545 t++; |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1546 continue; |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1547 } |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1548 |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1549 if (*(t - 1) == '.') |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1550 t--; |
9222 | 1551 if ((*(t - 1) == ')' && (inside_paren > 0))) { |
1552 t--; | |
1553 } | |
10461 | 1554 |
10902 | 1555 url_buf = g_strndup(c, t - c); |
8918 | 1556 tmpurlbuf = gaim_unescape_html(url_buf); |
7107
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1557 g_string_append_printf(ret, "<A HREF=\"%s\">%s</A>", |
8918 | 1558 tmpurlbuf, url_buf); |
10902 | 1559 g_free(url_buf); |
8918 | 1560 g_free(tmpurlbuf); |
7107
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1561 c = t; |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1562 break; |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1563 } |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1564 if (!t) |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1565 break; |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1566 t++; |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1567 |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1568 } |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1569 } else if (!g_ascii_strncasecmp(c, "www.", 4)) { |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1570 if (c[4] != '.') { |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1571 t = c; |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1572 while (1) { |
10461 | 1573 if (badchar(*t) || badentity(t)) { |
7107
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1574 if (t - c == 4) { |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1575 break; |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1576 } |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1577 |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1578 if (*(t) == ',' && (*(t + 1) != ' ')) { |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1579 t++; |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1580 continue; |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1581 } |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1582 |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1583 if (*(t - 1) == '.') |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1584 t--; |
9222 | 1585 if ((*(t - 1) == ')' && (inside_paren > 0))) { |
1586 t--; | |
1587 } | |
10902 | 1588 url_buf = g_strndup(c, t - c); |
8918 | 1589 tmpurlbuf = gaim_unescape_html(url_buf); |
7107
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1590 g_string_append_printf(ret, |
8918 | 1591 "<A HREF=\"http://%s\">%s</A>", tmpurlbuf, |
7107
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1592 url_buf); |
10902 | 1593 g_free(url_buf); |
8918 | 1594 g_free(tmpurlbuf); |
7107
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1595 c = t; |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1596 break; |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1597 } |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1598 if (!t) |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1599 break; |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1600 t++; |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1601 } |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1602 } |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1603 } else if (!g_ascii_strncasecmp(c, "ftp://", 6)) { |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1604 t = c; |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1605 while (1) { |
10461 | 1606 if (badchar(*t) || badentity(t)) { |
7107
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1607 if (*(t - 1) == '.') |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1608 t--; |
9222 | 1609 if ((*(t - 1) == ')' && (inside_paren > 0))) { |
1610 t--; | |
1611 } | |
10905 | 1612 url_buf = g_strndup(c, t - c); |
8918 | 1613 tmpurlbuf = gaim_unescape_html(url_buf); |
7107
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1614 g_string_append_printf(ret, "<A HREF=\"%s\">%s</A>", |
8918 | 1615 tmpurlbuf, url_buf); |
10902 | 1616 g_free(url_buf); |
8918 | 1617 g_free(tmpurlbuf); |
7107
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1618 c = t; |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1619 break; |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1620 } |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1621 if (!t) |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1622 break; |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1623 t++; |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1624 |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1625 } |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1626 } else if (!g_ascii_strncasecmp(c, "ftp.", 4)) { |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1627 if (c[4] != '.') { |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1628 t = c; |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1629 while (1) { |
10461 | 1630 if (badchar(*t) || badentity(t)) { |
7107
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1631 if (t - c == 4) { |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1632 break; |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1633 } |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1634 if (*(t - 1) == '.') |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1635 t--; |
9222 | 1636 if ((*(t - 1) == ')' && (inside_paren > 0))) { |
1637 t--; | |
1638 } | |
10902 | 1639 url_buf = g_strndup(c, t - c); |
8918 | 1640 tmpurlbuf = gaim_unescape_html(url_buf); |
7107
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1641 g_string_append_printf(ret, |
8918 | 1642 "<A HREF=\"ftp://%s\">%s</A>", tmpurlbuf, |
7107
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1643 url_buf); |
10902 | 1644 g_free(url_buf); |
8918 | 1645 g_free(tmpurlbuf); |
7107
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1646 c = t; |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1647 break; |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1648 } |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1649 if (!t) |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1650 break; |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1651 t++; |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1652 } |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1653 } |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1654 } else if (!g_ascii_strncasecmp(c, "mailto:", 7)) { |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1655 t = c; |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1656 while (1) { |
10461 | 1657 if (badchar(*t) || badentity(t)) { |
7107
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1658 if (*(t - 1) == '.') |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1659 t--; |
10902 | 1660 url_buf = g_strndup(c, t - c); |
8918 | 1661 tmpurlbuf = gaim_unescape_html(url_buf); |
7107
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1662 g_string_append_printf(ret, "<A HREF=\"%s\">%s</A>", |
8918 | 1663 tmpurlbuf, url_buf); |
10902 | 1664 g_free(url_buf); |
8918 | 1665 g_free(tmpurlbuf); |
7107
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1666 c = t; |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1667 break; |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1668 } |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1669 if (!t) |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1670 break; |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1671 t++; |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1672 |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1673 } |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1674 } else if (c != text && (*c == '@')) { |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1675 int flag; |
10905 | 1676 GString *gurl_buf = NULL; |
7107
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1677 const char illegal_chars[] = "!@#$%^&*()[]{}/|\\<>\":;\r\n \0"; |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1678 |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1679 if (strchr(illegal_chars,*(c - 1)) || strchr(illegal_chars, *(c + 1))) |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1680 flag = 0; |
10902 | 1681 else { |
7107
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1682 flag = 1; |
10902 | 1683 gurl_buf = g_string_new(""); |
1684 } | |
7107
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1685 |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1686 t = c; |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1687 while (flag) { |
9222 | 1688 /* iterate backwards grabbing the local part of an email address */ |
1689 g = g_utf8_get_char(t); | |
1690 if (badchar(*t) || (g >= 127) || (*t == '(') || | |
11176 | 1691 ((*t == ';') && ((t > (text+2) && (!g_ascii_strncasecmp(t - 3, "<", 4) || |
1692 !g_ascii_strncasecmp(t - 3, ">", 4))) || | |
1693 (t > (text+4) && (!g_ascii_strncasecmp(t - 5, """, 6)))))) { | |
9222 | 1694 /* local part will already be part of ret, strip it out */ |
1695 ret = g_string_truncate(ret, ret->len - (c - t)); | |
1696 ret = g_string_append_unichar(ret, g); | |
7107
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1697 break; |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1698 } else { |
9222 | 1699 g_string_prepend_unichar(gurl_buf, g); |
1700 t = g_utf8_find_prev_char(text, t); | |
7107
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1701 if (t < text) { |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1702 ret = g_string_assign(ret, ""); |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1703 break; |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1704 } |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1705 } |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1706 } |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1707 |
9222 | 1708 t = g_utf8_find_next_char(c, NULL); |
7107
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1709 |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1710 while (flag) { |
9222 | 1711 /* iterate forwards grabbing the domain part of an email address */ |
1712 g = g_utf8_get_char(t); | |
10461 | 1713 if (badchar(*t) || (g >= 127) || (*t == ')') || badentity(t)) { |
7107
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1714 char *d; |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1715 |
10902 | 1716 url_buf = g_string_free(gurl_buf, FALSE); |
9222 | 1717 |
1718 /* strip off trailing periods */ | |
10902 | 1719 if (strlen(url_buf) > 0) { |
1720 for (d = url_buf + strlen(url_buf) - 1; *d == '.'; d--, t--) | |
1721 *d = '\0'; | |
1722 } | |
7107
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1723 |
8918 | 1724 tmpurlbuf = gaim_unescape_html(url_buf); |
9045 | 1725 if (gaim_email_is_valid(tmpurlbuf)) { |
1726 g_string_append_printf(ret, "<A HREF=\"mailto:%s\">%s</A>", | |
1727 tmpurlbuf, url_buf); | |
1728 } else { | |
1729 g_string_append(ret, url_buf); | |
1730 } | |
10902 | 1731 g_free(url_buf); |
8918 | 1732 g_free(tmpurlbuf); |
7107
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1733 c = t; |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1734 |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1735 break; |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1736 } else { |
9222 | 1737 g_string_append_unichar(gurl_buf, g); |
1738 t = g_utf8_find_next_char(t, NULL); | |
7107
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1739 } |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1740 } |
9222 | 1741 } |
1742 | |
1743 if(*c == ')' && !inside_html) { | |
1744 inside_paren--; | |
1745 ret = g_string_append_c(ret, *c); | |
1746 c++; | |
7107
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1747 } |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1748 |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1749 if (*c == 0) |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1750 break; |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1751 |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1752 ret = g_string_append_c(ret, *c); |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1753 c++; |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1754 |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1755 } |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1756 tmp = ret->str; |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1757 g_string_free(ret, FALSE); |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1758 return tmp; |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1759 } |
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1760 |
8163 | 1761 char * |
8442 | 1762 gaim_unescape_html(const char *html) { |
1763 char *unescaped = NULL; | |
1764 | |
1765 if (html != NULL) { | |
1766 const char *c = html; | |
1767 GString *ret = g_string_new(""); | |
1768 while (*c) { | |
1769 if (!strncmp(c, "&", 5)) { | |
1770 ret = g_string_append_c(ret, '&'); | |
1771 c += 5; | |
1772 } else if (!strncmp(c, "<", 4)) { | |
1773 ret = g_string_append_c(ret, '<'); | |
1774 c += 4; | |
1775 } else if (!strncmp(c, ">", 4)) { | |
1776 ret = g_string_append_c(ret, '>'); | |
1777 c += 4; | |
1778 } else if (!strncmp(c, """, 6)) { | |
1779 ret = g_string_append_c(ret, '"'); | |
1780 c += 6; | |
8625 | 1781 } else if (!strncmp(c, "'", 6)) { |
1782 ret = g_string_append_c(ret, '\''); | |
1783 c += 6; | |
8500 | 1784 } else if (!strncmp(c, "<br>", 4)) { |
1785 ret = g_string_append_c(ret, '\n'); | |
1786 c += 4; | |
8442 | 1787 } else { |
1788 ret = g_string_append_c(ret, *c); | |
1789 c++; | |
1790 } | |
1791 } | |
1792 | |
1793 unescaped = ret->str; | |
1794 g_string_free(ret, FALSE); | |
1795 } | |
1796 return unescaped; | |
1797 | |
1798 } | |
1799 | |
9175 | 1800 char * |
1801 gaim_markup_slice(const char *str, guint x, guint y) | |
1802 { | |
1803 GString *ret; | |
1804 GQueue *q; | |
1805 guint z = 0; | |
1806 gboolean appended = FALSE; | |
1807 gunichar c; | |
1808 char *tag; | |
1809 | |
1810 g_return_val_if_fail(x <= y, NULL); | |
1811 | |
1812 if (x == y) | |
1813 return g_strdup(""); | |
1814 | |
1815 ret = g_string_new(""); | |
1816 q = g_queue_new(); | |
1817 | |
1818 while (*str && (z < y)) { | |
1819 c = g_utf8_get_char(str); | |
1820 | |
1821 if (c == '<') { | |
1822 char *end = strchr(str, '>'); | |
1823 | |
1824 if (!end) { | |
1825 g_string_free(ret, TRUE); | |
1826 while ((tag = g_queue_pop_head(q))) | |
1827 g_free(tag); | |
1828 g_queue_free(q); | |
1829 return NULL; | |
1830 } | |
1831 | |
1832 if (!g_ascii_strncasecmp(str, "<img ", 5)) { | |
1833 z += strlen("[Image]"); | |
1834 } else if (!g_ascii_strncasecmp(str, "<br", 3)) { | |
1835 z += 1; | |
1836 } else if (!g_ascii_strncasecmp(str, "<hr>", 4)) { | |
1837 z += strlen("\n---\n"); | |
1838 } else if (!g_ascii_strncasecmp(str, "</", 2)) { | |
1839 /* pop stack */ | |
1840 char *tmp; | |
1841 | |
1842 tmp = g_queue_pop_head(q); | |
1843 if (tmp) | |
1844 g_free(tmp); | |
1845 /* z += 0; */ | |
1846 } else { | |
1847 /* push it unto the stack */ | |
1848 char *tmp; | |
1849 | |
1850 tmp = g_strndup(str, end - str + 1); | |
1851 g_queue_push_head(q, tmp); | |
1852 /* z += 0; */ | |
1853 } | |
1854 | |
1855 if (z == x && !appended) { | |
1856 GList *l = q->tail; | |
1857 | |
1858 while (l) { | |
1859 tag = l->data; | |
1860 g_string_append(ret, tag); | |
1861 l = l->prev; | |
1862 } | |
1863 appended = TRUE; | |
1864 } else if (z >= x) { | |
1865 g_string_append_len(ret, str, end - str + 1); | |
1866 } | |
1867 | |
1868 str = end; | |
1869 } else if (c == '&') { | |
1870 char *end = strchr(str, ';'); | |
1871 if (!end) { | |
1872 g_string_free(ret, TRUE); | |
1873 while ((tag = g_queue_pop_head(q))) | |
1874 g_free(tag); | |
1875 g_queue_free(q); | |
1876 | |
1877 return NULL; | |
1878 } | |
1879 | |
1880 if (z >= x) | |
1881 g_string_append_len(ret, str, end - str + 1); | |
1882 | |
1883 z++; | |
1884 str = end; | |
1885 } else { | |
1886 if (z >= x) | |
1887 g_string_append_unichar(ret, c); | |
1888 z++; | |
1889 } | |
1890 | |
1891 str = g_utf8_next_char(str); | |
1892 } | |
1893 | |
1894 while ((tag = g_queue_pop_head(q))) { | |
1895 char *name; | |
1896 | |
1897 name = gaim_markup_get_tag_name(tag); | |
1898 g_string_append_printf(ret, "</%s>", name); | |
1899 g_free(name); | |
1900 g_free(tag); | |
1901 } | |
1902 | |
1903 g_queue_free(q); | |
1904 return g_string_free(ret, FALSE); | |
1905 } | |
1906 | |
1907 char * | |
1908 gaim_markup_get_tag_name(const char *tag) | |
1909 { | |
1910 int i; | |
1911 g_return_val_if_fail(tag != NULL, NULL); | |
1912 g_return_val_if_fail(*tag == '<', NULL); | |
1913 | |
1914 for (i = 1; tag[i]; i++) | |
1915 if (tag[i] == '>' || tag[i] == ' ' || tag[i] == '/') | |
1916 break; | |
1917 | |
1918 return g_strndup(tag, i); | |
1919 } | |
1920 | |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
1921 /************************************************************************** |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
1922 * Path/Filename Functions |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
1923 **************************************************************************/ |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
1924 const char * |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
1925 gaim_home_dir(void) |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
1926 { |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
1927 #ifndef _WIN32 |
11665 | 1928 return g_get_home_dir(); |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
1929 #else |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
1930 return wgaim_data_dir(); |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
1931 #endif |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
1932 } |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
1933 |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
1934 /* returns a string of the form ~/.gaim, where ~ is replaced by the user's home |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
1935 * dir. Note that there is no trailing slash after .gaim. */ |
10332 | 1936 const char * |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
1937 gaim_user_dir(void) |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
1938 { |
8596 | 1939 if (custom_home_dir != NULL && strlen(custom_home_dir) > 0) { |
1940 strcpy ((char*) &home_dir, (char*) &custom_home_dir); | |
1941 } else { | |
1942 const gchar *hd = gaim_home_dir(); | |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
1943 |
8596 | 1944 if (hd) { |
9916 | 1945 g_strlcpy((char*) &home_dir, hd, sizeof(home_dir)); |
1946 g_strlcat((char*) &home_dir, G_DIR_SEPARATOR_S ".gaim", | |
1947 sizeof(home_dir)); | |
8596 | 1948 } |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
1949 } |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
1950 |
8596 | 1951 return home_dir; |
1952 } | |
1953 | |
10871 | 1954 void gaim_util_set_user_dir(const char *dir) |
8596 | 1955 { |
1956 if (dir != NULL && strlen(dir) > 0) { | |
9916 | 1957 g_strlcpy((char*) &custom_home_dir, dir, |
1958 sizeof(custom_home_dir)); | |
8596 | 1959 } |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
1960 } |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
1961 |
7622 | 1962 int gaim_build_dir (const char *path, int mode) |
7612 | 1963 { |
11361 | 1964 #if GLIB_CHECK_VERSION(2,8,0) |
1965 return g_mkdir_with_parents(path, mode); | |
1966 #else | |
7612 | 1967 char *dir, **components, delim[] = { G_DIR_SEPARATOR, '\0' }; |
1968 int cur, len; | |
1969 | |
7632 | 1970 g_return_val_if_fail(path != NULL, -1); |
7612 | 1971 |
1972 dir = g_new0(char, strlen(path) + 1); | |
7622 | 1973 components = g_strsplit(path, delim, -1); |
7612 | 1974 len = 0; |
1975 for (cur = 0; components[cur] != NULL; cur++) { | |
7633 | 1976 /* If you don't know what you're doing on both |
1977 * win32 and *NIX, stay the hell away from this code */ | |
1978 if(cur > 1) | |
1979 dir[len++] = G_DIR_SEPARATOR; | |
7612 | 1980 strcpy(dir + len, components[cur]); |
1981 len += strlen(components[cur]); | |
7633 | 1982 if(cur == 0) |
1983 dir[len++] = G_DIR_SEPARATOR; | |
7632 | 1984 |
1985 if(g_file_test(dir, G_FILE_TEST_IS_DIR)) { | |
1986 continue; | |
10948
747ef488c600
[gaim-migrate @ 12747]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10905
diff
changeset
|
1987 #ifdef _WIN32 |
747ef488c600
[gaim-migrate @ 12747]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10905
diff
changeset
|
1988 /* allow us to create subdirs on UNC paths |
747ef488c600
[gaim-migrate @ 12747]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10905
diff
changeset
|
1989 * (\\machinename\path\to\blah) |
747ef488c600
[gaim-migrate @ 12747]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10905
diff
changeset
|
1990 * g_file_test() doesn't work on "\\machinename" */ |
747ef488c600
[gaim-migrate @ 12747]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10905
diff
changeset
|
1991 } else if (cur == 2 && dir[0] == '\\' && dir[1] == '\\' |
747ef488c600
[gaim-migrate @ 12747]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10905
diff
changeset
|
1992 && components[cur + 1] != NULL) { |
747ef488c600
[gaim-migrate @ 12747]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10905
diff
changeset
|
1993 continue; |
747ef488c600
[gaim-migrate @ 12747]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10905
diff
changeset
|
1994 #endif |
7632 | 1995 } else if(g_file_test(dir, G_FILE_TEST_EXISTS)) { |
9716 | 1996 gaim_debug_warning("build_dir", "bad path: %s\n", path); |
7612 | 1997 g_strfreev(components); |
1998 g_free(dir); | |
1999 return -1; | |
2000 } | |
2001 | |
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10569
diff
changeset
|
2002 if (g_mkdir(dir, mode) < 0) { |
9716 | 2003 gaim_debug_warning("build_dir", "mkdir: %s\n", strerror(errno)); |
7612 | 2004 g_strfreev(components); |
2005 g_free(dir); | |
2006 return -1; | |
2007 } | |
2008 } | |
2009 | |
2010 g_strfreev(components); | |
2011 g_free(dir); | |
2012 return 0; | |
11361 | 2013 #endif |
7612 | 2014 } |
2015 | |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2016 /* |
10414 | 2017 * This function is long and beautiful, like my--um, yeah. Anyway, |
2018 * it includes lots of error checking so as we don't overwrite | |
2019 * people's settings if there is a problem writing the new values. | |
2020 */ | |
2021 gboolean | |
10415 | 2022 gaim_util_write_data_to_file(const char *filename, const char *data, size_t size) |
10414 | 2023 { |
2024 const char *user_dir = gaim_user_dir(); | |
2025 gchar *filename_temp, *filename_full; | |
2026 FILE *file; | |
10415 | 2027 size_t real_size, byteswritten; |
10414 | 2028 struct stat st; |
2029 | |
2030 g_return_val_if_fail(user_dir != NULL, FALSE); | |
2031 | |
2032 gaim_debug_info("util", "Writing file %s to directory %s\n", | |
2033 filename, user_dir); | |
2034 | |
2035 /* Ensure the user directory exists */ | |
2036 if (!g_file_test(user_dir, G_FILE_TEST_IS_DIR)) | |
2037 { | |
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10569
diff
changeset
|
2038 if (g_mkdir(user_dir, S_IRUSR | S_IWUSR | S_IXUSR) == -1) |
10414 | 2039 { |
2040 gaim_debug_error("util", "Error creating directory %s: %s\n", | |
2041 user_dir, strerror(errno)); | |
2042 return FALSE; | |
2043 } | |
2044 } | |
2045 | |
10415 | 2046 filename_full = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s", user_dir, filename); |
10414 | 2047 filename_temp = g_strdup_printf("%s.save", filename_full); |
2048 | |
2049 /* Remove an old temporary file, if one exists */ | |
2050 if (g_file_test(filename_temp, G_FILE_TEST_EXISTS)) | |
2051 { | |
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10569
diff
changeset
|
2052 if (g_unlink(filename_temp) == -1) |
10414 | 2053 { |
2054 gaim_debug_error("util", "Error removing old file %s: %s\n", | |
2055 filename_temp, strerror(errno)); | |
2056 } | |
2057 } | |
2058 | |
2059 /* Open file */ | |
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10569
diff
changeset
|
2060 file = g_fopen(filename_temp, "wb"); |
10414 | 2061 if (file == NULL) |
2062 { | |
2063 gaim_debug_error("util", "Error opening file %s for writing: %s\n", | |
2064 filename_temp, strerror(errno)); | |
2065 g_free(filename_full); | |
2066 g_free(filename_temp); | |
2067 return FALSE; | |
2068 } | |
2069 | |
2070 /* Write to file */ | |
10415 | 2071 real_size = (size == -1) ? strlen(data) : size; |
2072 byteswritten = fwrite(data, 1, real_size, file); | |
10414 | 2073 |
2074 /* Close file */ | |
2075 if (fclose(file) != 0) | |
2076 { | |
2077 gaim_debug_error("util", "Error closing file %s: %s\n", | |
2078 filename_temp, strerror(errno)); | |
2079 g_free(filename_full); | |
2080 g_free(filename_temp); | |
2081 return FALSE; | |
2082 } | |
2083 | |
2084 /* Ensure the file is the correct size */ | |
10415 | 2085 if (byteswritten != real_size) |
10414 | 2086 { |
11656
f9c5480ad0ce
[gaim-migrate @ 13940]
Richard Laager <rlaager@wiktel.com>
parents:
11552
diff
changeset
|
2087 gaim_debug_error("util", "Error writing to file %s: Wrote %" G_GSIZE_FORMAT " bytes " |
f9c5480ad0ce
[gaim-migrate @ 13940]
Richard Laager <rlaager@wiktel.com>
parents:
11552
diff
changeset
|
2088 "but should have written %" G_GSIZE_FORMAT "; is your disk full?\n", |
10415 | 2089 filename_temp, byteswritten, real_size); |
10414 | 2090 g_free(filename_full); |
2091 g_free(filename_temp); | |
2092 return FALSE; | |
2093 } | |
2094 /* Use stat to be absolutely sure. */ | |
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10569
diff
changeset
|
2095 if ((g_stat(filename_temp, &st) == -1) || (st.st_size != real_size)) |
10414 | 2096 { |
2097 gaim_debug_error("util", "Error writing data to file %s: " | |
2098 "Incomplete file written; is your disk full?\n", | |
2099 filename_temp); | |
2100 g_free(filename_full); | |
2101 g_free(filename_temp); | |
2102 return FALSE; | |
2103 } | |
2104 | |
12622
b7860c9902f2
[gaim-migrate @ 14958]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12620
diff
changeset
|
2105 #ifndef _WIN32 |
10414 | 2106 /* Set file permissions */ |
2107 if (chmod(filename_temp, S_IRUSR | S_IWUSR) == -1) | |
2108 { | |
2109 gaim_debug_error("util", "Error setting permissions of file %s: %s\n", | |
2110 filename_temp, strerror(errno)); | |
2111 } | |
12622
b7860c9902f2
[gaim-migrate @ 14958]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12620
diff
changeset
|
2112 #endif |
10414 | 2113 |
2114 /* Remove the old file, if it exists */ | |
2115 if (g_file_test(filename_full, G_FILE_TEST_EXISTS)) | |
2116 { | |
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10569
diff
changeset
|
2117 if (g_unlink(filename_full) == -1) |
10414 | 2118 { |
2119 gaim_debug_error("util", "Error removing old file %s: %s\n", | |
2120 filename_full, strerror(errno)); | |
2121 } | |
2122 } | |
2123 | |
2124 /* Rename to the REAL name */ | |
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10569
diff
changeset
|
2125 if (g_rename(filename_temp, filename_full) == -1) |
10414 | 2126 { |
2127 gaim_debug_error("util", "Error renaming %s to %s: %s\n", | |
2128 filename_temp, filename_full, strerror(errno)); | |
2129 } | |
2130 | |
2131 g_free(filename_full); | |
2132 g_free(filename_temp); | |
2133 | |
2134 return TRUE; | |
2135 } | |
2136 | |
10425 | 2137 xmlnode * |
2138 gaim_util_read_xml_from_file(const char *filename, const char *description) | |
2139 { | |
2140 const char *user_dir = gaim_user_dir(); | |
2141 gchar *filename_full; | |
2142 GError *error; | |
2143 gchar *contents = NULL; | |
2144 gsize length; | |
2145 xmlnode *node = NULL; | |
2146 | |
2147 g_return_val_if_fail(user_dir != NULL, NULL); | |
2148 | |
2149 gaim_debug_info("util", "Reading file %s from directory %s\n", | |
2150 filename, user_dir); | |
2151 | |
2152 filename_full = g_build_filename(user_dir, filename, NULL); | |
2153 | |
2154 if (!g_file_test(filename_full, G_FILE_TEST_EXISTS)) | |
2155 { | |
2156 gaim_debug_info("util", "File %s does not exist (this is not " | |
2157 "necessarily an error)\n", filename_full); | |
2158 g_free(filename_full); | |
2159 return NULL; | |
2160 } | |
2161 | |
2162 if (!g_file_get_contents(filename_full, &contents, &length, &error)) | |
2163 { | |
2164 gaim_debug_error("util", "Error reading file %s: %s\n", | |
2165 filename_full, error->message); | |
2166 g_error_free(error); | |
2167 } | |
2168 | |
2169 if ((contents != NULL) && (length > 0)) | |
2170 { | |
2171 node = xmlnode_from_str(contents, length); | |
2172 | |
2173 /* If we were unable to parse the file then save its contents to a backup file */ | |
2174 if (node == NULL) | |
2175 { | |
2176 gchar *filename_temp; | |
2177 | |
2178 filename_temp = g_strdup_printf("%s~", filename); | |
2179 gaim_debug_error("util", "Error parsing file %s. Rrenaming old " | |
2180 "file to %s\n", filename_full, filename_temp); | |
2181 gaim_util_write_data_to_file(filename_temp, contents, length); | |
2182 g_free(filename_temp); | |
2183 } | |
2184 | |
2185 g_free(contents); | |
2186 } | |
2187 | |
2188 /* If we could not parse the file then show the user an error message */ | |
2189 if (node == NULL) | |
2190 { | |
2191 gchar *title, *msg; | |
2192 title = g_strdup_printf(_("Error Reading %s"), filename); | |
2193 msg = g_strdup_printf(_("An error was encountered reading your " | |
2194 "%s. They have not been loaded, and the old file " | |
11493 | 2195 "has been renamed to %s~."), description, filename_full); |
10425 | 2196 gaim_notify_error(NULL, NULL, title, msg); |
2197 g_free(title); | |
2198 g_free(msg); | |
2199 } | |
2200 | |
2201 g_free(filename_full); | |
2202 | |
2203 return node; | |
2204 } | |
2205 | |
10414 | 2206 /* |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2207 * Like mkstemp() but returns a file pointer, uses a pre-set template, |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2208 * uses the semantics of tempnam() for the directory to use and allocates |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2209 * the space for the filepath. |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2210 * |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2211 * Caller is responsible for closing the file and removing it when done, |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2212 * as well as freeing the space pointed-to by "path" with g_free(). |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2213 * |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2214 * Returns NULL on failure and cleans up after itself if so. |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2215 */ |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2216 static const char *gaim_mkstemp_templ = {"gaimXXXXXX"}; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2217 |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2218 FILE * |
10203
7ff9b8b22e7d
[gaim-migrate @ 11324]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
10112
diff
changeset
|
2219 gaim_mkstemp(char **fpath, gboolean binary) |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2220 { |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2221 const gchar *tmpdir; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2222 #ifndef _WIN32 |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2223 int fd; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2224 #endif |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2225 FILE *fp = NULL; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2226 |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2227 g_return_val_if_fail(fpath != NULL, NULL); |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2228 |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2229 if((tmpdir = (gchar*)g_get_tmp_dir()) != NULL) { |
9427 | 2230 if((*fpath = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s", tmpdir, gaim_mkstemp_templ)) != NULL) { |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2231 #ifdef _WIN32 |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2232 char* result = _mktemp( *fpath ); |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2233 if( result == NULL ) |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2234 gaim_debug(GAIM_DEBUG_ERROR, "gaim_mkstemp", |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2235 "Problem creating the template\n"); |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2236 else |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2237 { |
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10569
diff
changeset
|
2238 if( (fp = g_fopen( result, binary?"wb+":"w+")) == NULL ) { |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2239 gaim_debug(GAIM_DEBUG_ERROR, "gaim_mkstemp", |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2240 "Couldn't fopen() %s\n", result); |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2241 } |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2242 } |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2243 #else |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2244 if((fd = mkstemp(*fpath)) == -1) { |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2245 gaim_debug(GAIM_DEBUG_ERROR, "gaim_mkstemp", |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2246 "Couldn't make \"%s\", error: %d\n", |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2247 *fpath, errno); |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2248 } else { |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2249 if((fp = fdopen(fd, "r+")) == NULL) { |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2250 close(fd); |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2251 gaim_debug(GAIM_DEBUG_ERROR, "gaim_mkstemp", |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2252 "Couldn't fdopen(), error: %d\n", errno); |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2253 } |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2254 } |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2255 #endif |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2256 if(!fp) { |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2257 g_free(*fpath); |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2258 *fpath = NULL; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2259 } |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2260 } |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2261 } else { |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2262 gaim_debug(GAIM_DEBUG_ERROR, "gaim_mkstemp", |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2263 "g_get_tmp_dir() failed!"); |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2264 } |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2265 |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2266 return fp; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2267 } |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2268 |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2269 gboolean |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2270 gaim_program_is_valid(const char *program) |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2271 { |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2272 GError *error = NULL; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2273 char **argv; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2274 gchar *progname; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2275 gboolean is_valid = FALSE; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2276 |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2277 g_return_val_if_fail(program != NULL, FALSE); |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2278 g_return_val_if_fail(*program != '\0', FALSE); |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2279 |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2280 if (!g_shell_parse_argv(program, NULL, &argv, &error)) { |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2281 gaim_debug(GAIM_DEBUG_ERROR, "program_is_valid", |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2282 "Could not parse program '%s': %s\n", |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2283 program, error->message); |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2284 g_error_free(error); |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2285 return FALSE; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2286 } |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2287 |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2288 if (argv == NULL) { |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2289 return FALSE; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2290 } |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2291 |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2292 progname = g_find_program_in_path(argv[0]); |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2293 is_valid = (progname != NULL); |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2294 |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2295 g_strfreev(argv); |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2296 g_free(progname); |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2297 |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2298 return is_valid; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2299 } |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2300 |
11878 | 2301 |
2302 gboolean | |
2303 gaim_running_gnome(void) | |
2304 { | |
2305 gchar *tmp = g_find_program_in_path("gnome-open"); | |
12483
54448bd2ccc7
[gaim-migrate @ 14795]
Richard Laager <rlaager@wiktel.com>
parents:
12110
diff
changeset
|
2306 |
54448bd2ccc7
[gaim-migrate @ 14795]
Richard Laager <rlaager@wiktel.com>
parents:
12110
diff
changeset
|
2307 if (tmp == NULL) |
54448bd2ccc7
[gaim-migrate @ 14795]
Richard Laager <rlaager@wiktel.com>
parents:
12110
diff
changeset
|
2308 return FALSE; |
54448bd2ccc7
[gaim-migrate @ 14795]
Richard Laager <rlaager@wiktel.com>
parents:
12110
diff
changeset
|
2309 g_free(tmp); |
54448bd2ccc7
[gaim-migrate @ 14795]
Richard Laager <rlaager@wiktel.com>
parents:
12110
diff
changeset
|
2310 |
54448bd2ccc7
[gaim-migrate @ 14795]
Richard Laager <rlaager@wiktel.com>
parents:
12110
diff
changeset
|
2311 return (g_getenv("GNOME_DESKTOP_SESSION_ID") != NULL); |
54448bd2ccc7
[gaim-migrate @ 14795]
Richard Laager <rlaager@wiktel.com>
parents:
12110
diff
changeset
|
2312 } |
54448bd2ccc7
[gaim-migrate @ 14795]
Richard Laager <rlaager@wiktel.com>
parents:
12110
diff
changeset
|
2313 |
54448bd2ccc7
[gaim-migrate @ 14795]
Richard Laager <rlaager@wiktel.com>
parents:
12110
diff
changeset
|
2314 gboolean |
54448bd2ccc7
[gaim-migrate @ 14795]
Richard Laager <rlaager@wiktel.com>
parents:
12110
diff
changeset
|
2315 gaim_running_kde(void) |
54448bd2ccc7
[gaim-migrate @ 14795]
Richard Laager <rlaager@wiktel.com>
parents:
12110
diff
changeset
|
2316 { |
54448bd2ccc7
[gaim-migrate @ 14795]
Richard Laager <rlaager@wiktel.com>
parents:
12110
diff
changeset
|
2317 gchar *tmp = g_find_program_in_path("kfmclient"); |
54448bd2ccc7
[gaim-migrate @ 14795]
Richard Laager <rlaager@wiktel.com>
parents:
12110
diff
changeset
|
2318 const char *session; |
54448bd2ccc7
[gaim-migrate @ 14795]
Richard Laager <rlaager@wiktel.com>
parents:
12110
diff
changeset
|
2319 |
54448bd2ccc7
[gaim-migrate @ 14795]
Richard Laager <rlaager@wiktel.com>
parents:
12110
diff
changeset
|
2320 if (tmp == NULL) |
54448bd2ccc7
[gaim-migrate @ 14795]
Richard Laager <rlaager@wiktel.com>
parents:
12110
diff
changeset
|
2321 return FALSE; |
54448bd2ccc7
[gaim-migrate @ 14795]
Richard Laager <rlaager@wiktel.com>
parents:
12110
diff
changeset
|
2322 g_free(tmp); |
54448bd2ccc7
[gaim-migrate @ 14795]
Richard Laager <rlaager@wiktel.com>
parents:
12110
diff
changeset
|
2323 |
54448bd2ccc7
[gaim-migrate @ 14795]
Richard Laager <rlaager@wiktel.com>
parents:
12110
diff
changeset
|
2324 session = g_getenv("KDE_FULL_SESSION"); |
54448bd2ccc7
[gaim-migrate @ 14795]
Richard Laager <rlaager@wiktel.com>
parents:
12110
diff
changeset
|
2325 if (session != NULL && !strcmp(session, "true")) |
11878 | 2326 return TRUE; |
12483
54448bd2ccc7
[gaim-migrate @ 14795]
Richard Laager <rlaager@wiktel.com>
parents:
12110
diff
changeset
|
2327 |
54448bd2ccc7
[gaim-migrate @ 14795]
Richard Laager <rlaager@wiktel.com>
parents:
12110
diff
changeset
|
2328 /* If you run Gaim from Konsole under !KDE, this will provide a |
54448bd2ccc7
[gaim-migrate @ 14795]
Richard Laager <rlaager@wiktel.com>
parents:
12110
diff
changeset
|
2329 * a false positive. Since we do the GNOME checks first, this is |
54448bd2ccc7
[gaim-migrate @ 14795]
Richard Laager <rlaager@wiktel.com>
parents:
12110
diff
changeset
|
2330 * only a problem if you're running something !(KDE || GNOME) and |
54448bd2ccc7
[gaim-migrate @ 14795]
Richard Laager <rlaager@wiktel.com>
parents:
12110
diff
changeset
|
2331 * you run Gaim from Konsole. This really shouldn't be a problem. */ |
54448bd2ccc7
[gaim-migrate @ 14795]
Richard Laager <rlaager@wiktel.com>
parents:
12110
diff
changeset
|
2332 return ((g_getenv("KDEDIR") != NULL) || g_getenv("KDEDIRS") != NULL); |
11878 | 2333 } |
2334 | |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2335 char * |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2336 gaim_fd_get_ip(int fd) |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2337 { |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2338 struct sockaddr addr; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2339 socklen_t namelen = sizeof(addr); |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2340 |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2341 g_return_val_if_fail(fd != 0, NULL); |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2342 |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2343 if (getsockname(fd, &addr, &namelen)) |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2344 return NULL; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2345 |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2346 return g_strdup(inet_ntoa(((struct sockaddr_in *)&addr)->sin_addr)); |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2347 } |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2348 |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2349 |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2350 /************************************************************************** |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2351 * String Functions |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2352 **************************************************************************/ |
7162 | 2353 const char * |
11153 | 2354 gaim_normalize(const GaimAccount *account, const char *str) |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2355 { |
7261 | 2356 GaimPlugin *prpl = NULL; |
2357 GaimPluginProtocolInfo *prpl_info = NULL; | |
7430 | 2358 const char *ret = NULL; |
7261 | 2359 |
2360 if(account) | |
7956 | 2361 prpl = gaim_find_prpl(gaim_account_get_protocol_id(account)); |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2362 |
7261 | 2363 if(prpl) |
2364 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(prpl); | |
2365 | |
7430 | 2366 if(prpl_info && prpl_info->normalize) |
11153 | 2367 ret = prpl_info->normalize(account, str); |
2368 | |
2369 if(!ret) | |
2370 { | |
7261 | 2371 static char buf[BUF_LEN]; |
2372 char *tmp; | |
11153 | 2373 |
2374 tmp = g_utf8_normalize(str, -1, G_NORMALIZE_DEFAULT); | |
7261 | 2375 g_snprintf(buf, sizeof(buf), "%s", tmp); |
2376 g_free(tmp); | |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2377 |
7430 | 2378 ret = buf; |
7261 | 2379 } |
11153 | 2380 |
7430 | 2381 return ret; |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2382 } |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2383 |
11153 | 2384 /* |
2385 * You probably don't want to call this directly, it is | |
2386 * mainly for use as a PRPL callback function. See the | |
2387 * comments in util.h. | |
2388 */ | |
2389 const char * | |
2390 gaim_normalize_nocase(const GaimAccount *account, const char *str) | |
2391 { | |
2392 static char buf[BUF_LEN]; | |
2393 char *tmp1, *tmp2; | |
2394 | |
2395 g_return_val_if_fail(str != NULL, NULL); | |
2396 | |
2397 tmp1 = g_utf8_strdown(str, -1); | |
2398 tmp2 = g_utf8_normalize(tmp1, -1, G_NORMALIZE_DEFAULT); | |
2399 g_snprintf(buf, sizeof(buf), "%s", tmp2); | |
2400 g_free(tmp2); | |
2401 g_free(tmp1); | |
2402 | |
2403 return buf; | |
2404 } | |
2405 | |
8700 | 2406 gchar * |
2407 gaim_str_sub_away_formatters(const char *str, const char *name) | |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2408 { |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2409 char *c; |
11237 | 2410 GString *cpy; |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2411 time_t t; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2412 struct tm *tme; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2413 char tmp[20]; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2414 |
8700 | 2415 g_return_val_if_fail(str != NULL, NULL); |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2416 g_return_val_if_fail(name != NULL, NULL); |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2417 |
11237 | 2418 /* Create an empty GString that is hopefully big enough for most messages */ |
2419 cpy = g_string_sized_new(1024); | |
8700 | 2420 |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2421 t = time(NULL); |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2422 tme = localtime(&t); |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2423 |
8700 | 2424 c = (char *)str; |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2425 while (*c) { |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2426 switch (*c) { |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2427 case '%': |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2428 if (*(c + 1)) { |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2429 switch (*(c + 1)) { |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2430 case 'n': |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2431 /* append name */ |
11237 | 2432 g_string_append(cpy, name); |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2433 c++; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2434 break; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2435 case 'd': |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2436 /* append date */ |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2437 strftime(tmp, 20, "%m/%d/%Y", tme); |
11237 | 2438 g_string_append(cpy, tmp); |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2439 c++; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2440 break; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2441 case 't': |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2442 /* append time */ |
7459
825351e3b01b
[gaim-migrate @ 8072]
Christian Hammond <chipx86@chipx86.com>
parents:
7430
diff
changeset
|
2443 strftime(tmp, 20, "%I:%M:%S %p", tme); |
11237 | 2444 g_string_append(cpy, tmp); |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2445 c++; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2446 break; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2447 default: |
11237 | 2448 g_string_append_c(cpy, *c); |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2449 } |
10614 | 2450 } else { |
11237 | 2451 g_string_append_c(cpy, *c); |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2452 } |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2453 break; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2454 default: |
11237 | 2455 g_string_append_c(cpy, *c); |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2456 } |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2457 c++; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2458 } |
11237 | 2459 |
2460 return g_string_free(cpy, FALSE); | |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2461 } |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2462 |
8341 | 2463 gchar * |
2464 gaim_strdup_withhtml(const gchar *src) | |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2465 { |
8341 | 2466 gulong destsize, i, j; |
2467 gchar *dest; | |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2468 |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2469 g_return_val_if_fail(src != NULL, NULL); |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2470 |
8341 | 2471 /* New length is (length of src) + (number of \n's * 3) + 1 */ |
2472 for (i = 0, j = 0; src[i] != '\0'; i++) | |
2473 if (src[i] == '\n') | |
2474 j++; | |
2475 | |
2476 destsize = i + (j * 3) + 1; | |
2477 dest = g_malloc(destsize); | |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2478 |
8341 | 2479 /* Copy stuff, ignoring \r's, because they are dumb */ |
2480 for (i = 0, j = 0; src[i] != '\0'; i++) { | |
2481 if (src[i] == '\n') { | |
2482 strcpy(&dest[j], "<BR>"); | |
2483 j += 4; | |
2484 } else if (src[i] != '\r') | |
2485 dest[j++] = src[i]; | |
2486 } | |
2487 | |
2488 dest[destsize-1] = '\0'; | |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2489 |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2490 return dest; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2491 } |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2492 |
7628 | 2493 gboolean |
2494 gaim_str_has_prefix(const char *s, const char *p) | |
2495 { | |
11780 | 2496 #if GLIB_CHECK_VERSION(2,2,0) |
2497 return g_str_has_prefix(s, p); | |
2498 #else | |
7628 | 2499 if (!strncmp(s, p, strlen(p))) |
2500 return TRUE; | |
2501 | |
2502 return FALSE; | |
11780 | 2503 #endif |
7628 | 2504 } |
2505 | |
2506 gboolean | |
2507 gaim_str_has_suffix(const char *s, const char *x) | |
2508 { | |
11780 | 2509 #if GLIB_CHECK_VERSION(2,2,0) |
2510 return g_str_has_suffix(s, x); | |
2511 #else | |
7628 | 2512 int off = strlen(s) - strlen(x); |
2513 | |
2514 if (off >= 0 && !strcmp(s + off, x)) | |
2515 return TRUE; | |
2516 | |
2517 return FALSE; | |
11780 | 2518 #endif |
7628 | 2519 } |
2520 | |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2521 char * |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2522 gaim_str_add_cr(const char *text) |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2523 { |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2524 char *ret = NULL; |
7631 | 2525 int count = 0, j; |
2526 guint i; | |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2527 |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2528 g_return_val_if_fail(text != NULL, NULL); |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2529 |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2530 if (text[0] == '\n') |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2531 count++; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2532 for (i = 1; i < strlen(text); i++) |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2533 if (text[i] == '\n' && text[i - 1] != '\r') |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2534 count++; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2535 |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2536 if (count == 0) |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2537 return g_strdup(text); |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2538 |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2539 ret = g_malloc0(strlen(text) + count + 1); |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2540 |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2541 i = 0; j = 0; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2542 if (text[i] == '\n') |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2543 ret[j++] = '\r'; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2544 ret[j++] = text[i++]; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2545 for (; i < strlen(text); i++) { |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2546 if (text[i] == '\n' && text[i - 1] != '\r') |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2547 ret[j++] = '\r'; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2548 ret[j++] = text[i]; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2549 } |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2550 |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2551 gaim_debug_misc("gaim_str_add_cr", "got: %s, leaving with %s\n", |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2552 text, ret); |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2553 |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2554 return ret; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2555 } |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2556 |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2557 void |
11920 | 2558 gaim_str_strip_char(char *text, char thechar) |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2559 { |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2560 int i, j; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2561 |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2562 g_return_if_fail(text != NULL); |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2563 |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2564 for (i = 0, j = 0; text[i]; i++) |
11920 | 2565 if (text[i] != thechar) |
11142 | 2566 text[j++] = text[i]; |
2567 | |
2568 text[j++] = '\0'; | |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2569 } |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2570 |
11920 | 2571 void |
2572 gaim_util_chrreplace(char *string, char delimiter, | |
2573 char replacement) | |
2574 { | |
2575 int i = 0; | |
2576 | |
2577 g_return_if_fail(string != NULL); | |
2578 | |
2579 while (string[i] != '\0') | |
2580 { | |
2581 if (string[i] == delimiter) | |
2582 string[i] = replacement; | |
2583 i++; | |
2584 } | |
2585 } | |
2586 | |
8341 | 2587 gchar * |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2588 gaim_strreplace(const char *string, const char *delimiter, |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2589 const char *replacement) |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2590 { |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2591 gchar **split; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2592 gchar *ret; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2593 |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2594 g_return_val_if_fail(string != NULL, NULL); |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2595 g_return_val_if_fail(delimiter != NULL, NULL); |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2596 g_return_val_if_fail(replacement != NULL, NULL); |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2597 |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2598 split = g_strsplit(string, delimiter, 0); |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2599 ret = g_strjoinv(replacement, split); |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2600 g_strfreev(split); |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2601 |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2602 return ret; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2603 } |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2604 |
8341 | 2605 gchar * |
2606 gaim_strcasereplace(const char *string, const char *delimiter, | |
2607 const char *replacement) | |
2608 { | |
2609 gchar *ret; | |
2610 int length_del, length_rep, i, j; | |
2611 | |
2612 g_return_val_if_fail(string != NULL, NULL); | |
2613 g_return_val_if_fail(delimiter != NULL, NULL); | |
2614 g_return_val_if_fail(replacement != NULL, NULL); | |
2615 | |
2616 length_del = strlen(delimiter); | |
2617 length_rep = strlen(replacement); | |
2618 | |
2619 /* Count how many times the delimiter appears */ | |
2620 i = 0; /* position in the source string */ | |
8735
92cbf9713795
[gaim-migrate @ 9490]
Christian Hammond <chipx86@chipx86.com>
parents:
8700
diff
changeset
|
2621 j = 0; /* number of occurrences of "delimiter" */ |
8341 | 2622 while (string[i] != '\0') { |
2623 if (!strncasecmp(&string[i], delimiter, length_del)) { | |
2624 i += length_del; | |
2625 j += length_rep; | |
2626 } else { | |
2627 i++; | |
2628 j++; | |
2629 } | |
2630 } | |
2631 | |
2632 ret = g_malloc(j+1); | |
2633 | |
2634 i = 0; /* position in the source string */ | |
2635 j = 0; /* position in the destination string */ | |
2636 while (string[i] != '\0') { | |
2637 if (!strncasecmp(&string[i], delimiter, length_del)) { | |
2638 strncpy(&ret[j], replacement, length_rep); | |
2639 i += length_del; | |
2640 j += length_rep; | |
2641 } else { | |
2642 ret[j] = string[i]; | |
2643 i++; | |
2644 j++; | |
2645 } | |
2646 } | |
2647 | |
2648 ret[j] = '\0'; | |
2649 | |
2650 return ret; | |
2651 } | |
2652 | |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2653 const char * |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2654 gaim_strcasestr(const char *haystack, const char *needle) |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2655 { |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2656 size_t hlen, nlen; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2657 const char *tmp, *ret; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2658 |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2659 g_return_val_if_fail(haystack != NULL, NULL); |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2660 g_return_val_if_fail(needle != NULL, NULL); |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2661 |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2662 hlen = strlen(haystack); |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2663 nlen = strlen(needle); |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2664 tmp = haystack, |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2665 ret = NULL; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2666 |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2667 g_return_val_if_fail(hlen > 0, NULL); |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2668 g_return_val_if_fail(nlen > 0, NULL); |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2669 |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2670 while (*tmp && !ret) { |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2671 if (!g_ascii_strncasecmp(needle, tmp, nlen)) |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2672 ret = tmp; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2673 else |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2674 tmp++; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2675 } |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2676 |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2677 return ret; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2678 } |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2679 |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2680 char * |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2681 gaim_str_size_to_units(size_t size) |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2682 { |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2683 static const char *size_str[4] = { "bytes", "KB", "MB", "GB" }; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2684 float size_mag; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2685 int size_index = 0; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2686 |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2687 if (size == -1) { |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2688 return g_strdup(_("Calculating...")); |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2689 } |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2690 else if (size == 0) { |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2691 return g_strdup(_("Unknown.")); |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2692 } |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2693 else { |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2694 size_mag = (float)size; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2695 |
10569
12349012610b
[gaim-migrate @ 11957]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10506
diff
changeset
|
2696 while ((size_index < 3) && (size_mag > 1024)) { |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2697 size_mag /= 1024; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2698 size_index++; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2699 } |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2700 |
10103 | 2701 if (size_index == 0) { |
11656
f9c5480ad0ce
[gaim-migrate @ 13940]
Richard Laager <rlaager@wiktel.com>
parents:
11552
diff
changeset
|
2702 return g_strdup_printf("%" G_GSIZE_FORMAT " %s", size, size_str[size_index]); |
10103 | 2703 } else { |
2704 return g_strdup_printf("%.2f %s", size_mag, size_str[size_index]); | |
2705 } | |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2706 } |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2707 } |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2708 |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2709 char * |
9233
0c352d0e4ddc
[gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents:
9230
diff
changeset
|
2710 gaim_str_seconds_to_string(guint secs) |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2711 { |
9233
0c352d0e4ddc
[gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents:
9230
diff
changeset
|
2712 GString *gstr; |
0c352d0e4ddc
[gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents:
9230
diff
changeset
|
2713 const char *prefix = ""; |
0c352d0e4ddc
[gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents:
9230
diff
changeset
|
2714 guint days, hrs, mins; |
0c352d0e4ddc
[gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents:
9230
diff
changeset
|
2715 |
9424 | 2716 gstr = g_string_new(""); |
2717 | |
2718 if (secs < 60) | |
2719 { | |
2720 g_string_append_printf(gstr, "%d %s", secs, | |
2721 ngettext("second", "seconds", secs)); | |
2722 return g_string_free(gstr, FALSE); | |
2723 } | |
2724 | |
9233
0c352d0e4ddc
[gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents:
9230
diff
changeset
|
2725 days = secs / (60 * 60 * 24); |
0c352d0e4ddc
[gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents:
9230
diff
changeset
|
2726 secs = secs % (60 * 60 * 24); |
0c352d0e4ddc
[gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents:
9230
diff
changeset
|
2727 hrs = secs / (60 * 60); |
0c352d0e4ddc
[gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents:
9230
diff
changeset
|
2728 secs = secs % (60 * 60); |
0c352d0e4ddc
[gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents:
9230
diff
changeset
|
2729 mins = secs / 60; |
0c352d0e4ddc
[gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents:
9230
diff
changeset
|
2730 secs = secs % 60; |
0c352d0e4ddc
[gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents:
9230
diff
changeset
|
2731 |
0c352d0e4ddc
[gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents:
9230
diff
changeset
|
2732 if (days > 0) |
0c352d0e4ddc
[gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents:
9230
diff
changeset
|
2733 { |
0c352d0e4ddc
[gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents:
9230
diff
changeset
|
2734 g_string_append_printf(gstr, "%d %s", days, |
0c352d0e4ddc
[gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents:
9230
diff
changeset
|
2735 ngettext("day", "days", days)); |
0c352d0e4ddc
[gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents:
9230
diff
changeset
|
2736 |
0c352d0e4ddc
[gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents:
9230
diff
changeset
|
2737 prefix = ", "; |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2738 } |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2739 |
9233
0c352d0e4ddc
[gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents:
9230
diff
changeset
|
2740 if (hrs > 0) |
0c352d0e4ddc
[gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents:
9230
diff
changeset
|
2741 { |
0c352d0e4ddc
[gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents:
9230
diff
changeset
|
2742 g_string_append_printf(gstr, "%s%d %s", prefix, hrs, |
0c352d0e4ddc
[gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents:
9230
diff
changeset
|
2743 ngettext("hour", "hours", hrs)); |
0c352d0e4ddc
[gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents:
9230
diff
changeset
|
2744 |
0c352d0e4ddc
[gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents:
9230
diff
changeset
|
2745 prefix = ", "; |
0c352d0e4ddc
[gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents:
9230
diff
changeset
|
2746 } |
0c352d0e4ddc
[gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents:
9230
diff
changeset
|
2747 |
0c352d0e4ddc
[gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents:
9230
diff
changeset
|
2748 if (mins > 0) |
0c352d0e4ddc
[gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents:
9230
diff
changeset
|
2749 { |
0c352d0e4ddc
[gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents:
9230
diff
changeset
|
2750 g_string_append_printf(gstr, "%s%d %s", prefix, mins, |
0c352d0e4ddc
[gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents:
9230
diff
changeset
|
2751 ngettext("minute", "minutes", mins)); |
0c352d0e4ddc
[gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents:
9230
diff
changeset
|
2752 } |
0c352d0e4ddc
[gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents:
9230
diff
changeset
|
2753 |
0c352d0e4ddc
[gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents:
9230
diff
changeset
|
2754 return g_string_free(gstr, FALSE); |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2755 } |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2756 |
9277 | 2757 |
2758 char * | |
2759 gaim_str_binary_to_ascii(const unsigned char *binary, guint len) | |
2760 { | |
2761 GString *ret; | |
2762 guint i; | |
2763 | |
2764 g_return_val_if_fail(len > 0, NULL); | |
2765 | |
2766 ret = g_string_sized_new(len); | |
2767 | |
2768 for (i = 0; i < len; i++) | |
2769 if (binary[i] < 32 || binary[i] > 126) | |
2770 g_string_append_printf(ret, "\\x%02hhx", binary[i]); | |
2771 else if (binary[i] == '\\') | |
2772 g_string_append(ret, "\\\\"); | |
2773 else | |
2774 g_string_append_c(ret, binary[i]); | |
2775 | |
2776 return g_string_free(ret, FALSE); | |
2777 } | |
2778 | |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2779 /************************************************************************** |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2780 * URI/URL Functions |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2781 **************************************************************************/ |
7095
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
2782 gboolean |
7094
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2783 gaim_url_parse(const char *url, char **ret_host, int *ret_port, |
9227
9171e528d7e5
[gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents:
9222
diff
changeset
|
2784 char **ret_path, char **ret_user, char **ret_passwd) |
7094
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2785 { |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2786 char scan_info[255]; |
8118 | 2787 char port_str[6]; |
7094
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2788 int f; |
9341 | 2789 const char *at, *slash; |
7094
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2790 const char *turl; |
9227
9171e528d7e5
[gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents:
9222
diff
changeset
|
2791 char host[256], path[256], user[256], passwd[256]; |
7094
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2792 int port = 0; |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2793 /* hyphen at end includes it in control set */ |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2794 static char addr_ctrl[] = "A-Za-z0-9.-"; |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2795 static char port_ctrl[] = "0-9"; |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2796 static char page_ctrl[] = "A-Za-z0-9.~_/:*!@&%%?=+^-"; |
9227
9171e528d7e5
[gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents:
9222
diff
changeset
|
2797 static char user_ctrl[] = "A-Za-z0-9.~_/*!&%%?=+^-"; |
9171e528d7e5
[gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents:
9222
diff
changeset
|
2798 static char passwd_ctrl[] = "A-Za-z0-9.~_/*!&%%?=+^-"; |
7094
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2799 |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2800 g_return_val_if_fail(url != NULL, FALSE); |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2801 |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2802 if ((turl = strstr(url, "http://")) != NULL || |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2803 (turl = strstr(url, "HTTP://")) != NULL) |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2804 { |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2805 turl += 7; |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2806 url = turl; |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2807 } |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2808 |
9227
9171e528d7e5
[gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents:
9222
diff
changeset
|
2809 /* parse out authentication information if supplied */ |
9341 | 2810 /* Only care about @ char BEFORE the first / */ |
2811 at = strchr(url, '@'); | |
2812 slash = strchr(url, '/'); | |
2813 if ((at != NULL) && | |
2814 (((slash != NULL) && (strlen(at) > strlen(slash))) || | |
2815 (slash == NULL))) { | |
9227
9171e528d7e5
[gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents:
9222
diff
changeset
|
2816 g_snprintf(scan_info, sizeof(scan_info), |
9171e528d7e5
[gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents:
9222
diff
changeset
|
2817 "%%255[%s]:%%255[%s]^@", user_ctrl, passwd_ctrl); |
9171e528d7e5
[gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents:
9222
diff
changeset
|
2818 f = sscanf(url, scan_info, user, passwd); |
9171e528d7e5
[gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents:
9222
diff
changeset
|
2819 |
9171e528d7e5
[gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents:
9222
diff
changeset
|
2820 if (f ==1 ) { |
9171e528d7e5
[gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents:
9222
diff
changeset
|
2821 /* No passwd, possibly just username supplied */ |
9171e528d7e5
[gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents:
9222
diff
changeset
|
2822 g_snprintf(scan_info, sizeof(scan_info), |
9171e528d7e5
[gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents:
9222
diff
changeset
|
2823 "%%255[%s]^@", user_ctrl); |
9171e528d7e5
[gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents:
9222
diff
changeset
|
2824 f = sscanf(url, scan_info, user); |
9171e528d7e5
[gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents:
9222
diff
changeset
|
2825 *passwd = '\0'; |
9171e528d7e5
[gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents:
9222
diff
changeset
|
2826 } |
9171e528d7e5
[gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents:
9222
diff
changeset
|
2827 |
9230 | 2828 url = at+1; /* move pointer after the @ char */ |
9227
9171e528d7e5
[gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents:
9222
diff
changeset
|
2829 } else { |
9171e528d7e5
[gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents:
9222
diff
changeset
|
2830 *user = '\0'; |
9171e528d7e5
[gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents:
9222
diff
changeset
|
2831 *passwd = '\0'; |
9171e528d7e5
[gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents:
9222
diff
changeset
|
2832 } |
9171e528d7e5
[gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents:
9222
diff
changeset
|
2833 |
7094
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2834 g_snprintf(scan_info, sizeof(scan_info), |
8118 | 2835 "%%255[%s]:%%5[%s]/%%255[%s]", addr_ctrl, port_ctrl, page_ctrl); |
7094
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2836 |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2837 f = sscanf(url, scan_info, host, port_str, path); |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2838 |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2839 if (f == 1) |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2840 { |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2841 g_snprintf(scan_info, sizeof(scan_info), |
8118 | 2842 "%%255[%s]/%%255[%s]", |
7094
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2843 addr_ctrl, page_ctrl); |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2844 f = sscanf(url, scan_info, host, path); |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2845 g_snprintf(port_str, sizeof(port_str), "80"); |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2846 } |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2847 |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2848 if (f == 1) |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2849 *path = '\0'; |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2850 |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2851 sscanf(port_str, "%d", &port); |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2852 |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2853 if (ret_host != NULL) *ret_host = g_strdup(host); |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2854 if (ret_port != NULL) *ret_port = port; |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2855 if (ret_path != NULL) *ret_path = g_strdup(path); |
9227
9171e528d7e5
[gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents:
9222
diff
changeset
|
2856 if (ret_user != NULL) *ret_user = g_strdup(user); |
9171e528d7e5
[gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents:
9222
diff
changeset
|
2857 if (ret_passwd != NULL) *ret_passwd = g_strdup(passwd); |
7094
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2858 |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2859 return TRUE; |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2860 } |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2861 |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2862 static void |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2863 destroy_fetch_url_data(GaimFetchUrlData *gfud) |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2864 { |
12887
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
2865 g_free(gfud->webdata); |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
2866 g_free(gfud->url); |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
2867 g_free(gfud->user_agent); |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
2868 g_free(gfud->website.address); |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
2869 g_free(gfud->website.page); |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
2870 g_free(gfud->website.user); |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
2871 g_free(gfud->website.passwd); |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
2872 g_free(gfud->request); |
7094
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2873 |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2874 g_free(gfud); |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2875 } |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2876 |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2877 static gboolean |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2878 parse_redirect(const char *data, size_t data_len, gint sock, |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2879 GaimFetchUrlData *gfud) |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2880 { |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2881 gchar *s; |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2882 |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2883 if ((s = g_strstr_len(data, data_len, "Location: ")) != NULL) |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2884 { |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2885 gchar *new_url, *temp_url, *end; |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2886 gboolean full; |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2887 int len; |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2888 |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2889 s += strlen("Location: "); |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2890 end = strchr(s, '\r'); |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2891 |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2892 /* Just in case :) */ |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2893 if (end == NULL) |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2894 end = strchr(s, '\n'); |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2895 |
10623 | 2896 if (end == NULL) |
2897 return FALSE; | |
2898 | |
7094
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2899 len = end - s; |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2900 |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2901 new_url = g_malloc(len + 1); |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2902 strncpy(new_url, s, len); |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2903 new_url[len] = '\0'; |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2904 |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2905 full = gfud->full; |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2906 |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2907 if (*new_url == '/' || g_strstr_len(new_url, len, "://") == NULL) |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2908 { |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2909 temp_url = new_url; |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2910 |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2911 new_url = g_strdup_printf("%s:%d%s", gfud->website.address, |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2912 gfud->website.port, temp_url); |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2913 |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2914 g_free(temp_url); |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2915 |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2916 full = FALSE; |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2917 } |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2918 |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2919 /* Close the existing stuff. */ |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2920 gaim_input_remove(gfud->inpa); |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2921 close(sock); |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2922 |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2923 gaim_debug_info("gaim_url_fetch", "Redirecting to %s\n", new_url); |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2924 |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2925 /* Try again, with this new location. */ |
12887
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
2926 gaim_url_fetch_request(new_url, full, gfud->user_agent, |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
2927 gfud->http11, gfud->request, gfud->include_headers, |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
2928 gfud->callback, gfud->user_data); |
7094
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2929 |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2930 /* Free up. */ |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2931 g_free(new_url); |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2932 destroy_fetch_url_data(gfud); |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2933 |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2934 return TRUE; |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2935 } |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2936 |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2937 return FALSE; |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2938 } |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2939 |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2940 static size_t |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2941 parse_content_len(const char *data, size_t data_len) |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2942 { |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2943 size_t content_len = 0; |
9240 | 2944 const char *p = NULL; |
2945 | |
2946 /* This is still technically wrong, since headers are case-insensitive | |
2947 * [RFC 2616, section 4.2], though this ought to catch the normal case. | |
2948 * Note: data is _not_ nul-terminated. | |
2949 */ | |
2950 if (data_len > 16) { | |
12887
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
2951 p = strncmp(data, "Content-Length: ", 16) == 0 ? data : NULL; |
9240 | 2952 if (!p) { |
2953 p = g_strstr_len(data, data_len, "\nContent-Length: "); | |
2954 if (p) | |
2955 p += 1; | |
2956 } | |
12887
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
2957 if (!p) |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
2958 p = (strncmp(data, "CONTENT-LENGTH: ", 16) == 0) |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
2959 ? data : NULL; |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
2960 if (!p) { |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
2961 p = g_strstr_len(data, data_len, "\nContent-Length: "); |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
2962 if (p) |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
2963 p++; |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
2964 } |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
2965 if (!p) { |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
2966 p = g_strstr_len(data, data_len, "\nCONTENT-LENGTH: "); |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
2967 if (p) |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
2968 p++; |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
2969 } |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
2970 |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
2971 if (p) |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
2972 p += 16; |
9240 | 2973 } |
2974 | |
2975 /* If we can find a Content-Length header at all, try to sscanf it. | |
2976 * Response headers should end with at least \r\n, so sscanf is safe, | |
2977 * if we make sure that there is indeed a \n in our header. | |
2978 */ | |
2979 if (p && g_strstr_len(p, data_len - (p - data), "\n")) { | |
12887
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
2980 sscanf(p, "%" G_GSIZE_FORMAT, &content_len); |
9818 | 2981 gaim_debug_misc("parse_content_len", "parsed %u\n", content_len); |
9240 | 2982 } |
7094
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2983 |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2984 return content_len; |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2985 } |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2986 |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2987 static void |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2988 url_fetched_cb(gpointer url_data, gint sock, GaimInputCondition cond) |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2989 { |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2990 GaimFetchUrlData *gfud = url_data; |
12109
936e3b7cd864
[gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11920
diff
changeset
|
2991 int len; |
936e3b7cd864
[gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11920
diff
changeset
|
2992 char buf[4096]; |
936e3b7cd864
[gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11920
diff
changeset
|
2993 char *data_cursor; |
9240 | 2994 gboolean got_eof = FALSE; |
7094
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2995 |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2996 if (sock == -1) |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2997 { |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2998 gfud->callback(gfud->user_data, NULL, 0); |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2999 |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3000 destroy_fetch_url_data(gfud); |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3001 |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3002 return; |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3003 } |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3004 |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3005 if (!gfud->sentreq) |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3006 { |
12887
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
3007 char *send; |
7094
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3008 char buf[1024]; |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3009 |
12887
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
3010 if (gfud->request) { |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
3011 send = gfud->request; |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
3012 } else { |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
3013 if (gfud->user_agent) { |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
3014 /* Host header is not forbidden in HTTP/1.0 requests, and HTTP/1.1 |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
3015 * clients must know how to handle the "chunked" transfer encoding. |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
3016 * Gaim doesn't know how to handle "chunked", so should always send |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
3017 * the Host header regardless, to get around some observed problems |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
3018 */ |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
3019 g_snprintf(buf, sizeof(buf), |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
3020 "GET %s%s HTTP/%s\r\n" |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
3021 "Connection: close\r\n" |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
3022 "User-Agent: %s\r\n" |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
3023 "Host: %s\r\n\r\n", |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
3024 (gfud->full ? "" : "/"), |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
3025 (gfud->full ? gfud->url : gfud->website.page), |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
3026 (gfud->http11 ? "1.1" : "1.0"), |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
3027 gfud->user_agent, gfud->website.address); |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
3028 } else { |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
3029 g_snprintf(buf, sizeof(buf), |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
3030 "GET %s%s HTTP/%s\r\n" |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
3031 "Connection: close\r\n" |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
3032 "Host: %s\r\n\r\n", |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
3033 (gfud->full ? "" : "/"), |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
3034 (gfud->full ? gfud->url : gfud->website.page), |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
3035 (gfud->http11 ? "1.1" : "1.0"), |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
3036 gfud->website.address); |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
3037 } |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
3038 send = buf; |
7094
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3039 } |
12887
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
3040 |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
3041 gaim_debug_misc("gaim_url_fetch", "Request: %s\n", send); |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
3042 |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
3043 write(sock, send, strlen(send)); |
7094
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3044 fcntl(sock, F_SETFL, O_NONBLOCK); |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3045 gfud->sentreq = TRUE; |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3046 gfud->inpa = gaim_input_add(sock, GAIM_INPUT_READ, |
12887
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
3047 url_fetched_cb, url_data); |
7094
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3048 gfud->data_len = 4096; |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3049 gfud->webdata = g_malloc(gfud->data_len); |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3050 |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3051 return; |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3052 } |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3053 |
12109
936e3b7cd864
[gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11920
diff
changeset
|
3054 while ((len = read(sock, buf, sizeof(buf))) > 0) |
7094
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3055 { |
12109
936e3b7cd864
[gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11920
diff
changeset
|
3056 /* If we've filled up our butfer, make it bigger */ |
936e3b7cd864
[gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11920
diff
changeset
|
3057 if ((gfud->len + len) >= gfud->data_len) |
7094
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3058 { |
12109
936e3b7cd864
[gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11920
diff
changeset
|
3059 gfud->data_len += MAX(((gfud->data_len) / 2), sizeof(buf)); |
7094
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3060 |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3061 gfud->webdata = g_realloc(gfud->webdata, gfud->data_len); |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3062 } |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3063 |
12109
936e3b7cd864
[gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11920
diff
changeset
|
3064 data_cursor = gfud->webdata + gfud->len; |
936e3b7cd864
[gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11920
diff
changeset
|
3065 |
936e3b7cd864
[gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11920
diff
changeset
|
3066 gfud->len += len; |
936e3b7cd864
[gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11920
diff
changeset
|
3067 |
936e3b7cd864
[gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11920
diff
changeset
|
3068 memcpy(data_cursor, buf, len); |
936e3b7cd864
[gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11920
diff
changeset
|
3069 |
11407 | 3070 gfud->webdata[gfud->len] = '\0'; |
7094
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3071 |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3072 if (!gfud->startsaving) |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3073 { |
12109
936e3b7cd864
[gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11920
diff
changeset
|
3074 char *tmp; |
936e3b7cd864
[gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11920
diff
changeset
|
3075 |
936e3b7cd864
[gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11920
diff
changeset
|
3076 /** See if we've reached the end of the headers yet */ |
936e3b7cd864
[gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11920
diff
changeset
|
3077 if ((tmp = strstr(gfud->webdata, "\r\n\r\n"))) { |
936e3b7cd864
[gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11920
diff
changeset
|
3078 char * new_data; |
936e3b7cd864
[gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11920
diff
changeset
|
3079 guint header_len = (tmp + 4 - gfud->webdata); |
936e3b7cd864
[gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11920
diff
changeset
|
3080 size_t content_len, body_len = 0; |
936e3b7cd864
[gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11920
diff
changeset
|
3081 |
936e3b7cd864
[gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11920
diff
changeset
|
3082 gaim_debug_misc("gaim_url_fetch", "Response headers: '%.*s'\n", |
936e3b7cd864
[gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11920
diff
changeset
|
3083 header_len, gfud->webdata); |
936e3b7cd864
[gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11920
diff
changeset
|
3084 |
936e3b7cd864
[gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11920
diff
changeset
|
3085 /* See if we can find a redirect. */ |
936e3b7cd864
[gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11920
diff
changeset
|
3086 if (parse_redirect(gfud->webdata, header_len, sock, gfud)) |
936e3b7cd864
[gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11920
diff
changeset
|
3087 return; |
936e3b7cd864
[gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11920
diff
changeset
|
3088 |
936e3b7cd864
[gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11920
diff
changeset
|
3089 gfud->startsaving = TRUE; |
936e3b7cd864
[gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11920
diff
changeset
|
3090 |
936e3b7cd864
[gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11920
diff
changeset
|
3091 /* No redirect. See if we can find a content length. */ |
936e3b7cd864
[gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11920
diff
changeset
|
3092 content_len = parse_content_len(gfud->webdata, header_len); |
936e3b7cd864
[gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11920
diff
changeset
|
3093 |
936e3b7cd864
[gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11920
diff
changeset
|
3094 if (content_len == 0) |
7094
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3095 { |
12109
936e3b7cd864
[gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11920
diff
changeset
|
3096 /* We'll stick with an initial 8192 */ |
936e3b7cd864
[gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11920
diff
changeset
|
3097 content_len = 8192; |
7094
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3098 } |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3099 else |
12109
936e3b7cd864
[gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11920
diff
changeset
|
3100 { |
936e3b7cd864
[gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11920
diff
changeset
|
3101 gfud->has_explicit_data_len = TRUE; |
936e3b7cd864
[gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11920
diff
changeset
|
3102 } |
936e3b7cd864
[gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11920
diff
changeset
|
3103 |
12887
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
3104 content_len = MAX(content_len, body_len); |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
3105 |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
3106 /* If we're returning the headers too, we don't need to clean them out */ |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
3107 if (gfud->include_headers) { |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
3108 gfud->data_len = content_len + header_len; |
12907
5e338dda872f
[gaim-migrate @ 15260]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12887
diff
changeset
|
3109 } else { |
5e338dda872f
[gaim-migrate @ 15260]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12887
diff
changeset
|
3110 |
5e338dda872f
[gaim-migrate @ 15260]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12887
diff
changeset
|
3111 if (gfud->len > (header_len + 1)) |
5e338dda872f
[gaim-migrate @ 15260]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12887
diff
changeset
|
3112 body_len = (gfud->len - header_len); |
5e338dda872f
[gaim-migrate @ 15260]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12887
diff
changeset
|
3113 |
5e338dda872f
[gaim-migrate @ 15260]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12887
diff
changeset
|
3114 |
5e338dda872f
[gaim-migrate @ 15260]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12887
diff
changeset
|
3115 new_data = g_try_malloc(content_len); |
5e338dda872f
[gaim-migrate @ 15260]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12887
diff
changeset
|
3116 if (new_data == NULL) { |
5e338dda872f
[gaim-migrate @ 15260]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12887
diff
changeset
|
3117 gaim_debug_error("gaim_url_fetch", "Failed to allocate %u bytes: %s\n", |
5e338dda872f
[gaim-migrate @ 15260]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12887
diff
changeset
|
3118 content_len, strerror(errno)); |
5e338dda872f
[gaim-migrate @ 15260]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12887
diff
changeset
|
3119 gaim_input_remove(gfud->inpa); |
5e338dda872f
[gaim-migrate @ 15260]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12887
diff
changeset
|
3120 close(sock); |
5e338dda872f
[gaim-migrate @ 15260]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12887
diff
changeset
|
3121 gfud->callback(gfud->user_data, NULL, 0); |
5e338dda872f
[gaim-migrate @ 15260]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12887
diff
changeset
|
3122 destroy_fetch_url_data(gfud); |
5e338dda872f
[gaim-migrate @ 15260]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12887
diff
changeset
|
3123 |
5e338dda872f
[gaim-migrate @ 15260]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12887
diff
changeset
|
3124 return; |
5e338dda872f
[gaim-migrate @ 15260]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12887
diff
changeset
|
3125 } |
5e338dda872f
[gaim-migrate @ 15260]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12887
diff
changeset
|
3126 |
5e338dda872f
[gaim-migrate @ 15260]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12887
diff
changeset
|
3127 /* We may have read part of the body when reading the headers, don't lose it */ |
5e338dda872f
[gaim-migrate @ 15260]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12887
diff
changeset
|
3128 if (body_len > 0) { |
5e338dda872f
[gaim-migrate @ 15260]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12887
diff
changeset
|
3129 tmp += 4; |
5e338dda872f
[gaim-migrate @ 15260]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12887
diff
changeset
|
3130 memcpy(new_data, tmp, body_len); |
5e338dda872f
[gaim-migrate @ 15260]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12887
diff
changeset
|
3131 } |
5e338dda872f
[gaim-migrate @ 15260]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12887
diff
changeset
|
3132 |
5e338dda872f
[gaim-migrate @ 15260]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12887
diff
changeset
|
3133 /* Out with the old... */ |
5e338dda872f
[gaim-migrate @ 15260]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12887
diff
changeset
|
3134 g_free(gfud->webdata); |
5e338dda872f
[gaim-migrate @ 15260]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12887
diff
changeset
|
3135 gfud->webdata = NULL; |
5e338dda872f
[gaim-migrate @ 15260]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12887
diff
changeset
|
3136 |
5e338dda872f
[gaim-migrate @ 15260]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12887
diff
changeset
|
3137 /* In with the new. */ |
5e338dda872f
[gaim-migrate @ 15260]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12887
diff
changeset
|
3138 gfud->len = body_len; |
5e338dda872f
[gaim-migrate @ 15260]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12887
diff
changeset
|
3139 gfud->data_len = content_len; |
5e338dda872f
[gaim-migrate @ 15260]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12887
diff
changeset
|
3140 gfud->webdata = new_data; |
12887
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
3141 } |
7094
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3142 } |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3143 } |
12109
936e3b7cd864
[gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11920
diff
changeset
|
3144 |
936e3b7cd864
[gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11920
diff
changeset
|
3145 if (gfud->has_explicit_data_len && gfud->len >= gfud->data_len) |
9240 | 3146 { |
3147 got_eof = TRUE; | |
12109
936e3b7cd864
[gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11920
diff
changeset
|
3148 break; |
9240 | 3149 } |
7094
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3150 } |
12109
936e3b7cd864
[gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11920
diff
changeset
|
3151 |
936e3b7cd864
[gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11920
diff
changeset
|
3152 if (len <= 0) { |
936e3b7cd864
[gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11920
diff
changeset
|
3153 if (errno == EWOULDBLOCK) { |
936e3b7cd864
[gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11920
diff
changeset
|
3154 errno = 0; |
936e3b7cd864
[gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11920
diff
changeset
|
3155 return; |
936e3b7cd864
[gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11920
diff
changeset
|
3156 } else if (errno != ETIMEDOUT) { |
936e3b7cd864
[gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11920
diff
changeset
|
3157 got_eof = TRUE; |
936e3b7cd864
[gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11920
diff
changeset
|
3158 } else { |
936e3b7cd864
[gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11920
diff
changeset
|
3159 gaim_input_remove(gfud->inpa); |
936e3b7cd864
[gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11920
diff
changeset
|
3160 close(sock); |
936e3b7cd864
[gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11920
diff
changeset
|
3161 |
936e3b7cd864
[gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11920
diff
changeset
|
3162 gfud->callback(gfud->user_data, NULL, 0); |
936e3b7cd864
[gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11920
diff
changeset
|
3163 |
936e3b7cd864
[gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11920
diff
changeset
|
3164 destroy_fetch_url_data(gfud); |
936e3b7cd864
[gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11920
diff
changeset
|
3165 return; |
936e3b7cd864
[gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11920
diff
changeset
|
3166 } |
9240 | 3167 } |
3168 | |
3169 if (got_eof) { | |
7094
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3170 gfud->webdata = g_realloc(gfud->webdata, gfud->len + 1); |
12109
936e3b7cd864
[gaim-migrate @ 14409]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11920
diff
changeset
|
3171 gfud->webdata[gfud->len] = '\0'; |
7094
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3172 |
9311 | 3173 /* gaim_debug_misc("gaim_url_fetch", "Received: '%s'\n", gfud->webdata); */ |
7094
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3174 |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3175 gaim_input_remove(gfud->inpa); |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3176 close(sock); |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3177 gfud->callback(gfud->user_data, gfud->webdata, gfud->len); |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3178 |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3179 destroy_fetch_url_data(gfud); |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3180 } |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3181 } |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3182 |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3183 void |
12887
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
3184 gaim_url_fetch_request(const char *url, gboolean full, |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
3185 const char *user_agent, gboolean http11, |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
3186 const char *request, gboolean include_headers, |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
3187 GaimURLFetchCallback cb, void *user_data) |
7094
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3188 { |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3189 GaimFetchUrlData *gfud; |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3190 |
9257 | 3191 g_return_if_fail(url != NULL); |
3192 g_return_if_fail(cb != NULL); | |
9311 | 3193 |
9240 | 3194 gaim_debug_info("gaim_url_fetch", |
3195 "requested to fetch (%s), full=%d, user_agent=(%s), http11=%d\n", | |
9257 | 3196 url, full, user_agent?user_agent:"(null)", http11); |
7094
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3197 |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3198 gfud = g_new0(GaimFetchUrlData, 1); |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3199 |
12887
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
3200 gfud->callback = cb; |
7094
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3201 gfud->user_data = user_data; |
12887
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
3202 gfud->url = g_strdup(url); |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
3203 gfud->user_agent = user_agent ? g_strdup(user_agent) : NULL; |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
3204 gfud->http11 = http11; |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
3205 gfud->full = full; |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
3206 gfud->request = request ? g_strdup(request) : NULL; |
4229503f1cd9
[gaim-migrate @ 15240]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12880
diff
changeset
|
3207 gfud->include_headers = include_headers; |
7094
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3208 |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3209 gaim_url_parse(url, &gfud->website.address, &gfud->website.port, |
9227
9171e528d7e5
[gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents:
9222
diff
changeset
|
3210 &gfud->website.page, &gfud->website.user, &gfud->website.passwd); |
7094
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3211 |
12802
26b31b4c43a2
[gaim-migrate @ 15149]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12640
diff
changeset
|
3212 if (gaim_proxy_connect(NULL, gfud->website.address, |
7094
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3213 gfud->website.port, url_fetched_cb, |
12802
26b31b4c43a2
[gaim-migrate @ 15149]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
12640
diff
changeset
|
3214 gfud) != 0) |
7094
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3215 { |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3216 destroy_fetch_url_data(gfud); |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3217 |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3218 cb(user_data, g_strdup(_("g003: Error opening connection.\n")), 0); |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3219 } |
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3220 } |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3221 |
7162 | 3222 const char * |
7134 | 3223 gaim_url_decode(const char *str) |
3224 { | |
3225 static char buf[BUF_LEN]; | |
7631 | 3226 guint i, j = 0; |
7134 | 3227 char *bum; |
9804 | 3228 char hex[3]; |
7134 | 3229 |
3230 g_return_val_if_fail(str != NULL, NULL); | |
3231 | |
9804 | 3232 /* |
3233 * XXX - This check could be removed and buf could be made | |
3234 * dynamically allocated, but this is easier. | |
3235 */ | |
3236 if (strlen(str) >= BUF_LEN) | |
3237 return NULL; | |
3238 | |
7134 | 3239 for (i = 0; i < strlen(str); i++) { |
3240 | |
3241 if (str[i] != '%') | |
3242 buf[j++] = str[i]; | |
3243 else { | |
3244 strncpy(hex, str + ++i, 2); | |
3245 hex[2] = '\0'; | |
3246 | |
3247 /* i is pointing to the start of the number */ | |
3248 i++; | |
3249 | |
3250 /* | |
3251 * Now it's at the end and at the start of the for loop | |
3252 * will be at the next character. | |
3253 */ | |
3254 buf[j++] = strtol(hex, NULL, 16); | |
3255 } | |
3256 } | |
3257 | |
3258 buf[j] = '\0'; | |
3259 | |
3260 if (!g_utf8_validate(buf, -1, (const char **)&bum)) | |
3261 *bum = '\0'; | |
3262 | |
3263 return buf; | |
3264 } | |
3265 | |
7162 | 3266 const char * |
7134 | 3267 gaim_url_encode(const char *str) |
3268 { | |
10700
9505695acff8
[gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10636
diff
changeset
|
3269 const char *iter; |
7134 | 3270 static char buf[BUF_LEN]; |
10700
9505695acff8
[gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10636
diff
changeset
|
3271 char utf_char[6]; |
7631 | 3272 guint i, j = 0; |
7134 | 3273 |
3274 g_return_val_if_fail(str != NULL, NULL); | |
10700
9505695acff8
[gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10636
diff
changeset
|
3275 g_return_val_if_fail(g_utf8_validate(str, -1, NULL), NULL); |
9505695acff8
[gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10636
diff
changeset
|
3276 |
9505695acff8
[gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10636
diff
changeset
|
3277 iter = str; |
9505695acff8
[gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10636
diff
changeset
|
3278 for (; *iter && j < (BUF_LEN - 1) ; iter = g_utf8_next_char(iter)) { |
9505695acff8
[gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10636
diff
changeset
|
3279 gunichar c = g_utf8_get_char(iter); |
10800 | 3280 /* If the character is an ASCII character and is alphanumeric |
3281 * no need to escape */ | |
3282 if (c < 128 && isalnum(c)) { | |
10700
9505695acff8
[gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10636
diff
changeset
|
3283 buf[j++] = c; |
9505695acff8
[gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10636
diff
changeset
|
3284 } else { |
9505695acff8
[gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10636
diff
changeset
|
3285 int bytes = g_unichar_to_utf8(c, utf_char); |
9505695acff8
[gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10636
diff
changeset
|
3286 for (i = 0; i < bytes; i++) { |
9505695acff8
[gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10636
diff
changeset
|
3287 if (j > (BUF_LEN - 4)) |
9505695acff8
[gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10636
diff
changeset
|
3288 break; |
9505695acff8
[gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10636
diff
changeset
|
3289 sprintf(buf + j, "%%%02x", utf_char[i] & 0xff); |
9505695acff8
[gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10636
diff
changeset
|
3290 j += 3; |
9505695acff8
[gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10636
diff
changeset
|
3291 } |
7134 | 3292 } |
3293 } | |
3294 | |
3295 buf[j] = '\0'; | |
3296 | |
3297 return buf; | |
3298 } | |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3299 |
9054 | 3300 /* Originally lifted from |
3301 * http://www.oreillynet.com/pub/a/network/excerpt/spcookbook_chap03/index3.html | |
3302 * ... and slightly modified to be a bit more rfc822 compliant | |
3303 * ... and modified a bit more to make domain checking rfc1035 compliant | |
3304 * with the exception permitted in rfc1101 for domains to start with digit | |
3305 * but not completely checking to avoid conflicts with IP addresses | |
3306 */ | |
9045 | 3307 gboolean |
3308 gaim_email_is_valid(const char *address) | |
3309 { | |
3310 const char *c, *domain; | |
3311 static char *rfc822_specials = "()<>@,;:\\\"[]"; | |
3312 | |
9054 | 3313 /* first we validate the name portion (name@domain) (rfc822)*/ |
9045 | 3314 for (c = address; *c; c++) { |
3315 if (*c == '\"' && (c == address || *(c - 1) == '.' || *(c - 1) == '\"')) { | |
3316 while (*++c) { | |
9054 | 3317 if (*c == '\\') { |
3318 if (*c++ && *c < 127 && *c != '\n' && *c != '\r') continue; | |
3319 else return FALSE; | |
3320 } | |
9045 | 3321 if (*c == '\"') break; |
9054 | 3322 if (*c < ' ' || *c >= 127) return FALSE; |
9045 | 3323 } |
3324 if (!*c++) return FALSE; | |
3325 if (*c == '@') break; | |
3326 if (*c != '.') return FALSE; | |
3327 continue; | |
3328 } | |
3329 if (*c == '@') break; | |
3330 if (*c <= ' ' || *c >= 127) return FALSE; | |
3331 if (strchr(rfc822_specials, *c)) return FALSE; | |
3332 } | |
9054 | 3333 /* strictly we should return false if (*(c - 1) == '.') too, but I think |
3334 * we should permit user.@domain type addresses - they do work :) */ | |
3335 if (c == address) return FALSE; | |
3336 | |
3337 /* next we validate the domain portion (name@domain) (rfc1035 & rfc1011) */ | |
9045 | 3338 if (!*(domain = ++c)) return FALSE; |
3339 do { | |
9054 | 3340 if (*c == '.' && (c == domain || *(c - 1) == '.' || *(c - 1) == '-')) |
3341 return FALSE; | |
3342 if (*c == '-' && *(c - 1) == '.') return FALSE; | |
3343 if ((*c < '0' && *c != '-' && *c != '.') || (*c > '9' && *c < 'A') || | |
3344 (*c > 'Z' && *c < 'a') || (*c > 'z')) return FALSE; | |
9045 | 3345 } while (*++c); |
3346 | |
9054 | 3347 if (*(c - 1) == '-') return FALSE; |
3348 | |
3349 return ((c - domain) > 3 ? TRUE : FALSE); | |
9045 | 3350 } |
3351 | |
11135 | 3352 /* Stolen from gnome_uri_list_extract_uris */ |
3353 GList * | |
3354 gaim_uri_list_extract_uris(const gchar *uri_list) | |
3355 { | |
9670 | 3356 const gchar *p, *q; |
3357 gchar *retval; | |
3358 GList *result = NULL; | |
3359 | |
3360 g_return_val_if_fail (uri_list != NULL, NULL); | |
3361 | |
3362 p = uri_list; | |
3363 | |
3364 /* We don't actually try to validate the URI according to RFC | |
3365 * 2396, or even check for allowed characters - we just ignore | |
3366 * comments and trim whitespace off the ends. We also | |
3367 * allow LF delimination as well as the specified CRLF. | |
3368 */ | |
3369 while (p) { | |
3370 if (*p != '#') { | |
3371 while (isspace(*p)) | |
3372 p++; | |
3373 | |
3374 q = p; | |
3375 while (*q && (*q != '\n') && (*q != '\r')) | |
3376 q++; | |
3377 | |
3378 if (q > p) { | |
3379 q--; | |
3380 while (q > p && isspace(*q)) | |
3381 q--; | |
3382 | |
3383 retval = (gchar*)g_malloc (q - p + 2); | |
3384 strncpy (retval, p, q - p + 1); | |
3385 retval[q - p + 1] = '\0'; | |
3386 | |
3387 result = g_list_prepend (result, retval); | |
3388 } | |
3389 } | |
3390 p = strchr (p, '\n'); | |
3391 if (p) | |
3392 p++; | |
3393 } | |
3394 | |
3395 return g_list_reverse (result); | |
3396 } | |
3397 | |
3398 | |
11135 | 3399 /* Stolen from gnome_uri_list_extract_filenames */ |
3400 GList * | |
3401 gaim_uri_list_extract_filenames(const gchar *uri_list) | |
3402 { | |
9670 | 3403 GList *tmp_list, *node, *result; |
3404 | |
3405 g_return_val_if_fail (uri_list != NULL, NULL); | |
3406 | |
11135 | 3407 result = gaim_uri_list_extract_uris(uri_list); |
9670 | 3408 |
3409 tmp_list = result; | |
3410 while (tmp_list) { | |
3411 gchar *s = (gchar*)tmp_list->data; | |
3412 | |
3413 node = tmp_list; | |
3414 tmp_list = tmp_list->next; | |
3415 | |
3416 if (!strncmp (s, "file:", 5)) { | |
3417 node->data = g_filename_from_uri (s, NULL, NULL); | |
3418 /* not sure if this fallback is useful at all */ | |
3419 if (!node->data) node->data = g_strdup (s+5); | |
3420 } else { | |
3421 result = g_list_remove_link(result, node); | |
3422 g_list_free_1 (node); | |
3423 } | |
3424 g_free (s); | |
3425 } | |
3426 return result; | |
3427 } | |
9054 | 3428 |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3429 /************************************************************************** |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3430 * UTF8 String Functions |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3431 **************************************************************************/ |
9642 | 3432 gchar * |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3433 gaim_utf8_try_convert(const char *str) |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3434 { |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3435 gsize converted; |
9642 | 3436 gchar *utf8; |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3437 |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3438 g_return_val_if_fail(str != NULL, NULL); |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3439 |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3440 if (g_utf8_validate(str, -1, NULL)) { |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3441 return g_strdup(str); |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3442 } |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3443 |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3444 utf8 = g_locale_to_utf8(str, -1, &converted, NULL, NULL); |
9642 | 3445 if (utf8 != NULL) |
3446 return utf8; | |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3447 |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3448 utf8 = g_convert(str, -1, "UTF-8", "ISO-8859-15", &converted, NULL, NULL); |
9642 | 3449 if ((utf8 != NULL) && (converted == strlen(str))) |
3450 return utf8; | |
3451 | |
3452 if (utf8 != NULL) | |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3453 g_free(utf8); |
9642 | 3454 |
3455 return NULL; | |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3456 } |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3457 |
10258 | 3458 #define utf8_first(x) ((x & 0x80) == 0 || (x & 0xe0) == 0xc0 \ |
3459 || (x & 0xf0) == 0xe0 || (x & 0xf8) == 0xf) | |
3460 gchar * | |
3461 gaim_utf8_salvage(const char *str) | |
3462 { | |
3463 GString *workstr; | |
3464 const char *end; | |
3465 | |
3466 g_return_val_if_fail(str != NULL, NULL); | |
3467 | |
3468 workstr = g_string_sized_new(strlen(str)); | |
3469 | |
3470 do { | |
3471 g_utf8_validate(str, -1, &end); | |
3472 workstr = g_string_append_len(workstr, str, end - str); | |
3473 str = end; | |
3474 if (*str == '\0') | |
3475 break; | |
3476 do { | |
3477 workstr = g_string_append_c(workstr, '?'); | |
3478 str++; | |
3479 } while (!utf8_first(*str)); | |
3480 } while (*str != '\0'); | |
3481 | |
3482 return g_string_free(workstr, FALSE); | |
3483 } | |
3484 | |
12813
547c199072c8
[gaim-migrate @ 15161]
Christopher O'Brien <siege@pidgin.im>
parents:
12802
diff
changeset
|
3485 |
547c199072c8
[gaim-migrate @ 15161]
Christopher O'Brien <siege@pidgin.im>
parents:
12802
diff
changeset
|
3486 char * |
12880
bad785371fa5
[gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents:
12813
diff
changeset
|
3487 gaim_utf8_ncr_encode(const char *str) |
12813
547c199072c8
[gaim-migrate @ 15161]
Christopher O'Brien <siege@pidgin.im>
parents:
12802
diff
changeset
|
3488 { |
547c199072c8
[gaim-migrate @ 15161]
Christopher O'Brien <siege@pidgin.im>
parents:
12802
diff
changeset
|
3489 GString *out; |
547c199072c8
[gaim-migrate @ 15161]
Christopher O'Brien <siege@pidgin.im>
parents:
12802
diff
changeset
|
3490 |
12880
bad785371fa5
[gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents:
12813
diff
changeset
|
3491 g_return_val_if_fail(str != NULL, NULL); |
bad785371fa5
[gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents:
12813
diff
changeset
|
3492 g_return_val_if_fail(g_utf8_validate(str, -1, NULL), NULL); |
12813
547c199072c8
[gaim-migrate @ 15161]
Christopher O'Brien <siege@pidgin.im>
parents:
12802
diff
changeset
|
3493 |
547c199072c8
[gaim-migrate @ 15161]
Christopher O'Brien <siege@pidgin.im>
parents:
12802
diff
changeset
|
3494 out = g_string_new(""); |
547c199072c8
[gaim-migrate @ 15161]
Christopher O'Brien <siege@pidgin.im>
parents:
12802
diff
changeset
|
3495 |
12880
bad785371fa5
[gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents:
12813
diff
changeset
|
3496 for(; *str; str = g_utf8_next_char(str)) { |
bad785371fa5
[gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents:
12813
diff
changeset
|
3497 gunichar wc = g_utf8_get_char(str); |
bad785371fa5
[gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents:
12813
diff
changeset
|
3498 |
bad785371fa5
[gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents:
12813
diff
changeset
|
3499 /* super simple check. hopefully not too wrong. */ |
bad785371fa5
[gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents:
12813
diff
changeset
|
3500 if(wc >= 0x80) { |
12813
547c199072c8
[gaim-migrate @ 15161]
Christopher O'Brien <siege@pidgin.im>
parents:
12802
diff
changeset
|
3501 g_string_append_printf(out, "&#%u;", (guint32) wc); |
547c199072c8
[gaim-migrate @ 15161]
Christopher O'Brien <siege@pidgin.im>
parents:
12802
diff
changeset
|
3502 } else { |
547c199072c8
[gaim-migrate @ 15161]
Christopher O'Brien <siege@pidgin.im>
parents:
12802
diff
changeset
|
3503 g_string_append_unichar(out, wc); |
547c199072c8
[gaim-migrate @ 15161]
Christopher O'Brien <siege@pidgin.im>
parents:
12802
diff
changeset
|
3504 } |
547c199072c8
[gaim-migrate @ 15161]
Christopher O'Brien <siege@pidgin.im>
parents:
12802
diff
changeset
|
3505 } |
547c199072c8
[gaim-migrate @ 15161]
Christopher O'Brien <siege@pidgin.im>
parents:
12802
diff
changeset
|
3506 |
547c199072c8
[gaim-migrate @ 15161]
Christopher O'Brien <siege@pidgin.im>
parents:
12802
diff
changeset
|
3507 return g_string_free(out, FALSE); |
547c199072c8
[gaim-migrate @ 15161]
Christopher O'Brien <siege@pidgin.im>
parents:
12802
diff
changeset
|
3508 } |
547c199072c8
[gaim-migrate @ 15161]
Christopher O'Brien <siege@pidgin.im>
parents:
12802
diff
changeset
|
3509 |
547c199072c8
[gaim-migrate @ 15161]
Christopher O'Brien <siege@pidgin.im>
parents:
12802
diff
changeset
|
3510 |
9161 | 3511 char * |
12880
bad785371fa5
[gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents:
12813
diff
changeset
|
3512 gaim_utf8_ncr_decode(const char *str) |
9161 | 3513 { |
12813
547c199072c8
[gaim-migrate @ 15161]
Christopher O'Brien <siege@pidgin.im>
parents:
12802
diff
changeset
|
3514 GString *out; |
12880
bad785371fa5
[gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents:
12813
diff
changeset
|
3515 char *buf, *b; |
bad785371fa5
[gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents:
12813
diff
changeset
|
3516 |
bad785371fa5
[gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents:
12813
diff
changeset
|
3517 g_return_val_if_fail(str != NULL, NULL); |
bad785371fa5
[gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents:
12813
diff
changeset
|
3518 g_return_val_if_fail(g_utf8_validate(str, -1, NULL), NULL); |
bad785371fa5
[gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents:
12813
diff
changeset
|
3519 |
bad785371fa5
[gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents:
12813
diff
changeset
|
3520 buf = (char *) str; |
12813
547c199072c8
[gaim-migrate @ 15161]
Christopher O'Brien <siege@pidgin.im>
parents:
12802
diff
changeset
|
3521 out = g_string_new(""); |
547c199072c8
[gaim-migrate @ 15161]
Christopher O'Brien <siege@pidgin.im>
parents:
12802
diff
changeset
|
3522 |
12880
bad785371fa5
[gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents:
12813
diff
changeset
|
3523 while( (b = strstr(buf, "&#")) ) { |
bad785371fa5
[gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents:
12813
diff
changeset
|
3524 gunichar wc; |
bad785371fa5
[gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents:
12813
diff
changeset
|
3525 int base = 0; |
bad785371fa5
[gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents:
12813
diff
changeset
|
3526 |
bad785371fa5
[gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents:
12813
diff
changeset
|
3527 /* append everything leading up to the &# */ |
bad785371fa5
[gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents:
12813
diff
changeset
|
3528 g_string_append_len(out, buf, b-buf); |
bad785371fa5
[gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents:
12813
diff
changeset
|
3529 |
bad785371fa5
[gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents:
12813
diff
changeset
|
3530 b += 2; /* skip past the &# */ |
bad785371fa5
[gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents:
12813
diff
changeset
|
3531 |
bad785371fa5
[gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents:
12813
diff
changeset
|
3532 /* strtoul will handle 0x prefix as hex, but not x */ |
bad785371fa5
[gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents:
12813
diff
changeset
|
3533 if(*b == 'x' || *b == 'X') |
bad785371fa5
[gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents:
12813
diff
changeset
|
3534 base = 16; |
bad785371fa5
[gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents:
12813
diff
changeset
|
3535 |
bad785371fa5
[gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents:
12813
diff
changeset
|
3536 /* advances buf to the end of the ncr segment */ |
bad785371fa5
[gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents:
12813
diff
changeset
|
3537 wc = (gunichar) strtoul(b, &buf, base); |
bad785371fa5
[gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents:
12813
diff
changeset
|
3538 |
bad785371fa5
[gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents:
12813
diff
changeset
|
3539 /* this mimics the previous impl of ncr_decode */ |
bad785371fa5
[gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents:
12813
diff
changeset
|
3540 if(*buf == ';') { |
bad785371fa5
[gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents:
12813
diff
changeset
|
3541 g_string_append_unichar(out, wc); |
bad785371fa5
[gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents:
12813
diff
changeset
|
3542 buf++; |
9161 | 3543 } |
3544 } | |
12880
bad785371fa5
[gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents:
12813
diff
changeset
|
3545 |
bad785371fa5
[gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents:
12813
diff
changeset
|
3546 /* append whatever's left */ |
bad785371fa5
[gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents:
12813
diff
changeset
|
3547 g_string_append(out, buf); |
bad785371fa5
[gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents:
12813
diff
changeset
|
3548 |
9161 | 3549 return g_string_free(out, FALSE); |
3550 } | |
3551 | |
12880
bad785371fa5
[gaim-migrate @ 15232]
Christopher O'Brien <siege@pidgin.im>
parents:
12813
diff
changeset
|
3552 |
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3553 int |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3554 gaim_utf8_strcasecmp(const char *a, const char *b) |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3555 { |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3556 char *a_norm = NULL; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3557 char *b_norm = NULL; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3558 int ret = -1; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3559 |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3560 if(!a && b) |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3561 return -1; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3562 else if(!b && a) |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3563 return 1; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3564 else if(!a && !b) |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3565 return 0; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3566 |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3567 if(!g_utf8_validate(a, -1, NULL) || !g_utf8_validate(b, -1, NULL)) |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3568 { |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3569 gaim_debug_error("gaim_utf8_strcasecmp", |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3570 "One or both parameters are invalid UTF8\n"); |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3571 return ret; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3572 } |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3573 |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3574 a_norm = g_utf8_casefold(a, -1); |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3575 b_norm = g_utf8_casefold(b, -1); |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3576 ret = g_utf8_collate(a_norm, b_norm); |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3577 g_free(a_norm); |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3578 g_free(b_norm); |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3579 |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3580 return ret; |
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3581 } |
7162 | 3582 |
11552
11d30825c1bb
[gaim-migrate @ 13812]
Gary Kramlich <grim@reaperworld.com>
parents:
11493
diff
changeset
|
3583 /* previously conversation::find_nick() */ |
11d30825c1bb
[gaim-migrate @ 13812]
Gary Kramlich <grim@reaperworld.com>
parents:
11493
diff
changeset
|
3584 gboolean |
11d30825c1bb
[gaim-migrate @ 13812]
Gary Kramlich <grim@reaperworld.com>
parents:
11493
diff
changeset
|
3585 gaim_utf8_has_word(const char *haystack, const char *needle) |
11d30825c1bb
[gaim-migrate @ 13812]
Gary Kramlich <grim@reaperworld.com>
parents:
11493
diff
changeset
|
3586 { |
11d30825c1bb
[gaim-migrate @ 13812]
Gary Kramlich <grim@reaperworld.com>
parents:
11493
diff
changeset
|
3587 char *hay, *pin, *p; |
11d30825c1bb
[gaim-migrate @ 13812]
Gary Kramlich <grim@reaperworld.com>
parents:
11493
diff
changeset
|
3588 int n; |
11d30825c1bb
[gaim-migrate @ 13812]
Gary Kramlich <grim@reaperworld.com>
parents:
11493
diff
changeset
|
3589 gboolean ret = FALSE; |
11d30825c1bb
[gaim-migrate @ 13812]
Gary Kramlich <grim@reaperworld.com>
parents:
11493
diff
changeset
|
3590 |
11d30825c1bb
[gaim-migrate @ 13812]
Gary Kramlich <grim@reaperworld.com>
parents:
11493
diff
changeset
|
3591 hay = g_utf8_strdown(haystack, -1); |
11d30825c1bb
[gaim-migrate @ 13812]
Gary Kramlich <grim@reaperworld.com>
parents:
11493
diff
changeset
|
3592 |
11d30825c1bb
[gaim-migrate @ 13812]
Gary Kramlich <grim@reaperworld.com>
parents:
11493
diff
changeset
|
3593 pin = g_utf8_strdown(needle, -1); |
11d30825c1bb
[gaim-migrate @ 13812]
Gary Kramlich <grim@reaperworld.com>
parents:
11493
diff
changeset
|
3594 n = strlen(pin); |
11d30825c1bb
[gaim-migrate @ 13812]
Gary Kramlich <grim@reaperworld.com>
parents:
11493
diff
changeset
|
3595 |
11d30825c1bb
[gaim-migrate @ 13812]
Gary Kramlich <grim@reaperworld.com>
parents:
11493
diff
changeset
|
3596 if ((p = strstr(hay, pin)) != NULL) { |
11d30825c1bb
[gaim-migrate @ 13812]
Gary Kramlich <grim@reaperworld.com>
parents:
11493
diff
changeset
|
3597 if ((p == hay || !isalnum(*(p - 1))) && !isalnum(*(p + n))) { |
11d30825c1bb
[gaim-migrate @ 13812]
Gary Kramlich <grim@reaperworld.com>
parents:
11493
diff
changeset
|
3598 ret = TRUE; |
11d30825c1bb
[gaim-migrate @ 13812]
Gary Kramlich <grim@reaperworld.com>
parents:
11493
diff
changeset
|
3599 } |
11d30825c1bb
[gaim-migrate @ 13812]
Gary Kramlich <grim@reaperworld.com>
parents:
11493
diff
changeset
|
3600 } |
11d30825c1bb
[gaim-migrate @ 13812]
Gary Kramlich <grim@reaperworld.com>
parents:
11493
diff
changeset
|
3601 |
11d30825c1bb
[gaim-migrate @ 13812]
Gary Kramlich <grim@reaperworld.com>
parents:
11493
diff
changeset
|
3602 g_free(pin); |
11d30825c1bb
[gaim-migrate @ 13812]
Gary Kramlich <grim@reaperworld.com>
parents:
11493
diff
changeset
|
3603 g_free(hay); |
11d30825c1bb
[gaim-migrate @ 13812]
Gary Kramlich <grim@reaperworld.com>
parents:
11493
diff
changeset
|
3604 |
11d30825c1bb
[gaim-migrate @ 13812]
Gary Kramlich <grim@reaperworld.com>
parents:
11493
diff
changeset
|
3605 return ret; |
11d30825c1bb
[gaim-migrate @ 13812]
Gary Kramlich <grim@reaperworld.com>
parents:
11493
diff
changeset
|
3606 } |
11d30825c1bb
[gaim-migrate @ 13812]
Gary Kramlich <grim@reaperworld.com>
parents:
11493
diff
changeset
|
3607 |
7564 | 3608 gboolean gaim_message_meify(char *message, size_t len) |
3609 { | |
3610 char *c; | |
3611 gboolean inside_html = FALSE; | |
3612 | |
3613 g_return_val_if_fail(message != NULL, FALSE); | |
3614 | |
3615 if(len == -1) | |
3616 len = strlen(message); | |
3617 | |
3618 for (c = message; *c; c++, len--) { | |
3619 if(inside_html) { | |
3620 if(*c == '>') | |
3621 inside_html = FALSE; | |
3622 } else { | |
3623 if(*c == '<') | |
3624 inside_html = TRUE; | |
3625 else | |
3626 break; | |
3627 } | |
3628 } | |
3629 | |
3630 if(*c && !g_ascii_strncasecmp(c, "/me ", 4)) { | |
3631 memmove(c, c+4, len-3); | |
3632 return TRUE; | |
3633 } | |
3634 | |
3635 return FALSE; | |
3636 } | |
3637 | |
7889 | 3638 char *gaim_text_strip_mnemonic(const char *in) |
3639 { | |
3640 char *out; | |
3641 char *a; | |
8561 | 3642 char *a0; |
7889 | 3643 const char *b; |
3644 | |
3645 g_return_val_if_fail(in != NULL, NULL); | |
3646 | |
3647 out = g_malloc(strlen(in)+1); | |
3648 a = out; | |
3649 b = in; | |
3650 | |
8561 | 3651 a0 = a; /* The last non-space char seen so far, or the first char */ |
3652 | |
7889 | 3653 while(*b) { |
3654 if(*b == '_') { | |
8561 | 3655 if(a > out && b > in && *(b-1) == '(' && *(b+1) && !(*(b+1) & 0x80) && *(b+2) == ')') { |
3656 /* Detected CJK style shortcut (Bug 875311) */ | |
3657 a = a0; /* undo the left parenthesis */ | |
3658 b += 3; /* and skip the whole mess */ | |
3659 } else if(*(b+1) == '_') { | |
7889 | 3660 *(a++) = '_'; |
3661 b += 2; | |
8561 | 3662 a0 = a; |
7889 | 3663 } else { |
3664 b++; | |
3665 } | |
8561 | 3666 /* We don't want to corrupt the middle of UTF-8 characters */ |
3667 } else if (!(*b & 0x80)) { /* other 1-byte char */ | |
3668 if (*b != ' ') | |
3669 a0 = a; | |
3670 *(a++) = *(b++); | |
7889 | 3671 } else { |
8561 | 3672 /* Multibyte utf8 char, don't look for _ inside these */ |
3673 int n = 0; | |
3674 int i; | |
3675 if ((*b & 0xe0) == 0xc0) { | |
3676 n = 2; | |
3677 } else if ((*b & 0xf0) == 0xe0) { | |
3678 n = 3; | |
3679 } else if ((*b & 0xf8) == 0xf0) { | |
3680 n = 4; | |
3681 } else if ((*b & 0xfc) == 0xf8) { | |
3682 n = 5; | |
3683 } else if ((*b & 0xfe) == 0xfc) { | |
3684 n = 6; | |
3685 } else { /* Illegal utf8 */ | |
3686 n = 1; | |
3687 } | |
3688 a0 = a; /* unless we want to delete CJK spaces too */ | |
3689 for (i = 0; i < n && *b; i += 1) { | |
3690 *(a++) = *(b++); | |
3691 } | |
7889 | 3692 } |
3693 } | |
3694 *a = '\0'; | |
3695 | |
3696 return out; | |
3697 } | |
3698 | |
9926 | 3699 const char* gaim_unescape_filename(const char *escaped) { |
3700 return gaim_url_decode(escaped); | |
3701 } | |
3702 | |
3703 | |
3704 /* this is almost identical to gaim_url_encode (hence gaim_url_decode | |
3705 * being used above), but we want to keep certain characters unescaped | |
3706 * for compat reasons */ | |
3707 const char * | |
3708 gaim_escape_filename(const char *str) | |
3709 { | |
10700
9505695acff8
[gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10636
diff
changeset
|
3710 const char *iter; |
9926 | 3711 static char buf[BUF_LEN]; |
10700
9505695acff8
[gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10636
diff
changeset
|
3712 char utf_char[6]; |
9926 | 3713 guint i, j = 0; |
3714 | |
3715 g_return_val_if_fail(str != NULL, NULL); | |
10700
9505695acff8
[gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10636
diff
changeset
|
3716 g_return_val_if_fail(g_utf8_validate(str, -1, NULL), NULL); |
9505695acff8
[gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10636
diff
changeset
|
3717 |
9505695acff8
[gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10636
diff
changeset
|
3718 iter = str; |
9505695acff8
[gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10636
diff
changeset
|
3719 for (; *iter && j < (BUF_LEN - 1) ; iter = g_utf8_next_char(iter)) { |
9505695acff8
[gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10636
diff
changeset
|
3720 gunichar c = g_utf8_get_char(iter); |
9505695acff8
[gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10636
diff
changeset
|
3721 /* If the character is an ASCII character and is alphanumeric, |
9505695acff8
[gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10636
diff
changeset
|
3722 * or one of the specified values, no need to escape */ |
10800 | 3723 if (c < 128 && (isalnum(c) || c == '@' || c == '-' || |
10700
9505695acff8
[gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10636
diff
changeset
|
3724 c == '_' || c == '.' || c == '#')) { |
9505695acff8
[gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10636
diff
changeset
|
3725 buf[j++] = c; |
9505695acff8
[gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10636
diff
changeset
|
3726 } else { |
9505695acff8
[gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10636
diff
changeset
|
3727 int bytes = g_unichar_to_utf8(c, utf_char); |
9505695acff8
[gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10636
diff
changeset
|
3728 for (i = 0; i < bytes; i++) { |
9505695acff8
[gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10636
diff
changeset
|
3729 if (j > (BUF_LEN - 4)) |
9505695acff8
[gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10636
diff
changeset
|
3730 break; |
9505695acff8
[gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10636
diff
changeset
|
3731 sprintf(buf + j, "%%%02x", utf_char[i] & 0xff); |
9505695acff8
[gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10636
diff
changeset
|
3732 j += 3; |
9505695acff8
[gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10636
diff
changeset
|
3733 } |
9926 | 3734 } |
3735 } | |
3736 | |
3737 buf[j] = '\0'; | |
3738 | |
3739 return buf; | |
3740 } | |
3741 | |
3742 |