| 1 |
/* |
| 2 |
** CWURLName.m |
| 3 |
** |
| 4 |
** Copyright (c) 2001-2004 Ludovic Marcotte |
| 5 |
** |
| 6 |
** Author: Ludovic Marcotte <[email protected]> |
| 7 |
** |
| 8 |
** This library is free software; you can redistribute it and/or |
| 9 |
** modify it under the terms of the GNU Lesser General Public |
| 10 |
** License as published by the Free Software Foundation; either |
| 11 |
** version 2.1 of the License, or (at your option) any later version. |
| 12 |
** |
| 13 |
** This library is distributed in the hope that it will be useful, |
| 14 |
** but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
** Lesser General Public License for more details. |
| 17 |
** |
| 18 |
** You should have received a copy of the GNU General Public License |
| 19 |
** along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
|
| 22 |
#import <Pantomime/CWURLName.h> |
| 23 |
|
| 24 |
#import <Pantomime/CWConstants.h> |
| 25 |
#import <Foundation/NSPathUtilities.h> |
| 26 |
|
| 27 |
// |
| 28 |
// Private methods |
| 29 |
// |
| 30 |
@interface CWURLName (Private) |
| 31 |
|
| 32 |
- (void) _decodeIMAP: (NSString *) theString; |
| 33 |
- (void) _decodeLocal: (NSString *) theString; |
| 34 |
- (void) _decodePOP3: (NSString *) theString; |
| 35 |
- (void) _decodeURL: (NSString *) theString; |
| 36 |
|
| 37 |
@end |
| 38 |
|
| 39 |
|
| 40 |
// |
| 41 |
// |
| 42 |
// |
| 43 |
@implementation CWURLName |
| 44 |
|
| 45 |
- (id) initWithString: (NSString *) theString |
| 46 |
{ |
| 47 |
return [self initWithString: theString path: nil]; |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
// |
| 52 |
// |
| 53 |
// |
| 54 |
- (id) initWithString: (NSString *) theString |
| 55 |
path: (NSString *) thePath |
| 56 |
{ |
| 57 |
self = [super init]; |
| 58 |
if (self) |
| 59 |
{ |
| 60 |
// We initialize our ivars |
| 61 |
_protocol = nil; |
| 62 |
_foldername = nil; |
| 63 |
_host = nil; |
| 64 |
_port = 0; |
| 65 |
_username = nil; |
| 66 |
_password = nil; |
| 67 |
|
| 68 |
_path = thePath; |
| 69 |
|
| 70 |
if (_path) |
| 71 |
{ |
| 72 |
RETAIN(_path); |
| 73 |
} |
| 74 |
|
| 75 |
// We now decode our URL |
| 76 |
[self _decodeURL: theString]; |
| 77 |
} |
| 78 |
return self; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
// |
| 83 |
// |
| 84 |
// |
| 85 |
- (void) dealloc |
| 86 |
{ |
| 87 |
TEST_RELEASE(_protocol); |
| 88 |
TEST_RELEASE(_foldername); |
| 89 |
TEST_RELEASE(_path); |
| 90 |
TEST_RELEASE(_host); |
| 91 |
TEST_RELEASE(_username); |
| 92 |
TEST_RELEASE(_password); |
| 93 |
|
| 94 |
[super dealloc]; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
// |
| 99 |
// access/mutation methods |
| 100 |
// |
| 101 |
- (NSString *) protocol |
| 102 |
{ |
| 103 |
return _protocol; |
| 104 |
} |
| 105 |
|
| 106 |
- (NSString *) foldername |
| 107 |
{ |
| 108 |
return _foldername; |
| 109 |
} |
| 110 |
|
| 111 |
- (NSString *) path |
| 112 |
{ |
| 113 |
return _path; |
| 114 |
} |
| 115 |
|
| 116 |
- (NSString *) host; |
| 117 |
{ |
| 118 |
return _host; |
| 119 |
} |
| 120 |
|
| 121 |
- (unsigned int) port |
| 122 |
{ |
| 123 |
return _port; |
| 124 |
} |
| 125 |
|
| 126 |
- (NSString *) username |
| 127 |
{ |
| 128 |
return _username; |
| 129 |
} |
| 130 |
|
| 131 |
- (NSString *) password |
| 132 |
{ |
| 133 |
return _password; |
| 134 |
} |
| 135 |
|
| 136 |
- (NSString *) description |
| 137 |
{ |
| 138 |
return [NSString stringWithFormat: @"protocol = (%@), foldername = (%@), path = (%@), host = (%@), port = (%d), username = (%@), password = (%@)", |
| 139 |
_protocol, _foldername, _path, _host, _port, _username, _password]; |
| 140 |
} |
| 141 |
|
| 142 |
- (NSString *) stringValue |
| 143 |
{ |
| 144 |
if ([_protocol caseInsensitiveCompare: @"LOCAL"] == NSOrderedSame) |
| 145 |
{ |
| 146 |
return [NSString stringWithFormat: @"local://%@/%@", _path, _foldername]; |
| 147 |
} |
| 148 |
else if ([_protocol caseInsensitiveCompare: @"IMAP"] == NSOrderedSame) |
| 149 |
{ |
| 150 |
return [NSString stringWithFormat: @"imap://%@@%@/%@", _username, _host, _foldername]; |
| 151 |
} |
| 152 |
else |
| 153 |
{ |
| 154 |
return [NSString stringWithFormat: @"pop3://%@@%@", _username, _host]; |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
@end |
| 159 |
|
| 160 |
|
| 161 |
// |
| 162 |
// Private methods |
| 163 |
// |
| 164 |
@implementation CWURLName (Private) |
| 165 |
|
| 166 |
//// FIXME (finish!) |
| 167 |
// imap://<iserver>/<foldername> |
| 168 |
// |
| 169 |
// Examples: imap://minbari.org/gray-council;UIDVALIDITY=385759045/;UID=20 |
| 170 |
// imap://[email protected]/users.*;type=list |
| 171 |
// imap://psicorp.org/~peter/%E6%97%A5%E6%9C%AC%E8%AA%9E/ |
| 172 |
// imap://;[email protected]/gray-council/;uid=20/;section=1.2 |
| 173 |
// imap://;AUTH=*@minbari.org/gray%20council?SUBJECT%20shadows |
| 174 |
// |
| 175 |
// Note: The imap:// part isn't present in the received string as parameter. |
| 176 |
// |
| 177 |
- (void) _decodeIMAP: (NSString *) theString |
| 178 |
{ |
| 179 |
NSRange r1, r2; |
| 180 |
|
| 181 |
// We decode the username |
| 182 |
r1 = [theString rangeOfString: @"@" |
| 183 |
options: NSBackwardsSearch]; |
| 184 |
|
| 185 |
if (r1.length) |
| 186 |
{ |
| 187 |
_username = [theString substringToIndex: r1.location]; |
| 188 |
RETAIN(_username); |
| 189 |
} |
| 190 |
else |
| 191 |
{ |
| 192 |
r1.location = 0; |
| 193 |
} |
| 194 |
|
| 195 |
r2 = [theString rangeOfString: @"/" |
| 196 |
options: 0 |
| 197 |
range: NSMakeRange(r1.location, [theString length] - r1.location)]; |
| 198 |
|
| 199 |
if (r1.length) |
| 200 |
{ |
| 201 |
_host = [theString substringWithRange: NSMakeRange(r1.location + 1, r2.location - r1.location - 1)]; |
| 202 |
} |
| 203 |
else |
| 204 |
{ |
| 205 |
_host = [theString substringWithRange: NSMakeRange(r1.location, r2.location - r1.location)]; |
| 206 |
} |
| 207 |
|
| 208 |
RETAIN(_host); |
| 209 |
|
| 210 |
_foldername = [theString substringFromIndex: (r2.location + 1)]; |
| 211 |
RETAIN(_foldername); |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
// |
| 216 |
// local://<path>/<foldername> (full path) |
| 217 |
// |
| 218 |
// Note: The local:// part isn't present in the received string as parameter. |
| 219 |
// |
| 220 |
- (void) _decodeLocal: (NSString *) theString |
| 221 |
{ |
| 222 |
// If localMailDirectoryPath is nil, we return the last path component |
| 223 |
// of the URL as the foldername. |
| 224 |
if (!_path) |
| 225 |
{ |
| 226 |
_foldername = [theString lastPathComponent]; |
| 227 |
RETAIN(_foldername); |
| 228 |
|
| 229 |
_path = [theString substringToIndex: ([theString length] - [_foldername length])]; |
| 230 |
RETAIN(_path); |
| 231 |
} |
| 232 |
else |
| 233 |
{ |
| 234 |
_foldername = [theString substringFromIndex: ([_path length] + 1)]; |
| 235 |
RETAIN(_foldername); |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
// |
| 241 |
// FIXME (finish!) |
| 242 |
// pop://<user>;auth=<auth>@<host>:<port> |
| 243 |
// |
| 244 |
// Examples: pop://[email protected] |
| 245 |
// pop://rg;[email protected]:8110 |
| 246 |
// pop://baz;[email protected] |
| 247 |
// |
| 248 |
// Note: The pop:// part isn't present in the received string as parameter. |
| 249 |
// |
| 250 |
- (void) _decodePOP3: (NSString *) theString |
| 251 |
{ |
| 252 |
NSRange aRange; |
| 253 |
|
| 254 |
_foldername = [[NSString alloc] initWithString: @"INBOX"]; |
| 255 |
|
| 256 |
aRange = [theString rangeOfString: @"@"]; |
| 257 |
|
| 258 |
_username = [theString substringToIndex: aRange.location]; |
| 259 |
RETAIN(_username); |
| 260 |
|
| 261 |
_host = [theString substringFromIndex: (aRange.location + 1)]; |
| 262 |
RETAIN(_host); |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
// |
| 267 |
// |
| 268 |
// |
| 269 |
- (void) _decodeURL: (NSString *) theString |
| 270 |
{ |
| 271 |
NSRange aRange; |
| 272 |
|
| 273 |
// We first decode our protocol. |
| 274 |
aRange = [theString rangeOfString: @"://"]; |
| 275 |
|
| 276 |
if (aRange.length) |
| 277 |
{ |
| 278 |
NSString *aString; |
| 279 |
|
| 280 |
_protocol = [theString substringToIndex: aRange.location]; |
| 281 |
RETAIN(_protocol); |
| 282 |
|
| 283 |
aString = [theString substringFromIndex: (aRange.location + aRange.length)]; |
| 284 |
|
| 285 |
if ([_protocol caseInsensitiveCompare: @"LOCAL"] == NSOrderedSame) |
| 286 |
{ |
| 287 |
[self _decodeLocal: aString]; |
| 288 |
} |
| 289 |
else if ([_protocol caseInsensitiveCompare: @"POP3"] == NSOrderedSame) |
| 290 |
{ |
| 291 |
[self _decodePOP3: aString]; |
| 292 |
} |
| 293 |
else if ([_protocol caseInsensitiveCompare: @"IMAP"] == NSOrderedSame) |
| 294 |
{ |
| 295 |
[self _decodeIMAP: aString]; |
| 296 |
} |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
@end |