]> err.no Git - sope/blob - sope-mime/NGImap4/NGImap4EnvelopeAddress.m
fixed OGo bug #1587 (qp decoding in IMAP4 envelopes)
[sope] / sope-mime / NGImap4 / NGImap4EnvelopeAddress.m
1 /*
2   Copyright (C) 2004-2005 SKYRIX Software AG
3
4   This file is part of SOPE.
5
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
9   later version.
10
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.
15
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
19   02111-1307, USA.
20 */
21
22 #include "NGImap4EnvelopeAddress.h"
23 #include "imCommon.h"
24
25 @implementation NGImap4EnvelopeAddress
26
27 - (id)initWithPersonalName:(NSString *)_pname sourceRoute:(NSString *)_route
28   mailbox:(NSString *)_mbox host:(NSString *)_host
29 {
30   /* Note: we expect NSNull for unset values! */
31   if (_pname == nil || _route == nil || _mbox == nil || _host == nil) {
32     [self release];
33     return nil;
34   }
35   
36   if ((self = [super init])) {
37     if ([_pname isNotNull]) self->personalName = [_pname copy];
38     if ([_route isNotNull]) self->sourceRoute  = [_route copy];
39     if ([_mbox  isNotNull]) self->mailbox      = [_mbox  copy];
40     if ([_host  isNotNull]) self->host         = [_host  copy];
41   }
42   return self;
43 }
44 - (id)init {
45   return [self initWithPersonalName:nil sourceRoute:nil mailbox:nil host:nil];
46 }
47
48 - (id)initWithString:(NSString *)_str {
49   // TODO: properly parse string using NGMailAddressParser
50   return [self initWithPersonalName:nil 
51                sourceRoute:nil 
52                mailbox:_str host:nil];
53 }
54
55 - (id)initWithBodyStructureInfo:(NSDictionary *)_info {
56   if (![_info isNotNull]) {
57     [self release];
58     return nil;
59   }
60   return [self initWithPersonalName:[_info valueForKey:@"personalName"]
61                sourceRoute:[_info valueForKey:@"sourceRoute"]
62                mailbox:[_info valueForKey:@"mailboxName"]
63                host:[_info valueForKey:@"hostName"]];
64 }
65
66 - (void)dealloc {
67   [self->personalName release];
68   [self->sourceRoute  release];
69   [self->mailbox      release];
70   [self->host         release];
71   [super dealloc];
72 }
73
74 /* accessors */
75
76 - (NSString *)personalName {
77   return self->personalName;
78 }
79 - (NSString *)sourceRoute {
80   return self->sourceRoute;
81 }
82 - (NSString *)mailbox {
83   return self->mailbox;
84 }
85 - (NSString *)host {
86   return self->host;
87 }
88
89 /* NSCopying */
90
91 - (id)copyWithZone:(NSZone *)_zone {
92   /* we are immutable */
93   return [self retain];
94 }
95
96 /* derived accessors */
97
98 - (NSString *)baseEMail {
99   NSString *t;
100   
101   if ([self->mailbox length] == 0)
102     return nil;
103   if ([self->host length] == 0)
104     return self->mailbox;
105   
106   t = [self->mailbox stringByAppendingString:@"@"];
107   return [t stringByAppendingString:self->host];
108 }
109 - (NSString *)email {
110   NSString *t;
111   
112   if ([self->personalName length] == 0)
113     return [self baseEMail];
114   if ((t = [self baseEMail]) == nil)
115     return self->personalName;
116   
117   t = [[self->personalName
118             stringByAppendingString:@" <"] stringByAppendingString:t];
119   return [t stringByAppendingString:@">"];
120 }
121
122 /* description */
123
124 - (NSString *)description {
125   NSMutableString *ms;
126   
127   ms = [NSMutableString stringWithCapacity:128];
128   [ms appendFormat:@"<0x%08X[%@]:", self, NSStringFromClass([self class])];
129   
130   if (self->personalName) [ms appendFormat:@" name='%@'", self->personalName];
131   if (self->sourceRoute)  [ms appendFormat:@" source='%@'", self->sourceRoute];
132   if (self->mailbox)      [ms appendFormat:@" mailbox='%@'", self->mailbox];
133   if (self->host)         [ms appendFormat:@" host='%@'",    self->host];
134   
135   [ms appendString:@">"];
136   return ms;
137 }
138
139 @end /* NGImap4EnvelopeAddress */