Search in sources :

Example 1 with EscapeHTMLWriter

use of i2p.susi.util.EscapeHTMLWriter in project i2p.i2p by i2p.

the class WebMail method showPart.

/**
 * recursively render all mail body parts
 *
 * 1. if type is multipart/alternative, look for text/plain section and ignore others
 * 2. if type is multipart/*, recursively call all these parts
 * 3. if type is text/plain (or mail is not mime), print out
 * 4. in all other cases print out message, that part is not displayed
 *
 * @param out
 * @param mailPart
 * @param level is increased by recursively calling sub parts
 */
private static void showPart(PrintWriter out, MailPart mailPart, int level, boolean html) {
    String br = html ? "<br>\r\n" : "\r\n";
    if (html) {
        out.println("<!-- ");
        out.println("Debug: Showing Mail Part at level " + level + " with ID " + mailPart.getID());
        out.println("Debug: Mail Part headers follow");
        for (int i = 0; i < mailPart.headerLines.length; i++) {
            // fix Content-Type: multipart/alternative; boundary="----------8CDE39ECAF2633"
            out.println(mailPart.headerLines[i].replace("--", "&#45;&#45;"));
        }
        out.println("-->");
    }
    if (mailPart.multipart) {
        if (mailPart.type.equals("multipart/alternative")) {
            MailPart chosen = null;
            for (MailPart subPart : mailPart.parts) {
                if (subPart.type != null && subPart.type.equals("text/plain"))
                    chosen = subPart;
            }
            if (chosen != null) {
                showPart(out, chosen, level + 1, html);
                if (html) {
                    // DEBUG
                    for (MailPart subPart : mailPart.parts) {
                        if (chosen.equals(subPart))
                            continue;
                        out.println("<!-- ");
                        out.println("Debug: Not showing alternative Mail Part at level " + (level + 1) + " with ID " + subPart.getID());
                        out.println("Debug: Mail Part headers follow");
                        for (int i = 0; i < subPart.headerLines.length; i++) {
                            out.println(subPart.headerLines[i].replace("--", "&#45;&#45;"));
                        }
                        out.println("-->");
                    }
                }
                return;
            }
        }
        for (MailPart part : mailPart.parts) {
            showPart(out, part, level + 1, html);
        }
    } else if (mailPart.message) {
        for (MailPart part : mailPart.parts) {
            showPart(out, part, level + 1, html);
        }
    } else {
        boolean showBody = false;
        boolean prepareAttachment = false;
        String reason = "";
        String ident = quoteHTML((mailPart.description != null ? mailPart.description + ", " : "") + (mailPart.filename != null ? mailPart.filename + ", " : "") + (mailPart.name != null ? mailPart.name + ", " : "") + (mailPart.type != null ? '(' + mailPart.type + ')' : _t("unknown")));
        if (level == 0 && mailPart.version == null) {
            /*
				 * not a MIME mail, so simply print it literally
				 */
            showBody = true;
        }
        if (!showBody && mailPart.type != null) {
            if (mailPart.type.equals("text/plain")) {
                showBody = true;
            } else
                prepareAttachment = true;
        }
        if (reason != null && reason.length() > 0) {
            if (html)
                out.println("<p class=\"info\">");
            out.println(reason);
            if (html)
                out.println("</p>");
            reason = "";
        }
        if (html)
            out.println("<tr class=\"mailbody\"><td colspan=\"2\" align=\"center\">");
        if (showBody) {
            if (html)
                out.println("<p class=\"mailbody\"><br>");
            String charset = mailPart.charset;
            if (charset == null) {
                charset = "ISO-8859-1";
            // don't show this in text mode which is used to include the mail in the reply or forward
            // Too common, don't show this at all.
            // if (html)
            // reason = _t("Warning: no charset found, fallback to US-ASCII.") + br;
            }
            try {
                Writer escaper;
                if (html)
                    escaper = new EscapeHTMLWriter(out);
                else
                    escaper = out;
                Buffer ob = new OutputStreamBuffer(new DecodingOutputStream(escaper, charset));
                mailPart.decode(0, ob);
                // todo Finally
                ob.writeComplete(true);
            } catch (UnsupportedEncodingException uee) {
                showBody = false;
                reason = _t("Charset \\''{0}\\'' not supported.", quoteHTML(mailPart.charset)) + br;
            } catch (IOException e1) {
                showBody = false;
                reason += _t("Part ({0}) not shown, because of {1}", ident, e1.toString()) + br;
            }
            if (html)
                out.println("<br></p>");
        }
        if (reason != null && reason.length() > 0) {
            // FIXME css has -32 margin
            if (html)
                out.println("<p class=\"info\">");
            out.println(reason);
            if (html)
                out.println("</p>");
        }
        if (prepareAttachment) {
            if (html) {
                out.println("<hr><div class=\"attached\">");
                String type = mailPart.type;
                if (type != null && type.startsWith("image/")) {
                    // we at least show images safely...
                    String name = mailPart.filename;
                    if (name == null) {
                        name = mailPart.name;
                        if (name == null)
                            name = mailPart.description;
                    }
                    name = quoteHTML(name);
                    out.println("<img src=\"" + myself + '?' + RAW_ATTACHMENT + '=' + mailPart.getID() + "&amp;" + B64UIDL + '=' + Base64.encode(mailPart.uidl) + "\" alt=\"" + name + "\">");
                } else if (type != null && (// type list from snark
                type.startsWith("audio/") || type.equals("application/ogg") || type.startsWith("video/") || (type.startsWith("text/") && !type.equals("text/html")) || type.equals("application/zip") || type.equals("application/x-gtar") || type.equals("application/x-zip-compressed") || type.equals("application/compress") || type.equals("application/gzip") || type.equals("application/x-7z-compressed") || type.equals("application/x-rar-compressed") || type.equals("application/x-tar") || type.equals("application/x-bzip2") || type.equals("application/pdf") || type.equals("application/x-bittorrent") || type.equals("application/pgp-encrypted") || type.equals("application/pgp-signature"))) {
                    out.println("<a href=\"" + myself + '?' + RAW_ATTACHMENT + '=' + mailPart.getID() + "&amp;" + B64UIDL + '=' + Base64.encode(mailPart.uidl) + "\">" + _t("Download attachment {0}", ident) + "</a>");
                } else {
                    out.println("<a target=\"_blank\" href=\"" + myself + '?' + DOWNLOAD + '=' + mailPart.getID() + "&amp;" + B64UIDL + '=' + Base64.encode(mailPart.uidl) + "\">" + _t("Download attachment {0}", ident) + "</a>" + " (" + _t("File is packed into a zipfile for security reasons.") + ')');
                }
                out.println("</div>");
            } else {
                out.println(_t("Attachment ({0}).", ident));
            }
        }
        if (html)
            out.println("</td></tr>");
    }
    if (html) {
        out.println("<!-- ");
        out.println("Debug: End of Mail Part at level " + level + " with ID " + mailPart.getID());
        out.println("-->");
    }
}
Also used : OutputStreamBuffer(i2p.susi.util.OutputStreamBuffer) Buffer(i2p.susi.util.Buffer) OutputStreamBuffer(i2p.susi.util.OutputStreamBuffer) EscapeHTMLWriter(i2p.susi.util.EscapeHTMLWriter) DecodingOutputStream(i2p.susi.util.DecodingOutputStream) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter) Writer(java.io.Writer) StringWriter(java.io.StringWriter) EscapeHTMLWriter(i2p.susi.util.EscapeHTMLWriter)

Aggregations

Buffer (i2p.susi.util.Buffer)1 DecodingOutputStream (i2p.susi.util.DecodingOutputStream)1 EscapeHTMLWriter (i2p.susi.util.EscapeHTMLWriter)1 OutputStreamBuffer (i2p.susi.util.OutputStreamBuffer)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Writer (java.io.Writer)1