-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
174 lines (143 loc) · 3.26 KB
/
main.go
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
package main
import (
"flag"
"fmt"
"github.com/d1str0/sse"
"io/ioutil"
"net/mail"
"os"
"strconv"
)
var mailDir string // Directory to load
var fileCount int
var dirCount int
type Identity struct {
password string
salt string
iter int
}
var id = Identity{
password: "hunter2",
salt: "So Salty",
iter: 4096,
}
var c *sse.Client
func main() {
flag.StringVar(&mailDir, "mail-dir", "", "directory to load mail archives from")
flag.Parse()
var db sse.DBConn
db, err := sse.BoltDBOpen()
if err != nil {
fmt.Printf("Error creating BoltDB database: %v", err)
os.Exit(1)
}
c, err = sse.NewClient(db)
if err != nil {
fmt.Printf("Error creating new client: %v", err)
os.Exit(1)
}
c.SetKey("hunter2", "farts", 4096)
ReadAllFiles(mailDir)
fmt.Printf("%d total files in %d different directories!\n", fileCount, dirCount)
ids, err := c.Search("pallen@enron.com")
if err != nil {
fmt.Printf("Error searching database: %v", err)
os.Exit(1)
}
fmt.Println("Found these IDs:")
for _, id := range ids {
fmt.Println(id)
}
}
func ReadAllFiles(filename string) {
f, err := os.Open(filename)
if err != nil {
fmt.Printf("Error opening file %s: %s\n", filename, err.Error())
os.Exit(1)
}
stat, err := f.Stat()
if err != nil {
fmt.Printf("Error opening file stat %s: %s\n", filename, err.Error())
os.Exit(1)
}
if stat.IsDir() {
files, err := f.Readdir(0)
if err != nil {
fmt.Printf("Error reading directory %s: %s\n", stat.Name(), err.Error())
os.Exit(1)
}
fmt.Printf("%s/\n", stat.Name())
err = f.Close()
if err != nil {
fmt.Printf("Error closing directory %s: %s\n", stat.Name(), err.Error())
os.Exit(1)
}
for _, file := range files {
ReadAllFiles(fmt.Sprintf("%s/%s", filename, file.Name()))
}
dirCount++
return
}
ParseMail(f)
err = f.Close()
if err != nil {
fmt.Printf("Error closing file %s: %s\n", stat.Name(), err.Error())
os.Exit(1)
}
fileCount++
}
func ParseMail(f *os.File) {
m, err := mail.ReadMessage(f)
if err != nil {
fmt.Printf("Error reading mail message: %v\n", err)
stat, err := f.Stat()
if err != nil {
fmt.Printf("Error reading mail message: %v, %s\n", err, stat.Name())
}
return
}
/*
stat, err := f.Stat()
if err != nil {
fmt.Printf("Error reading mail message: %v\n", err)
os.Exit(1)
}
*/
header := m.Header
tags := []string{}
addrs, err := mail.ParseAddressList(header.Get("From"))
if err != nil {
//fmt.Printf("Error reading mail message: %v, %s\n", err, stat.Name())
} else {
for _, addr := range addrs {
tags = append(tags, addr.Address)
}
}
addrs, err = mail.ParseAddressList(header.Get("To"))
if err != nil {
//fmt.Printf("Error reading mail message: %v, %s\n", err, stat.Name())
} else {
for _, addr := range addrs {
tags = append(tags, addr.Address)
}
}
body, err := ioutil.ReadAll(f)
if err != nil {
fmt.Printf("Error reading all of mail file: %v\n", err)
os.Exit(1)
}
id := strconv.Itoa(fileCount)
err = c.Put(id, body)
if err != nil {
fmt.Printf("Error putting mail file in DB: %v\n", err)
os.Exit(1)
}
for _, tag := range tags {
err = c.AddDocToKeyword(tag, id)
if err != nil {
fmt.Printf("Error adding keyword to doc: %v\n", err)
os.Exit(1)
}
}
//fmt.Printf("Added document %d\n", fileCount)
}