]> err.no Git - sope/blob - sope-mime/NGMail/NGMimeMessageParser.m
some code cleanups
[sope] / sope-mime / NGMail / NGMimeMessageParser.m
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 #include <string.h>
26
27 @interface NGMimeMessageParserDelegate : NSObject
28 @end
29
30 @implementation NGMimeMessageParserDelegate
31
32 static Class NGMimeMessageParserClass = Nil; 
33
34 + (void)initialize {
35   if (NGMimeMessageParserClass == Nil)
36     NGMimeMessageParserClass = [NGMimeMessageParser class];
37 }
38
39 - (id)parser:(NGMimePartParser *)_p parseHeaderField:(NSString *)_field
40   data:(NSData *)_data
41 {
42   NGMimeMessageParser *parser = nil;
43   id v;
44
45   if ([_p isKindOfClass:NGMimeMessageParserClass])
46     return nil;
47   
48   parser = [[NGMimeMessageParserClass alloc] init];
49   v = [parser valueOfHeaderField:_field data:_data];
50   [parser release]; parser = nil;
51   return v;
52 }
53
54 - (id<NGMimeBodyParser>)parser:(NGMimePartParser *)_parser
55   bodyParserForPart:(id<NGMimePart>)_part
56 {
57   id         ctype;
58   NGMimeType *contentType;
59
60   ctype = [_part contentType];
61   
62   contentType = ([ctype isKindOfClass:[NGMimeType class]])
63     ? ctype
64     : [NGMimeType mimeType:[ctype stringValue]];
65   
66   if ([[contentType type] isEqualToString:@"message"] &&
67       [[contentType subType] isEqualToString:@"rfc822"]) {
68     return [[[NGMimeRfc822BodyParser alloc] init] autorelease];
69   }
70   return nil;
71 }
72
73
74 @end /* NGMimeMessageParserDelegate */
75
76 @implementation NGMimeMessageParser
77
78 static Class NSStringClass = Nil;
79
80 + (int)version {
81   return 3;
82 }
83 + (void)initialize {
84   NSAssert2([super version] == 3,
85             @"invalid superclass (%@) version %i !",
86             NSStringFromClass([self superclass]), [super version]);
87   if (NSStringClass == Nil)
88     NSStringClass = [NSString class];
89 }
90
91 - (id)init {
92   if ((self = [super init])) {
93     [self setDelegate:[NGMimeMessageParserDelegate new]];
94   }
95   return self;
96 }
97
98 /* factory */
99
100 - (id<NGMimePart>)producePartWithHeader:(NGHashMap *)_header {
101   return [NGMimeMessage messageWithHeader:_header];
102 }
103
104 /* header field specifics */
105
106 - (id)valueOfHeaderField:(NSString *)_name data:(id)_data {
107   // check data for 8-bit headerfields (RFC 2047 (MIME PART III))
108   
109   /* check whether we got passed a string ... */
110   if ([_data isKindOfClass:NSStringClass]) {
111     NSLog(@"%s: WARNING unexpected class for headerfield %@ (value %@)",
112           __PRETTY_FUNCTION__, _name, _data);
113     return [super valueOfHeaderField:_name data:_data];
114   }
115   _data = [_data decodeQuotedPrintableValueOfMIMEHeaderField:_name];
116   return [super valueOfHeaderField:_name data:_data];
117 }
118
119 @end /* NGMimeMessageParser */
120
121
122
123 @implementation NGMimeRfc822BodyParser
124
125 + (int)version {
126   return 2;
127 }
128 + (void)initialize {
129   NSAssert2([super version] == 2,
130             @"invalid superclass (%@) version %i !",
131             NSStringFromClass([self superclass]), [super version]);
132 }
133
134 - (id)parseBodyOfPart:(id<NGMimePart>)_part data:(NSData *)_data
135   delegate:(id)_d
136 {
137   id<NGMimePart> body;
138   id             parser; // NGMimeMessageParser
139
140   parser = [[NGMimeMessageParser alloc] init];
141   body = [parser parsePartFromData:_data];
142   [parser release]; parser = nil;
143   
144   return body;
145 }
146
147 @end /* NGMimeRfc822BodyParser */