File: CWIMAPMessage.m

package info (click to toggle)
pantomime 1.3.0%2Bdfsg1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster
  • size: 2,332 kB
  • sloc: objc: 22,026; makefile: 11; sh: 4
file content (199 lines) | stat: -rw-r--r-- 5,208 bytes parent folder | download
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
/*
**  CWIMAPMessage.m
**
**  Copyright (c) 2001-2007 Ludovic Marcotte
**  Copyright (C) 2014-2018 Riccardo Mottola
**
**  Author: Ludovic Marcotte <ludovic@Sophos.ca>
**          Riccardo Mottola <rm@gnu.org>
**
**  This library 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.1 of the License, or (at your option) any later version.
**  
**  This library 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 General Public License
** along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#import <Pantomime/CWIMAPMessage.h>
#import <Pantomime/CWParser.h>
#import <Pantomime/CWConstants.h>
#import <Pantomime/CWFlags.h>
#import <Pantomime/CWIMAPFolder.h>
#import <Pantomime/CWIMAPStore.h>

#import <Foundation/NSDebug.h>
#import <Foundation/NSException.h>
#import <Foundation/NSValue.h>

//
//
//
@implementation CWIMAPMessage 

- (id) init
{
  self = [super init];
  if (self)
    {
      _headers_were_prefetched = NO;
      _UID = 0;
    }
  return self;
}

- (id) initWithCacheRecord: (cache_record) cr
{
  self = [super init];
  if (self)
    {
      _headers_were_prefetched = NO;
      
      [self flags]->flags = cr.flags;  // FASTER and _RIGHT_ since we can't call -setFlags: on CWIMAPMessage
      [self setReceivedDate: [NSCalendarDate dateWithTimeIntervalSince1970: (NSTimeInterval)cr.date]];
      _UID = cr.imap_uid;
      _size = (unsigned long) cr.size;
      
      [CWParser parseFrom: cr.from  inMessage: self  quick: YES];
      [CWParser parseInReplyTo: cr.in_reply_to  inMessage: self  quick: YES];
      [CWParser parseMessageID: cr.message_id  inMessage: self  quick: YES];
      [CWParser parseReferences: cr.references  inMessage: self  quick: YES];
      [CWParser parseSubject: cr.subject inMessage: self  quick: YES];
      [CWParser parseDestination: cr.to
                         forType: PantomimeToRecipient
                       inMessage: self
                           quick: YES];
      [CWParser parseDestination: cr.cc
                         forType: PantomimeCcRecipient
                       inMessage: self
                           quick: YES];
    }
  return self;
}

//
// NSCoding protocol
//
- (void) encodeWithCoder: (NSCoder *) theCoder
{
  // Must also encode Message's superclass
  [super encodeWithCoder: theCoder];
  [theCoder encodeObject: [NSNumber numberWithUnsignedInt: _UID]];
}


//
//
//
- (id) initWithCoder: (NSCoder *) theCoder
{
  // Must also decode Message's superclass
  self = [super initWithCoder: theCoder];
  if (self)
    {
      _UID = (NSUInteger)[[theCoder decodeObject] unsignedIntValue];
    }
  return self;
}


//
//
//
- (NSUInteger) UID
{
  return _UID;
}

- (void) setUID: (NSUInteger) theUID
{
  _UID = theUID;
}


//
// This method is called to initialize the message if it wasn't.
// If we set it to NO and we HAD a content, we release the content.
//
- (void) setInitialized: (BOOL) theBOOL
{
  [super setInitialized: theBOOL];
  
  if (!theBOOL)
    {
      DESTROY(_content);
      return;
    }
  else if (![(CWIMAPFolder *)[self folder] selected])
    {
      [super setInitialized: NO];
      [NSException raise: PantomimeProtocolException
		   format: @"Unable to fetch message content from unselected mailbox."];
      return;
    }

  if (!_content)
    {
      id aStore;

      aStore = [(CWIMAPFolder *)[self folder] store];

      if (!_headers_were_prefetched)
	{
	  [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];
	}

      // If we are no longer connected to the IMAP server, we don't send the 2nd command.
      // This will prevent us from calling the delegate method twice (the one that handles
      // the disconnection from the IMAP server).
      if ([aStore isConnected])
	{
	  [aStore sendCommand: IMAP_UID_FETCH_BODY_TEXT  info: nil  arguments: @"UID FETCH %u:%u BODY[TEXT]", _UID, _UID];
	}

      // Since we are loading asynchronously our message, it's not yet initialized. It'll be set as an initialized one
      // in CWIMAPStore once the body is fully loaded.
      [super setInitialized: NO];
    }
  
  _headers_were_prefetched = YES;
}


//
//
//
- (NSData *) rawSource
{
  if (![(CWIMAPFolder *)[self folder] selected])
    {
      [NSException raise: PantomimeProtocolException
		   format: @"Unable to fetch message data from unselected mailbox."];
      return _rawSource;
    }

  if (!_rawSource)
    {
      [(CWIMAPStore *)[[self folder] store] sendCommand: IMAP_UID_FETCH_RFC822  info: nil  arguments: @"UID FETCH %u:%u RFC822", _UID, _UID];
    }
  
  return _rawSource;
}


//
//
//
- (void) setFlags: (CWFlags *) theFlags
{
  [[self folder] setFlags: theFlags
		 messages: [NSArray arrayWithObject: self]];
}

@end