2 Copyright (C) 2000-2005 SKYRIX Software AG
4 This file is part of SOPE.
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
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.
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
22 #include "NGMimeHeaderFieldGenerator.h"
23 #include "NGMimeHeaderFields.h"
24 #include <NGMail/NGMailAddressParser.h>
25 #include <NGMime/NGMimePartParser.h>
28 @interface NSObject(UsedProtocols)
29 - (NSString *)displayName; // hh: where is that implemented ?
32 @implementation NGMimeAddressHeaderFieldGenerator
34 static int UseLFSeperatedAddressEntries = -1;
41 NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
43 if (UseLFSeperatedAddressEntries == -1) {
46 if ((o = [ud objectForKey:@"UseLFSeperatedAddressEntries"]))
47 UseLFSeperatedAddressEntries = [o boolValue]?1:0;
49 UseLFSeperatedAddressEntries = 1;
55 - (NSData *)generateDataForHeaderFieldNamed:(NSString *)_headerField
58 NGMailAddressParser *parser;
59 NSMutableString *result;
62 NSEnumerator *enumerator;
64 parser = ([_value isKindOfClass:[NSString class]])
65 ? [NGMailAddressParser mailAddressParserWithString:_value]
66 : [NGMailAddressParser mailAddressParserWithData:_value];
68 enumerator = [[parser parseAddressList] objectEnumerator];
69 result = [[NSMutableString alloc] initWithCapacity:128];
71 while ((obj = [enumerator nextObject])) {
77 if ([result length] > 0) {
78 if (UseLFSeperatedAddressEntries == 1)
79 [result appendString:@",\n "];
81 [result appendString:@", "];
84 tmp = [obj displayName];
85 bufLen = [tmp cStringLength];
87 buffer = calloc(bufLen + 10, sizeof(char));
88 [tmp getCString:buffer];
93 while (cnt < bufLen) {
94 if ((unsigned char)buffer[cnt] > 127) {
102 unsigned char iso[] = "=?iso-8859-15?q?";
103 unsigned isoLen = 16;
104 unsigned char isoEnd[] = "?=";
105 unsigned isoEndLen = 2;
109 if (buffer != NULL) free(buffer); buffer = NULL;
113 #if APPLE_Foundation_LIBRARY || NeXT_Foundation_LIBRARY
114 data = [tmp dataUsingEncoding:NSISOLatin1StringEncoding];
116 data = [tmp dataUsingEncoding:NSISOLatin9StringEncoding];
119 bufLen = [data length];
120 buffer = malloc(bufLen + 10);
121 [data getBytes:buffer]; buffer[bufLen] = '\0';
124 desLen = bufLen * 3 + 20;
125 des = calloc(desLen + 10, sizeof(char));
127 memcpy(des, buffer, cnt);
128 memcpy(des + cnt, iso, isoLen);
130 NGEncodeQuotedPrintableMime((unsigned char *)buffer + cnt, bufLen- cnt,
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)];
139 [self logWithFormat:@"WARNING(%s:%i): An error occour during "
140 @"quoted-printable decoding",
141 __PRETTY_FUNCTION__, __LINE__];
145 if (buffer) free(buffer); buffer = NULL;
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:@">"];
157 else if ((tmp = [(NSHost *)obj address])) {
158 [result appendString:tmp];
162 #if APPLE_Foundation_LIBRARY || NeXT_Foundation_LIBRARY
163 data = [result dataUsingEncoding:NSISOLatin1StringEncoding];
165 data = [result dataUsingEncoding:NSISOLatin9StringEncoding];
172 @end /* NGMimeAddressHeaderFieldGenerator */