2001-09-29 00:14:13 +04:00
|
|
|
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
2004-04-17 22:33:16 +04:00
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
1999-03-19 23:00:36 +03:00
|
|
|
*
|
2004-04-17 22:33:16 +04:00
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
1999-03-19 23:00:36 +03:00
|
|
|
*
|
2001-09-29 00:14:13 +04:00
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
1999-03-19 23:00:36 +03:00
|
|
|
*
|
1999-11-06 06:43:54 +03:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
2004-04-17 22:33:16 +04:00
|
|
|
* The Initial Developer of the Original Code is
|
2001-09-29 00:14:13 +04:00
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
1999-11-06 06:43:54 +03:00
|
|
|
*
|
2001-09-29 00:14:13 +04:00
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
2004-04-17 22:33:16 +04:00
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
2001-09-29 00:14:13 +04:00
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
2004-04-17 22:33:16 +04:00
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
2001-09-29 00:14:13 +04:00
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
2004-04-17 22:33:16 +04:00
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
2001-09-29 00:14:13 +04:00
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
1999-03-19 23:00:36 +03:00
|
|
|
#include "mimethtm.h"
|
|
|
|
#include "prmem.h"
|
|
|
|
#include "plstr.h"
|
1999-04-13 07:14:46 +04:00
|
|
|
#include "prlog.h"
|
2001-07-16 06:40:48 +04:00
|
|
|
#include "prprf.h"
|
1999-04-13 07:14:46 +04:00
|
|
|
#include "msgCore.h"
|
1999-05-29 00:00:32 +04:00
|
|
|
#include "nsMimeStringResources.h"
|
2000-04-19 01:07:19 +04:00
|
|
|
#include "mimemoz2.h"
|
1999-03-19 23:00:36 +03:00
|
|
|
|
|
|
|
#define MIME_SUPERCLASS mimeInlineTextClass
|
|
|
|
MimeDefClass(MimeInlineTextHTML, MimeInlineTextHTMLClass,
|
|
|
|
mimeInlineTextHTMLClass, &MIME_SUPERCLASS);
|
|
|
|
|
|
|
|
static int MimeInlineTextHTML_parse_line (char *, PRInt32, MimeObject *);
|
|
|
|
static int MimeInlineTextHTML_parse_eof (MimeObject *, PRBool);
|
|
|
|
static int MimeInlineTextHTML_parse_begin (MimeObject *obj);
|
|
|
|
|
|
|
|
static int
|
|
|
|
MimeInlineTextHTMLClassInitialize(MimeInlineTextHTMLClass *clazz)
|
|
|
|
{
|
|
|
|
MimeObjectClass *oclass = (MimeObjectClass *) clazz;
|
|
|
|
PR_ASSERT(!oclass->class_initialized);
|
|
|
|
oclass->parse_begin = MimeInlineTextHTML_parse_begin;
|
|
|
|
oclass->parse_line = MimeInlineTextHTML_parse_line;
|
|
|
|
oclass->parse_eof = MimeInlineTextHTML_parse_eof;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
MimeInlineTextHTML_parse_begin (MimeObject *obj)
|
|
|
|
{
|
|
|
|
int status = ((MimeObjectClass*)&mimeLeafClass)->parse_begin(obj);
|
|
|
|
if (status < 0) return status;
|
2000-01-25 06:18:36 +03:00
|
|
|
|
1999-03-19 23:00:36 +03:00
|
|
|
if (!obj->output_p) return 0;
|
2000-05-12 01:01:19 +04:00
|
|
|
|
|
|
|
// Set a default font (otherwise unicode font will be used since the data is UTF-8).
|
2000-04-19 01:07:19 +04:00
|
|
|
if (nsMimeOutput::nsMimeMessageBodyDisplay == obj->options->format_out ||
|
|
|
|
nsMimeOutput::nsMimeMessagePrintOutput == obj->options->format_out)
|
2000-05-12 01:01:19 +04:00
|
|
|
{
|
|
|
|
char buf[256]; // local buffer for html tag
|
|
|
|
PRInt32 fontSize; // default font size
|
2000-11-28 00:44:08 +03:00
|
|
|
PRInt32 fontSizePercentage; // size percentage
|
2002-02-15 03:46:08 +03:00
|
|
|
nsCAutoString fontLang; // langgroup of the font.
|
|
|
|
if (NS_SUCCEEDED(GetMailNewsFont(obj, PR_FALSE, &fontSize, &fontSizePercentage,fontLang)))
|
2000-05-12 01:01:19 +04:00
|
|
|
{
|
2002-02-15 03:46:08 +03:00
|
|
|
PR_snprintf(buf, 256, "<div class=\"moz-text-html\" lang=\"%s\">",
|
2002-02-15 04:13:05 +03:00
|
|
|
fontLang.get());
|
2002-02-19 11:42:48 +03:00
|
|
|
status = MimeObject_write(obj, buf, strlen(buf), PR_FALSE);
|
2000-05-12 01:01:19 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2000-09-13 06:11:00 +04:00
|
|
|
status = MimeObject_write(obj, "<div class=\"moz-text-html\">", 27, PR_FALSE);
|
2000-05-12 01:01:19 +04:00
|
|
|
}
|
|
|
|
if(status<0) return status;
|
|
|
|
}
|
2000-04-19 01:07:19 +04:00
|
|
|
|
2000-01-14 04:01:18 +03:00
|
|
|
MimeInlineTextHTML *textHTML = (MimeInlineTextHTML *) obj;
|
2000-01-25 06:18:36 +03:00
|
|
|
|
2000-01-14 04:01:18 +03:00
|
|
|
textHTML->charset = nsnull;
|
2000-01-25 06:18:36 +03:00
|
|
|
|
1999-03-19 23:00:36 +03:00
|
|
|
/* If this HTML part has a Content-Base header, and if we're displaying
|
|
|
|
to the screen (that is, not writing this part "raw") then translate
|
2000-01-25 06:18:36 +03:00
|
|
|
that Content-Base header into a <BASE> tag in the HTML.
|
|
|
|
*/
|
1999-03-19 23:00:36 +03:00
|
|
|
if (obj->options &&
|
2000-01-25 06:18:36 +03:00
|
|
|
obj->options->write_html_p &&
|
|
|
|
obj->options->output_fn)
|
|
|
|
{
|
|
|
|
char *base_hdr = MimeHeaders_get (obj->headers, HEADER_CONTENT_BASE,
|
|
|
|
PR_FALSE, PR_FALSE);
|
|
|
|
|
1999-03-19 23:00:36 +03:00
|
|
|
/* rhp - for MHTML Spec changes!!! */
|
|
|
|
if (!base_hdr)
|
|
|
|
{
|
|
|
|
base_hdr = MimeHeaders_get (obj->headers, HEADER_CONTENT_LOCATION, PR_FALSE, PR_FALSE);
|
|
|
|
}
|
|
|
|
/* rhp - for MHTML Spec changes!!! */
|
2000-01-25 06:18:36 +03:00
|
|
|
|
|
|
|
if (base_hdr)
|
|
|
|
{
|
2002-02-19 11:42:48 +03:00
|
|
|
char *buf = (char *) PR_MALLOC(strlen(base_hdr) + 20);
|
2000-01-25 06:18:36 +03:00
|
|
|
const char *in;
|
|
|
|
char *out;
|
|
|
|
if (!buf)
|
|
|
|
return MIME_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
/* The value of the Content-Base header is a number of "words".
|
|
|
|
Whitespace in this header is not significant -- it is assumed
|
|
|
|
that any real whitespace in the URL has already been encoded,
|
|
|
|
and whitespace has been inserted to allow the lines in the
|
|
|
|
mail header to be wrapped reasonably. Creators are supposed
|
|
|
|
to insert whitespace every 40 characters or less.
|
|
|
|
*/
|
|
|
|
PL_strcpy(buf, "<BASE HREF=\"");
|
2002-02-19 11:42:48 +03:00
|
|
|
out = buf + strlen(buf);
|
2000-01-25 06:18:36 +03:00
|
|
|
|
|
|
|
for (in = base_hdr; *in; in++)
|
|
|
|
/* ignore whitespace and quotes */
|
2000-03-12 12:14:14 +03:00
|
|
|
if (!nsCRT::IsAsciiSpace(*in) && *in != '"')
|
2000-01-25 06:18:36 +03:00
|
|
|
*out++ = *in;
|
|
|
|
|
|
|
|
/* Close the tag and argument. */
|
|
|
|
*out++ = '"';
|
|
|
|
*out++ = '>';
|
|
|
|
*out++ = 0;
|
|
|
|
|
|
|
|
PR_Free(base_hdr);
|
|
|
|
|
2002-02-19 11:42:48 +03:00
|
|
|
status = MimeObject_write(obj, buf, strlen(buf), PR_FALSE);
|
2000-01-25 06:18:36 +03:00
|
|
|
PR_Free(buf);
|
|
|
|
if (status < 0) return status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-11-19 18:19:53 +03:00
|
|
|
// rhp: For a change, we will write out a separator after formatted text
|
|
|
|
// bodies.
|
|
|
|
status = MimeObject_write_separator(obj);
|
2000-01-25 06:18:36 +03:00
|
|
|
if (status < 0) return status;
|
|
|
|
|
1999-03-19 23:00:36 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
MimeInlineTextHTML_parse_line (char *line, PRInt32 length, MimeObject *obj)
|
|
|
|
{
|
2000-01-14 04:01:18 +03:00
|
|
|
MimeInlineTextHTML *textHTML = (MimeInlineTextHTML *) obj;
|
|
|
|
|
|
|
|
if (!obj->output_p)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!obj->options || !obj->options->output_fn)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!textHTML->charset)
|
|
|
|
{
|
2002-01-04 03:49:44 +03:00
|
|
|
char * cp;
|
2000-01-14 04:01:18 +03:00
|
|
|
// First, try to detect a charset via a META tag!
|
2002-01-04 03:49:44 +03:00
|
|
|
if ((cp = PL_strncasestr(line, "META", length)) &&
|
|
|
|
(cp = PL_strncasestr(cp, "HTTP-EQUIV=", length - (int)(cp - line))) &&
|
|
|
|
(cp = PL_strncasestr(cp, "CONTENT=", length - (int)(cp - line))) &&
|
|
|
|
(cp = PL_strncasestr(cp, "CHARSET=", length - (int)(cp - line)))
|
2000-01-14 04:01:18 +03:00
|
|
|
)
|
2002-01-04 03:49:44 +03:00
|
|
|
{
|
2002-04-16 22:41:45 +04:00
|
|
|
char* cp1 = cp + 8; //8 for the length of "CHARSET="
|
|
|
|
char* cp2 = PL_strnpbrk(cp1, " \"\'", length - (int)(cp1 - line));
|
|
|
|
if (cp2)
|
2000-01-14 04:01:18 +03:00
|
|
|
{
|
2002-04-16 22:41:45 +04:00
|
|
|
char* charset = PL_strndup(cp1, (int)(cp2 - cp1));
|
2002-01-04 03:49:44 +03:00
|
|
|
|
2002-04-16 22:41:45 +04:00
|
|
|
// Fix bug 101434, in this case since this parsing is a char*
|
|
|
|
// operation, a real UTF-16 or UTF-32 document won't be parse
|
|
|
|
// correctly, if it got parse, it cannot be UTF-16 nor UTF-32
|
|
|
|
// there fore, we ignore them if somehow we got that value
|
|
|
|
// 6 == strlen("UTF-16") or strlen("UTF-32"), this will cover
|
|
|
|
// UTF-16, UTF-16BE, UTF-16LE, UTF-32, UTF-32BE, UTF-32LE
|
|
|
|
if ((charset != nsnull) &&
|
|
|
|
nsCRT::strncasecmp(charset, "UTF-16", 6) &&
|
|
|
|
nsCRT::strncasecmp(charset, "UTF-32", 6))
|
|
|
|
{
|
|
|
|
textHTML->charset = charset;
|
|
|
|
|
|
|
|
// write out the data without the charset part...
|
|
|
|
if (textHTML->charset)
|
|
|
|
{
|
|
|
|
int err = MimeObject_write(obj, line, cp - line, PR_TRUE);
|
|
|
|
if (err == 0)
|
|
|
|
err = MimeObject_write(obj, cp2, length - (int)(cp2 - line), PR_TRUE);
|
|
|
|
|
|
|
|
return err;
|
2000-11-07 01:12:26 +03:00
|
|
|
}
|
2000-01-18 17:23:00 +03:00
|
|
|
}
|
2002-04-16 22:41:45 +04:00
|
|
|
PR_FREEIF(charset);
|
2000-01-14 04:01:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1999-03-19 23:00:36 +03:00
|
|
|
|
2000-01-14 04:01:18 +03:00
|
|
|
// Now, just write out the data...
|
|
|
|
return MimeObject_write(obj, line, length, PR_TRUE);
|
1999-03-19 23:00:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
MimeInlineTextHTML_parse_eof (MimeObject *obj, PRBool abort_p)
|
|
|
|
{
|
|
|
|
int status;
|
2000-01-14 04:01:18 +03:00
|
|
|
MimeInlineTextHTML *textHTML = (MimeInlineTextHTML *) obj;
|
1999-03-19 23:00:36 +03:00
|
|
|
if (obj->closed_p) return 0;
|
|
|
|
|
2000-01-14 04:01:18 +03:00
|
|
|
PR_FREEIF(textHTML->charset);
|
|
|
|
|
1999-03-19 23:00:36 +03:00
|
|
|
/* Run parent method first, to flush out any buffered data. */
|
|
|
|
status = ((MimeObjectClass*)&MIME_SUPERCLASS)->parse_eof(obj, abort_p);
|
|
|
|
if (status < 0) return status;
|
|
|
|
|
2000-04-19 01:07:19 +04:00
|
|
|
if (nsMimeOutput::nsMimeMessageBodyDisplay == obj->options->format_out ||
|
|
|
|
nsMimeOutput::nsMimeMessagePrintOutput == obj->options->format_out)
|
2000-05-12 01:01:19 +04:00
|
|
|
status = MimeObject_write(obj, "</div>", 6, PR_FALSE);
|
2000-04-19 01:07:19 +04:00
|
|
|
|
1999-03-19 23:00:36 +03:00
|
|
|
return 0;
|
|
|
|
}
|