Debian Codegraph @

Framework/Pantomime/CWLocalFolder+maildir.m

raw
1 /*
2 ** CWLocalFolder+maildir.m
3 **
4 ** Copyright (c) 2004-2007 Ludovic Marcotte
5 ** Copyright (C) 2017-2022 Riccardo Mottola
6 **
7 ** Author: Ludovic Marcotte <[email protected]>
8 ** Riccardo Mottola <[email protected]>
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/CWLocalFolder+maildir.h>
25
26 #import <Pantomime/CWFlags.h>
27 #import <Pantomime/CWLocalCacheManager.h>
28 #import <Pantomime/CWLocalFolder+mbox.h>
29 #import <Pantomime/CWLocalMessage.h>
30 #import <Pantomime/CWLocalStore.h>
31 #import <Pantomime/NSString+Extensions.h>
32
33 #import <Foundation/NSFileManager.h>
34 #import <Foundation/NSNotification.h>
35
36 //
37 // The maildir format is well documented here:
38 //
39 // http://www.qmail.org/man/man5/maildir.html
40 // http://cr.yp.to/proto/maildir.html
41 //
42 @implementation CWLocalFolder (maildir)
43
44 - (void) expunge_maildir
45 {
46 NSMutableArray *aMutableArray;
47 CWLocalMessage *aMessage;
48 CWFlags *theFlags;
49 NSUInteger count, i;
50 unsigned int msn;
51
52 aMutableArray = AUTORELEASE([[NSMutableArray alloc] init]);
53 count = [_allMessages count];
54
55 // We assume that our write operation was successful and we initialize our msn to 1
56 msn = 1;
57
58 for (i = 0; i < count; i++)
59 {
60 aMessage = [_allMessages objectAtIndex: i];
61
62 theFlags = [aMessage flags];
63
64 if ([theFlags contain: PantomimeDeleted])
65 {
66 [[NSFileManager defaultManager] removeFileAtPath: [NSString stringWithFormat: @"%@/cur/%@", [self path], [aMessage mailFilename]]
67 handler: nil];
68 [aMutableArray addObject: aMessage];
69 }
70 else
71 {
72 // rewrite the message to account for changes in the flags
73 NSString *uniquePattern, *newFileName;
74 NSUInteger indexOfPatternSeparator;
75
76 // We update our message's ivars (folder and size don't change)
77 [aMessage setMessageNumber: msn];
78 msn++;
79
80 // we rename the message according to the maildir spec by appending the status information the name
81 // name of file will be unique_pattern:info with the status flags in the info field
82 indexOfPatternSeparator = [[aMessage mailFilename] indexOfCharacter: ':'];
83
84 if (indexOfPatternSeparator != NSNotFound && indexOfPatternSeparator > 1)
85 {
86 uniquePattern = [[aMessage mailFilename] substringToIndex: indexOfPatternSeparator];
87 }
88 else
89 {
90 uniquePattern = [aMessage mailFilename];
91 }
92
93 // We build the new file name
94 newFileName = [NSString stringWithFormat: @"%@:%@", uniquePattern, [theFlags maildirString]];
95
96 // We rename the message file
97 if ([[NSFileManager defaultManager] movePath: [NSString stringWithFormat: @"%@/cur/%@", [self path], [aMessage mailFilename]]
98 toPath: [NSString stringWithFormat: @"%@/cur/%@", [self path], newFileName]
99 handler: nil])
100 {
101 [aMessage setMailFilename: newFileName];
102 }
103 }
104 }
105
106 // We sync our cache
107 if (_cacheManager) [_cacheManager expunge];
108 [_allMessages removeObjectsInArray: aMutableArray];
109
110 #warning also return when invoking the delegate
111 POST_NOTIFICATION(PantomimeFolderExpungeCompleted, self, nil);
112 PERFORM_SELECTOR_2([[self store] delegate], @selector(folderExpungeCompleted:), PantomimeFolderExpungeCompleted, self, @"Folder");
113 }
114
115
116 //
117 // This parses a local structure for messages by looking in the "cur" and "new" sub-directories.
118 //
119 - (void) parse_maildir: (NSString *) theDirectory all: (BOOL) theBOOL
120 {
121 NSString *aPath, *aNewPath, *thisMailFile;
122 NSFileManager *aFileManager;
123 NSMutableArray *allFiles;
124 FILE *aStream;
125 NSUInteger i, count;
126 BOOL b;
127
128 if (!theDirectory)
129 {
130 return;
131 }
132
133 // We check if we must later move the file after
134 // parsing it.
135 b = NO;
136
137 if ([theDirectory isEqualToString: @"new"] || [theDirectory isEqualToString: @"tmp"])
138 {
139 b = YES;
140 }
141
142 aFileManager = [NSFileManager defaultManager];
143
144 // Read the directory
145 aPath = [NSString stringWithFormat: @"%@/%@", _path, theDirectory];
146 allFiles = [[NSMutableArray alloc] initWithArray: [aFileManager directoryContentsAtPath: aPath]];
147
148 // We remove Apple Mac OS X .DS_Store file
149 [allFiles removeObject: @".DS_Store"];
150 // We remove GWorkspace .gwdir file
151 [allFiles removeObject: @".gwdir"];
152 count = [allFiles count];
153
154 aNewPath = nil;
155 if (allFiles != nil && count > 0)
156 {
157 for (i = 0; i < count; i++)
158 {
159 thisMailFile = [NSString stringWithFormat: @"%@/%@", aPath, [allFiles objectAtIndex: i]];
160
161 if (b)
162 {
163 aNewPath = [NSString stringWithFormat: @"%@/cur/%@", _path, [allFiles objectAtIndex: i]];
164 }
165
166 #ifdef __MINGW32__
167 aStream = fopen([thisMailFile UTF8String], "rb");
168 #else
169 aStream = fopen([thisMailFile UTF8String], "r");
170 #endif
171
172 if (!aStream)
173 {
174 continue;
175 }
176
177 [self parse_mbox: (b ? aNewPath : thisMailFile) stream: aStream flags: nil all: theBOOL];
178
179 fclose(aStream);
180
181 // If we read this from the "new" or "tmp" sub-directories,
182 // move it to the "cur" directory
183 if (b)
184 {
185 [aFileManager movePath: thisMailFile toPath: aNewPath handler: nil];
186 }
187 }
188
189 [_cacheManager synchronize];
190 }
191
192 RELEASE(allFiles);
193 }
194
195 @end