-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
executable file
·272 lines (229 loc) · 7.38 KB
/
index.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
'use strict';
var BPromise = require('bluebird');
var simplesmtp = require('simplesmtp');
var fs = BPromise.promisifyAll(require('fs'));
var compress = require('compression');
var MailParser = require('mailparser').MailParser;
var EventEmitter = require('events').EventEmitter;
var knex = require('knex');
var inherits = require('inherits');
var _ = require('lodash');
var JSONStream = require('JSONStream');
module.exports = RestSmtpSink;
inherits(RestSmtpSink, EventEmitter);
function RestSmtpSink(options) {
EventEmitter.call(this);
var self = this;
self.smtpport = options.smtp || 2525;
self.httpport = options.listen || 2526;
self.filename = options.file || 'rest-smtp-sink.sqlite';
this.setMaxListeners(Infinity);
}
RestSmtpSink.prototype.start = function() {
var self = this;
return self.createSchema()
.then(function () {
self.createSmtpSever();
self.smtp.listen(self.smtpport);
self.emit('info', 'SMTP server listening on port ' + self.smtpport);
self.server = self.createWebServer().listen(self.httpport, function() {
self.emit('info', 'HTTP server listening on port ' + self.httpport);
});
})
}
RestSmtpSink.prototype.createSchema = function () {
var self = this;
self.db = knex({
client: 'sqlite3',
connection: { filename: self.filename }
});
return self.db.schema.createTable('emails', function (table) {
table.increments();
table.timestamps();
['html','text','headers','subject','messageId','priority','from','to']
.map(function (id) { table.json(id) });
})
.catch(function (err) {
if (~err.message.indexOf('SQLITE_ERROR: table "emails" already exists')) {
self.emit('info', err.message);
} else {
self.emit('error', err);
throw err;
}
})
}
RestSmtpSink.prototype.createSmtpSever = function() {
var self = this;
self.smtp = simplesmtp.createServer({
enableAuthentication: true,
requireAuthentication: false,
SMTPBanner: 'rest-smtp-sink',
disableDNSValidation: true
});
self.smtp.on("startData", function(connection){
connection.mailparser = new MailParser();
connection.mailparser.on("end", function(mail_object){
self.db('emails')
.insert({
"created_at": new Date() ,
"updated_at": new Date() ,
'html': JSON.stringify(mail_object.html) ,
'text': JSON.stringify(mail_object.text) ,
'headers': JSON.stringify(mail_object.headers) ,
'subject': JSON.stringify(mail_object.subject) ,
'messageId': JSON.stringify(mail_object.messageId) ,
'priority': JSON.stringify(mail_object.priority) ,
'from': JSON.stringify(connection.from) ,
'to': JSON.stringify(connection.to)
})
.then(function (record) {
self.db('emails')
.select('*')
.where('id', '=', record[0]) // primary key from DB
.then(function (mail) {
self.emit('email', self.deserialize(mail[0]));
});
connection.donecallback(null, record);
});
});
});
self.smtp.on("data", function(connection, chunk){
connection.mailparser.write(chunk);
});
self.smtp.on("dataReady", function(connection, callback){
connection.donecallback = callback;
connection.mailparser.end();
});
}
RestSmtpSink.prototype.deserialize = function (o) {
o.html = JSON.parse(o.html)
o.text = JSON.parse(o.text)
o.headers = JSON.parse(o.headers)
o.subject = JSON.parse(o.subject)
o.messageId = JSON.parse(o.messageId)
o.priority = JSON.parse(o.priority)
o.from = JSON.parse(o.from)
o.to = JSON.parse(o.to)
return o;
}
RestSmtpSink.prototype.createWebServer = function () {
var self = this;
var express = require('express');
var app = express();
app.use(compress());
app.get('/', function(req, res){
res.set('Content-Type', 'text/html');
// Yes, this is valid HTML 5! According to the specs, the <html>, <head> and <body>
// tags can be omitted, but their respective DOM elements will still be there
// implicitly when a browser renders that markup.
res.write('rest-smtp-sink'
+ '<br><br>SMTP server listening on port ' + _.escape(self.smtpport)
+ '; HTTP listening on port ' + _.escape(self.httpport)
+ '<br>Note: This page dynamically updates as email arrives.'
+ '<br><br>API'
+ '<br><a href="/api/email">All Emails ( /api/email )</a>'
+ '<br><a href="/api/email">All Email, streamed, may load faster ( /api/email/stream )</a>'
+ '<br><a href="/api/email/latest">Last received Email</a> ( /api/email/latest )'
);
res.write('<table><thead><tr><td>ID<td>Del<td>Purge<td>To<td>From<td>Subject<td>Date</thead><tbody>');
res.flush(); // make sure the above data gets sent, so it doesn't look like the page is hanging.
function render_item(item) {
return '<tr><td><a href="/api/email/' + _.escape(item.id) + '">' + _.escape(item.id) + '</a>'
+ '<td><a href="/api/email/delete/' + _.escape(item.id) + '"> Del </a>'
+ '<td><a href="/api/email/purge/' + _.escape(item.id) + '"> Purge </a>'
+ '<td>' + _.escape(item.to)
+ '<td>' + _.escape(item.from)
+ '<td>' + _.escape(item.subject)
+ '<td>' + _.escape(new Date(item.created_at))
}
self.db.select('*').from('emails')
.then(function (resp) {
resp.forEach(self.deserialize);
resp.forEach(function (item) {
res.write(render_item(item));
res.flush();
});
});
var listener = function (item) {
res.write(render_item(item));
res.flush();
}
self.on('email', listener);
req.on('close', function () {
self.removeListener('email', listener);
})
});
app.get('/api/email', function(req, res, next){
self.db.select('*').from('emails')
.then(function (resp) {
resp.forEach(self.deserialize);
res.json(resp);
})
.catch(next)
});
app.get('/api/email/stream', function(req, res, next){
var stream = self.db.select('*').from('emails').stream()
stream.pipe(JSONStream.stringify('[',
',',
']')).pipe(res);
stream.on('end', function() {
res.end();
});
});
app.get('/api/email/latest', function(req, res, next){
self.db.select('*').from('emails')
.orderBy('id', 'desc')
.limit(1)
.then(function (resp) {
if (resp.length < 1) {
res.status(404).send('Not found')
} else {
res.json(self.deserialize(resp[0]));
}
})
.catch(next)
});
app.get('/api/email/:id', function(req, res, next){
self.db.select('*').from('emails')
.where('id', '=', req.params.id)
.then(function (resp) {
if (resp.length < 1) {
res.status(404).send('Not found')
} else {
res.json(self.deserialize(resp[0]));
}
})
.catch(next)
});
app.get('/api/email/delete/:id', function(req, res, next){
self.db.select('*').from('emails')
.where('id', '=', req.params.id)
.then(function (resp) {
if (resp.length < 1) {
res.status(404).send('Not found')
} else {
self.db('emails')
.where('id', '=', req.params.id)
.del().exec(function(err,resp) {console.log(resp)});
res.status(200).send('Removed');
}
})
.catch(next)
});
app.get('/api/email/purge/:id', function(req, res, next){
self.db.select('*').from('emails')
.where('id', '<=', req.params.id)
.then(function (resp) {
if (resp.length < 1) {
res.status(404).send('Not found')
} else {
self.db('emails')
.where('id', '<=', req.params.id)
.del().exec(function(err,resp) {console.log(resp)});
res.status(200).send('Purged records older than ' + req.params.id);
}
})
.catch(next)
});
return app;
}