[Xfce4-commits] <postler:master> Implement initial multipart support

Christian Dywan noreply at xfce.org
Tue Jun 1 17:58:01 CEST 2010


Updating branch refs/heads/master
         to b63214b895b269fe3208c3553d21cd2f7cab835e (commit)
       from a5ad154854aa3f419b1ee9791739a70d048e79aa (commit)

commit b63214b895b269fe3208c3553d21cd2f7cab835e
Author: Christian Dywan <christian at twotoasts.de>
Date:   Mon May 31 21:49:39 2010 +0200

    Implement initial multipart support

 postler/postler-content.vala |   90 +++++++++++++++++++++++++++++++++++-------
 1 files changed, 75 insertions(+), 15 deletions(-)

diff --git a/postler/postler-content.vala b/postler/postler-content.vala
index 94b040f..ab83c14 100644
--- a/postler/postler-content.vala
+++ b/postler/postler-content.vala
@@ -187,10 +187,9 @@ public class Postler.Content : WebKit.WebView {
             string mime_type = "text/plain";
             string charset = null;
             string[] parts;
-            GLib.StringBuilder body;
 
             if (view_source) {
-                body = new GLib.StringBuilder ();
+                GLib.StringBuilder body = new GLib.StringBuilder ();
                 while ((line = stream.read_line (null, null)) != null) {
                     parts = line.split (":", 2);
                     if (parts != null && parts[0] != null) {
@@ -299,6 +298,7 @@ public class Postler.Content : WebKit.WebView {
             if (reply != "")
                 reply = linkify_address (reply, arguments);
 
+            string boundary = null;
             if (content_type != null) {
                 parts = content_type.split_set ("; ");
                 string filename = null;
@@ -307,6 +307,8 @@ public class Postler.Content : WebKit.WebView {
                         charset = part.substring (8, part.length - 8);
                     else if (part.has_prefix ("name="))
                         filename = part.substring (5, part.length - 5);
+                    else if (part.has_prefix ("boundary="))
+                        boundary = part.substring (9, part.length - 9);
                     else if (part != "" && !part.contains ("="))
                         mime_type = part.down ();
                 }
@@ -327,17 +329,62 @@ public class Postler.Content : WebKit.WebView {
                 plain_text = true;
             }
 
+            uint multipart = mime_type.has_prefix ("multipart/") ? 1 : 0;
+            if (multipart > 0 && boundary != null)
+                boundary = boundary.replace ("\"", " ").strip ();
+            else
+                multipart = 0;
+            if (!(mime_type.has_prefix ("text/") || multipart > 0)) {
+                load_string ("<h1>%s</h1><p>%s".printf (
+                             _("Error"),
+                             _("This message of type \"%s\" can't be displayed.").
+                             printf (mime_type)),
+                             "text/html", "UTF-8", "about:blank");
+                last_location = location;
+                return false;
+            }
+
             /* Message body starts here */
-            body = new GLib.StringBuilder ();
+            GLib.StringBuilder[] body = {};
+            string[] mime_types = {};
+            int slice = -1;
+            if (multipart == 0) {
+                body += new GLib.StringBuilder ();
+                mime_types += mime_type;
+                slice++;
+            }
             while ((line = stream.read_line (null, null)) != null) {
-                if (!mime_type.has_prefix ("text/")) {
-                    body.append ("<h1>%s</h1><p>%s".printf (
-                        _("Error"),
-                        _("This message of type \"%s\" can't be displayed.").
-                            printf (mime_type)
-                        ));
-                    mime_type = "text/html";
-                    break;
+                if (multipart > 0) {
+                    if (line.has_prefix ("--")) {
+                        if (line == "--" + boundary) {
+                            body += new GLib.StringBuilder ();
+                            mime_types += "text/plain";
+                            plain_text = true;
+                            slice++;
+                            multipart = 2;
+                            continue;
+                        }
+                    }
+                    else if (multipart == 2) {
+                        parts = line.split (":", 2);
+                        if (parts[0] != null) {
+                            string field = parts[0].down ();
+                            if (field == "content-type") {
+                                string ctype = parts[1].strip ();
+                                parts = ctype.split_set ("; ");
+                                mime_types[slice] = parts[0].strip ().down ();
+                                if (mime_types[slice] != "text/plain")
+                                    plain_text = false;
+                                continue;
+                            }
+                            else if (field == "content-transfer-encoding") {
+                                /* TODO: Handle encoding */
+                                continue;
+                            }
+                        }
+                    }
+                    else if (multipart == 1)
+                        continue;
                 }
 
                 if (content_encoding == "quoted-printable")
@@ -356,17 +403,30 @@ public class Postler.Content : WebKit.WebView {
                 /* Render signature gray, beginning with "--=20" */
                 /* Looks like quoting */
                 if (line.has_prefix (">"))
-                    body.append ("<span style=\"color: GrayText\">");
+                    body[slice].append ("<span style=\"color: GrayText\">");
                 string appendage;
                 if (plain_text && content_encoding != "base64")
                     appendage = line + "\n";
                 else
                     appendage = line;
-                body.append (appendage);
+                body[slice].append (appendage);
                 if (line.has_prefix (">"))
-                    body.append ("</span>");
+                    body[slice].append ("</span>");
+                }
+
+            string body_chunk;
+            if (multipart == 2) {
+                body_chunk = body[0].str;
+                mime_type = mime_types[0];
+                if (mime_type == "text/plain") {
+                    mime_type = "text/html";
+                    plain_text = true;
                 }
-            string body_chunk = body.str;
+            }
+            else if (multipart > 0)
+                body_chunk = content_type;
+            else
+                body_chunk = body[0].str;
 
             /* Linkify */
             if (plain_text) {



More information about the Xfce4-commits mailing list