2 Copyright (C) 2000-2005 SKYRIX Software AG
4 This file is part of SOPE.
6 SOPE 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 SOPE 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 SOPE; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 #ifndef __NGImap4Message_BodyStructure_H__
23 #define __NGImap4Message_BodyStructure_H__
25 @interface NSCalendarDate(RFC822Dates)
26 + (NSCalendarDate *)calendarDateWithRfc822DateString:(NSString *)_str;
27 @end /* NSString(RFC822Dates) */
29 @interface NGImap4Message(BodyStructure)
31 static id _buildMultipartMessage(id self, NSURL *_baseUrl, NSDictionary *_dict,
33 static id _buildMessage(id self, NSURL *_baseUrl, NSDictionary *_dict);
34 static id _buildPart(id self, NSURL *_baseUrl, NSDictionary *_dict);
35 static id _buildMimeBodyPart(id self, NSDictionary *_dict);
36 static id _buildMimeMessageBody(id self, NSURL *_baseUrl, NSDictionary *_dict,
38 @end /* NGImap4Message(BodyStructure) */
40 @implementation NGImap4Message(BodyStructure)
42 static id _buildMultipartMessage(id self, NSURL *_baseUrl, NSDictionary *_dict,
43 id<NGMimePart>_part) {
44 NGMimeMultipartBody *body;
45 NSEnumerator *enumerator;
49 body = [[NGMimeMultipartBody alloc] initWithPart:_part];
50 enumerator = [[_dict objectForKey:@"parts"] objectEnumerator];
53 while ((part = [enumerator nextObject])) {
59 baseStr = [_baseUrl absoluteString];
61 if ([baseStr hasSuffix:@"="])
62 url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%d",
65 url = [NSURL URLWithString:[NSString stringWithFormat:@"%@.%d",
68 [body addBodyPart:_buildPart(self, url, part)];
71 return [body autorelease];
74 static id _buildMessage(id self, NSURL *_baseUrl, NSDictionary *_dict) {
75 NGMutableHashMap *map;
76 NGMimeMessage *message;
79 static NGMimeHeaderNames *Fields = NULL;
82 Fields = (NGMimeHeaderNames *)[NGMimePartParser headerFieldNames];
85 map = [NGMutableHashMap hashMapWithCapacity:4];
87 if ([(value = [_dict objectForKey:@"subject"]) isNotNull])
88 [map setObject:value forKey:Fields->subject];
90 if ([(value = [_dict objectForKey:@"messageId"]) isNotNull])
91 [map setObject:value forKey:Fields->messageID];
93 if ([(value = [_dict objectForKey:@"in-reply-to"]) isNotNull])
94 [map setObject:value forKey:@"in-reply-to"];
96 if ([(value = [_dict objectForKey:Fields->contentLength]) isNotNull])
97 [map setObject:value forKey:Fields->contentLength];
99 if ([(value = [_dict objectForKey:@"date"]) isNotNull]) {
102 if ((d = [NSCalendarDate calendarDateWithRfc822DateString:value]))
103 [map setObject:d forKey:Fields->date];
106 NSEnumerator *enumerator;
108 static NSArray *Array = nil;
111 Array = [[NSArray alloc] initWithObjects:
119 enumerator = [Array objectEnumerator];
120 while ((obj = [enumerator nextObject])) {
123 if ((addrs = [_dict objectForKey:obj])) {
124 NSEnumerator *addrEnum;
126 NSMutableString *str;
128 addrEnum = [addrs objectEnumerator];
130 while ((addr = [addrEnum nextObject])) {
131 NSString *personalName, *mailboxName, *hostName;
133 str = [NSMutableString stringWithCapacity:32];
136 [str appendString:@", "];
138 personalName = [addr objectForKey:@"personalName"];
139 mailboxName = [addr objectForKey:@"mailboxName"];
140 hostName = [addr objectForKey:@"hostName"];
142 if ([personalName length]) {
143 [str appendFormat:@"\"%@\" <%@@%@>", personalName,
144 mailboxName, hostName];
147 [str appendFormat:@"%@@%@", mailboxName, hostName];
151 [map setObject:str forKey:obj];
158 part = _buildPart(self, _baseUrl, [_dict objectForKey:@"body"]);
160 [map setObject:[part contentType] forKey:Fields->contentType];
162 if (![[map valueForKey:Fields->contentTransferEncoding] length]) {
163 if ((tmp = [[part valuesOfHeaderFieldWithName:
164 Fields->contentTransferEncoding] nextObject])) {
165 [map setObject:tmp forKey:Fields->contentTransferEncoding];
168 if (![[map valueForKey:Fields->contentLength] intValue]) {
169 if ((tmp = [[part valuesOfHeaderFieldWithName:Fields->contentLength]
171 [map setObject:tmp forKey:Fields->contentLength];
174 message = [NGMimeMessage messageWithHeader:map];
176 if ([(body = [part body]) isKindOfClass:[NSURL class]]) {
179 baseStr = [body absoluteString];
181 if ([baseStr hasPrefix:@"="]) {
182 body = [NSURL URLWithString:[NSString stringWithFormat:@"%@1",
186 body = [NSURL URLWithString:[NSString stringWithFormat:@"%@.1",
189 [message setBody:body];
194 static id _buildPart(id self, NSURL *_baseUrl, NSDictionary *_dict) {
195 NSString *type, *subType;
196 NGMimeBodyPart *part;
200 type = [[_dict objectForKey:@"type"] lowercaseString];
201 subType = [[_dict objectForKey:@"subtype"] lowercaseString];
203 part = _buildMimeBodyPart(self, _dict);
205 if ([type isEqualToString:@"multipart"]) {
206 [part setBody:_buildMultipartMessage(self, url, _dict, part)];
208 else if ([type isEqualToString:@"message"] &&
209 [subType isEqualToString:@"rfc822"]) {
210 [part setBody:_buildMessage(self, url, _dict)];
218 static id _buildMimeBodyPart(id self, NSDictionary *_dict)
220 NGMutableHashMap *dict;
223 static NGMimeHeaderNames *Fields = NULL;
226 Fields = (NGMimeHeaderNames *)[NGMimePartParser headerFieldNames];
229 dict = [NGMutableHashMap hashMapWithCapacity:8];
231 type = [NGMimeType mimeType:[[_dict objectForKey:@"type"] lowercaseString]
232 subType:[[_dict objectForKey:@"subtype"] lowercaseString]
233 parameters:[_dict objectForKey:@"parameterList"]];
235 [dict setObject:type forKey:Fields->contentType];
237 if (([value = [_dict objectForKey:@"bodyId"] length])) {
238 [dict setObject:value forKey:Fields->messageID];
241 if (([value = [_dict objectForKey:@"size"] length])) {
242 [dict setObject:value forKey:Fields->contentLength];
244 if (([value = [_dict objectForKey:@"encoding"] length])) {
245 [dict setObject:[value lowercaseString]
246 forKey:Fields->contentTransferEncoding];
248 return [NGMimeBodyPart bodyPartWithHeader:dict];
252 static id _buildMimeMessageBody(id self, NSURL *_baseUrl, NSDictionary *_dict,
255 NSString *type, *subType;
259 type = [[_dict objectForKey:@"type"] lowercaseString];
260 subType = [[_dict objectForKey:@"subtype"] lowercaseString];
262 url = [NSURL URLWithString:[[_baseUrl absoluteString]
263 stringByAppendingString:@"?part="]];
264 if ([type isEqualToString:@"multipart"]) {
265 result = _buildMultipartMessage(self, url, _dict, _part);
268 if ([type isEqualToString:@"message"] &&
269 [subType isEqualToString:@"rfc822"]) {
270 result = _buildMessage(self, url, _dict);
273 result = [NSURL URLWithString:[[_baseUrl absoluteString]
274 stringByAppendingString:@"?part=1"]];
280 @end /* NGImap4Message(BodyStructure) */
282 #endif /* __NGImap4Message_BodyStructure_H__ */