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