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 */
182 enc = [[self partInfo] valueForKey: @"encoding"];
184 /* if we haven't found one, check out the main message's encoding
185 as we could be trying to fetch the message's content as a part */
187 enc = [[[[self mailObject] fetchCoreInfos] valueForKey: @"body"]
188 valueForKey: @"encoding"];
192 enc = [enc uppercaseString];
194 if ([enc isEqualToString:@"BASE64"])
195 data = [data dataByDecodingBase64];
196 else if ([enc isEqualToString:@"7BIT"])
197 ; /* keep data as is */ // TODO: do we need to change encodings?
199 [self errorWithFormat:@"unsupported encoding: %@", enc];
207 - (NSString *)contentTypeForBodyPartInfo:(id)_info {
208 NSMutableString *type;
210 NSDictionary *parameters;
214 if (![_info isNotNull])
217 mt = [_info valueForKey:@"type"]; if (![mt isNotNull]) return nil;
218 st = [_info valueForKey:@"subtype"]; if (![st isNotNull]) return nil;
220 type = [NSMutableString stringWithCapacity:16];
221 [type appendString:[mt lowercaseString]];
222 [type appendString:@"/"];
223 [type appendString:[st lowercaseString]];
225 parameters = [_info valueForKey:@"parameterList"];
226 ke = [parameters keyEnumerator];
227 while ((pn = [ke nextObject]) != nil) {
228 [type appendString:@"; "];
229 [type appendString:pn];
230 [type appendString:@"=\""];
231 [type appendString:[[parameters objectForKey:pn] stringValue]];
232 [type appendString:@"\""];
237 - (NSString *) contentTypeForPathExtension: (NSString *) pe
239 if ([pe length] == 0)
240 return @"application/octet-stream";
242 /* TODO: add some map */
243 if ([pe isEqualToString:@"gif"]) return @"image/gif";
244 if ([pe isEqualToString:@"png"]) return @"image/png";
245 if ([pe isEqualToString:@"jpg"]) return @"image/jpeg";
246 if ([pe isEqualToString:@"txt"]) return @"text/plain";
248 return @"application/octet-stream";
251 - (NSString *) davContentType
253 // TODO: what about the content-type and other headers?
254 // => we could pass them in as the extension? (eg generate 1.gif!)
255 NSString *parts, *contentType, *extension;
257 /* try type from body structure info */
259 parts = [self contentTypeForBodyPartInfo: [self partInfo]];
260 contentType = [[parts componentsSeparatedByString: @";"] objectAtIndex: 0];
262 if (![contentType length])
264 extension = [[self nameInContainer] pathExtension];
265 contentType = [self contentTypeForPathExtension: extension];
273 - (id)GETAction:(id)_ctx {
277 NSString *etag, *mimeType;
279 if ((error = [self matchesRequestConditionInContext:_ctx]) != nil) {
280 // TODO: currently we fetch the body structure to get here - check this!
281 /* check whether the mail still exists */
282 if (![[self mailObject] doesMailExist]) {
283 return [NSException exceptionWithHTTPStatus:404 /* Not Found */
284 reason:@"mail was deleted"];
286 return error; /* return 304 or 416 */
289 [self debugWithFormat:@"should fetch body part: %@",
290 [self bodyPartIdentifier]];
292 if ((data = [self fetchBLOB]) == nil) {
293 return [NSException exceptionWithHTTPStatus:404 /* not found */
294 reason:@"did not find body part"];
297 [self debugWithFormat:@" fetched %d bytes: %@", [data length],
300 // TODO: wrong, could be encoded
301 r = [(WOContext *)_ctx response];
302 mimeType = [self davContentType];
303 if ([mimeType isEqualToString: @"application/x-xpinstall"])
304 mimeType = @"application/octet-stream";
306 [r setHeader: mimeType forKey:@"content-type"];
307 [r setHeader: [NSString stringWithFormat:@"%d", [data length]]
308 forKey: @"content-length"];
310 if ((etag = [self davEntityTag]) != nil)
311 [r setHeader:etag forKey:@"etag"];
320 + (Class) bodyPartClassForKey: (NSString *) _key
325 pe = [_key pathExtension];
326 if (![pe isNotNull] || [pe length] == 0)
329 /* hard coded for now */
331 switch ([pe length]) {
333 if ([pe isEqualToString:@"gif"] ||
334 [pe isEqualToString:@"png"] ||
335 [pe isEqualToString:@"jpg"])
336 return NSClassFromString(@"SOGoImageMailBodyPart");
337 if ([pe isEqualToString:@"ics"])
338 return NSClassFromString(@"SOGoCalendarMailBodyPart");
339 if ([pe isEqualToString:@"vcf"])
340 return NSClassFromString(@"SOGoVCardMailBodyPart");
343 if ([pe isEqualToString:@"mail"])
344 return NSClassFromString(@"SOGoMessageMailBodyPart");
352 + (Class) bodyPartClassForMimeType: (NSString *) mimeType
355 NSString *classString;
358 if ([mimeType isEqualToString: @"image/gif"]
359 || [mimeType isEqualToString: @"image/png"]
360 || [mimeType isEqualToString: @"image/jpg"]
361 || [mimeType isEqualToString: @"image/jpeg"])
362 classString = @"SOGoImageMailBodyPart";
363 else if ([mimeType isEqualToString: @"text/calendar"])
364 classString = @"SOGoCalendarMailBodyPart";
365 else if ([mimeType isEqualToString: @"text/x-vcard"])
366 classString = @"SOGoVCardMailBodyPart";
367 else if ([mimeType isEqualToString: @"message/rfc822"])
368 classString = @"SOGoMessageMailBodyPart";
371 classString = @"SOGoMailBodyPart";
372 // NSLog (@"unhandled mime type: '%@'", mimeType);
375 klazz = NSClassFromString (classString);
388 - (BOOL)isDebuggingEnabled {
392 @end /* SOGoMailBodyPart */