-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathcli.js
More file actions
288 lines (257 loc) · 9.22 KB
/
cli.js
File metadata and controls
288 lines (257 loc) · 9.22 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
const { Command } = require('commander');
const program = new Command();
const MsgReader = require('./lib/MsgReader').default;
const { props, typeNames } = require('./lib/Defs');
const { Reader, TypeEnum } = require('./lib/Reader');
const { burn } = require('./lib/Burner');
const fs = require('fs');
const path = require('path');
const { decompressRTF } = require('@kenjiuno/decompressrtf');
program
.command('parse <msgFilePath>')
.description('Parse msg file and print parsed structure')
.option('-f, --full-json', 'print full JSON')
.option('-i, --include-raw-props', 'include raw (and also unknown) props')
.action((msgFilePath, options) => {
const msgFileBuffer = fs.readFileSync(msgFilePath)
const testMsg = new MsgReader(msgFileBuffer)
testMsg.parserConfig = testMsg.parserConfig || {};
if (options.includeRawProps) {
testMsg.parserConfig.includeRawProps = true;
}
const testMsgInfo = testMsg.getFileData();
console.log(
options.fullJson
? JSON.stringify(testMsgInfo, null, 2)
: testMsgInfo
);
});
program
.command('rtf <msgFilePath> [saveToRtfFilePath]')
.description('Parse msg file and print decompressed rtf')
.action((msgFilePath, saveToRtfFilePath) => {
const msgFileBuffer = fs.readFileSync(msgFilePath)
const testMsg = new MsgReader(msgFileBuffer)
const testMsgInfo = testMsg.getFileData()
const body = Buffer.from(decompressRTF(testMsgInfo.compressedRtf))
if (typeof saveToRtfFilePath === "string" && saveToRtfFilePath.length >= 1) {
fs.writeFileSync(saveToRtfFilePath, body);
}
else {
console.log(body.toString("utf8"));
}
});
function listAttachmentsRecursively(fieldsData, delimiter) {
const attachments = []
const walk = (fieldsData, prefix, attachments) => {
for (const att of fieldsData.attachments) {
if (att.innerMsgContent) {
attachments.push({
fileName: prefix + att.name + ".msg",
attachmentRef: att,
})
walk(att.innerMsgContentFields, att.name + delimiter, attachments);
}
else {
attachments.push({
fileName: prefix + att.fileName,
attachmentRef: att,
})
}
}
}
walk(fieldsData, "", attachments)
return attachments
}
program
.command('list-att <msgFilePath>')
.description('Parse msg file and list attachment file names')
.action((msgFilePath) => {
const msgFileBuffer = fs.readFileSync(msgFilePath)
const testMsg = new MsgReader(msgFileBuffer)
const testMsgInfo = testMsg.getFileData()
const attachments = listAttachmentsRecursively(testMsgInfo, "_");
for (let attachment of attachments) {
console.log(attachment.fileName)
}
});
program
.command('save-att <msgFilePath> <saveToDir>')
.description('Parse msg file and write all attachment files')
.action((msgFilePath, saveToDir) => {
const msgFileBuffer = fs.readFileSync(msgFilePath)
const testMsg = new MsgReader(msgFileBuffer)
const testMsgInfo = testMsg.getFileData()
fs.mkdirSync(path.resolve(saveToDir), { recursive: true })
const attachments = listAttachmentsRecursively(testMsgInfo, "_");
for (let attachment of attachments) {
const attFilePath = path.resolve(saveToDir, attachment.fileName);
fs.writeFileSync(attFilePath, testMsg.getAttachment(attachment.attachmentRef).content)
}
});
program
.command('dump <msgFilePath>')
.description('Dump msg file and print data')
.option('-p, --print-raw-data', 'print raw data')
.action((msgFilePath, options) => {
const msgFileBuffer = fs.readFileSync(msgFilePath)
const testMsg = new MsgReader(msgFileBuffer)
let msgIndex = 0
testMsg.parserConfig = {
propertyObserver: (fields, tag, raw) => {
if (fields.msgIndex === undefined) {
fields.msgIndex = msgIndex++;
}
{
const key = tag.toString(16).padStart(8, "0").toUpperCase();
const prop = props.filter(it => it.key === key).shift();
const type = typeNames[parseInt(key.substr(4), 16)];
console.info(
"msgIdx:", fields.msgIndex,
"dataType:", `'${fields.dataType}'`,
"tag:", `0x${key}`,
"name:", prop && prop.name || null,
"type:", type && type || null,
"size:", raw && raw.byteLength,
"data:", options.printRawData ? raw : undefined,
)
}
}
}
const testMsgInfo = testMsg.getFileData()
});
program
.command('expose <msgFilePath> <exportToDir>')
.description('Expose files/folders in Compound File Binary Format (CFBF)')
.action((msgFilePath, exportToDir, options) => {
const msgFileBuffer = fs.readFileSync(msgFilePath);
const store = new Reader(msgFileBuffer);
store.parse();
function expose(folder, saveTo) {
fs.mkdir(saveTo, { recursive: true }, (err) => {
if (err) {
return;
}
for (let fileName of folder.fileNames()) {
const array = folder.readFile(fileName);
const path = saveTo + "/" + fileName;
console.info(path);
fs.writeFileSync(path, (array === null) ? [] : array);
}
for (let subFolder of folder.subFolders()) {
expose(subFolder, saveTo + "/" + subFolder.name);
}
});
}
expose(store.rootFolder(), exportToDir);
});
program
.command('makecfbf <msgFilePath> <importFromDir>')
.description('Create a Compound File Binary Format (CFBF) from files/folders')
.action((msgFilePath, importFromDir, options) => {
const entries = [
{
name: "Root Entry",
type: TypeEnum.ROOT,
children: [],
length: 0,
}
];
function addFolder(dir, parentIndex) {
for (const fileName of fs.readdirSync(dir)) {
const fullPath = path.join(dir, fileName);
const stat = fs.statSync(fullPath);
const index = entries.length;
entries[parentIndex].children.push(index);
if (stat.isDirectory()) {
entries.push({
name: fileName,
type: TypeEnum.DIRECTORY,
children: [],
length: 0,
});
addFolder(fullPath, index);
}
else {
entries.push({
name: fileName,
type: TypeEnum.DOCUMENT,
binaryProvider: () => {
const b = fs.readFileSync(fullPath);
const a = new Uint8Array(b, b.byteOffset, b.byteLength);
a.set(b);
return a;
},
length: stat.size,
});
}
}
}
addFolder(importFromDir, 0);
const array = burn(entries);
fs.writeFileSync(msgFilePath, array);
});
program
.command('html <msgFilePath>')
.description('Parse msg file and display 1013001f:bodyHtml or 10130102:html')
.option('-e, --encoding <encoding>', 'The encoding type to decode binary html.', 'utf8')
.action((msgFilePath, options) => {
const msgFileBuffer = fs.readFileSync(msgFilePath);
const testMsg = new MsgReader(msgFileBuffer);
const testMsgInfo = testMsg.getFileData();
if (testMsgInfo.html !== undefined) {
console.log(Buffer.from(testMsgInfo.html).toString(options.encoding));
}
else if (testMsgInfo.bodyHtml !== undefined) {
console.log(testMsgInfo.bodyHtml);
}
else {
console.warn("no html is contained.");
}
});
program
.command('walk <msgFilePath>')
.description('Walk entire msg file as a raw CFBF')
.action((msgFilePath, options) => {
const msgFileBuffer = fs.readFileSync(msgFilePath);
const reader = new Reader(msgFileBuffer);
reader.parse();
function walk(folder, prefix) {
console.info("Walking folder:", prefix);
for (let fileSet of folder.fileNameSets()) {
const contents = fileSet.provider();
console.info("Verify file:", fileSet.name, "(", fileSet.length, ")", "read", contents.length, "bytes");
if (fileSet.length != contents.length) {
throw new Error();
}
}
for (let subFolder of folder.subFolders()) {
walk(subFolder, `${prefix}${subFolder.name}/`);
}
}
walk(reader.rootFolder(), "/");
});
program
.command('dummy1')
.action(() => {
const msgFileBuffer = fs.readFileSync('test/msgInMsg.msg');
const testMsg = new MsgReader(msgFileBuffer);
const testMsgInfo = testMsg.getFileData();
const testMsgAttachment0 = testMsg.getAttachment(0);
console.log(testMsgAttachment0);
});
program
.command('dummy2')
.action(() => {
const msgFileBuffer = fs.readFileSync('test/msgInMsg.msg');
const testMsg = new MsgReader(msgFileBuffer)
const testMsgInfo = testMsg.getFileData()
for (const att of testMsgInfo.attachments) {
const attachment = testMsg.getAttachment(att);
console.log(attachment.fileName);
//console.log(Buffer.from(attachment.content).toString('base64'));
fs.writeFileSync("save-" + attachment.fileName, attachment.content);
}
});
program
.parse(process.argv);