Permalink
Browse files

- deliver message to an actual file

  • Loading branch information...
1 parent 9252b26 commit 7ca417ef36fefae8b1c53dfbb47452614172dde8 @aredridel committed Aug 23, 2010
Showing with 37 additions and 9 deletions.
  1. +12 −3 bin/smtpd
  2. +25 −6 lib/smtp.js
View
@@ -3,6 +3,9 @@
var smtp = require('smtp')
var fs = require('fs')
var sys = require('sys')
+var mailbox = require('mailbox')
+
+var destination = new mailbox.Maildir('test')
var server = smtp.createServer(function(conn) {
conn.on('MAIL FROM', function(sender) {
@@ -20,9 +23,15 @@ var server = smtp.createServer(function(conn) {
conn.on('EHLO', f)
conn.on('DATA', function(message) {
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'})
- sys.pump(message, mbox, function() {
- mbox.close()
+ message.pause()
+ destination.appendMessage(function(error, mbox) {
+ if(error) {
+ console.log(error)
+ } else {
+ sys.pump(message, mbox, function() {
+ mbox.close()
+ })
+ }
})
message.on('data', function(data) {
console.log('DATA: ' + data);
View
@@ -20,6 +20,9 @@ function Message() {
}
sys.inherits(Message, events.EventEmitter)
+Message.prototype.pause = function() { this.emit('pause') }
+Message.prototype.resume = function() { this.emit('resume') }
+
var debug;
var debugLevel = parseInt(process.env.NODE_DEBUG, 16);
if (debugLevel & 0x4) {
@@ -46,9 +49,12 @@ exports.createServer = function(clientListener) {
function connectionListener(socket) {
var In = new linebuffer.LineBuffer()
var Out = socket
- sys.pump(socket, In)
var self = this
+ sys.pump(socket, In, function() {
+ In.end()
+ })
+
debug("new smtp connection");
socket.setTimeout(2*60*1000); // 2 minute timeout
@@ -58,7 +64,11 @@ function connectionListener(socket) {
In.setEncoding('utf8');
Out.out = function(a){
- Out.write(a+'\r\n');
+ try {
+ Out.write(a+'\r\n')
+ } catch(e) {
+ Out.emit('error', e)
+ }
console.info('>'+a);
}
socket.on('connect', function () {
@@ -122,10 +132,19 @@ function connectionListener(socket) {
} else if(line.match(/^DATA\b/i)) {
socket.state = 'data';
Out.out('354 Enter mail, end with "." on a line by itself');
- socket.currentMessage = new Message()
- socket.currentMessage.recipients = socket.recipients
- socket.currentMessage.sender = socket.sender
- socket.currentMessage.connection = socket
+ var message = new Message()
+ socket.currentMessage = message
+ message.recipients = socket.recipients
+ 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)
} else if(line.match(/^NOOP\b/i)) {
Out.out('250 OK');

0 comments on commit 7ca417e

Please sign in to comment.