File: CWInternetAddress.m

package info (click to toggle)
pantomime 1.3.0%2Bdfsg1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster
  • size: 2,332 kB
  • sloc: objc: 22,026; makefile: 11; sh: 4
file content (420 lines) | stat: -rw-r--r-- 9,558 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*
**  CWInternetAddress.m
**
**  Copyright (c) 2001-2007 Ludovic Marcotte
**  Copyright (C) 2017-2018  Riccardo Mottola
**
**  Author: Ludovic Marcotte <ludovic@Sophos.ca>
**          Riccardo Mottola <rm@gnu.org>
**
**  This library is free software; you can redistribute it and/or
**  modify it under the terms of the GNU Lesser General Public
**  License as published by the Free Software Foundation; either
**  version 2.1 of the License, or (at your option) any later version.
**  
**  This library is distributed in the hope that it will be useful,
**  but WITHOUT ANY WARRANTY; without even the implied warranty of
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
**  Lesser General Public License for more details.
**  
** You should have received a copy of the GNU General Public License
** along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#import <Pantomime/CWInternetAddress.h>

#import <Pantomime/CWConstants.h>
#import <Pantomime/CWMIMEUtility.h>
#import <Pantomime/NSString+Extensions.h>

#import <Foundation/Foundation.h>

//
//
//
@implementation CWInternetAddress : NSObject

- (id) initWithString: (NSString *) theString
{
  NSUInteger idxLeftPar;
  NSUInteger idxLeftAngBrk;
  NSUInteger idxLeftAddr;
  
  if (nil == theString || [theString length] == 0)
    {
      AUTORELEASE(self);
      return nil;
    }
  
  self = [super init];
  if (self)
    {
      // Some potential addresses:
      //
      // Ludovic Marcotte <ludovic@Sophos.ca>
      // ludovic@Sophos.ca
      // <ludovic@Sophos.ca>
      // "Marcotte, Ludovic" <ludovic@Sophos.ca>
      // "Joe" User <joe@acme.com>
      // "joe@acme.com (Joe User)"
      // "Joe <joe@acme.com>" <joe@acme.com>

      // Given the case of address inside quote " <xx>" <xx> we search from the back!
      idxLeftAngBrk = [theString indexOfLastCharacter:'<'];
      idxLeftPar = [theString indexOfCharacter: '('];

      if (idxLeftAngBrk != NSNotFound)
	{
	  NSUInteger idxRightAddr;
	
	  idxLeftAddr = idxLeftAngBrk;
	  idxRightAddr = [theString indexOfCharacter: '>'  fromIndex: idxLeftAddr+1];

	  // If the trailing '>' is missing, then just take the rest of the string
	  if (idxRightAddr == NSNotFound)
	    {
	      idxRightAddr = [theString length];
	    }
      
	  [self setAddress: [theString substringWithRange: NSMakeRange(idxLeftAddr+1,idxRightAddr-idxLeftAddr-1)]];
	  
	  if (idxLeftAddr != NSNotFound && idxLeftAddr > 0)
	    {
	      NSString *nameSubStr;
	      NSUInteger idxFirstQuote;

	      nameSubStr = [[theString substringToIndex:idxLeftAddr] stringByTrimmingWhiteSpaces];
	      idxFirstQuote = [nameSubStr indexOfCharacter: '"'];

	      if (idxFirstQuote != NSNotFound)
		{
		  NSRange rangeLastQuote;

		  // check for the last quote
		  rangeLastQuote = [nameSubStr rangeOfCharacterFromSet: [NSCharacterSet characterSetWithCharactersInString:@"\""] options:NSBackwardsSearch range:NSMakeRange(0, [nameSubStr length])];
	      
		  // first check for a sane case
		  if (idxFirstQuote < rangeLastQuote.location)
		    {
		      NSRange rangeLastQuotedQuote;
		      // check for the last quoted quote
		      rangeLastQuotedQuote = [nameSubStr rangeOfCharacterFromSet: [NSCharacterSet characterSetWithCharactersInString:@"\\"""] options:NSBackwardsSearch range:NSMakeRange(0, [nameSubStr length])];

		      // "Marcotte, Ludovic" <ludovic@Sophos.ca>
		      // we unquote only if the first quote is the first character and the last one is not an escaped quote
		      if (idxFirstQuote == 0 && rangeLastQuote.location != rangeLastQuotedQuote.location)
			{
			  nameSubStr = [nameSubStr substringWithRange:NSMakeRange(1, rangeLastQuote.location - 1)];
			}
		    }
	      
		  // un-escape certain characters
		  if ([nameSubStr rangeOfCharacterFromSet: [NSCharacterSet characterSetWithCharactersInString:@"\\()[]"""]].location != NSNotFound)
		    {
		      NSMutableString *mutStr;
                  
		      mutStr = [NSMutableString stringWithString:nameSubStr];
		      [mutStr replaceOccurrencesOfString:@"\\(" withString:@"(" options:0 range:NSMakeRange(0, [mutStr length])];
		      [mutStr replaceOccurrencesOfString:@"\\)" withString:@")" options:0 range:NSMakeRange(0, [mutStr length])];
		      [mutStr replaceOccurrencesOfString:@"\\[" withString:@"]" options:0 range:NSMakeRange(0, [mutStr length])];
		      [mutStr replaceOccurrencesOfString:@"\\[" withString:@"]" options:0 range:NSMakeRange(0, [mutStr length])];
		      [mutStr replaceOccurrencesOfString:@"\\""" withString:@"""" options:0 range:NSMakeRange(0, [mutStr length])];
		      nameSubStr = [NSString stringWithString:mutStr];
		    }
		  [self setPersonal: nameSubStr];
		}
	      else
		{
		  [self setPersonal: [[theString substringWithRange: NSMakeRange(0,idxLeftAddr)]
				       stringByTrimmingWhiteSpaces]];
		}
	    }
	}
      else if (idxLeftPar != NSNotFound)
	{
	  NSUInteger idxRightPar;
	  NSUInteger idxFirstQuote;
	  NSString *addrStr;
      
	  // we may have:
	  // john@doe.com (John Doe (Jonny))
	  // so we check the right parentheses from the end
	  idxRightPar = [theString indexOfLastCharacter: ')'];
	  idxFirstQuote = [theString indexOfCharacter: '"'];

	  addrStr = [theString substringWithRange: NSMakeRange(0, idxLeftPar)];
	  if (idxFirstQuote == 0)
	    addrStr = [addrStr substringFromIndex:1];
      
	  [self setAddress: [addrStr stringByTrimmingWhiteSpaces]];

	  if (idxRightPar != NSNotFound && idxRightPar > idxLeftPar)
	    {
	      NSString *nameStr;
          
	      nameStr = [theString substringWithRange: NSMakeRange(idxLeftPar+1, idxRightPar-idxLeftPar-1)];
	      [self setPersonal: nameStr];
	    }
	  else
	    NSLog(@"Issue decoding personal part from |%@|", theString); 
	} 
      else
	{
	  [self setAddress: theString];
	}
    }
  return self;
}


//
//
//
- (id) initWithPersonal: (NSString *) thePersonal
		address: (NSString *) theAddress
{
  self = [super init];
  if (self)
    {
      [self setPersonal: thePersonal];
      [self setAddress: theAddress];
    }
  return self;
}


//
//
//
- (void) dealloc
{
  RELEASE(_address);
  RELEASE(_personal);
  [super dealloc];
}


//
// NSCoding protocol
//
- (void) encodeWithCoder: (NSCoder *) theCoder
{
  [theCoder encodeObject: [NSNumber numberWithInt: _type]];
  [theCoder encodeObject: _address];
  [theCoder encodeObject: [self personal]];
}

- (id) initWithCoder: (NSCoder *) theCoder
{
  self = [super init];
  if (self)
    {
      [self setType: [[theCoder decodeObject] intValue]];
      [self setAddress: [theCoder decodeObject]];
      [self setPersonal: [theCoder decodeObject]];
    }
  return self;
}


//
//
//
- (NSString *) address
{
  return _address;
}

- (void) setAddress: (NSString *) theAddress
{
  ASSIGN(_address, theAddress);
}


//
//
//
- (NSString *) personal
{
  return _personal;
}

- (void) setPersonal: (NSString *) thePersonal
{
  ASSIGN(_personal, thePersonal);
}

- (NSString *) personalQuoted
{
  NSString *quoted;

  // We verify if we need to quote the name
  if ([_personal indexOfCharacter: ','] != NSNotFound  &&
      ![_personal hasPrefix: @"\""] &&
      ![_personal hasSuffix: @"\""])
    {
      quoted = [NSString stringWithFormat: @"\"%@\"", _personal];
    }
  else
    quoted = [NSString stringWithString:_personal];

  return quoted;
}



//
//
//
- (PantomimeRecipientType) type 
{
  return _type;
}

- (void) setType: (PantomimeRecipientType) theType
{
  _type = theType;
}


//
//
//
- (NSData *) dataValue
{
  if ([self personal] && [[self personal] length] > 0)
    {
      NSMutableData *aMutableData;

      aMutableData = [[NSMutableData alloc] init];

      [aMutableData appendData: [CWMIMEUtility encodeWordUsingQuotedPrintable: [self personal] prefixLength: 0]];

      if (_address)
	{
	  [aMutableData appendBytes: " <"  length: 2];
	  [aMutableData appendData: [_address dataUsingEncoding: NSASCIIStringEncoding]];
	  [aMutableData appendBytes: ">" length: 1];
	}

      return AUTORELEASE(aMutableData);
    }
  else
    {
      return [_address dataUsingEncoding: NSASCIIStringEncoding];
    }
}

//
//
//
- (NSString *) stringValue
{
  if ([self personal] && [[self personal] length] > 0)
    {
      if (_address)
	{
	  return [NSString stringWithFormat: @"%@ <%@>", [self personalQuoted], _address];
	}
      else
	{
	  return [NSString stringWithFormat: @"%@", [self personalQuoted]];
	}
    }
  else
    {
      return _address;
    }
}

//
//
//
- (NSComparisonResult) compare: (id) theAddress
  
{
  return [[self stringValue] compare: [(NSObject *)theAddress valueForKey: @"stringValue"]];
}

//
//
//
- (BOOL) isEqualToAddress: (CWInternetAddress *) theAddress
{
  if (![theAddress isMemberOfClass: [self class]])
    {
      return NO;
    }

  return [_address isEqualToString: [theAddress address]];
}


//
// For debugging support
//
- (NSString *) description
{
  return [self stringValue];
}

//
// For scripting support 
//
- (id) container
{
  return _container;
}

- (void) setContainer: (id) theContainer
{
  _container = theContainer;
}
@end


//
// For scripting support 
//
@implementation ToRecipient

- (id) init
{
  self = [super init];
  [self setType: PantomimeToRecipient];
  return self;
}

@end


//
// For scripting support 
//
@implementation CcRecipient

- (id) init
{
  self = [super init];
  [self setType: PantomimeCcRecipient];
  return self;
}

@end


//
// For scripting support 
//
@implementation BccRecipient

- (id) init
{
  self = [super init];
  [self setType: PantomimeBccRecipient];
  return self;
}


@end