Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit 1c5c9f349514d6e19edbb6c9eb5765bc0cc635f1 @aredridel committed Aug 23, 2010
Showing with 46 additions and 0 deletions.
  1. +38 −0 lib/mailbox.js
  2. +8 −0 test.js
@@ -0,0 +1,38 @@
+var events = require('events')
+var sys = require('sys')
+var fs = require('fs')
+
+function Maildir(path) {
+ this.path = path
+ events.EventEmitter.call(this)
+}
+sys.inherits(Maildir, events.EventEmitter)
+
+Maildir.prototype.appendMessage = function(callback) {
+ var messageName = generateMessageName()
+ var s = fs.createWriteStream(this.path + '/tmp/' + messageName,
+ {flags: process.O_CREAT | process.O_EXCL | process.O_WRONLY
+ , mode: 0600}
+ )
+ var self = this
+ s.on('error', function(err) {
+ console.log(err)
+ if(err.message.match(/EEXIST/)) {
+ self.appendMessage(callback)
+ } else {
+ s.destroy()
+ }
+ })
+ s.on('open', function(fd) {
+ if(callback) callback(s)
+ })
+ s.filename = messageName
+ return s
+}
+
+function generateMessageName() {
+ var d = new Date()
+ return d.valueOf() + "." + process.pid + "." + d.getMilliseconds()
+}
+
+exports.Maildir = Maildir
@@ -0,0 +1,8 @@
+var mailbox = require('mailbox')
+
+var maildir = new mailbox.Maildir('test')
+
+maildir.appendMessage(function(s) {
+ s.write('Subject: test message\r\n\r\nWhy hello thar!')
+})
+

0 comments on commit 1c5c9f3

Please sign in to comment.