Re: PATCH: GMime support for message reading



Hello,

A little follow-up, the following patch adds generating a gmime body in 
libbalsa_create_msg. The generation of a mutt body isn't removed because 
it's still used in adding the message to mailboxes(fcc, draft, sent). And 
because i wasn't planning on rewriting/changing the mutt_write_fcc 
function, so this is as far as the mime changes will go by me.

Bart
--
GPG key = 1024D/4B086D06
Fingerprint = CD4D 5601 287D F075 6F96  6157 99F9 E56A 4B08 6D06

diff -ru -X .cvsignore -X src/.cvsignore -x CVS -x *.o -x *~ ./libbalsa/body.c ../balsa-mime/libbalsa/body.c
--- ./libbalsa/body.c	Thu Oct 18 22:18:52 2001
+++ ../balsa-mime/libbalsa/body.c	Thu Oct 18 22:24:01 2001
@@ -71,6 +71,9 @@
     libbalsa_message_body_free(body->next);
     libbalsa_message_body_free(body->parts);
 
+    if (body->mime_part)
+	g_mime_part_destroy(body->mime_part);	
+    
     /* FIXME: Need to free MuttBody?? */
 
     g_free(body);
@@ -209,5 +212,5 @@
 gboolean
 libbalsa_message_body_is_multipart(LibBalsaMessageBody * body)
 {
-    return body->mime_part->children;
+    return !!body->mime_part->children;
 }
diff -ru -X .cvsignore -X src/.cvsignore -x CVS -x *.o -x *~ ./libbalsa/send.c ../balsa-mime/libbalsa/send.c
--- ./libbalsa/send.c	Thu Oct 18 22:20:20 2001
+++ ../balsa-mime/libbalsa/send.c	Thu Oct 18 22:43:57 2001
@@ -216,7 +216,7 @@
 encode_descriptions (BODY *b)
 {
     BODY *t;
-    
+
     for (t = b; t; t = t->next)
 	{
 	    if (t->description)
@@ -230,6 +230,66 @@
 	}
 }
 
+static GMimePart *
+add_mime_body_plain(LibBalsaMessageBody *body, gint encoding_style, 
+		    gboolean flow)
+{
+    GMimePart *mime_part;
+    const gchar * charset;
+    GMimePartEncodingType mime_encoding=GMIME_PART_ENCODING_DEFAULT;
+    GMimeContentType *mime_type;
+
+    g_return_val_if_fail(body, NULL);
+    
+    charset=body->charset;
+    g_return_val_if_fail(charset, NULL);
+
+    if (body->mime_type) {
+        /* change the type and subtype within the mutt body */
+        gchar *type, *subtype;
+
+        type = g_strdup (body->mime_type);
+        if ((subtype = strchr (type, '/'))) {
+            *subtype++ = 0;
+            mime_part = g_mime_part_new_with_type(type, subtype);
+        } else {
+            mime_part = g_mime_part_new_with_type("text", "plain");
+        }
+        g_free (type);
+    } else {
+        mime_part = g_mime_part_new_with_type("text", "plain");
+    }
+
+    switch (encoding_style) {
+        case ENCUUENCODED:
+            /* FIXME */
+        case ENCOTHER:
+        case ENCBINARY: /* or is binary mode the same as 8bit ? */
+            mime_encoding=GMIME_PART_ENCODING_DEFAULT;
+            break;
+        case ENC7BIT:
+            mime_encoding=GMIME_PART_ENCODING_7BIT;
+            break;
+        case ENC8BIT:
+            mime_encoding=GMIME_PART_ENCODING_8BIT;
+            break;
+        case ENCQUOTEDPRINTABLE:
+            mime_encoding=GMIME_PART_ENCODING_QUOTEDPRINTABLE;
+            break;
+        case ENCBASE64:
+            mime_encoding=GMIME_PART_ENCODING_BASE64;
+            break;
+    }
+    g_mime_part_set_encoding(mime_part, mime_encoding);
+    mime_type=(GMimeContentType*)g_mime_part_get_content_type(mime_part);
+    g_mime_content_type_add_parameter(mime_type, "charset", charset);
+    if (flow)
+        g_mime_content_type_add_parameter(mime_type, "format", "flowed");
+
+    g_mime_part_set_content(mime_part, body->buffer, strlen(body->buffer));
+    return mime_part;
+}
+
 static BODY *
 add_mutt_body_plain(LibBalsaMessageBody *body, gint encoding_style, 
 		    gboolean flow)
@@ -1262,7 +1326,7 @@
 		g_strfreev(mime_type);
 	    }
 	} else if (body->buffer) {
-            newbdy = add_mutt_body_plain(body, encoding, flow);
+	    newbdy = add_mutt_body_plain(body, encoding, flow);
 	}
 
 	if (newbdy) {
@@ -1362,11 +1427,12 @@
     LibBalsaMessageBody *body;
     GList *list;
     gchar **mime_type;
-
+    GMimePart *mime_root=NULL;
+    GMimePart *mime_part;
 
     message2HEADER(message, msg);
 
-    /* If the message has references set, add them to he envelope */
+    /* If the message has references set, add them to the envelope */
     if (message->references != NULL) {
 	list = message->references;
 	msg->env->references = mutt_new_list();
@@ -1394,6 +1460,8 @@
 	libbalsa_message_body_ref(message);
 
     body = message->body_list;
+    if (body && body->next)
+	mime_root=g_mime_part_new_with_type("multipart", "mixed");
 
     last = msg->content;
     while (last && last->next)
@@ -1401,6 +1469,7 @@
 
     while (body) {
 	newbdy = NULL;
+	mime_part=NULL;
 
 	if (body->filename) {
 	    if (body->attach_as_extbody) {
@@ -1428,13 +1497,20 @@
 			mime_type = g_strsplit(body->mime_type, "/", 2);
 		    /* use BASE64 encoding for non-text mime types 
 		       use 8BIT for message */
+		    mime_part=g_mime_part_new_with_type(mime_type[0], mime_type[1]);
+		    g_mime_part_set_filename(mime_part, body->filename);
 		    libbalsa_lock_mutt();
 		    if(!strcasecmp(mime_type[0],"message") && 
 		       !strcasecmp(mime_type[1],"rfc822")) {
+			g_mime_part_set_encoding(mime_part, GMIME_PART_ENCODING_8BIT);
+			g_mime_part_set_content_disposition(mime_part, "inline");
 			newbdy->encoding = ENC8BIT;
 			newbdy->disposition = DISPINLINE;
 		    } else if(strcasecmp(mime_type[0],"text") != 0)
+		    {
+			g_mime_part_set_encoding(mime_part, GMIME_PART_ENCODING_BASE64);
 			newbdy->encoding = ENCBASE64;
+		    }
 		    newbdy->type = mutt_check_mime_type(mime_type[0]);
 		    g_free(newbdy->subtype);
 		    newbdy->subtype = g_strdup(mime_type[1]);
@@ -1443,9 +1519,15 @@
 		}
 	    }
 	} else if (body->buffer) {
+	    mime_part = add_mime_body_plain(body, encoding, flow);
 	    newbdy = add_mutt_body_plain(body, encoding, flow);
 	}
 
+	if (mime_root) {
+	    g_mime_part_add_subpart(mime_root, mime_part);
+	} else {
+	    mime_root = mime_part;
+	}
 	if (newbdy) {
 	    if (last)
 		last->next = newbdy;
@@ -1458,6 +1540,10 @@
 	body = body->next;
     }
 
+    if (mime_root) {
+	message->body_list->mime_part=mime_root;
+    }
+	
     if (msg->content) {
 	if (msg->content->next)
 	    msg->content = mutt_make_multipart(msg->content);
@@ -1470,26 +1556,26 @@
 /* We create the message in MIME format here, we use the same format 
  * for local delivery that for SMTP */
     if (queu == 0) {
+	gchar *msg_text;
 	mutt_mktemp(tmpfile);
 	if ((tempfp = safe_fopen(tmpfile, "w")) == NULL)
 	    return (-1);
 
-	mutt_write_rfc822_header(tempfp, msg->env, msg->content, -1);
-	fputc('\n', tempfp);	/* tie off the header. */
-
-	if ((mutt_write_mime_body(msg->content, tempfp) == -1)) {
+	mutt_write_rfc822_header(tempfp, msg->env, NULL, -1);
+	msg_text=g_mime_part_to_string(mime_root, 1);
+	if (fwrite(msg_text, strlen(msg_text), 1, tempfp)!=1) {
+	    g_free(msg_text);
 	    fclose(tempfp);
 	    unlink(tmpfile);
 	    return (-1);
 	}
-	fputc('\n', tempfp);	/* tie off the body. */
+	g_free(msg_text);
 
 	if (fclose(tempfp) != 0) {
 	    mutt_perror(tmpfile);
 	    unlink(tmpfile);
 	    return (-1);
 	}
-
     } else {
 	/* the message is in the queue */
 
diff -ru -X .cvsignore -X src/.cvsignore -x CVS -x *.o -x *~ ./libmutt/sendlib.c ../balsa-mime/libmutt/sendlib.c
--- ./libmutt/sendlib.c	Thu Oct 18 22:17:22 2001
+++ ../balsa-mime/libmutt/sendlib.c	Wed Oct 17 23:42:41 2001
@@ -1361,9 +1361,13 @@
       fputc('\n', fp);
     }
 
+    /* BALSA: only do mime headers if attach != NULL, we do our own */
     /* Add the MIME headers */
+    if (attach) {
     fputs ("Mime-Version: 1.0\n", fp);
     mutt_write_mime_header (attach, fp);
+    }
+    /* BALSA: end mime headers */
   }
 #ifndef LIBMUTT
 #ifndef NO_XMAILER
diff -ru -X .cvsignore -X src/.cvsignore -x CVS -x *.o -x *~ ./src/print.c ../balsa-mime/src/print.c
--- ./src/print.c	Thu Oct 18 22:17:23 2001
+++ ../balsa-mime/src/print.c	Thu Oct 18 21:45:07 2001
@@ -673,7 +673,7 @@
     pdata = g_malloc(sizeof(DefaultInfo));
     pdata->id_tag = BALSA_PRINT_TYPE_DEFAULT;
 
-    if (body->mutt_body)
+    if (body->mime_part)
 	conttype = libbalsa_message_body_get_content_type(body);
     else
 	conttype = 
@@ -875,7 +875,7 @@
 	if (body->buffer)
 	    conttype = g_strdup("text");
 	else
-	    if (!body->mutt_body)
+	    if (!body->mime_part)
 		conttype = g_strdup("default");
 	    else
 		conttype = libbalsa_message_body_get_content_type(body);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]