Debian Codegraph @

Framework/Pantomime/CWInternetAddress.m

raw
1 /*
2 ** CWInternetAddress.m
3 **
4 ** Copyright (c) 2001-2007 Ludovic Marcotte
5 ** Copyright (C) 2017-2018 Riccardo Mottola
6 **
7 ** Author: Ludovic Marcotte <[email protected]>
8 ** Riccardo Mottola <[email protected]>
9 **
10 ** This library is free software; you can redistribute it and/or
11 ** modify it under the terms of the GNU Lesser General Public
12 ** License as published by the Free Software Foundation; either
13 ** version 2.1 of the License, or (at your option) any later version.
14 **
15 ** This library is distributed in the hope that it will be useful,
16 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ** Lesser General Public License for more details.
19 **
20 ** You should have received a copy of the GNU General Public License
21 ** along with this program. If not, see <http://www.gnu.org/licenses/>.
22 */
23 #import <Pantomime/CWInternetAddress.h>
24
25 #import <Pantomime/CWConstants.h>
26 #import <Pantomime/CWMIMEUtility.h>
27 #import <Pantomime/NSString+Extensions.h>
28
29 #import <Foundation/Foundation.h>
30
31 //
32 //
33 //
34 @implementation CWInternetAddress : NSObject
35
36 - (id) initWithString: (NSString *) theString
37 {
38 NSUInteger idxLeftPar;
39 NSUInteger idxLeftAngBrk;
40 NSUInteger idxLeftAddr;
41
42 if (nil == theString || [theString length] == 0)
43 {
44 AUTORELEASE(self);
45 return nil;
46 }
47
48 self = [super init];
49 if (self)
50 {
51 // Some potential addresses:
52 //
53 // Ludovic Marcotte <[email protected]>
54 // [email protected]
55 // <[email protected]>
56 // "Marcotte, Ludovic" <[email protected]>
57 // "Joe" User <[email protected]>
58 // "[email protected] (Joe User)"
59 // "Joe <[email protected]>" <[email protected]>
60
61 // Given the case of address inside quote " <xx>" <xx> we search from the back!
62 idxLeftAngBrk = [theString indexOfLastCharacter:'<'];
63 idxLeftPar = [theString indexOfCharacter: '('];
64
65 if (idxLeftAngBrk != NSNotFound)
66 {
67 NSUInteger idxRightAddr;
68
69 idxLeftAddr = idxLeftAngBrk;
70 idxRightAddr = [theString indexOfCharacter: '>' fromIndex: idxLeftAddr+1];
71
72 // If the trailing '>' is missing, then just take the rest of the string
73 if (idxRightAddr == NSNotFound)
74 {
75 idxRightAddr = [theString length];
76 }
77
78 [self setAddress: [theString substringWithRange: NSMakeRange(idxLeftAddr+1,idxRightAddr-idxLeftAddr-1)]];
79
80 if (idxLeftAddr != NSNotFound && idxLeftAddr > 0)
81 {
82 NSString *nameSubStr;
83 NSUInteger idxFirstQuote;
84
85 nameSubStr = [[theString substringToIndex:idxLeftAddr] stringByTrimmingWhiteSpaces];
86 idxFirstQuote = [nameSubStr indexOfCharacter: '"'];
87
88 if (idxFirstQuote != NSNotFound)
89 {
90 NSRange rangeLastQuote;
91
92 // check for the last quote
93 rangeLastQuote = [nameSubStr rangeOfCharacterFromSet: [NSCharacterSet characterSetWithCharactersInString:@"\""] options:NSBackwardsSearch range:NSMakeRange(0, [nameSubStr length])];
94
95 // first check for a sane case
96 if (idxFirstQuote < rangeLastQuote.location)
97 {
98 NSRange rangeLastQuotedQuote;
99 // check for the last quoted quote
100 rangeLastQuotedQuote = [nameSubStr rangeOfCharacterFromSet: [NSCharacterSet characterSetWithCharactersInString:@"\\"""] options:NSBackwardsSearch range:NSMakeRange(0, [nameSubStr length])];
101
102 // "Marcotte, Ludovic" <[email protected]>
103 // we unquote only if the first quote is the first character and the last one is not an escaped quote
104 if (idxFirstQuote == 0 && rangeLastQuote.location != rangeLastQuotedQuote.location)
105 {
106 nameSubStr = [nameSubStr substringWithRange:NSMakeRange(1, rangeLastQuote.location - 1)];
107 }
108 }
109
110 // un-escape certain characters
111 if ([nameSubStr rangeOfCharacterFromSet: [NSCharacterSet characterSetWithCharactersInString:@"\\()[]"""]].location != NSNotFound)
112 {
113 NSMutableString *mutStr;
114
115 mutStr = [NSMutableString stringWithString:nameSubStr];
116 [mutStr replaceOccurrencesOfString:@"\\(" withString:@"(" options:0 range:NSMakeRange(0, [mutStr length])];
117 [mutStr replaceOccurrencesOfString:@"\\)" withString:@")" options:0 range:NSMakeRange(0, [mutStr length])];
118 [mutStr replaceOccurrencesOfString:@"\\[" withString:@"]" options:0 range:NSMakeRange(0, [mutStr length])];
119 [mutStr replaceOccurrencesOfString:@"\\[" withString:@"]" options:0 range:NSMakeRange(0, [mutStr length])];
120 [mutStr replaceOccurrencesOfString:@"\\""" withString:@"""" options:0 range:NSMakeRange(0, [mutStr length])];
121 nameSubStr = [NSString stringWithString:mutStr];
122 }
123 [self setPersonal: nameSubStr];
124 }
125 else
126 {
127 [self setPersonal: [[theString substringWithRange: NSMakeRange(0,idxLeftAddr)]
128 stringByTrimmingWhiteSpaces]];
129 }
130 }
131 }
132 else if (idxLeftPar != NSNotFound)
133 {
134 NSUInteger idxRightPar;
135 NSUInteger idxFirstQuote;
136 NSString *addrStr;
137
138 // we may have:
139 // [email protected] (John Doe (Jonny))
140 // so we check the right parentheses from the end
141 idxRightPar = [theString indexOfLastCharacter: ')'];
142 idxFirstQuote = [theString indexOfCharacter: '"'];
143
144 addrStr = [theString substringWithRange: NSMakeRange(0, idxLeftPar)];
145 if (idxFirstQuote == 0)
146 addrStr = [addrStr substringFromIndex:1];
147
148 [self setAddress: [addrStr stringByTrimmingWhiteSpaces]];
149
150 if (idxRightPar != NSNotFound && idxRightPar > idxLeftPar)
151 {
152 NSString *nameStr;
153
154 nameStr = [theString substringWithRange: NSMakeRange(idxLeftPar+1, idxRightPar-idxLeftPar-1)];
155 [self setPersonal: nameStr];
156 }
157 else
158 NSLog(@"Issue decoding personal part from |%@|", theString);
159 }
160 else
161 {
162 [self setAddress: theString];
163 }
164 }
165 return self;
166 }
167
168
169 //
170 //
171 //
172 - (id) initWithPersonal: (NSString *) thePersonal
173 address: (NSString *) theAddress
174 {
175 self = [super init];
176 if (self)
177 {
178 [self setPersonal: thePersonal];
179 [self setAddress: theAddress];
180 }
181 return self;
182 }
183
184
185 //
186 //
187 //
188 - (void) dealloc
189 {
190 RELEASE(_address);
191 RELEASE(_personal);
192 [super dealloc];
193 }
194
195
196 //
197 // NSCoding protocol
198 //
199 - (void) encodeWithCoder: (NSCoder *) theCoder
200 {
201 [theCoder encodeObject: [NSNumber numberWithInt: _type]];
202 [theCoder encodeObject: _address];
203 [theCoder encodeObject: [self personal]];
204 }
205
206 - (id) initWithCoder: (NSCoder *) theCoder
207 {
208 self = [super init];
209 if (self)
210 {
211 [self setType: [[theCoder decodeObject] intValue]];
212 [self setAddress: [theCoder decodeObject]];
213 [self setPersonal: [theCoder decodeObject]];
214 }
215 return self;
216 }
217
218
219 //
220 //
221 //
222 - (NSString *) address
223 {
224 return _address;
225 }
226
227 - (void) setAddress: (NSString *) theAddress
228 {
229 ASSIGN(_address, theAddress);
230 }
231
232
233 //
234 //
235 //
236 - (NSString *) personal
237 {
238 return _personal;
239 }
240
241 - (void) setPersonal: (NSString *) thePersonal
242 {
243 ASSIGN(_personal, thePersonal);
244 }
245
246 - (NSString *) personalQuoted
247 {
248 NSString *quoted;
249
250 // We verify if we need to quote the name
251 if ([_personal indexOfCharacter: ','] != NSNotFound &&
252 ![_personal hasPrefix: @"\""] &&
253 ![_personal hasSuffix: @"\""])
254 {
255 quoted = [NSString stringWithFormat: @"\"%@\"", _personal];
256 }
257 else
258 quoted = [NSString stringWithString:_personal];
259
260 return quoted;
261 }
262
263
264
265 //
266 //
267 //
268 - (PantomimeRecipientType) type
269 {
270 return _type;
271 }
272
273 - (void) setType: (PantomimeRecipientType) theType
274 {
275 _type = theType;
276 }
277
278
279 //
280 //
281 //
282 - (NSData *) dataValue
283 {
284 if ([self personal] && [[self personal] length] > 0)
285 {
286 NSMutableData *aMutableData;
287
288 aMutableData = [[NSMutableData alloc] init];
289
290 [aMutableData appendData: [CWMIMEUtility encodeWordUsingQuotedPrintable: [self personal] prefixLength: 0]];
291
292 if (_address)
293 {
294 [aMutableData appendBytes: " <" length: 2];
295 [aMutableData appendData: [_address dataUsingEncoding: NSASCIIStringEncoding]];
296 [aMutableData appendBytes: ">" length: 1];
297 }
298
299 return AUTORELEASE(aMutableData);
300 }
301 else
302 {
303 return [_address dataUsingEncoding: NSASCIIStringEncoding];
304 }
305 }
306
307 //
308 //
309 //
310 - (NSString *) stringValue
311 {
312 if ([self personal] && [[self personal] length] > 0)
313 {
314 if (_address)
315 {
316 return [NSString stringWithFormat: @"%@ <%@>", [self personalQuoted], _address];
317 }
318 else
319 {
320 return [NSString stringWithFormat: @"%@", [self personalQuoted]];
321 }
322 }
323 else
324 {
325 return _address;
326 }
327 }
328
329 //
330 //
331 //
332 - (NSComparisonResult) compare: (id) theAddress
333
334 {
335 return [[self stringValue] compare: [(NSObject *)theAddress valueForKey: @"stringValue"]];
336 }
337
338 //
339 //
340 //
341 - (BOOL) isEqualToAddress: (CWInternetAddress *) theAddress
342 {
343 if (![theAddress isMemberOfClass: [self class]])
344 {
345 return NO;
346 }
347
348 return [_address isEqualToString: [theAddress address]];
349 }
350
351
352 //
353 // For debugging support
354 //
355 - (NSString *) description
356 {
357 return [self stringValue];
358 }
359
360 //
361 // For scripting support
362 //
363 - (id) container
364 {
365 return _container;
366 }
367
368 - (void) setContainer: (id) theContainer
369 {
370 _container = theContainer;
371 }
372 @end
373
374
375 //
376 // For scripting support
377 //
378 @implementation ToRecipient
379
380 - (id) init
381 {
382 self = [super init];
383 [self setType: PantomimeToRecipient];
384 return self;
385 }
386
387 @end
388
389
390 //
391 // For scripting support
392 //
393 @implementation CcRecipient
394
395 - (id) init
396 {
397 self = [super init];
398 [self setType: PantomimeCcRecipient];
399 return self;
400 }
401
402 @end
403
404
405 //
406 // For scripting support
407 //
408 @implementation BccRecipient
409
410 - (id) init
411 {
412 self = [super init];
413 [self setType: PantomimeBccRecipient];
414 return self;
415 }
416
417
418 @end
419
420