-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmailbox.js
84 lines (73 loc) · 2.13 KB
/
mailbox.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
var EventEmitter = require('events').EventEmitter;
var crypto = require('crypto');
var path = require('path');
var fs = require('fs');
var async = require('async');
var mkdirp = require('mkdirp');
function Mailbox(dir, create, cb, _exists) {
EventEmitter.call(this);
this.dir = dir;
this.uid_list = null;
this.uid_map = null;
this.flags_map = null;
this.uid_validity = 0;
this.uid_next = 1;
if(_exists) {
return this;
}
if(typeof create == 'function') {
cb = create;
create = false;
}
if(create) {
Mailbox.create(dir, this, cb);
return this;
}
this.open(cb);
}
module.exports = Mailbox;
Mailbox.prototype = Object.create(EventEmitter.prototype);
Mailbox.create = function(dir, open, cb) {
if(typeof open == 'function') {
cb = open;
open = false;
}
else {
// ensure not null nor undefined
open = open || false;
}
// cb is not mandatory when called from `new Maildir(dir, true)`
if(typeof cb != 'function' && (!open.constructor || open.constructor != Mailbox)) {
var e = new TypeError('A callback is required');
Error.captureStackTrace(e, Mailbox.create);
throw e;
}
function onError(err) {
if(typeof cb == 'function') {
cb(err);
}
else {
open.emit('error', err);
}
}
async.series({
_dir: mkdirp.bind(null, dir),
uid_list: fs.open.bind(fs, path.join(dir, 'uid-list'), 'w+'),
uid_map: fs.open.bind(fs, path.join(dir, 'uid-map'), 'w+'),
flags_map: fs.open.bind(fs, path.join(dir, 'flags-map'), 'w+')
}, function(err, res) {
if(err) {
return onError(err);
}
var uid_validity = crypto.randomBytes(4).readInt32BE(0) & 0x7fffffff;
});
};
Mailbox.prototype.open = function() {
async.parallel({
uid_list: fs.open.bind(fs, path.join(dir, 'uid-list'), 'r+'),
uid_map: fs.open.bind(fs, path.join(dir, 'uid-map'), 'r+'),
flags_map: fs.open.bind(fs, path.join(dir, 'flags-map'), 'r+')
}, function(err, res) {
//
});
};