| 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
 | /*
** Copyright 2002-2006, Double Precision Inc.
**
** See COPYING for distribution information.
*/
#include "libmail_config.h"
#include "mboxgetmessage.H"
#include "file.H"
#include "rfc2045/rfc2045.h"
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
using namespace std;
mail::mbox::GenericGetMessageTask::GenericGetMessageTask(mail::mbox &mboxAccount,
							 mail::callback
							 &callbackArg,
							 string uidArg,
							 size_t
							 messageNumberArg,
							 bool peekArg,
							 int *fdretArg,
							 struct rfc2045
							 **structretArg)
	: LockTask(mboxAccount, callbackArg),
	  uid(uidArg),
	  messageNumber(messageNumberArg),
	  peek(peekArg),
	  fdret(fdretArg),
	  structret(structretArg),
	  tempFp(NULL),
	  tempStruct(NULL)
{
}
mail::mbox::GenericGetMessageTask::~GenericGetMessageTask()
{
	// tempFp/tempStruct should be null, if all's well.  If not, clean up
	// after outselves.
	if (tempFp)
		fclose(tempFp);
	if (tempStruct)
		rfc2045_free(tempStruct);
}
bool mail::mbox::GenericGetMessageTask::locked(mail::file &file)
{
	if (!mboxAccount.verifyUid(uid, messageNumber, callback))
	{
		done();
		return true;
	}
	mail::ptr<mail::mbox> me= &mboxAccount;
	size_t realMsgNum=mboxAccount.uidmap.find(uid)->second;
	off_t startingPos=mboxAccount.folderMessageIndex[realMsgNum]
		.startingPos;
	if (fseek(file, startingPos, SEEK_SET) < 0 ||
	    (tempFp=tmpfile()) == NULL ||
	    (tempStruct=rfc2045_alloc()) == NULL)
	{
		fail(strerror(errno));
		return true;
	}
	off_t endingPos=0;
	if (realMsgNum + 1 >= mboxAccount.folderMessageIndex.size())
	{
		struct stat stat_buf;
		if (fstat(fileno(static_cast<FILE *>(file)), &stat_buf) < 0)
			endingPos=stat_buf.st_size;
	}
	else
		endingPos=mboxAccount.folderMessageIndex[realMsgNum+1]
			.startingPos;
	if (endingPos >= startingPos)
		endingPos -= startingPos;
	else
		endingPos=0;
	off_t nextUpdatePos=startingPos;
	bool firstLine=true;
	while (!feof(file) && !me.isDestroyed())
	{
		off_t pos=file.tell();
		// Progress update every 10K
		if (pos >= nextUpdatePos)
		{
			nextUpdatePos=pos + 10000;
			callback.reportProgress(pos - startingPos, endingPos,
						0, 1);
		}
		string line=file.getline();
		if (line.size() == 0 && feof(file))
			break;
		if (strncmp(line.c_str(), "From ", 5) == 0)
		{
			if (firstLine)
				continue;
			break;
		}
		if (firstLine)
		{
			firstLine=false;
			if (mail::mboxMagicTag(line,
					       mboxAccount.keywordHashtable)
			    .good())
				continue; // Ignore magic tag line.
		}
		const char *p=line.c_str();
		if (*p == '>')
		{
			while (*p == '>')
				p++;
			if (strncmp(p, "From ", 5) == 0)
				line=line.substr(1);
		}
		line += "\n";
		if (fwrite(&line[0], line.size(), 1, tempFp) != 1)
			; // Ignore gcc warning
		rfc2045_parse(tempStruct, &line[0], line.size());
	}
	if (fflush(tempFp) < 0 || ferror(tempFp) < 0)
	{
		fail(strerror(errno));
		return true;
	}
	mboxAccount.cachedMessageUid="";
	if (mboxAccount.cachedMessageFp)
		fclose(mboxAccount.cachedMessageFp);
	if (mboxAccount.cachedMessageRfcp)
		rfc2045_free(mboxAccount.cachedMessageRfcp);
	mboxAccount.cachedMessageFp=NULL;
	mboxAccount.cachedMessageRfcp=NULL;
	mboxAccount.cachedMessageUid=uid;
	mboxAccount.cachedMessageFp=tempFp;
	mboxAccount.cachedMessageRfcp=tempStruct;
	fcntl(fileno(tempFp), F_SETFD, FD_CLOEXEC);
	tempFp=NULL;
	tempStruct=NULL;
	if (fdret)
		*fdret=fileno(mboxAccount.cachedMessageFp);
	if (structret)
		*structret=mboxAccount.cachedMessageRfcp;
	if (mboxAccount.currentFolderCallback && !peek &&
	    mboxAccount.index[messageNumber].unread)
	{
		mboxAccount.index[messageNumber].unread=false;
		mboxAccount.folderDirty=true;
		mboxAccount.currentFolderCallback->messageChanged(messageNumber);
	}
	callback.success("OK");
	done();
	return true;
}
 |