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