File: pop3maildrop.C

package info (click to toggle)
cone 0.89-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 25,628 kB
  • ctags: 14,171
  • sloc: ansic: 85,400; cpp: 82,903; sh: 11,713; makefile: 1,732; perl: 832; yacc: 291; sed: 16
file content (440 lines) | stat: -rw-r--r-- 9,076 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
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*
** Copyright 2002-2008, Double Precision Inc.
**
** See COPYING for distribution information.
*/
#include "libmail_config.h"
#include "pop3maildrop.H"
#include "pop3.H"
#include "maildiradd.H"
#include "driver.H"
#include "misc.H"

#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>

using namespace std;

/////////////////////////////////////////////////////////////////////////////

LIBMAIL_START

static bool open_pop3maildrop(mail::account *&accountRet,
			 mail::account::openInfo &oi,
			 mail::callback &callback,
			 mail::callback::disconnect &disconnectCallback)
{
	mail::loginInfo pop3LoginInfo;

	if (!mail::loginUrlDecode(oi.url, pop3LoginInfo))
		return false;

	string pop3method;

	if (pop3LoginInfo.method == "pop3maildrop")
		pop3method="pop3";
	else if (pop3LoginInfo.method == "pop3maildrops")
		pop3method="pop3s";
	else
		return false;

	accountRet=new mail::pop3maildrop(disconnectCallback,
					  oi.loginCallbackObj,
					  callback,
					  oi.extraString,
					  pop3method +
					  oi.url.substr(oi.url.find(':')),
					  oi.pwd,
					  oi.certificates);

	return true;
}

static bool pop3maildrop_remote(string url, bool &flag)
{
	mail::loginInfo pop3LoginInfo;

	if (!mail::loginUrlDecode(url, pop3LoginInfo))
		return false;

	if (pop3LoginInfo.method != "pop3maildrop" &&
	    pop3LoginInfo.method != "pop3maildrops")
		return false;

	flag=false;
	return true;
}

driver pop3maildrop_driver= { &open_pop3maildrop, &pop3maildrop_remote };

LIBMAIL_END


class mail::pop3maildrop::checkNewMailHelper
	: public mail::callback {

	size_t messagesProcessed;

public:
	mail::pop3maildrop::pop3acct *pop3acct;
private:
	mail::pop3maildrop *maildiracct;

	mail::callback *origCallback;

	void success(std::string message);
	void fail(std::string message);

	void reportProgress(size_t bytesCompleted,
			    size_t bytesEstimatedTotal,

			    size_t messagesCompleted,
			    size_t messagesEstimatedTotal);

public:

	checkNewMailHelper(mail::pop3maildrop::pop3acct *pop3acctArg,
			   mail::pop3maildrop *maildiracctArg,
			   mail::callback *origCallbackArg);
	~checkNewMailHelper();
};

/////////////////////////////////////////////////////////////////////////////
//
// Subclass pop3 to forego UIDL, and just push all msgs to the pop3maildrop
// maildir.

class mail::pop3maildrop::pop3acct : public mail::pop3,
	    private mail::callback {

	mail::pop3maildrop *myMaildrop;

public:
	friend class mail::pop3maildrop::checkNewMailHelper;

	pop3acct(mail::pop3maildrop *myMaildropArg,
		 std::string url, std::string passwd,
		 std::vector<std::string> &certificates,
		 mail::loginCallback *loginCallbackFunc,
		 callback &callback,
		 callback::disconnect &disconnectCallback);
	~pop3acct();

	vector<mail::addMessage *> downloadedMsgs;
	size_t messageCount;

	bool ispop3maildrop();
	void pop3maildropreset();
	mail::addMessage *newDownloadMsg();
	string commitDownloadedMsgs();

private:
	bool errflag;
	string errmsg;

	// Inherited 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);


};


mail::pop3maildrop::pop3acct::pop3acct(mail::pop3maildrop *myMaildropArg,
				       std::string url, std::string passwd,
				       std::vector<std::string> &certificates,
				       mail::loginCallback *loginCallbackFunc,
				       callback &callback,
				       callback::disconnect
				       &disconnectCallback)
	: pop3(url, passwd, certificates,
	       loginCallbackFunc, callback, disconnectCallback),
	  myMaildrop(myMaildropArg)
{
}

mail::pop3maildrop::pop3acct::~pop3acct()
{
	pop3maildropreset();
}

bool mail::pop3maildrop::pop3acct::ispop3maildrop()
{
	return true;
}

void mail::pop3maildrop::pop3acct::pop3maildropreset()
{
	vector<mail::addMessage *>::iterator b=downloadedMsgs.begin(),
		e=downloadedMsgs.end();

	while (b != e)
	{
		if (*b)
			(*b)->fail("Cancelled.");
		*b=NULL;
		++b;
	}

	downloadedMsgs.clear();
	errflag=false;
	return;
}

mail::addMessage *mail::pop3maildrop::pop3acct::newDownloadMsg()
{
	downloadedMsgs.push_back(NULL);

	return (downloadedMsgs.end()[-1]=new
		mail::maildir::addmessage(myMaildrop,
					  myMaildrop->path, *this));
}

string mail::pop3maildrop::pop3acct::commitDownloadedMsgs()
{
	messageCount=downloadedMsgs.size();

	vector<mail::addMessage *>::iterator b=downloadedMsgs.begin(),
		e=downloadedMsgs.end();

	while (b != e)
	{
		if (*b)
			(*b)->go();
		*b=NULL;
		++b;
	}
	downloadedMsgs.clear();
	if (!errflag)
		errmsg="";
	myMaildrop->maildir::checkNewMail(NULL);
	return errmsg;
}

void mail::pop3maildrop::pop3acct::success(std::string message)
{
}

void mail::pop3maildrop::pop3acct::fail(std::string message)
{
	errflag=true;
	errmsg=message;
}

void mail::pop3maildrop
::pop3acct::reportProgress(size_t bytesCompleted,
			   size_t bytesEstimatedTotal,
			   size_t messagesCompleted,
			   size_t messagesEstimatedTotal)
{
}

/////////////////////////////////////////////////////////////////////////////

mail::pop3maildrop::pop3maildrop(mail::callback::disconnect
				 &disconnect_callback,
				 mail::loginCallback *loginCallbackFunc,
				 mail::callback &callback,
				 std::string pathArg,
				 std::string pop3url,
				 std::string pop3pass,
				 std::vector<std::string> &certificates)
	: maildir((mail::callback::disconnect &)*this),
	  myPop3Acct(NULL)
{
	struct stat stat_buf;

	// Automatically create the maildir, if it doesn't exist.

	if (stat(pathArg.c_str(), &stat_buf) < 0 && errno == ENOENT)
	{
		if (!mail::maildir::maildirmake(pathArg, false))
		{
			callback.fail(strerror(errno));
			return;
		}
	}

	if (!init(callback, pathArg))
		return;
	maildir::ispop3maildrop=true;

	mail::loginInfo pop3LoginInfo;

	if (!mail::loginUrlDecode(pop3url, pop3LoginInfo) ||
	    (pop3LoginInfo.method != "pop3" &&
	     pop3LoginInfo.method != "pop3s"))
	{
		callback.fail("Invalid POP3 maildrop URL");
		return;
	}

	mail::pop3maildrop::checkNewMailHelper *h=
		new mail::pop3maildrop::checkNewMailHelper(NULL,
							   this,
							   &callback);
	if (!h)
	{
		callback.fail(strerror(errno));
		return;
	}

	try
	{
		myPop3Acct=new pop3acct(this,
					pop3url,
					pop3pass,
					certificates,
					loginCallbackFunc,
					*h,
					disconnect_callback);
		if (!myPop3Acct)
		{
			delete h;
			callback.fail(strerror(errno));
		}
		h->pop3acct=myPop3Acct;
	}
	catch (...)
	{
		delete h;
		throw;
	}
}


bool mail::pop3maildrop::hasCapability(string capability)
{
	if (capability == LIBMAIL_SINGLEFOLDER)
		return true;

	return maildir::hasCapability(capability);
}

string mail::pop3maildrop::getCapability(string capability)
{
	if (capability == LIBMAIL_SERVERTYPE)
	{
		return "pop3maildrop";
	}

	if (myPop3Acct)
	{
		if (capability == LIBMAIL_SERVERDESCR)
		{
			return myPop3Acct->getCapability(capability);
		}
	}

	return maildir::getCapability(capability);		
}


// Callbacks for maildir's disconnect/servererror logic:

void mail::pop3maildrop::disconnected(const char *errmsg)
{
}

void mail::pop3maildrop::servererror(const char *errmsg)
{
}

mail::pop3maildrop::~pop3maildrop()
{
	if (myPop3Acct)
		delete myPop3Acct;
}

void mail::pop3maildrop::logout(callback &callback)
{
	if (myPop3Acct)
		myPop3Acct->logout(callback);
	else
		maildir::logout(callback);
}

mail::pop3maildrop::checkNewMailHelper
::checkNewMailHelper(mail::pop3maildrop::pop3acct *pop3acctArg,
		     mail::pop3maildrop *maildiracctArg,
		     mail::callback *origCallbackArg)
	: messagesProcessed(0),
	  pop3acct(pop3acctArg),
	  maildiracct(maildiracctArg),
	  origCallback(origCallbackArg)
{
}

mail::pop3maildrop::checkNewMailHelper::~checkNewMailHelper()
{
}

void mail::pop3maildrop::checkNewMailHelper::success(std::string message)
{
	if (pop3acct->messageCount > 0)
	{
		messagesProcessed += pop3acct->messageCount;
		pop3acct->checkNewMail(*this);
		return;
	}
	mail::callback *c=origCallback;
	origCallback=NULL;
	delete this;
	c->success(message);

}

void mail::pop3maildrop::checkNewMailHelper::fail(std::string message)
{
	mail::callback *c=origCallback;
	origCallback=NULL;
	delete this;
	c->fail(message);
}

void mail::pop3maildrop::checkNewMailHelper
::reportProgress(size_t bytesCompleted,
		 size_t bytesEstimatedTotal,

		 size_t messagesCompleted,
		 size_t messagesEstimatedTotal)
{
	origCallback->reportProgress(bytesCompleted,
				     bytesEstimatedTotal,
				     messagesCompleted+messagesProcessed,
				     messagesEstimatedTotal+messagesProcessed);
}


void mail::pop3maildrop::checkNewMail(callback &callback)
{
	if (folderCallback && strcasecmp(folderPath.c_str(), "INBOX") == 0 &&
	    myPop3Acct)
	{
		mail::pop3maildrop::checkNewMailHelper *h=
			new mail::pop3maildrop::checkNewMailHelper(myPop3Acct,
								   this,
								   &callback);

		if (!h)
		{
			callback.fail(strerror(errno));
			return;
		}

		myPop3Acct->checkNewMail(*h);
		return;
	}

	mail::maildir::checkNewMail(callback);
}