Author: Adam <git@apiote.xyz>
check recipient by template in mímir
config_example.dirty | 5 ++- db.go | 4 ++ main.go | 11 +++++---- mímir.go | 49 ++++++++++++++++++++++++++++++++-------------
diff --git a/config_example.dirty b/config_example.dirty index ccc7372d04dfeb22bdc5d2ff40ed138c8a8c178b..67061ae3dc80044e505f90d773624f1c6722dc58 100644 --- a/config_example.dirty +++ b/config_example.dirty @@ -33,8 +33,9 @@ ( ('imapAddress' '') ('imapUsername' '') ('imapPassword' '') - ('imapInboxes' ()) - ('imapFolderSent' 'Sent') + ('imapInbox' 'Inbox') + ('recipientTemplate' '[:]@exmaple.com') + ('categories' ('cars', 'music')) ) ) ) diff --git a/db.go b/db.go index f17d7f2af7137e50e2872c1b2d0f2b1cbee96b55..56f3ecb37b62925c7d85bac9107e7cae51aaa3b7 100644 --- a/db.go +++ b/db.go @@ -123,7 +123,9 @@ address.addressFrom, address.addressTo, address.ban) return err } -func addArchiveEntry(messageID, subject string, body []byte, date time.Time, inReplyTo string, dkim bool, sender *imap.Address, recipients []*imap.Address) { +func addArchiveEntry(messageID, category, subject string, body []byte, date time.Time, inReplyTo string, dkim bool, sender *imap.Address, recipients []*imap.Address) { + // todo column rootID with rootID of parent or this.messageID + fmt.Printf("adding Archive Entry for %s “%s”, %v, in reply to %s with dkim %v from %s\n", messageID, subject, date, inReplyTo, dkim, sender) fmt.Printf("body: %s\n", body) // todo diff --git a/main.go b/main.go index b51d017da987908a16c857644032aec30d3f9e6f..58b6335bd39d3655bce3b408ae09efba32d0144a 100644 --- a/main.go +++ b/main.go @@ -33,11 +33,12 @@ SmtpUsername string PublicKey string } type MímirConfig struct { - ImapAddress string - ImapUsername string - ImapPassword string - ImapInboxes []string - ImapFolderSent string + ImapAddress string + ImapUsername string + ImapPassword string + ImapInbox string + RecipientTemplate string + Categories []string } type Config struct { diff --git a/mímir.go b/mímir.go index f88b2f696f4038e4d12fec8db3c365b70bebce12..b5c8958c5f2bec249069d3046832f76487f82d58 100644 --- a/mímir.go +++ b/mímir.go @@ -2,11 +2,14 @@ package main import ( "bytes" + "errors" "fmt" "io" "log" "net/http" "net/mail" + "regexp" + "strings" "database/sql" "github.com/emersion/go-imap" @@ -14,7 +17,31 @@ "github.com/emersion/go-imap/client" "github.com/emersion/go-msgauth/dkim" ) -func archiveInbox(mboxName string, c *client.Client) error { +func checkRecipientTemplate(recipients []*imap.Address, config Config) (string, error) { + categories := config.Mímir.Categories + if len(categories) == 0 { + return "", nil + } + parts := strings.SplitN(config.Mímir.RecipientTemplate, "[:]", 2) + r, err := regexp.Compile(parts[0] + "(.*)" + parts[1]) + if err != nil { + return "", err + } + for _, recipient := range recipients { + matches := r.FindStringSubmatch(recipient.Address()) + if len(matches) != 2 { + return "", errors.New("No category in recipient") + } + for _, category := range categories { + if matches[1] == category { + return category, nil + } + } + } + return "", errors.New("Unknown category") +} + +func archiveInbox(mboxName string, c *client.Client, config Config) error { mbox, err := c.Select(mboxName, true) if err != nil { return err @@ -37,9 +64,11 @@ go func() { done <- c.Fetch(seqset, items, messages) }() for msg := range messages { - // if any recipient ~= git\+[^@]+@apiote.xyz and no recipient ~= git@apiote.xyz - // and if sender ~= git\+[^@]+@apiote.xyz recipients := append(msg.Envelope.To, msg.Envelope.Cc...) + project, err := checkRecipientTemplate(recipients, config) + if err != nil { + return err + } sender := msg.Envelope.From[0] messageID := msg.Envelope.MessageId subject := msg.Envelope.Subject @@ -48,9 +77,7 @@ inReplyTo := msg.Envelope.InReplyTo dkimStatus := false r := msg.GetBody(section) if r == nil { - fmt.Println("here") - // todo return custom error - return err + return errors.New("No body in message") } messageBytes, err := io.ReadAll(r) if err != nil { @@ -81,7 +108,7 @@ return err } // select part text/plain from body - addArchiveEntry(messageID, subject, body, date, inReplyTo, dkimStatus, sender, recipients) + addArchiveEntry(messageID, project, subject, body, date, inReplyTo, dkimStatus, sender, recipients) } if err := <-done; err != nil { @@ -101,13 +128,7 @@ if err := c.Login(config.Mímir.ImapUsername, config.Mímir.ImapPassword); err != nil { log.Fatalln(err) } log.Println("Logged in") - for _, inbox := range config.Mímir.ImapInboxes { - err = archiveInbox(inbox, c) - if err != nil { - log.Fatalln(err) - } - } - err = archiveInbox(config.Mímir.ImapFolderSent, c) // todo archive only mail that is reply-to of what is already in db + err = archiveInbox(config.Mímir.ImapInbox, c, config) if err != nil { log.Fatalln(err) }