Skip to content

Commit

Permalink
- deliver message to an actual file
Browse files Browse the repository at this point in the history
  • Loading branch information
aredridel committed Aug 23, 2010
1 parent 9252b26 commit 7ca417e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
15 changes: 12 additions & 3 deletions bin/smtpd
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
var smtp = require('smtp') var smtp = require('smtp')
var fs = require('fs') var fs = require('fs')
var sys = require('sys') var sys = require('sys')
var mailbox = require('mailbox')

var destination = new mailbox.Maildir('test')


var server = smtp.createServer(function(conn) { var server = smtp.createServer(function(conn) {
conn.on('MAIL FROM', function(sender) { conn.on('MAIL FROM', function(sender) {
Expand All @@ -20,9 +23,15 @@ var server = smtp.createServer(function(conn) {
conn.on('EHLO', f) conn.on('EHLO', f)
conn.on('DATA', function(message) { conn.on('DATA', function(message) {
console.log("Message incoming! From " + message.sender.address + " to " + message.recipients.map(function(e) { return e.address }).join(', ')) console.log("Message incoming! From " + message.sender.address + " to " + message.recipients.map(function(e) { return e.address }).join(', '))
var mbox = fs.createWriteStream('mbox', {flags: 'a'}) message.pause()
sys.pump(message, mbox, function() { destination.appendMessage(function(error, mbox) {
mbox.close() if(error) {
console.log(error)
} else {
sys.pump(message, mbox, function() {
mbox.close()
})
}
}) })
message.on('data', function(data) { message.on('data', function(data) {
console.log('DATA: ' + data); console.log('DATA: ' + data);
Expand Down
31 changes: 25 additions & 6 deletions lib/smtp.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ function Message() {
} }
sys.inherits(Message, events.EventEmitter) sys.inherits(Message, events.EventEmitter)


Message.prototype.pause = function() { this.emit('pause') }
Message.prototype.resume = function() { this.emit('resume') }

var debug; var debug;
var debugLevel = parseInt(process.env.NODE_DEBUG, 16); var debugLevel = parseInt(process.env.NODE_DEBUG, 16);
if (debugLevel & 0x4) { if (debugLevel & 0x4) {
Expand All @@ -46,9 +49,12 @@ exports.createServer = function(clientListener) {
function connectionListener(socket) { function connectionListener(socket) {
var In = new linebuffer.LineBuffer() var In = new linebuffer.LineBuffer()
var Out = socket var Out = socket
sys.pump(socket, In)
var self = this var self = this


sys.pump(socket, In, function() {
In.end()
})

debug("new smtp connection"); debug("new smtp connection");


socket.setTimeout(2*60*1000); // 2 minute timeout socket.setTimeout(2*60*1000); // 2 minute timeout
Expand All @@ -58,7 +64,11 @@ function connectionListener(socket) {


In.setEncoding('utf8'); In.setEncoding('utf8');
Out.out = function(a){ Out.out = function(a){
Out.write(a+'\r\n'); try {
Out.write(a+'\r\n')
} catch(e) {
Out.emit('error', e)
}
console.info('>'+a); console.info('>'+a);
} }
socket.on('connect', function () { socket.on('connect', function () {
Expand Down Expand Up @@ -122,10 +132,19 @@ function connectionListener(socket) {
} else if(line.match(/^DATA\b/i)) { } else if(line.match(/^DATA\b/i)) {
socket.state = 'data'; socket.state = 'data';
Out.out('354 Enter mail, end with "." on a line by itself'); Out.out('354 Enter mail, end with "." on a line by itself');
socket.currentMessage = new Message() var message = new Message()
socket.currentMessage.recipients = socket.recipients socket.currentMessage = message
socket.currentMessage.sender = socket.sender message.recipients = socket.recipients
socket.currentMessage.connection = socket message.sender = socket.sender
message.connection = socket
message.on('pause', function() {
console.log('paused')
In.pause()
})
message.on('resume', function() {
console.log('resumed')
In.resume()
})
socket.emit('DATA', socket.currentMessage) socket.emit('DATA', socket.currentMessage)
} else if(line.match(/^NOOP\b/i)) { } else if(line.match(/^NOOP\b/i)) {
Out.out('250 OK'); Out.out('250 OK');
Expand Down

0 comments on commit 7ca417e

Please sign in to comment.