File: pop3.H

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 (400 lines) | stat: -rw-r--r-- 9,844 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
/*
** Copyright 2002-2008, Double Precision Inc.
**
** See COPYING for distribution information.
*/
#ifndef libmail_pop3_H
#define libmail_pop3_H

#include "libmail_config.h"
#if HAVE_FCNTL_H
#include <fcntl.h>
#endif
#include	"mail.H"
#include	<sys/types.h>

#include	"maildir/maildirkeywords.h"
#include	"logininfo.H"
#include	"fd.H"
#include	"generic.H"
#include	"snapshot.H"
#include	<stdio.h>
#include	<time.h>
#include	<string>
#include	<map>
#include	<list>
#include	<vector>

///////////////////////////////////////////////////////////////////////////
//
// An POP3 implementation
//
// The POP3 INBOX folder

LIBMAIL_START

class pop3;
class addMessage;

class pop3Folder : public folder {

	void sameServerAsHelperFunc() const;

	pop3 *server;

public:
	pop3Folder(pop3 *serverArg);
	~pop3Folder();

	std::string getName() const;
	std::string getPath() const;

	bool hasMessages() const;
	bool hasSubFolders() const;
	bool isParentOf(std::string path) const;

	void hasMessages(bool);
	void hasSubFolders(bool);

	void getParentFolder(callback::folderList &callback1,
			     callback &callback2) const;

	void readFolderInfo( class callback::folderInfo
			     &callback1,
			     callback &callback2) const;

	void readSubFolders( callback::folderList &callback1,
			     callback &callback2) const;

	mail::addMessage *addMessage(callback &callback) const;

	void createSubFolder(std::string name, bool isDirectory,
			     class callback::folderList &callback1,
			     callback &callback2) const;

	void create(bool isDirectory,
		    callback &callback) const;

	void destroy(callback &callback, bool destroyDir) const;

	void renameFolder(const mail::folder *newParent, std::string newName,
			  mail::callback::folderList &callback1,
			  callback &callback) const;

	folder *clone() const;
	std::string toString() const;

	void open(class callback &openCallback,
		  snapshot *restoreSnapshot,
		  class callback::folder &folderCallback) const;
};


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

class pop3 : public fd, public generic, public snapshot::restore {

private:
	bool calledDisconnected;
	// True when the disconnect callback is invoked

	bool orderlyShutdown;

	time_t timeoutSetting;
	time_t noopSetting;

	// Superclass of POP3 tasks.  Methods create subclasses of Task
	// objects, and push them to the tasks queue.  Replies from the
	// POP3 server are routed to the foremost Task on the queue.

	class Task {

	protected:
		callback *callbackPtr;	// App callback.
		pop3 * volatile myserver;
		// Marked volatile due to destructor monkey business

		time_t defaultTimeout;

	public:
		virtual void done();
		// Task completed, start the next task on the queue,
		// and delete this Task object.

		Task(callback *callbackArg,
		     pop3 &myserverArg);
		virtual ~Task();

		virtual int getTimeout();
		// How long before this task times out.

		virtual void serverResponse(const char *message)=0;
		// Process a line of text from the server

		virtual void disconnected(const char *reason);
		// Server has disconnected.
		// The default implementation takes this task off the queue,
		// calls the next Task's disconnect method, then deletes
		// itself.

		virtual void installedTask();
		// This task is now at the front of the queue

		void resetTimeout();
		// Reset timeout interval

		virtual bool isLogoutTask();
		// This is a logout task.  If it's not the logout task,
		// its completion time is recorded.  If nothing happens,
		// a new NOOP task is started to keep the server from timing
		// out.

		virtual bool willReconnect();
		// This logout task is a part of expunge processing, so
		// do not do disconnect processing.
	};

#if 0
	class AutologoutCallback : public callback {
	public:
		AutologoutCallback();
		~AutologoutCallback();

		void success(std::string message);
		void fail(std::string message);
	};
#endif

	void resumed();
	void handler(std::vector<pollfd> &fds, int &timeout);

	std::map<std::string, std::string> capabilities;

	loginInfo pop3LoginInfo;

	loginInfo savedLoginInfo;

	int socketRead(const std::string &readbuffer);

	void disconnect(const char *reason);

	std::list<Task *> tasks;

	void installTask(Task *p);

	time_t lastTaskCompleted;

	class LoginTask;
	class LogoutTask;
	class LoggedInTask;

	class CheckNewMailTask;
	class ReadMessageTask;
	class ForceCheckNewMailTask;
	class CacheMessageTask;
	class UpdateTask;
	class NoopTask;

	pop3Folder inbox;

	// A map from message UID to the current message number in the POP3
	// server.
	std::map<std::string, int> uidlMap;

	std::map<int, unsigned long> listMap;
	// Size of each msg, in bytes.  Should be an array, though, but that's
	// ok.


	callback::folder *folderCallback;
	// The folder callback.
	// POP3 implements snapshots for the purpose of preserving message
	// status flags.  There is no concept of message status flags in POP3,
	// so by default, unless snapshots are used, each message is marked
	// as a new message, when the POP3 folder is opened.  If snapshots are
	// used, the message status flags will be restored from the snapshot.
	// The snapshot ID is always fixed, "POP3", since POP3 keys off msg
	// UIDs entirely, so a snapshot restore simply restores whatever
	// snapshot UIDs are available, then does a checknewmail.
	//
	// A snapshot is also saved each time message status flags are updated.

	// The virtual folder.

	class pop3MessageInfo : public messageInfo {
	public:
		pop3MessageInfo();
		~pop3MessageInfo();

		mail::keywords::Message keywords;
	};

	mail::keywords::Hashtable keywordHashtable;

	std::vector<pop3MessageInfo> currentFolderIndex;
	// Virtual folder index

public:
	friend class Task;
	friend class LoginTask;
	friend class LogoutTask;

	friend class CheckNewMailTask;
	friend class ReadMessageTask;
	friend class ForceCheckNewMailTask;
	friend class CacheMessageTask;
	friend class UpdateTask;
	friend class NoopTask;

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

	pop3(const pop3 &); // UNDEFINED
	pop3 &operator=(const pop3 &); // UNDEFINED

	~pop3();

	void logout(callback &callback);

	void checkNewMail(callback &callback);
	bool hasCapability(std::string capability);
	std::string getCapability(std::string capability);

	folder *folderFromString(std::string);

	void readTopLevelFolders(callback::folderList &callback1,
				 callback &callback2);
	void findFolder(std::string folder,
			class callback::folderList &callback1,
			class callback &callback2);
	std::string translatePath(std::string path);

	void readMessageAttributes(const std::vector<size_t> &messages,
				   MessageAttributes attributes,
				   callback::message &callback);

	void readMessageContent(const std::vector<size_t> &messages,
				bool peek,
				enum mail::readMode readType,
				callback::message &callback);

	void readMessageContent(size_t messageNum,
				bool peek,
				const mimestruct &msginfo,
				enum mail::readMode readType,
				callback::message &callback);

	void readMessageContentDecoded(size_t messageNum,
				       bool peek,
				       const mimestruct &msginfo,
				       callback::message &callback);

	size_t getFolderIndexSize();
	messageInfo getFolderIndexInfo(size_t);

	void saveFolderIndexInfo(size_t,
				 const messageInfo &,
				 callback &);
	void getFolderKeywordInfo(size_t, std::set<std::string> &);
	void updateKeywords(const std::vector<size_t> &messages,
			    const std::set<std::string> &keywords,
			    bool setOrChange,
			    // false: set, true: see changeTo
			    bool changeTo,
			    callback &cb);
private:
	bool genericProcessKeyword(size_t messageNumber,
				   updateKeywordHelper &helper);
public:
	void updateFolderIndexFlags(const std::vector<size_t> &messages,
				    bool doFlip,
				    bool enableDisable,
				    const messageInfo &flags,
				    callback &callback);

	void updateFolderIndexInfo(callback &);
	void removeMessages(const std::vector<size_t> &messages,
			    callback &cb);

	void copyMessagesTo(const std::vector<size_t> &messages,
			    folder *copyTo,
			    callback &callback);

	void searchMessages(const searchParams &searchInfo,
			    searchCallback &callback);

	// Used by pop3Folder:

	void readFolderInfo( callback::folderInfo &callback1,
			     callback &callback2);

	void open(callback &openCallback,
		  callback::folder &folderCallback,
		  mail::snapshot *restoreSnapshot);


	bool reconcileFolderIndex();

private:

	void genericMarkRead(size_t messageNumber);

	void genericMessageRead(std::string uid,
				size_t messageNumber,
				bool peek,
				mail::readMode readType,
				callback::message &callback);

	void genericMessageSize(std::string uid,
				size_t messageNumber,
				callback::message &callback);

	void genericGetMessageFd(std::string uid,
				 size_t messageNumber,
				 bool peek,
				 int &fdRet,
				 callback &callback);

	void genericGetMessageStruct(std::string uid,
				     size_t messageNumber,
				     struct rfc2045 *&structRet,
				     callback &callback);

	bool genericCachedUid(std::string uid);

	// One message is cached to a temp file, and parsed.

	std::string cachedUid;
	int genericTmpFd;
	struct rfc2045 *genericTmpRfcp;

	//
	// Inherited from mail::snapshot::restore:
	//

	void restoreIndex(size_t msgNum,
			  const mail::messageInfo &info);
	void restoreKeywords(size_t msgNum,
			     const std::set<std::string> &);
	void abortRestore();

	bool restoreAborted;


protected:
	// Subclassed by mail::pop3maildrop::pop3acct():

	virtual bool ispop3maildrop();
	virtual void pop3maildropreset();

	virtual mail::addMessage *newDownloadMsg();
	virtual std::string commitDownloadedMsgs();

};

LIBMAIL_END

#endif