-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmbox.js
185 lines (161 loc) · 4.4 KB
/
mbox.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
const {EventEmitter} = require('events')
class Mbox extends EventEmitter {
constructor(file, fs) {
super()
if (!fs) fs = require('fs')
this.fs = fs
this.lineend = '\n'
this.blockSize = 4096
this.index = []
this.done = false
this.on('message', message=>this.index.push(message))
this.on('end', ()=>this.done = true)
if (typeof file === 'number') {
this.fd = file
file = undefined
setImmediate(()=>this.checkMagic())
} else {
this.open(file)
}
}
open(file) {
this.fs.open(file, 'r', (err, fd)=>{
this.fd = fd
if (err) this.emit('err', err)
else this.checkMagic()
})
}
readMessage(i, cb) {
let msg = this.index[i]
let length = msg.headerLength + msg.contentLength
let buffer = Buffer.alloc(length)
this.fs.read(this.fd, buffer, 0, length, msg.pos, (err,count,data)=>{
if (err) this.emit('err', err)
else if (count !== length) this.emit('err', new Error(`Read ${count} bytes instead of ${length}.`))
else cb(data)
})
}
read(pos, cb) {
let buffer = Buffer.alloc(this.blockSize)
this.fs.read(this.fd, buffer, 0, this.blockSize, pos, cb)
}
parseHeaders(buffer) {
const headerString = buffer.toString()
const headerLines = headerString.split(this.lineend)
const headers = headerLines.reduce( (acc, cur)=>{
if (cur.startsWith(' ') || cur.startsWith('\t')) {
acc[acc.length - 1] = acc[acc.length - 1] + cur
} else {
acc.push(cur)
}
return acc
}, [])
const parsedHeaders = headers.reduce( (acc, cur)=>{
let [name, ...value] = cur.split(':')
value = value.join(':')
if (name.startsWith('From ')) {
name = "Envelope-From"
value = cur.slice(5)
}
if (name in acc) {
acc[name] = [].concat(acc[name], value)
} else {
acc[name] = value
}
return acc
}, {})
return parsedHeaders
}
analyzeMessage(headers, cb) {
if (headers.length === 0) {
return this.emit('end')
}
let parsedHeaders = this.parseHeaders(headers)
let contentLength = +parsedHeaders['Content-Length']
let messageData = {
pos: this.readPos,
headers: parsedHeaders,
headerLength: headers.length,
contentLength,
length: headers.length + contentLength + 3
}
if (isNaN(contentLength)) {
this.readPos += headers.length
this.findNextMessageStart(this.readPos, pos=>{
contentLength = pos - this.readPos
messageData.contentLength = contentLength
messageData.length = headers.length + contentLength
this.readPos = pos
cb(messageData)
})
} else {
this.readPos += messageData.length
cb(messageData)
}
}
findNextMessageStart(pos, cb) {
let needle = Buffer.from(this.lineend + "From ")
this.read(pos, (err, bytesRead, buffer)=>{
let p = buffer.indexOf(needle)
if (p === -1) {
this.findNextMessageStart(pos+bytesRead - needle.length, cb)
} else {
cb(pos + p + this.lineend.length + 1)
}
})
}
readMbox() {
this.readPos = 0
let next = (err, headers)=>{
if (err) return this.emit(err)
this.analyzeMessage(headers, message=>{
if (message.pos === 0 && message.headers.From.startsWith(' Mail System Internal Data')) {
this.emit('log', "Skipping Folder Internal Data")
} else {
this.emit('message', message)
}
this.readHeaders(this.readPos, next)
})
}
this.readHeaders(this.readPos, next)
}
checkMagic() {
this.read(0, (err, len, data)=>{
this.emit('log','checking magic')
if (err) return this.emit('err', err)
if (!data.slice(0,5).equals(Buffer.from('From ')))
return this.emit('err', new Error('Not an mbox file'))
this.emit('log','looks legit')
if (data.indexOf('\r') > 0) this.lineend = '\r\n'
this.readMbox()
})
}
readHeaders(pos, cb) {
let buffers = []
let lastBuffer = Buffer.alloc(0)
const needle = Buffer.from(this.lineend + this.lineend)
const readComplete = (err, bytesRead, buffer)=>{
if (err) return cb(err)
if (bytesRead < this.blockSize) {
buffers.push(buffer.slice(0, bytesRead))
cb(null, Buffer.concat(buffers))
} else {
let haystack = Buffer.concat([lastBuffer, buffer])
let npos = haystack.indexOf(needle)
let offset = lastBuffer.length
lastBuffer = buffer
if (npos < 0) {
buffers.push(buffer)
pos += buffer.length
this.read(pos, readComplete)
} else {
let sliceLength = npos - offset
buffers.push(buffer.slice(0, sliceLength))
cb(null, Buffer.concat(buffers))
}
}
}
this.read(pos, readComplete)
}
}
module.exports = Mbox