From 9a059a2d2f4fce2c79a71ab37f7d344a5c3cfd0b Mon Sep 17 00:00:00 2001 From: CaS Date: Sun, 26 May 2002 05:45:36 +0000 Subject: [PATCH] Added GSMimeHeader class ... unused git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@13700 72102866-910b-0410-8b05-ffd578937521 --- ChangeLog | 5 + Headers/gnustep/base/GSMime.h | 20 +++ Source/Additions/GSMime.m | 233 ++++++++++++++++++++++++++++++++++ 3 files changed, 258 insertions(+) diff --git a/ChangeLog b/ChangeLog index ff8414fee..d2ae2b5a4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2002-05-26 Richard Frith-Macdonald + + * Headers/Foundation/GSMime.h: Add GSMimeHeader class. + * Source/Additions/GSMime.m: Add GSMimeHeader class. + 2002-05-23 Richard Frith-Macdonald * Source/Additions/GSXML.m: Fix a few errors in last changes. diff --git a/Headers/gnustep/base/GSMime.h b/Headers/gnustep/base/GSMime.h index 11fb47375..d3b97e4b4 100644 --- a/Headers/gnustep/base/GSMime.h +++ b/Headers/gnustep/base/GSMime.h @@ -53,6 +53,26 @@ - (void) setAtEnd: (BOOL)flag; @end +@interface GSMimeHeader : NSObject +{ + NSString *name; + NSString *value; + NSMutableDictionary *params; +} +- (id) initWithName: (NSString*)n value: (NSString*)v params: (NSDictionary*)p; +- (NSString*) makeQuoted: (NSString*)v; +- (NSString*) makeToken: (NSString*)t; +- (NSString*) name; +- (NSDictionary*) params; +- (void) setName: (NSString*)s; +- (void) setParam: (NSString*)v forKey: (NSString*)k; +- (void) setParams: (NSDictionary*)d; +- (void) setValue: (NSString*)s; +- (NSString*) text; +- (NSString*) value; +@end + + @interface GSMimeDocument : NSObject { NSMutableArray *headers; diff --git a/Source/Additions/GSMime.m b/Source/Additions/GSMime.m index 378ee0017..2da679cf5 100644 --- a/Source/Additions/GSMime.m +++ b/Source/Additions/GSMime.m @@ -2163,6 +2163,239 @@ parseCharacterSet(NSString *token) +@implementation GSMimeHeader + +static NSCharacterSet *nonToken = nil; +static NSCharacterSet *tokenSet = nil; + ++ (void) initialize +{ + if (nonToken == nil) + { + NSMutableCharacterSet *ms; + + ms = [NSMutableCharacterSet new]; + [ms addCharactersInRange: NSMakeRange(33, 126-32)]; + [ms removeCharactersInString: @"()<>@,;:\\\"/[]?="]; + tokenSet = [ms copy]; + RELEASE(ms); + nonToken = RETAIN([tokenSet invertedSet]); + } +} + +- (void) dealloc +{ + RELEASE(name); + RELEASE(value); + RELEASE(params); + [super dealloc]; +} + +- (id) init +{ + RELEASE(self); + NSLog(@"You should initialise GSMime using initWithName:value:params:"); + return nil; +} + +- (id) initWithName: (NSString*)n value: (NSString*)v params: (NSDictionary*)p +{ + [self setName: n]; + [self setValue: v]; + [self setParams: p]; + return self; +} + +/** + * Makes the value into a quoted string if necessary. + */ +- (NSString*) makeQuoted: (NSString*)v +{ + NSRange r; + unsigned pos = 0; + unsigned l = [v length]; + + r = [v rangeOfCharacterFromSet: nonToken + options: NSLiteralSearch + range: NSMakeRange(pos, l - pos)]; + if (r.length > 0) + { + NSMutableString *m = [NSMutableString new]; + + [m appendString: @"\""]; + while (r.length > 0) + { + unichar c; + + if (r.location > pos) + { + [m appendString: + [v substringFromRange: NSMakeRange(pos, r.location - pos)]]; + } + pos = r.location + 1; + c = [v characterAtIndex: r.location]; + if (c < 128) + { + [m appendFormat: @"\\%c", c]; + } + else + { + NSLog(@"NON ASCII characters not yet implemented"); + } + r = [v rangeOfCharacterFromSet: nonToken + options: NSLiteralSearch + range: NSMakeRange(pos, l - pos)]; + } + [m appendString: @"\""]; + v = AUTORELEASE(m); + } + return v; +} + +/** + * Convert the supplied string to a standardized token by making it + * lowercase and removing all illegal characters. + */ +- (NSString*) makeToken: (NSString*)t +{ + NSRange r; + + t = [t lowercaseString]; + r = [t rangeOfCharacterFromSet: nonToken]; + if (r.length > 0) + { + NSMutableString *m = [t mutableCopy]; + + while (r.length > 0) + { + [m deleteCharactersInRange: r]; + r = [m rangeOfCharacterFromSet: nonToken]; + } + t = AUTORELEASE(m); + } + return t; +} + +/** + * Returns the name of this header ... a lowercase string. + */ +- (NSString*) name +{ + return name; +} + +/** + * Returns the parameters of this header ... a dictionary whose keys + * are all lowercase strings, and whosre value is a string which may + * contain mixed case. + */ +- (NSDictionary*) params +{ + return AUTORELEASE([params copy]); +} + +/** + * Sets the name of this header ... converts to lowercase. + */ +- (void) setName: (NSString*)s +{ + s = [self makeToken: s]; + + ASSIGN(name, s); +} + +/** + * Sets a parameter of this header ... converts name to lowercase.
+ * If a nil parameter name is supplied, removes any parameter with the + * specified key. + */ +- (void) setParam: (NSString*)v forKey: (NSString*)k +{ + if (params == nil) + { + params = [NSMutableDictionary new]; + } + k = [self makeToken: k]; + if (v == nil) + { + [params removeObjectForKey: k]; + } + else + { + [params setObject: v forKey: k]; + } +} + +/** + * Sets all parameters of this header ... converts names to lowercase. + */ +- (void) setParams: (NSDictionary*)d +{ + NSMutableDictionary *m = [NSMutableDictionary new]; + NSEnumerator *e = [d keyEnumerator]; + NSString *k; + + while ((k = [e nextObject]) != nil) + { + [m setObject: [d objectForKey: k] forKey: [self makeToken: k]]; + } + DESTROY(params); + params = m; +} + +/** + * Sets the value of this header (without changing parameters) + */ +- (void) setValue: (NSString*)s +{ + ASSIGN(value, s); +} + +/** + * Returns the full text of the header, built from its component parts, + * and inclufding a terminating CR-LF + */ +- (NSString*) text +{ + NSMutableString *t = [NSMutableString new]; + NSEnumerator *e = [params keyEnumerator]; + NSString *k; + unsigned l; + + [t appendString: [[self name] capitalizedString]]; + [t appendString: @": "]; + [t appendString: value]; + l = [t length]; + while ((k = [e nextObject]) != nil) + { + NSString *v = [self makeQuoted: [params objectForKey: k]]; + unsigned kl = [k length]; + unsigned vl = [v length]; + + if ((l + kl + vl + 3) > 72) + { + [t appendString: @"\r\n\t"]; + l = 8; + } + [t appendFormat: @"; %@=%@", k, v]; + l += kl + vl + 3; + } + [t appendString: @"\r\n"]; + + return AUTORELEASE(t); +} + +/** + * Returns the value of this header (excluding any parameters) + */ +- (NSString*) value +{ + return value; +} +@end + + + /** *

* This class is intended to provide a wrapper for MIME messages