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