| 1 |
#import "DUMMail.h" |
| 2 |
|
| 3 |
@implementation DUMMail |
| 4 |
// Private Method |
| 5 |
-(void)parseMailData:(NSString *)mailData{ |
| 6 |
// かなりいい加減な実装 |
| 7 |
NSRange textRange; |
| 8 |
NSRange lineRange; |
| 9 |
NSString *line; |
| 10 |
NSMutableString *headerTextBuf; |
| 11 |
|
| 12 |
headerTextBuf = [NSMutableString stringWithCapacity:500]; |
| 13 |
textRange = NSMakeRange(0, [mailData length]); |
| 14 |
while(textRange.length > 0){ |
| 15 |
lineRange = [mailData lineRangeForRange: |
| 16 |
NSMakeRange(textRange.location, 0)]; |
| 17 |
line = [mailData substringWithRange:lineRange]; |
| 18 |
if([line hasPrefix:@"From: "]){ |
| 19 |
[headerTextBuf appendString:line]; |
| 20 |
from = [[DUMMailAddress alloc] |
| 21 |
initWithMailAddressString:[line substringFromIndex:[@"From: " length]]]; |
| 22 |
[from retain]; |
| 23 |
}else if([line hasPrefix:@"To: "]){ |
| 24 |
[headerTextBuf appendString:line]; |
| 25 |
to = [[DUMMailAddress alloc] |
| 26 |
initWithMailAddressString:[line substringFromIndex:[@"To: " length]]]; |
| 27 |
}else if([line hasPrefix:@"Subject: "]){ |
| 28 |
[headerTextBuf appendString:line]; |
| 29 |
subject = [line substringFromIndex:[@"Subject: " length]]; |
| 30 |
[subject retain]; |
| 31 |
}else if([line hasPrefix:@"Date: "]){ |
| 32 |
[headerTextBuf appendString:line]; |
| 33 |
NSString *dateString = [line substringFromIndex:[@"Date: " length]]; |
| 34 |
date = [NSCalendarDate dateWithString:dateString |
| 35 |
calendarFormat:@"%a, %d %b %Y %H:%M:%S %z"]; |
| 36 |
[date retain]; |
| 37 |
}else if([line isEqual:@"\r\n"]){ |
| 38 |
content = [mailData substringFromIndex:textRange.location]; |
| 39 |
[content retain]; |
| 40 |
headerText = [[headerTextBuf description] retain]; |
| 41 |
return; |
| 42 |
}else{ |
| 43 |
//無視 |
| 44 |
} |
| 45 |
|
| 46 |
textRange.location = NSMaxRange(lineRange); |
| 47 |
textRange.length -= lineRange.length; |
| 48 |
} |
| 49 |
} |
| 50 |
// Public Method |
| 51 |
-(id)initWithMailData:(NSString *)mailData{ |
| 52 |
self = [super init]; |
| 53 |
subject = nil; |
| 54 |
date = nil; |
| 55 |
content = nil; |
| 56 |
isUnread = YES; |
| 57 |
[self parseMailData:mailData]; |
| 58 |
return self; |
| 59 |
} |
| 60 |
-(void)dealloc{ |
| 61 |
[from release]; |
| 62 |
[to release]; |
| 63 |
[subject release]; |
| 64 |
[date release]; |
| 65 |
[content release]; |
| 66 |
[super dealloc]; |
| 67 |
} |
| 68 |
-(DUMMailAddress *)from{ |
| 69 |
return from; |
| 70 |
} |
| 71 |
-(DUMMailAddress *)to{ |
| 72 |
return to; |
| 73 |
} |
| 74 |
-(NSString *)subject{ |
| 75 |
return subject; |
| 76 |
} |
| 77 |
-(NSCalendarDate *)date{ |
| 78 |
return date; |
| 79 |
} |
| 80 |
-(NSString *)headerText{ |
| 81 |
return headerText; |
| 82 |
} |
| 83 |
-(NSString *)content{ |
| 84 |
return content; |
| 85 |
} |
| 86 |
-(BOOL)isUnread{ |
| 87 |
return isUnread; |
| 88 |
} |
| 89 |
-(void)setIsUnread:(BOOL)flag{ |
| 90 |
isUnread = flag; |
| 91 |
} |
| 92 |
- (NSString *)description{ |
| 93 |
return [NSString stringWithFormat:@"From:%@\nTo:%@\nSubject:%@\n%@" |
| 94 |
,from, to, subject, content]; |
| 95 |
} |
| 96 |
@end |