forked from unkaktus/mm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
327 lines (288 loc) · 7.97 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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// main.go - Move from POP3 to Maildir
package main
import (
"crypto/tls"
"fmt"
"io/ioutil"
"log"
"net"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"sync"
"time"
"golang.org/x/net/proxy"
"gopkg.in/yaml.v2"
)
const version = "1.15.5"
type Config struct {
Username string
Password string
TLSDomain string
Port string
EntryServer string
ProxyPort string
TLS bool
Keep bool
Maildir string
Active bool
}
type writer struct{}
var (
self = ""
home = ""
accounts = make(map[string]string)
wg sync.WaitGroup
)
func usage(msg string) { // I:self,version
fmt.Print(self + " v" + version + ` - Move from POP3 to Maildir
* Downloading emails from POP3 servers and moving them into Maildir folders.
* Repo: github.com/pepa65/m2m
* Usage: m2m [ -h|--help | -q|--quiet ]
-h/--help: Output this help text.
-q/--quiet: Output only on critical errors (on 'stderr').
No flag: A minimal report is sent to 'stdout' (nothing on no mails),
and any additional verbose output is logged to 'stderr'.
* The directory '~/.m2m.conf' contains all the account config files, which
are checked concurrently. The filename is taken as the account name.
Lockfiles '.ACCOUNT_locked' get placed here when an account gets checked.
* Parameter names (lowercase!) in the configuration files:
active: true/false Account is active [default] or not
username: POP3 username [mandatory]
password: POP3 password [mandatory]
tlsdomain: Server domainname (as in its certificate) [mandatory]
port: Port [default: 995]
entryserver: Initial server IP/Domainname [default: not used]
proxyport: Proxy server (server:port) [default: not used]
tls: true/false Use TLS [default], or not
keep: true/false Keep mails on POP3 server, or delete them [default]
maildir: Path to the Maildir directory [default: '~/Maildir']
`)
if msg != "" { // Critical message
fmt.Fprintf(os.Stderr, "\n%v\n", msg)
os.Exit(1)
}
os.Exit(0)
}
func (w writer) Write(bytes []byte) (int, error) {
s := fmt.Sprint(time.Now().String()[:23] + " " + string(bytes))
fmt.Fprint(os.Stderr, s)
return len(s), nil
}
func main() { // I:accounts O:self,home IO:wg
selfparts := strings.Split(os.Args[0], "/")
self = selfparts[len(selfparts)-1]
if len(os.Args) > 2 { // Critical message
usage("Only 1 (optional) argument allowed: -h/--help / -q/--quiet")
}
quiet := false
if len(os.Args) == 2 {
if os.Args[1] == "-h" || os.Args[1] == "--help" {
usage("")
} else if os.Args[1] == "-q" || os.Args[1] == "--quiet" {
quiet = true
} else { // Critical message
usage("The only argument allowed is: -h/--help / -q/--quiet")
}
}
log := log.New(new(writer), "- ", log.Lmsgprefix)
var err error
home, err = os.UserHomeDir()
if err != nil { // Critical message
log.Fatal(err)
}
cfgpath := filepath.Join(home, ".m2m.conf")
dir, err := os.Open(cfgpath)
if err != nil { // Critical message
log.Fatal(err)
}
files, err := dir.Readdirnames(0)
if err != nil { // Critical message
log.Fatal(err)
}
start := time.Now()
sort.Strings(files)
for _, file := range files {
if file[0:1] != "." {
wg.Add(1)
go check(file, cfgpath, quiet)
}
}
wg.Wait()
duration := time.Since(start).Seconds()
if !quiet {
mails := false
logline := time.Now().Format("2006-01-02_15:04:05 ")
for _, file := range files {
n := accounts[file]
if n != "" {
logline += file + ": " + n + " "
if n != "0" {
mails = true
}
}
}
if mails {
fmt.Printf("%s(%.3fs) ", logline, duration)
}
}
if !quiet {
log.Printf("Server checking time: %fs", duration)
}
}
func unpanic() {
recover()
}
func check(account string, m2mdir string, quiet bool) { // I:home O:accounts IO:wg
defer unpanic()
defer wg.Done()
log := log.New(new(writer), account + ": ", log.Lmsgprefix)
lockfile := filepath.Join(m2mdir, "." + account + "_locked")
file, err := os.Open(lockfile)
if err == nil { // Account locked: skip
file.Close()
log.Panic("Locked")
}
filename := filepath.Join(m2mdir, account)
cfgdata, err := ioutil.ReadFile(filename)
if err != nil { // Critical message
log.Panic(err)
}
var cfg Config
// Default values
cfg.Port = "995"
cfg.TLS = true
cfg.Maildir = filepath.Join(home, "Maildir")
cfg.Active = true
err = yaml.UnmarshalStrict(cfgdata, &cfg)
if err != nil { // Critical message
log.Panic("Error in config file '" + filename + "'\n" + err.Error())
}
if !cfg.Active && !quiet {
log.Panic("Inactive")
}
if cfg.Username == "" { // Critical message
log.Panic("Missing 'username' in configfile '" + filename + "'")
}
if cfg.TLSDomain == "" && cfg.TLS == true { // Critical message
log.Panic("Missing 'tlsdomain' in configfile '" + filename + "' while TLS required")
}
// Lock account before going online
file, err = os.OpenFile(lockfile, os.O_CREATE, 0400)
if err != nil {
log.Panic("Cannot create lock file '" + lockfile + "'")
}
defer os.Remove(lockfile)
defer file.Close()
var dialer Dialer
dialer = &net.Dialer{}
if cfg.ProxyPort != "" {
dialer, err = proxy.SOCKS5("tcp", cfg.ProxyPort, nil, proxy.Direct)
if err != nil { // Critical message
log.Panic(err)
}
}
var conn net.Conn
if cfg.EntryServer != "" {
conn, err = dialer.Dial("tcp", cfg.EntryServer+":"+cfg.Port)
} else {
conn, err = dialer.Dial("tcp", cfg.TLSDomain+":"+cfg.Port)
}
if err != nil { // Critical message
log.Panic(err)
}
defer conn.Close()
if cfg.TLS {
tlsConfig := &tls.Config{ServerName: cfg.TLSDomain}
tlsConn := tls.Client(conn, tlsConfig)
if err != nil { // Critical message
log.Panic(err)
}
conn = tlsConn
}
buf := make([]byte, 255)
n, err := conn.Read(buf)
if err != nil { // Critical message
log.Panic(err)
}
ok, msg, err := ParseResponseLine(string(buf[:n]))
if err != nil { // Critical message
log.Panic(err)
}
if !ok { // Critical message
log.Panic("Server error: " + msg)
}
popConn := NewPOP3Conn(conn)
popConn.Cmd("UTF8")
line, err := popConn.Cmd("USER %s", cfg.Username)
if err != nil { // Critical message
log.Panic(err)
}
line, err = popConn.Cmd("PASS %s", cfg.Password)
if err != nil { // Critical message
log.Panic(err)
}
line, err = popConn.Cmd("STAT")
if err != nil { // Critical message
log.Panic(err)
}
stat := strings.Split(line, " ")
if len(stat) != 2 { // Critical message
log.Panic("STAT response malformed: " + line)
}
nmsg, err := strconv.Atoi(stat[0])
if err == nil {
accounts[account] = stat[0]
} else { // Critical message
log.Panic("Malformed number of messages: " + stat[0])
}
boxsize, err := strconv.Atoi(stat[1])
if err != nil { // Critical message
log.Panic("Malformed mailbox size: " + stat[1])
}
if !quiet {
log.Printf("%d messages %d bytes", nmsg, boxsize)
}
delerrs := 0
for i := 1; i <= nmsg; i++ {
line, data, err := popConn.CmdMulti("RETR %d", i)
if err != nil { // Critical message
log.Printf("Error retrieving message %d/%d: %s", i, nmsg, err.Error())
continue
}
size, _, ok := strings.Cut(line, " ")
if !ok && !quiet {
log.Printf("RETR response malformed for message %d/%d: %s", i, nmsg, line)
}
_, err = strconv.Atoi(size)
if err != nil && !quiet {
log.Printf("Malformed size for message %d/%d: %s", i, nmsg, size)
size = "?"
}
if !quiet {
log.Printf("Fetched message %d/%d (%s bytes)", i, nmsg, size)
}
err = SaveToMaildir(cfg.Maildir, data)
if err != nil { // Critical message
log.Printf("Error saving mesage %d/%d to maildir %s: %s", i, nmsg, cfg.Maildir, err.Error())
continue
}
if !cfg.Keep {
line, err = popConn.Cmd("DELE %d", i)
if err != nil { // Critical message
delerrs += 1
log.Printf("Error deleting mesage %d/%d from the server: %s", i, nmsg, err.Error())
}
}
}
if !quiet && nmsg > 0 {
if cfg.Keep {
log.Print("Not deleting messages from the server")
} else {
log.Printf("Messages deleted from the server: %d/%d", nmsg-delerrs, nmsg)
}
}
popConn.Cmd("QUIT")
}