-
Notifications
You must be signed in to change notification settings - Fork 1
/
ETMailAccount.m
219 lines (176 loc) · 4.73 KB
/
ETMailAccount.m
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
/*
Copyright (C) 2009 Eric Wasylishen
Author: Eric Wasylishen <[email protected]>
Date: September 2009
License: Modified BSD (see COPYING)
*/
#import "ETMailAccount.h"
#import "ETMailFolder.h"
#import "ETMailMessage.h"
@implementation ETMailAccount
- (id) init
{
SUPERINIT;
_properties = [[NSMutableDictionary alloc] init];
_folders = [[NSMutableDictionary alloc] init];
return self;
}
- (NSString *)displayName
{
return [NSString stringWithFormat: @"Mail Account %@@%@",
[_properties valueForKey: @"username"],
[_properties valueForKey: @"server"]];
}
DEALLOC(DESTROY(_properties);DESTROY(_folders);)
- (void) reconnect
{
[_service release];
if (nil == [self valueForProperty: @"server"])
{
return;
}
// TODO: Guess the server type (pop/imap), and retry if incorrect
NSLog(@"Creating a service connection to %@", [self valueForProperty: @"server"]);
_service = [[CWIMAPStore alloc] initWithName: [self valueForProperty: @"server"]
port: 993];
[_service setDelegate: self];
[_service connectInBackgroundAndNotify];
NSLog(@"Told %@ to connect. del %@", _service, [_service delegate] );
}
/* Pantomime delegate methods */
- (void) connectionEstablished: (NSNotification *) theNotification
{
NSLog(@"Connected!");
NSLog(@"Now starting SSL...");
[(CWTCPConnection *)[_service connection] startSSL];
}
- (void) connectionLost: (NSNotification *) theNotification
{
NSLog(@"Connection lost");
}
- (void) connectionTimedOut: (NSNotification *) theNotification
{
NSLog(@"timeout");
}
- (void) connectionTerminated: (NSNotification *) theNotification
{
NSLog(@"Connection closed.");
RELEASE(_service);
}
- (void) serviceInitialized: (NSNotification *) theNotification
{
NSLog(@"SSL handshaking completed.");
NSLog(@"Available authentication mechanisms: %@", [_service supportedMechanisms]);
[_service authenticate: [self valueForProperty: @"username"]
password: [self valueForProperty: @"password"]
mechanism: @""];
}
- (void) authenticationFailed: (NSNotification *) theNotification
{
NSLog(@"Authentication failed! Closing the connection...");
[_service close];
}
- (void) authenticationCompleted: (NSNotification *) theNotification
{
NSLog(@"Authentication completed! Enumerating folders...");
// This will return nil and later trigger folderListCompleted:
[_service folderEnumerator];
}
- (void) folderPrefetchCompleted: (NSNotification *) theNotification
{
NSLog(@"Folder prefetch complete (%@)", theNotification );
CWFolder *folder = [[theNotification userInfo] valueForKey: @"Folder"];
NSLog(@"Folder %@, contents: %@", folder, [folder allMessages]);
ETMailFolder *mailFolder = [_folders valueForKey: [folder name]];
[mailFolder loadContentsOfCWFolder: folder];
[[NSApp delegate] reload];
}
- (void) messagePrefetchCompleted: (NSNotification *) theNotification
{
NSLog(@"Message prefetch complete (%@)", theNotification);
}
- (void) service: (CWService *) theService receivedData: (NSData *) theData
{
//NSLog(@" Got %@", theData);
}
- (void) service: (CWService *) theService sentData: (NSData *) theData
{
// NSLog(@"sent %@", theData);
}
- (void) requestCancelled: (NSNotification *) theNotification
{
NSLog(@"req cancelled. %@", theNotification);
}
- (void) serviceReconnected: (NSNotification *) theNotification
{
NSLog(@"Service reconnected.");
}
- (void) folderListCompleted: (NSNotification *) theNotification
{
NSLog(@"Folder list completed. Reloading..");
NSEnumerator *enumerator = [_service folderEnumerator];
NSLog(@"Got folder enumerator: %@", enumerator);
FOREACHE(nil, folderName, NSString *, enumerator)
{
NSLog(@"found folder %@", folderName);
[_folders setObject: [ETMailFolder folderWithName: folderName service: _service]
forKey: folderName];
}
[[NSApp delegate] reload];
}
/* ETPropertyValueCoding */
- (NSArray *) properties
{
return [A(@"username", @"password", @"server")
arrayByAddingObjectsFromArray: [super properties]];
}
- (id) valueForProperty: (NSString *)key
{
if ([A(@"username", @"password", @"server") containsObject: key])
{
return [_properties valueForKey: key];
}
else
{
return [super valueForProperty: key];
}
}
- (BOOL) setValue: (id)value forProperty: (NSString *)key
{
if ([A(@"username", @"password", @"server") containsObject: key])
{
[_properties setValue: value forKey: key];
[self reconnect];
return YES;
}
else
{
return [super setValue: value forProperty: key];
}
}
/* ETCollection */
- (BOOL) isOrdered
{
return YES;
}
- (BOOL) isEmpty
{
return [_folders count] == 0;
}
- (id) content
{
return [_folders allValues];
}
- (NSArray *) contentArray
{
return [self content];
}
- (NSEnumerator *) objectEnumerator
{
return [[self content] objectEnumerator];
}
- (NSUInteger) count
{
return [_folders count];
}
@end