2007-02-15 22:04:31 +01:00
|
|
|
/* UIxMailPartHTMLViewer.m - this file is part of SOGo
|
|
|
|
*
|
2009-04-01 15:49:24 +02:00
|
|
|
* Copyright (C) 2007-2009 Inverse inc.
|
2007-02-15 22:04:31 +01:00
|
|
|
*
|
|
|
|
* Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
|
|
|
|
*
|
|
|
|
* This file is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This file is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
2006-06-15 21:34:10 +02:00
|
|
|
|
2007-02-15 22:04:31 +01:00
|
|
|
#import <Foundation/NSArray.h>
|
2007-04-04 16:12:18 +02:00
|
|
|
#import <Foundation/NSDictionary.h>
|
2008-07-04 18:06:09 +02:00
|
|
|
#import <Foundation/NSEnumerator.h>
|
2007-11-20 23:18:19 +01:00
|
|
|
#import <Foundation/NSKeyValueCoding.h>
|
|
|
|
#import <Foundation/NSValue.h>
|
|
|
|
|
2007-02-15 22:04:31 +01:00
|
|
|
#import <SaxObjC/SaxAttributes.h>
|
|
|
|
#import <SaxObjC/SaxContentHandler.h>
|
|
|
|
#import <SaxObjC/SaxLexicalHandler.h>
|
|
|
|
#import <SaxObjC/SaxXMLReader.h>
|
|
|
|
#import <SaxObjC/SaxXMLReaderFactory.h>
|
|
|
|
#import <NGExtensions/NSString+misc.h>
|
|
|
|
#import <NGObjWeb/SoObjects.h>
|
2006-06-15 21:34:10 +02:00
|
|
|
|
2007-11-20 23:18:19 +01:00
|
|
|
#include <libxml/encoding.h>
|
|
|
|
|
2007-12-13 00:59:36 +01:00
|
|
|
#import <SoObjects/Mailer/SOGoMailObject.h>
|
2008-08-22 20:43:08 +02:00
|
|
|
#import <SoObjects/Mailer/SOGoMailBodyPart.h>
|
2007-12-13 00:59:36 +01:00
|
|
|
|
2009-05-21 17:12:10 +02:00
|
|
|
#import <NGMime/NGMimeType.h>
|
|
|
|
|
2007-02-15 22:04:31 +01:00
|
|
|
#import "UIxMailPartHTMLViewer.h"
|
2006-06-15 21:34:10 +02:00
|
|
|
|
2007-08-20 19:46:15 +02:00
|
|
|
#if 0
|
2007-02-15 22:04:31 +01:00
|
|
|
#define showWhoWeAre() NSLog(@"invoked '%@'", NSStringFromSelector(_cmd))
|
2007-08-20 19:46:15 +02:00
|
|
|
#else
|
|
|
|
#define showWhoWeAre()
|
|
|
|
#endif
|
2006-06-15 21:34:10 +02:00
|
|
|
|
2009-06-10 22:00:45 +02:00
|
|
|
/* Tags that are forbidden within the body of the html content */
|
|
|
|
static NSArray *BannedTags = nil;
|
|
|
|
|
2008-08-22 20:43:08 +02:00
|
|
|
static xmlCharEncoding
|
|
|
|
_xmlCharsetForCharset (NSString *charset)
|
|
|
|
{
|
|
|
|
struct { NSString *name; xmlCharEncoding encoding; } xmlEncodings[] = {
|
|
|
|
{ @"us-ascii", XML_CHAR_ENCODING_ASCII},
|
|
|
|
{ @"utf-8", XML_CHAR_ENCODING_UTF8},
|
|
|
|
{ @"utf-16le", XML_CHAR_ENCODING_UTF16LE},
|
|
|
|
{ @"utf-16be", XML_CHAR_ENCODING_UTF16BE},
|
|
|
|
{ @"ucs-4le", XML_CHAR_ENCODING_UCS4LE},
|
|
|
|
{ @"ucs-4be", XML_CHAR_ENCODING_UCS4BE},
|
|
|
|
{ @"ebcdic", XML_CHAR_ENCODING_EBCDIC},
|
|
|
|
// { @"iso-10646" , XML_CHAR_ENCODING_UCS4_2143},
|
|
|
|
// { , XML_CHAR_ENCODING_UCS4_3412},
|
|
|
|
// { @"ucs-2", XML_CHAR_ENCODING_UCS2},
|
|
|
|
{ @"iso8859_1", XML_CHAR_ENCODING_8859_1},
|
|
|
|
{ @"iso-8859-1", XML_CHAR_ENCODING_8859_1},
|
|
|
|
{ @"iso-8859-2", XML_CHAR_ENCODING_8859_2},
|
|
|
|
{ @"iso-8859-3", XML_CHAR_ENCODING_8859_3},
|
|
|
|
{ @"iso-8859-4", XML_CHAR_ENCODING_8859_4},
|
|
|
|
{ @"iso-8859-5", XML_CHAR_ENCODING_8859_5},
|
|
|
|
{ @"iso-8859-6", XML_CHAR_ENCODING_8859_6},
|
|
|
|
{ @"iso-8859-7", XML_CHAR_ENCODING_8859_7},
|
|
|
|
{ @"iso-8859-8", XML_CHAR_ENCODING_8859_8},
|
|
|
|
{ @"iso-8859-9", XML_CHAR_ENCODING_8859_9},
|
|
|
|
{ @"iso-2022-jp", XML_CHAR_ENCODING_2022_JP},
|
|
|
|
// { @"iso-2022-jp", XML_CHAR_ENCODING_SHIFT_JIS},
|
2009-05-21 17:12:10 +02:00
|
|
|
{ @"koi8-r", XML_CHAR_ENCODING_ERROR}, // unsupported, will trigger koi8-r -> utf8 conversion
|
|
|
|
{ @"windows-1251", XML_CHAR_ENCODING_ERROR}, // unsupported, will trigger windows-1251 -> utf8 conversion
|
2008-08-22 20:43:08 +02:00
|
|
|
{ @"euc-jp", XML_CHAR_ENCODING_EUC_JP}};
|
|
|
|
unsigned count;
|
|
|
|
xmlCharEncoding encoding;
|
|
|
|
|
|
|
|
encoding = XML_CHAR_ENCODING_NONE;
|
|
|
|
count = 0;
|
|
|
|
|
|
|
|
while (encoding == XML_CHAR_ENCODING_NONE
|
|
|
|
&& count < (sizeof (xmlEncodings) / sizeof (xmlEncodings[0])))
|
|
|
|
if ([charset isEqualToString: xmlEncodings[count].name])
|
|
|
|
encoding = xmlEncodings[count].encoding;
|
|
|
|
else
|
|
|
|
count++;
|
|
|
|
|
|
|
|
if (encoding == XML_CHAR_ENCODING_NONE)
|
|
|
|
encoding = XML_CHAR_ENCODING_8859_1;
|
|
|
|
|
|
|
|
return encoding;
|
|
|
|
}
|
|
|
|
|
2007-02-15 22:04:31 +01:00
|
|
|
@interface _UIxHTMLMailContentHandler : NSObject <SaxContentHandler, SaxLexicalHandler>
|
|
|
|
{
|
|
|
|
NSMutableString *result;
|
|
|
|
NSMutableString *css;
|
|
|
|
NSDictionary *attachmentIds;
|
2009-06-10 22:00:45 +02:00
|
|
|
BOOL ignoreContent;
|
|
|
|
NSString *ignoreTag;
|
2007-02-15 22:04:31 +01:00
|
|
|
BOOL inBody;
|
|
|
|
BOOL inStyle;
|
|
|
|
BOOL inCSSDeclaration;
|
2007-02-26 17:37:59 +01:00
|
|
|
BOOL hasEmbeddedCSS;
|
2007-11-20 23:18:19 +01:00
|
|
|
xmlCharEncoding contentEncoding;
|
2007-02-15 22:04:31 +01:00
|
|
|
}
|
2006-06-15 21:34:10 +02:00
|
|
|
|
2007-02-15 22:04:31 +01:00
|
|
|
- (NSString *) result;
|
2006-06-15 21:34:10 +02:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
2007-02-15 22:04:31 +01:00
|
|
|
@implementation _UIxHTMLMailContentHandler
|
|
|
|
|
2009-06-10 22:00:45 +02:00
|
|
|
+ (void) initialize
|
|
|
|
{
|
|
|
|
if (!BannedTags)
|
|
|
|
{
|
2009-09-15 01:05:45 +02:00
|
|
|
BannedTags = [NSArray arrayWithObjects: @"script", @"link", @"base",
|
|
|
|
@"meta", @"title", nil];
|
2009-06-10 22:00:45 +02:00
|
|
|
[BannedTags retain];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-15 22:04:31 +01:00
|
|
|
- (id) init
|
|
|
|
{
|
|
|
|
if ((self = [super init]))
|
|
|
|
{
|
|
|
|
css = nil;
|
|
|
|
result = nil;
|
2009-06-10 22:00:45 +02:00
|
|
|
ignoreTag = nil;
|
2007-02-15 22:04:31 +01:00
|
|
|
attachmentIds = nil;
|
2007-11-20 23:18:19 +01:00
|
|
|
contentEncoding = XML_CHAR_ENCODING_UTF8;
|
2007-02-15 22:04:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
2007-09-07 17:45:46 +02:00
|
|
|
[result release];
|
|
|
|
[css release];
|
2007-02-15 22:04:31 +01:00
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
2007-11-20 23:18:19 +01:00
|
|
|
- (void) setContentEncoding: (xmlCharEncoding) newContentEncoding
|
|
|
|
{
|
|
|
|
contentEncoding = newContentEncoding;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (xmlCharEncoding) contentEncoding
|
|
|
|
{
|
|
|
|
return contentEncoding;
|
|
|
|
}
|
|
|
|
|
2007-02-15 22:04:31 +01:00
|
|
|
- (void) setAttachmentIds: (NSDictionary *) newAttachmentIds
|
|
|
|
{
|
|
|
|
attachmentIds = newAttachmentIds;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) css
|
|
|
|
{
|
2007-09-07 17:45:46 +02:00
|
|
|
return css;
|
2007-02-15 22:04:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) result
|
|
|
|
{
|
2007-09-07 17:45:46 +02:00
|
|
|
return result;
|
2007-02-15 22:04:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* SaxContentHandler */
|
|
|
|
- (void) startDocument
|
|
|
|
{
|
2007-08-20 19:46:15 +02:00
|
|
|
showWhoWeAre();
|
|
|
|
|
2007-09-07 17:45:46 +02:00
|
|
|
[css release];
|
|
|
|
[result release];
|
2007-02-15 22:04:31 +01:00
|
|
|
|
|
|
|
result = [NSMutableString new];
|
|
|
|
css = [NSMutableString new];
|
2009-06-10 22:00:45 +02:00
|
|
|
|
|
|
|
ignoreContent = NO;
|
|
|
|
[ignoreTag release];
|
|
|
|
ignoreTag = nil;
|
2007-09-07 17:45:46 +02:00
|
|
|
|
2007-02-15 22:04:31 +01:00
|
|
|
inBody = NO;
|
|
|
|
inStyle = NO;
|
|
|
|
inCSSDeclaration = NO;
|
2007-02-26 17:37:59 +01:00
|
|
|
hasEmbeddedCSS = NO;
|
2007-02-15 22:04:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) endDocument
|
|
|
|
{
|
2007-08-20 19:46:15 +02:00
|
|
|
showWhoWeAre();
|
2007-02-15 22:04:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) startPrefixMapping: (NSString *)_prefix
|
|
|
|
uri: (NSString *)_uri
|
|
|
|
{
|
|
|
|
showWhoWeAre();
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) endPrefixMapping: (NSString *)_prefix
|
|
|
|
{
|
|
|
|
showWhoWeAre();
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) _appendStyle: (unichar *) _chars
|
|
|
|
length: (int) _len
|
|
|
|
{
|
2009-06-10 22:00:45 +02:00
|
|
|
unsigned int count, length;
|
2007-02-15 22:04:31 +01:00
|
|
|
unichar *start, *currentChar;
|
|
|
|
|
|
|
|
start = _chars;
|
2009-06-10 22:00:45 +02:00
|
|
|
while (*start < 33)
|
|
|
|
start++;
|
|
|
|
|
2007-02-15 22:04:31 +01:00
|
|
|
currentChar = start;
|
|
|
|
for (count = 0; count < _len; count++)
|
|
|
|
{
|
|
|
|
currentChar = _chars + count;
|
|
|
|
if (inCSSDeclaration)
|
|
|
|
{
|
2008-04-15 18:04:16 +02:00
|
|
|
if (*currentChar == '}')
|
2007-02-26 17:37:59 +01:00
|
|
|
{
|
|
|
|
inCSSDeclaration = NO;
|
|
|
|
hasEmbeddedCSS = NO;
|
|
|
|
}
|
2007-02-15 22:04:31 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-06-10 22:00:45 +02:00
|
|
|
if (*currentChar < 32)
|
2007-02-15 22:04:31 +01:00
|
|
|
{
|
2009-06-10 22:00:45 +02:00
|
|
|
if (currentChar > start)
|
|
|
|
[css appendString: [NSString stringWithCharacters: start
|
|
|
|
length: (currentChar - start)]];
|
|
|
|
start = currentChar + 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (*currentChar == '{')
|
|
|
|
inCSSDeclaration = YES;
|
|
|
|
else if (*currentChar == ',')
|
|
|
|
hasEmbeddedCSS = NO;
|
|
|
|
else if (!hasEmbeddedCSS)
|
|
|
|
{
|
|
|
|
if (*currentChar == '@')
|
2007-02-26 17:37:59 +01:00
|
|
|
hasEmbeddedCSS = YES;
|
2009-06-10 22:00:45 +02:00
|
|
|
else
|
|
|
|
if (*currentChar > 32)
|
|
|
|
{
|
|
|
|
length = (currentChar - start);
|
|
|
|
[css appendFormat: @"%@\n.SOGoHTMLMail-CSS-Delimiter ",
|
|
|
|
[NSString stringWithCharacters: start length: length]];
|
|
|
|
hasEmbeddedCSS = YES;
|
|
|
|
start = currentChar;
|
|
|
|
}
|
|
|
|
}
|
2007-02-15 22:04:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-06-10 22:00:45 +02:00
|
|
|
if (currentChar > start)
|
|
|
|
[css appendString: [NSString stringWithCharacters: start
|
|
|
|
length: (currentChar - start)]];
|
2007-02-15 22:04:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) startElement: (NSString *) _localName
|
|
|
|
namespace: (NSString *) _ns
|
|
|
|
rawName: (NSString *) _rawName
|
|
|
|
attributes: (id <SaxAttributes>) _attributes
|
|
|
|
{
|
|
|
|
unsigned int count, max;
|
2009-09-15 01:05:45 +02:00
|
|
|
NSString *name, *value, *cid, *lowerName;
|
2007-02-15 22:04:31 +01:00
|
|
|
NSMutableString *resultPart;
|
|
|
|
BOOL skipAttribute;
|
|
|
|
|
2007-08-20 19:46:15 +02:00
|
|
|
showWhoWeAre();
|
2009-06-10 22:00:45 +02:00
|
|
|
|
2009-09-15 01:05:45 +02:00
|
|
|
lowerName = [_localName lowercaseString];
|
2009-06-10 22:00:45 +02:00
|
|
|
if (inStyle || ignoreContent)
|
2007-02-15 22:04:31 +01:00
|
|
|
;
|
2009-09-15 01:05:45 +02:00
|
|
|
else if ([lowerName isEqualToString: @"base"])
|
2009-02-06 02:15:24 +01:00
|
|
|
;
|
2009-09-15 01:05:45 +02:00
|
|
|
else if ([lowerName isEqualToString: @"meta"])
|
2009-05-21 17:12:10 +02:00
|
|
|
;
|
2009-09-15 01:05:45 +02:00
|
|
|
else if ([lowerName isEqualToString: @"body"])
|
2007-02-15 22:04:31 +01:00
|
|
|
inBody = YES;
|
2009-09-15 01:05:45 +02:00
|
|
|
else if ([lowerName isEqualToString: @"style"])
|
2007-02-15 22:04:31 +01:00
|
|
|
inStyle = YES;
|
|
|
|
else if (inBody)
|
|
|
|
{
|
2009-09-15 01:05:45 +02:00
|
|
|
if ([BannedTags containsObject: lowerName])
|
2009-06-10 22:00:45 +02:00
|
|
|
{
|
2009-09-15 01:05:45 +02:00
|
|
|
ignoreTag = [lowerName copy];
|
2009-06-10 22:00:45 +02:00
|
|
|
ignoreContent = YES;
|
|
|
|
}
|
|
|
|
else
|
2007-02-15 22:04:31 +01:00
|
|
|
{
|
2009-06-10 22:00:45 +02:00
|
|
|
resultPart = [NSMutableString new];
|
|
|
|
[resultPart appendFormat: @"<%@", _rawName];
|
|
|
|
|
|
|
|
max = [_attributes count];
|
|
|
|
for (count = 0; count < max; count++)
|
2007-02-15 22:04:31 +01:00
|
|
|
{
|
2009-06-10 22:00:45 +02:00
|
|
|
skipAttribute = NO;
|
2009-09-15 01:05:45 +02:00
|
|
|
name = [[_attributes nameAtIndex: count] lowercaseString];
|
2009-06-10 22:00:45 +02:00
|
|
|
if ([name hasPrefix: @"ON"])
|
|
|
|
skipAttribute = YES;
|
2009-09-15 01:05:45 +02:00
|
|
|
else if ([name isEqualToString: @"src"])
|
2009-06-10 22:00:45 +02:00
|
|
|
{
|
|
|
|
value = [_attributes valueAtIndex: count];
|
|
|
|
if ([value hasPrefix: @"cid:"])
|
|
|
|
{
|
|
|
|
cid = [NSString stringWithFormat: @"<%@>",
|
|
|
|
[value substringFromIndex: 4]];
|
|
|
|
value = [attachmentIds objectForKey: cid];
|
|
|
|
skipAttribute = (value == nil);
|
|
|
|
}
|
2009-09-15 01:05:45 +02:00
|
|
|
else if ([lowerName isEqualToString: @"img"])
|
2009-06-10 22:00:45 +02:00
|
|
|
{
|
2009-09-15 01:05:45 +02:00
|
|
|
/* [resultPart appendString:
|
|
|
|
@"src=\"/SOGo.woa/WebServerResources/empty.gif\""]; */
|
|
|
|
name = @"unsafe-src";
|
2009-06-10 22:00:45 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
skipAttribute = YES;
|
|
|
|
}
|
2009-09-15 01:05:45 +02:00
|
|
|
else if ([name isEqualToString: @"href"]
|
|
|
|
|| [name isEqualToString: @"action"])
|
2007-02-15 22:04:31 +01:00
|
|
|
{
|
2009-06-10 22:00:45 +02:00
|
|
|
value = [_attributes valueAtIndex: count];
|
|
|
|
skipAttribute = ([value rangeOfString: @"://"].location
|
|
|
|
== NSNotFound
|
|
|
|
&& ![value hasPrefix: @"#"]);
|
2007-02-15 22:04:31 +01:00
|
|
|
}
|
2009-06-10 22:00:45 +02:00
|
|
|
else
|
|
|
|
value = [_attributes valueAtIndex: count];
|
|
|
|
if (!skipAttribute)
|
|
|
|
[resultPart appendFormat: @" %@=\"%@\"",
|
|
|
|
name, [value stringByReplacingString: @"\""
|
|
|
|
withString: @"\\\""]];
|
2007-02-15 22:04:31 +01:00
|
|
|
}
|
|
|
|
|
2009-06-10 22:00:45 +02:00
|
|
|
[resultPart appendString: @">"];
|
|
|
|
[result appendString: resultPart];
|
|
|
|
[resultPart release];
|
|
|
|
}
|
2007-02-15 22:04:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-07 17:45:46 +02:00
|
|
|
- (void) _finishCSS
|
|
|
|
{
|
2009-06-10 22:00:45 +02:00
|
|
|
NSRange excessiveDelimiter;
|
|
|
|
|
|
|
|
[css replaceString: @"<!--" withString: @""];
|
|
|
|
[css replaceString: @"-->" withString: @""];
|
2007-09-07 17:45:46 +02:00
|
|
|
[css replaceString: @".SOGoHTMLMail-CSS-Delimiter body"
|
|
|
|
withString: @".SOGoHTMLMail-CSS-Delimiter"];
|
|
|
|
[css replaceString: @";" withString: @" !important;"];
|
2009-06-10 22:00:45 +02:00
|
|
|
|
|
|
|
excessiveDelimiter = [css rangeOfString: @".SOGoHTMLMail-CSS-Delimiter "
|
|
|
|
options: NSBackwardsSearch];
|
|
|
|
if (excessiveDelimiter.location != NSNotFound)
|
|
|
|
{
|
|
|
|
if (NSMaxRange (excessiveDelimiter) == [css length])
|
|
|
|
[css deleteCharactersInRange: excessiveDelimiter];
|
|
|
|
}
|
2007-09-07 17:45:46 +02:00
|
|
|
}
|
|
|
|
|
2007-02-15 22:04:31 +01:00
|
|
|
- (void) endElement: (NSString *) _localName
|
|
|
|
namespace: (NSString *) _ns
|
|
|
|
rawName: (NSString *) _rawName
|
|
|
|
{
|
2009-09-15 01:05:45 +02:00
|
|
|
NSString *lowerName;
|
2009-06-10 22:00:45 +02:00
|
|
|
|
2007-08-20 19:46:15 +02:00
|
|
|
showWhoWeAre();
|
|
|
|
|
2009-09-15 01:05:45 +02:00
|
|
|
lowerName = [_localName lowercaseString];
|
2009-06-10 22:00:45 +02:00
|
|
|
|
|
|
|
if (ignoreContent)
|
2007-02-15 22:04:31 +01:00
|
|
|
{
|
2009-09-15 01:05:45 +02:00
|
|
|
if ([lowerName isEqualToString: ignoreTag])
|
2009-06-10 22:00:45 +02:00
|
|
|
{
|
|
|
|
ignoreContent = NO;
|
|
|
|
[ignoreTag release];
|
|
|
|
ignoreTag = nil;
|
|
|
|
}
|
2007-02-15 22:04:31 +01:00
|
|
|
}
|
2009-06-10 22:00:45 +02:00
|
|
|
else
|
2007-02-15 22:04:31 +01:00
|
|
|
{
|
2009-06-10 22:00:45 +02:00
|
|
|
if (inStyle)
|
|
|
|
{
|
2009-09-15 01:05:45 +02:00
|
|
|
if ([lowerName isEqualToString: @"style"])
|
2009-06-10 22:00:45 +02:00
|
|
|
{
|
|
|
|
inStyle = NO;
|
|
|
|
inCSSDeclaration = NO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (inBody)
|
|
|
|
{
|
2009-09-15 01:05:45 +02:00
|
|
|
if ([lowerName isEqualToString: @"body"])
|
2009-06-10 22:00:45 +02:00
|
|
|
{
|
|
|
|
inBody = NO;
|
|
|
|
if (css)
|
|
|
|
[self _finishCSS];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-06-30 20:01:14 +02:00
|
|
|
//NSLog (@"%@", _localName);
|
2009-06-10 22:00:45 +02:00
|
|
|
[result appendFormat: @"</%@>", _localName];
|
|
|
|
}
|
|
|
|
}
|
2007-02-15 22:04:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) characters: (unichar *) _chars
|
|
|
|
length: (int) _len
|
|
|
|
{
|
2007-08-20 19:46:15 +02:00
|
|
|
showWhoWeAre();
|
2009-06-10 22:00:45 +02:00
|
|
|
if (!ignoreContent)
|
2007-02-15 22:04:31 +01:00
|
|
|
{
|
|
|
|
if (inStyle)
|
|
|
|
[self _appendStyle: _chars length: _len];
|
2007-09-07 17:45:46 +02:00
|
|
|
else if (inBody)
|
2007-02-15 22:04:31 +01:00
|
|
|
{
|
2007-12-12 17:47:01 +01:00
|
|
|
NSString *tmpString;
|
|
|
|
|
2007-02-15 22:04:31 +01:00
|
|
|
tmpString = [NSString stringWithCharacters: _chars length: _len];
|
2009-06-10 22:00:45 +02:00
|
|
|
|
2007-12-12 17:47:01 +01:00
|
|
|
// HACK: This is to avoid appending the useless junk in the <html> tag
|
|
|
|
// that Outlook adds. It seems to confuse the XML parser for
|
|
|
|
// forwarded messages as we get this in the _body_ of the email
|
|
|
|
// while we really aren't in it!
|
|
|
|
if (![tmpString hasPrefix: @" xmlns:v=\"urn:schemas-microsoft-com:vml\""])
|
|
|
|
[result appendString: [tmpString stringByEscapingHTMLString]];
|
2007-02-15 22:04:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) ignorableWhitespace: (unichar *) _chars
|
|
|
|
length: (int) _len
|
|
|
|
{
|
|
|
|
showWhoWeAre();
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) processingInstruction: (NSString *) _pi
|
|
|
|
data: (NSString *) _data
|
|
|
|
{
|
|
|
|
showWhoWeAre();
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setDocumentLocator: (id <NSObject, SaxLocator>) _locator
|
|
|
|
{
|
|
|
|
showWhoWeAre();
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) skippedEntity: (NSString *) _entityName
|
|
|
|
{
|
|
|
|
showWhoWeAre();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* SaxLexicalHandler */
|
|
|
|
- (void) comment: (unichar *) _chars
|
|
|
|
length: (int) _len
|
|
|
|
{
|
2007-08-20 19:46:15 +02:00
|
|
|
showWhoWeAre();
|
2007-02-15 22:04:31 +01:00
|
|
|
if (inStyle)
|
|
|
|
[self _appendStyle: _chars length: _len];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) startDTD: (NSString *) _name
|
|
|
|
publicId: (NSString *) _pub
|
|
|
|
systemId: (NSString *) _sys
|
|
|
|
{
|
|
|
|
showWhoWeAre();
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) endDTD
|
|
|
|
{
|
|
|
|
showWhoWeAre();
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) startEntity: (NSString *) _name
|
|
|
|
{
|
|
|
|
showWhoWeAre();
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) endEntity: (NSString *) _name
|
|
|
|
{
|
|
|
|
showWhoWeAre();
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) startCDATA
|
|
|
|
{
|
|
|
|
showWhoWeAre();
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) endCDATA
|
|
|
|
{
|
|
|
|
showWhoWeAre();
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface NSDictionary (SOGoDebug)
|
|
|
|
|
|
|
|
- (void) dump;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation NSDictionary (SOGoDebug)
|
|
|
|
|
|
|
|
- (void) dump
|
|
|
|
{
|
|
|
|
NSEnumerator *keys;
|
|
|
|
NSString *key;
|
|
|
|
NSMutableString *dump;
|
|
|
|
|
|
|
|
dump = [NSMutableString new];
|
|
|
|
[dump appendFormat: @"\nNSDictionary dump (%@):\n", self];
|
|
|
|
keys = [[self allKeys] objectEnumerator];
|
|
|
|
key = [keys nextObject];
|
|
|
|
while (key)
|
|
|
|
{
|
|
|
|
[dump appendFormat: @"%@: %@\n", key, [self objectForKey: key]];
|
|
|
|
key = [keys nextObject];
|
|
|
|
}
|
|
|
|
[dump appendFormat: @"--- end ---\n"];
|
|
|
|
|
|
|
|
NSLog (dump);
|
|
|
|
[dump release];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
2006-06-15 21:34:10 +02:00
|
|
|
|
|
|
|
@implementation UIxMailPartHTMLViewer
|
2007-02-15 22:04:31 +01:00
|
|
|
|
2007-09-07 17:45:46 +02:00
|
|
|
- (id) init
|
|
|
|
{
|
|
|
|
if ((self = [super init]))
|
|
|
|
{
|
|
|
|
handler = nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
[handler release];
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
2008-08-22 20:43:08 +02:00
|
|
|
- (xmlCharEncoding) _xmlCharEncoding
|
2007-11-20 23:18:19 +01:00
|
|
|
{
|
2008-08-22 20:43:08 +02:00
|
|
|
NSString *charset;
|
2007-11-20 23:18:19 +01:00
|
|
|
|
2008-08-22 20:43:08 +02:00
|
|
|
charset = [[bodyInfo objectForKey:@"parameterList"]
|
|
|
|
objectForKey: @"charset"];
|
|
|
|
if (![charset length])
|
|
|
|
charset = @"us-ascii";
|
2007-11-20 23:18:19 +01:00
|
|
|
|
2009-05-21 17:12:10 +02:00
|
|
|
return _xmlCharsetForCharset([charset lowercaseString]);
|
2008-08-22 20:43:08 +02:00
|
|
|
}
|
2007-11-20 23:18:19 +01:00
|
|
|
|
2008-08-22 20:43:08 +02:00
|
|
|
- (void) _parseContent
|
|
|
|
{
|
|
|
|
NSObject <SaxXMLReader> *parser;
|
|
|
|
NSData *preparsedContent;
|
|
|
|
SOGoMailObject *mail;
|
2009-05-21 17:12:10 +02:00
|
|
|
xmlCharEncoding enc;
|
2007-11-20 23:18:19 +01:00
|
|
|
|
2008-08-22 20:43:08 +02:00
|
|
|
mail = [self clientObject];
|
|
|
|
|
|
|
|
preparsedContent = [super decodedFlatContent];
|
|
|
|
parser = [[SaxXMLReaderFactory standardXMLReaderFactory]
|
|
|
|
createXMLReaderForMimeType: @"text/html"];
|
|
|
|
|
|
|
|
handler = [_UIxHTMLMailContentHandler new];
|
|
|
|
[handler setAttachmentIds: [mail fetchAttachmentIds]];
|
2009-05-21 17:12:10 +02:00
|
|
|
|
|
|
|
// We check if we got an unsupported charset. If so
|
|
|
|
// we convert everything to UTF-16{LE,BE} so it passes
|
|
|
|
// in libxml2 and also in characters: length: defined
|
|
|
|
// in this file (that expects unichar:s)
|
|
|
|
enc = [self _xmlCharEncoding];
|
|
|
|
if (enc == XML_CHAR_ENCODING_ERROR)
|
|
|
|
{
|
|
|
|
NSString *s;
|
|
|
|
|
|
|
|
s = [[NSString alloc] initWithData: preparsedContent
|
|
|
|
encoding: [NGMimeType stringEncodingForCharset:
|
|
|
|
[[bodyInfo objectForKey:@"parameterList"]
|
|
|
|
objectForKey: @"charset"]]];
|
|
|
|
[s autorelease];
|
|
|
|
|
|
|
|
#if BYTE_ORDER == BIG_ENDIAN
|
|
|
|
preparsedContent = [s dataUsingEncoding: NSUTF16BigEndianStringEncoding];
|
|
|
|
enc = XML_CHAR_ENCODING_UTF16BE;
|
|
|
|
#else
|
|
|
|
preparsedContent = [s dataUsingEncoding: NSUTF16LittleEndianStringEncoding];
|
|
|
|
enc = XML_CHAR_ENCODING_UTF16LE;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
[handler setContentEncoding: enc];
|
2008-09-30 21:34:31 +02:00
|
|
|
|
2008-08-22 20:43:08 +02:00
|
|
|
[parser setContentHandler: handler];
|
|
|
|
[parser parseFromSource: preparsedContent];
|
2007-11-20 23:18:19 +01:00
|
|
|
}
|
|
|
|
|
2008-08-22 20:43:08 +02:00
|
|
|
- (NSString *) cssContent
|
|
|
|
{
|
|
|
|
NSString *cssContent, *css;
|
|
|
|
|
|
|
|
if (!handler)
|
|
|
|
[self _parseContent];
|
|
|
|
|
|
|
|
css = [handler css];
|
|
|
|
if ([css length])
|
|
|
|
cssContent
|
|
|
|
= [NSString stringWithFormat: @"<style type=\"text/css\">%@</style>",
|
|
|
|
[handler css]];
|
|
|
|
else
|
|
|
|
cssContent = @"";
|
|
|
|
|
|
|
|
return cssContent;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) flatContentAsString
|
|
|
|
{
|
|
|
|
if (!handler)
|
|
|
|
[self _parseContent];
|
2009-05-21 17:12:10 +02:00
|
|
|
|
2008-08-22 20:43:08 +02:00
|
|
|
return [handler result];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation UIxMailPartExternalHTMLViewer
|
|
|
|
|
|
|
|
- (id) init
|
|
|
|
{
|
|
|
|
if ((self = [super init]))
|
|
|
|
{
|
|
|
|
handler = nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
2007-11-20 23:18:19 +01:00
|
|
|
{
|
2008-08-22 20:43:08 +02:00
|
|
|
[handler release];
|
|
|
|
[super dealloc];
|
|
|
|
}
|
2007-11-20 23:18:19 +01:00
|
|
|
|
2008-08-22 20:43:08 +02:00
|
|
|
- (xmlCharEncoding) _xmlCharEncoding
|
|
|
|
{
|
2007-11-20 23:18:19 +01:00
|
|
|
NSString *charset;
|
|
|
|
|
|
|
|
charset = [[bodyInfo objectForKey:@"parameterList"]
|
|
|
|
objectForKey: @"charset"];
|
|
|
|
if (![charset length])
|
|
|
|
charset = @"us-ascii";
|
2008-08-22 20:43:08 +02:00
|
|
|
|
2009-05-21 17:12:10 +02:00
|
|
|
return _xmlCharsetForCharset([charset lowercaseString]);
|
2007-11-20 23:18:19 +01:00
|
|
|
}
|
|
|
|
|
2007-09-07 17:45:46 +02:00
|
|
|
- (void) _parseContent
|
2007-02-15 22:04:31 +01:00
|
|
|
{
|
2007-11-20 23:18:19 +01:00
|
|
|
NSObject <SaxXMLReader> *parser;
|
2007-08-20 19:46:15 +02:00
|
|
|
NSData *preparsedContent;
|
2007-12-13 00:59:36 +01:00
|
|
|
SOGoMailObject *mail;
|
2008-08-22 20:43:08 +02:00
|
|
|
SOGoMailBodyPart *part;
|
|
|
|
NSString *encoding;
|
2009-05-21 17:12:10 +02:00
|
|
|
xmlCharEncoding enc;
|
2007-12-13 00:59:36 +01:00
|
|
|
|
2008-08-22 20:43:08 +02:00
|
|
|
part = [self clientObject];
|
|
|
|
mail = [part mailObject];
|
2007-08-20 19:46:15 +02:00
|
|
|
|
2008-08-22 20:43:08 +02:00
|
|
|
preparsedContent = [part fetchBLOB];
|
2007-02-15 22:04:31 +01:00
|
|
|
parser = [[SaxXMLReaderFactory standardXMLReaderFactory]
|
|
|
|
createXMLReaderForMimeType: @"text/html"];
|
2008-08-22 20:43:08 +02:00
|
|
|
encoding = [[part partInfo] valueForKey: @"encoding"];
|
|
|
|
if (![encoding length])
|
|
|
|
encoding = @"us-ascii";
|
2007-02-15 22:04:31 +01:00
|
|
|
|
|
|
|
handler = [_UIxHTMLMailContentHandler new];
|
2007-12-13 00:59:36 +01:00
|
|
|
[handler setAttachmentIds: [mail fetchAttachmentIds]];
|
2009-05-21 17:12:10 +02:00
|
|
|
|
|
|
|
// We check if we got an unsupported charset. If so
|
|
|
|
// we convert everything to UTF-16{LE,BE} so it passes
|
|
|
|
// in libxml2 and also in characters: length: defined
|
|
|
|
// in this file (that expects unichar:s)
|
|
|
|
enc = _xmlCharsetForCharset(encoding);
|
|
|
|
if (enc == XML_CHAR_ENCODING_ERROR)
|
|
|
|
{
|
|
|
|
NSString *s;
|
|
|
|
|
|
|
|
s = [[NSString alloc] initWithData: preparsedContent
|
|
|
|
encoding: [NGMimeType stringEncodingForCharset:
|
|
|
|
[[bodyInfo objectForKey:@"parameterList"]
|
|
|
|
objectForKey: @"charset"]]];
|
|
|
|
[s autorelease];
|
|
|
|
|
|
|
|
#if BYTE_ORDER == BIG_ENDIAN
|
|
|
|
preparsedContent = [s dataUsingEncoding: NSUTF16BigEndianStringEncoding];
|
|
|
|
enc = XML_CHAR_ENCODING_UTF16BE;
|
|
|
|
#else
|
|
|
|
preparsedContent = [s dataUsingEncoding: NSUTF16LittleEndianStringEncoding];
|
|
|
|
enc = XML_CHAR_ENCODING_UTF16LE;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
[handler setContentEncoding: enc];
|
2007-02-15 22:04:31 +01:00
|
|
|
[parser setContentHandler: handler];
|
|
|
|
[parser parseFromSource: preparsedContent];
|
2007-09-07 17:45:46 +02:00
|
|
|
}
|
|
|
|
|
2008-08-22 20:43:08 +02:00
|
|
|
- (NSString *) filename
|
|
|
|
{
|
|
|
|
return [[self clientObject] filename];
|
|
|
|
}
|
|
|
|
|
2007-09-07 17:45:46 +02:00
|
|
|
- (NSString *) cssContent
|
|
|
|
{
|
|
|
|
NSString *cssContent, *css;
|
|
|
|
|
|
|
|
if (!handler)
|
|
|
|
[self _parseContent];
|
2007-02-15 22:04:31 +01:00
|
|
|
|
|
|
|
css = [handler css];
|
|
|
|
if ([css length])
|
2007-11-19 21:01:27 +01:00
|
|
|
cssContent
|
|
|
|
= [NSString stringWithFormat: @"<style type=\"text/css\">%@</style>",
|
|
|
|
[handler css]];
|
2007-09-07 17:45:46 +02:00
|
|
|
else
|
|
|
|
cssContent = @"";
|
|
|
|
|
|
|
|
return cssContent;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) flatContentAsString
|
|
|
|
{
|
|
|
|
if (!handler)
|
|
|
|
[self _parseContent];
|
2007-02-15 22:04:31 +01:00
|
|
|
|
2007-09-07 17:45:46 +02:00
|
|
|
return [handler result];
|
2007-02-15 22:04:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|