Debian Codegraph @

Framework/Pantomime/CWIMAPFolder.m

raw
1 /*
2 ** CWIMAPFolder.m
3 **
4 ** Copyright (c) 2001-2007 Ludovic Marcotte
5 ** Copyright (C) 2020 Riccardo Mottola
6 **
7 ** Authors: Ludovic Marcotte <[email protected]>
8 ** Riccardo Mottola
9 **
10 ** This library is free software; you can redistribute it and/or
11 ** modify it under the terms of the GNU Lesser General Public
12 ** License as published by the Free Software Foundation; either
13 ** version 2.1 of the License, or (at your option) any later version.
14 **
15 ** This library is distributed in the hope that it will be useful,
16 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ** Lesser General Public License for more details.
19 **
20 ** You should have received a copy of the GNU General Public License
21 ** along with this program. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 #import <Pantomime/CWIMAPFolder.h>
25
26 #import <Pantomime/CWConnection.h>
27 #import <Pantomime/CWConstants.h>
28 #import <Pantomime/CWFlags.h>
29 #import <Pantomime/CWIMAPCacheManager.h>
30 #import <Pantomime/CWIMAPStore.h>
31 #import <Pantomime/CWIMAPMessage.h>
32 #import <Pantomime/CWTCPConnection.h>
33 #import <Pantomime/NSData+Extensions.h>
34 #import <Pantomime/NSString+Extensions.h>
35
36 //
37 // Private methods
38 //
39 @interface CWIMAPFolder (Private)
40 - (NSString *) _flagsAsStringFromFlags: (CWFlags *) theFlags;
41 - (NSData *) _removeInvalidHeadersFromMessage: (NSData *) theMessage;
42 @end
43
44
45 //
46 //
47 //
48 @implementation CWIMAPFolder
49
50 - (id) initWithName: (NSString *) theName
51 {
52 self = [super initWithName: theName];
53 if (self)
54 {
55 [self setSelected: NO];
56 }
57 return self;
58 }
59
60
61 //
62 //
63 //
64 - (id) initWithName: (NSString *) theName
65 mode: (PantomimeFolderMode) theMode
66 {
67 self = [self initWithName: theName];
68 if (self)
69 {
70 _mode = theMode;
71 }
72 return self;
73 }
74
75
76 //
77 //
78 //
79 - (void) appendMessageFromRawSource: (NSData *) theData
80 flags: (CWFlags *) theFlags
81 {
82 [self appendMessageFromRawSource: theData
83 flags: theFlags
84 internalDate: nil];
85 }
86
87 //
88 //
89 //
90 - (void) appendMessageFromRawSource: (NSData *) theData
91 flags: (CWFlags *) theFlags
92 internalDate: (NSCalendarDate *) theDate
93 {
94 NSDictionary *aDictionary;
95 NSString *flagsAsString;
96 NSData *aData;
97
98 if (theFlags)
99 {
100 flagsAsString = [self _flagsAsStringFromFlags: theFlags];
101 }
102 else
103 {
104 flagsAsString = @"";
105 }
106
107 // We remove any invalid headers from our message
108 aData = [self _removeInvalidHeadersFromMessage: theData];
109
110 if (theFlags)
111 {
112 aDictionary = [NSDictionary dictionaryWithObjectsAndKeys: aData, @"NSDataToAppend", theData, @"NSData", self, @"Folder", theFlags, @"Flags", nil];
113 }
114 else
115 {
116 aDictionary = [NSDictionary dictionaryWithObjectsAndKeys: aData, @"NSDataToAppend", theData, @"NSData", self, @"Folder", nil];
117 }
118
119
120 if (theDate)
121 {
122 [_store sendCommand: IMAP_APPEND
123 info: aDictionary
124 arguments: @"APPEND \"%@\" (%@) \"%@\" {%d}", // IMAP command
125 [_name modifiedUTF7String], // folder name
126 flagsAsString, // flags
127 [theDate descriptionWithCalendarFormat:@"%d-%b-%Y %H:%M:%S %z"], // internal date
128 [aData length]]; // length of the data to write
129 }
130 else
131 {
132 [_store sendCommand: IMAP_APPEND
133 info: aDictionary
134 arguments: @"APPEND \"%@\" (%@) {%d}", // IMAP command
135 [_name modifiedUTF7String], // folder name
136 flagsAsString, // flags
137 [aData length]]; // length of the data to write
138 }
139 }
140
141 //
142 //
143 //
144 - (void) copyMessages: (NSArray *) theMessages
145 toFolder: (NSString *) theFolder
146 {
147 NSMutableString *aMutableString;
148 NSUInteger i, count;
149
150 // We create our message's UID set
151 aMutableString = [[NSMutableString alloc] init];
152 count = [theMessages count];
153
154 for (i = 0; i < count; i++)
155 {
156 if (i == count-1)
157 {
158 [aMutableString appendFormat: @"%lu",
159 (unsigned long)[[theMessages objectAtIndex: i] UID]];
160 }
161 else
162 {
163 [aMutableString appendFormat: @"%lu,",
164 (unsigned long)[[theMessages objectAtIndex: i] UID]];
165 }
166 }
167
168 // We send our IMAP command
169 [_store sendCommand: IMAP_UID_COPY
170 info: [NSDictionary dictionaryWithObjectsAndKeys: theMessages, @"Messages", theFolder, @"Name", self, @"Folder", nil]
171 arguments: @"UID COPY %@ \"%@\"",
172 aMutableString,
173 [theFolder modifiedUTF7String]];
174
175 RELEASE(aMutableString);
176 }
177
178
179 //
180 //
181 //
182 - (void) prefetch
183 {
184 // We first update the messages in our cache, if we need to.
185 if (_cacheManager && [self countVisible])
186 {
187 [_store sendCommand: IMAP_UID_SEARCH info: nil arguments: @"UID SEARCH 1:*"];
188 }
189 else
190 {
191 //
192 // We must send this command since our IMAP cache might be empty (or have been removed).
193 // In that case, we much fetch again all messages, starting at UID 1.
194 //
195 [_store sendCommand: IMAP_UID_FETCH_HEADER_FIELDS info: nil arguments: @"UID FETCH %u:* (UID FLAGS RFC822.SIZE BODY.PEEK[HEADER.FIELDS (From To Cc Subject Date Message-ID References In-Reply-To)])", 1];
196 }
197 }
198
199
200 //
201 // This method simply close the selected mailbox (ie. folder)
202 //
203 - (void) close
204 {
205 IMAPCommand theCommand;
206
207 if (![self selected])
208 {
209 [_store removeFolderFromOpenFolders: self];
210 return;
211 }
212
213 // If we are opening a mailbox but -close was called before we
214 // finished opening it, we close the connection immediately.
215 theCommand = [[self store] lastCommand];
216
217 if (theCommand == IMAP_SELECT || theCommand == IMAP_UID_SEARCH || theCommand == IMAP_UID_SEARCH_ANSWERED ||
218 theCommand == IMAP_UID_SEARCH_FLAGGED || theCommand == IMAP_UID_SEARCH_UNSEEN)
219 {
220 [_store removeFolderFromOpenFolders: self];
221 [[self store] cancelRequest];
222 [[self store] reconnect];
223 return;
224 }
225
226 if (_cacheManager)
227 {
228 [_cacheManager synchronize];
229 }
230
231 // We set the _folder ivar to nil for all messages. This is required in case
232 // an IMAPMessage instance was retained and we invoke -setFlags: on it, which
233 // will try to access the _folder ivar in order to communicate with the IMAP server.
234 [_allMessages makeObjectsPerformSelector: @selector(setFolder:) withObject: nil];
235
236 // We close the selected IMAP folder to _expunge_ messages marked as \Deleted
237 // if and only we are NOT showing DELETED messages. We also don't send the command
238 // if we are NOT connected since a MUA using Pantomime needs to call -close
239 // on IMAPFolder to clean-up the "open" folder.
240 if ([_store isConnected] && ![self showDeleted])
241 {
242 [_store sendCommand: IMAP_CLOSE
243 info: [NSDictionary dictionaryWithObject: self forKey: @"Folder"]
244 arguments: @"CLOSE"];
245 }
246 else
247 {
248 PERFORM_SELECTOR_2([_store delegate], @selector(folderCloseCompleted:), PantomimeFolderCloseCompleted, self, @"Folder");
249 POST_NOTIFICATION(PantomimeFolderCloseCompleted, _store, [NSDictionary dictionaryWithObject: self forKey: @"Folder"]);
250 }
251
252 [_store removeFolderFromOpenFolders: self];
253 }
254
255
256 //
257 // This method returns all messages that have the flag PantomimeDeleted.
258 //
259 - (void) expunge
260 {
261 //
262 // We send our EXPUNGE command. The responses will be processed in IMAPStore and
263 // the MSN will be updated in IMAPStore: -_parseExpunge.
264 //
265 [_store sendCommand: IMAP_EXPUNGE info: nil arguments: @"EXPUNGE"];
266 }
267
268
269 //
270 //
271 //
272 - (unsigned int) UIDValidity
273 {
274 return _uid_validity;
275 }
276
277
278 //
279 //
280 //
281 - (void) setUIDValidity: (unsigned int) theUIDValidity
282 {
283 _uid_validity = theUIDValidity;
284
285 if (_cacheManager)
286 {
287 if ([(CWIMAPCacheManager *)_cacheManager UIDValidity] == 0 || [(CWIMAPCacheManager *)_cacheManager UIDValidity] != _uid_validity)
288 {
289 [_cacheManager invalidate];
290 [(CWIMAPCacheManager *)_cacheManager setUIDValidity: _uid_validity];
291 }
292 }
293 }
294
295
296 //
297 //
298 //
299 - (BOOL) selected
300 {
301 return _selected;
302 }
303
304
305 //
306 //
307 //
308 - (void) setSelected: (BOOL) theBOOL
309 {
310 _selected = theBOOL;
311 }
312
313 //
314 //
315 //
316 - (void) setFlags: (CWFlags *) theFlags
317 messages: (NSArray *) theMessages
318 {
319 NSMutableString *aMutableString, *aSequenceSet;
320 CWIMAPMessage *aMessage;
321
322 if ([theMessages count] == 1)
323 {
324 aMessage = [theMessages lastObject];
325 // We set the flags right away, just in case someone asks for them
326 // just after invoking this method. Nevertheless, they WILL be set
327 // in IMAPStore: -_parseOK:.
328 // We do the same below, when the count > 1
329 [[aMessage flags] replaceWithFlags: theFlags];
330 aSequenceSet = [NSMutableString stringWithFormat: @"%lu:%lu",
331 (unsigned long)[aMessage UID], (unsigned long)[aMessage UID]];
332 }
333 else
334 {
335 NSUInteger i, count;
336
337 aSequenceSet = AUTORELEASE([[NSMutableString alloc] init]);
338 count = [theMessages count];
339
340 for (i = 0; i < count; i++)
341 {
342 aMessage = [theMessages objectAtIndex: i];
343 [[aMessage flags] replaceWithFlags: theFlags];
344
345 if (aMessage == [theMessages lastObject])
346 {
347 [aSequenceSet appendFormat: @"%lu", (unsigned long)[aMessage UID]];
348 }
349 else
350 {
351 [aSequenceSet appendFormat: @"%lu,", (unsigned long)[aMessage UID]];
352 }
353 }
354 }
355
356 aMutableString = [[NSMutableString alloc] init];
357
358 //
359 // If we're removing all flags, we rather send a STORE -FLAGS (<current flags>)
360 // than a STORE FLAGS (<new flags>) since some broken servers might not
361 // support it (like Cyrus v1.5.19 and v1.6.24).
362 //
363 if (theFlags->flags == 0)
364 {
365 [aMutableString appendFormat: @"UID STORE %@ -FLAGS.SILENT (", aSequenceSet];
366 [aMutableString appendString: [self _flagsAsStringFromFlags: theFlags]];
367 [aMutableString appendString: @")"];
368 }
369 else
370 {
371 [aMutableString appendFormat: @"UID STORE %@ FLAGS.SILENT (", aSequenceSet];
372 [aMutableString appendString: [self _flagsAsStringFromFlags: theFlags]];
373 [aMutableString appendString: @")"];
374 }
375
376 [_store sendCommand: IMAP_UID_STORE
377 info: [NSDictionary dictionaryWithObjectsAndKeys: theMessages, @"Messages", theFlags, @"Flags", nil]
378 arguments: aMutableString];
379 RELEASE(aMutableString);
380 }
381
382
383
384 //
385 // Using IMAP, we ignore most parameters.
386 //
387 - (void) search: (NSString *) theString
388 mask: (PantomimeSearchMask) theMask
389 options: (PantomimeSearchOption) theOptions
390 {
391 NSString *aString;
392
393 switch (theMask)
394 {
395 case PantomimeFrom:
396 aString = [NSString stringWithFormat: @"UID SEARCH ALL FROM \"%@\"", theString];
397 break;
398
399 case PantomimeTo:
400 aString = [NSString stringWithFormat: @"UID SEARCH ALL TO \"%@\"", theString];
401 break;
402
403 case PantomimeContent:
404 aString = [NSString stringWithFormat: @"UID SEARCH ALL BODY \"%@\"", theString];
405 break;
406
407 case PantomimeSubject:
408 default:
409 aString = [NSString stringWithFormat: @"UID SEARCH ALL SUBJECT \"%@\"", theString];
410 }
411
412 // We send our SEARCH command. Store->searchResponse will have the result.
413 [_store sendCommand: IMAP_UID_SEARCH_ALL info: [NSDictionary dictionaryWithObject: self forKey: @"Folder"] arguments: aString];
414 }
415
416 @end
417
418
419 //
420 // Private methods
421 //
422 @implementation CWIMAPFolder (Private)
423
424 - (NSString *) _flagsAsStringFromFlags: (CWFlags *) theFlags
425 {
426 NSMutableString *aMutableString;
427
428 aMutableString = [[NSMutableString alloc] init];
429 AUTORELEASE(aMutableString);
430
431 if ([theFlags contain: PantomimeAnswered])
432 {
433 [aMutableString appendString: @"\\Answered "];
434 }
435
436 if ([theFlags contain: PantomimeDraft] )
437 {
438 [aMutableString appendString: @"\\Draft "];
439 }
440
441 if ([theFlags contain: PantomimeFlagged])
442 {
443 [aMutableString appendString: @"\\Flagged "];
444 }
445
446 if ([theFlags contain: PantomimeSeen])
447 {
448 [aMutableString appendString: @"\\Seen "];
449 }
450
451 if ([theFlags contain: PantomimeDeleted])
452 {
453 [aMutableString appendString: @"\\Deleted "];
454 }
455
456 return [aMutableString stringByTrimmingWhiteSpaces];
457 }
458
459
460 //
461 //
462 //
463 - (NSData *) _removeInvalidHeadersFromMessage: (NSData *) theMessage
464 {
465 NSMutableData *aMutableData;
466 NSArray *allLines;
467 NSUInteger i, count;
468
469 // We allocate our mutable data object
470 aMutableData = [[NSMutableData alloc] initWithCapacity: [theMessage length]];
471
472 // We now replace all \n by \r\n
473 allLines = [theMessage componentsSeparatedByCString: "\n"];
474 count = [allLines count];
475
476 for (i = 0; i < count; i++)
477 {
478 NSData *aLine;
479
480 // We get a line...
481 aLine = [allLines objectAtIndex: i];
482
483 // We skip dumb headers
484 if ([aLine hasCPrefix: "From "])
485 {
486 continue;
487 }
488
489 [aMutableData appendData: aLine];
490 [aMutableData appendCString: "\r\n"];
491 }
492
493 return AUTORELEASE(aMutableData);
494 }
495
496 @end
497