-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand-flow.js
More file actions
171 lines (160 loc) · 4.38 KB
/
command-flow.js
File metadata and controls
171 lines (160 loc) · 4.38 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
const meta = require('./package')
const debug = require('debug')(meta.name + ':command-flow')
const tty = require('tty')
const fs = require('fs')
const _ = require('lodash')
const async = require('async')
const expand = require('expand-tilde')
const chalk = require('chalk')
const printf = require('printf')
const progress = require('cli-progress')
exports.command = 'flow [options]'
exports.describe = 'Export flow information from an email archive'
exports.builder = (yargs) => {
yargs.options({
'provider': {
demandOption: true
},
'secret': {},
'output': {},
'user-words': {},
'domain-words': {},
'mac-index': {},
'imap-host': {},
'imap-port': {},
'imap-tls': {},
'imap-filter': {},
'imap-invert': {}
})
yargs.check((argv) => {
switch (argv['provider']) {
case 'imap':
if (!argv['imap-host'])
throw new Error('Missing required argument: imap-host')
default:
return true
}
})
}
exports.handler = (argv) => {
const streams = {
info: tty.isatty(process.stdout.fd) ? process.stdout : process.stderr,
data: argv.output ? fs.createWriteStream(argv.output) : process.stdout,
mapping: argv.mapping && fs.createWriteStream(argv.mapping)
}
const anonymizer = require('./lib/anonymizer')({
secret: argv['secret'],
words: {
user: argv['user-words'],
domain: argv['domain-words']
}
})
async.waterfall([
(callback) => {
let credentials
switch (argv.provider) {
case 'imap':
credentials = require('./lib/imap-credentials')({
output: streams.info
})
return credentials.get(callback)
case 'gmail':
credentials = require('./lib/gmail-credentials')({
output: streams.info
})
return credentials.get(callback)
default:
return callback(null, null)
}
},
(credentials, callback) => {
switch (argv.provider) {
case 'imap':
credentials.host = argv['imap-host']
credentials.port = argv['imap-port']
credentials.tls = argv['imap-tls']
return callback(null, credentials)
case 'gmail':
credentials.host = 'imap.gmail.com'
credentials.port = 993
credentials.tls = true
return callback(null, credentials)
default:
return callback(null, null)
}
},
(options, callback) => {
let provider
switch (argv.provider) {
case 'mac':
provider = require('./lib/mac-provider')({
path: expand(argv['mac-index'])
})
return callback(null, provider)
case 'imap':
case 'gmail':
provider = require('./lib/imap-provider')({
imap: options,
filter: argv['imap-filter'] && new RegExp(argv['imap-filter']),
invert: argv['imap-invert']
})
return callback(null, provider)
}
},
(provider, callback) => {
if (argv.output || !tty.isatty(process.stdout.fd)) {
const bar = new progress.Bar({
format: '{bar} {percentage}% | processing message {value} of {total}',
barsize: 20,
clearOnComplete: true,
stream: streams.info
}, progress.Presets.shades_grey)
bar.start()
provider.on('progress', (state) => {
bar.setTotal(state.total)
bar.update(state.actual)
})
provider.on('end', () => {
bar.stop()
})
}
return callback(null, provider)
},
(provider, callback) => {
printf(streams.data, 'id,date,type,sender,receiver\n')
provider.on('data', (row) => {
row.sender = anonymizer.map(row.sender)
row.receiver = anonymizer.map(row.receiver)
printf(streams.data, '%s,%s,%s,%s,%s\n',
row.id,
row.date.toISOString(),
row.type, row.sender, row.receiver)
})
provider.on('end', callback)
provider.on('error', callback)
},
(callback) => {
if (streams.mapping) {
printf(streams.mapping, 'anonymized,original\n')
_.forEach(anonymizer.mappings.addresses, (reals, fake) => {
_.forEach(reals, (bool, real) => {
printf(streams.mapping, '%s,%s\n', fake, real)
})
})
}
return callback()
}
], (err) => {
if (err) throw err
printf(streams.info,
chalk.bold('domain anonymization produced %d conflicts\n'),
anonymizer.conflicts.domains)
printf(streams.info,
chalk.bold('address anonymizazion produced %d conflicts\n'),
anonymizer.conflicts.addresses)
if (anonymizer.conflicts.domains + anonymizer.conflicts.addresses > 0)
printf(streams.info,
chalk.bold('you may want to increase user and domain words',
'to reduce conflicts\n'))
})
}