summaryrefslogtreecommitdiffstats
path: root/libmail/addmessageremoveattachments.C
blob: 650058e3141288b05ea473b1b9fa5216aae28ddf (plain)
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/*
** Copyright 2004-2008, Double Precision Inc.
**
** See COPYING for distribution information.
*/
#include "libmail_config.h"
#include "addmessage.H"
#include "attachments.H"
#include "headers.H"
#include <errno.h>
#include <vector>
#include <cstring>

using namespace std;

class mail::addMessage::assembleRemoveAttachmentsHelper
	: public mail::callback::message {

public:
	void doNextCopy(std::string message);


private:
	// Imported from mail::callback:
	void success(std::string message);
	void fail(std::string message);

	void reportProgress(size_t bytesCompleted,
			    size_t bytesEstimatedTotal,
			    size_t messagesCompleted,
			    size_t messagesEstimatedTotal);
	void messageTextCallback(size_t n, std::string text);

	size_t &handleRet;
	mail::ptr<mail::account> acct;
	string msgUid;
	size_t msgNum;

	mail::addMessage *add;
	mail::callback &origCallback;

	void (assembleRemoveAttachmentsHelper::*success_func)(std::string);
	void doMultiPart(std::string message);

	string collectedHeaders;

public:
	assembleRemoveAttachmentsHelper(size_t &handleRetArg,
					mail::account *acctArg,
					string msgUidArg,
					mail::addMessage *addArg,
					mail::callback &origCallbackArg)
		: handleRet(handleRetArg),
		  acct(acctArg),
		  msgUid(msgUidArg),
		  msgNum(0),
		  add(addArg),
		  origCallback(origCallbackArg)
	{
	}

	~assembleRemoveAttachmentsHelper() {}


	// We get a message's MIME structure, and the MIME ids of attachments
	// to remove.  Create a tree structure for each MIME section with
	// two data points:
	//
	// A) Whether or not either this MIME section should be copied, or
	//    at least one of the MIME subsections should be copied
	//
	// B) Whether or not either this MIME section should not be copied, or
	//    at least one of the MIME subsections should not be copied
	//
	class mimeAction;

	class mimeAction {
	public:
		const mail::mimestruct &section;

		bool somethingCopied;
		bool somethingNotCopied;

		//
		// The list of MIME actions is built into a list
		// (so we don't need to manually allocate these objects),
		// and the MIME subsections are saved in the following
		// vector of iterators to the subsection of this section

		vector< list<mimeAction>::iterator > subsections;
		size_t handleId;

		mimeAction(const mail::mimestruct &sectionArg, bool a, bool b)
			: section(sectionArg), somethingCopied(a),
			  somethingNotCopied(b) {}

		~mimeAction() {}
	};

	list<mimeAction> parseTree;

	list<mimeAction>::iterator
	computeToCopy(const mail::mimestruct &section,
		      const set<string> &whatNotToCopy); // Compute parseTree

	//
	// Now, iterate over the parse tree, and create the execution plan
	// of things to copy.
	//

	class plan {
	public:
		bool multiPartCopy;
		list<mimeAction>::iterator whichPart;

		plan(bool multipartCopyArg,
		     list<mimeAction>::iterator whichPartArg)
			: multiPartCopy(multipartCopyArg),
			  whichPart(whichPartArg)
		{
		}

		~plan() {}
	};


	vector< plan > copyPlan;
	vector< plan >::iterator ToDo;

	void computePlan( list<mimeAction>::iterator );
};

#define MIMEACTION mail::addMessage::assembleRemoveAttachmentsHelper::mimeAction

list<MIMEACTION>::iterator
mail::addMessage::assembleRemoveAttachmentsHelper
::computeToCopy(const mail::mimestruct &section,
		const set<string> &whatNotToCopy)
{
	list<MIMEACTION>::iterator p;

	if (whatNotToCopy.count(section.mime_id) > 0)
	{
		parseTree.push_back(MIMEACTION(section, false, true));
		p=parseTree.end();
		return --p;
	}

	size_t n=section.getNumChildren();

	if (n == 0)
	{
		// Copy this one, by default

		parseTree.push_back(MIMEACTION(section, true, false));
		p=parseTree.end();
		return --p;
	}

	vector< list<MIMEACTION>::iterator > subsections;

	size_t i;

	bool somethingCopied=false;
	bool somethingNotCopied=false;

	for (i=0; i<n; i++)
	{
		p=computeToCopy(*section.getChild(i), whatNotToCopy);

		if (p->somethingNotCopied)
			somethingNotCopied=true;

		if (!p->somethingCopied)
			continue;

		somethingCopied=true;
		subsections.push_back(p);
	}

	parseTree.push_back(MIMEACTION(section, somethingCopied,
				       somethingNotCopied));

	p=parseTree.end();
	--p;

	p->subsections=subsections;
	return p;
}


void mail::addMessage::assembleRemoveAttachmentsHelper
::computePlan(list<MIMEACTION>::iterator nodeArg)
{
	if (!nodeArg->somethingCopied)
		return; // Skip this entire MIME section

	if (!nodeArg->somethingNotCopied)
	{
		copyPlan.push_back(plan(false, nodeArg));
		// Copy this entire section, multipart or not
		return;
	}

	size_t i, n= nodeArg->subsections.size();

	for (i=0; i<n; i++)
		computePlan(nodeArg->subsections[i]);

	copyPlan.push_back(plan(true, nodeArg));
}

void mail::addMessage
::assembleRemoveAttachmentsFrom(size_t &handleRetArg,
				mail::account *acctArg,
				string msgUidArg,
				const mail::mimestruct &msgStructArg,
				const set<string> &removeUidListArg,
				mail::callback &cbArg)
{
	assembleRemoveAttachmentsHelper *helper=
		new assembleRemoveAttachmentsHelper(handleRetArg,
						    acctArg,
						    msgUidArg,
						    this,
						    cbArg);

	if (!helper)
	{
		cbArg.fail(strerror(errno));
		return;
	}

	try {
		list<MIMEACTION>::iterator topNode;

		topNode=helper->computeToCopy(msgStructArg, removeUidListArg);

		if (!topNode->somethingCopied)
		{
			delete helper;
			helper=NULL;
			cbArg.fail("Cannot remove every attachment.");
			return;
		}

		if (!topNode->somethingNotCopied)
		{
			delete helper;
			helper=NULL;
			cbArg.fail("No attachments selected for deletion");
			return;
		}
		helper->computePlan(topNode);
		helper->ToDo=helper->copyPlan.begin();
		helper->doNextCopy("Ok."); // Kick into action.
	} catch (...)
	{
		if (helper)
			delete helper;
		cbArg.fail(strerror(errno));
	}
}

void mail::addMessage
::assembleRemoveAttachmentsHelper::success(std::string message)
{
	(this->*success_func)(message);
}

void mail::addMessage
::assembleRemoveAttachmentsHelper::doNextCopy(std::string message)
{
	origCallback.reportProgress(0, 0, ToDo-copyPlan.begin()+1,
				    copyPlan.size());

	if (ToDo == copyPlan.end())
	{
		try {
			--ToDo;
			handleRet=ToDo->whichPart->handleId;

			origCallback.success(message);
		} catch (...)
		{
			delete this;
			throw;
		}
		delete this;
		return;
	}

	if (acct.isDestroyed())
	{
		fail("Server connection terminated.");
		return;
	}

	if (!chkMsgNum(acct, msgUid, msgNum))
	{
		fail("Message deleted in the middle of being copied.");
		return;
	}

	plan &toDoNext= *ToDo;
	mimeAction &toDoInfo= *toDoNext.whichPart;

	if (toDoNext.multiPartCopy)
	{
		collectedHeaders="";
		success_func= &assembleRemoveAttachmentsHelper::doMultiPart;

		acct->readMessageContent(msgNum, true,
					 toDoInfo.section,
					 readHeaders, *this);
		return;
	}

	++ToDo;
	success_func= &assembleRemoveAttachmentsHelper::doNextCopy;

	add->assembleImportAttachment(toDoInfo.handleId, acct, msgUid,
				      toDoInfo.section,
				      *this);
}

void mail::addMessage
::assembleRemoveAttachmentsHelper::doMultiPart(std::string message)
{
	if (acct.isDestroyed())
	{
		fail("Server connection terminated.");
		return;
	}

	plan &toDoNext= *ToDo;
	mimeAction &toDoInfo= *toDoNext.whichPart;

	vector<size_t> parts;

	vector< list<mimeAction>::iterator >::iterator b, e;

	for (b=toDoInfo.subsections.begin(),
		     e=toDoInfo.subsections.end(); b != e; ++b)
		parts.push_back( (*b)->handleId );

	string::iterator hstart, hend;

	string encoding=
		mail::Attachment::find_header("CONTENT-TRANSFER-ENCODING:",
					      collectedHeaders, hstart, hend);

	if (hstart != hend)
		collectedHeaders.erase(hstart, hend);

	string content_type_header=
		mail::Attachment::find_header("CONTENT-TYPE:",
					      collectedHeaders, hstart, hend);

	mail::Header::mime ct=
		mail::Header::mime::fromString(string(hstart, hend));
	if (hstart != hend)
		collectedHeaders.erase(hstart, hend);

	++ToDo;
	success_func= &assembleRemoveAttachmentsHelper::doNextCopy;

	add->assembleMultipart(toDoInfo.handleId, collectedHeaders,
			       parts,
			       ct.value,
			       ct.parameters,
			       *this);
}

void mail::addMessage
::assembleRemoveAttachmentsHelper::fail(std::string message)
{
	try {
		origCallback.fail(strerror(errno));
	} catch (...)
	{
		delete this;
		throw;
	}
	delete this;
}

void mail::addMessage
::assembleRemoveAttachmentsHelper::reportProgress(size_t bytesCompleted,
						  size_t bytesEstimatedTotal,
						  size_t messagesCompleted,
						  size_t messagesEstimatedTotal)
{
	origCallback.reportProgress(bytesCompleted, bytesEstimatedTotal,
				    ToDo-copyPlan.begin(),
				    copyPlan.size());
}

void mail::addMessage
::assembleRemoveAttachmentsHelper::messageTextCallback(size_t n,
						       std::string text)
{
	collectedHeaders += text;
}