~ubuntu-branches/ubuntu/edgy/sope/edgy

« back to all changes in this revision

Viewing changes to sope-mime/NGMail/NGMimeMessageParser.m

  • Committer: Bazaar Package Importer
  • Author(s): Sebastian Ley
  • Date: 2005-08-19 16:53:31 UTC
  • Revision ID: james.westby@ubuntu.com-20050819165331-hs683wz1osm708pw
Tags: upstream-4.4rc.2
ImportĀ upstreamĀ versionĀ 4.4rc.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  Copyright (C) 2000-2005 SKYRIX Software AG
 
3
 
 
4
  This file is part of SOPE.
 
5
 
 
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
 
9
  later version.
 
10
 
 
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.
 
15
 
 
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
 
19
  02111-1307, USA.
 
20
*/
 
21
 
 
22
#include "NGMimeMessageParser.h"
 
23
#include "NGMimeMessage.h"
 
24
#include "common.h"
 
25
 
 
26
 
 
27
 
 
28
@interface NGMimeMessageParserDelegate : NSObject
 
29
@end
 
30
 
 
31
@implementation NGMimeMessageParserDelegate
 
32
 
 
33
static int   UseFoundationStringEncodingForMimeHeader = -1;
 
34
static Class NGMimeMessageParserClass                 = NULL; 
 
35
 
 
36
+ (void)initialize {
 
37
  if (UseFoundationStringEncodingForMimeHeader == -1) {
 
38
    UseFoundationStringEncodingForMimeHeader
 
39
      = [[NSUserDefaults standardUserDefaults]
 
40
                         boolForKey:@"UseFoundationStringEncodingForMimeHeader"]
 
41
      ? 1 : 0;
 
42
  }
 
43
  if (NGMimeMessageParserClass == NULL) {
 
44
    NGMimeMessageParserClass = [NGMimeMessageParser class];
 
45
  }
 
46
}
 
47
 
 
48
- (id)parser:(id)_parser parseHeaderField:(NSString *)_field data:(NSData *)_data
 
49
{
 
50
  id v = nil;
 
51
 
 
52
  if ([_parser isKindOfClass:NGMimeMessageParserClass] == NO) {
 
53
    NGMimeMessageParser *parser = nil;
 
54
 
 
55
    parser = [[NGMimeMessageParserClass alloc] init];
 
56
    v = [parser valueOfHeaderField:_field data:_data];
 
57
    [parser release]; parser = nil;
 
58
  }
 
59
  return v;
 
60
}
 
61
 
 
62
- (id<NGMimeBodyParser>)parser:(NGMimePartParser *)_parser
 
63
  bodyParserForPart:(id<NGMimePart>)_part
 
64
{
 
65
  id                   ctype;
 
66
  NGMimeType           *contentType;
 
67
 
 
68
  ctype = [_part contentType];
 
69
  
 
70
  contentType = ([ctype isKindOfClass:[NGMimeType class]])
 
71
    ? ctype
 
72
    : [NGMimeType mimeType:[ctype stringValue]];
 
73
  
 
74
  if ([[contentType type] isEqualToString:@"message"] &&
 
75
      [[contentType subType] isEqualToString:@"rfc822"]) {
 
76
    return [[[NGMimeRfc822BodyParser alloc] init] autorelease];
 
77
  }
 
78
  return nil;
 
79
}
 
80
 
 
81
 
 
82
@end /* NGMimeMessageParserDelegate */
 
83
 
 
84
@implementation NGMimeMessageParser
 
85
 
 
86
static Class NSStringClass = Nil;
 
87
 
 
88
+ (int)version {
 
89
  return 3;
 
90
}
 
91
+ (void)initialize {
 
92
  NSAssert2([super version] == 3,
 
93
            @"invalid superclass (%@) version %i !",
 
94
            NSStringFromClass([self superclass]), [super version]);
 
95
  if (NSStringClass == Nil)
 
96
    NSStringClass = [NSString class];
 
97
}
 
98
 
 
99
- (id)init {
 
100
  if ((self = [super init])) {
 
101
    [self setDelegate:[NGMimeMessageParserDelegate new]];
 
102
  }
 
103
  return self;
 
104
}
 
105
 
 
106
/* factory */
 
107
 
 
108
- (id<NGMimePart>)producePartWithHeader:(NGHashMap *)_header {
 
109
  return [NGMimeMessage messageWithHeader:_header];
 
110
}
 
111
 
 
112
/* header field specifics */
 
113
 
 
114
- (id)valueOfHeaderField:(NSString *)_name data:(id)_data {
 
115
  // check data for 8-bit headerfields (RFC 2047 (MIME PART III))
 
116
  
 
117
  /* check whether we got passed a string ... */
 
118
  if ([_data isKindOfClass:NSStringClass]) {
 
119
    NSLog(@"%s: WARNING unexpected class for headerfield %@ (value %@)",
 
120
          __PRETTY_FUNCTION__, _name, _data);
 
121
    return [super valueOfHeaderField:_name data:_data];
 
122
  }
 
123
  _data = [_data decodeQuotedPrintableValueOfMIMEHeaderField:_name];
 
124
  return [super valueOfHeaderField:_name data:_data];
 
125
}
 
126
 
 
127
@end /* NGMimeMessageParser */
 
128
 
 
129
@implementation NSData(MimeQPHeaderFieldDecoding)
 
130
 
 
131
- (id)decodeQuotedPrintableValueOfMIMEHeaderField:(NSString *)_name {
 
132
  // check data for 8-bit headerfields (RFC 2047 (MIME PART III))
 
133
  static Class NGMimeTypeClass = Nil;
 
134
  enum {
 
135
    NGMimeMessageParser_quoted_start   = 1,
 
136
    NGMimeMessageParser_quoted_charSet = 2,
 
137
    NGMimeMessageParser_quoted_qpData  = 3,
 
138
    NGMimeMessageParser_quoted_end     = 4
 
139
  } status = NGMimeMessageParser_quoted_start;
 
140
  unsigned int        length;
 
141
  const unsigned char *bytes, *firstEq;
 
142
  BOOL foundQP = NO;
 
143
 
 
144
  if (NSStringClass   == Nil) NSStringClass   = [NSString class];
 
145
  if (NGMimeTypeClass == Nil) NGMimeTypeClass = [NGMimeType class];
 
146
  
 
147
  length = [self length];
 
148
  
 
149
  /* check whether the string is long enough to be quoted etc */
 
150
  if (length <= 6)
 
151
    return self;
 
152
  
 
153
  /* check whether the string contains QP tokens ... */
 
154
  bytes = [self bytes];
 
155
  
 
156
  if ((firstEq = memchr(bytes, '=', length)) == NULL)
 
157
    return self;
 
158
  
 
159
  /* process data ... (quoting etc) */
 
160
  {
 
161
    unichar       *buffer;
 
162
    unsigned int  bufLen, maxBufLen;
 
163
    NSString      *charset;
 
164
    BOOL          appendLC;
 
165
    int           cnt, tmp;
 
166
    unsigned char encoding;
 
167
    
 
168
    buffer = calloc(length + 13, sizeof(unichar));
 
169
    
 
170
    maxBufLen             = length + 3;
 
171
    buffer[maxBufLen - 1] = '\0';
 
172
    bufLen                = 0;
 
173
    
 
174
    encoding = 0;
 
175
    tmp      = -1;
 
176
    appendLC = YES;      
 
177
    charset  = nil;
 
178
    status   = NGMimeMessageParser_quoted_start;
 
179
 
 
180
    /* copy data up to first '=' sign */
 
181
    if ((cnt = (firstEq - bytes)) > 0) {
 
182
      for (; bufLen < cnt; bufLen++) 
 
183
        buffer[bufLen] = bytes[bufLen];
 
184
    }
 
185
    
 
186
    for (; cnt < (length-1); cnt++) {
 
187
      appendLC = YES;      
 
188
      
 
189
      if (status == NGMimeMessageParser_quoted_start) {
 
190
        if ((bytes[cnt] == '=') && (bytes[cnt + 1] == '?')) { // found begin
 
191
          cnt++;
 
192
          status = NGMimeMessageParser_quoted_charSet;
 
193
        }
 
194
        else { // other char
 
195
          if (bytes[cnt + 1] != '=') {
 
196
            buffer[bufLen++] = bytes[cnt];
 
197
            buffer[bufLen++] = bytes[cnt+1];
 
198
            cnt++;
 
199
            if (cnt >= length - 1)
 
200
              appendLC = NO;
 
201
          }
 
202
          else {
 
203
            buffer[bufLen++] = bytes[cnt];
 
204
          }
 
205
        }
 
206
      }
 
207
      else if (status == NGMimeMessageParser_quoted_charSet) {
 
208
        if (tmp == -1)
 
209
          tmp = cnt;
 
210
        
 
211
        if (bytes[cnt] == '?') {
 
212
          charset = 
 
213
            [NSStringClass stringWithCString:(bytes + tmp) length:cnt - tmp];
 
214
          tmp = -1;
 
215
          
 
216
          if ((length - cnt) > 2) { 
 
217
            // set encoding (eg 'q' for quoted printable)
 
218
            cnt++; // skip '?'
 
219
            encoding = bytes[cnt];
 
220
            cnt++; // skip encoding
 
221
            status = NGMimeMessageParser_quoted_qpData;
 
222
          }
 
223
          else { // unexpected end
 
224
            NSLog(@"WARNING: unexpected end of header");
 
225
            appendLC = NO;
 
226
            break;
 
227
          }
 
228
        }
 
229
      }
 
230
      else if (status == NGMimeMessageParser_quoted_qpData) {
 
231
        if (tmp == -1)
 
232
          tmp = cnt;
 
233
        
 
234
        if ((bytes[cnt] == '?') && (bytes[cnt + 1] == '=')) {
 
235
          NSData           *tmpData;
 
236
          NSString         *tmpStr;
 
237
          unsigned int     tmpLen;
 
238
          
 
239
          tmpData = _rfc2047Decoding(encoding, bytes + tmp, cnt - tmp);
 
240
          foundQP = YES;
 
241
 
 
242
          /* 
 
243
             create a temporary string for charset conversion ... 
 
244
             Note: the headerfield is currently held in ISO Latin 1
 
245
          */
 
246
          tmpStr = nil;
 
247
          
 
248
          if (!UseFoundationStringEncodingForMimeHeader) {
 
249
            tmpStr = [NSStringClass stringWithData:tmpData
 
250
                                    usingEncodingNamed:charset];
 
251
          }
 
252
          if (tmpStr == nil) {
 
253
            NSStringEncoding enc;
 
254
            
 
255
            enc    = [NGMimeTypeClass stringEncodingForCharset:charset];
 
256
            tmpStr = [[[NSStringClass alloc] initWithData:tmpData encoding:enc]
 
257
                                      autorelease];
 
258
          }
 
259
          tmpLen = [tmpStr length];
 
260
          
 
261
          if ((tmpLen + bufLen) < maxBufLen) {
 
262
            [tmpStr getCharacters:(buffer + bufLen)];
 
263
            bufLen += tmpLen;
 
264
          }
 
265
          else {
 
266
            NSLog(@"ERROR[%s]: quoted data to large --> ignored %@",
 
267
                  __PRETTY_FUNCTION__, tmpStr);
 
268
          }
 
269
          tmp = -1;
 
270
          cnt++;
 
271
          appendLC = YES;
 
272
          status   = NGMimeMessageParser_quoted_start;
 
273
        }
 
274
      }
 
275
    }
 
276
    if (appendLC == YES) {
 
277
      if (cnt < length) {
 
278
        buffer[bufLen] = bytes[cnt];
 
279
        bufLen++;
 
280
      }
 
281
    }
 
282
    buffer[bufLen] = '\0';
 
283
    {
 
284
      id data;
 
285
 
 
286
      data = nil;
 
287
      
 
288
      if (buffer && foundQP) {
 
289
        data = [[[NSString alloc] initWithCharacters:buffer length:bufLen]
 
290
                           autorelease];
 
291
        if (data == nil) {
 
292
          NSLog(@"%s: got no string for buffer '%s', length '%i' !", 
 
293
                __PRETTY_FUNCTION__,
 
294
                buffer, bufLen);
 
295
        }
 
296
      }
 
297
      if (!data) {
 
298
        data = self;
 
299
      }
 
300
      free(buffer); buffer = NULL;
 
301
      return data;
 
302
    }
 
303
  }
 
304
  return self;
 
305
}
 
306
 
 
307
@end /* NSData(MimeQPHeaderFieldDecoding) */
 
308
 
 
309
@implementation NGMimeRfc822BodyParser
 
310
 
 
311
+ (int)version {
 
312
  return 2;
 
313
}
 
314
+ (void)initialize {
 
315
  NSAssert2([super version] == 2,
 
316
            @"invalid superclass (%@) version %i !",
 
317
            NSStringFromClass([self superclass]), [super version]);
 
318
}
 
319
 
 
320
- (id)parseBodyOfPart:(id<NGMimePart>)_part data:(NSData *)_data
 
321
  delegate:(id)_d
 
322
{
 
323
  id<NGMimePart> body;
 
324
  id             parser; // NGMimeMessageParser
 
325
 
 
326
  parser = [[NGMimeMessageParser alloc] init];
 
327
  body = [parser parsePartFromData:_data];
 
328
  [parser release]; parser = nil;
 
329
  
 
330
  return body;
 
331
}
 
332
 
 
333
@end /* NGMimeRfc822BodyParser */