2 Copyright (C) 2000-2004 SKYRIX Software AG
4 This file is part of OpenGroupware.org.
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
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.
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
21 // $Id: NGMimeAddressHeaderFieldGenerator.m 1 2004-08-20 10:08:27Z znek $
23 #include "NGMimeHeaderFieldGenerator.h"
24 #include "NGMimeHeaderFields.h"
25 #include <NGMail/NGMailAddressParser.h>
26 #include <NGMime/NGMimePartParser.h>
29 @interface NSObject(UsedProtocols)
30 - (NSString *)displayName; // hh: where is that implemented ?
33 @implementation NGMimeAddressHeaderFieldGenerator
35 static int UseLFSeperatedAddressEntries = -1;
42 NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
44 if (UseLFSeperatedAddressEntries == -1) {
47 if ((o = [ud objectForKey:@"UseLFSeperatedAddressEntries"]))
48 UseLFSeperatedAddressEntries = [o boolValue]?1:0;
50 UseLFSeperatedAddressEntries = 1;
56 - (NSData *)generateDataForHeaderFieldNamed:(NSString *)_headerField
59 NGMailAddressParser *parser;
60 NSMutableString *result;
63 NSEnumerator *enumerator;
65 parser = ([_value isKindOfClass:[NSString class]])
66 ? [NGMailAddressParser mailAddressParserWithString:_value]
67 : [NGMailAddressParser mailAddressParserWithData:_value];
69 enumerator = [[parser parseAddressList] objectEnumerator];
70 result = [[NSMutableString alloc] initWithCapacity:128];
72 while ((obj = [enumerator nextObject])) {
78 if ([result length] > 0) {
79 if (UseLFSeperatedAddressEntries == 1)
80 [result appendString:@",\n "];
82 [result appendString:@", "];
85 tmp = [obj displayName];
86 bufLen = [tmp cStringLength];
88 buffer = calloc(bufLen + 10, sizeof(char));
89 [tmp getCString:buffer];
94 while (cnt < bufLen) {
95 if ((unsigned char)buffer[cnt] > 127) {
103 unsigned char iso[] = "=?iso-8859-15?q?";
104 unsigned isoLen = 16;
105 unsigned char isoEnd[] = "?=";
106 unsigned isoEndLen = 2;
110 if (buffer) free(buffer);
114 #if APPLE_Foundation_LIBRARY || NeXT_Foundation_LIBRARY
115 data = [tmp dataUsingEncoding:NSISOLatin1StringEncoding];
117 data = [tmp dataUsingEncoding:NSISOLatin9StringEncoding];
120 bufLen = [data length];
121 buffer = malloc(bufLen + 10);
122 [data getBytes:buffer]; buffer[bufLen] = '\0';
125 desLen = bufLen * 3 + 20;
126 des = calloc(desLen + 10, sizeof(char));
128 memcpy(des, buffer, cnt);
129 memcpy(des + cnt, iso, isoLen);
131 NGEncodeQuotedPrintableMime(buffer + cnt, bufLen - cnt,
133 desLen - cnt - isoLen);
134 if ((int)desLen != -1) {
135 memcpy(des + cnt + isoLen + desLen, isoEnd, isoEndLen);
136 tmp = [NSString stringWithCString:des
137 length:(cnt + isoLen + desLen + isoEndLen)];
140 [self logWithFormat:@"WARNING(%s:%i): An error occour during "
141 @"quoted-printable decoding",
142 __PRETTY_FUNCTION__, __LINE__];
146 if (buffer) free(buffer); buffer = NULL;
148 if ([tmp length] > 0) {
149 [result appendString:@"\""];
150 [result appendString:tmp];
151 [result appendString:@"\""];
152 if ((tmp = [(NSHost *)obj address])) {
153 [result appendString:@" <"];
154 [result appendString:tmp];
155 [result appendString:@">"];
158 else if ((tmp = [(NSHost *)obj address])) {
159 [result appendString:tmp];
163 #if APPLE_Foundation_LIBRARY || NeXT_Foundation_LIBRARY
164 data = [result dataUsingEncoding:NSISOLatin1StringEncoding];
166 data = [result dataUsingEncoding:NSISOLatin9StringEncoding];
173 @end /* NGMimeAddressHeaderFieldGenerator */