-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
220 lines (196 loc) · 6.45 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
package main
// Execute scripts on events using IDLE imap command (Go version)
// Copyright (C) 2017-2024 Jorge Javier Araya Navarro
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
import (
"flag"
"fmt"
"github.com/emersion/go-imap"
"os"
"os/signal"
"path/filepath"
"slices"
"sync"
"syscall"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
var (
commit string
gittag string
branch string
)
func getDefaultConfigPath() string {
home := os.Getenv("XDG_CONFIG_HOME")
if home == "" {
return filepath.Join(os.Getenv("HOME"), ".config", "goimapnotify")
}
return filepath.Join(home, "goimapnotify")
}
func usage() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s:\n", os.Args[0])
flag.PrintDefaults()
msg := donateMessage(8)
fmt.Fprint(flag.CommandLine.Output(), "\n"+msg)
}
func main() {
// imap.DefaultLogMask = imap.LogConn | imap.LogRaw
fileconf := flag.String(
"conf",
filepath.Join(getDefaultConfigPath(), fmt.Sprintf("goimapnotify.%s", viper.SupportedExts[2])),
"Configuration file",
)
list := flag.Bool("list", false, "List all mailboxes and exit")
debug := flag.Bool("debug", false, "Output all network activity to the terminal")
wait := flag.Int("wait", 1, "Period in seconds between IDLE event and execution of scripts")
flag.Usage = usage
flag.Parse()
logrus.SetLevel(logrus.InfoLevel)
if *debug {
logrus.SetLevel(logrus.DebugLevel)
}
logrus.Infof("ℹ Running commit %s, tag %s, branch %s", commit, gittag, branch)
viper.SetConfigFile(*fileconf)
if err := viper.ReadInConfig(); err != nil {
logrus.Fatalf("Can't read file: '%s', error: %v", *fileconf, err)
}
topConfig, err := loadConfiguration(*fileconf)
if err != nil {
logrus.Fatalf("can't load the configuration: %v", err)
}
logrus.Debugf("configuration loaded successfuly: %s", *fileconf)
idleChan := make(chan IDLEEvent)
boxChan := make(chan BoxEvent, 1)
quit := make(chan os.Signal, 1)
doneChan := make(chan struct{})
running := NewRunningBox(*debug, *wait)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
wg := &sync.WaitGroup{}
// indexes used because we need to change struct data
for i := range topConfig.Configurations {
topConfig.Configurations[i].Debug = *debug
topConfig.Configurations[i] = retrieveCmd(topConfig.Configurations[i])
if topConfig.Configurations[i].Alias == "" {
if *debug {
topConfig.Configurations[i].Alias = censorEmailAddress(
topConfig.Configurations[i].Username,
)
} else {
topConfig.Configurations[i].Alias = topConfig.Configurations[i].Username
}
}
if *list {
client, cErr := newClient(topConfig.Configurations[i])
if cErr != nil {
logrus.Fatalf("[%s] Something went wrong creating IMAP client: %s",
topConfig.Configurations[i].Alias, cErr)
}
// nolint
defer client.Logout()
max, err := printDelimiter(client)
if err != nil {
logrus.WithError(err).Warning("listing mailboxes finished with error")
}
_ = walkMailbox(client, "", 0, max)
} else {
if len(topConfig.Configurations[i].Boxes) == 0 {
client, iErr := newIMAPIDLEClient(topConfig.Configurations[i])
if iErr != nil {
logrus.Fatalf(iErr.Error())
}
mailboxes := make(chan *imap.MailboxInfo, 10)
done := make(chan error, 1)
go func() {
done <- client.List("", "*", mailboxes)
}()
var mboxes []string
for m := range mailboxes {
if slices.Contains([]string{"[Gmail]", "[Gmail]/All Mail"}, m.Name) {
continue
}
mboxes = append(mboxes, m.Name)
}
for _, m := range mboxes {
client, iErr := newIMAPIDLEClient(topConfig.Configurations[i])
if iErr != nil {
logrus.Fatalf(iErr.Error())
}
nConf := topConfig.Configurations[i]
box := Box{
Alias: nConf.Alias,
Mailbox: m,
OnNewMail: nConf.OnNewMail,
OnNewMailPost: nConf.OnNewMailPost,
OnDeletedMail: nConf.OnDeletedMail,
OnDeletedMailPost: nConf.OnDeletedMailPost,
}
key := box.Alias + box.Mailbox
running.mutex[key] = new(sync.RWMutex)
NewWatchBox(client, NotifyConfig{}, box, idleChan, boxChan, doneChan, wg)
}
} else {
//launch watchers for all mailboxes
//listen in "boxes"
for j := range topConfig.Configurations[i].Boxes {
/*
* Copy default names if empty. Use SKIP to skip execution
* The check is happening in running.go:run
*/
topConfig.Configurations[i].Boxes[j] = setFromConfig(topConfig.Configurations[i], topConfig.Configurations[i].Boxes[j])
client, iErr := newIMAPIDLEClient(topConfig.Configurations[i])
if iErr != nil {
logrus.Fatalf("[%s:%s] Something went wrong creating IDLE client: %s",
topConfig.Configurations[i].Boxes[j].Alias, topConfig.Configurations[i].Boxes[j].Mailbox, iErr)
}
box := topConfig.Configurations[i].Boxes[j]
key := box.Alias + box.Mailbox
running.mutex[key] = new(sync.RWMutex)
NewWatchBox(client, topConfig.Configurations[i], topConfig.Configurations[i].Boxes[j],
idleChan, boxChan, doneChan, wg)
}
}
}
}
run := true
if *list {
run = false
}
for run {
select {
case boxEvent := <-boxChan:
logrus.Infof("[%s:%s] Restarting watcher for mailbox",
boxEvent.Mailbox.Alias, boxEvent.Mailbox.Mailbox)
client, fErr := newIMAPIDLEClient(boxEvent.Conf)
if fErr != nil {
logrus.Fatalf("[%s:%s] Something went wrong creating IDLE client: %s",
boxEvent.Mailbox.Alias, boxEvent.Mailbox.Mailbox, fErr)
}
NewWatchBox(client, boxEvent.Conf, boxEvent.Mailbox,
idleChan, boxChan, doneChan, wg)
case <-quit:
// OS asked nicely to close, we ask our
// goroutines to do the same
close(doneChan)
run = false
case idleEvent := <-idleChan:
wg.Add(1)
go func() {
defer wg.Done()
running.schedule(idleEvent, doneChan)
}()
}
}
logrus.Info("Waiting other goroutines to stop...")
wg.Wait()
printDonate(os.Stderr, 11)
logrus.Info("Bye")
}