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