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