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