-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenvelope.m
478 lines (401 loc) · 11.6 KB
/
envelope.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/*
* $Id: envelope.m 180 2012-02-12 16:29:03Z lhagan $
*
* Copyright (c) 2004 Branden J. Moore.
*
* This file is part of MacBiff, and is free software; you can redistribute
* it and/or modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* MacBiff is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with MacBiff; if not, write to the Free Software Foundation, Inc., 59
* Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*
*/
#import "envelope.h"
#include <string.h>
#if 0
# define EBUG 1
#endif
#include "debug.h"
int b64translate( char bin )
{
if ( bin >= 'A' && bin <= 'Z' )
return bin-65;
else if ( bin >= 'a' && bin <= 'z' )
return ((bin-97) + 26);
else if ( bin >= '0' && bin <= '9' )
return ((bin-48) + 52);
else if ( bin == '+' )
return 62;
else if ( bin == '/' )
return 63;
else
return 0;
}
void deBase64(char* bin, char* bout)
{
char buf[4];
buf[0] = b64translate(bin[0]);
buf[1] = b64translate(bin[1]);
buf[2] = b64translate(bin[2]);
buf[3] = b64translate(bin[3]);
bout[0] = ((buf[0] << 2) & 0xfc) | ((buf[1] >> 4) & 0x03);
bout[1] = ((buf[1] << 4) & 0xf0) | ((buf[2] >> 2) & 0x0f);
bout[2] = ((buf[2] << 6) & 0xc0) | (buf[3] & 0x3f);
}
NSString* deMIMEEncodedWord(NSString* instr)
{
NSArray *parts = [instr componentsSeparatedByString: @"?"];
if ( !parts || [parts count] != 5 ) {
return [NSString stringWithString: instr];
}
NSString *charset = [parts objectAtIndex: 1];
NSStringEncoding charEncode =
CFStringConvertEncodingToNSStringEncoding(
CFStringConvertIANACharSetNameToEncoding(
(CFStringRef)charset));
NSString *encoding = [parts objectAtIndex: 2];
NSString *encodedText = [parts objectAtIndex: 3];
NSMutableData *encodedData = [[[NSMutableData alloc] init] autorelease];
[encodedData setData: [encodedText dataUsingEncoding:
NSASCIIStringEncoding
allowLossyConversion: YES]];
// int len = [encodedData length];
int len = (int)[encodedData length];
if ([encoding caseInsensitiveCompare: @"Q"] == NSOrderedSame ) {
char hex[3] = {0};
int val = 0;
char byte;
int i;
char *data = (char*)malloc(len);
// [encodedData getBytes: data];
[encodedData getBytes: data length: sizeof(data)];
NSRange range;
for ( i = len-3 ; i >= 0 ; --i ) {
if ( data[i] != '=' ) {
continue;
} else {
range = NSMakeRange(i, 3);
hex[0] = data[i+1];
hex[1] = data[i+2];
sscanf(hex, "%x", &val);
byte = val & 0xff;
[encodedData replaceBytesInRange: range
withBytes: (void*)&byte length: 1];
}
}
free(data);
} else if ([encoding caseInsensitiveCompare: @"B"] == NSOrderedSame ) {
char fdata[4];
char tdata[3];
NSRange range;
int i;
for ( i = len-4 ; i >= 0 ; i-=4 ) {
range = NSMakeRange(i, 4);
[encodedData getBytes: fdata range: range];
deBase64(fdata, tdata);
[encodedData replaceBytesInRange: range
withBytes: tdata length: 3];
}
}
NSString *ostr= [[[NSString alloc] initWithData: encodedData
encoding: charEncode] autorelease];
return ostr;
}
NSString* deMIMEString( NSString *str )
{
dprintf("deMIMEing string: '%s' [length: %d]\n", [str UTF8String], (int)[str length]);
NSMutableString *nStr = [[[NSMutableString alloc] init] autorelease];
[nStr setString: str];
NSRange MIMERange = [nStr rangeOfString: @"=?"];
while ( MIMERange.location != NSNotFound ) {
NSRange MIMERange2 = [nStr rangeOfString: @"?"
options: NSLiteralSearch
range: NSMakeRange(MIMERange.location +2,
[nStr length] - MIMERange.location -2)];
MIMERange2 = [nStr rangeOfString: @"?="
options: NSLiteralSearch
range: NSMakeRange(MIMERange2.location+3,
[nStr length] - MIMERange2.location -3)];
if ( MIMERange2.location == NSNotFound ) {
return nStr;
}
MIMERange.length = MIMERange2.location - MIMERange.location +2;
NSString *pStr = deMIMEEncodedWord([nStr substringWithRange: MIMERange]);
dprintf("deMIMEd component: '%s'\n", [pStr UTF8String]);
if ( pStr ) {
/* Successful decoding */
[nStr replaceCharactersInRange: MIMERange withString: pStr];
MIMERange = [nStr rangeOfString: @"=?"];
} else {
/* Failed decoding. Skip */
MIMERange.location++;
MIMERange.length = [nStr length] - MIMERange.location;
MIMERange = [nStr rangeOfString: @"=?"
options: NSLiteralSearch
range: MIMERange];
}
}
return nStr;
}
@implementation envelope
- (NSArray*) parseStringArray: (NSString*) sArr
{
unsigned length;
char *i;
char *j;
char tchar;
int pcount;
char *str = nil;
NSMutableArray *arr = nil;
NSArray *tarr;
NSMutableString *tstr;
if ( !sArr )
return nil;
str = strdup( [sArr UTF8String] );
if ( str[0] != '(' )
goto out;
arr = [[NSMutableArray alloc] initWithCapacity: 4];
// length = strlen( str );
length = (int)strlen( str );
if ( length != [sArr length] ) {
alert("Warning, c string length (%u) != NSString length (%u)\n",
// length, [sArr length]);
length, (int)[sArr length]);
}
dprintf("Analyzing '%s'...\n", str);
for ( i = str+1 ; *i ; i++ ) {
if ( *i == ' ' ) {
continue;
} else if ( *i == '(' ) {
/* Start of an array */
pcount = 1; j = i;
do {
j++;
if ( *j == '(' ) pcount++;
else if ( *j == ')' ) pcount--;
} while ( pcount && *j );
/* Note: Arrays are paren-bound */
j++;
tchar = *j;
*j = '\0';
// tstr = [NSString stringWithCString: i encoding: 4];
tstr = [NSMutableString stringWithCString: i encoding: 4];
*j = tchar;
i = j;
dprintf("Sending '%s' to parse as an array.\n",
[tstr UTF8String]);
tarr = [self parseStringArray: tstr];
if ( tarr ) {
[arr addObject: tarr];
}
} else if ( *i == ')' ) {
/* End of an array */
dprintf("Found end of array at %d of %lu\n",
(int)(i-str), strlen(str));
/* MDaemon 7.0.1 does NOT follow RFC3501 */
[arr addObject: @"NIL"];
break;
} else if ( *i == '\"' ) {
/* Start of a string */
j = i+1;
while ( *j != '\"' ||
(*j == '\"' && *(j-1) == '\\' &&
*(j-2) != '\\') ) {
j++;
}
tchar = *j;
*j = '\0';
dprintf("Taking string: '%s'\n", i+1);
tstr = [NSMutableString stringWithCString: i+1 encoding: 4];
[tstr replaceOccurrencesOfString: @"\\\""
withString: @"\""
options: NSLiteralSearch
range: NSMakeRange(0, [tstr length])];
*j = tchar;
i = j;
dprintf("Storing '%s'\n", [tstr UTF8String]);
[arr addObject: tstr];
} else {
if ( *i != 'N' && *i != 'n' ) {
dprintf("Unexpected character: %c at"
" position %d\n", *i, (int)(i-str));
} else {
dprintf("Storing NIL\n");
[arr addObject: @"NIL"];
do { i++; } while ( *i != ' ' && *i != ')');
}
}
}
out:
free( str );
dprintf("Returning from %s [[%s]]\n", __FUNCTION__, [sArr UTF8String]);
return [arr autorelease];
}
- (NSString*) stringFromAddrArray: (NSArray*) arr
{
if ( !arr ) return @"NA";
if ( [[arr objectAtIndex: 0] isKindOfClass: [NSArray class]] ) {
return [self stringFromAddrArray: [arr objectAtIndex: 0]];
}
if ( ![[arr objectAtIndex: 0] isEqualTo: @"NIL"] ) {
return [arr objectAtIndex: 0];
} else {
return [NSString stringWithFormat: @"%@@%@",
[arr objectAtIndex: 2],
[arr objectAtIndex: 3]];
}
}
- (NSString*) stringFromObject: (id) obj
{
if ( [obj isKindOfClass: [NSString class]] ) {
return obj;
} else if ( [obj isKindOfClass: [NSArray class]] ) {
return [self stringFromAddrArray: obj];
} else {
alert("Unrecognized class %s\n", [[obj className] UTF8String]);
return @"NA";
}
}
- (id) initWithIMAPEnvelope: (NSString*) env andUID: (unsigned) uid;
{
// if ( !env ) return env;
if ( !env ) return nil;
self = [super init];
dprintf("In %s for '%s'\n", __FUNCTION__, [env UTF8String]);
NSArray* envArr = [self parseStringArray: env];
_uid = uid;
int i;
for ( i = 0 ; i < [envArr count] ; ++i ) {
dprintf("Object %d is of type %s\n", i,
[[[envArr objectAtIndex: i] className] UTF8String]);
}
// _date = [[NSCalendarDate dateWithNaturalLanguageString:
// [envArr objectAtIndex: 0] retain];
NSString* dateStr = [envArr objectAtIndex: 0];
//remove day of week at beginning if there
if ([[[dateStr substringToIndex:4] substringFromIndex:3] isEqual: @","]) {
dateStr = [dateStr substringFromIndex:5];
}
int dateLen = (int)[dateStr length];
//remove parenthetical time zone at end if there
if ([[[dateStr substringToIndex:dateLen] substringFromIndex:(dateLen-1)] isEqual: @")"]) {
int datePos = dateLen - 1;
bool parFound = false;
while (!parFound) {
if ([[[dateStr substringToIndex:datePos] substringFromIndex:(datePos-1)] isEqual: @"("]) {
parFound = true;
} else {
datePos -= 1;
}
}
dateStr = [dateStr substringToIndex:(datePos - 2)];
}
//add seconds if missing
if (([[dateStr componentsSeparatedByString:@":"] count] - 1) == 1) {
NSArray* arr = [dateStr componentsSeparatedByString:@":"];
int minutes = (int)[[arr objectAtIndex:0] length]+3;
NSString* temp = [NSString stringWithFormat:@"%@",dateStr];
dateStr = [NSString stringWithFormat:@"%@%@%@",[temp substringToIndex:minutes],@":00",[temp substringFromIndex:(minutes-1)]];
}
NSDateFormatter *dFormatter = [[[NSDateFormatter alloc] init] autorelease];
dFormatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
dFormatter.dateFormat = @"dd MMM yyyy HH:mm:ss ZZZZZ";
dFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
_date = [[dFormatter dateFromString:dateStr] retain];
_subject = [deMIMEString([envArr objectAtIndex: 1]) copy];
_from = [deMIMEString([self stringFromObject: [envArr objectAtIndex: 2]]) copy];
_sender = [deMIMEString([self stringFromObject: [envArr objectAtIndex: 3]]) copy];
_reply_to = [deMIMEString([self stringFromObject: [envArr objectAtIndex: 4]]) copy];
_to = [deMIMEString([self stringFromObject: [envArr objectAtIndex: 5]]) copy];
_cc = [deMIMEString([self stringFromObject: [envArr objectAtIndex: 6]]) copy];
_bcc = [deMIMEString([self stringFromObject: [envArr objectAtIndex: 7]]) copy];
_in_reply_to = [deMIMEString([envArr objectAtIndex: 8]) copy];
_message_id = [[envArr objectAtIndex: 9] retain];
_initialized = YES;
return (self);
}
- (void) dealloc
{
[_date release];
[_subject release];
[_from release];
[_sender release];
[_reply_to release];
[_to release];
[_cc release];
[_bcc release];
[_in_reply_to release];
[_message_id release];
[super dealloc];
}
- (BOOL) isEqual: (id) anObject
{
envelope *other = anObject;
return (_uid == [other uid]);
}
- (NSUInteger) hash
{
return _uid;
}
- (void) setUID: (unsigned) uid
{
_uid = uid;
}
- (unsigned) uid
{
return _uid;
}
//- (NSCalendarDate*) date
- (NSDate*) date
{
return _date;
}
- (NSString*) subject
{
return _subject;
}
- (NSString*) from
{
return _from;
}
- (NSString*) sender
{
return _sender;
}
- (NSString*) reply_to
{
return _reply_to;
}
- (NSString*) to
{
return _to;
}
- (NSString*) cc
{
return _cc;
}
- (NSString*) bcc
{
return _bcc;
}
- (NSString*) in_reply_to
{
return _in_reply_to;
}
- (NSString*) message_id
{
return _message_id;
}
- (bool) initialized
{
return _initialized;
}
@end