-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmbox.js
More file actions
102 lines (84 loc) 路 2.62 KB
/
Copy pathmbox.js
File metadata and controls
102 lines (84 loc) 路 2.62 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
import tld from 'tldjs'
import _ from 'lodash'
import stringify from 'csv-stringify/lib/browser/sync'
// import tosdr from './tosdr'
// const breaches = []
// const getScore = (site) => {
// const tosdrClass = tosdr[String(site)]?.class
// const tosdrScore = tosdr[String(site)]?.score
// if (tosdrClass || tosdrScore) {
// let score = 5
// // asign a score value to the classes/scores provided in the JSON file
// if (tosdrClass === 'A') {
// score = 0
// } else if (tosdrClass === 'B') {
// score = 1
// } else if (tosdrClass === 'D' || tosdrScore > 150) {
// score = 10
// } else if (tosdrClass === 'C' || tosdrScore > 100) {
// score = 7
// }
// return score
// }
// }
const toCsv = (data) => {
// const headers = Object.keys(data[0])
// 'Data Types',
// 'Privacy Score',
// 'Breach',
// 'Usage',
const headers = ['Domain', 'Joined', 'Needed', 'Reclaimed']
const contents = data.map((row) => Object.values(row))
const csv = stringify([headers, ...contents])
return csv
}
const toUrl = (csv) => {
const blob = new Blob(['\uFEFF', csv])
const href = URL.createObjectURL(blob)
const name = `mbox.csv`
return {
href,
name,
}
}
export const parseMbox = (mbox) => {
const messages = mbox.split('\nFrom ').map((message) => {
const line = message.replace('From ', '').split('\n')[0]
const when = line.split(' ')
const sender = when.shift()
const match = message.match(/From: .*@.*/g)
const senderGmail =
match && match[0].match(/(<.*@.*>)|\S*@.*/g)[0].replace(/<|>/g, '')
const date = new Date(when.join(' '))
let senderDomain = sender && sender.split('@')[1]
if (senderDomain === 'xxx')
senderDomain = senderGmail && senderGmail.split('@')[1]
const site = tld.getDomain(senderDomain) || senderDomain
const result = {
date,
domain: site,
}
return result
})
const groups = _.groupBy(messages, (message) => message.domain)
// let csv = 'Domain, Date Joined';
const companies = Object.values(groups).map((group) => {
const first = _.sortBy(group, ['date'])[0]
const { domain } = first
// const privacyScore = getScore(first.domain)
// const breach = breaches.find((breach) => breach.Domain === first.domain)
return {
domain,
joined: new Date(first.date).toDateString(),
}
// return {
// domain,
// joined: first.date,
// ...(privacyScore ? { privacyScore } : {}),
// ...(breach ? { breach } : {}),
// }
})
const csv = toCsv(companies)
const { name, href } = toUrl(csv)
return { companies, csv, name, href }
}