| 1 |
/* |
| 2 |
** CWPart.m |
| 3 |
** |
| 4 |
** Copyright (c) 2001-2007 Ludovic Marcotte |
| 5 |
** Copyright (C) 2017-2018 Riccardo Mottola |
| 6 |
** |
| 7 |
** Author: Ludovic Marcotte <[email protected]> |
| 8 |
** Riccardo Mottola <[email protected]> |
| 9 |
** |
| 10 |
** This library is free software; you can redistribute it and/or |
| 11 |
** modify it under the terms of the GNU Lesser General Public |
| 12 |
** License as published by the Free Software Foundation; either |
| 13 |
** version 2.1 of the License, or (at your option) any later version. |
| 14 |
** |
| 15 |
** This library is distributed in the hope that it will be useful, |
| 16 |
** but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 |
** Lesser General Public License for more details. |
| 19 |
** |
| 20 |
** You should have received a copy of the GNU General Public License |
| 21 |
** along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 22 |
*/ |
| 23 |
|
| 24 |
#import <Pantomime/CWPart.h> |
| 25 |
|
| 26 |
#import <Pantomime/CWConstants.h> |
| 27 |
#import <Pantomime/CWMessage.h> |
| 28 |
#import <Pantomime/CWMIMEMultipart.h> |
| 29 |
#import <Pantomime/CWMIMEUtility.h> |
| 30 |
#import <Pantomime/NSData+Extensions.h> |
| 31 |
#import <Pantomime/NSString+Extensions.h> |
| 32 |
#import <Pantomime/CWParser.h> |
| 33 |
|
| 34 |
#import <Foundation/NSAutoreleasePool.h> |
| 35 |
#import <Foundation/NSException.h> |
| 36 |
#import <Foundation/NSValue.h> |
| 37 |
|
| 38 |
#include <string.h> |
| 39 |
|
| 40 |
#define LF "\n" |
| 41 |
|
| 42 |
static int currentPartVersion = 2; |
| 43 |
|
| 44 |
// |
| 45 |
// |
| 46 |
// |
| 47 |
@implementation CWPart |
| 48 |
|
| 49 |
+ (void) initialize |
| 50 |
{ |
| 51 |
[CWPart setVersion: currentPartVersion]; |
| 52 |
} |
| 53 |
|
| 54 |
- (id) init |
| 55 |
{ |
| 56 |
self = [super init]; |
| 57 |
if (self) |
| 58 |
{ |
| 59 |
// We initialize our dictionary that will hold all our headers with a capacity of 25. |
| 60 |
// This is an empirical number that is used to speedup the addition of headers w/o |
| 61 |
// reallocating our array everytime we add a new element. |
| 62 |
_headers = [[NSMutableDictionary alloc] initWithCapacity: 25]; |
| 63 |
_parameters = [[NSMutableDictionary alloc] init]; |
| 64 |
_line_length = 0; |
| 65 |
_size = 0; |
| 66 |
_content = nil; |
| 67 |
} |
| 68 |
|
| 69 |
return self; |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
// |
| 74 |
// |
| 75 |
// |
| 76 |
- (void) dealloc |
| 77 |
{ |
| 78 |
RELEASE(_defaultCharset); |
| 79 |
RELEASE(_parameters); |
| 80 |
RELEASE(_headers); |
| 81 |
RELEASE(_content); |
| 82 |
|
| 83 |
[super dealloc]; |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
// |
| 88 |
// |
| 89 |
// |
| 90 |
- (id) initWithData: (NSData *) theData |
| 91 |
{ |
| 92 |
NSRange aRange; |
| 93 |
|
| 94 |
aRange = [theData rangeOfCString: "\n\n"]; |
| 95 |
|
| 96 |
if (aRange.length == 0) |
| 97 |
{ |
| 98 |
AUTORELEASE(self); |
| 99 |
return nil; |
| 100 |
} |
| 101 |
|
| 102 |
// We initialize our message with the headers and the content |
| 103 |
self = [self init]; |
| 104 |
if (self) |
| 105 |
{ |
| 106 |
// We verify if we have an empty body part content like: |
| 107 |
// X-UID: 5dc5aa4b82240000 |
| 108 |
// |
| 109 |
// This is a MIME Message |
| 110 |
// |
| 111 |
// ------=_NextPart_000_007F_01BDF6C7.FABAC1B0 |
| 112 |
// |
| 113 |
// |
| 114 |
// ------=_NextPart_000_007F_01BDF6C7.FABAC1B0 |
| 115 |
// Content-Type: text/html; name="7english.co.kr.htm" |
| 116 |
if ([theData length] == 2) |
| 117 |
{ |
| 118 |
[self setContent: [NSData data]]; |
| 119 |
} |
| 120 |
else |
| 121 |
{ |
| 122 |
[self setHeadersFromData: |
| 123 |
[theData subdataWithRange: NSMakeRange(0,aRange.location)]]; |
| 124 |
[CWMIMEUtility setContentFromRawSource: |
| 125 |
[theData subdataWithRange: |
| 126 |
NSMakeRange(aRange.location + 2, |
| 127 |
[theData length]-(aRange.location+2))] |
| 128 |
inPart: self]; |
| 129 |
} |
| 130 |
} |
| 131 |
return self; |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
// |
| 136 |
// |
| 137 |
// |
| 138 |
- (id) initWithData: (NSData *) theData |
| 139 |
charset: (NSString *) theCharset |
| 140 |
{ |
| 141 |
self = [self initWithData: theData]; |
| 142 |
if (self) |
| 143 |
{ |
| 144 |
[self setDefaultCharset: theCharset]; |
| 145 |
} |
| 146 |
return self; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
// |
| 151 |
// NSCoding protocol |
| 152 |
// |
| 153 |
- (void) encodeWithCoder: (NSCoder *) theCoder |
| 154 |
{ |
| 155 |
[theCoder encodeObject: [self contentType]]; |
| 156 |
[theCoder encodeObject: [self contentID]]; |
| 157 |
[theCoder encodeObject: [self contentDescription]]; |
| 158 |
[theCoder encodeObject: [NSNumber numberWithUnsignedInt: (unsigned)[self contentDisposition]]]; |
| 159 |
[theCoder encodeObject: [self filename]]; |
| 160 |
|
| 161 |
[theCoder encodeObject: [NSNumber numberWithUnsignedInt: (unsigned)[self contentTransferEncoding]]]; |
| 162 |
[theCoder encodeObject: [NSNumber numberWithUnsignedInt: (unsigned)[self format]]]; |
| 163 |
[theCoder encodeObject: [NSNumber numberWithUnsignedLong: _size]]; |
| 164 |
|
| 165 |
[theCoder encodeObject: [self boundary]]; |
| 166 |
[theCoder encodeObject: [self charset]]; |
| 167 |
[theCoder encodeObject: _defaultCharset]; |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
- (id) initWithCoder: (NSCoder *) theCoder |
| 172 |
{ |
| 173 |
self = [super init]; |
| 174 |
if (self) |
| 175 |
{ |
| 176 |
_headers = [[NSMutableDictionary alloc] initWithCapacity: 25]; |
| 177 |
_parameters = [[NSMutableDictionary alloc] init]; |
| 178 |
|
| 179 |
[self setContentType: [theCoder decodeObject]]; |
| 180 |
[self setContentID: [theCoder decodeObject]]; |
| 181 |
[self setContentDescription: [theCoder decodeObject]]; |
| 182 |
[self setContentDisposition: (PantomimeContentDisposition)[[theCoder decodeObject] unsignedIntValue]]; |
| 183 |
[self setFilename: [theCoder decodeObject]]; |
| 184 |
|
| 185 |
[self setContentTransferEncoding: (PantomimeEncoding)[[theCoder decodeObject] unsignedIntValue]]; |
| 186 |
[self setFormat: (PantomimeMessageFormat)[[theCoder decodeObject] unsignedIntValue]]; |
| 187 |
[self setSize: [[theCoder decodeObject] unsignedLongValue]]; |
| 188 |
|
| 189 |
[self setBoundary: [theCoder decodeObject]]; |
| 190 |
[self setCharset: [theCoder decodeObject]]; |
| 191 |
[self setDefaultCharset: [theCoder decodeObject]]; |
| 192 |
|
| 193 |
_content = nil; |
| 194 |
} |
| 195 |
return self; |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
// |
| 200 |
// access / mutation methods |
| 201 |
// |
| 202 |
- (NSObject *) content |
| 203 |
{ |
| 204 |
return _content; |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
// |
| 209 |
// |
| 210 |
// |
| 211 |
- (void) setContent: (NSObject *) theContent |
| 212 |
{ |
| 213 |
if (theContent && !([theContent isKindOfClass: [NSData class]] || |
| 214 |
[theContent isKindOfClass: [CWMessage class]] || |
| 215 |
[theContent isKindOfClass: [CWMIMEMultipart class]])) |
| 216 |
{ |
| 217 |
[NSException raise: NSInvalidArgumentException |
| 218 |
format: @"Invalid argument to CWPart: -setContent: The content MUST be either a NSData, CWMessage or CWMIMEMessage instance."]; |
| 219 |
} |
| 220 |
|
| 221 |
ASSIGN(_content, theContent); |
| 222 |
} |
| 223 |
|
| 224 |
// |
| 225 |
// |
| 226 |
// |
| 227 |
- (NSString *) contentType |
| 228 |
{ |
| 229 |
return [_headers objectForKey: @"Content-Type"]; |
| 230 |
} |
| 231 |
|
| 232 |
- (void) setContentType: (NSString*) theContentType |
| 233 |
{ |
| 234 |
if (theContentType) |
| 235 |
{ |
| 236 |
[_headers setObject: theContentType forKey: @"Content-Type"]; |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
// |
| 241 |
// |
| 242 |
// |
| 243 |
- (NSString *) contentID |
| 244 |
{ |
| 245 |
return [_headers objectForKey: @"Content-Id"]; |
| 246 |
} |
| 247 |
|
| 248 |
- (void) setContentID: (NSString *) theContentID |
| 249 |
{ |
| 250 |
if (theContentID) |
| 251 |
{ |
| 252 |
[_headers setObject: theContentID forKey: @"Content-Id"]; |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
// |
| 257 |
// |
| 258 |
// |
| 259 |
- (NSString *) contentDescription |
| 260 |
{ |
| 261 |
return [_headers objectForKey: @"Content-Description"]; |
| 262 |
} |
| 263 |
|
| 264 |
- (void) setContentDescription: (NSString *) theContentDescription |
| 265 |
{ |
| 266 |
if (theContentDescription) |
| 267 |
{ |
| 268 |
[_headers setObject: theContentDescription forKey: @"Content-Description"]; |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
// |
| 274 |
// |
| 275 |
// |
| 276 |
- (PantomimeContentDisposition) contentDisposition |
| 277 |
{ |
| 278 |
id o; |
| 279 |
|
| 280 |
o = [_headers objectForKey: @"Content-Disposition"]; |
| 281 |
|
| 282 |
return (o ? (PantomimeContentDisposition)[o intValue] : PantomimeInlineDisposition); |
| 283 |
} |
| 284 |
|
| 285 |
- (void) setContentDisposition: (PantomimeContentDisposition) theContentDisposition |
| 286 |
{ |
| 287 |
[_headers setObject: [NSNumber numberWithUnsignedInt: (unsigned)theContentDisposition] forKey: @"Content-Disposition"]; |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
// |
| 292 |
// |
| 293 |
// |
| 294 |
- (PantomimeEncoding) contentTransferEncoding |
| 295 |
{ |
| 296 |
id o; |
| 297 |
|
| 298 |
o = [_headers objectForKey: @"Content-Transfer-Encoding"]; |
| 299 |
|
| 300 |
if (o) |
| 301 |
{ |
| 302 |
return [o unsignedIntValue]; |
| 303 |
} |
| 304 |
|
| 305 |
// Default value for the Content-Transfer-Encoding. |
| 306 |
// See RFC2045 - 6.1. Content-Transfer-Encoding Syntax. |
| 307 |
return PantomimeEncodingNone; |
| 308 |
} |
| 309 |
|
| 310 |
- (void) setContentTransferEncoding: (PantomimeEncoding) theEncoding |
| 311 |
{ |
| 312 |
[_headers setObject: [NSNumber numberWithUnsignedInt: (unsigned)theEncoding] forKey: @"Content-Transfer-Encoding"]; |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
// |
| 317 |
// |
| 318 |
// |
| 319 |
- (NSString *) filename |
| 320 |
{ |
| 321 |
return [_parameters objectForKey: @"filename"]; |
| 322 |
//return _filename; |
| 323 |
} |
| 324 |
|
| 325 |
- (void) setFilename: (NSString *) theFilename |
| 326 |
{ |
| 327 |
if (theFilename && ([theFilename length] > 0)) |
| 328 |
{ |
| 329 |
//ASSIGN(_filename, theFilename); |
| 330 |
[_parameters setObject: theFilename forKey: @"filename"]; |
| 331 |
} |
| 332 |
else |
| 333 |
{ |
| 334 |
//ASSIGN(_filename, @"unknown"); |
| 335 |
[_parameters setObject: @"unknown" forKey: @"filename"]; |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
// |
| 341 |
// |
| 342 |
// |
| 343 |
- (PantomimeMessageFormat) format |
| 344 |
{ |
| 345 |
id o; |
| 346 |
|
| 347 |
o = [_parameters objectForKey: @"format"]; |
| 348 |
|
| 349 |
if (o) |
| 350 |
{ |
| 351 |
return [o unsignedIntValue]; |
| 352 |
} |
| 353 |
|
| 354 |
return PantomimeFormatUnknown; |
| 355 |
} |
| 356 |
|
| 357 |
- (void) setFormat: (PantomimeMessageFormat) theFormat |
| 358 |
{ |
| 359 |
[_parameters setObject: [NSNumber numberWithUnsignedInt: theFormat] forKey: @"format"]; |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
// |
| 364 |
// |
| 365 |
// |
| 366 |
- (NSUInteger) lineLength |
| 367 |
{ |
| 368 |
return _line_length; |
| 369 |
} |
| 370 |
|
| 371 |
- (void) setLineLength: (NSUInteger) theLineLength |
| 372 |
{ |
| 373 |
_line_length = theLineLength; |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
// |
| 378 |
// This method is used to verify if the part is of the following primaryType / subType |
| 379 |
// |
| 380 |
- (BOOL) isMIMEType: (NSString *) thePrimaryType |
| 381 |
subType: (NSString *) theSubType |
| 382 |
{ |
| 383 |
NSString *aString; |
| 384 |
|
| 385 |
if (![self contentType]) |
| 386 |
{ |
| 387 |
NSDebugLog(@"CWPart isMimeType: %@ subType: %@ - contentType is nil", thePrimaryType, theSubType); |
| 388 |
return NO; |
| 389 |
} |
| 390 |
|
| 391 |
if ([theSubType compare: @"*"] == NSOrderedSame) |
| 392 |
{ |
| 393 |
if ([[self contentType] hasCaseInsensitivePrefix: thePrimaryType]) |
| 394 |
{ |
| 395 |
return YES; |
| 396 |
} |
| 397 |
} |
| 398 |
else |
| 399 |
{ |
| 400 |
aString = [NSString stringWithFormat: @"%@/%@", thePrimaryType, theSubType]; |
| 401 |
|
| 402 |
if ([aString caseInsensitiveCompare: [self contentType]] == NSOrderedSame) |
| 403 |
{ |
| 404 |
return YES; |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
return NO; |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
// |
| 413 |
// |
| 414 |
// |
| 415 |
- (unsigned long) size |
| 416 |
{ |
| 417 |
return _size; |
| 418 |
} |
| 419 |
|
| 420 |
- (void) setSize: (unsigned long) theSize |
| 421 |
{ |
| 422 |
_size = theSize; |
| 423 |
} |
| 424 |
|
| 425 |
|
| 426 |
// |
| 427 |
// |
| 428 |
// |
| 429 |
- (NSData *) dataValue |
| 430 |
{ |
| 431 |
NSMutableData *aMutableData; |
| 432 |
NSMutableArray *allKeys; |
| 433 |
NSData *aDataToSend; |
| 434 |
NSArray *allLines; |
| 435 |
NSString *aFilename; |
| 436 |
NSUInteger i, count; |
| 437 |
|
| 438 |
aMutableData = [[NSMutableData alloc] init]; |
| 439 |
|
| 440 |
// We start off by exactring the filename of the part. |
| 441 |
if ([[self filename] is7bitSafe]) |
| 442 |
{ |
| 443 |
aFilename = [self filename]; |
| 444 |
} |
| 445 |
else |
| 446 |
{ |
| 447 |
aFilename = [[NSString alloc] initWithData: [CWMIMEUtility encodeWordUsingQuotedPrintable: [self filename] |
| 448 |
prefixLength: 0] |
| 449 |
encoding: NSASCIIStringEncoding]; |
| 450 |
AUTORELEASE(aFilename); |
| 451 |
} |
| 452 |
|
| 453 |
// We encode our Content-Transfer-Encoding header. |
| 454 |
if ([self contentTransferEncoding] != PantomimeEncodingNone) |
| 455 |
{ |
| 456 |
[aMutableData appendCFormat: @"Content-Transfer-Encoding: %@%s", |
| 457 |
[NSString stringValueOfTransferEncoding: [self contentTransferEncoding]], |
| 458 |
LF]; |
| 459 |
} |
| 460 |
|
| 461 |
// We encode our Content-ID header. |
| 462 |
if ([self contentID]) |
| 463 |
{ |
| 464 |
[aMutableData appendCFormat: @"Content-ID: %@%s", [self contentID], LF]; |
| 465 |
} |
| 466 |
|
| 467 |
// We encode our Content-Description header. |
| 468 |
if ([self contentDescription]) |
| 469 |
{ |
| 470 |
[aMutableData appendCString: "Content-Description: "]; |
| 471 |
[aMutableData appendData: [CWMIMEUtility encodeWordUsingQuotedPrintable: [self contentDescription] |
| 472 |
prefixLength: 21]]; |
| 473 |
[aMutableData appendCString: LF]; |
| 474 |
} |
| 475 |
|
| 476 |
// We now encode the Content-Type header with its parameters. |
| 477 |
[aMutableData appendCFormat: @"Content-Type: %@", [self contentType]]; |
| 478 |
|
| 479 |
if ([self charset]) |
| 480 |
{ |
| 481 |
[aMutableData appendCFormat: @"; charset=\"%@\"", [self charset]]; |
| 482 |
} |
| 483 |
|
| 484 |
if ([self format] == PantomimeFormatFlowed && |
| 485 |
([self contentTransferEncoding] == PantomimeEncodingNone || [self contentTransferEncoding] == PantomimeEncoding8bit)) |
| 486 |
{ |
| 487 |
[aMutableData appendCString: "; format=\"flowed\""]; |
| 488 |
} |
| 489 |
|
| 490 |
if (aFilename && [aFilename length]) |
| 491 |
{ |
| 492 |
[aMutableData appendCFormat: @"; name=\"%@\"", aFilename]; |
| 493 |
} |
| 494 |
|
| 495 |
// Before checking for all other parameters, we check for the boundary one |
| 496 |
// If we got a CWMIMEMultipart instance as the content but no boundary |
| 497 |
// was set, we create a boundary and we set it. |
| 498 |
if ([self boundary] || [_content isKindOfClass: [CWMIMEMultipart class]]) |
| 499 |
{ |
| 500 |
if (![self boundary]) |
| 501 |
{ |
| 502 |
[self setBoundary: [CWMIMEUtility globallyUniqueBoundary]]; |
| 503 |
} |
| 504 |
|
| 505 |
[aMutableData appendCFormat: @";%s\tboundary=\"",LF]; |
| 506 |
[aMutableData appendData: [self boundary]]; |
| 507 |
[aMutableData appendCString: "\""]; |
| 508 |
} |
| 509 |
|
| 510 |
// We now check for any other additional parameters. If we have some, |
| 511 |
// we add them one per line. We first REMOVE what we have added! We'll |
| 512 |
// likely and protocol= here. |
| 513 |
allKeys = [NSMutableArray arrayWithArray: [_parameters allKeys]]; |
| 514 |
[allKeys removeObject: @"boundary"]; |
| 515 |
[allKeys removeObject: @"charset"]; |
| 516 |
[allKeys removeObject: @"filename"]; |
| 517 |
[allKeys removeObject: @"format"]; |
| 518 |
|
| 519 |
for (i = 0; i < [allKeys count]; i++) |
| 520 |
{ |
| 521 |
[aMutableData appendCFormat: @";%s", LF]; |
| 522 |
[aMutableData appendCFormat: @"\t%@=\"%@\"", [allKeys objectAtIndex: i], [_parameters objectForKey: [allKeys objectAtIndex: i]]]; |
| 523 |
} |
| 524 |
|
| 525 |
[aMutableData appendCString: LF]; |
| 526 |
|
| 527 |
// We encode our Content-Disposition header. We ignore other parameters |
| 528 |
// (other than the filename one) since they are pretty much worthless. |
| 529 |
// See RFC2183 for details. |
| 530 |
if ([self contentDisposition] == PantomimeAttachmentDisposition) |
| 531 |
{ |
| 532 |
[aMutableData appendCString: "Content-Disposition: attachment"]; |
| 533 |
|
| 534 |
if (aFilename && [aFilename length]) |
| 535 |
{ |
| 536 |
[aMutableData appendCFormat: @"; filename=\"%@\"", aFilename]; |
| 537 |
} |
| 538 |
|
| 539 |
[aMutableData appendCString: LF]; |
| 540 |
} |
| 541 |
|
| 542 |
if ([_content isKindOfClass: [CWMessage class]]) |
| 543 |
{ |
| 544 |
aDataToSend = [(CWMessage *)_content rawSource]; |
| 545 |
} |
| 546 |
else if ([_content isKindOfClass: [CWMIMEMultipart class]]) |
| 547 |
{ |
| 548 |
CWMIMEMultipart *aMimeMultipart; |
| 549 |
NSMutableData *md; |
| 550 |
CWPart *aPart; |
| 551 |
|
| 552 |
md = [[NSMutableData alloc] init]; |
| 553 |
aMimeMultipart = (CWMIMEMultipart *)_content; |
| 554 |
count = [aMimeMultipart count]; |
| 555 |
|
| 556 |
for (i = 0; i < count; i++) |
| 557 |
{ |
| 558 |
aPart = [aMimeMultipart partAtIndex: i]; |
| 559 |
|
| 560 |
if (i > 0) |
| 561 |
{ |
| 562 |
[md appendBytes: LF length: strlen(LF)]; |
| 563 |
} |
| 564 |
|
| 565 |
[md appendBytes: "--" length: 2]; |
| 566 |
[md appendData: [self boundary]]; |
| 567 |
[md appendBytes: LF length: strlen(LF)]; |
| 568 |
[md appendData: [aPart dataValue]]; |
| 569 |
} |
| 570 |
|
| 571 |
[md appendBytes: "--" length: 2]; |
| 572 |
[md appendData: [self boundary]]; |
| 573 |
[md appendBytes: "--" length: 2]; |
| 574 |
[md appendBytes: LF length: strlen(LF)]; |
| 575 |
|
| 576 |
aDataToSend = AUTORELEASE(md); |
| 577 |
} |
| 578 |
else |
| 579 |
{ |
| 580 |
aDataToSend = (NSData *)_content; |
| 581 |
} |
| 582 |
|
| 583 |
// We separe our part's headers from the content |
| 584 |
[aMutableData appendCFormat: @"%s", LF]; |
| 585 |
|
| 586 |
// We now encode our content the way it was specified |
| 587 |
if ([self contentTransferEncoding] == PantomimeEncodingQuotedPrintable) |
| 588 |
{ |
| 589 |
aDataToSend = [aDataToSend encodeQuotedPrintableWithLineLength: 72 inHeader: NO]; |
| 590 |
} |
| 591 |
else if ([self contentTransferEncoding] == PantomimeEncodingBase64) |
| 592 |
{ |
| 593 |
aDataToSend = [aDataToSend encodeBase64WithLineLength: 72]; |
| 594 |
} |
| 595 |
else if (([self contentTransferEncoding] == PantomimeEncodingNone || [self contentTransferEncoding] == PantomimeEncoding8bit) && |
| 596 |
[self format] == PantomimeFormatFlowed) |
| 597 |
{ |
| 598 |
NSUInteger limit; |
| 599 |
|
| 600 |
limit = _line_length; |
| 601 |
|
| 602 |
if (limit < 2 || limit > 998) |
| 603 |
{ |
| 604 |
limit = 72; |
| 605 |
} |
| 606 |
|
| 607 |
aDataToSend = [aDataToSend wrapWithLimit: limit]; |
| 608 |
} |
| 609 |
|
| 610 |
allLines = [aDataToSend componentsSeparatedByCString: "\n"]; |
| 611 |
count = [allLines count]; |
| 612 |
|
| 613 |
for (i = 0; i < count; i++) |
| 614 |
{ |
| 615 |
if (i == count-1 && [[allLines objectAtIndex: i] length] == 0) |
| 616 |
{ |
| 617 |
break; |
| 618 |
} |
| 619 |
|
| 620 |
[aMutableData appendData: [allLines objectAtIndex: i]]; |
| 621 |
[aMutableData appendBytes: LF length: 1]; |
| 622 |
} |
| 623 |
|
| 624 |
return AUTORELEASE(aMutableData); |
| 625 |
} |
| 626 |
|
| 627 |
|
| 628 |
// |
| 629 |
// |
| 630 |
// |
| 631 |
- (NSData *) boundary |
| 632 |
{ |
| 633 |
return [_parameters objectForKey: @"boundary"]; |
| 634 |
} |
| 635 |
|
| 636 |
- (void) setBoundary: (NSData *) theBoundary |
| 637 |
{ |
| 638 |
if (theBoundary) |
| 639 |
{ |
| 640 |
[_parameters setObject: theBoundary forKey: @"boundary"]; |
| 641 |
} |
| 642 |
} |
| 643 |
|
| 644 |
|
| 645 |
// |
| 646 |
// |
| 647 |
// |
| 648 |
- (NSData *) protocol |
| 649 |
{ |
| 650 |
return [_parameters objectForKey: @"protocol"]; |
| 651 |
//return _protocol; |
| 652 |
} |
| 653 |
|
| 654 |
- (void) setProtocol: (NSData *) theProtocol |
| 655 |
{ |
| 656 |
//ASSIGN(_protocol, theProtocol); |
| 657 |
if (theProtocol) |
| 658 |
{ |
| 659 |
[_parameters setObject: theProtocol forKey: @"protocol"]; |
| 660 |
} |
| 661 |
} |
| 662 |
|
| 663 |
|
| 664 |
// |
| 665 |
// |
| 666 |
// |
| 667 |
- (NSString *) charset |
| 668 |
{ |
| 669 |
return [_parameters objectForKey: @"charset"]; |
| 670 |
} |
| 671 |
|
| 672 |
- (void) setCharset: (NSString *) theCharset |
| 673 |
{ |
| 674 |
if (theCharset) |
| 675 |
{ |
| 676 |
[_parameters setObject: theCharset forKey: @"charset"]; |
| 677 |
} |
| 678 |
} |
| 679 |
|
| 680 |
|
| 681 |
// |
| 682 |
// |
| 683 |
// |
| 684 |
- (NSString *) defaultCharset |
| 685 |
{ |
| 686 |
return _defaultCharset; |
| 687 |
} |
| 688 |
|
| 689 |
|
| 690 |
// |
| 691 |
// |
| 692 |
// |
| 693 |
- (void) setDefaultCharset: (NSString *) theCharset |
| 694 |
{ |
| 695 |
ASSIGN(_defaultCharset, theCharset); |
| 696 |
} |
| 697 |
|
| 698 |
|
| 699 |
// |
| 700 |
// |
| 701 |
// |
| 702 |
- (void) setHeadersFromData: (NSData *) theHeaders |
| 703 |
{ |
| 704 |
NSAutoreleasePool *pool; |
| 705 |
NSArray *allLines; |
| 706 |
NSUInteger i, count; |
| 707 |
|
| 708 |
if (!theHeaders || [theHeaders length] == 0) |
| 709 |
{ |
| 710 |
return; |
| 711 |
} |
| 712 |
|
| 713 |
// We initialize a local autorelease pool |
| 714 |
pool = [[NSAutoreleasePool alloc] init]; |
| 715 |
|
| 716 |
// We MUST be sure to unfold all headers properly before |
| 717 |
// decoding the headers |
| 718 |
theHeaders = [theHeaders unfoldLines]; |
| 719 |
|
| 720 |
allLines = [theHeaders componentsSeparatedByCString: "\n"]; |
| 721 |
count = [allLines count]; |
| 722 |
|
| 723 |
for (i = 0; i < count; i++) |
| 724 |
{ |
| 725 |
NSData *aLine = [allLines objectAtIndex: i]; |
| 726 |
|
| 727 |
// We stop if we found the header separator. (\n\n) since someone could |
| 728 |
// have called this method with the entire rawsource of a message. |
| 729 |
if ([aLine length] == 0) |
| 730 |
{ |
| 731 |
break; |
| 732 |
} |
| 733 |
|
| 734 |
if ([aLine hasCaseInsensitiveCPrefix: "Content-Description"]) |
| 735 |
{ |
| 736 |
[CWParser parseContentDescription: aLine inPart: self]; |
| 737 |
} |
| 738 |
else if ([aLine hasCaseInsensitiveCPrefix: "Content-Disposition"]) |
| 739 |
{ |
| 740 |
[CWParser parseContentDisposition: aLine inPart: self]; |
| 741 |
} |
| 742 |
else if ([aLine hasCaseInsensitiveCPrefix: "Content-ID"]) |
| 743 |
{ |
| 744 |
[CWParser parseContentID: aLine inPart: self]; |
| 745 |
} |
| 746 |
else if ([aLine hasCaseInsensitiveCPrefix: "Content-Length"]) |
| 747 |
{ |
| 748 |
// We just ignore that for now. |
| 749 |
} |
| 750 |
else if ([aLine hasCaseInsensitiveCPrefix: "Content-Transfer-Encoding"]) |
| 751 |
{ |
| 752 |
[CWParser parseContentTransferEncoding: aLine inPart: self]; |
| 753 |
} |
| 754 |
else if ([aLine hasCaseInsensitiveCPrefix: "Content-Type"]) |
| 755 |
{ |
| 756 |
[CWParser parseContentType: aLine inPart: self]; |
| 757 |
} |
| 758 |
} |
| 759 |
|
| 760 |
RELEASE(pool); |
| 761 |
} |
| 762 |
|
| 763 |
|
| 764 |
// |
| 765 |
// |
| 766 |
// |
| 767 |
- (id) parameterForKey: (NSString *) theKey |
| 768 |
{ |
| 769 |
return [_parameters objectForKey: theKey]; |
| 770 |
} |
| 771 |
|
| 772 |
- (void) setParameter: (id) theParameter forKey: (NSString *) theKey |
| 773 |
{ |
| 774 |
if (theParameter) |
| 775 |
{ |
| 776 |
[_parameters setObject: theParameter forKey: theKey]; |
| 777 |
} |
| 778 |
else |
| 779 |
{ |
| 780 |
[_parameters removeObjectForKey: theKey]; |
| 781 |
} |
| 782 |
} |
| 783 |
|
| 784 |
// |
| 785 |
// |
| 786 |
// |
| 787 |
- (NSDictionary *) allHeaders |
| 788 |
{ |
| 789 |
return _headers; |
| 790 |
} |
| 791 |
|
| 792 |
// |
| 793 |
// |
| 794 |
// |
| 795 |
- (id) headerValueForName: (NSString *) theName |
| 796 |
{ |
| 797 |
NSArray *allKeys; |
| 798 |
NSUInteger count; |
| 799 |
|
| 800 |
allKeys = [_headers allKeys]; |
| 801 |
count = [allKeys count]; |
| 802 |
|
| 803 |
while (count--) |
| 804 |
{ |
| 805 |
if ([[allKeys objectAtIndex: count] caseInsensitiveCompare: theName] == NSOrderedSame) |
| 806 |
{ |
| 807 |
return [_headers objectForKey: [allKeys objectAtIndex: count]]; |
| 808 |
} |
| 809 |
} |
| 810 |
|
| 811 |
return nil; |
| 812 |
} |
| 813 |
|
| 814 |
// |
| 815 |
// |
| 816 |
// |
| 817 |
- (void) setHeaders: (NSDictionary *) theHeaders |
| 818 |
{ |
| 819 |
if (theHeaders) |
| 820 |
{ |
| 821 |
[_headers addEntriesFromDictionary: theHeaders]; |
| 822 |
} |
| 823 |
else |
| 824 |
{ |
| 825 |
[_headers removeAllObjects]; |
| 826 |
} |
| 827 |
} |
| 828 |
@end |