-
Notifications
You must be signed in to change notification settings - Fork 0
/
maildir2addr.go
229 lines (178 loc) · 4.11 KB
/
maildir2addr.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
package main
import (
"flag"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
"github.com/pkg/errors"
)
func main() {
var E error
defer func() {
if E == nil {
return
}
Flog(
os.Stderr,
"1;91m",
"ERROR",
E.Error(),
)
os.Exit(1)
}()
const SZ_HELP_PREFIX = `
maildir2addr
------------
Scans maildir folders for e-mail addresses, outputs results in aerc-style
address-book-cmd format:
[E-MAIL1]\t[NAME1]\n
[E-MAIL2]\t[NAME2]\n
...
The [E-MAIL] column is forced to lowercase in the output.
Defaults store data in the $HOME/.local/share/maildir2addr directory.
A file of exclude regexes can be specified with the -e option. One regexp per
line, each applied to the [E-MAIL] part only. If this file is specified but
does not exist, it will be created & populated with sane defaults.
USAGE
maildir2addr [OPTION...] [MAILDIR_PATH...]
OPTIONS
`
// HELP MESSAGE
flag.Usage = func() {
fmt.Fprint(os.Stdout, SZ_HELP_PREFIX)
flag.PrintDefaults()
fmt.Fprint(os.Stdout, "\n")
}
var sO Opts
flag.BoolVar(&sO.IncludeSpamMsgs, "s", false, "process spam messages (where X-Spam-Flag == YES)")
flag.BoolVar(&sO.Verbose, "v", false, "verbose, log details to STDERR")
var dbInFile, dbOutFile, szExcludesFile string
defaultDir := os.ExpandEnv("$HOME/.local/share/" + filepath.Base(os.Args[0]))
flag.StringVar(&dbInFile, "i", defaultDir+"/addrs.tsv", "address database input file\n")
flag.StringVar(&dbOutFile, "o", defaultDir+"/addrs.tsv", "address database output file\n")
flag.StringVar(&szExcludesFile, "e", defaultDir+"/excludes.regexp", "address exclusion regex file [one per line]\n")
flag.Parse()
// LOAD ADDRS
if len(dbInFile) > 0 {
sO.LogVerbose(
"1;93m",
"READING ADDRS",
dbInFile,
)
if E2 := sO.AddrsReadFromFile(dbInFile); E2 != nil {
E = errors.Wrap(E2, "read addrs "+dbInFile)
return
}
}
// EXCLUDE RULES INIT
if len(szExcludesFile) > 0 {
// CREATE DEFAULT EXCLUDES IF FILE SPECIFIED, BUT NOT FOUND
if !FileExists(szExcludesFile) {
sO.LogVerbose(
"1;93m",
"INITIALIZING EXCLUDE RULES",
szExcludesFile,
)
sRgx := []string{
`^(customer|message|orders|webdesign|receipts|sales|service|support|submissions)`,
`subscribe`,
`daemon`,
`[[:xdigit:]]{7,}`,
`not?[-_.]?reply`,
`.{50,}`,
}
exclDir := filepath.Dir(szExcludesFile)
if E = os.MkdirAll(exclDir, 0770); E != nil {
return
}
if E = os.WriteFile(szExcludesFile, []byte(strings.Join(sRgx, "\n")), 0660); E != nil {
return
}
}
// LOAD EXCLUDE RULES
sO.LogVerbose(
"1;93m",
"READING EXCLUDE RULES",
szExcludesFile,
)
if E = sO.ExcludesReadFromFile(szExcludesFile); E != nil {
return
}
}
// WALKDIR FUNC: SCANS ADDRS FROM SELECTED FILES INTO DB
nScannedMsgs := 0
fnWalk := func(path string, de fs.DirEntry, err error) error {
if err != nil {
return err
}
// SKIP DIRS
if de.IsDir() {
return nil
}
// SKIP DOTFILES
bname := filepath.Base(path)
if strings.HasPrefix(bname, ".") {
return nil
}
// ABSOLUTE PATH
absPath, eMsg := filepath.Abs(path)
if eMsg != nil {
Flog(
os.Stderr,
"1;91m",
"MSGERR",
eMsg.Error(),
path,
)
}
// SCAN ADDRS
nScannedMsgs += 1
if eMsg := sO.ScanMsgsForAddrs(absPath); eMsg != nil {
Flog(
os.Stderr,
"1;91m",
"MSGERR",
eMsg.Error(),
absPath,
)
}
return nil
}
// PROCESS FILES IN SPECIFIED MAILDIR(S)
sArgs := flag.Args()
if len(sArgs) > 0 {
for _, mailDir := range sArgs {
if E = filepath.WalkDir(mailDir, fnWalk); E != nil {
return
}
}
}
// NOTE: running regardless of scan to purge
// existing addresses with new exclusions
sO.AddrsPurgeExcluded()
if len(sArgs) > 0 {
Flog(
os.Stderr,
"1;92m",
"SCAN COMPLETE",
fmt.Sprintf(
"SCANNED %d ADDRS IN %d MSGS; %d NEW UNIQUE ADDRS FOUND",
sO.nScannedAddrs,
nScannedMsgs,
sO.nNewAddrs,
),
)
}
// WRITE DB
sO.LogVerbose(
"1;93m",
"WRITING ADDRS",
dbOutFile,
)
if E2 := sO.AddrsWriteToFile(dbOutFile); E2 != nil {
E = errors.Wrap(E2, "write addrs "+dbOutFile)
return
}
}