2020-06-27 13:25:16 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/emersion/go-imap"
|
|
|
|
"github.com/emersion/go-imap/client"
|
|
|
|
"github.com/jhillyerd/enmime"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
"github.com/Luzifer/rconfig/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
cfg = struct {
|
|
|
|
Config string `flag:"config,c" default:"config.yaml" description:"Configuration file with instruction"`
|
|
|
|
FetchInterval time.Duration `flag:"interval,i" default:"1m" description:"Interval to fetch mails"`
|
|
|
|
IMAPHost string `flag:"imap-host,h" default:"" description:"Host of the IMAP server" validate:"nonzero"`
|
|
|
|
IMAPPort int `flag:"imap-port" default:"993" description:"Port of the IMAP server" validate:"nonzero"`
|
|
|
|
IMAPUser string `flag:"imap-user,u" default:"" description:"Username to access the IMAP server" validate:"nonzero"`
|
|
|
|
IMAPPass string `flag:"imap-pass,p" default:"" description:"Password to access the IMAP server" validate:"nonzero"`
|
|
|
|
LogLevel string `flag:"log-level" default:"info" description:"Log level (debug, info, warn, error, fatal)"`
|
|
|
|
Mailbox string `flag:"mailbox,m" default:"INBOX" description:"Mailbox to fetch from"`
|
2023-02-25 14:00:48 +00:00
|
|
|
StorageType string `flag:"storage-type" default:"file" description:"Driver to use for storing persistent info"`
|
|
|
|
StorageDSN string `flag:"storage-dsn" default:"store.yaml" description:"Where to store persistent info"`
|
2020-06-27 13:25:16 +00:00
|
|
|
VersionAndExit bool `flag:"version" default:"false" description:"Prints current version and exits"`
|
|
|
|
}{}
|
|
|
|
|
|
|
|
version = "dev"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rconfig.AutoEnv(true)
|
|
|
|
if err := rconfig.ParseAndValidate(&cfg); err != nil {
|
2023-02-25 14:00:48 +00:00
|
|
|
log.WithError(err).Fatalf("parsing commandline options")
|
2020-06-27 13:25:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.VersionAndExit {
|
|
|
|
fmt.Printf("automail %s\n", version)
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
if l, err := log.ParseLevel(cfg.LogLevel); err != nil {
|
2023-02-25 14:00:48 +00:00
|
|
|
log.WithError(err).Fatal("parsing log level")
|
2020-06-27 13:25:16 +00:00
|
|
|
} else {
|
|
|
|
log.SetLevel(l)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
bodySection, err := imap.ParseBodySectionName("BODY[]")
|
|
|
|
if err != nil {
|
2023-02-25 14:00:48 +00:00
|
|
|
log.WithError(err).Fatal("parsing body section")
|
2020-06-27 13:25:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
conf, err := loadConfig()
|
|
|
|
if err != nil {
|
2023-02-25 14:00:48 +00:00
|
|
|
log.WithError(err).Fatal("loading config")
|
2020-06-27 13:25:16 +00:00
|
|
|
}
|
|
|
|
|
2023-02-25 14:00:48 +00:00
|
|
|
store, err := newStorage(cfg.StorageType, cfg.StorageDSN)
|
2020-06-27 13:25:16 +00:00
|
|
|
if err != nil {
|
2023-02-25 14:00:48 +00:00
|
|
|
log.WithError(err).Fatal("creating storage interface")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = store.Load(); err != nil {
|
|
|
|
log.WithError(err).Fatal("loading persistent storage data")
|
2020-06-27 13:25:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2020-06-28 15:51:52 +00:00
|
|
|
imapClient *client.Client
|
|
|
|
messages = make(chan *imap.Message, 1000)
|
|
|
|
needLogin = make(chan struct{}, 1)
|
2020-06-28 15:53:37 +00:00
|
|
|
sigs = make(chan os.Signal, 1)
|
2020-06-28 15:51:52 +00:00
|
|
|
ticker = time.NewTicker(cfg.FetchInterval)
|
2020-06-27 13:25:16 +00:00
|
|
|
)
|
|
|
|
signal.Notify(sigs, os.Interrupt, syscall.SIGTERM)
|
2020-06-28 15:51:52 +00:00
|
|
|
needLogin <- struct{}{}
|
2020-06-27 13:25:16 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
|
2020-06-28 15:51:52 +00:00
|
|
|
case <-needLogin:
|
|
|
|
if imapClient != nil {
|
|
|
|
imapClient.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
imapClient, err = client.DialTLS(fmt.Sprintf("%s:%d", cfg.IMAPHost, cfg.IMAPPort), nil)
|
|
|
|
if err != nil {
|
2023-02-25 14:00:48 +00:00
|
|
|
log.WithError(err).Fatal("connecting to IMAP server")
|
2020-06-28 15:51:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err = imapClient.Login(cfg.IMAPUser, cfg.IMAPPass); err != nil {
|
2023-02-25 14:00:48 +00:00
|
|
|
log.WithError(err).Fatal("loggin in to IMAP server")
|
2020-06-28 15:51:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("IMAP connected and logged in")
|
|
|
|
|
|
|
|
if _, err = imapClient.Select(cfg.Mailbox, false); err != nil {
|
2023-02-25 14:00:48 +00:00
|
|
|
log.WithError(err).Fatal("selecting mailbox")
|
2020-06-28 15:51:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
// Trigger re-login when log-out was received
|
|
|
|
<-imapClient.LoggedOut()
|
|
|
|
needLogin <- struct{}{}
|
|
|
|
}()
|
|
|
|
|
2020-06-27 13:25:16 +00:00
|
|
|
case <-ticker.C:
|
2020-06-28 15:51:52 +00:00
|
|
|
if _, err := imapClient.Select(cfg.Mailbox, false); err != nil {
|
2023-02-25 14:00:48 +00:00
|
|
|
log.WithError(err).Error("selecting mailbox")
|
2020-06-28 15:51:52 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-02-25 14:00:48 +00:00
|
|
|
seq, err := imap.ParseSeqSet(fmt.Sprintf("%d:*", store.GetLastUID()+1))
|
2020-06-27 13:25:16 +00:00
|
|
|
if err != nil {
|
2023-02-25 14:00:48 +00:00
|
|
|
log.WithError(err).Error("parsing sequence set")
|
2020-06-27 13:25:16 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
ids, err := imapClient.UidSearch(&imap.SearchCriteria{
|
|
|
|
Uid: seq,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2023-02-25 14:00:48 +00:00
|
|
|
log.WithError(err).Error("searching for messages")
|
2020-06-27 13:25:16 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(ids) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-02-25 14:00:48 +00:00
|
|
|
tmpMsg := make(chan *imap.Message)
|
2020-06-27 13:25:16 +00:00
|
|
|
go func() {
|
|
|
|
for msg := range tmpMsg {
|
2023-02-25 14:00:48 +00:00
|
|
|
if msg.Uid <= store.GetLastUID() {
|
2020-06-27 13:25:16 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
messages <- msg
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
fetchSeq := &imap.SeqSet{}
|
|
|
|
fetchSeq.AddNum(ids...)
|
|
|
|
|
|
|
|
if err = imapClient.UidFetch(fetchSeq, []imap.FetchItem{
|
|
|
|
imap.FetchFlags,
|
|
|
|
imap.FetchItem("BODY.PEEK[]"),
|
|
|
|
imap.FetchUid,
|
|
|
|
}, tmpMsg); err != nil {
|
2023-02-25 14:00:48 +00:00
|
|
|
log.WithError(err).Error("fetching messages")
|
2020-06-27 13:25:16 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
case <-sigs:
|
|
|
|
return
|
|
|
|
|
|
|
|
case msg := <-messages:
|
|
|
|
body := msg.GetBody(bodySection)
|
|
|
|
if body == nil {
|
|
|
|
log.WithField("uid", msg.Uid).Debug("Got message with nil body")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
mail, err := enmime.ReadEnvelope(body)
|
|
|
|
if err != nil {
|
2023-02-25 14:00:48 +00:00
|
|
|
log.WithError(err).Error("parsing message")
|
2020-06-27 13:25:16 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"subject": mail.GetHeader("subject"),
|
|
|
|
"uid": msg.Uid,
|
|
|
|
}).Debug("Fetched message")
|
|
|
|
|
|
|
|
// Check all handlers whether they want to handle the message
|
|
|
|
for _, hdl := range conf.Handlers {
|
|
|
|
if hdl.Handles(mail) {
|
2023-02-25 14:00:48 +00:00
|
|
|
go func(msg *imap.Message, hdl mailHandler) {
|
2020-06-27 13:25:16 +00:00
|
|
|
if err := hdl.Process(imapClient, msg, mail); err != nil {
|
2023-02-25 14:00:48 +00:00
|
|
|
log.WithError(err).Error("processing message")
|
2020-06-27 13:25:16 +00:00
|
|
|
}
|
2023-02-25 14:00:48 +00:00
|
|
|
}(msg, hdl)
|
2020-06-27 13:25:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark message as processed in store
|
2023-02-25 14:00:48 +00:00
|
|
|
if msg.Uid > store.GetLastUID() {
|
|
|
|
store.SetUID(msg.Uid)
|
|
|
|
if err = store.Save(); err != nil {
|
|
|
|
log.WithError(err).Error("saving storage")
|
2020-06-27 13:25:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|