-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTBCompoundMimeMessage.m
executable file
·192 lines (166 loc) · 4.66 KB
/
TBCompoundMimeMessage.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/* TBCompoundMimeMessage.m created by todd on Mon 12-Jun-2000 */
#import "TBCompoundMimeMessage.h"
#import "TBContentType.h"
static const char crlf[] = { 13, 10, 0 };
// replacement for strstr - ignores embedded null characters and scans until end
const char* hd_strscan(const char* s1, const char* s2, const char* end)
{
const char* p = s1;
const char* pp;
const char* sp;
while(p < end)
{
while(p < end && *p != *s2) { ++p; }
pp = p++;
sp = s2;
while(*sp && pp < end && *pp == *sp)
{
++sp;
++pp;
}
if(!*sp && pp <= end) { return p-1; }
}
return 0;
}
@implementation TBCompoundMimeMessage
-initWithHeaders:(NSDictionary *)headers body: (NSData *) data
{
const char* bytes = [data bytes];
const char* endBytes = bytes + [data length];
char startbound[1024];
char endbound[1024];
int boundlen;
NSString* bndry;
[super initWithHeaders: headers];
bndry = [[self contentType] boundary];
if(bndry)
{
const char *startptr, *nextptr, *endptr;
strcpy(startbound,"--");
strncat(startbound,[bndry cString],1024);
strcpy(endbound,startbound);
strncat(endbound,"--",1024);
boundlen = strlen(startbound);
endptr = hd_strscan(bytes, endbound, endBytes);
if(endptr)
{
NSRange range;
startptr = hd_strscan(bytes, startbound, endBytes);
while(startptr && startptr != endptr)
{
NSData *d;
//NSString *s;
startptr += boundlen;
nextptr = hd_strscan(startptr,startbound,endBytes);
range.location = startptr-bytes;
range.length = nextptr-startptr;
d = [data subdataWithRange: range];
//s = [[[NSString alloc] initWithData: d encoding: NSASCIIStringEncoding] autorelease];
[self addPart: [TBMimeMessage mimeMessageFromData: [data subdataWithRange: range]]];
startptr = nextptr;
}
}
}
return self;
}
-initWithHeaders:(NSDictionary *)headers
{
TBContentType *ct = [headers objectForKey: @"content-type"];
if(![ct boundary])
{
[ct setBoundary: [[NSProcessInfo processInfo] globallyUniqueString]];
}
return [super initWithHeaders: headers];
}
-init
{
[super init];
_parts = [[NSMutableArray array] retain];
return self;
}
-(void)addPart:(TBMimeMessage *)msg
{
[_parts addObject: msg];
}
-(id)body
{
return _parts;
}
-(void)dealloc
{
[_parts release];
[super dealloc];
}
-(NSString *)description
{
return [[super description] stringByAppendingFormat:@"\n%@",_parts];
}
-(NSData *)messageData
{
NSData *bndry = [[[self contentType] boundary] dataUsingEncoding: NSASCIIStringEncoding];
int i;
NSMutableData *body = [NSMutableData data];
NSMutableData *header;
if([_parts count])
{
[body appendBytes: "--" length: 2];
[body appendData: bndry];
for(i = 0; i < [_parts count]; ++i)
{
[body appendBytes: crlf length: 2];
[body appendData: [[_parts objectAtIndex: i] messageData]];
[body appendBytes: crlf length: 2];
[body appendBytes: "--" length: 2];
[body appendData: bndry];
}
[body appendBytes: "--" length: 2];
[body appendBytes: crlf length: 2];
}
header = [[super messageData] mutableCopy];
[header appendData: [[NSString stringWithFormat:@"Content-Length: %d%s%s",[body length],crlf,crlf] dataUsingEncoding: NSASCIIStringEncoding]];
[header appendData: body];
return [header autorelease];
}
-(id)messageForContentId:(TBContentId *) cid
{
id msg = [super messageForContentId: cid];
if(!msg)
{
int i;
for(i = 0; i < [_parts count]; ++i)
{
msg = [[_parts objectAtIndex: i] messageForContentId: cid];
if(msg) break;
}
}
return msg;
}
-(id)messageForContentLocation:(NSURL *) loc
{
id msg = [super messageForContentLocation: loc];
if(!msg)
{
int i;
for(i = 0; i < [_parts count]; ++i)
{
msg = [[_parts objectAtIndex: i] messageForContentLocation: loc];
if(msg) break;
}
}
return msg;
}
-(id)messageForFilename:(NSString *)filename
{
id msg = [super messageForFilename: filename];
if(!msg)
{
int i;
for(i = 0; i < [_parts count]; ++i)
{
msg = [[_parts objectAtIndex: i] messageForFilename: filename];
if(msg) break;
}
}
return msg;
}
@end