Project: Wakanda
Code Location: git://github.com/Wakanda/waf-mail.gitmaster
Browse
/
Outline
Download File
POP3.js
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
/* Copyright (c) 4D, 2011
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

// POP3 library.
//
// Usage:
//
//		Use getAllMailAndDelete() or getAllMail() functions to retrieve all available mails on a POP3 server.
//		getAllMail() will left them on server. This is the most common use case. 
//
//		For more control, create a POP3 object, connect it to a server, and issue individual commands. You may 
//		find out the number of message(s) available for read, their individual sizes, etc.
//		
//		If you need even more control and are familiar with POP3 protocol, use the POP3Client low-level library.

function POP3Scope () {

	var DEFAULT_PORT			= 110;

	var STATES = {
		
		NOT_CONNECTED:			1,	// State just after creation.
		IDLE:					2,	// Connected to POP3 server, awaiting commands.		
		AUTHENTIFICATION:		3,	// USER then PASS commands authentification.
		WAITING_REPLY:			4,	// Command sent, waiting response from server.
		TERMINATED:				5,	// Has sent QUIT command.
		
		CONNECTION_BROKEN:		-1,	// Connection has been lost.
			
	};
	
	var isWakanda	= typeof requireNative != 'undefined';
	var pop3		= isWakanda ? require('waf-mail/pop3Client') : require('./pop3Client.js');
	
	// Exception for POP3, see codes below.
	
	var POP3Exception = function (code) {
	
		this.code = code;
	
	}
	POP3Exception.INVALID_STATE		= -1;	// Command or operation can't be performed in current state.
	POP3Exception.INVALID_ARGUMENT	= -2;	// At least an argument is wrong.
	
	// If arguments are given, connect to server on object's creation.
	
	function POP3 (address, port, isSSL, callback) {
				
		var state		= STATES.NOT_CONNECTED;
		var pop3Client	= new pop3.POP3Client();
		
		this.POP3Exception = POP3Exception;
		
		// Connect to a POP3 server. 
		
		this.connect = function (address, port, isSSL, callback) {
			
			if (state != STATES.NOT_CONNECTED)
			
				throw new POP3Exception(POP3Exception.INVALID_STATE);
			
			if (typeof address != 'string') 
			
				throw new POP3Exception(POP3Exception.INVALID_ARGUMENT);
							
			if (typeof port == 'undefined') 
				
				port = DEFAULT_PORT;
				
			else if (typeof port == 'string') 
				
				port = port.toNumber();
					
			else if (typeof port != 'number') 
				
				throw new POP3Exception(POP3Exception.INVALID_ARGUMENT);
				
			var	connectFunction = isSSL ? pop3Client.sslConnect : pop3Client.connect;
			
			connectFunction(address, port, function (isOkResponse, response) {

				state = STATES.IDLE;
				if (typeof callback == 'function')
				
					callback(isOkResponse == 0, response);	// Server's greeting in response.

			});
			
		}
		
		var authenticatePassword, authenticateCallback;
		
		var sendPasswordCallback = function (isOkResponse, response) {
		
			if (isOkResponse != 0) {
			
				state = STATES.IDLE;
				if (typeof authenticateCallback == 'function')
				
					authenticateCallback(false, response);				
			
			} else {
			
				pop3Client.sendPASS(authenticatePassword, function (isOkResponse, response) {
				
					state = STATES.IDLE;
					if (typeof authenticateCallback == 'function')
				
						authenticateCallback(isOkResponse == 0, response);

				});
				
			}
		
		}
				
		// Authenticate using USER and PASS commands, username and password arguments are mandatory. Callback is 
		// called with a boolean as first argument, indicating if authentification was successful. Second argument
		// is the response from server for PASS command if successful. Or first command (either USER or PASS) to 
		// fail.
		
		this.authenticate = function (username, password, callback) {
		
			if (state != STATES.IDLE) 
		
				throw new POP3Exception(POP3Exception.INVALID_STATE);
		
			else if (typeof username != 'string' || typeof password != 'string')
			
				throw new POP3Exception(POP3Exception.INVALID_ARGUMENT);
				
			else {

				authenticatePassword = password;
				authenticateCallback = callback;
				
				state = STATES.AUTHENTIFICATION;
				pop3Client.sendUSER(username, sendPasswordCallback);
			
			}
		
		}
		
		// Get 'status' of POP3 server. If successful, callback has as additional arguments the number of message(s)
		// available for read and the total size of mail box. 
		
		this.getStatus = function (callback) {
				
			if (state != STATES.IDLE) 
			
				throw new POP3Exception(POP3Exception.INVALID_STATE);
		
			state = STATES.WAITING_REPLY;
			pop3Client.sendSTAT(function (isOkResponse, response, numberMessages, totalSize) {
			
				state = STATES.IDLE;
				if (typeof callback == 'function') 
									
					callback(isOkResponse == 0, response, numberMessages, totalSize);
			
			});
		
		}
		
		// Get the size of a specific message. Note that message numbers start at 1, not zero. If successful, callback 
		// has an message's size as an additional argument.
		
		this.getMessageSize = function (messageNumber, callback) {
		
			if (state != STATES.IDLE) 
			
				throw new POP3Exception(POP3Exception.INVALID_STATE);
			
			if (typeof messageNumber == 'number')
			
				messageNumber = messageNumber.toString();
				
			else if (typeof messageNumber != 'string')
			
				throw new POP3Exception(POP3Exception.INVALID_ARGUMENT);
			
			state = STATES.WAITING_REPLY;
			pop3Client.sendLIST(messageNumber, function (isOkResponse, response, size) {
			
				state = STATES.IDLE;
				if (typeof callback == 'function')
									
					callback(isOkResponse == 0, response, size);
				
			});
		
		}
		
		// Get the size of all messages. Callback will have an additional argument array of size(s) ordered by message 
		// number.
		
		this.getAllMessageSizes = function (callback) {
		
			if (state != STATES.IDLE) 
			
				throw new POP3Exception(POP3Exception.INVALID_STATE);
			
			state = STATES.WAITING_REPLY;
			pop3Client.sendLIST(function (isOkResponse, response, sizes) {
			
				state = STATES.IDLE;
				if (typeof callback == 'function')
				
					callback(isOkResponse == 0, response, sizes);
				
			});
		
		}
		
		// Retrieve an email. If successful, the email is in the server's reponse: Ignore first line which is POP3
		// protocol specific. Then content of email follows, a header (containing fields such as "From", "To", or
		// "Subject"), an empty line separator, followed by message's body. The email is terminated by a dot ('.')
		// on a single line. You can use Mail.parse() function to parse it.
		
		this.retrieveMessage = function (messageNumber, callback) {
		
			if (state != STATES.IDLE)
			
				throw new POP3Exception(POP3Exception.INVALID_STATE);
			
			if (typeof messageNumber == 'number')
			
				messageNumber = messageNumber.toString();
				
			else if (typeof messageNumber != 'string')
			
				throw new POP3Exception(POP3Exception.INVALID_ARGUMENT);
			
			state = STATES.WAITING_REPLY;			
			pop3Client.sendRETR(messageNumber, function (isOkResponse, response) {

				state = STATES.IDLE;
				if (typeof callback == 'function')
			
					callback(isOkResponse == 0, response);
			
			});
		
		}
		
		// Mark a message for deletion. This message will not be deleted until a QUIT command has been issued (the POP3 
		// client must actually disconnect from server for it to do the actual update).
		
		this.markForDeletion = function (messageNumber, callback) {
		
			if (state != STATES.IDLE)

				throw new POP3Exception(POP3Exception.INVALID_STATE);
				
			if (typeof messageNumber == 'number')
			
				messageNumber = messageNumber.toString();
				
			else if (typeof messageNumber != 'string')
			
				throw new POP3Exception(POP3Exception.INVALID_ARGUMENT);
			
			state = STATES.WAITING_REPLY;			
			pop3Client.sendDELE(messageNumber, function (isOkResponse, response) {

				state = STATES.IDLE;
				if (typeof callback == 'function')
				
					callback(isOkResponse == 0, response);

			});

		}
		
		// This will clear all deletion mark(s) (you will need to mark them again).
		
		this.clearDeletionMarks = function (callback) {
		
			if (state != STATES.IDLE) 
			
				throw new POP3Exception(POP3Exception.INVALID_STATE);
			
			state = STATES.WAITING_REPLY;			
			pop3Client.sendRSET(function (isOkResponse, response) {

				state = STATES.IDLE;
				if (typeof callback == 'function')

					callback(isOkResponse == 0, response);
			
			});
		
		}
		
		// Force closing and freeing of POP3 client. This will release all resources. Can be called several times.
		
		var forceClose = function () {

			state = STATES.TERMINATED;
			if (pop3Client != null) {
				
				pop3Client.forceClose();
				pop3Client = null;
			
			}

		}
		this.forceClose = forceClose;

		// Send QUIT command. The server will reply a response, then break the connection with client. It will then
		// delete marked messages, as requested.
		
		this.quit = function (callback) {

			if (state != STATES.IDLE)
			
				throw new POP3Exception(POP3Exception.INVALID_STATE);
			
			state = STATES.WAITING_REPLY;			
			pop3Client.sendQUIT(function (isOkResponse, response) {
		
				forceClose();
				if (typeof callback == 'function')

					callback(isOkResponse == 0, response);
			
			});

		}
		
		// Connect on creation.
		
		if (typeof address != 'undefined') 
			
			this.connect(address, port, isSSL, callback);
		
	}
	
	return POP3;

}

var POP3	= POP3Scope();

var getAll = function (address, port, isSSL, username, password, allMails, doMarkForDeletion) {
	
	var isWakanda	= typeof requireNative != 'undefined';
	var pop3		= new POP3();
	var	status		= false;
	var mailModule	= require("waf-mail/mail");
	
	if (typeof address != 'string' || typeof port != 'number' || typeof isSSL != 'boolean'
	|| typeof username != 'string' || typeof password != 'string' 
	|| !(allMails instanceof Array) || allMails.length)
		
		throw new pop3.POP3Exception(pop3.POP3Exception.INVALID_ARGUMENT);	

	// Function to exit event loop. Wakanda has the exitWait() function. Otherwise terminate POP3
	// client, this will close the socket and get out of event loop.
	
	var exit = function	() {
	
		if (isWakanda)
			
			exitWait();
		
		else 
			
			pop3.forceClose();	
		
	}
	
	// Function callbacks are named by states.
	
	var authentificationState, statusState, retrievalState, quittingState;
			
	authentificationState = function (isOk, response) { 
	
		if (!isOk) 
			
			exit();
			
		else
				
			pop3.authenticate(username, password, statusState);

	}
			
	statusState = function (isOk, response) {
	
		if (!isOk)
		
			exit();
	
		else
		
			pop3.getStatus(retrievalState);
	
	}
		
	retrievalState = function (isOk, response, numberMessages) {		
		
		if (!isOk)
				
			exit();
			
		else if (!numberMessages)
		
			pop3.quit(quittingState);
			
		else {
			
			var	i = 1;
			
			// Callbacks for retrieval "asynchronous" loop.
			
			var markCallback, retrieveCallback;
			
			markCallback = function (isOk, response) {
							
				if (!isOk) 

					exit();
													
				else if (i == numberMessages) {
					
					// Last message has been marked for deletion.
					
					pop3.quit(quittingState);
					
				} else {
						
					// Message successfully marked, move on and retrieve remaining message(s).
						
					i++;
					pop3.retrieveMessage(i, retrieveCallback);

				}
							
			}
							
			retrieveCallback = function (isOk, response) {

				if (!isOk) 
					
					exit();
										
				else {
									
					// Received mail(s) are parsed.
					
					var	newMail	= new mailModule.Mail();
					
					newMail.parse(response);
					allMails.push(newMail);
					
					if (doMarkForDeletion) 
						
						pop3.markForDeletion(i, markCallback);
										
					else if (i == numberMessages) {
						
						// We're done.
					
						pop3.quit(quittingState);
						
					} else {
						
						// Retrieve remaining message(s).
						
						i++;
						pop3.retrieveMessage(i, retrieveCallback);
						
					}
				
				}		

			}
			
			// Retrieve first message, this will start the retrieval "asynchronous" loop.
				
			pop3.retrieveMessage(i, retrieveCallback);
			
		}

	}
	
	quittingState = function (isOk, response) {
		
		status = isOk;
		exit();
	
	}
		
	// Connect to POP3 server. Code will go asynchronously from state to state via callbacks.

	pop3.connect(address, port, isSSL, authentificationState);	
	
	// If using Wakanda, function is made synchronous.
	
	if (isWakanda) {
			
		// Indefinite wait, callback events will exit wait when done.
	
		wait();
	
		// Force termination and release of POP3 client resource.
	
		pop3.forceClose();
		
	}
	
	return status;	
}

// Create a new POP3 object.

var createClient = function (address, port, isSSL, callback) {

	return new POP3(address, port, isSSL, callback);
	
}

// Connect to a POP3 server, retrieve all available messages, and delete them. All arguments are mandatory. allMails
// must be an empty Array, it will be filled with the retrieved message. Format of message is same as those returned
// by POP3.retrieveMessage() function. This function returns true if successful. If not, allMails contain message(s) 
// successfully read until error, and all messages are left on server (not deleted). This function is synchronous if
// Wakanda is used.

var getAllMailAndDelete = function (address, port, isSSL, username, password, allMails) {

	return getAll(address, port, isSSL, username, password, allMails, true);

}

// Same as getAllMailAndDelete(), except retrieved mails are left (not deleted) on POP3 server. 

var getAllMail = function (address, port, isSSL, username, password, allMails) {

	return getAll(address, port, isSSL, username, password, allMails, false);

}

exports.createClient = createClient;
exports.getAllMail = getAllMail;
exports.getAllMailAndDelete = getAllMailAndDelete;
exports.POP3 = POP3;