forked from andris9/n3
-
Notifications
You must be signed in to change notification settings - Fork 2
/
connection.js
68 lines (60 loc) · 1.95 KB
/
connection.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
'use strict';
const debug = require('debug')('pop3-connection');
const TEN_MINUTES = 10 * 60 * 1000;
const States = require('./states');
var POP3Connnection = module.exports = function(server, socket, connection_id) {
debug('connection ' + connection_id, socket.remoteAddress);
var self = this;
this.socket = socket;
this.server = server;
this.connection_id = connection_id;
this.state = States.AUTHENTICATION;
this.UID = this.connection_id + "." + (+new Date());
this.response("+OK POP3 Server ready <" + self.UID + "@" + self.server_name + ">");
};
POP3Connnection.prototype.response = function response(message) {
let resBuffer;
if (message.toString) {
message = message.toString();
}
if (typeof message == "string") {
debug('responding', this.user, message.substring(0, 60));
resBuffer = new Buffer(message + "\r\n", "utf-8");
} else {
resBuffer = Buffer.concat([message, new Buffer("\r\n", "utf-8")]);
}
this.socket.write(resBuffer);
};
/**
* POP3Server#destroy() -> undefined
*
* Clears the used variables just in case (garbage collector should
* do this by itself)
**/
POP3Connnection.prototype.destroy = function destroy() {
const remoteAddress = this.socket ? this.socket.remoteAddress : null;
debug('destroying connection', this.user, this.connection_id, remoteAddress);
if (this.timer) {
clearTimeout(this.timer);
}
this.timer = null;
if (this.server.connected_users[this.user]) {
delete this.server.connected_users[this.user];
}
if (this.socket && this.socket.end) {
this.socket.end();
}
};
// kill client after inactivity
POP3Connnection.prototype.updateTimeout = function() {
if (this.timer) clearTimeout(this.timer);
this.timer = setTimeout((function() {
if (!this.socket)
return;
if (this.state == States.TRANSACTION) {
this.state = States.UPDATE;
}
debug("Connection closed for client inactivity", this.user);
this.destroy();
}).bind(this), TEN_MINUTES);
};