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