]> err.no Git - sope/blob - sope-mime/NGMime/NGMimeHeaderFieldParser.m
minor code cleanups
[sope] / sope-mime / NGMime / NGMimeHeaderFieldParser.m
1 /*
2   Copyright (C) 2000-2003 SKYRIX Software AG
3
4   This file is part of OGo
5
6   OGo 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   OGo 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 OGo; 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 // $Id$
22
23 #include "NGMimeHeaderFieldParser.h"
24 #include "NGMimeHeaderFields.h"
25 #include "NGMimeUtilities.h"
26 #include "common.h"
27 #include <NGMime/NGMimePartParser.h>
28
29 @implementation NGMimeHeaderFieldParser
30
31 static int MimeLogEnabled     = -1;
32 static int StripLeadingSpaces = -1;
33
34 + (void)initialize {
35   NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
36   
37   if (MimeLogEnabled == -1)
38     MimeLogEnabled = [ud boolForKey:@"MimeLogEnabled"]?1:0;
39   if (StripLeadingSpaces == -1)
40     StripLeadingSpaces = [ud boolForKey:@"StripLeadingSpaces"]?1:0;
41 }
42 + (BOOL)isMIMELogEnabled {
43   return MimeLogEnabled ? YES : NO;
44 }
45 + (BOOL)doesStripLeadingSpaces {
46   return StripLeadingSpaces ? YES : NO;
47 }
48
49 + (int)version {
50   return 2;
51 }
52
53 - (NSString *)removeCommentsFromValue:(NSString *)_rawValue {
54   unsigned int len = [_rawValue length];
55   unichar      bytes[len + 1];
56   unsigned int cnt;
57   NSString     *str;
58
59   if (_rawValue == NULL) return nil;
60
61   [_rawValue getCharacters:bytes];
62   bytes[len] = '\0';
63   cnt   = 0;
64   str   = nil;
65
66   while ((cnt < len) && (bytes[cnt] != '(')) cnt++;
67   
68   if (cnt < len) {
69     unichar  result[len+1];
70     int      resLen, commentNesting, begin;
71     BOOL     modifyValue;
72
73     resLen         = 0;
74     commentNesting = 0;
75     begin          = 0;
76     modifyValue    = NO;
77     
78     for (cnt = 0; cnt < len; cnt++) {
79       if (commentNesting == 0) {
80         if (isRfc822_QUOTE(bytes[cnt])) {
81           cnt++;
82           while ((cnt < len) && !isRfc822_QUOTE(bytes[cnt]))
83             cnt++;
84         }
85         else if (bytes[cnt] == '(') {
86           modifyValue = YES;
87           
88           if ((cnt - begin) > 0) {
89             int c;
90
91             for (c = begin; c < cnt; c++) 
92               result[resLen++]  = bytes[c];
93           }
94           commentNesting++;
95         }
96       }
97       else {
98         if (bytes[cnt] == ')') {
99           commentNesting--;
100           if (commentNesting == 0)
101             begin = (cnt + 1);
102         }
103         else if (bytes[cnt] == '(')
104           commentNesting++;
105       }
106     }
107     if (modifyValue) {
108       if ((cnt - begin) > 0) {
109         int c;
110
111         for (c = begin; c < cnt; c++)
112           result[resLen++]  = bytes[c];
113       }
114       str = [[[NSString alloc] initWithCharacters:result length:resLen]
115                         autorelease];
116     }
117   }
118   if (str == nil)
119     str = _rawValue;
120
121   if (MimeLogEnabled) {
122     if (str != _rawValue) {
123       [self logWithFormat:@"%s:%d remove comment [%@] -> [%@]",
124             __PRETTY_FUNCTION__, __LINE__, _rawValue, str];
125     }
126   }
127   return str;
128 }
129
130 - (NSData *)quotedPrintableDecoding:(NSData *)_value {
131   return _value;
132 }
133
134 - (id)parseValue:(NSString *)_data ofHeaderField:(NSString *)_field { 
135   // abstract
136   NSLog(@"ERROR(%s): subclass should override this method: %@",
137         __PRETTY_FUNCTION__, self);
138   return nil;
139 }
140
141 @end /* NGMimeHeaderFieldParser */