-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
94 lines (90 loc) · 1.81 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
package main
import (
"bufio"
"flag"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"strings"
"time"
)
var (
dirpath = flag.String("d", "", "maildir to be scanned")
noop = flag.Bool("n", false, "scan the maildir but do not delete any files")
messageHeader = "message-id:"
)
func main() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage for %s\n", filepath.Base(os.Args[0]))
flag.PrintDefaults()
}
flag.Parse()
var (
mail = make(map[string]bool)
start = time.Now()
mails, dups int
)
if *dirpath == "" {
flag.Usage()
os.Exit(1)
}
if _, err := os.Stat(*dirpath); err != nil {
fmt.Fprintf(os.Stderr, "stat %q: %s\n", *dirpath, err)
os.Exit(1)
}
if err := filepath.Walk(*dirpath, func(path string, info fs.FileInfo, err error) error {
if !info.Mode().IsRegular() {
return err
}
mails++
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
r := bufio.NewReader(f)
for {
ln, err := r.ReadString('\n')
if err != nil {
if err == io.EOF {
break
}
return err
}
if !strings.HasPrefix(strings.ToLower(ln), messageHeader) {
continue
}
var (
parts = strings.Split(ln[:len(ln)-1], " ")
msgid = strings.Trim(strings.Join(parts[1:], ""), "<>")
)
if msgid == "" {
return nil
}
if !mail[msgid] {
mail[msgid] = true
continue
}
dups++
fmt.Fprintf(os.Stderr, "%s\t%s\n", path, msgid)
if *noop {
continue
}
if err := os.Remove(path); err != nil {
return err
}
return nil
}
return nil
}); err != nil {
fmt.Fprintf(os.Stderr, "walk %q: %s\n", *dirpath, err)
os.Exit(1)
}
fmt.Fprintf(os.Stderr, "processed %d mail(s) in %.2fs. ", mails, time.Since(start).Seconds())
if dups > 0 {
fmt.Fprintf(os.Stderr, "%d duplicate(s) found.", dups)
}
fmt.Fprintln(os.Stderr)
}