Mercurial > pidgin
annotate src/util.c @ 10905:d41e285af79e
[gaim-migrate @ 12634]
Whoops.
committer: Tailor Script <tailor@pidgin.im>
| author | Stu Tomlinson <stu@nosnilmot.com> |
|---|---|
| date | Sat, 07 May 2005 17:44:00 +0000 |
| parents | acaf64c659b9 |
| children | 747ef488c600 |
| 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; |
| 10902 | 1418 char *tmp, *tmpurlbuf, *url_buf; |
| 9222 | 1419 gunichar g; |
| 8538 | 1420 gboolean inside_html = FALSE; |
| 9222 | 1421 int inside_paren = 0; |
|
7107
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1422 GString *ret = g_string_new(""); |
|
10372
618a330c0260
[gaim-migrate @ 11595]
Luke Schierer <lschiere@pidgin.im>
parents:
10332
diff
changeset
|
1423 /* 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
|
1424 |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1425 c = text; |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1426 while (*c) { |
| 9222 | 1427 |
| 1428 if(*c == '(' && !inside_html) { | |
| 1429 inside_paren++; | |
| 1430 ret = g_string_append_c(ret, *c); | |
| 1431 c++; | |
| 1432 } | |
| 1433 | |
| 8538 | 1434 if(inside_html) { |
| 1435 if(*c == '>') { | |
| 1436 inside_html = FALSE; | |
| 1437 } else if(!q && (*c == '\"' || *c == '\'')) { | |
| 1438 q = c; | |
| 1439 } else if(q) { | |
| 1440 if(*c == *q) | |
| 1441 q = NULL; | |
| 1442 } | |
| 1443 } else if(*c == '<') { | |
| 1444 inside_html = TRUE; | |
| 9077 | 1445 if (!g_ascii_strncasecmp(c, "<A", 2)) { |
| 1446 while (1) { | |
| 1447 if (!g_ascii_strncasecmp(c, "/A>", 3)) { | |
| 1448 inside_html = FALSE; | |
| 1449 break; | |
| 1450 } | |
| 1451 ret = g_string_append_c(ret, *c); | |
| 1452 c++; | |
| 1453 if (!(*c)) | |
| 1454 break; | |
|
7107
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1455 } |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1456 } |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1457 } else if ((*c=='h') && (!g_ascii_strncasecmp(c, "http://", 7) || |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1458 (!g_ascii_strncasecmp(c, "https://", 8)))) { |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1459 t = c; |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1460 while (1) { |
| 10461 | 1461 if (badchar(*t) || badentity(t)) { |
|
7107
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1462 |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1463 if (*(t) == ',' && (*(t + 1) != ' ')) { |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1464 t++; |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1465 continue; |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1466 } |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1467 |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1468 if (*(t - 1) == '.') |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1469 t--; |
| 9222 | 1470 if ((*(t - 1) == ')' && (inside_paren > 0))) { |
| 1471 t--; | |
| 1472 } | |
| 10461 | 1473 |
| 10902 | 1474 url_buf = g_strndup(c, t - c); |
| 8918 | 1475 tmpurlbuf = gaim_unescape_html(url_buf); |
|
7107
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1476 g_string_append_printf(ret, "<A HREF=\"%s\">%s</A>", |
| 8918 | 1477 tmpurlbuf, url_buf); |
| 10902 | 1478 g_free(url_buf); |
| 8918 | 1479 g_free(tmpurlbuf); |
|
7107
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1480 c = t; |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1481 break; |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1482 } |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1483 if (!t) |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1484 break; |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1485 t++; |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1486 |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1487 } |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1488 } else if (!g_ascii_strncasecmp(c, "www.", 4)) { |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1489 if (c[4] != '.') { |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1490 t = c; |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1491 while (1) { |
| 10461 | 1492 if (badchar(*t) || badentity(t)) { |
|
7107
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1493 if (t - c == 4) { |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1494 break; |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1495 } |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1496 |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1497 if (*(t) == ',' && (*(t + 1) != ' ')) { |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1498 t++; |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1499 continue; |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1500 } |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1501 |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1502 if (*(t - 1) == '.') |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1503 t--; |
| 9222 | 1504 if ((*(t - 1) == ')' && (inside_paren > 0))) { |
| 1505 t--; | |
| 1506 } | |
| 10902 | 1507 url_buf = g_strndup(c, t - c); |
| 8918 | 1508 tmpurlbuf = gaim_unescape_html(url_buf); |
|
7107
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1509 g_string_append_printf(ret, |
| 8918 | 1510 "<A HREF=\"http://%s\">%s</A>", tmpurlbuf, |
|
7107
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1511 url_buf); |
| 10902 | 1512 g_free(url_buf); |
| 8918 | 1513 g_free(tmpurlbuf); |
|
7107
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1514 c = t; |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1515 break; |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1516 } |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1517 if (!t) |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1518 break; |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1519 t++; |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1520 } |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1521 } |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1522 } else if (!g_ascii_strncasecmp(c, "ftp://", 6)) { |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1523 t = c; |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1524 while (1) { |
| 10461 | 1525 if (badchar(*t) || badentity(t)) { |
|
7107
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1526 if (*(t - 1) == '.') |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1527 t--; |
| 9222 | 1528 if ((*(t - 1) == ')' && (inside_paren > 0))) { |
| 1529 t--; | |
| 1530 } | |
| 10905 | 1531 url_buf = g_strndup(c, t - c); |
| 8918 | 1532 tmpurlbuf = gaim_unescape_html(url_buf); |
|
7107
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1533 g_string_append_printf(ret, "<A HREF=\"%s\">%s</A>", |
| 8918 | 1534 tmpurlbuf, url_buf); |
| 10902 | 1535 g_free(url_buf); |
| 8918 | 1536 g_free(tmpurlbuf); |
|
7107
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1537 c = t; |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1538 break; |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1539 } |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1540 if (!t) |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1541 break; |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1542 t++; |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1543 |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1544 } |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1545 } else if (!g_ascii_strncasecmp(c, "ftp.", 4)) { |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1546 if (c[4] != '.') { |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1547 t = c; |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1548 while (1) { |
| 10461 | 1549 if (badchar(*t) || badentity(t)) { |
|
7107
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1550 if (t - c == 4) { |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1551 break; |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1552 } |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1553 if (*(t - 1) == '.') |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1554 t--; |
| 9222 | 1555 if ((*(t - 1) == ')' && (inside_paren > 0))) { |
| 1556 t--; | |
| 1557 } | |
| 10902 | 1558 url_buf = g_strndup(c, t - c); |
| 8918 | 1559 tmpurlbuf = gaim_unescape_html(url_buf); |
|
7107
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1560 g_string_append_printf(ret, |
| 8918 | 1561 "<A HREF=\"ftp://%s\">%s</A>", tmpurlbuf, |
|
7107
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1562 url_buf); |
| 10902 | 1563 g_free(url_buf); |
| 8918 | 1564 g_free(tmpurlbuf); |
|
7107
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1565 c = t; |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1566 break; |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1567 } |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1568 if (!t) |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1569 break; |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1570 t++; |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1571 } |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1572 } |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1573 } else if (!g_ascii_strncasecmp(c, "mailto:", 7)) { |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1574 t = c; |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1575 while (1) { |
| 10461 | 1576 if (badchar(*t) || badentity(t)) { |
|
7107
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1577 if (*(t - 1) == '.') |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1578 t--; |
| 10902 | 1579 url_buf = g_strndup(c, t - c); |
| 8918 | 1580 tmpurlbuf = gaim_unescape_html(url_buf); |
|
7107
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1581 g_string_append_printf(ret, "<A HREF=\"%s\">%s</A>", |
| 8918 | 1582 tmpurlbuf, url_buf); |
| 10902 | 1583 g_free(url_buf); |
| 8918 | 1584 g_free(tmpurlbuf); |
|
7107
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1585 c = t; |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1586 break; |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1587 } |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1588 if (!t) |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1589 break; |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1590 t++; |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1591 |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1592 } |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1593 } else if (c != text && (*c == '@')) { |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1594 int flag; |
| 10905 | 1595 GString *gurl_buf = NULL; |
|
7107
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1596 const char illegal_chars[] = "!@#$%^&*()[]{}/|\\<>\":;\r\n \0"; |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1597 |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1598 if (strchr(illegal_chars,*(c - 1)) || strchr(illegal_chars, *(c + 1))) |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1599 flag = 0; |
| 10902 | 1600 else { |
|
7107
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1601 flag = 1; |
| 10902 | 1602 gurl_buf = g_string_new(""); |
| 1603 } | |
|
7107
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; |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1606 while (flag) { |
| 9222 | 1607 /* iterate backwards grabbing the local part of an email address */ |
| 1608 g = g_utf8_get_char(t); | |
| 1609 if (badchar(*t) || (g >= 127) || (*t == '(') || | |
| 1610 ((*t == ';') && (t > (text+2)) && !g_ascii_strncasecmp(t - 3, "<", 4))) { | |
| 1611 /* local part will already be part of ret, strip it out */ | |
| 1612 ret = g_string_truncate(ret, ret->len - (c - t)); | |
| 1613 ret = g_string_append_unichar(ret, g); | |
|
7107
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1614 break; |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1615 } else { |
| 9222 | 1616 g_string_prepend_unichar(gurl_buf, g); |
| 1617 t = g_utf8_find_prev_char(text, t); | |
|
7107
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1618 if (t < text) { |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1619 ret = g_string_assign(ret, ""); |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1620 break; |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1621 } |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1622 } |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1623 } |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1624 |
| 9222 | 1625 t = g_utf8_find_next_char(c, NULL); |
|
7107
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1626 |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1627 while (flag) { |
| 9222 | 1628 /* iterate forwards grabbing the domain part of an email address */ |
| 1629 g = g_utf8_get_char(t); | |
| 10461 | 1630 if (badchar(*t) || (g >= 127) || (*t == ')') || badentity(t)) { |
|
7107
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1631 char *d; |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1632 |
| 10902 | 1633 url_buf = g_string_free(gurl_buf, FALSE); |
| 9222 | 1634 |
| 1635 /* strip off trailing periods */ | |
| 10902 | 1636 if (strlen(url_buf) > 0) { |
| 1637 for (d = url_buf + strlen(url_buf) - 1; *d == '.'; d--, t--) | |
| 1638 *d = '\0'; | |
| 1639 } | |
|
7107
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1640 |
| 8918 | 1641 tmpurlbuf = gaim_unescape_html(url_buf); |
| 9045 | 1642 if (gaim_email_is_valid(tmpurlbuf)) { |
| 1643 g_string_append_printf(ret, "<A HREF=\"mailto:%s\">%s</A>", | |
| 1644 tmpurlbuf, url_buf); | |
| 1645 } else { | |
| 1646 g_string_append(ret, url_buf); | |
| 1647 } | |
| 10902 | 1648 g_free(url_buf); |
| 8918 | 1649 g_free(tmpurlbuf); |
|
7107
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1650 c = t; |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1651 |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1652 break; |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1653 } else { |
| 9222 | 1654 g_string_append_unichar(gurl_buf, g); |
| 1655 t = g_utf8_find_next_char(t, NULL); | |
|
7107
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1656 } |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1657 } |
| 9222 | 1658 } |
| 1659 | |
| 1660 if(*c == ')' && !inside_html) { | |
| 1661 inside_paren--; | |
| 1662 ret = g_string_append_c(ret, *c); | |
| 1663 c++; | |
|
7107
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 |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1666 if (*c == 0) |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1667 break; |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1668 |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1669 ret = g_string_append_c(ret, *c); |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1670 c++; |
|
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 } |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1673 tmp = ret->str; |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1674 g_string_free(ret, FALSE); |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1675 return tmp; |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1676 } |
|
9220c7490cd1
[gaim-migrate @ 7672]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
1677 |
| 8163 | 1678 char * |
| 8442 | 1679 gaim_unescape_html(const char *html) { |
| 1680 char *unescaped = NULL; | |
| 1681 | |
| 1682 if (html != NULL) { | |
| 1683 const char *c = html; | |
| 1684 GString *ret = g_string_new(""); | |
| 1685 while (*c) { | |
| 1686 if (!strncmp(c, "&", 5)) { | |
| 1687 ret = g_string_append_c(ret, '&'); | |
| 1688 c += 5; | |
| 1689 } else if (!strncmp(c, "<", 4)) { | |
| 1690 ret = g_string_append_c(ret, '<'); | |
| 1691 c += 4; | |
| 1692 } else if (!strncmp(c, ">", 4)) { | |
| 1693 ret = g_string_append_c(ret, '>'); | |
| 1694 c += 4; | |
| 1695 } else if (!strncmp(c, """, 6)) { | |
| 1696 ret = g_string_append_c(ret, '"'); | |
| 1697 c += 6; | |
| 8625 | 1698 } else if (!strncmp(c, "'", 6)) { |
| 1699 ret = g_string_append_c(ret, '\''); | |
| 1700 c += 6; | |
| 8500 | 1701 } else if (!strncmp(c, "<br>", 4)) { |
| 1702 ret = g_string_append_c(ret, '\n'); | |
| 1703 c += 4; | |
| 8442 | 1704 } else { |
| 1705 ret = g_string_append_c(ret, *c); | |
| 1706 c++; | |
| 1707 } | |
| 1708 } | |
| 1709 | |
| 1710 unescaped = ret->str; | |
| 1711 g_string_free(ret, FALSE); | |
| 1712 } | |
| 1713 return unescaped; | |
| 1714 | |
| 1715 } | |
| 1716 | |
| 9175 | 1717 char * |
| 1718 gaim_markup_slice(const char *str, guint x, guint y) | |
| 1719 { | |
| 1720 GString *ret; | |
| 1721 GQueue *q; | |
| 1722 guint z = 0; | |
| 1723 gboolean appended = FALSE; | |
| 1724 gunichar c; | |
| 1725 char *tag; | |
| 1726 | |
| 1727 g_return_val_if_fail(x <= y, NULL); | |
| 1728 | |
| 1729 if (x == y) | |
| 1730 return g_strdup(""); | |
| 1731 | |
| 1732 ret = g_string_new(""); | |
| 1733 q = g_queue_new(); | |
| 1734 | |
| 1735 while (*str && (z < y)) { | |
| 1736 c = g_utf8_get_char(str); | |
| 1737 | |
| 1738 if (c == '<') { | |
| 1739 char *end = strchr(str, '>'); | |
| 1740 | |
| 1741 if (!end) { | |
| 1742 g_string_free(ret, TRUE); | |
| 1743 while ((tag = g_queue_pop_head(q))) | |
| 1744 g_free(tag); | |
| 1745 g_queue_free(q); | |
| 1746 return NULL; | |
| 1747 } | |
| 1748 | |
| 1749 if (!g_ascii_strncasecmp(str, "<img ", 5)) { | |
| 1750 z += strlen("[Image]"); | |
| 1751 } else if (!g_ascii_strncasecmp(str, "<br", 3)) { | |
| 1752 z += 1; | |
| 1753 } else if (!g_ascii_strncasecmp(str, "<hr>", 4)) { | |
| 1754 z += strlen("\n---\n"); | |
| 1755 } else if (!g_ascii_strncasecmp(str, "</", 2)) { | |
| 1756 /* pop stack */ | |
| 1757 char *tmp; | |
| 1758 | |
| 1759 tmp = g_queue_pop_head(q); | |
| 1760 if (tmp) | |
| 1761 g_free(tmp); | |
| 1762 /* z += 0; */ | |
| 1763 } else { | |
| 1764 /* push it unto the stack */ | |
| 1765 char *tmp; | |
| 1766 | |
| 1767 tmp = g_strndup(str, end - str + 1); | |
| 1768 g_queue_push_head(q, tmp); | |
| 1769 /* z += 0; */ | |
| 1770 } | |
| 1771 | |
| 1772 if (z == x && !appended) { | |
| 1773 GList *l = q->tail; | |
| 1774 | |
| 1775 while (l) { | |
| 1776 tag = l->data; | |
| 1777 g_string_append(ret, tag); | |
| 1778 l = l->prev; | |
| 1779 } | |
| 1780 appended = TRUE; | |
| 1781 } else if (z >= x) { | |
| 1782 g_string_append_len(ret, str, end - str + 1); | |
| 1783 } | |
| 1784 | |
| 1785 str = end; | |
| 1786 } else if (c == '&') { | |
| 1787 char *end = strchr(str, ';'); | |
| 1788 if (!end) { | |
| 1789 g_string_free(ret, TRUE); | |
| 1790 while ((tag = g_queue_pop_head(q))) | |
| 1791 g_free(tag); | |
| 1792 g_queue_free(q); | |
| 1793 | |
| 1794 return NULL; | |
| 1795 } | |
| 1796 | |
| 1797 if (z >= x) | |
| 1798 g_string_append_len(ret, str, end - str + 1); | |
| 1799 | |
| 1800 z++; | |
| 1801 str = end; | |
| 1802 } else { | |
| 1803 if (z >= x) | |
| 1804 g_string_append_unichar(ret, c); | |
| 1805 z++; | |
| 1806 } | |
| 1807 | |
| 1808 str = g_utf8_next_char(str); | |
| 1809 } | |
| 1810 | |
| 1811 while ((tag = g_queue_pop_head(q))) { | |
| 1812 char *name; | |
| 1813 | |
| 1814 name = gaim_markup_get_tag_name(tag); | |
| 1815 g_string_append_printf(ret, "</%s>", name); | |
| 1816 g_free(name); | |
| 1817 g_free(tag); | |
| 1818 } | |
| 1819 | |
| 1820 g_queue_free(q); | |
| 1821 return g_string_free(ret, FALSE); | |
| 1822 } | |
| 1823 | |
| 1824 char * | |
| 1825 gaim_markup_get_tag_name(const char *tag) | |
| 1826 { | |
| 1827 int i; | |
| 1828 g_return_val_if_fail(tag != NULL, NULL); | |
| 1829 g_return_val_if_fail(*tag == '<', NULL); | |
| 1830 | |
| 1831 for (i = 1; tag[i]; i++) | |
| 1832 if (tag[i] == '>' || tag[i] == ' ' || tag[i] == '/') | |
| 1833 break; | |
| 1834 | |
| 1835 return g_strndup(tag, i); | |
| 1836 } | |
| 1837 | |
|
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
1838 /************************************************************************** |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
1839 * Path/Filename Functions |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
1840 **************************************************************************/ |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
1841 const char * |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
1842 gaim_home_dir(void) |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
1843 { |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
1844 #ifndef _WIN32 |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
1845 if(g_get_home_dir()) |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
1846 return g_get_home_dir(); |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
1847 else |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
1848 return NULL; |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
1849 #else |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
1850 return wgaim_data_dir(); |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
1851 #endif |
|
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 |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
1854 /* 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
|
1855 * dir. Note that there is no trailing slash after .gaim. */ |
| 10332 | 1856 const char * |
|
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
1857 gaim_user_dir(void) |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
1858 { |
| 8596 | 1859 if (custom_home_dir != NULL && strlen(custom_home_dir) > 0) { |
| 1860 strcpy ((char*) &home_dir, (char*) &custom_home_dir); | |
| 1861 } else { | |
| 1862 const gchar *hd = gaim_home_dir(); | |
|
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
1863 |
| 8596 | 1864 if (hd) { |
| 9916 | 1865 g_strlcpy((char*) &home_dir, hd, sizeof(home_dir)); |
| 1866 g_strlcat((char*) &home_dir, G_DIR_SEPARATOR_S ".gaim", | |
| 1867 sizeof(home_dir)); | |
| 8596 | 1868 } |
|
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
1869 } |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
1870 |
| 8596 | 1871 return home_dir; |
| 1872 } | |
| 1873 | |
| 10871 | 1874 void gaim_util_set_user_dir(const char *dir) |
| 8596 | 1875 { |
| 1876 if (dir != NULL && strlen(dir) > 0) { | |
| 9916 | 1877 g_strlcpy((char*) &custom_home_dir, dir, |
| 1878 sizeof(custom_home_dir)); | |
| 8596 | 1879 } |
|
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
1880 } |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
1881 |
| 7622 | 1882 int gaim_build_dir (const char *path, int mode) |
| 7612 | 1883 { |
| 1884 char *dir, **components, delim[] = { G_DIR_SEPARATOR, '\0' }; | |
| 1885 int cur, len; | |
| 1886 | |
| 7632 | 1887 g_return_val_if_fail(path != NULL, -1); |
| 7612 | 1888 |
| 1889 dir = g_new0(char, strlen(path) + 1); | |
| 7622 | 1890 components = g_strsplit(path, delim, -1); |
| 7612 | 1891 len = 0; |
| 1892 for (cur = 0; components[cur] != NULL; cur++) { | |
| 7633 | 1893 /* If you don't know what you're doing on both |
| 1894 * win32 and *NIX, stay the hell away from this code */ | |
| 1895 if(cur > 1) | |
| 1896 dir[len++] = G_DIR_SEPARATOR; | |
| 7612 | 1897 strcpy(dir + len, components[cur]); |
| 1898 len += strlen(components[cur]); | |
| 7633 | 1899 if(cur == 0) |
| 1900 dir[len++] = G_DIR_SEPARATOR; | |
| 7632 | 1901 |
| 1902 if(g_file_test(dir, G_FILE_TEST_IS_DIR)) { | |
| 1903 continue; | |
| 1904 } else if(g_file_test(dir, G_FILE_TEST_EXISTS)) { | |
| 9716 | 1905 gaim_debug_warning("build_dir", "bad path: %s\n", path); |
| 7612 | 1906 g_strfreev(components); |
| 1907 g_free(dir); | |
| 1908 return -1; | |
| 1909 } | |
| 1910 | |
|
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10569
diff
changeset
|
1911 if (g_mkdir(dir, mode) < 0) { |
| 9716 | 1912 gaim_debug_warning("build_dir", "mkdir: %s\n", strerror(errno)); |
| 7612 | 1913 g_strfreev(components); |
| 1914 g_free(dir); | |
| 1915 return -1; | |
| 1916 } | |
| 1917 } | |
| 1918 | |
| 1919 g_strfreev(components); | |
| 1920 g_free(dir); | |
| 1921 return 0; | |
| 1922 } | |
| 1923 | |
|
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
1924 /* |
| 10414 | 1925 * This function is long and beautiful, like my--um, yeah. Anyway, |
| 1926 * it includes lots of error checking so as we don't overwrite | |
| 1927 * people's settings if there is a problem writing the new values. | |
| 1928 */ | |
| 1929 gboolean | |
| 10415 | 1930 gaim_util_write_data_to_file(const char *filename, const char *data, size_t size) |
| 10414 | 1931 { |
| 1932 const char *user_dir = gaim_user_dir(); | |
| 1933 gchar *filename_temp, *filename_full; | |
| 1934 FILE *file; | |
| 10415 | 1935 size_t real_size, byteswritten; |
| 10414 | 1936 struct stat st; |
| 1937 | |
| 1938 g_return_val_if_fail(user_dir != NULL, FALSE); | |
| 1939 | |
| 1940 gaim_debug_info("util", "Writing file %s to directory %s\n", | |
| 1941 filename, user_dir); | |
| 1942 | |
| 1943 /* Ensure the user directory exists */ | |
| 1944 if (!g_file_test(user_dir, G_FILE_TEST_IS_DIR)) | |
| 1945 { | |
|
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10569
diff
changeset
|
1946 if (g_mkdir(user_dir, S_IRUSR | S_IWUSR | S_IXUSR) == -1) |
| 10414 | 1947 { |
| 1948 gaim_debug_error("util", "Error creating directory %s: %s\n", | |
| 1949 user_dir, strerror(errno)); | |
| 1950 return FALSE; | |
| 1951 } | |
| 1952 } | |
| 1953 | |
| 10415 | 1954 filename_full = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s", user_dir, filename); |
| 10414 | 1955 filename_temp = g_strdup_printf("%s.save", filename_full); |
| 1956 | |
| 1957 /* Remove an old temporary file, if one exists */ | |
| 1958 if (g_file_test(filename_temp, G_FILE_TEST_EXISTS)) | |
| 1959 { | |
|
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10569
diff
changeset
|
1960 if (g_unlink(filename_temp) == -1) |
| 10414 | 1961 { |
| 1962 gaim_debug_error("util", "Error removing old file %s: %s\n", | |
| 1963 filename_temp, strerror(errno)); | |
| 1964 } | |
| 1965 } | |
| 1966 | |
| 1967 /* Open file */ | |
|
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10569
diff
changeset
|
1968 file = g_fopen(filename_temp, "wb"); |
| 10414 | 1969 if (file == NULL) |
| 1970 { | |
| 1971 gaim_debug_error("util", "Error opening file %s for writing: %s\n", | |
| 1972 filename_temp, strerror(errno)); | |
| 1973 g_free(filename_full); | |
| 1974 g_free(filename_temp); | |
| 1975 return FALSE; | |
| 1976 } | |
| 1977 | |
| 1978 /* Write to file */ | |
| 10415 | 1979 real_size = (size == -1) ? strlen(data) : size; |
| 1980 byteswritten = fwrite(data, 1, real_size, file); | |
| 10414 | 1981 |
| 1982 /* Close file */ | |
| 1983 if (fclose(file) != 0) | |
| 1984 { | |
| 1985 gaim_debug_error("util", "Error closing file %s: %s\n", | |
| 1986 filename_temp, strerror(errno)); | |
| 1987 g_free(filename_full); | |
| 1988 g_free(filename_temp); | |
| 1989 return FALSE; | |
| 1990 } | |
| 1991 | |
| 1992 /* Ensure the file is the correct size */ | |
| 10415 | 1993 if (byteswritten != real_size) |
| 10414 | 1994 { |
| 1995 gaim_debug_error("util", "Error writing to file %s: Wrote %z bytes " | |
| 1996 "but should have written %z; is your disk full?\n", | |
| 10415 | 1997 filename_temp, byteswritten, real_size); |
| 10414 | 1998 g_free(filename_full); |
| 1999 g_free(filename_temp); | |
| 2000 return FALSE; | |
| 2001 } | |
| 2002 /* Use stat to be absolutely sure. */ | |
|
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10569
diff
changeset
|
2003 if ((g_stat(filename_temp, &st) == -1) || (st.st_size != real_size)) |
| 10414 | 2004 { |
| 2005 gaim_debug_error("util", "Error writing data to file %s: " | |
| 2006 "Incomplete file written; is your disk full?\n", | |
| 2007 filename_temp); | |
| 2008 g_free(filename_full); | |
| 2009 g_free(filename_temp); | |
| 2010 return FALSE; | |
| 2011 } | |
| 2012 | |
| 2013 /* Set file permissions */ | |
| 2014 if (chmod(filename_temp, S_IRUSR | S_IWUSR) == -1) | |
| 2015 { | |
| 2016 gaim_debug_error("util", "Error setting permissions of file %s: %s\n", | |
| 2017 filename_temp, strerror(errno)); | |
| 2018 } | |
| 2019 | |
| 2020 /* Remove the old file, if it exists */ | |
| 2021 if (g_file_test(filename_full, G_FILE_TEST_EXISTS)) | |
| 2022 { | |
|
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10569
diff
changeset
|
2023 if (g_unlink(filename_full) == -1) |
| 10414 | 2024 { |
| 2025 gaim_debug_error("util", "Error removing old file %s: %s\n", | |
| 2026 filename_full, strerror(errno)); | |
| 2027 } | |
| 2028 } | |
| 2029 | |
| 2030 /* Rename to the REAL name */ | |
|
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10569
diff
changeset
|
2031 if (g_rename(filename_temp, filename_full) == -1) |
| 10414 | 2032 { |
| 2033 gaim_debug_error("util", "Error renaming %s to %s: %s\n", | |
| 2034 filename_temp, filename_full, strerror(errno)); | |
| 2035 } | |
| 2036 | |
| 2037 g_free(filename_full); | |
| 2038 g_free(filename_temp); | |
| 2039 | |
| 2040 return TRUE; | |
| 2041 } | |
| 2042 | |
| 10425 | 2043 xmlnode * |
| 2044 gaim_util_read_xml_from_file(const char *filename, const char *description) | |
| 2045 { | |
| 2046 const char *user_dir = gaim_user_dir(); | |
| 2047 gchar *filename_full; | |
| 2048 GError *error; | |
| 2049 gchar *contents = NULL; | |
| 2050 gsize length; | |
| 2051 xmlnode *node = NULL; | |
| 2052 | |
| 2053 g_return_val_if_fail(user_dir != NULL, NULL); | |
| 2054 | |
| 2055 gaim_debug_info("util", "Reading file %s from directory %s\n", | |
| 2056 filename, user_dir); | |
| 2057 | |
| 2058 filename_full = g_build_filename(user_dir, filename, NULL); | |
| 2059 | |
| 2060 if (!g_file_test(filename_full, G_FILE_TEST_EXISTS)) | |
| 2061 { | |
| 2062 gaim_debug_info("util", "File %s does not exist (this is not " | |
| 2063 "necessarily an error)\n", filename_full); | |
| 2064 g_free(filename_full); | |
| 2065 return NULL; | |
| 2066 } | |
| 2067 | |
| 2068 if (!g_file_get_contents(filename_full, &contents, &length, &error)) | |
| 2069 { | |
| 2070 gaim_debug_error("util", "Error reading file %s: %s\n", | |
| 2071 filename_full, error->message); | |
| 2072 g_error_free(error); | |
| 2073 } | |
| 2074 | |
| 2075 if ((contents != NULL) && (length > 0)) | |
| 2076 { | |
| 2077 node = xmlnode_from_str(contents, length); | |
| 2078 | |
| 2079 /* If we were unable to parse the file then save its contents to a backup file */ | |
| 2080 if (node == NULL) | |
| 2081 { | |
| 2082 gchar *filename_temp; | |
| 2083 | |
| 2084 filename_temp = g_strdup_printf("%s~", filename); | |
| 2085 gaim_debug_error("util", "Error parsing file %s. Rrenaming old " | |
| 2086 "file to %s\n", filename_full, filename_temp); | |
| 2087 gaim_util_write_data_to_file(filename_temp, contents, length); | |
| 2088 g_free(filename_temp); | |
| 2089 } | |
| 2090 | |
| 2091 g_free(contents); | |
| 2092 } | |
| 2093 | |
| 2094 /* If we could not parse the file then show the user an error message */ | |
| 2095 if (node == NULL) | |
| 2096 { | |
| 2097 gchar *title, *msg; | |
| 2098 title = g_strdup_printf(_("Error Reading %s"), filename); | |
| 2099 msg = g_strdup_printf(_("An error was encountered reading your " | |
| 2100 "%s. They have not been loaded, and the old file " | |
| 2101 "been renamed to %s~."), description, filename_full); | |
| 2102 gaim_notify_error(NULL, NULL, title, msg); | |
| 2103 g_free(title); | |
| 2104 g_free(msg); | |
| 2105 } | |
| 2106 | |
| 2107 g_free(filename_full); | |
| 2108 | |
| 2109 return node; | |
| 2110 } | |
| 2111 | |
| 10414 | 2112 /* |
|
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2113 * 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
|
2114 * 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
|
2115 * the space for the filepath. |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2116 * |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2117 * 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
|
2118 * 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
|
2119 * |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2120 * Returns NULL on failure and cleans up after itself if so. |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2121 */ |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2122 static const char *gaim_mkstemp_templ = {"gaimXXXXXX"}; |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2123 |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2124 FILE * |
|
10203
7ff9b8b22e7d
[gaim-migrate @ 11324]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
10112
diff
changeset
|
2125 gaim_mkstemp(char **fpath, gboolean binary) |
|
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2126 { |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2127 const gchar *tmpdir; |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2128 #ifndef _WIN32 |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2129 int fd; |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2130 #endif |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2131 FILE *fp = NULL; |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2132 |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2133 g_return_val_if_fail(fpath != NULL, NULL); |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2134 |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2135 if((tmpdir = (gchar*)g_get_tmp_dir()) != NULL) { |
| 9427 | 2136 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
|
2137 #ifdef _WIN32 |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2138 char* result = _mktemp( *fpath ); |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2139 if( result == NULL ) |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2140 gaim_debug(GAIM_DEBUG_ERROR, "gaim_mkstemp", |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2141 "Problem creating the template\n"); |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2142 else |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2143 { |
|
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10569
diff
changeset
|
2144 if( (fp = g_fopen( result, binary?"wb+":"w+")) == NULL ) { |
|
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2145 gaim_debug(GAIM_DEBUG_ERROR, "gaim_mkstemp", |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2146 "Couldn't fopen() %s\n", result); |
|
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 } |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2149 #else |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2150 if((fd = mkstemp(*fpath)) == -1) { |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2151 gaim_debug(GAIM_DEBUG_ERROR, "gaim_mkstemp", |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2152 "Couldn't make \"%s\", error: %d\n", |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2153 *fpath, errno); |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2154 } else { |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2155 if((fp = fdopen(fd, "r+")) == NULL) { |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2156 close(fd); |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2157 gaim_debug(GAIM_DEBUG_ERROR, "gaim_mkstemp", |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2158 "Couldn't fdopen(), error: %d\n", errno); |
|
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 } |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2161 #endif |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2162 if(!fp) { |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2163 g_free(*fpath); |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2164 *fpath = NULL; |
|
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 } |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2167 } else { |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2168 gaim_debug(GAIM_DEBUG_ERROR, "gaim_mkstemp", |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2169 "g_get_tmp_dir() failed!"); |
|
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 |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2172 return fp; |
|
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 |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2175 gboolean |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2176 gaim_program_is_valid(const char *program) |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2177 { |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2178 GError *error = NULL; |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2179 char **argv; |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2180 gchar *progname; |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2181 gboolean is_valid = FALSE; |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2182 |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2183 g_return_val_if_fail(program != NULL, FALSE); |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2184 g_return_val_if_fail(*program != '\0', FALSE); |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2185 |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2186 if (!g_shell_parse_argv(program, NULL, &argv, &error)) { |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2187 gaim_debug(GAIM_DEBUG_ERROR, "program_is_valid", |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2188 "Could not parse program '%s': %s\n", |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2189 program, error->message); |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2190 g_error_free(error); |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2191 return FALSE; |
|
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 |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2194 if (argv == NULL) { |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2195 return FALSE; |
|
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 |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2198 progname = g_find_program_in_path(argv[0]); |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2199 is_valid = (progname != NULL); |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2200 |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2201 g_strfreev(argv); |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2202 g_free(progname); |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2203 |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2204 return is_valid; |
|
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 |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2207 char * |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2208 gaim_fd_get_ip(int fd) |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2209 { |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2210 struct sockaddr addr; |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2211 socklen_t namelen = sizeof(addr); |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2212 |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2213 g_return_val_if_fail(fd != 0, NULL); |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2214 |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2215 if (getsockname(fd, &addr, &namelen)) |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2216 return NULL; |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2217 |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2218 return g_strdup(inet_ntoa(((struct sockaddr_in *)&addr)->sin_addr)); |
|
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 /************************************************************************** |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2223 * String Functions |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2224 **************************************************************************/ |
| 7162 | 2225 const char * |
| 7261 | 2226 gaim_normalize(const GaimAccount *account, const char *s) |
|
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2227 { |
| 7261 | 2228 GaimPlugin *prpl = NULL; |
| 2229 GaimPluginProtocolInfo *prpl_info = NULL; | |
| 7430 | 2230 const char *ret = NULL; |
| 7261 | 2231 |
| 2232 if(account) | |
| 7956 | 2233 prpl = gaim_find_prpl(gaim_account_get_protocol_id(account)); |
|
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2234 |
| 7261 | 2235 if(prpl) |
| 2236 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(prpl); | |
| 2237 | |
| 7430 | 2238 if(prpl_info && prpl_info->normalize) |
| 2239 ret = prpl_info->normalize(account, s); | |
| 2240 | |
| 2241 if(!ret) { | |
| 7261 | 2242 static char buf[BUF_LEN]; |
| 2243 char *tmp; | |
| 2244 int i, j; | |
|
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2245 |
| 7261 | 2246 g_return_val_if_fail(s != NULL, NULL); |
| 2247 | |
| 2248 strncpy(buf, s, BUF_LEN); | |
| 2249 for (i=0, j=0; buf[j]; i++, j++) { | |
| 2250 while (buf[j] == ' ') | |
| 2251 j++; | |
| 2252 buf[i] = buf[j]; | |
| 2253 } | |
| 2254 buf[i] = '\0'; | |
|
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2255 |
| 7261 | 2256 tmp = g_utf8_strdown(buf, -1); |
| 2257 g_snprintf(buf, sizeof(buf), "%s", tmp); | |
| 2258 g_free(tmp); | |
| 2259 tmp = g_utf8_normalize(buf, -1, G_NORMALIZE_DEFAULT); | |
| 2260 g_snprintf(buf, sizeof(buf), "%s", tmp); | |
| 2261 g_free(tmp); | |
|
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2262 |
| 7430 | 2263 ret = buf; |
| 7261 | 2264 } |
| 7430 | 2265 return ret; |
|
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2266 } |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2267 |
| 8700 | 2268 gchar * |
| 2269 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
|
2270 { |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2271 char *c; |
| 8700 | 2272 gchar *cpy; |
|
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2273 int cnt = 0; |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2274 time_t t; |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2275 struct tm *tme; |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2276 char tmp[20]; |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2277 |
| 8700 | 2278 g_return_val_if_fail(str != NULL, NULL); |
|
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2279 g_return_val_if_fail(name != NULL, NULL); |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2280 |
| 8700 | 2281 cpy = g_malloc(BUF_LONG); |
| 2282 | |
|
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2283 t = time(NULL); |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2284 tme = localtime(&t); |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2285 |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2286 cpy[0] = '\0'; |
| 8700 | 2287 c = (char *)str; |
|
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2288 while (*c) { |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2289 switch (*c) { |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2290 case '%': |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2291 if (*(c + 1)) { |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2292 switch (*(c + 1)) { |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2293 case 'n': |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2294 /* append name */ |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2295 strcpy(cpy + cnt, name); |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2296 cnt += strlen(name); |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2297 c++; |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2298 break; |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2299 case 'd': |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2300 /* append date */ |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2301 strftime(tmp, 20, "%m/%d/%Y", tme); |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2302 strcpy(cpy + cnt, tmp); |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2303 cnt += strlen(tmp); |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2304 c++; |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2305 break; |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2306 case 't': |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2307 /* append time */ |
|
7459
825351e3b01b
[gaim-migrate @ 8072]
Christian Hammond <chipx86@chipx86.com>
parents:
7430
diff
changeset
|
2308 strftime(tmp, 20, "%I:%M:%S %p", tme); |
|
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2309 strcpy(cpy + cnt, tmp); |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2310 cnt += strlen(tmp); |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2311 c++; |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2312 break; |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2313 default: |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2314 cpy[cnt++] = *c; |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2315 } |
| 10614 | 2316 } else { |
| 2317 cpy[cnt++] = *c; | |
|
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2318 } |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2319 break; |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2320 default: |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2321 cpy[cnt++] = *c; |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2322 } |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2323 c++; |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2324 } |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2325 cpy[cnt] = '\0'; |
| 8700 | 2326 |
| 2327 return cpy; | |
|
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2328 } |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2329 |
| 8341 | 2330 gchar * |
| 2331 gaim_strdup_withhtml(const gchar *src) | |
|
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2332 { |
| 8341 | 2333 gulong destsize, i, j; |
| 2334 gchar *dest; | |
|
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2335 |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2336 g_return_val_if_fail(src != NULL, NULL); |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2337 |
| 8341 | 2338 /* New length is (length of src) + (number of \n's * 3) + 1 */ |
| 2339 for (i = 0, j = 0; src[i] != '\0'; i++) | |
| 2340 if (src[i] == '\n') | |
| 2341 j++; | |
| 2342 | |
| 2343 destsize = i + (j * 3) + 1; | |
| 2344 dest = g_malloc(destsize); | |
|
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2345 |
| 8341 | 2346 /* Copy stuff, ignoring \r's, because they are dumb */ |
| 2347 for (i = 0, j = 0; src[i] != '\0'; i++) { | |
| 2348 if (src[i] == '\n') { | |
| 2349 strcpy(&dest[j], "<BR>"); | |
| 2350 j += 4; | |
| 2351 } else if (src[i] != '\r') | |
| 2352 dest[j++] = src[i]; | |
| 2353 } | |
| 2354 | |
| 2355 dest[destsize-1] = '\0'; | |
|
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2356 |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2357 return dest; |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2358 } |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2359 |
| 7628 | 2360 gboolean |
| 2361 gaim_str_has_prefix(const char *s, const char *p) | |
| 2362 { | |
| 2363 if (!strncmp(s, p, strlen(p))) | |
| 2364 return TRUE; | |
| 2365 | |
| 2366 return FALSE; | |
| 2367 } | |
| 2368 | |
| 2369 gboolean | |
| 2370 gaim_str_has_suffix(const char *s, const char *x) | |
| 2371 { | |
| 2372 int off = strlen(s) - strlen(x); | |
| 2373 | |
| 2374 if (off >= 0 && !strcmp(s + off, x)) | |
| 2375 return TRUE; | |
| 2376 | |
| 2377 return FALSE; | |
| 2378 } | |
| 2379 | |
|
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2380 char * |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2381 gaim_str_add_cr(const char *text) |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2382 { |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2383 char *ret = NULL; |
| 7631 | 2384 int count = 0, j; |
| 2385 guint i; | |
|
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2386 |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2387 g_return_val_if_fail(text != NULL, NULL); |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2388 |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2389 if (text[0] == '\n') |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2390 count++; |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2391 for (i = 1; i < strlen(text); i++) |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2392 if (text[i] == '\n' && text[i - 1] != '\r') |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2393 count++; |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2394 |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2395 if (count == 0) |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2396 return g_strdup(text); |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2397 |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2398 ret = g_malloc0(strlen(text) + count + 1); |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2399 |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2400 i = 0; j = 0; |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2401 if (text[i] == '\n') |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2402 ret[j++] = '\r'; |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2403 ret[j++] = text[i++]; |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2404 for (; i < strlen(text); i++) { |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2405 if (text[i] == '\n' && text[i - 1] != '\r') |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2406 ret[j++] = '\r'; |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2407 ret[j++] = text[i]; |
|
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 |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2410 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
|
2411 text, ret); |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2412 |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2413 return ret; |
|
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 |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2416 void |
|
7478
3c21f3084ff0
[gaim-migrate @ 8091]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
7459
diff
changeset
|
2417 gaim_str_strip_cr(char *text) |
|
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2418 { |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2419 int i, j; |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2420 char *text2; |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2421 |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2422 g_return_if_fail(text != NULL); |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2423 |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2424 text2 = g_malloc(strlen(text) + 1); |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2425 |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2426 for (i = 0, j = 0; text[i]; i++) |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2427 if (text[i] != '\r') |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2428 text2[j++] = text[i]; |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2429 text2[j] = '\0'; |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2430 |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2431 strcpy(text, text2); |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2432 g_free(text2); |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2433 } |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2434 |
| 8341 | 2435 gchar * |
|
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2436 gaim_strreplace(const char *string, const char *delimiter, |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2437 const char *replacement) |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2438 { |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2439 gchar **split; |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2440 gchar *ret; |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2441 |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2442 g_return_val_if_fail(string != NULL, NULL); |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2443 g_return_val_if_fail(delimiter != NULL, NULL); |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2444 g_return_val_if_fail(replacement != NULL, NULL); |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2445 |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2446 split = g_strsplit(string, delimiter, 0); |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2447 ret = g_strjoinv(replacement, split); |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2448 g_strfreev(split); |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2449 |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2450 return ret; |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2451 } |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2452 |
| 8341 | 2453 gchar * |
| 2454 gaim_strcasereplace(const char *string, const char *delimiter, | |
| 2455 const char *replacement) | |
| 2456 { | |
| 2457 gchar *ret; | |
| 2458 int length_del, length_rep, i, j; | |
| 2459 | |
| 2460 g_return_val_if_fail(string != NULL, NULL); | |
| 2461 g_return_val_if_fail(delimiter != NULL, NULL); | |
| 2462 g_return_val_if_fail(replacement != NULL, NULL); | |
| 2463 | |
| 2464 length_del = strlen(delimiter); | |
| 2465 length_rep = strlen(replacement); | |
| 2466 | |
| 2467 /* Count how many times the delimiter appears */ | |
| 2468 i = 0; /* position in the source string */ | |
|
8735
92cbf9713795
[gaim-migrate @ 9490]
Christian Hammond <chipx86@chipx86.com>
parents:
8700
diff
changeset
|
2469 j = 0; /* number of occurrences of "delimiter" */ |
| 8341 | 2470 while (string[i] != '\0') { |
| 2471 if (!strncasecmp(&string[i], delimiter, length_del)) { | |
| 2472 i += length_del; | |
| 2473 j += length_rep; | |
| 2474 } else { | |
| 2475 i++; | |
| 2476 j++; | |
| 2477 } | |
| 2478 } | |
| 2479 | |
| 2480 ret = g_malloc(j+1); | |
| 2481 | |
| 2482 i = 0; /* position in the source string */ | |
| 2483 j = 0; /* position in the destination string */ | |
| 2484 while (string[i] != '\0') { | |
| 2485 if (!strncasecmp(&string[i], delimiter, length_del)) { | |
| 2486 strncpy(&ret[j], replacement, length_rep); | |
| 2487 i += length_del; | |
| 2488 j += length_rep; | |
| 2489 } else { | |
| 2490 ret[j] = string[i]; | |
| 2491 i++; | |
| 2492 j++; | |
| 2493 } | |
| 2494 } | |
| 2495 | |
| 2496 ret[j] = '\0'; | |
| 2497 | |
| 2498 return ret; | |
| 2499 } | |
| 2500 | |
|
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2501 const char * |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2502 gaim_strcasestr(const char *haystack, const char *needle) |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2503 { |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2504 size_t hlen, nlen; |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2505 const char *tmp, *ret; |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2506 |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2507 g_return_val_if_fail(haystack != NULL, NULL); |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2508 g_return_val_if_fail(needle != NULL, NULL); |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2509 |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2510 hlen = strlen(haystack); |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2511 nlen = strlen(needle); |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2512 tmp = haystack, |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2513 ret = NULL; |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2514 |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2515 g_return_val_if_fail(hlen > 0, NULL); |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2516 g_return_val_if_fail(nlen > 0, NULL); |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2517 |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2518 while (*tmp && !ret) { |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2519 if (!g_ascii_strncasecmp(needle, tmp, nlen)) |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2520 ret = tmp; |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2521 else |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2522 tmp++; |
|
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 |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2525 return ret; |
|
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 |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2528 char * |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2529 gaim_str_size_to_units(size_t size) |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2530 { |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2531 static const char *size_str[4] = { "bytes", "KB", "MB", "GB" }; |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2532 float size_mag; |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2533 int size_index = 0; |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2534 |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2535 if (size == -1) { |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2536 return g_strdup(_("Calculating...")); |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2537 } |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2538 else if (size == 0) { |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2539 return g_strdup(_("Unknown.")); |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2540 } |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2541 else { |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2542 size_mag = (float)size; |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2543 |
|
10569
12349012610b
[gaim-migrate @ 11957]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10506
diff
changeset
|
2544 while ((size_index < 3) && (size_mag > 1024)) { |
|
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2545 size_mag /= 1024; |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2546 size_index++; |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2547 } |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2548 |
| 10103 | 2549 if (size_index == 0) { |
| 10112 | 2550 return g_strdup_printf("%zd %s", size, size_str[size_index]); |
| 10103 | 2551 } else { |
| 2552 return g_strdup_printf("%.2f %s", size_mag, size_str[size_index]); | |
| 2553 } | |
|
7108
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 |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2557 char * |
|
9233
0c352d0e4ddc
[gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents:
9230
diff
changeset
|
2558 gaim_str_seconds_to_string(guint secs) |
|
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2559 { |
|
9233
0c352d0e4ddc
[gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents:
9230
diff
changeset
|
2560 GString *gstr; |
|
0c352d0e4ddc
[gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents:
9230
diff
changeset
|
2561 const char *prefix = ""; |
|
0c352d0e4ddc
[gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents:
9230
diff
changeset
|
2562 guint days, hrs, mins; |
|
0c352d0e4ddc
[gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents:
9230
diff
changeset
|
2563 |
| 9424 | 2564 gstr = g_string_new(""); |
| 2565 | |
| 2566 if (secs < 60) | |
| 2567 { | |
| 2568 g_string_append_printf(gstr, "%d %s", secs, | |
| 2569 ngettext("second", "seconds", secs)); | |
| 2570 return g_string_free(gstr, FALSE); | |
| 2571 } | |
| 2572 | |
|
9233
0c352d0e4ddc
[gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents:
9230
diff
changeset
|
2573 days = secs / (60 * 60 * 24); |
|
0c352d0e4ddc
[gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents:
9230
diff
changeset
|
2574 secs = secs % (60 * 60 * 24); |
|
0c352d0e4ddc
[gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents:
9230
diff
changeset
|
2575 hrs = secs / (60 * 60); |
|
0c352d0e4ddc
[gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents:
9230
diff
changeset
|
2576 secs = secs % (60 * 60); |
|
0c352d0e4ddc
[gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents:
9230
diff
changeset
|
2577 mins = secs / 60; |
|
0c352d0e4ddc
[gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents:
9230
diff
changeset
|
2578 secs = secs % 60; |
|
0c352d0e4ddc
[gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents:
9230
diff
changeset
|
2579 |
|
0c352d0e4ddc
[gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents:
9230
diff
changeset
|
2580 if (days > 0) |
|
0c352d0e4ddc
[gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents:
9230
diff
changeset
|
2581 { |
|
0c352d0e4ddc
[gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents:
9230
diff
changeset
|
2582 g_string_append_printf(gstr, "%d %s", days, |
|
0c352d0e4ddc
[gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents:
9230
diff
changeset
|
2583 ngettext("day", "days", days)); |
|
0c352d0e4ddc
[gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents:
9230
diff
changeset
|
2584 |
|
0c352d0e4ddc
[gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents:
9230
diff
changeset
|
2585 prefix = ", "; |
|
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2586 } |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2587 |
|
9233
0c352d0e4ddc
[gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents:
9230
diff
changeset
|
2588 if (hrs > 0) |
|
0c352d0e4ddc
[gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents:
9230
diff
changeset
|
2589 { |
|
0c352d0e4ddc
[gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents:
9230
diff
changeset
|
2590 g_string_append_printf(gstr, "%s%d %s", prefix, hrs, |
|
0c352d0e4ddc
[gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents:
9230
diff
changeset
|
2591 ngettext("hour", "hours", hrs)); |
|
0c352d0e4ddc
[gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents:
9230
diff
changeset
|
2592 |
|
0c352d0e4ddc
[gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents:
9230
diff
changeset
|
2593 prefix = ", "; |
|
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 |
|
0c352d0e4ddc
[gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents:
9230
diff
changeset
|
2596 if (mins > 0) |
|
0c352d0e4ddc
[gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents:
9230
diff
changeset
|
2597 { |
|
0c352d0e4ddc
[gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents:
9230
diff
changeset
|
2598 g_string_append_printf(gstr, "%s%d %s", prefix, mins, |
|
0c352d0e4ddc
[gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents:
9230
diff
changeset
|
2599 ngettext("minute", "minutes", mins)); |
|
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 |
|
0c352d0e4ddc
[gaim-migrate @ 10029]
Christian Hammond <chipx86@chipx86.com>
parents:
9230
diff
changeset
|
2602 return g_string_free(gstr, FALSE); |
|
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2603 } |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2604 |
| 9277 | 2605 |
| 2606 char * | |
| 2607 gaim_str_binary_to_ascii(const unsigned char *binary, guint len) | |
| 2608 { | |
| 2609 GString *ret; | |
| 2610 guint i; | |
| 2611 | |
| 2612 g_return_val_if_fail(len > 0, NULL); | |
| 2613 | |
| 2614 ret = g_string_sized_new(len); | |
| 2615 | |
| 2616 for (i = 0; i < len; i++) | |
| 2617 if (binary[i] < 32 || binary[i] > 126) | |
| 2618 g_string_append_printf(ret, "\\x%02hhx", binary[i]); | |
| 2619 else if (binary[i] == '\\') | |
| 2620 g_string_append(ret, "\\\\"); | |
| 2621 else | |
| 2622 g_string_append_c(ret, binary[i]); | |
| 2623 | |
| 2624 return g_string_free(ret, FALSE); | |
| 2625 } | |
| 2626 | |
|
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2627 /************************************************************************** |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2628 * URI/URL Functions |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
2629 **************************************************************************/ |
|
7095
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7094
diff
changeset
|
2630 gboolean |
|
7094
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2631 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
|
2632 char **ret_path, char **ret_user, char **ret_passwd) |
|
7094
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2633 { |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2634 char scan_info[255]; |
| 8118 | 2635 char port_str[6]; |
|
7094
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2636 int f; |
| 9341 | 2637 const char *at, *slash; |
|
7094
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2638 const char *turl; |
|
9227
9171e528d7e5
[gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents:
9222
diff
changeset
|
2639 char host[256], path[256], user[256], passwd[256]; |
|
7094
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2640 int port = 0; |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2641 /* hyphen at end includes it in control set */ |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2642 static char addr_ctrl[] = "A-Za-z0-9.-"; |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2643 static char port_ctrl[] = "0-9"; |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2644 static char page_ctrl[] = "A-Za-z0-9.~_/:*!@&%%?=+^-"; |
|
9227
9171e528d7e5
[gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents:
9222
diff
changeset
|
2645 static char user_ctrl[] = "A-Za-z0-9.~_/*!&%%?=+^-"; |
|
9171e528d7e5
[gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents:
9222
diff
changeset
|
2646 static char passwd_ctrl[] = "A-Za-z0-9.~_/*!&%%?=+^-"; |
|
7094
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2647 |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2648 g_return_val_if_fail(url != NULL, FALSE); |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2649 |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2650 if ((turl = strstr(url, "http://")) != NULL || |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2651 (turl = strstr(url, "HTTP://")) != NULL) |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2652 { |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2653 turl += 7; |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2654 url = turl; |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2655 } |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2656 |
|
9227
9171e528d7e5
[gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents:
9222
diff
changeset
|
2657 /* parse out authentication information if supplied */ |
| 9341 | 2658 /* Only care about @ char BEFORE the first / */ |
| 2659 at = strchr(url, '@'); | |
| 2660 slash = strchr(url, '/'); | |
| 2661 if ((at != NULL) && | |
| 2662 (((slash != NULL) && (strlen(at) > strlen(slash))) || | |
| 2663 (slash == NULL))) { | |
|
9227
9171e528d7e5
[gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents:
9222
diff
changeset
|
2664 g_snprintf(scan_info, sizeof(scan_info), |
|
9171e528d7e5
[gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents:
9222
diff
changeset
|
2665 "%%255[%s]:%%255[%s]^@", user_ctrl, passwd_ctrl); |
|
9171e528d7e5
[gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents:
9222
diff
changeset
|
2666 f = sscanf(url, scan_info, user, passwd); |
|
9171e528d7e5
[gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents:
9222
diff
changeset
|
2667 |
|
9171e528d7e5
[gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents:
9222
diff
changeset
|
2668 if (f ==1 ) { |
|
9171e528d7e5
[gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents:
9222
diff
changeset
|
2669 /* No passwd, possibly just username supplied */ |
|
9171e528d7e5
[gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents:
9222
diff
changeset
|
2670 g_snprintf(scan_info, sizeof(scan_info), |
|
9171e528d7e5
[gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents:
9222
diff
changeset
|
2671 "%%255[%s]^@", user_ctrl); |
|
9171e528d7e5
[gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents:
9222
diff
changeset
|
2672 f = sscanf(url, scan_info, user); |
|
9171e528d7e5
[gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents:
9222
diff
changeset
|
2673 *passwd = '\0'; |
|
9171e528d7e5
[gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents:
9222
diff
changeset
|
2674 } |
|
9171e528d7e5
[gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents:
9222
diff
changeset
|
2675 |
| 9230 | 2676 url = at+1; /* move pointer after the @ char */ |
|
9227
9171e528d7e5
[gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents:
9222
diff
changeset
|
2677 } else { |
|
9171e528d7e5
[gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents:
9222
diff
changeset
|
2678 *user = '\0'; |
|
9171e528d7e5
[gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents:
9222
diff
changeset
|
2679 *passwd = '\0'; |
|
9171e528d7e5
[gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents:
9222
diff
changeset
|
2680 } |
|
9171e528d7e5
[gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents:
9222
diff
changeset
|
2681 |
|
7094
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2682 g_snprintf(scan_info, sizeof(scan_info), |
| 8118 | 2683 "%%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
|
2684 |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2685 f = sscanf(url, scan_info, host, port_str, path); |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2686 |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2687 if (f == 1) |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2688 { |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2689 g_snprintf(scan_info, sizeof(scan_info), |
| 8118 | 2690 "%%255[%s]/%%255[%s]", |
|
7094
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2691 addr_ctrl, page_ctrl); |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2692 f = sscanf(url, scan_info, host, path); |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2693 g_snprintf(port_str, sizeof(port_str), "80"); |
|
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 |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2696 if (f == 1) |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2697 *path = '\0'; |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2698 |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2699 sscanf(port_str, "%d", &port); |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2700 |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2701 if (ret_host != NULL) *ret_host = g_strdup(host); |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2702 if (ret_port != NULL) *ret_port = port; |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2703 if (ret_path != NULL) *ret_path = g_strdup(path); |
|
9227
9171e528d7e5
[gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents:
9222
diff
changeset
|
2704 if (ret_user != NULL) *ret_user = g_strdup(user); |
|
9171e528d7e5
[gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents:
9222
diff
changeset
|
2705 if (ret_passwd != NULL) *ret_passwd = g_strdup(passwd); |
|
7094
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2706 |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2707 return TRUE; |
|
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 |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2710 static void |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2711 destroy_fetch_url_data(GaimFetchUrlData *gfud) |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2712 { |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2713 if (gfud->webdata != NULL) g_free(gfud->webdata); |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2714 if (gfud->url != NULL) g_free(gfud->url); |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2715 if (gfud->user_agent != NULL) g_free(gfud->user_agent); |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2716 if (gfud->website.address != NULL) g_free(gfud->website.address); |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2717 if (gfud->website.page != NULL) g_free(gfud->website.page); |
|
9227
9171e528d7e5
[gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents:
9222
diff
changeset
|
2718 if (gfud->website.user != NULL) g_free(gfud->website.user); |
|
9171e528d7e5
[gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents:
9222
diff
changeset
|
2719 if (gfud->website.passwd != NULL) g_free(gfud->website.passwd); |
|
7094
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2720 |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2721 g_free(gfud); |
|
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 |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2724 static gboolean |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2725 parse_redirect(const char *data, size_t data_len, gint sock, |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2726 GaimFetchUrlData *gfud) |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2727 { |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2728 gchar *s; |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2729 |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2730 if ((s = g_strstr_len(data, data_len, "Location: ")) != NULL) |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2731 { |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2732 gchar *new_url, *temp_url, *end; |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2733 gboolean full; |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2734 int len; |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2735 |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2736 s += strlen("Location: "); |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2737 end = strchr(s, '\r'); |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2738 |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2739 /* Just in case :) */ |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2740 if (end == NULL) |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2741 end = strchr(s, '\n'); |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2742 |
| 10623 | 2743 if (end == NULL) |
| 2744 return FALSE; | |
| 2745 | |
|
7094
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2746 len = end - s; |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2747 |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2748 new_url = g_malloc(len + 1); |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2749 strncpy(new_url, s, len); |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2750 new_url[len] = '\0'; |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2751 |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2752 full = gfud->full; |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2753 |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2754 if (*new_url == '/' || g_strstr_len(new_url, len, "://") == NULL) |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2755 { |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2756 temp_url = new_url; |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2757 |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2758 new_url = g_strdup_printf("%s:%d%s", gfud->website.address, |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2759 gfud->website.port, temp_url); |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2760 |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2761 g_free(temp_url); |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2762 |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2763 full = FALSE; |
|
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 |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2766 /* Close the existing stuff. */ |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2767 gaim_input_remove(gfud->inpa); |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2768 close(sock); |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2769 |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2770 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
|
2771 |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2772 /* Try again, with this new location. */ |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2773 gaim_url_fetch(new_url, full, gfud->user_agent, gfud->http11, |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2774 gfud->callback, gfud->user_data); |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2775 |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2776 /* Free up. */ |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2777 g_free(new_url); |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2778 destroy_fetch_url_data(gfud); |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2779 |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2780 return TRUE; |
|
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 |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2783 return FALSE; |
|
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 |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2786 static size_t |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2787 parse_content_len(const char *data, size_t data_len) |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2788 { |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2789 size_t content_len = 0; |
| 9240 | 2790 const char *p = NULL; |
| 2791 | |
| 2792 /* This is still technically wrong, since headers are case-insensitive | |
| 2793 * [RFC 2616, section 4.2], though this ought to catch the normal case. | |
| 2794 * Note: data is _not_ nul-terminated. | |
| 2795 */ | |
| 2796 if (data_len > 16) { | |
| 2797 p = strncmp(data, "Content-Length: ", 16) == 0? data: NULL; | |
| 2798 if (!p) { | |
| 2799 p = g_strstr_len(data, data_len, "\nContent-Length: "); | |
| 2800 if (p) | |
| 2801 p += 1; | |
| 2802 } | |
| 2803 } | |
| 2804 | |
| 2805 /* If we can find a Content-Length header at all, try to sscanf it. | |
| 2806 * Response headers should end with at least \r\n, so sscanf is safe, | |
| 2807 * if we make sure that there is indeed a \n in our header. | |
| 2808 */ | |
| 2809 if (p && g_strstr_len(p, data_len - (p - data), "\n")) { | |
| 10112 | 2810 sscanf(p, "Content-Length: %zu", &content_len); |
| 9818 | 2811 gaim_debug_misc("parse_content_len", "parsed %u\n", content_len); |
| 9240 | 2812 } |
|
7094
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2813 |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2814 return content_len; |
|
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 |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2817 static void |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2818 url_fetched_cb(gpointer url_data, gint sock, GaimInputCondition cond) |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2819 { |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2820 GaimFetchUrlData *gfud = url_data; |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2821 char data; |
| 9240 | 2822 gboolean got_eof = FALSE; |
|
7094
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2823 |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2824 if (sock == -1) |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2825 { |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2826 gfud->callback(gfud->user_data, NULL, 0); |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2827 |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2828 destroy_fetch_url_data(gfud); |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2829 |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2830 return; |
|
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 |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2833 if (!gfud->sentreq) |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2834 { |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2835 char buf[1024]; |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2836 |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2837 if (gfud->user_agent) |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2838 { |
| 9240 | 2839 /* Host header is not forbidden in HTTP/1.0 requests, and HTTP/1.1 |
| 2840 * clients must know how to handle the "chunked" transfer encoding. | |
| 2841 * Gaim doesn't know how to handle "chunked", so should always send | |
| 2842 * the Host header regardless, to get around some observed problems | |
| 2843 */ | |
| 2844 g_snprintf(buf, sizeof(buf), | |
| 2845 "GET %s%s HTTP/%s\r\n" | |
| 2846 "User-Agent: %s\r\n" | |
| 2847 "Host: %s\r\n\r\n", | |
| 2848 (gfud->full ? "" : "/"), | |
| 2849 (gfud->full ? gfud->url : gfud->website.page), | |
| 2850 (gfud->http11 ? "1.1" : "1.0"), | |
| 2851 gfud->user_agent, gfud->website.address); | |
|
7094
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2852 } |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2853 else |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2854 { |
| 9240 | 2855 g_snprintf(buf, sizeof(buf), |
| 2856 "GET %s%s HTTP/%s\r\n" | |
| 2857 "Host: %s\r\n\r\n", | |
| 2858 (gfud->full ? "" : "/"), | |
| 2859 (gfud->full ? gfud->url : gfud->website.page), | |
| 2860 (gfud->http11 ? "1.1" : "1.0"), | |
| 2861 gfud->website.address); | |
|
7094
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 |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2864 gaim_debug_misc("gaim_url_fetch", "Request: %s\n", buf); |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2865 |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2866 write(sock, buf, strlen(buf)); |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2867 fcntl(sock, F_SETFL, O_NONBLOCK); |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2868 gfud->sentreq = TRUE; |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2869 gfud->inpa = gaim_input_add(sock, GAIM_INPUT_READ, |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2870 url_fetched_cb, url_data); |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2871 gfud->data_len = 4096; |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2872 gfud->webdata = g_malloc(gfud->data_len); |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2873 |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2874 return; |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2875 } |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2876 |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2877 if (read(sock, &data, 1) > 0 || errno == EWOULDBLOCK) |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2878 { |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2879 if (errno == EWOULDBLOCK) |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2880 { |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2881 errno = 0; |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2882 |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2883 return; |
|
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 |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2886 gfud->len++; |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2887 |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2888 if (gfud->len == gfud->data_len + 1) |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2889 { |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2890 gfud->data_len += (gfud->data_len) / 2; |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2891 |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2892 gfud->webdata = g_realloc(gfud->webdata, gfud->data_len); |
|
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 |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2895 gfud->webdata[gfud->len - 1] = data; |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2896 |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2897 if (!gfud->startsaving) |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2898 { |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2899 if (data == '\r') |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2900 return; |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2901 |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2902 if (data == '\n') |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2903 { |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2904 if (gfud->newline) |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2905 { |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2906 size_t content_len; |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2907 gfud->startsaving = TRUE; |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2908 |
| 9240 | 2909 gaim_debug_misc("gaim_url_fetch", "Response headers: '%*.*s'\n", gfud->len, gfud->len, gfud->webdata); |
| 2910 | |
|
7094
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2911 /* See if we can find a redirect. */ |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2912 if (parse_redirect(gfud->webdata, gfud->len, sock, gfud)) |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2913 return; |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2914 |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2915 /* No redirect. See if we can find a content length. */ |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2916 content_len = parse_content_len(gfud->webdata, gfud->len); |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2917 |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2918 if (content_len == 0) |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2919 { |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2920 /* We'll stick with an initial 8192 */ |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2921 content_len = 8192; |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2922 } |
| 9240 | 2923 else |
| 2924 { | |
| 2925 gfud->has_explicit_data_len = TRUE; | |
| 2926 } | |
|
7094
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2927 |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2928 /* Out with the old... */ |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2929 gfud->len = 0; |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2930 g_free(gfud->webdata); |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2931 gfud->webdata = NULL; |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2932 |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2933 /* In with the new. */ |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2934 gfud->data_len = content_len; |
| 9818 | 2935 gfud->webdata = g_try_malloc(gfud->data_len); |
| 2936 if (gfud->webdata == NULL) { | |
| 2937 gaim_debug_error("gaim_url_fetch", "Failed to allocate %u bytes: %s\n", gfud->data_len, strerror(errno)); | |
| 2938 gaim_input_remove(gfud->inpa); | |
| 2939 close(sock); | |
| 2940 gfud->callback(gfud->user_data, NULL, 0); | |
| 2941 destroy_fetch_url_data(gfud); | |
| 2942 } | |
|
7094
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2943 } |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2944 else |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2945 gfud->newline = TRUE; |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2946 |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2947 return; |
|
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 |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2950 gfud->newline = FALSE; |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2951 } |
| 9240 | 2952 else if (gfud->has_explicit_data_len && gfud->len == gfud->data_len) |
| 2953 { | |
| 2954 got_eof = TRUE; | |
| 2955 } | |
|
7094
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2956 } |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2957 else if (errno != ETIMEDOUT) |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2958 { |
| 9240 | 2959 got_eof = TRUE; |
| 2960 } | |
| 2961 else | |
| 2962 { | |
| 2963 gaim_input_remove(gfud->inpa); | |
| 2964 close(sock); | |
| 2965 | |
| 2966 gfud->callback(gfud->user_data, NULL, 0); | |
| 2967 | |
| 2968 destroy_fetch_url_data(gfud); | |
| 2969 } | |
| 2970 | |
| 2971 if (got_eof) { | |
|
7094
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2972 gfud->webdata = g_realloc(gfud->webdata, gfud->len + 1); |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2973 gfud->webdata[gfud->len] = 0; |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2974 |
| 9311 | 2975 /* 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
|
2976 |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2977 gaim_input_remove(gfud->inpa); |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2978 close(sock); |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2979 gfud->callback(gfud->user_data, gfud->webdata, gfud->len); |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2980 |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2981 destroy_fetch_url_data(gfud); |
|
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 |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2985 void |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2986 gaim_url_fetch(const char *url, gboolean full, |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2987 const char *user_agent, gboolean http11, |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2988 void (*cb)(gpointer, const char *, size_t), |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2989 void *user_data) |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2990 { |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2991 int sock; |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2992 GaimFetchUrlData *gfud; |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
2993 |
| 9257 | 2994 g_return_if_fail(url != NULL); |
| 2995 g_return_if_fail(cb != NULL); | |
| 9311 | 2996 |
| 9240 | 2997 gaim_debug_info("gaim_url_fetch", |
| 2998 "requested to fetch (%s), full=%d, user_agent=(%s), http11=%d\n", | |
| 9257 | 2999 url, full, user_agent?user_agent:"(null)", http11); |
|
7094
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3000 |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3001 gfud = g_new0(GaimFetchUrlData, 1); |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3002 |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3003 gfud->callback = cb; |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3004 gfud->user_data = user_data; |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3005 gfud->url = g_strdup(url); |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3006 gfud->user_agent = (user_agent != NULL ? g_strdup(user_agent) : NULL); |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3007 gfud->http11 = http11; |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3008 gfud->full = full; |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3009 |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3010 gaim_url_parse(url, &gfud->website.address, &gfud->website.port, |
|
9227
9171e528d7e5
[gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents:
9222
diff
changeset
|
3011 &gfud->website.page, &gfud->website.user, &gfud->website.passwd); |
|
7094
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3012 |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3013 if ((sock = gaim_proxy_connect(NULL, gfud->website.address, |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3014 gfud->website.port, url_fetched_cb, |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3015 gfud)) < 0) |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3016 { |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3017 destroy_fetch_url_data(gfud); |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3018 |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3019 cb(user_data, g_strdup(_("g003: Error opening connection.\n")), 0); |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3020 } |
|
2343c3aa1dec
[gaim-migrate @ 7659]
Christian Hammond <chipx86@chipx86.com>
parents:
7078
diff
changeset
|
3021 } |
|
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3022 |
| 7162 | 3023 const char * |
| 7134 | 3024 gaim_url_decode(const char *str) |
| 3025 { | |
| 3026 static char buf[BUF_LEN]; | |
| 7631 | 3027 guint i, j = 0; |
| 7134 | 3028 char *bum; |
| 9804 | 3029 char hex[3]; |
| 7134 | 3030 |
| 3031 g_return_val_if_fail(str != NULL, NULL); | |
| 3032 | |
| 9804 | 3033 /* |
| 3034 * XXX - This check could be removed and buf could be made | |
| 3035 * dynamically allocated, but this is easier. | |
| 3036 */ | |
| 3037 if (strlen(str) >= BUF_LEN) | |
| 3038 return NULL; | |
| 3039 | |
| 7134 | 3040 for (i = 0; i < strlen(str); i++) { |
| 3041 | |
| 3042 if (str[i] != '%') | |
| 3043 buf[j++] = str[i]; | |
| 3044 else { | |
| 3045 strncpy(hex, str + ++i, 2); | |
| 3046 hex[2] = '\0'; | |
| 3047 | |
| 3048 /* i is pointing to the start of the number */ | |
| 3049 i++; | |
| 3050 | |
| 3051 /* | |
| 3052 * Now it's at the end and at the start of the for loop | |
| 3053 * will be at the next character. | |
| 3054 */ | |
| 3055 buf[j++] = strtol(hex, NULL, 16); | |
| 3056 } | |
| 3057 } | |
| 3058 | |
| 3059 buf[j] = '\0'; | |
| 3060 | |
| 3061 if (!g_utf8_validate(buf, -1, (const char **)&bum)) | |
| 3062 *bum = '\0'; | |
| 3063 | |
| 3064 return buf; | |
| 3065 } | |
| 3066 | |
| 7162 | 3067 const char * |
| 7134 | 3068 gaim_url_encode(const char *str) |
| 3069 { | |
|
10700
9505695acff8
[gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10636
diff
changeset
|
3070 const char *iter; |
| 7134 | 3071 static char buf[BUF_LEN]; |
|
10700
9505695acff8
[gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10636
diff
changeset
|
3072 char utf_char[6]; |
| 7631 | 3073 guint i, j = 0; |
| 7134 | 3074 |
| 3075 g_return_val_if_fail(str != NULL, NULL); | |
|
10700
9505695acff8
[gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10636
diff
changeset
|
3076 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
|
3077 |
|
9505695acff8
[gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10636
diff
changeset
|
3078 iter = str; |
|
9505695acff8
[gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10636
diff
changeset
|
3079 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
|
3080 gunichar c = g_utf8_get_char(iter); |
| 10800 | 3081 /* If the character is an ASCII character and is alphanumeric |
| 3082 * no need to escape */ | |
| 3083 if (c < 128 && isalnum(c)) { | |
|
10700
9505695acff8
[gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10636
diff
changeset
|
3084 buf[j++] = c; |
|
9505695acff8
[gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10636
diff
changeset
|
3085 } else { |
|
9505695acff8
[gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10636
diff
changeset
|
3086 int bytes = g_unichar_to_utf8(c, utf_char); |
|
9505695acff8
[gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10636
diff
changeset
|
3087 for (i = 0; i < bytes; i++) { |
|
9505695acff8
[gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10636
diff
changeset
|
3088 if (j > (BUF_LEN - 4)) |
|
9505695acff8
[gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10636
diff
changeset
|
3089 break; |
|
9505695acff8
[gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10636
diff
changeset
|
3090 sprintf(buf + j, "%%%02x", utf_char[i] & 0xff); |
|
9505695acff8
[gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10636
diff
changeset
|
3091 j += 3; |
|
9505695acff8
[gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10636
diff
changeset
|
3092 } |
| 7134 | 3093 } |
| 3094 } | |
| 3095 | |
| 3096 buf[j] = '\0'; | |
| 3097 | |
| 3098 return buf; | |
| 3099 } | |
|
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3100 |
| 9054 | 3101 /* Originally lifted from |
| 3102 * http://www.oreillynet.com/pub/a/network/excerpt/spcookbook_chap03/index3.html | |
| 3103 * ... and slightly modified to be a bit more rfc822 compliant | |
| 3104 * ... and modified a bit more to make domain checking rfc1035 compliant | |
| 3105 * with the exception permitted in rfc1101 for domains to start with digit | |
| 3106 * but not completely checking to avoid conflicts with IP addresses | |
| 3107 */ | |
| 9045 | 3108 gboolean |
| 3109 gaim_email_is_valid(const char *address) | |
| 3110 { | |
| 3111 const char *c, *domain; | |
| 3112 static char *rfc822_specials = "()<>@,;:\\\"[]"; | |
| 3113 | |
| 9054 | 3114 /* first we validate the name portion (name@domain) (rfc822)*/ |
| 9045 | 3115 for (c = address; *c; c++) { |
| 3116 if (*c == '\"' && (c == address || *(c - 1) == '.' || *(c - 1) == '\"')) { | |
| 3117 while (*++c) { | |
| 9054 | 3118 if (*c == '\\') { |
| 3119 if (*c++ && *c < 127 && *c != '\n' && *c != '\r') continue; | |
| 3120 else return FALSE; | |
| 3121 } | |
| 9045 | 3122 if (*c == '\"') break; |
| 9054 | 3123 if (*c < ' ' || *c >= 127) return FALSE; |
| 9045 | 3124 } |
| 3125 if (!*c++) return FALSE; | |
| 3126 if (*c == '@') break; | |
| 3127 if (*c != '.') return FALSE; | |
| 3128 continue; | |
| 3129 } | |
| 3130 if (*c == '@') break; | |
| 3131 if (*c <= ' ' || *c >= 127) return FALSE; | |
| 3132 if (strchr(rfc822_specials, *c)) return FALSE; | |
| 3133 } | |
| 9054 | 3134 /* strictly we should return false if (*(c - 1) == '.') too, but I think |
| 3135 * we should permit user.@domain type addresses - they do work :) */ | |
| 3136 if (c == address) return FALSE; | |
| 3137 | |
| 3138 /* next we validate the domain portion (name@domain) (rfc1035 & rfc1011) */ | |
| 9045 | 3139 if (!*(domain = ++c)) return FALSE; |
| 3140 do { | |
| 9054 | 3141 if (*c == '.' && (c == domain || *(c - 1) == '.' || *(c - 1) == '-')) |
| 3142 return FALSE; | |
| 3143 if (*c == '-' && *(c - 1) == '.') return FALSE; | |
| 3144 if ((*c < '0' && *c != '-' && *c != '.') || (*c > '9' && *c < 'A') || | |
| 3145 (*c > 'Z' && *c < 'a') || (*c > 'z')) return FALSE; | |
| 9045 | 3146 } while (*++c); |
| 3147 | |
| 9054 | 3148 if (*(c - 1) == '-') return FALSE; |
| 3149 | |
| 3150 return ((c - domain) > 3 ? TRUE : FALSE); | |
| 9045 | 3151 } |
| 3152 | |
| 9670 | 3153 /** Stolen from gnome_uri_list_extract_uris **/ |
| 3154 GList* gaim_uri_list_extract_uris (const gchar* uri_list) { | |
| 3155 const gchar *p, *q; | |
| 3156 gchar *retval; | |
| 3157 GList *result = NULL; | |
| 3158 | |
| 3159 g_return_val_if_fail (uri_list != NULL, NULL); | |
| 3160 | |
| 3161 p = uri_list; | |
| 3162 | |
| 3163 /* We don't actually try to validate the URI according to RFC | |
| 3164 * 2396, or even check for allowed characters - we just ignore | |
| 3165 * comments and trim whitespace off the ends. We also | |
| 3166 * allow LF delimination as well as the specified CRLF. | |
| 3167 */ | |
| 3168 while (p) { | |
| 3169 if (*p != '#') { | |
| 3170 while (isspace(*p)) | |
| 3171 p++; | |
| 3172 | |
| 3173 q = p; | |
| 3174 while (*q && (*q != '\n') && (*q != '\r')) | |
| 3175 q++; | |
| 3176 | |
| 3177 if (q > p) { | |
| 3178 q--; | |
| 3179 while (q > p && isspace(*q)) | |
| 3180 q--; | |
| 3181 | |
| 3182 retval = (gchar*)g_malloc (q - p + 2); | |
| 3183 strncpy (retval, p, q - p + 1); | |
| 3184 retval[q - p + 1] = '\0'; | |
| 3185 | |
| 3186 result = g_list_prepend (result, retval); | |
| 3187 } | |
| 3188 } | |
| 3189 p = strchr (p, '\n'); | |
| 3190 if (p) | |
| 3191 p++; | |
| 3192 } | |
| 3193 | |
| 3194 return g_list_reverse (result); | |
| 3195 } | |
| 3196 | |
| 3197 | |
| 3198 /** Stolen from gaim_uri_list_extract_filenames **/ | |
| 3199 GList* gaim_uri_list_extract_filenames (const gchar* uri_list) { | |
| 3200 GList *tmp_list, *node, *result; | |
| 3201 | |
| 3202 g_return_val_if_fail (uri_list != NULL, NULL); | |
| 3203 | |
| 3204 result = gaim_uri_list_extract_uris (uri_list); | |
| 3205 | |
| 3206 tmp_list = result; | |
| 3207 while (tmp_list) { | |
| 3208 gchar *s = (gchar*)tmp_list->data; | |
| 3209 | |
| 3210 node = tmp_list; | |
| 3211 tmp_list = tmp_list->next; | |
| 3212 | |
| 3213 if (!strncmp (s, "file:", 5)) { | |
| 3214 node->data = g_filename_from_uri (s, NULL, NULL); | |
| 3215 /* not sure if this fallback is useful at all */ | |
| 3216 if (!node->data) node->data = g_strdup (s+5); | |
| 3217 } else { | |
| 3218 result = g_list_remove_link(result, node); | |
| 3219 g_list_free_1 (node); | |
| 3220 } | |
| 3221 g_free (s); | |
| 3222 } | |
| 3223 return result; | |
| 3224 } | |
| 9054 | 3225 |
|
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3226 /************************************************************************** |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3227 * UTF8 String Functions |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3228 **************************************************************************/ |
| 9642 | 3229 gchar * |
|
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3230 gaim_utf8_try_convert(const char *str) |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3231 { |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3232 gsize converted; |
| 9642 | 3233 gchar *utf8; |
|
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3234 |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3235 g_return_val_if_fail(str != NULL, NULL); |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3236 |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3237 if (g_utf8_validate(str, -1, NULL)) { |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3238 return g_strdup(str); |
|
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 |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3241 utf8 = g_locale_to_utf8(str, -1, &converted, NULL, NULL); |
| 9642 | 3242 if (utf8 != NULL) |
| 3243 return utf8; | |
|
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3244 |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3245 utf8 = g_convert(str, -1, "UTF-8", "ISO-8859-15", &converted, NULL, NULL); |
| 9642 | 3246 if ((utf8 != NULL) && (converted == strlen(str))) |
| 3247 return utf8; | |
| 3248 | |
| 3249 if (utf8 != NULL) | |
|
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3250 g_free(utf8); |
| 9642 | 3251 |
| 3252 return NULL; | |
|
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3253 } |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3254 |
| 10258 | 3255 #define utf8_first(x) ((x & 0x80) == 0 || (x & 0xe0) == 0xc0 \ |
| 3256 || (x & 0xf0) == 0xe0 || (x & 0xf8) == 0xf) | |
| 3257 gchar * | |
| 3258 gaim_utf8_salvage(const char *str) | |
| 3259 { | |
| 3260 GString *workstr; | |
| 3261 const char *end; | |
| 3262 | |
| 3263 g_return_val_if_fail(str != NULL, NULL); | |
| 3264 | |
| 3265 workstr = g_string_sized_new(strlen(str)); | |
| 3266 | |
| 3267 do { | |
| 3268 g_utf8_validate(str, -1, &end); | |
| 3269 workstr = g_string_append_len(workstr, str, end - str); | |
| 3270 str = end; | |
| 3271 if (*str == '\0') | |
| 3272 break; | |
| 3273 do { | |
| 3274 workstr = g_string_append_c(workstr, '?'); | |
| 3275 str++; | |
| 3276 } while (!utf8_first(*str)); | |
| 3277 } while (*str != '\0'); | |
| 3278 | |
| 3279 return g_string_free(workstr, FALSE); | |
| 3280 } | |
| 3281 | |
| 9161 | 3282 char * |
| 3283 gaim_utf8_ncr_decode(const char *in) | |
| 3284 { | |
| 3285 GString *out = g_string_new(""); | |
| 3286 int i; | |
| 3287 | |
| 3288 g_return_val_if_fail(in != NULL, NULL); | |
| 3289 g_return_val_if_fail(g_utf8_validate(in, -1, NULL), NULL); | |
| 3290 | |
| 3291 for (i = 0; in[i]; i += 1) { | |
| 3292 gboolean ncr_found_p = FALSE; | |
| 3293 if (in[i] == '&' && in[i + 1] == '#' && isdigit(in[i + 2])) { | |
| 3294 gunichar wc; | |
| 3295 int j; | |
| 3296 for (wc = 0, j = i + 2; isdigit(in[j]); j += 1) { | |
| 3297 wc *= 10; | |
| 3298 wc += in[j] - '0'; | |
| 3299 } | |
| 3300 if (in[j] == ';') { /* Technically not completely correct */ | |
| 3301 g_string_append_unichar(out, wc); | |
| 3302 i = j; | |
| 3303 ncr_found_p = TRUE; | |
| 3304 } | |
| 3305 } | |
| 3306 if (!ncr_found_p) { | |
| 3307 g_string_append_c(out, in[i]); | |
| 3308 } | |
| 3309 } | |
| 3310 return g_string_free(out, FALSE); | |
| 3311 } | |
| 3312 | |
|
7108
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3313 int |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3314 gaim_utf8_strcasecmp(const char *a, const char *b) |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3315 { |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3316 char *a_norm = NULL; |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3317 char *b_norm = NULL; |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3318 int ret = -1; |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3319 |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3320 if(!a && b) |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3321 return -1; |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3322 else if(!b && a) |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3323 return 1; |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3324 else if(!a && !b) |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3325 return 0; |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3326 |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3327 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
|
3328 { |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3329 gaim_debug_error("gaim_utf8_strcasecmp", |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3330 "One or both parameters are invalid UTF8\n"); |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3331 return ret; |
|
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 |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3334 a_norm = g_utf8_casefold(a, -1); |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3335 b_norm = g_utf8_casefold(b, -1); |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3336 ret = g_utf8_collate(a_norm, b_norm); |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3337 g_free(a_norm); |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3338 g_free(b_norm); |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3339 |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3340 return ret; |
|
6faeeecab0dc
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7107
diff
changeset
|
3341 } |
| 7162 | 3342 |
| 7564 | 3343 gboolean gaim_message_meify(char *message, size_t len) |
| 3344 { | |
| 3345 char *c; | |
| 3346 gboolean inside_html = FALSE; | |
| 3347 | |
| 3348 g_return_val_if_fail(message != NULL, FALSE); | |
| 3349 | |
| 3350 if(len == -1) | |
| 3351 len = strlen(message); | |
| 3352 | |
| 3353 for (c = message; *c; c++, len--) { | |
| 3354 if(inside_html) { | |
| 3355 if(*c == '>') | |
| 3356 inside_html = FALSE; | |
| 3357 } else { | |
| 3358 if(*c == '<') | |
| 3359 inside_html = TRUE; | |
| 3360 else | |
| 3361 break; | |
| 3362 } | |
| 3363 } | |
| 3364 | |
| 3365 if(*c && !g_ascii_strncasecmp(c, "/me ", 4)) { | |
| 3366 memmove(c, c+4, len-3); | |
| 3367 return TRUE; | |
| 3368 } | |
| 3369 | |
| 3370 return FALSE; | |
| 3371 } | |
| 3372 | |
| 7889 | 3373 char *gaim_text_strip_mnemonic(const char *in) |
| 3374 { | |
| 3375 char *out; | |
| 3376 char *a; | |
| 8561 | 3377 char *a0; |
| 7889 | 3378 const char *b; |
| 3379 | |
| 3380 g_return_val_if_fail(in != NULL, NULL); | |
| 3381 | |
| 3382 out = g_malloc(strlen(in)+1); | |
| 3383 a = out; | |
| 3384 b = in; | |
| 3385 | |
| 8561 | 3386 a0 = a; /* The last non-space char seen so far, or the first char */ |
| 3387 | |
| 7889 | 3388 while(*b) { |
| 3389 if(*b == '_') { | |
| 8561 | 3390 if(a > out && b > in && *(b-1) == '(' && *(b+1) && !(*(b+1) & 0x80) && *(b+2) == ')') { |
| 3391 /* Detected CJK style shortcut (Bug 875311) */ | |
| 3392 a = a0; /* undo the left parenthesis */ | |
| 3393 b += 3; /* and skip the whole mess */ | |
| 3394 } else if(*(b+1) == '_') { | |
| 7889 | 3395 *(a++) = '_'; |
| 3396 b += 2; | |
| 8561 | 3397 a0 = a; |
| 7889 | 3398 } else { |
| 3399 b++; | |
| 3400 } | |
| 8561 | 3401 /* We don't want to corrupt the middle of UTF-8 characters */ |
| 3402 } else if (!(*b & 0x80)) { /* other 1-byte char */ | |
| 3403 if (*b != ' ') | |
| 3404 a0 = a; | |
| 3405 *(a++) = *(b++); | |
| 7889 | 3406 } else { |
| 8561 | 3407 /* Multibyte utf8 char, don't look for _ inside these */ |
| 3408 int n = 0; | |
| 3409 int i; | |
| 3410 if ((*b & 0xe0) == 0xc0) { | |
| 3411 n = 2; | |
| 3412 } else if ((*b & 0xf0) == 0xe0) { | |
| 3413 n = 3; | |
| 3414 } else if ((*b & 0xf8) == 0xf0) { | |
| 3415 n = 4; | |
| 3416 } else if ((*b & 0xfc) == 0xf8) { | |
| 3417 n = 5; | |
| 3418 } else if ((*b & 0xfe) == 0xfc) { | |
| 3419 n = 6; | |
| 3420 } else { /* Illegal utf8 */ | |
| 3421 n = 1; | |
| 3422 } | |
| 3423 a0 = a; /* unless we want to delete CJK spaces too */ | |
| 3424 for (i = 0; i < n && *b; i += 1) { | |
| 3425 *(a++) = *(b++); | |
| 3426 } | |
| 7889 | 3427 } |
| 3428 } | |
| 3429 *a = '\0'; | |
| 3430 | |
| 3431 return out; | |
| 3432 } | |
| 3433 | |
| 9926 | 3434 const char* gaim_unescape_filename(const char *escaped) { |
| 3435 return gaim_url_decode(escaped); | |
| 3436 } | |
| 3437 | |
| 3438 | |
| 3439 /* this is almost identical to gaim_url_encode (hence gaim_url_decode | |
| 3440 * being used above), but we want to keep certain characters unescaped | |
| 3441 * for compat reasons */ | |
| 3442 const char * | |
| 3443 gaim_escape_filename(const char *str) | |
| 3444 { | |
|
10700
9505695acff8
[gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10636
diff
changeset
|
3445 const char *iter; |
| 9926 | 3446 static char buf[BUF_LEN]; |
|
10700
9505695acff8
[gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10636
diff
changeset
|
3447 char utf_char[6]; |
| 9926 | 3448 guint i, j = 0; |
| 3449 | |
| 3450 g_return_val_if_fail(str != NULL, NULL); | |
|
10700
9505695acff8
[gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10636
diff
changeset
|
3451 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
|
3452 |
|
9505695acff8
[gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10636
diff
changeset
|
3453 iter = str; |
|
9505695acff8
[gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10636
diff
changeset
|
3454 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
|
3455 gunichar c = g_utf8_get_char(iter); |
|
9505695acff8
[gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10636
diff
changeset
|
3456 /* If the character is an ASCII character and is alphanumeric, |
|
9505695acff8
[gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10636
diff
changeset
|
3457 * or one of the specified values, no need to escape */ |
| 10800 | 3458 if (c < 128 && (isalnum(c) || c == '@' || c == '-' || |
|
10700
9505695acff8
[gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10636
diff
changeset
|
3459 c == '_' || c == '.' || c == '#')) { |
|
9505695acff8
[gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10636
diff
changeset
|
3460 buf[j++] = c; |
|
9505695acff8
[gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10636
diff
changeset
|
3461 } else { |
|
9505695acff8
[gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10636
diff
changeset
|
3462 int bytes = g_unichar_to_utf8(c, utf_char); |
|
9505695acff8
[gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10636
diff
changeset
|
3463 for (i = 0; i < bytes; i++) { |
|
9505695acff8
[gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10636
diff
changeset
|
3464 if (j > (BUF_LEN - 4)) |
|
9505695acff8
[gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10636
diff
changeset
|
3465 break; |
|
9505695acff8
[gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10636
diff
changeset
|
3466 sprintf(buf + j, "%%%02x", utf_char[i] & 0xff); |
|
9505695acff8
[gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10636
diff
changeset
|
3467 j += 3; |
|
9505695acff8
[gaim-migrate @ 12273]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10636
diff
changeset
|
3468 } |
| 9926 | 3469 } |
| 3470 } | |
| 3471 | |
| 3472 buf[j] = '\0'; | |
| 3473 | |
| 3474 return buf; | |
| 3475 } | |
| 3476 | |
| 3477 |
