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