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