Debian Codegraph @

Framework/Pantomime/CWLocalMessage.m

raw
1 /*
2 ** CWLocalMessage.m
3 **
4 ** Copyright (c) 2001-2007 Ludovic Marcotte
5 ** 2018 Riccardo Mottola
6 **
7 ** Author: Ludovic Marcotte <[email protected]>
8 **
9 ** This library is free software; you can redistribute it and/or
10 ** modify it under the terms of the GNU Lesser General Public
11 ** License as published by the Free Software Foundation; either
12 ** version 2.1 of the License, or (at your option) any later version.
13 **
14 ** This library is distributed in the hope that it will be useful,
15 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 ** Lesser General Public License for more details.
18 **
19 ** You should have received a copy of the GNU General Public License
20 ** along with this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #import <Pantomime/CWLocalMessage.h>
24
25 #import <Pantomime/io.h>
26 #import <Pantomime/CWConstants.h>
27 #import <Pantomime/CWLocalFolder.h>
28 #import <Pantomime/CWLocalStore.h>
29 #import <Pantomime/CWMIMEUtility.h>
30 #import <Pantomime/NSData+Extensions.h>
31 #import <Pantomime/CWParser.h>
32 #import <Pantomime/CWFlags.h>
33
34 #import <Foundation/NSException.h>
35 #import <Foundation/NSValue.h>
36
37 #include <fcntl.h> // O_RDONLY
38 #include <stdlib.h> // free() and malloc()
39 #include <unistd.h> // lseek() and close()
40
41 #ifdef __MINGW32__
42 #include <io.h>
43 #endif
44
45 static int currentLocalMessageVersion = 1;
46
47 //
48 //
49 //
50 @implementation CWLocalMessage
51
52 + (void) initialize
53 {
54 [CWLocalMessage setVersion: currentLocalMessageVersion];
55 }
56
57 - (id) init
58 {
59 self = [super init];
60 if (self)
61 {
62 _mailFilename = nil;
63 _file_position = 0;
64 }
65 return self;
66 }
67
68 - (id) initWithCacheRecord: (cache_record) cr
69 {
70 self = [super init];
71 {
72 _mailFilename = nil;
73
74 [self flags]->flags = cr.flags;
75 [self setReceivedDate: [NSCalendarDate dateWithTimeIntervalSince1970: (NSTimeInterval)cr.date]];
76 _file_position = cr.position;
77 _size = (unsigned long) cr.size;
78
79 [CWParser parseFrom: cr.from inMessage: self quick: YES];
80 [CWParser parseInReplyTo: cr.in_reply_to inMessage: self quick: YES];
81 [CWParser parseMessageID: cr.message_id inMessage: self quick: YES];
82 [CWParser parseReferences: cr.references inMessage: self quick: YES];
83 [CWParser parseSubject: cr.subject inMessage: self quick: YES];
84 [CWParser parseDestination: cr.to
85 forType: PantomimeToRecipient
86 inMessage: self
87 quick: YES];
88 [CWParser parseDestination: cr.cc
89 forType: PantomimeCcRecipient
90 inMessage: self
91 quick: YES];
92 }
93 return self;
94 }
95
96
97
98 //
99 // NSCoding protocol
100 //
101 - (void) encodeWithCoder: (NSCoder *) theCoder
102 {
103 [super encodeWithCoder: theCoder];
104
105 [theCoder encodeObject: [NSNumber numberWithLong: _file_position]];
106
107 // Store the name of the file; we need it for local.
108 [theCoder encodeObject: _mailFilename];
109
110 // Store the message type; useful to have.
111 [theCoder encodeObject: [NSNumber numberWithInt: _type]];
112 }
113
114
115 //
116 //
117 //
118 - (id) initWithCoder: (NSCoder *) theCoder
119 {
120 self = [super initWithCoder: theCoder];
121
122 if (self)
123 {
124 _file_position = [[theCoder decodeObject] longValue];
125
126 // Retrieve the mail file name which we need for local storage.
127 [self setMailFilename: [theCoder decodeObject]];
128
129 // Retrieve the message type
130 _type = (PantomimeFolderFormat)[[theCoder decodeObject] intValue];
131 }
132 return self;
133 }
134
135
136 //
137 // access / mutation methods
138 //
139 - (long int) filePosition
140 {
141 return _file_position;
142 }
143
144 - (void) setFilePosition: (long int) theFilePosition
145 {
146 _file_position = theFilePosition;
147 }
148
149 //
150 //
151 //
152 - (PantomimeFolderFormat) type
153 {
154 return _type;
155 }
156
157 - (void) setType: (PantomimeFolderFormat) theType
158 {
159 _type = theType;
160 }
161
162
163 //
164 //
165 //
166 - (NSString *) mailFilename
167 {
168 return _mailFilename;
169 }
170
171 - (void) setMailFilename: (NSString *) theFilename
172 {
173 ASSIGN(_mailFilename, theFilename);
174 }
175
176
177 //
178 //
179 //
180 - (void) dealloc
181 {
182 TEST_RELEASE(_mailFilename);
183 [super dealloc];
184 }
185
186
187 //
188 //
189 //
190 - (NSData *) rawSource
191 {
192 NSData *aData;
193 char *buf;
194 int fd;
195
196 // If we are reading from a mbox file, the file is already open
197 if ([(CWLocalFolder *)[self folder] type] == PantomimeFormatMbox)
198 {
199 fd = [(CWLocalFolder *)[self folder] fd];
200 }
201 // For maildir, we need to open the specific file
202 else
203 {
204 #ifdef __MINGW32__
205 fd = _open([[NSString stringWithFormat: @"%@/cur/%@", [(CWLocalFolder *)[self folder] path], _mailFilename] UTF8String], O_RDONLY);
206 #else
207 fd = open([[NSString stringWithFormat: @"%@/cur/%@", [(CWLocalFolder *)[self folder] path], _mailFilename] UTF8String], O_RDONLY);
208 #endif
209 }
210
211 if (fd < 0)
212 {
213 NSLog(@"Unable to get the file descriptor");
214 return nil;
215 }
216
217 //NSLog(@"Seeking to %d", [self filePosition]);
218
219 #ifdef __MINGW32__
220 if (_lseek(fd, [self filePosition], SEEK_SET) < 0)
221 #else
222 if (lseek(fd, [self filePosition], SEEK_SET) < 0)
223 #endif
224 {
225 NSLog(@"CWLocalMessage rawSource: Unable to seek to %li", [self filePosition]);
226 return nil;
227 }
228
229 buf = (char *)malloc(_size*sizeof(char));
230
231 if (buf != NULL && read_block(fd, buf, _size) >= 0)
232 {
233 aData = [NSData dataWithBytesNoCopy: buf length: _size freeWhenDone: YES];
234 }
235 else
236 {
237 free(buf);
238 aData = nil;
239 }
240
241 // If we are operating on a local file, close it.
242 if ([(CWLocalFolder *)[self folder] type] == PantomimeFormatMaildir)
243 {
244 safe_close(fd);
245 }
246
247 //NSLog(@"READ |%@|", [aData asciiString]);
248
249 return aData;
250 }
251
252
253 //
254 // This method is called to initialize the message if it wasn't.
255 // If we set it to NO and we HAD a content, we release the content;
256 //
257 - (void) setInitialized: (BOOL) aBOOL
258 {
259 [super setInitialized: aBOOL];
260
261 if (aBOOL)
262 {
263 NSData *aData;
264
265 aData = [self rawSource];
266
267 if (aData)
268 {
269 NSRange aRange;
270
271 aRange = [aData rangeOfCString: "\n\n"];
272
273 if (aRange.length == 0)
274 {
275 [super setInitialized: NO];
276 return;
277 }
278
279 [self setHeadersFromData: [aData subdataWithRange: NSMakeRange(0,aRange.location)]];
280 [CWMIMEUtility setContentFromRawSource:
281 [aData subdataWithRange:
282 NSMakeRange(aRange.location + 2, [aData length]-(aRange.location+2))]
283 inPart: self];
284 }
285 else
286 {
287 [super setInitialized: NO];
288 return;
289 }
290 }
291 else
292 {
293 DESTROY(_content);
294 }
295 }
296
297 @end