--- libbalsa/mime.c	2000/10/06 15:50:51	1.38
+++ libbalsa/mime.c	2001/08/16 10:52:09
@@ -21,6 +21,7 @@
  */
 
 #include <string.h>
+#include  <ctype.h>
 #include "config.h"
 
 #include "libbalsa.h"
@@ -36,6 +37,102 @@
 				  LibBalsaMessageBody * body,
 				  gchar * reply_prefix_str, gint llen);
 
+static gchar char_ref_char(const gchar *char_ref) 
+{
+    if(char_ref[1]=='#')
+	return (gchar )atoi(char_ref+1);
+    /* These are essential: */
+    else if(g_strcasecmp(char_ref, "lt")==0)
+	return '<';
+    else if(g_strcasecmp(char_ref, "gt")==0)
+	return '>';
+    else if(g_strcasecmp(char_ref, "amp")==0)
+	return '&';
+    else if(g_strcasecmp(char_ref, "nbsp")==0)
+	return ' ';
+    /* Some special characters I'm are using a lot... */
+    else if(strcmp(char_ref, "aelig")==0)
+	return 'æ';
+    else if(strcmp(char_ref, "AElig")==0)
+	return 'Æ';
+    else if(strcmp(char_ref, "oslash")==0)
+	return 'ø';
+    else if(strcmp(char_ref, "Oslash")==0)
+	return 'Ø';
+    else if(strcmp(char_ref, "aring")==0)
+	return 'å';
+    else if(strcmp(char_ref, "Aring")==0)
+	return 'Å';
+    /* Return first character for the rest. Often makes sense because reference
+       is "<character base><info on accent etc.>" */
+    return char_ref[0];
+}
+
+static gchar *extract_tag(gchar **html, gchar end_sep)
+/* Pre : *html is at start of tag, i.e. points to '<' or similar
+   Post: *html points to position after end of tag. */
+{
+    gchar *end=strchr(*html, end_sep), *tag;
+
+    if(!end)			/* No '>', skip rest of string. */
+	end=*html+strlen(*html);
+    
+    tag=g_strndup(*html+1, end-*html-1);
+    *html=end+1;
+    
+    return tag;
+}
+
+static gchar *convert_html(const gchar *html)
+/* Remarks: This is just a quick hack - look for external library routine 
+            that will do this */
+{
+    gchar *txt=calloc(strlen(html)+1, sizeof(gchar));
+
+    if(txt) {
+	gchar *c_html=(gchar *)html, *c_txt=txt;
+
+	while(*c_html) {
+	    if(*c_html=='<') {
+		gchar *tag=extract_tag(&c_html, '>');
+
+		if(g_strcasecmp(tag, "br")==0) {
+		    *c_txt++='\n';
+		} else if(g_strcasecmp(tag, "p")==0 ||
+			(tolower(tag[0])=='h' && isdigit(tag[1]))) {
+		    *c_txt++='\n';
+		    *c_txt++='\n';
+		} else {
+		    gchar *ref=strstr(tag, "href=");
+		    
+		    if(!ref) { /* *** Want 'strcasestr' */
+			ref=strstr(tag, "HREF=");
+		    }
+		    
+		    if(ref) {
+			*c_txt++='[';
+			ref+=5;
+			
+			while(*ref)
+			    *c_txt++=*ref++;
+			*c_txt++=']';
+		    }
+		}
+		g_free(tag);
+	    } else if(*c_html=='&') {
+		gchar *char_ref=extract_tag(&c_html, ';');
+
+		*c_txt++=char_ref_char(char_ref);
+		g_free(char_ref);
+	    } else {
+		*c_txt++=*c_html++;
+	    }
+	}
+    }
+    return txt;
+}
+
+
 static GString *
 process_mime_part(LibBalsaMessage * message, LibBalsaMessageBody * body,
 		  gchar * reply_prefix_str, gint llen)
@@ -44,7 +141,10 @@
     size_t alloced;
     gchar *res = NULL;
     GString *reply = NULL;
+    gchar *content_type = libbalsa_message_body_get_content_type(body);
+    gboolean ishtml=(g_strcasecmp(content_type, "text/html") == 0);
 
+    
     switch (libbalsa_message_body_type(body)) {
     case LIBBALSA_MESSAGE_BODY_TYPE_OTHER:
     case LIBBALSA_MESSAGE_BODY_TYPE_AUDIO:
@@ -68,6 +168,17 @@
 	fclose(part);
 	if (!res)
 	    break;
+
+	if(ishtml) {
+	    gchar *res_ascii=convert_html(res);
+	    
+	    if(res_ascii) {
+		g_free(res);
+		res=res_ascii;
+	    }
+	}
+	    
+
 	if (llen > 0) {
 	    if (reply_prefix_str)
 		llen -= strlen(reply_prefix_str);
@@ -93,6 +204,8 @@
 	g_free(res);
 	break;
     }
+    g_free(content_type);	
+
     return reply;
 }
 
