/
SOGoMailBaseObject.m
358 lines (290 loc) · 9.14 KB
/
SOGoMailBaseObject.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
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
/*
Copyright (C) 2007-2014 Inverse inc.
Copyright (C) 2004-2005 SKYRIX Software AG
This file is part of SOGo.
SOGo 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, or (at your option) any
later version.
SOGo 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 Lesser General Public
License along with OGo; see the file COPYING. If not, write to the
Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.
*/
#import <NGObjWeb/SoObject+SoDAV.h>
#import <NGObjWeb/WOContext+SoObjects.h>
#import <NGExtensions/NSNull+misc.h>
#import <NGExtensions/NSObject+Logs.h>
#import <NGExtensions/NSString+misc.h>
#import <NGExtensions/NSURL+misc.h>
#import <NGImap4/NGImap4Connection.h>
#import <NGImap4/NGImap4Client.h>
#import <SOGo/SOGoCache.h>
#import <SOGo/SOGoUser.h>
#import <SOGo/WORequest+SOGo.h>
#import "SOGoMailAccount.h"
#import "SOGoMailManager.h"
@implementation SOGoMailBaseObject
- (id) init
{
if ((self = [super init]))
{
NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
// With this to YES, imap4Connection will raise exception if a working
// connection cannot be provided
imap4ExceptionsEnabled = [ud boolForKey:@"SoIMAP4ExceptionsEnabled"];
}
return self;
}
- (id) initWithImap4URL: (NSURL *) _url
inContainer: (id) _container
{
NSString *n;
n = [[_url path] lastPathComponent];
if ((self = [self initWithName: n inContainer:_container]))
{
imap4URL = [_url retain];
}
return self;
}
- (void) dealloc
{
[imap4URL release];
[imap4 release];
[super dealloc];
}
- (BOOL) isFolderish
{
return YES;
}
/* hierarchy */
- (SOGoMailAccount *) mailAccountFolder
{
SOGoMailAccount *folder;
if ([container respondsToSelector: _cmd])
folder = [container mailAccountFolder];
else
{
[self warnWithFormat: @"weird container of mailfolder: %@",
container];
folder = nil;
}
return folder;
}
- (SOGoMailAccounts *) mailAccountsFolder
{
id o;
SOGoMailAccounts *folder;
Class folderClass;
folder = nil;
folderClass = NSClassFromString (@"SOGoMailAccounts");
o = container;
while (!folder && [o isNotNull])
if ([o isKindOfClass: folderClass])
folder = o;
else
o = [o container];
return o;
}
- (BOOL) isInDraftsFolder
{
return [container isInDraftsFolder];
}
/* IMAP4 */
- (NGImap4ConnectionManager *) mailManager
{
return [NGImap4ConnectionManager defaultConnectionManager];
}
- (NGImap4Connection *) _createIMAP4Connection
{
NGImap4ConnectionManager *manager;
NGImap4Connection *newConnection;
NSString *password;
NSHost *host;
[self imap4URL];
// We first check if we're trying to establish an IMAP connection to localhost
// for an account number greater than 0 (default account). We prevent that
// for security reasons if admins use an IMAP trust.
host = [NSHost hostWithName: [[self imap4URL] host]];
if (![[[self mailAccountFolder] nameInContainer] isEqualToString: @"0"] &&
[[host address] isEqualToString: @"127.0.0.1"])
{
[self errorWithFormat: @"Trying to use localhost for additional IMAP account - aborting."];
return nil;
}
manager = [self mailManager];
password = [self imap4PasswordRenewed: NO];
if (password)
{
newConnection = [manager connectionForURL: imap4URL
password: password];
if (!newConnection)
{
[self logWithFormat: @"renewing imap4 password"];
password = [self imap4PasswordRenewed: YES];
if (password)
newConnection = [manager connectionForURL: imap4URL
password: password];
}
}
else
newConnection = nil;
if (!newConnection)
{
newConnection = (NGImap4Connection *) [NSNull null];
if (imap4ExceptionsEnabled)
[NSException raise: @"IOException" format: @"IMAP connection failed"];
else
[self errorWithFormat:@"Could not connect IMAP4"];
}
else
{
// If the server has the ID capability (RFC 2971), we set the x-originating-ip
// accordingly for the IMAP connection.
NSString *remoteHost;
remoteHost = [[context request] headerForKey: @"x-webobjects-remote-host"];
if (remoteHost)
{
if ([[[[newConnection client] capability] objectForKey: @"capability"] containsObject: @"id"])
{
[[newConnection client] processCommand: [NSString stringWithFormat: @"ID (\"x-originating-ip\" \"%@\")", remoteHost]];
}
}
}
return newConnection;
}
- (NGImap4Connection *) imap4Connection
{
NSString *cacheKey, *login;
SOGoCache *sogoCache;
if (!imap4)
{
sogoCache = [SOGoCache sharedCache];
// The cacheKey *MUST* be prefixed by the username here as
// the cache is shared across OpenChange users and not necessarily
// flushed between requests. This could lead us to using the wrong
// IMAP connection.
//
// We also have a HACK in case self's context doesn't exist, we
// use the container's one.
//
login = [[[self context] activeUser] login];
if (!login)
login = [[[[self container] context] activeUser] login];
cacheKey = [NSString stringWithFormat: @"%@+%@",
login,
[[self mailAccountFolder] nameInContainer]];
imap4 = [sogoCache imap4ConnectionForKey: cacheKey];
if (!imap4)
{
imap4 = [self _createIMAP4Connection];
[sogoCache registerIMAP4Connection: imap4
forKey: cacheKey];
}
[imap4 retain];
}
// Connection broken, try to reconnect
if (![imap4 isKindOfClass: [NSNull class]] && ![[[imap4 client] isConnected] boolValue])
{
[self warnWithFormat: @"IMAP connection is broken, trying to reconnect..."];
[[imap4 client] reconnect];
// Still broken, give up
if (![[[imap4 client] isConnected] boolValue])
{
if (imap4ExceptionsEnabled)
[NSException raise: @"IOException" format: @"IMAP connection failed"];
else
[self errorWithFormat: @"Could not get a valid IMAP connection"];
}
}
return [imap4 isKindOfClass: [NSNull class]] ? nil : imap4;
}
- (NSString *) relativeImap4Name
{
[self subclassResponsibility: _cmd];
return nil;
}
- (NSURL *) baseImap4URL
{
NSURL *url;
if ([container respondsToSelector: @selector(imap4URL)])
url = [container imap4URL];
else
{
[self warnWithFormat:@"container does not implement -imap4URL!"];
url = nil;
}
return url;
}
- (NSMutableString *) imap4URLString
{
NSMutableString *urlString;
NSString *imap4Name;
urlString = [container imap4URLString];
imap4Name = [[self relativeImap4Name] stringByEscapingURL];
[urlString appendFormat: @"%@", imap4Name];
return urlString;
}
- (NSURL *) imap4URL
{
SOGoMailAccount *account;
NSString *urlString;
/* this could probably be handled better from NSURL but it's buggy in
GNUstep */
if (!imap4URL)
{
account = [self mailAccountFolder];
if ([[account encryption] isEqualToString: @"tls"])
urlString = [NSString stringWithFormat: @"%@?tls=YES",
[self imap4URLString]];
else
urlString = [self imap4URLString];
imap4URL = [[NSURL alloc] initWithString: urlString];
}
return imap4URL;
}
- (NSString *) imap4PasswordRenewed: (BOOL) renewed
{
return [[self mailAccountFolder] imap4PasswordRenewed: renewed];
}
- (NSMutableString *) traversalFromMailAccount
{
NSMutableString *currentTraversal;
currentTraversal = [container traversalFromMailAccount];
if ([container isKindOfClass: [SOGoMailAccount class]])
[currentTraversal appendString: [self relativeImap4Name]];
else
[currentTraversal appendFormat: @"/%@", [self relativeImap4Name]];
return currentTraversal;
}
- (void)flushMailCaches {
[[self mailManager] flushCachesForURL:[self imap4URL]];
}
/* IMAP4 names */
#warning we could improve this by simply testing if the reference is the filename of an attachment or if the body part mentionned actually exists in the list of body parts. Another way is to use a prefix such as "attachment-*" to declare attachments.
- (BOOL) isBodyPartKey: (NSString *) key
{
NSString *trimmedKey;
trimmedKey = [key stringByTrimmingCharactersInSet:
[NSCharacterSet decimalDigitCharacterSet]];
return (![trimmedKey length]);
}
- (NSArray *) aclsForUser: (NSString *) uid
{
return nil;
}
- (int) IMAP4IDFromAppendResult: (NSDictionary *) result
{
NSDictionary *results;
NSString *flag, *newIdString;
results = [[result objectForKey: @"RawResponse"]
objectForKey: @"ResponseResult"];
flag = [results objectForKey: @"flag"];
newIdString = [[flag componentsSeparatedByString: @" "] objectAtIndex: 2];
return [newIdString intValue];
}
@end /* SOGoMailBaseObject */