2 Copyright (C) 2004-2005 SKYRIX Software AG
4 This file is part of OpenGroupware.org.
6 OGo is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with OGo; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 #import <Foundation/NSArray.h>
23 #import <Foundation/NSDictionary.h>
24 #import <Foundation/NSEnumerator.h>
25 #import <Foundation/NSString.h>
26 #import <Foundation/NSUserDefaults.h>
28 #import <NGObjWeb/NSException+HTTP.h>
29 #import <NGObjWeb/SoObject+SoDAV.h>
30 #import <NGObjWeb/WOContext.h>
31 #import <NGObjWeb/WOResponse.h>
32 #import <NGExtensions/NSNull+misc.h>
33 #import <NGExtensions/NSObject+Logs.h>
34 #import <NGExtensions/NGBase64Coding.h>
35 #import <NGImap4/NGImap4Connection.h>
37 #import <SoObjects/SOGo/NSDictionary+Utilities.h>
39 #import "SOGoMailObject.h"
40 #import "SOGoMailManager.h"
42 #import "SOGoMailBodyPart.h"
44 @implementation SOGoMailBodyPart
46 static NSString *mailETag = nil;
47 static BOOL debugOn = NO;
51 NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
53 if (![[ud objectForKey:@"SOGoMailDisableETag"] boolValue]) {
54 mailETag = [[NSString alloc] initWithFormat:@"\"imap4url_%d_%d_%03d\"",
55 UIX_MAILER_MAJOR_VERSION,
56 UIX_MAILER_MINOR_VERSION,
57 UIX_MAILER_SUBMINOR_VERSION];
58 NSLog(@"Note(SOGoMailBodyPart): using constant etag for mail parts: '%@'",
62 NSLog(@"Note(SOGoMailBodyPart): etag caching disabled!");
66 [self->partInfo release];
67 [self->identifier release];
68 [self->pathToPart release];
74 - (SOGoMailObject *)mailObject {
75 return [[self container] mailObject];
80 - (NSString *)bodyPartName {
84 s = [self nameInContainer];
85 r = [s rangeOfString:@"."]; /* strip extensions */
88 return [s substringToIndex:r.location];
91 - (NSArray *)bodyPartPath {
95 if (self->pathToPart != nil)
96 return [self->pathToPart isNotNull] ? self->pathToPart : nil;
98 p = [[NSMutableArray alloc] initWithCapacity:8];
99 for (obj = self; [obj isKindOfClass:[SOGoMailBodyPart class]];
100 obj = [obj container]) {
101 [p insertObject:[obj bodyPartName] atIndex:0];
104 self->pathToPart = [p copy];
106 return self->pathToPart;
109 - (NSString *)bodyPartIdentifier {
110 if (self->identifier != nil)
111 return [self->identifier isNotNull] ? self->identifier : nil;
114 [[[self bodyPartPath] componentsJoinedByString:@"."] copy];
115 return self->identifier;
118 - (NSURL *)imap4URL {
119 /* reuse URL of message */
120 return [[self mailObject] imap4URL];
126 if (self->partInfo != nil)
127 return [self->partInfo isNotNull] ? self->partInfo : nil;
130 [[[self mailObject] lookupInfoForBodyPart:[self bodyPartPath]] retain];
131 return self->partInfo;
136 - (id) lookupImap4BodyPartKey: (NSString *) _key
139 // TODO: we might want to check for existence prior controller creation
142 clazz = [SOGoMailBodyPart bodyPartClassForKey:_key inContext:_ctx];
144 return [clazz objectWithName: _key inContainer: self];
147 - (id) lookupName: (NSString *) _key
149 acquire: (BOOL) _flag
153 /* first check attributes directly bound to the application */
154 obj = [super lookupName:_key inContext:_ctx acquire:NO];
157 /* lookup body part */
158 if ([self isBodyPartKey:_key inContext:_ctx])
159 obj = [self lookupImap4BodyPartKey:_key inContext:_ctx];
160 /* should check whether such a filename exist in the attached names */
170 - (NSData *) fetchBLOB
172 // HEADER, HEADER.FIELDS, HEADER.FIELDS.NOT, MIME, TEXT
176 data = [[self imap4Connection] fetchContentOfBodyPart:
177 [self bodyPartIdentifier]
178 atURL:[self imap4URL]];
179 if (data == nil) return nil;
181 /* check for content encodings */
183 if ((enc = [[self partInfo] valueForKey:@"encoding"]) != nil) {
184 enc = [enc uppercaseString];
186 if ([enc isEqualToString:@"BASE64"])
187 data = [data dataByDecodingBase64];
188 else if ([enc isEqualToString:@"7BIT"])
189 ; /* keep data as is */ // TODO: do we need to change encodings?
191 [self errorWithFormat:@"unsupported encoding: %@", enc];
199 - (NSString *)contentTypeForBodyPartInfo:(id)_info {
200 NSMutableString *type;
202 NSDictionary *parameters;
206 if (![_info isNotNull])
209 mt = [_info valueForKey:@"type"]; if (![mt isNotNull]) return nil;
210 st = [_info valueForKey:@"subtype"]; if (![st isNotNull]) return nil;
212 type = [NSMutableString stringWithCapacity:16];
213 [type appendString:[mt lowercaseString]];
214 [type appendString:@"/"];
215 [type appendString:[st lowercaseString]];
217 parameters = [_info valueForKey:@"parameterList"];
218 ke = [parameters keyEnumerator];
219 while ((pn = [ke nextObject]) != nil) {
220 [type appendString:@"; "];
221 [type appendString:pn];
222 [type appendString:@"=\""];
223 [type appendString:[[parameters objectForKey:pn] stringValue]];
224 [type appendString:@"\""];
229 - (NSString *) contentTypeForPathExtension: (NSString *) pe
231 if ([pe length] == 0)
232 return @"application/octet-stream";
234 /* TODO: add some map */
235 if ([pe isEqualToString:@"gif"]) return @"image/gif";
236 if ([pe isEqualToString:@"png"]) return @"image/png";
237 if ([pe isEqualToString:@"jpg"]) return @"image/jpeg";
238 if ([pe isEqualToString:@"txt"]) return @"text/plain";
240 return @"application/octet-stream";
243 - (NSString *) davContentType
245 // TODO: what about the content-type and other headers?
246 // => we could pass them in as the extension? (eg generate 1.gif!)
247 NSString *parts, *contentType, *extension;
249 /* try type from body structure info */
251 parts = [self contentTypeForBodyPartInfo: [self partInfo]];
252 contentType = [[parts componentsSeparatedByString: @";"] objectAtIndex: 0];
254 if (![contentType length])
256 extension = [[self nameInContainer] pathExtension];
257 contentType = [self contentTypeForPathExtension: extension];
265 - (id)GETAction:(id)_ctx {
269 NSString *etag, *mimeType;
271 if ((error = [self matchesRequestConditionInContext:_ctx]) != nil) {
272 // TODO: currently we fetch the body structure to get here - check this!
273 /* check whether the mail still exists */
274 if (![[self mailObject] doesMailExist]) {
275 return [NSException exceptionWithHTTPStatus:404 /* Not Found */
276 reason:@"mail was deleted"];
278 return error; /* return 304 or 416 */
281 [self debugWithFormat:@"should fetch body part: %@",
282 [self bodyPartIdentifier]];
284 if ((data = [self fetchBLOB]) == nil) {
285 return [NSException exceptionWithHTTPStatus:404 /* not found */
286 reason:@"did not find body part"];
289 [self debugWithFormat:@" fetched %d bytes: %@", [data length],
292 // TODO: wrong, could be encoded
293 r = [(WOContext *)_ctx response];
294 mimeType = [self davContentType];
295 if ([mimeType isEqualToString: @"application/x-xpinstall"])
296 mimeType = @"application/octet-stream";
298 [r setHeader: mimeType forKey:@"content-type"];
299 [r setHeader: [NSString stringWithFormat:@"%d", [data length]]
300 forKey: @"content-length"];
302 if ((etag = [self davEntityTag]) != nil)
303 [r setHeader:etag forKey:@"etag"];
312 + (Class) bodyPartClassForKey: (NSString *) _key
317 pe = [_key pathExtension];
318 if (![pe isNotNull] || [pe length] == 0)
321 /* hard coded for now */
323 switch ([pe length]) {
325 if ([pe isEqualToString:@"gif"] ||
326 [pe isEqualToString:@"png"] ||
327 [pe isEqualToString:@"jpg"])
328 return NSClassFromString(@"SOGoImageMailBodyPart");
329 if ([pe isEqualToString:@"ics"])
330 return NSClassFromString(@"SOGoCalendarMailBodyPart");
331 if ([pe isEqualToString:@"vcf"])
332 return NSClassFromString(@"SOGoVCardMailBodyPart");
335 if ([pe isEqualToString:@"mail"])
336 return NSClassFromString(@"SOGoMessageMailBodyPart");
344 + (Class) bodyPartClassForMimeType: (NSString *) mimeType
347 NSString *classString;
350 if ([mimeType isEqualToString: @"image/gif"]
351 || [mimeType isEqualToString: @"image/png"]
352 || [mimeType isEqualToString: @"image/jpg"]
353 || [mimeType isEqualToString: @"image/jpeg"])
354 classString = @"SOGoImageMailBodyPart";
355 else if ([mimeType isEqualToString: @"text/calendar"])
356 classString = @"SOGoCalendarMailBodyPart";
357 else if ([mimeType isEqualToString: @"text/x-vcard"])
358 classString = @"SOGoVCardMailBodyPart";
359 else if ([mimeType isEqualToString: @"message/rfc822"])
360 classString = @"SOGoMessageMailBodyPart";
363 NSLog (@"unhandled mime type: '%@'", mimeType);
368 klazz = NSClassFromString (classString);
370 klazz = [SOGoMailBodyPart class];
383 - (BOOL)isDebuggingEnabled {
387 @end /* SOGoMailBodyPart */