]> err.no Git - sope/blob - sope-mime/NGMime/NGMimeAddressHeaderFieldGenerator.m
synced with latest additions and bumped framework versions
[sope] / sope-mime / NGMime / NGMimeAddressHeaderFieldGenerator.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 "NGMimeHeaderFieldGenerator.h"
23 #include "NGMimeHeaderFields.h"
24 #include <NGMail/NGMailAddressParser.h>
25 #include <NGMime/NGMimePartParser.h>
26 #include "common.h"
27
28 @interface NSObject(UsedProtocols)
29 - (NSString *)displayName; // hh: where is that implemented ?
30 @end
31
32 @implementation NGMimeAddressHeaderFieldGenerator
33
34 static int UseLFSeperatedAddressEntries = -1;
35
36 + (int)version {
37   return 2;
38 }
39
40 + (void)initialize {
41   NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
42
43   if (UseLFSeperatedAddressEntries == -1) {
44     id o;
45
46     if ((o = [ud objectForKey:@"UseLFSeperatedAddressEntries"]))
47       UseLFSeperatedAddressEntries = [o boolValue]?1:0;
48     else
49       UseLFSeperatedAddressEntries = 1;
50   }
51 }
52
53 /* operation */
54
55 - (NSData *)generateDataForHeaderFieldNamed:(NSString *)_headerField
56   value:(id)_value
57 {
58   NGMailAddressParser *parser;
59   NSMutableString     *result;
60   NSData              *data;
61   id                  obj;
62   NSEnumerator        *enumerator;
63   
64   parser = ([_value isKindOfClass:[NSString class]])
65     ? [NGMailAddressParser mailAddressParserWithString:_value]
66     : [NGMailAddressParser mailAddressParserWithData:_value];
67   
68   enumerator = [[parser parseAddressList] objectEnumerator];
69   result     = [[NSMutableString alloc] initWithCapacity:128];
70   
71   while ((obj = [enumerator nextObject])) {
72     NSString   *tmp;
73     char       *buffer;
74     unsigned   bufLen, cnt;
75     BOOL       doEnc;
76     
77     if ([result length] > 0) {
78       if (UseLFSeperatedAddressEntries == 1)
79         [result appendString:@",\n   "];
80       else
81         [result appendString:@", "];
82     }
83     
84     tmp    = [obj displayName];
85     bufLen = [tmp cStringLength];
86     
87     buffer = calloc(bufLen + 10, sizeof(char));
88     [tmp getCString:buffer];
89     
90     cnt   = 0;
91     doEnc = NO;
92     
93     while (cnt < bufLen) {
94       if ((unsigned char)buffer[cnt] > 127) {
95         doEnc = YES;
96         break;
97       }
98       cnt++;
99     }
100     
101     if (doEnc) {
102       unsigned char iso[]     = "=?iso-8859-15?q?";
103       unsigned      isoLen    = 16;
104       unsigned char isoEnd[]  = "?=";
105       unsigned      isoEndLen = 2;
106       unsigned      desLen;
107       unsigned char *des;
108       
109       if (buffer != NULL) free(buffer); buffer = NULL;
110       {
111         NSData *data;
112
113 #if APPLE_Foundation_LIBRARY || NeXT_Foundation_LIBRARY
114         data = [tmp dataUsingEncoding:NSISOLatin1StringEncoding];
115 #else
116         data = [tmp dataUsingEncoding:NSISOLatin9StringEncoding];
117 #endif
118
119         bufLen  = [data length];
120         buffer =  malloc(bufLen + 10);
121         [data getBytes:buffer];  buffer[bufLen] = '\0';
122       }
123           
124       desLen = bufLen * 3 + 20;
125       des    = calloc(desLen + 10, sizeof(char));
126       
127       memcpy(des, buffer, cnt);
128       memcpy(des + cnt, iso, isoLen);
129       desLen =
130         NGEncodeQuotedPrintableMime((unsigned char *)buffer + cnt, bufLen- cnt,
131                                     des + cnt + isoLen,
132                                     desLen - cnt - isoLen);
133       if ((int)desLen != -1) {
134         memcpy(des + cnt + isoLen + desLen, isoEnd, isoEndLen);
135         tmp = [NSString stringWithCString:(char *)des
136                         length:(cnt + isoLen + desLen + isoEndLen)];
137       }
138       else {
139         [self logWithFormat:@"WARNING(%s:%i): An error occour during "
140                 @"quoted-printable decoding",
141                 __PRETTY_FUNCTION__, __LINE__];
142       }
143       if (des) free(des);
144     }
145     if (buffer) free(buffer); buffer = NULL;
146
147     if ([tmp length] > 0) {
148       [result appendString:@"\""];
149       [result appendString:tmp];
150       [result appendString:@"\""];
151       if ((tmp = [(NSHost *)obj address])) {
152         [result appendString:@" <"];
153         [result appendString:tmp];
154         [result appendString:@">"];
155       }
156     }
157     else if ((tmp = [(NSHost *)obj address])) {
158       [result appendString:tmp];
159     }
160   }
161   
162 #if APPLE_Foundation_LIBRARY || NeXT_Foundation_LIBRARY
163   data = [result dataUsingEncoding:NSISOLatin1StringEncoding];
164 #else
165   data = [result dataUsingEncoding:NSISOLatin9StringEncoding];
166 #endif
167   [result release];
168   
169   return data;
170 }
171
172 @end /* NGMimeAddressHeaderFieldGenerator */