Debian Codegraph @

Framework/Pantomime/CWIMAPMessage.m

raw
1 /*
2 ** CWIMAPMessage.m
3 **
4 ** Copyright (c) 2001-2007 Ludovic Marcotte
5 ** Copyright (C) 2014-2020 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/CWIMAPMessage.h>
25 #import <Pantomime/CWParser.h>
26 #import <Pantomime/CWConstants.h>
27 #import <Pantomime/CWFlags.h>
28 #import <Pantomime/CWIMAPFolder.h>
29 #import <Pantomime/CWIMAPStore.h>
30
31 #import <Foundation/NSDebug.h>
32 #import <Foundation/NSException.h>
33 #import <Foundation/NSValue.h>
34
35 #if defined(__APPLE__) && (MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_4)
36 @interface NSNumber (TigerExtensions)
37 + (NSNumber *)numberWithUnsignedInteger:(NSUInteger)value;
38 - (NSUInteger)unsignedIntegerValue;
39 @end
40
41 @implementation NSNumber (TigerExtensions)
42 + (NSNumber *)numberWithUnsignedInteger:(NSUInteger)value
43 {
44 return [NSNumber numberWithUnsignedInt:(unsigned int)value];
45 }
46 - (NSUInteger)unsignedIntegerValue
47 {
48 return (NSUInteger)[self unsignedIntValue];
49 }
50 @end
51 #endif
52
53
54 //
55 //
56 //
57 @implementation CWIMAPMessage
58
59 - (id) init
60 {
61 self = [super init];
62 if (self)
63 {
64 _headers_were_prefetched = NO;
65 _UID = 0;
66 }
67 return self;
68 }
69
70 - (id) initWithCacheRecord: (cache_record) cr
71 {
72 self = [super init];
73 if (self)
74 {
75 _headers_were_prefetched = NO;
76
77 [self flags]->flags = cr.flags; // FASTER and _RIGHT_ since we can't call -setFlags: on CWIMAPMessage
78 [self setReceivedDate: [NSCalendarDate dateWithTimeIntervalSince1970: (NSTimeInterval)cr.date]];
79 _UID = cr.imap_uid;
80 _size = (unsigned long) cr.size;
81
82 [CWParser parseFrom: cr.from inMessage: self quick: YES];
83 [CWParser parseInReplyTo: cr.in_reply_to inMessage: self quick: YES];
84 [CWParser parseMessageID: cr.message_id inMessage: self quick: YES];
85 [CWParser parseReferences: cr.references inMessage: self quick: YES];
86 [CWParser parseSubject: cr.subject inMessage: self quick: YES];
87 [CWParser parseDestination: cr.to
88 forType: PantomimeToRecipient
89 inMessage: self
90 quick: YES];
91 [CWParser parseDestination: cr.cc
92 forType: PantomimeCcRecipient
93 inMessage: self
94 quick: YES];
95 }
96 return self;
97 }
98
99 //
100 // NSCoding protocol
101 //
102 - (void) encodeWithCoder: (NSCoder *) theCoder
103 {
104 // Must also encode Message's superclass
105 [super encodeWithCoder: theCoder];
106 [theCoder encodeObject: [NSNumber numberWithUnsignedInteger: _UID]];
107 }
108
109
110 //
111 //
112 //
113 - (id) initWithCoder: (NSCoder *) theCoder
114 {
115 // Must also decode Message's superclass
116 self = [super initWithCoder: theCoder];
117 if (self)
118 {
119 _UID = [[theCoder decodeObject] unsignedIntegerValue];
120 }
121 return self;
122 }
123
124
125 //
126 //
127 //
128 - (NSUInteger) UID
129 {
130 return _UID;
131 }
132
133 - (void) setUID: (NSUInteger) theUID
134 {
135 _UID = theUID;
136 }
137
138
139 //
140 // This method is called to initialize the message if it wasn't.
141 // If we set it to NO and we HAD a content, we release the content.
142 //
143 - (void) setInitialized: (BOOL) theBOOL
144 {
145 [super setInitialized: theBOOL];
146
147 if (!theBOOL)
148 {
149 DESTROY(_content);
150 return;
151 }
152 else if (![(CWIMAPFolder *)[self folder] selected])
153 {
154 [super setInitialized: NO];
155 [NSException raise: PantomimeProtocolException
156 format: @"Unable to fetch message content from unselected mailbox."];
157 return;
158 }
159
160 if (!_content)
161 {
162 id aStore;
163
164 aStore = [(CWIMAPFolder *)[self folder] store];
165
166 if (!_headers_were_prefetched)
167 {
168 [aStore sendCommand: IMAP_UID_FETCH_HEADER_FIELDS_NOT info: nil arguments: @"UID FETCH %u:%u BODY.PEEK[HEADER.FIELDS.NOT (From To Cc Subject Date Message-ID References In-Reply-To)]", _UID, _UID];
169 }
170
171 // If we are no longer connected to the IMAP server, we don't send the 2nd command.
172 // This will prevent us from calling the delegate method twice (the one that handles
173 // the disconnection from the IMAP server).
174 if ([aStore isConnected])
175 {
176 [aStore sendCommand: IMAP_UID_FETCH_BODY_TEXT info: nil arguments: @"UID FETCH %u:%u BODY[TEXT]", _UID, _UID];
177 }
178
179 // Since we are loading asynchronously our message, it's not yet initialized. It'll be set as an initialized one
180 // in CWIMAPStore once the body is fully loaded.
181 [super setInitialized: NO];
182 }
183
184 _headers_were_prefetched = YES;
185 }
186
187
188 //
189 //
190 //
191 - (NSData *) rawSource
192 {
193 if (![(CWIMAPFolder *)[self folder] selected])
194 {
195 [NSException raise: PantomimeProtocolException
196 format: @"Unable to fetch message data from unselected mailbox."];
197 return _rawSource;
198 }
199
200 if (!_rawSource)
201 {
202 [(CWIMAPStore *)[[self folder] store] sendCommand: IMAP_UID_FETCH_RFC822 info: nil arguments: @"UID FETCH %u:%u RFC822", _UID, _UID];
203 }
204
205 return _rawSource;
206 }
207
208
209 //
210 //
211 //
212 - (void) setFlags: (CWFlags *) theFlags
213 {
214 [[self folder] setFlags: theFlags
215 messages: [NSArray arrayWithObject: self]];
216 }
217
218 @end