-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
187 lines (150 loc) · 3.21 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
175
176
177
178
179
180
181
182
183
184
185
186
187
package main
import (
"flag"
"fmt"
"log"
"os"
"syscall"
"time"
"github.com/cheggaaa/pb/v3"
"github.com/loeffel-io/mail-downloader/search"
"gopkg.in/yaml.v3"
)
func main() {
var config *Config
// flags
configPath := flag.String("config", "", "config path")
from := flag.String("from", "", "from date")
to := flag.String("to", "", "to date")
flag.Parse()
// yaml
yamlBytes, err := os.ReadFile(*configPath)
if err != nil {
log.Fatal(err)
}
// yaml to config
err = yaml.Unmarshal(yamlBytes, &config)
if err != nil {
log.Fatal(err)
}
// imap
imap := &imap{
Username: config.Imap.Username,
Password: config.Imap.Password,
Server: config.Imap.Server,
Port: config.Imap.Port,
}
if err := imap.connect(); err != nil {
log.Fatal(err)
}
if err := imap.login(); err != nil {
log.Fatal(err)
}
imap.enableCharsetReader()
// Mailbox
_, err = imap.selectMailbox("INBOX")
if err != nil {
log.Fatal(err)
}
// search uids
fromDate, err := time.Parse("2006-01-02", *from) // yyyy-MM-dd ISO 8601
if err != nil {
log.Fatal(err)
}
toDate, err := time.Parse("2006-01-02", *to) // yyyy-MM-dd ISO 8601
if err != nil {
log.Fatal(err)
}
uids, err := imap.search(fromDate, toDate)
if err != nil {
log.Fatal(err)
}
// seqset
seqset := imap.createSeqSet(uids)
// channel
mailsChan := make(chan *mail)
// fetch messages
go func() {
if err = imap.fetchMessages(seqset, mailsChan); err != nil {
log.Fatal(err)
}
close(mailsChan)
}()
// start bar
fmt.Println("Fetching messages...")
bar := pb.StartNew(len(uids))
// mails
mails := make([]*mail, 0)
// fetch messages
for mail := range mailsChan {
mails = append(mails, mail)
bar.Increment()
}
// logout
if err := imap.Client.Logout(); err != nil {
log.Fatal(err)
}
// start bar
fmt.Println("Processing messages...")
bar.SetCurrent(0)
// process messages
for _, mail := range mails {
dir := mail.getDirectoryName(imap.Username)
if mail.Error != nil {
log.Println(mail.getErrorText())
bar.Increment()
continue
}
// attachments
for _, attachment := range mail.Attachments {
s := &search.Search{
Search: config.Attachments.Mimetypes,
Data: attachment.Mimetype,
}
if !s.Find() {
continue
}
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
log.Fatal(err)
}
if err = os.WriteFile(fmt.Sprintf("%s/%s", dir, attachment.Filename), attachment.Body, 0o644); err != nil {
log.Printf("attachment.Filename: %s", attachment.Filename)
if pe, ok := err.(*os.PathError); ok {
if pe.Err == syscall.ENAMETOOLONG {
log.Println(err.Error())
continue
}
}
log.Fatal(err)
}
}
// pdf
s := &search.Search{
Search: config.Mails.Subjects,
Data: mail.Subject,
}
if !s.Find() {
bar.Increment()
continue
}
bytes, err := mail.generatePdf()
if err != nil {
log.Println(err.Error())
bar.Increment()
continue
}
if bytes == nil {
bar.Increment()
continue
}
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
log.Fatal(err)
}
if err = os.WriteFile(fmt.Sprintf("%s/mail-%d.pdf", dir, mail.Uid), bytes, 0o644); err != nil {
log.Fatal(err)
}
bar.Increment()
}
// done
fmt.Println("Done")
}