]> err.no Git - sope/blobdiff - sope-mime/NGMail/NGMimeMessage.m
synced with latest additions and bumped framework versions
[sope] / sope-mime / NGMail / NGMimeMessage.m
index 02c0ba627860a6169f509993817cfb990b2b98c2..0664546e0f72b72d1c15b09406ffcbd728fe3f2e 100644 (file)
@@ -1,24 +1,23 @@
 /*
-  Copyright (C) 2000-2003 SKYRIX Software AG
+  Copyright (C) 2000-2005 SKYRIX Software AG
 
-  This file is part of OGo
+  This file is part of SOPE.
 
-  OGo is free software; you can redistribute it and/or modify it under
+  SOPE is free software; you can redistribute it and/or modify it under
   the terms of the GNU Lesser General Public License as published by the
   Free Software Foundation; either version 2, or (at your option) any
   later version.
 
-  OGo is distributed in the hope that it will be useful, but WITHOUT ANY
+  SOPE 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 Lesser General Public
   License for more details.
 
   You should have received a copy of the GNU Lesser General Public
-  License along with OGo; see the file COPYING.  If not, write to the
+  License along with SOPE; see the file COPYING.  If not, write to the
   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
   02111-1307, USA.
 */
-// $Id$
 
 #include "NGMimeMessage.h"
 #include "common.h"
@@ -53,7 +52,7 @@ static NGMimeType *defaultDataType = nil;
 }
 - (id)initWithHeader:(NGHashMap *)_header {
   if ((self = [super init])) {
-    self->header = [_header retain];
+    self->header = [_header copy];
   }
   return self;
 }
@@ -68,9 +67,9 @@ static NGMimeType *defaultDataType = nil;
 /* NGPart */
 
 - (NSEnumerator *)valuesOfHeaderFieldWithName:(NSString *)_name {
-  if ([_name isEqualToString:@"content-type"] == YES) {
+  if ([_name isEqualToString:@"content-type"])
     return [[NSArray arrayWithObject:[self contentType]] objectEnumerator];
-  }
+  
   return [self->header objectEnumeratorForKey:_name];
 }
 - (NSEnumerator *)headerFieldNames {
@@ -88,29 +87,22 @@ static NGMimeType *defaultDataType = nil;
 /* NGMimePart */
 
 - (NGMimeType *)autodetectContentType {
-  NGMimeType *type = nil;
+  const char *bytes;
+  unsigned   length;
   
-  if ((self->body != nil) &&
-      ([self->body isKindOfClass:[NSData class]] == YES)) {
-    const char *bytes = NULL;
-    unsigned   length = 0;
-
-    bytes  = [self->body bytes];
-    length = [self->body length];
-        
-    while (length > 0) {
-      if ((unsigned char)*bytes > 127) {
-       break;
-      }
-      bytes++;
-      length--;
-    }
-    type = (length > 0) ? defaultDataType : defaultTextType;
-  }
-  else
-    type = defaultTextType;
+  if (!((self->body != nil) && [self->body isKindOfClass:[NSData class]]))
+    return defaultTextType;
   
-  return type;
+  bytes  = [self->body bytes];
+  length = [self->body length];
+  while (length > 0) {
+    if ((unsigned char)*bytes > 127)
+      break;
+    
+    bytes++;
+    length--;
+  }
+  return (length > 0) ? defaultDataType : defaultTextType;
 }
 
 - (NGMimeType *)contentType {
@@ -148,15 +140,84 @@ static NGMimeType *defaultDataType = nil;
   return [self->header objectForKey:@"content-description"];
 }
 
+/* convenience */
+
+- (NSString *)headerForKey:(NSString *)_key {
+  return [[self->header objectEnumeratorForKey:_key] nextObject];
+}
+
+- (NSArray *)headersForKey:(NSString *)_key {
+  NSEnumerator   *values;
+  NSMutableArray *array;
+  id value;
+  
+  if ((values = [self->header objectEnumeratorForKey:_key]) == nil)
+    return nil;
+  
+  array = [NSMutableArray arrayWithCapacity:4];
+  while ((value = [values nextObject]) != nil)
+    [array addObject:value];
+  return array;
+}
+
+- (NSArray *)headerKeys {
+  NSEnumerator *values;
+  NSMutableArray *array  = nil;
+  id name = nil;
+
+  if ((values = [self->header keyEnumerator]) == nil)
+    return nil;
+  
+  array = [[NSMutableArray alloc] init];
+  while ((name = [values nextObject]) != nil)
+    [array addObject:name];
+
+  name = [array copy];
+  [array release];
+  
+  return [name autorelease];
+}
+
+- (NSDictionary *)headers {
+  return [self->header asDictionary];
+}
+
+- (NSString *)headersAsString {
+  // TODO: not correct for MIME
+  NSMutableString *ms;
+  NSEnumerator *keys;
+  NSString     *key;
+  
+  ms = [NSMutableString stringWithCapacity:1024];
+  
+  /* headers */
+  keys = [[self headerKeys] objectEnumerator];
+  while ((key = [keys nextObject]) != nil) {
+    NSEnumerator *vals;
+    id val;
+    
+    vals = [[self headersForKey:key] objectEnumerator];
+    while ((val = [vals nextObject])) {
+      [ms appendString:key];
+      [ms appendString:@": "];
+      [ms appendString:[val stringValue]];
+      [ms appendString:@"\r\n"];
+    }
+  }
+  return ms;
+}
+
 /* description */
 
 - (NSString *)description {
-  NSMutableString *d = [NSMutableString stringWithCapacity:64];
-  id b = [self body];
+  NSMutableString *d;
+  id b;
 
+  d = [NSMutableString stringWithCapacity:64];
   [d appendFormat:@"<%@[0x%08X]: header=%@",
        NSStringFromClass([self class]), self, self->header];
 
+  b = [self body];
   if ([b isKindOfClass:[NSString class]] || [b isKindOfClass:[NSData class]]) {
     if ([b length] < 512)
       [d appendFormat:@" body=%@", b];
@@ -165,9 +226,8 @@ static NGMimeType *defaultDataType = nil;
   }
   else
     [d appendFormat:@" body=%@", b];
-
+  
   [d appendString:@">"];
-
   return d;
 }