changeset 6629:bdc448cf4cb6

[gaim-migrate @ 7153] Tim Ringenbach (marv_sf) writes: " This patch makes sending colors in yahoo work. It also makes a few changing to receiving them, and addresses most of the problems with that patch (which I think were all related to the fact it didn't do outgoing colors). It now handles bold, italic, underline, font face, font size, and font color in both directions. It disables the background color button if the prpl is yahoo (in a generic way), and farthermore strips out any <body> tags that the user might try to type anyway (the yahoo server purposely mangles them). It also adds a line to g_strescape some debug messages because I got tired them of changing the color of my terminal. I think I got all the bugs out. If you run with -d or open the debug window, it will show you what both conversion function returned, which should help track down any problems." committer: Tailor Script <tailor@pidgin.im>
author Luke Schierer <lschiere@pidgin.im>
date Tue, 26 Aug 2003 01:34:51 +0000
parents 31b39f6c2142
children 889279abb909
files ChangeLog src/connection.h src/gtkconv.c src/protocols/yahoo/util.c src/protocols/yahoo/yahoo.c src/protocols/yahoo/yahoo.h
diffstat 6 files changed, 418 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Mon Aug 25 23:17:01 2003 +0000
+++ b/ChangeLog	Tue Aug 26 01:34:51 2003 +0000
@@ -6,7 +6,7 @@
 	* Added plugin dependency support.
 	* Rewrote the Perl plugin. All old scripts will break, but it offers
 	  a much better API for new scripts.
-	* Yahoo color receiving support (Tim Ringenbach (marv_sf))
+	* Yahoo color support (Tim Ringenbach (marv_sf))
 	* Yahoo and MSN get info support (Nathan Poznick)
 	* Fixed Jabber registrations.
 	* Corrected problems with proxy preferences.
--- a/src/connection.h	Mon Aug 25 23:17:01 2003 +0000
+++ b/src/connection.h	Tue Aug 26 01:34:51 2003 +0000
@@ -36,7 +36,8 @@
 typedef enum
 {
 	GAIM_CONNECTION_HTML      = 0x0001, /**< Connection sends/receives in 'HTML'. */
-	GAIM_CONNECTION_AUTO_RESP = 0x0002  /**< Send auto responses when away.       */
+	GAIM_CONNECTION_NO_BGCOLOR = 0x0002, /**< Connection does not send/recieve background colors. */
+	GAIM_CONNECTION_AUTO_RESP = 0x0004444  /**< Send auto responses when away.       */
 } GaimConnectionFlags;
 
 typedef enum
--- a/src/gtkconv.c	Mon Aug 25 23:17:01 2003 +0000
+++ b/src/gtkconv.c	Tue Aug 26 01:34:51 2003 +0000
@@ -427,7 +427,7 @@
 			strcpy(buf, buf2);
 		}
 
-		if (gaim_prefs_get_bool("/gaim/gtk/conversations/use_custom_bgcolor")) {
+		if (!(gc->flags & GAIM_CONNECTION_NO_BGCOLOR) && gaim_prefs_get_bool("/gaim/gtk/conversations/use_custom_bgcolor")) {
 			g_snprintf(buf2, limit,
 					   "<BODY BGCOLOR=\"#%02X%02X%02X\">%s</BODY>",
 					   gtkconv->bg_color.red   / 256,
@@ -5429,6 +5429,9 @@
 
 		if (win != NULL)
 			gtk_widget_set_sensitive(gtkwin->menu.insert_link, TRUE);
+
+		gtk_widget_set_sensitive(gtkconv->toolbar.bgcolor,
+				!(gc->flags & GAIM_CONNECTION_NO_BGCOLOR));
 	}
 
 	if (gaim_conversation_get_type(conv) == GAIM_CONV_IM) {
--- a/src/protocols/yahoo/util.c	Mon Aug 25 23:17:01 2003 +0000
+++ b/src/protocols/yahoo/util.c	Tue Aug 26 01:34:51 2003 +0000
@@ -59,7 +59,7 @@
 void yahoo_init_colorht()
 {
 	ht = g_hash_table_new(g_str_hash, g_str_equal);
-
+/* the numbers in comments are what gyach uses, but i think they're incorrect */
 	g_hash_table_insert(ht, "30", "<FONT COLOR=\"#000000\">"); /* black */
 	g_hash_table_insert(ht, "31", "<FONT COLOR=\"#0000FF\">"); /* blue */
 	g_hash_table_insert(ht, "32", "<FONT COLOR=\"#008080\">"); /* cyan */      /* 00b2b2 */
@@ -78,6 +78,14 @@
 	g_hash_table_insert(ht,  "4",  "<U>");
 	g_hash_table_insert(ht, "x4", "</U>");
 
+	/* these just tell us the text they surround is supposed
+	 * to be a link. gaim figures that out on its own so we
+	 * just ignore it.
+	 */
+	g_hash_table_insert(ht, "l", ""); /* link start */
+	g_hash_table_insert(ht, "xl", ""); /* link end */
+
+
 	g_hash_table_insert(ht, "<black>",  "<FONT COLOR=\"#000000\">");
 	g_hash_table_insert(ht, "<blue>",   "<FONT COLOR=\"#0000FF\">");
 	g_hash_table_insert(ht, "<cyan>",   "<FONT COLOR=\"#008284\">");
@@ -110,10 +118,50 @@
 	g_hash_table_destroy(ht);
 }
 
-char *yahoo_codes_to_html(char *x)
+static int point_to_html(int x)
+{
+	if (x < 9)
+		return 1;
+	if (x < 11)
+		return 2;
+	if (x < 13)
+		return 3;
+	if (x < 17)
+		return 4;
+	if (x < 25)
+		return 5;
+	if (x < 35)
+		return 6;
+	return 7;
+}
+static void _font_tags_fix_size(GString *tag, GString *dest)
+{
+	char *x, *end;
+	int size;
+
+	if (((x = strstr(tag->str, "size"))) && ((x = strchr(tag->str, '=')))) {
+		while (*x && !g_ascii_isdigit(*x))
+			x++;
+		if (*x) {
+			size = strtol(x, &end, 10);
+			size = point_to_html(size);
+			g_string_append_len(dest, tag->str, x - tag->str);
+			g_string_append_printf(dest, "%d", size);
+			g_string_append(dest, end);
+		} else {
+			g_string_append(dest, tag->str);
+			return;
+		}
+	} else {
+		g_string_append(dest, tag->str);
+		return;
+	}
+}
+
+char *yahoo_codes_to_html(const char *x)
 {
 	GString *s, *tmp;
-	int i, j, xs, nomoreendtags = 0;
+	int i, j, xs, nomoreendtags = 0; /* s/endtags/closinganglebrackets */
 	char *match, *ret;
 
 
@@ -147,7 +195,7 @@
 
 
 		} else if (!nomoreendtags && (x[i] == '<')) {
-			j = i + 1;
+			j = i;
 
 			while (j++ < xs) {
 				if (x[j] != '>')
@@ -163,15 +211,17 @@
 
 					if ((match = (char *) g_hash_table_lookup(ht, tmp->str)))
 						g_string_append(s, match);
-					else if (!g_ascii_strncasecmp(tmp->str, "<FADE ", 6) ||
-						!g_ascii_strncasecmp(tmp->str, "<ALT ", 5) ||
-						!g_ascii_strncasecmp(tmp->str, "<SND ", 5)) {
+					else if (!strncmp(tmp->str, "<fade ", 6) ||
+						!strncmp(tmp->str, "<alt ", 5) ||
+						!strncmp(tmp->str, "<snd ", 5)) {
 
-						/* remove this if gtkhtml ever supports any of these */
+						/* remove this if gtkimhtml ever supports any of these */
 						i = j;
 						g_string_free(tmp, TRUE);
 						break;
 
+					} else if (!strncmp(tmp->str, "<font ", 6)) {
+						_font_tags_fix_size(tmp, s);
 					} else {
 						g_string_append_c(s, '<');
 						g_string_free(tmp, TRUE);
@@ -194,6 +244,345 @@
 
 	ret = s->str;
 	g_string_free(s, FALSE);
-	gaim_debug(GAIM_DEBUG_MISC, "yahoo", "Returning string: '%s'.\n", ret);
+	gaim_debug(GAIM_DEBUG_MISC, "yahoo", "yahoo_codes_to_html:  Returning string: '%s'.\n", ret);
 	return ret;
 }
+
+/* borrowed from gtkimhtml */
+#define MAX_FONT_SIZE 7
+#define POINT_SIZE(x) (_point_sizes [MIN ((x), MAX_FONT_SIZE) - 1])
+static gint _point_sizes [] = { 8, 10, 12, 14, 20, 30, 40 };
+
+enum fatype { size, color, face, junk };
+typedef struct {
+	enum fatype type;
+	union {
+		int size;
+		char *color;
+		char *face;
+		char *junk;
+	} u;
+} fontattr;
+
+static void fontattr_free(fontattr *f)
+{
+	if (f->type == color)
+		g_free(f->u.color);
+	else if (f->type == face)
+		g_free(f->u.face);
+	g_free(f);
+}
+
+static void yahoo_htc_queue_cleanup(GQueue *q)
+{
+	char *tmp;
+
+	while ((tmp = g_queue_pop_tail(q)))
+		g_free(tmp);
+	g_queue_free(q);
+}
+
+static void _parse_font_tag(const char *src, GString *dest, int *i, int *j,
+				int len, GQueue *colors, GQueue *tags, GQueue *ftattr)
+{
+
+	int m, n, vstart;
+	gboolean quote = 0, done = 0;
+
+	m = *j;
+
+	while (1) {
+		m++;
+
+		if (m >= len) {
+			g_string_append(dest, &src[*i]);
+			*i = len;
+			break;
+		}
+
+		if (src[m] == '=') {
+			n = vstart = m;
+			while (1) {
+				n++;
+
+				if (n >= len) {
+					m = n;
+					break;
+				}
+
+				if (src[n] == '"')
+					if (!quote) {
+						quote = 1;
+						vstart = n;
+						continue;
+					} else {
+						done = 1;
+					}
+
+				if (!quote && ((src[n] == ' ') || (src[n] == '>')))
+					done = 1;
+
+				if (done) {
+					if (!g_ascii_strncasecmp(&src[*j+1], "FACE", m - *j - 1)) {
+						fontattr *f;
+
+						f = g_new(fontattr, 1);
+						f->type = face;
+						f->u.face = g_strndup(&src[vstart+1], n-vstart-1);
+						if (!ftattr)
+							ftattr = g_queue_new();
+						g_queue_push_tail(ftattr, f);
+						m = n;
+						break;
+					} else if (!g_ascii_strncasecmp(&src[*j+1], "SIZE", m - *j - 1)) {
+						fontattr *f;
+
+						f = g_new(fontattr, 1);
+						f->type = size;
+						f->u.size = POINT_SIZE(strtol(&src[vstart+1], NULL, 10));
+						if (!ftattr)
+							ftattr = g_queue_new();
+						g_queue_push_tail(ftattr, f);
+						m = n;
+						break;
+					} else if (!g_ascii_strncasecmp(&src[*j+1], "COLOR", m - *j - 1)) {
+						fontattr *f;
+
+						f = g_new(fontattr, 1);
+						f->type = color;
+						f->u.color = g_strndup(&src[vstart+1], n-vstart-1);
+						if (!ftattr)
+							ftattr = g_queue_new();
+						g_queue_push_head(ftattr, f);
+						m = n;
+						break;
+					} else {
+						fontattr *f;
+
+						f = g_new(fontattr, 1);
+						f->type = junk;
+						f->u.junk = g_strndup(&src[*j+1], n-*j);
+						if (!ftattr)
+							ftattr = g_queue_new();
+						g_queue_push_tail(ftattr, f);
+						m = n;
+						break;
+					}
+
+				}
+			}
+		}
+
+		if (src[m] == ' ')
+			*j = m;
+
+
+
+		if (src[m] == '>') {
+			gboolean needendtag = 0;
+			fontattr *f;
+			GString *tmp = g_string_new(NULL);
+			char *colorstr;
+
+			if (!g_queue_is_empty(ftattr)) {
+				while ((f = g_queue_pop_tail(ftattr))) {
+					switch (f->type) {
+					case size:
+						if (!needendtag) {
+							needendtag = 1;
+							g_string_append(dest, "<font ");
+						}
+
+						g_string_append_printf(dest, "size=\"%d\" ", f->u.size);
+						fontattr_free(f);
+						break;
+					case face:
+						if (!needendtag) {
+							needendtag = 1;
+							g_string_append(dest, "<font ");
+						}
+
+						g_string_append_printf(dest, "face=\"%s\" ", f->u.face);
+						fontattr_free(f);
+						break;
+					case junk:
+						if (!needendtag) {
+							needendtag = 1;
+							g_string_append(dest, "<font ");
+						}
+
+						g_string_append(dest, f->u.junk);
+						fontattr_free(f);
+						break;
+
+					case color:
+						if (needendtag) {
+							g_string_append(tmp, "</font>");
+							dest->str[dest->len-1] = '>';
+							needendtag = 0;
+						}
+
+						colorstr = g_queue_peek_tail(colors);
+						g_string_append(tmp, colorstr ? colorstr : "\033[#000000m");
+						g_string_append_printf(dest, "\033[%sm", f->u.color);
+						g_queue_push_tail(colors, g_strdup_printf("\033[%sm", f->u.color));
+						fontattr_free(f);
+						break;
+					}
+				}
+
+				g_queue_free(ftattr);
+				ftattr = NULL;
+
+				if (needendtag) {
+					dest->str[dest->len-1] = '>';
+					g_queue_push_tail(tags, g_strdup("</font>"));
+					g_string_free(tmp, TRUE);
+				} else {
+					g_queue_push_tail(tags, tmp->str);
+					g_string_free(tmp, FALSE);
+				}
+			}
+
+			*i = *j = m;
+			break;
+		}
+	}
+
+}
+
+char *yahoo_html_to_codes(const char *src)
+{
+	int i, j, m, n, vstart, len;
+	GString *dest;
+	char *ret, *esc;
+	GQueue *colors, *tags;
+	GQueue *ftattr = NULL;
+
+
+	colors = g_queue_new();
+	tags = g_queue_new();
+
+	dest = g_string_sized_new(strlen(src));
+
+	for (i = 0, len = strlen(src); i < len; i++) {
+
+		if (src[i] == '<') {
+			j = i;
+
+			while (1) {
+				j++;
+
+				if (j >= len) { /* no '>' */
+					g_string_append_len(dest, &src[i], len - i);
+					i = len;
+
+					break;
+				}
+
+				if (src[j] == '<') {
+					g_string_append_len(dest, &src[i], j - i);
+					i = j - 1;
+					if (ftattr) {
+						fontattr *f;
+
+						while ((f = g_queue_pop_head(ftattr)))
+							fontattr_free(f);
+						g_queue_free(ftattr);
+						ftattr = NULL;
+					}
+					break;
+				}
+
+				if (src[j] == ' ') {
+					if (!g_ascii_strncasecmp(&src[i+1], "BODY", j - i - 1)) {
+						char *t = strchr(&src[j], '>');
+						if (!t) {
+							g_string_append(dest, &src[i]);
+							i = len;
+							break;
+						} else {
+							i = t - src;
+							break;
+						}
+					} else if (g_ascii_strncasecmp(&src[i+1], "FONT", j - i - 1)) { /* not interested! */
+						while (1) {
+							if (++j >= len) {
+								g_string_append(dest, &src[i]);
+								i = len;
+								break;
+							}
+							if (src[j] == '>') {
+								g_string_append_len(dest, &src[i], j - i + 1);
+								i = j;
+								break;
+							}
+						}
+					} else { /* yay we have a font tag */
+						_parse_font_tag(src, dest, &i, &j, len, colors, tags, ftattr);
+					}
+
+					break;
+				}
+
+				if (src[j] == '>') {
+					int sublen = j - i - 1;
+
+					if (sublen) {
+						if (!g_ascii_strncasecmp(&src[i+1], "B", sublen)) {
+							g_string_append(dest, "\033[1m");
+						} else if (!g_ascii_strncasecmp(&src[i+1], "/B", sublen)) {
+							g_string_append(dest, "\033[x1m");
+						} else if (!g_ascii_strncasecmp(&src[i+1], "I", sublen)) {
+							g_string_append(dest, "\033[2m");
+						} else if (!g_ascii_strncasecmp(&src[i+1], "/I", sublen)) {
+							g_string_append(dest, "\033[x2m");
+						} else if (!g_ascii_strncasecmp(&src[i+1], "U", sublen)) {
+							g_string_append(dest, "\033[4m");
+						} else if (!g_ascii_strncasecmp(&src[i+1], "/U", sublen)) {
+							g_string_append(dest, "\033[x4m");
+						} else if (!g_ascii_strncasecmp(&src[i+1], "/BODY", sublen)) {
+							/* mmm, </body> tags. *BURP* */
+						} else if (!g_ascii_strncasecmp(&src[i+1], "/FONT", sublen) && g_queue_peek_tail(tags)) {
+							char *etag, *cl;
+
+							etag = g_queue_pop_tail(tags);
+							if (etag) {
+								g_string_append(dest, etag);
+								if (!strcmp(etag, "</font>")) {
+									cl = g_queue_pop_tail(colors);
+									if (cl)
+										g_free(cl);
+								}
+								g_free(etag);
+							}
+						} else {
+							g_string_append_len(dest, &src[i], j - i + 1);
+						}
+					} else {
+						g_string_append_len(dest, &src[i], j - i + 1);
+					}
+
+					i = j;
+					break;
+				}
+
+			}
+
+		} else {
+			g_string_append_c(dest, src[i]);
+		}
+	}
+
+	ret = dest->str;
+	g_string_free(dest, FALSE);
+
+	esc = g_strescape(ret, NULL);
+	gaim_debug(GAIM_DEBUG_MISC, "yahoo", "yahoo_html_to_codes:  Returning string: '%s'.\n", esc);
+	g_free(esc);
+
+	yahoo_htc_queue_cleanup(colors);
+	yahoo_htc_queue_cleanup(tags);
+
+	return ret;
+}
--- a/src/protocols/yahoo/yahoo.c	Mon Aug 25 23:17:01 2003 +0000
+++ b/src/protocols/yahoo/yahoo.c	Tue Aug 26 01:34:51 2003 +0000
@@ -204,7 +204,7 @@
 	int pos = 0;
 
 	while (pos + 1 < len) {
-		char key[64], *value = NULL;
+		char key[64], *value = NULL, *esc;
 		int accept;
 		int x;
 
@@ -238,8 +238,10 @@
 			pair->value = g_strdup(value);
 			g_free(value);
 			pkt->hash = g_slist_append(pkt->hash, pair);
+			esc = g_strescape(pair->value, NULL);
 			gaim_debug(GAIM_DEBUG_MISC, "yahoo",
-					   "Key: %d  \tValue: %s\n", pair->key, pair->value);
+					   "Key: %d  \tValue: %s\n", pair->key, esc);
+			g_free(esc);
 		} else {
 			g_free(pair);
 		}
@@ -975,6 +977,8 @@
 	GaimConnection *gc = gaim_account_get_connection(account);
 	struct yahoo_data *yd = gc->proto_data = g_new0(struct yahoo_data, 1);
 
+	gc->flags |= GAIM_CONNECTION_HTML | GAIM_CONNECTION_NO_BGCOLOR;
+
 	gaim_connection_update_progress(gc, _("Connecting"), 1, 2);
 
 	yd->fd = -1;
@@ -1197,7 +1201,7 @@
 {
 	struct yahoo_data *yd = gc->proto_data;
 	struct yahoo_packet *pkt = yahoo_packet_new(YAHOO_SERVICE_MESSAGE, YAHOO_STATUS_OFFLINE, 0);
-	char *msg = g_strdup(what);
+	char *msg = yahoo_html_to_codes(what);
 
 	yahoo_packet_hash(pkt, 1, gaim_connection_get_display_name(gc));
 	yahoo_packet_hash(pkt, 5, who);
@@ -1207,7 +1211,9 @@
 	yahoo_send_packet(yd, pkt);
 
 	yahoo_packet_free(pkt);
-	
+
+	g_free(msg);
+
 	return 1;
 }
 
@@ -1549,7 +1555,7 @@
 static GaimPluginProtocolInfo prpl_info =
 {
 	GAIM_PROTO_YAHOO,
-	OPT_PROTO_MAIL_CHECK | OPT_PROTO_USE_POINTSIZE,
+	OPT_PROTO_MAIL_CHECK,
 	NULL,
 	NULL,
 	yahoo_list_icon,
--- a/src/protocols/yahoo/yahoo.h	Mon Aug 25 23:17:01 2003 +0000
+++ b/src/protocols/yahoo/yahoo.h	Tue Aug 26 01:34:51 2003 +0000
@@ -26,6 +26,7 @@
 
 void yahoo_init_colorht();
 void yahoo_dest_colorht();
-char *yahoo_codes_to_html(char *x);
+char *yahoo_codes_to_html(const char *x);
+char *yahoo_html_to_codes(const char *src);
 
 #endif /* _YAHOO_H_ */