-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkmail.go
230 lines (185 loc) · 4.94 KB
/
checkmail.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
package main
import (
"fmt"
"os"
"github.com/fatih/color"
"github.com/spf13/pflag"
)
type Config struct {
Server string
User string
Password string
Mailbox string
NoTls bool `ini:"no_tls"`
}
type Opts struct {
NoColors bool
Verbose bool
Debug bool
File string
Server string
User string
Mailbox string
NoTls bool
}
const (
Version = "0.2"
DefaultConfigFile = "~/.config/checkmail/checkmail.ini"
)
var (
opts Opts
dateColor = color.New(color.FgYellow).SprintFunc()
addrColor = color.New(color.FgGreen).SprintFunc()
totalColor = color.New(color.FgGreen).SprintFunc()
unseenColor = color.New(color.FgRed).SprintFunc()
seenColor = color.New(color.FgHiBlack).SprintFunc()
)
//--------------------------------------------------------------------------------
// main()
//--------------------------------------------------------------------------------
func main() {
cmd, arg := parseCmdline()
var cfg *Config
if opts.File == "" {
cfg = loadConfigFile(DefaultConfigFile, false)
} else {
cfg = loadConfigFile(opts.File, true)
}
prepareConfig(cfg, &opts)
switch cmd {
case "", "count":
countCmd(cfg, arg)
case "list":
listCmd(cfg, arg)
case "touch":
touchCmd(cfg, arg)
default:
syntax("unknown command: %q", cmd)
}
}
func prepareConfig(cfg *Config, opts *Opts) {
// cmdline-options overwrite config-file
if opts.Server != "" {
cfg.Server = opts.Server
}
if opts.User != "" {
cfg.User = opts.User
}
if opts.Mailbox != "" {
cfg.Mailbox = opts.Mailbox
}
if opts.NoTls == true {
cfg.NoTls = true
}
// check for required config-values
if cfg.Server == "" {
syntax("No server given (in config-file or with --server)")
}
if cfg.User == "" {
syntax("No user given (in config-file or with --user)")
}
if cfg.Password == "" {
syntax("No password given (in config-file)")
}
// default-values (don't set option-default!!!)
if cfg.Mailbox == "" {
cfg.Mailbox = "INBOX"
}
}
//--------------------------------------------------------------------------------
// Parse commandline
//--------------------------------------------------------------------------------
func parseCmdline() (cmd, arg string) {
pflag.CommandLine.Init("", pflag.ContinueOnError)
pflag.StringVarP(&opts.Server, "server", "s", "", "")
pflag.StringVarP(&opts.User, "user", "u", "", "")
pflag.BoolVarP(&opts.Verbose, "verbose", "v", false, "")
pflag.BoolVarP(&opts.Debug, "debug", "d", false, "")
pflag.BoolVarP(&opts.NoTls, "no-tls", "t", false, "")
pflag.StringVarP(&opts.Mailbox, "mailbox", "m", "", "")
pflag.StringVarP(&opts.File, "file", "f", "", "")
noColors := pflag.BoolP("no-colors", "n", false, "")
help := pflag.BoolP("help", "?", false, "")
version := pflag.BoolP("version", "V", false, "")
if err := pflag.CommandLine.Parse(os.Args[1:]); err != nil {
syntax(err.Error())
}
if *version {
info("checkmail v" + Version)
os.Exit(0)
}
if *help {
printHelp()
}
if *noColors {
color.NoColor = true
}
args := pflag.Args()
switch len(args) {
case 0:
case 1:
cmd = args[0]
case 2:
cmd = args[0]
arg = args[1]
default:
syntax("too many arguments")
}
return cmd, arg
}
func printHelp() {
fmt.Println(`Usage: checkmail [OPTIONS] [COMMAND]
Check an IMAP-server for un-/seen messages and count or list them.
To mark all messages as seen use the command 'touch'.
The command 'list boxes' shows all available mailboxes.
Options:
-s, --server=HOST:PORT select the IMAP-server and port
-t, --no-tls use un-encryptet connection
-u, --user=USER login as USER
-m, --mailbox=BOX select mailbox (default is INBOX)
-v, --verbose show verbose output
-d, --debug show debugging information
-n, --no-colors disable ANSI-Colors
-f, --file=CFGFILE load another config-file
(default is
~/.config/checkmail/checkmail.ini)
Commands:
list [boxes|seen|unseen|all] list messages or mailboxes
count [seen|unseen|all] count messages in Mailbox
touch mark all messages as seen
`)
os.Exit(0)
}
//--------------------------------------------------------------------------------
// Output
//--------------------------------------------------------------------------------
func debug(format string, args ...any) {
if opts.Debug {
fmt.Printf(format, args...)
fmt.Println("")
}
}
func verbose(format string, args ...any) {
if opts.Verbose {
fmt.Printf(format, args...)
fmt.Println("")
}
}
func info(format string, args ...any) {
fmt.Printf(format, args...)
fmt.Println("")
}
func warning(format string, args ...any) {
fmt.Fprint(os.Stderr, "checkmail: ")
fmt.Fprintf(os.Stderr, format, args...)
fmt.Fprintln(os.Stderr, "\n")
}
func failure(format string, args ...any) {
warning(format, args...)
os.Exit(1)
}
func syntax(format string, args ...any) {
warning(format, args...)
fmt.Fprintln(os.Stderr, "Try 'checkmail --help' for more information.\n")
os.Exit(1)
}