Author: Adam Evyčędo <git@apiote.xyz>
send quaratine message only for messages to main address
config_example.dirty | 2 ++ main.go | 2 ++ tyr.go | 25 ++++++++++++++++++++-----
diff --git a/config_example.dirty b/config_example.dirty index 570f2484cd838af55d78008218c7d814ec8163f9..4826198da1d15cebac9b2b994fd41e20770755bd 100644 --- a/config_example.dirty +++ b/config_example.dirty @@ -11,6 +11,8 @@ ('imapFolderDrafts' 'Drafts') ('imapFolderQuarantine' 'Quarantine') ('imapFolderSent' 'Sent') ('imapFolderTrash' 'Trash') + ('recipientDomain' '') + ('mainEmailAddress' '') ) ) ('hermodr' diff --git a/main.go b/main.go index a76995e2dbf5b2811b1f4e49919e3d3b1b96d0ca..643c0fee8916484786061cf4e8e9b51274f30150 100644 --- a/main.go +++ b/main.go @@ -26,6 +26,8 @@ ImapFolderTrash string ImapFolderDrafts string ImapFolderQuarantine string ImapFolderSent string + RecipientDomain string + MainEmailAddress string } type HermodrConfig struct { diff --git a/tyr.go b/tyr.go index b046607a818514d8c44007c732170f5a3716b66b..2b9671f07c380725130e5006edccfae8aff4b5dd 100644 --- a/tyr.go +++ b/tyr.go @@ -51,7 +51,7 @@ } /* ASGARD */ -func addSentTo(db *sql.DB, c *client.Client, mbox *imap.MailboxStatus) error { +func addSentTo(db *sql.DB, c *client.Client, mbox *imap.MailboxStatus, config Config) error { // todo also release from Quarantine from := uint32(1) to := mbox.Messages @@ -129,6 +129,8 @@ continue messagesLoop } } recipients := append(msg.Envelope.To, msg.Envelope.Cc...) + recipients = append(recipients, msg.Envelope.Bcc...) + domainRecipient := findDomainRecipient(recipients, config) recipients_ := map[string]struct{}{} for _, recipient := range recipients { recipients_[recipient.Address()] = struct{}{} @@ -162,7 +164,9 @@ if !lock.empty() { now := time.Now() weekBefore := now.AddDate(0, 0, -7) if lock.date.Before(weekBefore) { - sendRepeatedQuarantine(sender, lock) + if domainRecipient == config.Tyr.MainEmailAddress { + sendRepeatedQuarantine(sender, lock) + } lock.date = time.Now() updateLock(db, lock) } else { @@ -173,10 +177,12 @@ moveSet.AddNum(msg.Uid) continue } - sendQuarantine(sender) + if domainRecipient == config.Tyr.MainEmailAddress { + sendQuarantine(sender) + } lock = NewLock(sender.Address()) insertLock(db, lock) - log.Printf("moving %v : %s from %s to quarantine\n", msg.Envelope.Date, msg.Envelope.Subject, msg.Envelope.From[0].Address()) + log.Printf("moving %v : %s from %s to %s to quarantine\n", msg.Envelope.Date, msg.Envelope.Subject, msg.Envelope.From[0].Address(), domainRecipient) moveSet.AddNum(msg.Uid) } @@ -184,6 +190,15 @@ if err := <-done; err != nil { return err } return moveMultiple(c, moveSet, config.Tyr.ImapFolderQuarantine) +} + +func findDomainRecipient(recipients []*imap.Address, config Config) string { + for _, recipient := range recipients { + if recipient.HostName == config.Tyr.RecipientDomain { + return recipient.Address() + } + } + return config.Tyr.MainEmailAddress } func tyr(db *sql.DB, config Config) { @@ -202,7 +217,7 @@ mbox, err := c.Select(config.Tyr.ImapFolderSent, false) if err != nil { log.Fatalln(err) } - err = addSentTo(db, c, mbox) + err = addSentTo(db, c, mbox, config) if err != nil { log.Fatalln(err) }