Thu, 15 Jan 2004 22:20:29 +0000
[gaim-migrate @ 8817]
Tim 'marv' Ringenbach wrote us a wonderful core/ui split chat room list
thing-a-ma-jig. I've taken the liberty of adding jabber support to it
as well.
committer: Nathan Walp <nwalp@pidgin.im>
| 6513 | 1 | /* |
| 2 | * gaim | |
| 3 | * | |
| 4 | * Some code copyright 2003 Tim Ringenbach <omarvo@hotmail.com> | |
| 5 | * (marv on irc.freenode.net) | |
| 6 | * | |
| 7 | * This program is free software; you can redistribute it and/or modify | |
| 8 | * it under the terms of the GNU General Public License as published by | |
| 9 | * the Free Software Foundation; either version 2 of the License, or | |
| 10 | * (at your option) any later version. | |
| 11 | * | |
| 12 | * This program is distributed in the hope that it will be useful, | |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 15 | * GNU General Public License for more details. | |
| 16 | * | |
| 17 | * You should have received a copy of the GNU General Public License | |
| 18 | * along with this program; if not, write to the Free Software | |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 20 | * | |
| 21 | */ | |
| 22 | ||
| 23 | #ifdef HAVE_CONFIG_H | |
| 24 | #include "config.h" | |
| 25 | #endif | |
| 26 | ||
| 27 | #include "prpl.h" | |
| 28 | #include "debug.h" | |
| 7827 | 29 | #include "yahoo.h" |
| 6513 | 30 | |
|
6546
6a14dfe43d5c
[gaim-migrate @ 7068]
Mark Doliner <markdoliner@pidgin.im>
parents:
6513
diff
changeset
|
31 | #include <string.h> |
|
6a14dfe43d5c
[gaim-migrate @ 7068]
Mark Doliner <markdoliner@pidgin.im>
parents:
6513
diff
changeset
|
32 | |
| 7827 | 33 | /** |
| 34 | * Encode some text to send to the yahoo server. | |
| 35 | * | |
| 36 | * @param gc The connection handle. | |
| 37 | * @param str The null terminated utf8 string to encode. | |
| 38 | * @param utf8 If not @c NULL, whether utf8 is okay or not. | |
| 39 | * Even if it is okay, we may not use it. If we | |
| 40 | * used it, we set this to @c TRUE, else to | |
| 41 | * @c FALSE. If @c NULL, false is assumed, and | |
| 42 | * it is not dereferenced. | |
| 43 | * @return The g_malloced string in the appropriate encoding. | |
| 44 | */ | |
| 45 | char *yahoo_string_encode(GaimConnection *gc, const char *str, gboolean *utf8) | |
| 46 | { | |
| 47 | char *ret; | |
| 48 | char *to_codeset; | |
| 49 | ||
| 50 | if (utf8 && *utf8) /* FIXME: maybe don't use utf8 if it'll fit in latin1 */ | |
| 51 | return g_strdup(str); | |
| 52 | ||
| 53 | to_codeset = "ISO-8859-1"; | |
| 54 | ||
| 55 | ret = g_convert_with_fallback(str, strlen(str), to_codeset, "UTF-8", NULL, NULL, NULL, NULL); | |
| 56 | if (ret) | |
| 57 | return ret; | |
| 58 | else | |
| 59 | return g_strdup(""); | |
| 60 | } | |
| 61 | ||
| 62 | /** | |
| 63 | * Decode some text received from the server. | |
| 64 | * | |
| 65 | * @param gc The gc handle. | |
| 66 | * @param str The null terminated string to decode. | |
| 67 | * @param utf8 Did the server tell us it was supposed to be utf8? | |
| 68 | * @return The decoded, utf-8 string, which must be g_free()'d. | |
| 69 | */ | |
| 70 | char *yahoo_string_decode(GaimConnection *gc, const char *str, gboolean utf8) | |
| 71 | { | |
| 72 | char *ret; | |
| 73 | char *from_codeset; | |
| 74 | ||
| 75 | if (utf8) { | |
| 76 | if (g_utf8_validate(str, -1, NULL)) | |
| 77 | return g_strdup(str); | |
| 78 | } | |
| 79 | ||
| 80 | from_codeset = "ISO-8859-1"; | |
| 81 | ||
| 82 | ret = g_convert_with_fallback(str, strlen(str), "UTF-8", from_codeset, NULL, NULL, NULL, NULL); | |
| 83 | ||
| 84 | if (ret) | |
| 85 | return ret; | |
| 86 | else | |
| 87 | return g_strdup(""); | |
| 88 | } | |
| 89 | ||
| 90 | ||
| 6513 | 91 | |
| 92 | /* | |
| 93 | * I found these on some website but i don't know that they actually | |
| 94 | * work (or are supposed to work). I didn't impliment them yet. | |
| 95 | * | |
| 96 | * [0;30m ---black | |
| 97 | * [1;37m ---white | |
| 98 | * [0;37m ---tan | |
| 99 | * [0;38m ---light black | |
| 100 | * [1;39m ---dark blue | |
| 101 | * [0;32m ---green | |
| 102 | * [0;33m ---yellow | |
| 103 | * [0;35m ---pink | |
| 104 | * [1;35m ---purple | |
| 105 | * [1;30m ---light blue | |
| 106 | * [0;31m ---red | |
| 107 | * [0;34m ---blue | |
| 108 | * [0;36m ---aqua | |
| 109 | * (shift+comma)lyellow(shift+period) ---light yellow | |
| 110 | * (shift+comma)lgreen(shift+period) ---light green | |
| 111 | [2;30m <--white out | |
| 112 | ||
| 113 | */ | |
| 114 | ||
| 115 | ||
| 116 | static GHashTable *ht = NULL; | |
| 117 | ||
| 118 | void yahoo_init_colorht() | |
| 119 | { | |
| 120 | ht = g_hash_table_new(g_str_hash, g_str_equal); | |
| 6629 | 121 | /* the numbers in comments are what gyach uses, but i think they're incorrect */ |
| 6513 | 122 | g_hash_table_insert(ht, "30", "<FONT COLOR=\"#000000\">"); /* black */ |
| 123 | g_hash_table_insert(ht, "31", "<FONT COLOR=\"#0000FF\">"); /* blue */ | |
| 124 | g_hash_table_insert(ht, "32", "<FONT COLOR=\"#008080\">"); /* cyan */ /* 00b2b2 */ | |
| 125 | g_hash_table_insert(ht, "33", "<FONT COLOR=\"#808080\">"); /* gray */ /* 808080 */ | |
| 126 | g_hash_table_insert(ht, "34", "<FONT COLOR=\"#008000\">"); /* green */ /* 00c200 */ | |
| 127 | g_hash_table_insert(ht, "35", "<FONT COLOR=\"#FF0080\">"); /* pink */ /* ffafaf */ | |
| 128 | g_hash_table_insert(ht, "36", "<FONT COLOR=\"#800080\">"); /* purple */ /* b200b2 */ | |
| 129 | g_hash_table_insert(ht, "37", "<FONT COLOR=\"#FF8000\">"); /* orange */ /* ffff00 */ | |
| 130 | g_hash_table_insert(ht, "38", "<FONT COLOR=\"#FF0000\">"); /* red */ | |
| 131 | g_hash_table_insert(ht, "39", "<FONT COLOR=\"#808000\">"); /* olive */ /* 546b50 */ | |
| 132 | ||
| 133 | g_hash_table_insert(ht, "1", "<B>"); | |
| 134 | g_hash_table_insert(ht, "x1", "</B>"); | |
| 135 | g_hash_table_insert(ht, "2", "<I>"); | |
| 136 | g_hash_table_insert(ht, "x2", "</I>"); | |
| 137 | g_hash_table_insert(ht, "4", "<U>"); | |
| 138 | g_hash_table_insert(ht, "x4", "</U>"); | |
| 139 | ||
| 6629 | 140 | /* these just tell us the text they surround is supposed |
| 141 | * to be a link. gaim figures that out on its own so we | |
| 142 | * just ignore it. | |
| 143 | */ | |
| 144 | g_hash_table_insert(ht, "l", ""); /* link start */ | |
| 145 | g_hash_table_insert(ht, "xl", ""); /* link end */ | |
| 146 | ||
| 147 | ||
| 6513 | 148 | g_hash_table_insert(ht, "<black>", "<FONT COLOR=\"#000000\">"); |
| 149 | g_hash_table_insert(ht, "<blue>", "<FONT COLOR=\"#0000FF\">"); | |
| 150 | g_hash_table_insert(ht, "<cyan>", "<FONT COLOR=\"#008284\">"); | |
| 151 | g_hash_table_insert(ht, "<gray>", "<FONT COLOR=\"#848284\">"); | |
| 152 | g_hash_table_insert(ht, "<green>", "<FONT COLOR=\"#008200\">"); | |
| 153 | g_hash_table_insert(ht, "<pink>", "<FONT COLOR=\"#FF0084\">"); | |
| 154 | g_hash_table_insert(ht, "<purple>", "<FONT COLOR=\"#840084\">"); | |
| 155 | g_hash_table_insert(ht, "<orange>", "<FONT COLOR=\"#FF8000\">"); | |
| 156 | g_hash_table_insert(ht, "<red>", "<FONT COLOR=\"#FF0000\">"); | |
| 157 | g_hash_table_insert(ht, "<yellow>", "<FONT COLOR=\"#848200\">"); | |
| 158 | ||
| 159 | g_hash_table_insert(ht, "</black>", "</FONT>"); | |
| 160 | g_hash_table_insert(ht, "</blue>", "</FONT>"); | |
| 161 | g_hash_table_insert(ht, "</cyan>", "</FONT>"); | |
| 162 | g_hash_table_insert(ht, "</gray>", "</FONT>"); | |
| 163 | g_hash_table_insert(ht, "</green>", "</FONT>"); | |
| 164 | g_hash_table_insert(ht, "</pink>", "</FONT>"); | |
| 165 | g_hash_table_insert(ht, "</purple>", "</FONT>"); | |
| 166 | g_hash_table_insert(ht, "</orange>", "</FONT>"); | |
| 167 | g_hash_table_insert(ht, "</red>", "</FONT>"); | |
| 168 | g_hash_table_insert(ht, "</yellow>", "</FONT>"); | |
| 169 | ||
| 170 | /* remove these once we have proper support for <FADE> and <ALT> */ | |
| 171 | g_hash_table_insert(ht, "</fade>", ""); | |
| 172 | g_hash_table_insert(ht, "</alt>", ""); | |
| 173 | } | |
| 174 | ||
| 175 | void yahoo_dest_colorht() | |
| 176 | { | |
| 177 | g_hash_table_destroy(ht); | |
| 178 | } | |
| 179 | ||
| 6629 | 180 | static int point_to_html(int x) |
| 181 | { | |
| 182 | if (x < 9) | |
| 183 | return 1; | |
| 184 | if (x < 11) | |
| 185 | return 2; | |
| 186 | if (x < 13) | |
| 187 | return 3; | |
| 188 | if (x < 17) | |
| 189 | return 4; | |
| 190 | if (x < 25) | |
| 191 | return 5; | |
| 192 | if (x < 35) | |
| 193 | return 6; | |
| 194 | return 7; | |
| 195 | } | |
| 196 | static void _font_tags_fix_size(GString *tag, GString *dest) | |
| 197 | { | |
| 198 | char *x, *end; | |
| 199 | int size; | |
| 200 | ||
| 201 | if (((x = strstr(tag->str, "size"))) && ((x = strchr(tag->str, '=')))) { | |
| 202 | while (*x && !g_ascii_isdigit(*x)) | |
| 203 | x++; | |
| 204 | if (*x) { | |
| 205 | size = strtol(x, &end, 10); | |
| 206 | size = point_to_html(size); | |
| 207 | g_string_append_len(dest, tag->str, x - tag->str); | |
| 208 | g_string_append_printf(dest, "%d", size); | |
| 209 | g_string_append(dest, end); | |
| 210 | } else { | |
| 211 | g_string_append(dest, tag->str); | |
| 212 | return; | |
| 213 | } | |
| 214 | } else { | |
| 215 | g_string_append(dest, tag->str); | |
| 216 | return; | |
| 217 | } | |
| 218 | } | |
| 219 | ||
| 220 | char *yahoo_codes_to_html(const char *x) | |
| 6513 | 221 | { |
| 222 | GString *s, *tmp; | |
| 6629 | 223 | int i, j, xs, nomoreendtags = 0; /* s/endtags/closinganglebrackets */ |
| 6513 | 224 | char *match, *ret; |
| 225 | ||
| 226 | ||
| 227 | s = g_string_sized_new(strlen(x)); | |
| 228 | ||
| 229 | for (i = 0, xs = strlen(x); i < xs; i++) { | |
| 230 | if ((x[i] == 0x1b) && (x[i+1] == '[')) { | |
| 231 | j = i + 1; | |
| 232 | ||
| 233 | while (j++ < xs) { | |
| 234 | if (x[j] != 'm') | |
| 235 | continue; | |
| 236 | else { | |
| 237 | tmp = g_string_new_len(x + i + 2, j - i - 2); | |
|
6621
2a18ef3e5224
[gaim-migrate @ 7145]
Robert McQueen <robot101@debian.org>
parents:
6546
diff
changeset
|
238 | if (tmp->str[0] == '#') |
| 6513 | 239 | g_string_append_printf(s, "<FONT COLOR=\"%s\">", tmp->str); |
|
6546
6a14dfe43d5c
[gaim-migrate @ 7068]
Mark Doliner <markdoliner@pidgin.im>
parents:
6513
diff
changeset
|
240 | else if ((match = (char *) g_hash_table_lookup(ht, tmp->str))) |
| 6513 | 241 | g_string_append(s, match); |
| 242 | else { | |
| 243 | gaim_debug(GAIM_DEBUG_ERROR, "yahoo", | |
| 244 | "Unknown ansi code 'ESC[%sm'.\n", tmp->str); | |
| 245 | g_string_free(tmp, TRUE); | |
| 246 | break; | |
| 247 | } | |
| 248 | ||
| 249 | i = j; | |
| 250 | g_string_free(tmp, TRUE); | |
| 251 | break; | |
| 252 | } | |
| 253 | } | |
| 254 | ||
| 255 | ||
| 256 | } else if (!nomoreendtags && (x[i] == '<')) { | |
| 6629 | 257 | j = i; |
| 6513 | 258 | |
| 259 | while (j++ < xs) { | |
| 260 | if (x[j] != '>') | |
| 261 | if (j == xs) { | |
| 262 | g_string_append_c(s, '<'); | |
| 263 | nomoreendtags = 1; | |
| 264 | } | |
| 265 | else | |
| 266 | continue; | |
| 267 | else { | |
| 268 | tmp = g_string_new_len(x + i, j - i + 1); | |
| 269 | g_string_ascii_down(tmp); | |
| 270 | ||
|
6546
6a14dfe43d5c
[gaim-migrate @ 7068]
Mark Doliner <markdoliner@pidgin.im>
parents:
6513
diff
changeset
|
271 | if ((match = (char *) g_hash_table_lookup(ht, tmp->str))) |
| 6513 | 272 | g_string_append(s, match); |
| 6629 | 273 | else if (!strncmp(tmp->str, "<fade ", 6) || |
| 274 | !strncmp(tmp->str, "<alt ", 5) || | |
| 275 | !strncmp(tmp->str, "<snd ", 5)) { | |
| 6513 | 276 | |
| 6629 | 277 | /* remove this if gtkimhtml ever supports any of these */ |
| 6513 | 278 | i = j; |
| 279 | g_string_free(tmp, TRUE); | |
| 280 | break; | |
| 281 | ||
| 6629 | 282 | } else if (!strncmp(tmp->str, "<font ", 6)) { |
| 283 | _font_tags_fix_size(tmp, s); | |
| 6513 | 284 | } else { |
| 285 | g_string_append_c(s, '<'); | |
| 286 | g_string_free(tmp, TRUE); | |
| 287 | break; | |
| 288 | } | |
| 289 | ||
| 290 | i = j; | |
| 291 | g_string_free(tmp, TRUE); | |
| 292 | break; | |
| 293 | } | |
| 294 | ||
| 295 | } | |
| 296 | ||
| 297 | ||
| 298 | ||
| 299 | } else { | |
| 300 | g_string_append_c(s, x[i]); | |
| 301 | } | |
| 302 | } | |
| 303 | ||
| 304 | ret = s->str; | |
| 305 | g_string_free(s, FALSE); | |
| 6629 | 306 | gaim_debug(GAIM_DEBUG_MISC, "yahoo", "yahoo_codes_to_html: Returning string: '%s'.\n", ret); |
| 6513 | 307 | return ret; |
| 308 | } | |
| 6629 | 309 | |
| 310 | /* borrowed from gtkimhtml */ | |
| 311 | #define MAX_FONT_SIZE 7 | |
| 312 | #define POINT_SIZE(x) (_point_sizes [MIN ((x), MAX_FONT_SIZE) - 1]) | |
| 313 | static gint _point_sizes [] = { 8, 10, 12, 14, 20, 30, 40 }; | |
| 314 | ||
| 315 | enum fatype { size, color, face, junk }; | |
| 316 | typedef struct { | |
| 317 | enum fatype type; | |
| 318 | union { | |
| 319 | int size; | |
| 320 | char *color; | |
| 321 | char *face; | |
| 322 | char *junk; | |
| 323 | } u; | |
| 324 | } fontattr; | |
| 325 | ||
| 326 | static void fontattr_free(fontattr *f) | |
| 327 | { | |
| 328 | if (f->type == color) | |
| 329 | g_free(f->u.color); | |
| 330 | else if (f->type == face) | |
| 331 | g_free(f->u.face); | |
| 332 | g_free(f); | |
| 333 | } | |
| 334 | ||
| 335 | static void yahoo_htc_queue_cleanup(GQueue *q) | |
| 336 | { | |
| 337 | char *tmp; | |
| 338 | ||
| 339 | while ((tmp = g_queue_pop_tail(q))) | |
| 340 | g_free(tmp); | |
| 341 | g_queue_free(q); | |
| 342 | } | |
| 343 | ||
| 344 | static void _parse_font_tag(const char *src, GString *dest, int *i, int *j, | |
| 345 | int len, GQueue *colors, GQueue *tags, GQueue *ftattr) | |
| 346 | { | |
| 347 | ||
| 348 | int m, n, vstart; | |
| 349 | gboolean quote = 0, done = 0; | |
| 350 | ||
| 351 | m = *j; | |
| 352 | ||
| 353 | while (1) { | |
| 354 | m++; | |
| 355 | ||
| 356 | if (m >= len) { | |
| 357 | g_string_append(dest, &src[*i]); | |
| 358 | *i = len; | |
| 359 | break; | |
| 360 | } | |
| 361 | ||
| 362 | if (src[m] == '=') { | |
| 363 | n = vstart = m; | |
| 364 | while (1) { | |
| 365 | n++; | |
| 366 | ||
| 367 | if (n >= len) { | |
| 368 | m = n; | |
| 369 | break; | |
| 370 | } | |
| 371 | ||
|
6631
a2608d595a32
[gaim-migrate @ 7155]
Björn Voigt <bjoern@cs.tu-berlin.de>
parents:
6629
diff
changeset
|
372 | if (src[n] == '"') { |
| 6629 | 373 | if (!quote) { |
| 374 | quote = 1; | |
| 375 | vstart = n; | |
| 376 | continue; | |
| 377 | } else { | |
| 378 | done = 1; | |
| 379 | } | |
|
6631
a2608d595a32
[gaim-migrate @ 7155]
Björn Voigt <bjoern@cs.tu-berlin.de>
parents:
6629
diff
changeset
|
380 | } |
| 6629 | 381 | |
| 382 | if (!quote && ((src[n] == ' ') || (src[n] == '>'))) | |
| 383 | done = 1; | |
| 384 | ||
| 385 | if (done) { | |
| 386 | if (!g_ascii_strncasecmp(&src[*j+1], "FACE", m - *j - 1)) { | |
| 387 | fontattr *f; | |
| 388 | ||
| 389 | f = g_new(fontattr, 1); | |
| 390 | f->type = face; | |
| 391 | f->u.face = g_strndup(&src[vstart+1], n-vstart-1); | |
| 392 | if (!ftattr) | |
| 393 | ftattr = g_queue_new(); | |
| 394 | g_queue_push_tail(ftattr, f); | |
| 395 | m = n; | |
| 396 | break; | |
| 397 | } else if (!g_ascii_strncasecmp(&src[*j+1], "SIZE", m - *j - 1)) { | |
| 398 | fontattr *f; | |
| 399 | ||
| 400 | f = g_new(fontattr, 1); | |
| 401 | f->type = size; | |
| 402 | f->u.size = POINT_SIZE(strtol(&src[vstart+1], NULL, 10)); | |
| 403 | if (!ftattr) | |
| 404 | ftattr = g_queue_new(); | |
| 405 | g_queue_push_tail(ftattr, f); | |
| 406 | m = n; | |
| 407 | break; | |
| 408 | } else if (!g_ascii_strncasecmp(&src[*j+1], "COLOR", m - *j - 1)) { | |
| 409 | fontattr *f; | |
| 410 | ||
| 411 | f = g_new(fontattr, 1); | |
| 412 | f->type = color; | |
| 413 | f->u.color = g_strndup(&src[vstart+1], n-vstart-1); | |
| 414 | if (!ftattr) | |
| 415 | ftattr = g_queue_new(); | |
| 416 | g_queue_push_head(ftattr, f); | |
| 417 | m = n; | |
| 418 | break; | |
| 419 | } else { | |
| 420 | fontattr *f; | |
| 421 | ||
| 422 | f = g_new(fontattr, 1); | |
| 423 | f->type = junk; | |
| 424 | f->u.junk = g_strndup(&src[*j+1], n-*j); | |
| 425 | if (!ftattr) | |
| 426 | ftattr = g_queue_new(); | |
| 427 | g_queue_push_tail(ftattr, f); | |
| 428 | m = n; | |
| 429 | break; | |
| 430 | } | |
| 431 | ||
| 432 | } | |
| 433 | } | |
| 434 | } | |
| 435 | ||
| 436 | if (src[m] == ' ') | |
| 437 | *j = m; | |
| 438 | ||
| 439 | ||
| 440 | ||
| 441 | if (src[m] == '>') { | |
| 442 | gboolean needendtag = 0; | |
| 443 | fontattr *f; | |
| 444 | GString *tmp = g_string_new(NULL); | |
| 445 | char *colorstr; | |
| 446 | ||
| 447 | if (!g_queue_is_empty(ftattr)) { | |
| 448 | while ((f = g_queue_pop_tail(ftattr))) { | |
| 449 | switch (f->type) { | |
| 450 | case size: | |
| 451 | if (!needendtag) { | |
| 452 | needendtag = 1; | |
| 453 | g_string_append(dest, "<font "); | |
| 454 | } | |
| 455 | ||
| 456 | g_string_append_printf(dest, "size=\"%d\" ", f->u.size); | |
| 457 | fontattr_free(f); | |
| 458 | break; | |
| 459 | case face: | |
| 460 | if (!needendtag) { | |
| 461 | needendtag = 1; | |
| 462 | g_string_append(dest, "<font "); | |
| 463 | } | |
| 464 | ||
| 465 | g_string_append_printf(dest, "face=\"%s\" ", f->u.face); | |
| 466 | fontattr_free(f); | |
| 467 | break; | |
| 468 | case junk: | |
| 469 | if (!needendtag) { | |
| 470 | needendtag = 1; | |
| 471 | g_string_append(dest, "<font "); | |
| 472 | } | |
| 473 | ||
| 474 | g_string_append(dest, f->u.junk); | |
| 475 | fontattr_free(f); | |
| 476 | break; | |
| 477 | ||
| 478 | case color: | |
| 479 | if (needendtag) { | |
| 480 | g_string_append(tmp, "</font>"); | |
| 481 | dest->str[dest->len-1] = '>'; | |
| 482 | needendtag = 0; | |
| 483 | } | |
| 484 | ||
| 485 | colorstr = g_queue_peek_tail(colors); | |
| 486 | g_string_append(tmp, colorstr ? colorstr : "\033[#000000m"); | |
| 487 | g_string_append_printf(dest, "\033[%sm", f->u.color); | |
| 488 | g_queue_push_tail(colors, g_strdup_printf("\033[%sm", f->u.color)); | |
| 489 | fontattr_free(f); | |
| 490 | break; | |
| 491 | } | |
| 492 | } | |
| 493 | ||
| 494 | g_queue_free(ftattr); | |
| 495 | ftattr = NULL; | |
| 496 | ||
| 497 | if (needendtag) { | |
| 498 | dest->str[dest->len-1] = '>'; | |
| 499 | g_queue_push_tail(tags, g_strdup("</font>")); | |
| 500 | g_string_free(tmp, TRUE); | |
| 501 | } else { | |
| 502 | g_queue_push_tail(tags, tmp->str); | |
| 503 | g_string_free(tmp, FALSE); | |
| 504 | } | |
| 505 | } | |
| 506 | ||
| 507 | *i = *j = m; | |
| 508 | break; | |
| 509 | } | |
| 510 | } | |
| 511 | ||
| 512 | } | |
| 513 | ||
| 514 | char *yahoo_html_to_codes(const char *src) | |
| 515 | { | |
|
6631
a2608d595a32
[gaim-migrate @ 7155]
Björn Voigt <bjoern@cs.tu-berlin.de>
parents:
6629
diff
changeset
|
516 | int i, j, len; |
| 6629 | 517 | GString *dest; |
| 518 | char *ret, *esc; | |
| 519 | GQueue *colors, *tags; | |
| 520 | GQueue *ftattr = NULL; | |
| 521 | ||
| 522 | ||
| 523 | colors = g_queue_new(); | |
| 524 | tags = g_queue_new(); | |
| 525 | ||
| 526 | dest = g_string_sized_new(strlen(src)); | |
| 527 | ||
| 528 | for (i = 0, len = strlen(src); i < len; i++) { | |
| 529 | ||
| 530 | if (src[i] == '<') { | |
| 531 | j = i; | |
| 532 | ||
| 533 | while (1) { | |
| 534 | j++; | |
| 535 | ||
| 536 | if (j >= len) { /* no '>' */ | |
| 537 | g_string_append_len(dest, &src[i], len - i); | |
| 538 | i = len; | |
| 539 | ||
| 540 | break; | |
| 541 | } | |
| 542 | ||
| 543 | if (src[j] == '<') { | |
| 544 | g_string_append_len(dest, &src[i], j - i); | |
| 545 | i = j - 1; | |
| 546 | if (ftattr) { | |
| 547 | fontattr *f; | |
| 548 | ||
| 549 | while ((f = g_queue_pop_head(ftattr))) | |
| 550 | fontattr_free(f); | |
| 551 | g_queue_free(ftattr); | |
| 552 | ftattr = NULL; | |
| 553 | } | |
| 554 | break; | |
| 555 | } | |
| 556 | ||
| 557 | if (src[j] == ' ') { | |
| 558 | if (!g_ascii_strncasecmp(&src[i+1], "BODY", j - i - 1)) { | |
| 559 | char *t = strchr(&src[j], '>'); | |
| 560 | if (!t) { | |
| 561 | g_string_append(dest, &src[i]); | |
| 562 | i = len; | |
| 563 | break; | |
| 564 | } else { | |
| 565 | i = t - src; | |
| 566 | break; | |
| 567 | } | |
| 568 | } else if (g_ascii_strncasecmp(&src[i+1], "FONT", j - i - 1)) { /* not interested! */ | |
| 569 | while (1) { | |
| 570 | if (++j >= len) { | |
| 571 | g_string_append(dest, &src[i]); | |
| 572 | i = len; | |
| 573 | break; | |
| 574 | } | |
| 575 | if (src[j] == '>') { | |
| 576 | g_string_append_len(dest, &src[i], j - i + 1); | |
| 577 | i = j; | |
| 578 | break; | |
| 579 | } | |
| 580 | } | |
| 581 | } else { /* yay we have a font tag */ | |
| 582 | _parse_font_tag(src, dest, &i, &j, len, colors, tags, ftattr); | |
| 583 | } | |
| 584 | ||
| 585 | break; | |
| 586 | } | |
| 587 | ||
| 588 | if (src[j] == '>') { | |
| 589 | int sublen = j - i - 1; | |
| 590 | ||
| 591 | if (sublen) { | |
| 592 | if (!g_ascii_strncasecmp(&src[i+1], "B", sublen)) { | |
| 593 | g_string_append(dest, "\033[1m"); | |
| 594 | } else if (!g_ascii_strncasecmp(&src[i+1], "/B", sublen)) { | |
| 595 | g_string_append(dest, "\033[x1m"); | |
| 596 | } else if (!g_ascii_strncasecmp(&src[i+1], "I", sublen)) { | |
| 597 | g_string_append(dest, "\033[2m"); | |
| 598 | } else if (!g_ascii_strncasecmp(&src[i+1], "/I", sublen)) { | |
| 599 | g_string_append(dest, "\033[x2m"); | |
| 600 | } else if (!g_ascii_strncasecmp(&src[i+1], "U", sublen)) { | |
| 601 | g_string_append(dest, "\033[4m"); | |
| 602 | } else if (!g_ascii_strncasecmp(&src[i+1], "/U", sublen)) { | |
| 603 | g_string_append(dest, "\033[x4m"); | |
| 604 | } else if (!g_ascii_strncasecmp(&src[i+1], "/BODY", sublen)) { | |
| 605 | /* mmm, </body> tags. *BURP* */ | |
| 606 | } else if (!g_ascii_strncasecmp(&src[i+1], "/FONT", sublen) && g_queue_peek_tail(tags)) { | |
| 607 | char *etag, *cl; | |
| 608 | ||
| 609 | etag = g_queue_pop_tail(tags); | |
| 610 | if (etag) { | |
| 611 | g_string_append(dest, etag); | |
| 612 | if (!strcmp(etag, "</font>")) { | |
| 613 | cl = g_queue_pop_tail(colors); | |
| 614 | if (cl) | |
| 615 | g_free(cl); | |
| 616 | } | |
| 617 | g_free(etag); | |
| 618 | } | |
| 619 | } else { | |
| 620 | g_string_append_len(dest, &src[i], j - i + 1); | |
| 621 | } | |
| 622 | } else { | |
| 623 | g_string_append_len(dest, &src[i], j - i + 1); | |
| 624 | } | |
| 625 | ||
| 626 | i = j; | |
| 627 | break; | |
| 628 | } | |
| 629 | ||
| 630 | } | |
| 631 | ||
| 632 | } else { | |
| 633 | g_string_append_c(dest, src[i]); | |
| 634 | } | |
| 635 | } | |
| 636 | ||
| 637 | ret = dest->str; | |
| 638 | g_string_free(dest, FALSE); | |
| 639 | ||
| 640 | esc = g_strescape(ret, NULL); | |
| 641 | gaim_debug(GAIM_DEBUG_MISC, "yahoo", "yahoo_html_to_codes: Returning string: '%s'.\n", esc); | |
| 642 | g_free(esc); | |
| 643 | ||
| 644 | yahoo_htc_queue_cleanup(colors); | |
| 645 | yahoo_htc_queue_cleanup(tags); | |
| 646 | ||
| 647 | return ret; | |
| 648 | } |