2 Copyright (C) 2000-2004 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
23 #ifndef __NGImap4Message_BodyStructure_H__
24 #define __NGImap4Message_BodyStructure_H__
27 @interface NSCalendarDate(RFC822Dates)
28 + (NSCalendarDate *)calendarDateWithRfc822DateString:(NSString *)_str;
29 @end /* NSString(RFC822Dates) */
31 @interface NGImap4Message(BodyStructure)
33 static id _buildMultipartMessage(id self, NSURL *_baseUrl, NSDictionary *_dict,
35 static id _buildMessage(id self, NSURL *_baseUrl, NSDictionary *_dict);
36 static id _buildPart(id self, NSURL *_baseUrl, NSDictionary *_dict);
37 static id _buildMimeBodyPart(id self, NSDictionary *_dict);
38 static id _buildMimeMessageBody(id self, NSURL *_baseUrl, NSDictionary *_dict,
40 @end /* NGImap4Message(BodyStructure) */
42 @implementation NGImap4Message(BodyStructure)
44 static id _buildMultipartMessage(id self, NSURL *_baseUrl, NSDictionary *_dict,
45 id<NGMimePart>_part) {
46 NGMimeMultipartBody *body;
47 NSEnumerator *enumerator;
51 body = [[NGMimeMultipartBody alloc] initWithPart:_part];
52 enumerator = [[_dict objectForKey:@"parts"] objectEnumerator];
55 while ((part = [enumerator nextObject])) {
61 baseStr = [_baseUrl absoluteString];
63 if ([baseStr hasSuffix:@"="])
64 url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%d",
67 url = [NSURL URLWithString:[NSString stringWithFormat:@"%@.%d",
70 [body addBodyPart:_buildPart(self, url, part)];
73 return [body autorelease];
76 static id _buildMessage(id self, NSURL *_baseUrl, NSDictionary *_dict) {
77 NGMutableHashMap *map;
78 NGMimeMessage *message;
81 static NGMimeHeaderNames *Fields = NULL;
84 Fields = (NGMimeHeaderNames *)[NGMimePartParser headerFieldNames];
87 map = [NGMutableHashMap hashMapWithCapacity:4];
89 if ((value = [_dict objectForKey:@"subject"])) {
90 [map setObject:value forKey:Fields->subject];
92 if ((value = [_dict objectForKey:@"messageId"])) {
93 [map setObject:value forKey:Fields->messageID];
95 if ((value = [_dict objectForKey:@"in-reply-to"])) {
96 [map setObject:value forKey:@"in-reply-to"];
98 if ((value = [_dict objectForKey:Fields->contentLength])) {
99 [map setObject:value forKey:Fields->contentLength];
101 if ((value = [_dict objectForKey:@"date"])) {
104 if ((d = [NSCalendarDate calendarDateWithRfc822DateString:value]))
105 [map setObject:d forKey:Fields->date];
108 NSEnumerator *enumerator;
110 static NSArray *Array = nil;
113 Array = [[NSArray alloc] initWithObjects:
121 enumerator = [Array objectEnumerator];
122 while ((obj = [enumerator nextObject])) {
125 if ((addrs = [_dict objectForKey:obj])) {
126 NSEnumerator *addrEnum;
128 NSMutableString *str;
130 addrEnum = [addrs objectEnumerator];
132 while ((addr = [addrEnum nextObject])) {
133 NSString *personalName, *mailboxName, *hostName;
135 str = [NSMutableString stringWithCapacity:32];
138 [str appendString:@", "];
140 personalName = [addr objectForKey:@"personalName"];
141 mailboxName = [addr objectForKey:@"mailboxName"];
142 hostName = [addr objectForKey:@"hostName"];
144 if ([personalName length]) {
145 [str appendFormat:@"\"%@\" <%@@%@>", personalName,
146 mailboxName, hostName];
149 [str appendFormat:@"%@@%@", mailboxName, hostName];
153 [map setObject:str forKey:obj];
160 part = _buildPart(self, _baseUrl, [_dict objectForKey:@"body"]);
162 [map setObject:[part contentType] forKey:Fields->contentType];
164 if (![[map valueForKey:Fields->contentTransferEncoding] length]) {
165 if ((tmp = [[part valuesOfHeaderFieldWithName:
166 Fields->contentTransferEncoding] nextObject])) {
167 [map setObject:tmp forKey:Fields->contentTransferEncoding];
170 if (![[map valueForKey:Fields->contentLength] intValue]) {
171 if ((tmp = [[part valuesOfHeaderFieldWithName:Fields->contentLength]
173 [map setObject:tmp forKey:Fields->contentLength];
176 message = [NGMimeMessage messageWithHeader:map];
178 if ([(body = [part body]) isKindOfClass:[NSURL class]]) {
181 baseStr = [body absoluteString];
183 if ([baseStr hasPrefix:@"="]) {
184 body = [NSURL URLWithString:[NSString stringWithFormat:@"%@1",
188 body = [NSURL URLWithString:[NSString stringWithFormat:@"%@.1",
191 [message setBody:body];
196 static id _buildPart(id self, NSURL *_baseUrl, NSDictionary *_dict) {
197 NSString *type, *subType;
198 NGMimeBodyPart *part;
202 type = [[_dict objectForKey:@"type"] lowercaseString];
203 subType = [[_dict objectForKey:@"subtype"] lowercaseString];
205 part = _buildMimeBodyPart(self, _dict);
207 if ([type isEqualToString:@"multipart"]) {
208 [part setBody:_buildMultipartMessage(self, url, _dict, part)];
210 else if ([type isEqualToString:@"message"] &&
211 [subType isEqualToString:@"rfc822"]) {
212 [part setBody:_buildMessage(self, url, _dict)];
220 static id _buildMimeBodyPart(id self, NSDictionary *_dict)
222 NGMutableHashMap *dict;
225 static NGMimeHeaderNames *Fields = NULL;
228 Fields = (NGMimeHeaderNames *)[NGMimePartParser headerFieldNames];
231 dict = [NGMutableHashMap hashMapWithCapacity:8];
233 type = [NGMimeType mimeType:[[_dict objectForKey:@"type"] lowercaseString]
234 subType:[[_dict objectForKey:@"subtype"] lowercaseString]
235 parameters:[_dict objectForKey:@"parameterList"]];
237 [dict setObject:type forKey:Fields->contentType];
239 if (([value = [_dict objectForKey:@"bodyId"] length])) {
240 [dict setObject:value forKey:Fields->messageID];
243 if (([value = [_dict objectForKey:@"size"] length])) {
244 [dict setObject:value forKey:Fields->contentLength];
246 if (([value = [_dict objectForKey:@"encoding"] length])) {
247 [dict setObject:[value lowercaseString]
248 forKey:Fields->contentTransferEncoding];
250 return [NGMimeBodyPart bodyPartWithHeader:dict];
254 static id _buildMimeMessageBody(id self, NSURL *_baseUrl, NSDictionary *_dict,
257 NSString *type, *subType;
261 type = [[_dict objectForKey:@"type"] lowercaseString];
262 subType = [[_dict objectForKey:@"subtype"] lowercaseString];
264 url = [NSURL URLWithString:[[_baseUrl absoluteString]
265 stringByAppendingString:@"?part="]];
266 if ([type isEqualToString:@"multipart"]) {
267 result = _buildMultipartMessage(self, url, _dict, _part);
270 if ([type isEqualToString:@"message"] &&
271 [subType isEqualToString:@"rfc822"]) {
272 result = _buildMessage(self, url, _dict);
275 result = [NSURL URLWithString:[[_baseUrl absoluteString]
276 stringByAppendingString:@"?part=1"]];
282 @end /* NGImap4Message(BodyStructure) */
284 #endif /* __NGImap4Message_BodyStructure_H__ */