small pixel drawing of a pufferfish clist

Refactor 2: electric boogaloo
j3s j3s@c3f.net
Sun, 24 Nov 2019 23:41:58 -0600
commit

e7cd5ca32d1a700304ec3bbd648c16bd077fe993

parent

c3f84bb7c570344e9d7495aa67ea397e30f8cf24

2 files changed, 55 insertions(+), 27 deletions(-)

jump to
M mail/main.gomail/main.go

@@ -426,7 +426,7 @@ ContentType string

Data io.Reader } -func sepAddrs(mails []*mail.Address) string { +func commaSep(mails []*mail.Address) string { var response []string var sm string for _, m := range mails {

@@ -438,22 +438,46 @@ }

func (e *Email) ToBytes() []byte { var buf bytes.Buffer - fmt.Fprintf(&buf, "From: %s\r\n", sepAddrs(e.From)) - fmt.Fprintf(&buf, "To: %s\r\n", sepAddrs(e.To)) - fmt.Fprintf(&buf, "Cc: %s\r\n", sepAddrs(e.Cc)) - fmt.Fprintf(&buf, "Reply-To: %s\r\n", sepAddrs(e.ReplyTo)) + fmt.Fprintf(&buf, "Sender: %s\r\n", e.Sender) + fmt.Fprintf(&buf, "From: %s\r\n", commaSep(e.From)) + if len(e.To) > 0 { + fmt.Fprintf(&buf, "To: %s\r\n", commaSep(e.To)) + } + if len(e.Cc) > 0 { + fmt.Fprintf(&buf, "Cc: %s\r\n", commaSep(e.Cc)) + } if !e.Date.IsZero() { fmt.Fprintf(&buf, "Date: %s\r\n", e.Date) } if len(e.MessageID) > 0 { - fmt.Fprintf(&buf, "Messsage-ID: %s\r\n", e.MessageID) + fmt.Fprintf(&buf, "Message-ID: %s\r\n", e.MessageID) } if len(e.InReplyTo) > 0 { fmt.Fprintf(&buf, "In-Reply-To: %s\r\n", strings.Join(e.InReplyTo, ",")) } - // go over remainder of headers - for k, v := range e.Header { - fmt.Fprintf(&buf, "%s: %s\r\n", k, strings.Join(v, ",")) + if len(e.Header.Get("List-Id")) > 0 { + fmt.Fprintf(&buf, "List-Id: %s\r\n", e.Header.Get("List-Id")) + } + if len(e.Header.Get("List-Post")) > 0 { + fmt.Fprintf(&buf, "List-Post: %s\r\n", e.Header.Get("List-Post")) + } + if len(e.Header.Get("List-Help")) > 0 { + fmt.Fprintf(&buf, "List-Help: %s\r\n", e.Header.Get("List-Help")) + } + if len(e.Header.Get("List-Subscribe")) > 0 { + fmt.Fprintf(&buf, "List-Subscribe: %s\r\n", e.Header.Get("List-Subscribe")) + } + if len(e.Header.Get("List-Unsubscribe")) > 0 { + fmt.Fprintf(&buf, "List-Unsubscribe: %s\r\n", e.Header.Get("List-Unsubscribe")) + } + if len(e.Header.Get("List-Owner")) > 0 { + fmt.Fprintf(&buf, "List-Owner: %s\r\n", e.Header.Get("List-Owner")) + } + if len(e.Header.Get("Precedence")) > 0 { + fmt.Fprintf(&buf, "Precedence: %s\r\n", e.Header.Get("Precedence")) + } + if len(e.Header.Get("List-Archive")) > 0 { + fmt.Fprintf(&buf, "List-Archive: %s\r\n", e.Header.Get("List-Archive")) } fmt.Fprintf(&buf, "Subject: %s\r\n", e.Subject) fmt.Fprintf(&buf, "\r\n%s", e.TextBody)
M main.gomain.go

@@ -102,7 +102,7 @@

log.Printf("matchedLists: %q", matchedLists) if len(matchedLists) == 1 { list := matchedLists[0] - if list.CanPost(msg.Sender) { + if list.CanPost(msg.From[0]) { msg := buildListEmail(msg, list) send(msg) log.Printf("MESSAGE_SENT ListId=%q",

@@ -236,11 +236,11 @@ os.Exit(0)

} var body bytes.Buffer - if isSubscribed(msg.Sender, listId) { + if isSubscribed(msg.From[0], listId) { fmt.Fprintf(&body, "You are already subscribed to %s\r\n", listId) log.Printf("DUPLICATE_SUBSCRIPTION_REQUEST User=%q List=%q\n", msg.From, listId) } else { - addSubscription(msg.Sender, listId) + addSubscription(msg.From[0], listId) fmt.Fprintf(&body, "You are now subscribed to %s\r\n", listId) fmt.Fprintf(&body, "To send a message to this list, send an email to %s\r\n", list.Address) }

@@ -260,11 +260,11 @@ os.Exit(0)

} var body bytes.Buffer - if !isSubscribed(msg.Sender, listId) { + if !isSubscribed(msg.From[0], listId) { fmt.Fprintf(&body, "You aren't subscribed to %s\r\n", listId) log.Printf("DUPLICATE_UNSUBSCRIPTION_REQUEST User=%q List=%q\n", msg.From, listId) } else { - removeSubscription(msg.Sender, listId) + removeSubscription(msg.From[0], listId) fmt.Fprintf(&body, "You are now unsubscribed from %s\r\n", listId) } reply := buildCommandEmail(msg, body)

@@ -299,15 +299,19 @@

func buildCommandEmail(e *parsemail.Email, t bytes.Buffer) *parsemail.Email { response := parsemail.Email{} response.Sender = &mail.Address{"", gConfig.CommandAddress} - var from []*mail.Address - response.From = append(from, &mail.Address{"", gConfig.CommandAddress}) + from := &mail.Address{"clist", gConfig.CommandAddress} + response.From = []*mail.Address{from} response.To = e.From response.Bcc = e.From response.Subject = "Re: " + e.Subject + header := make(map[string][]string) + header["Date"] = []string{time.Now().Format("Mon, 2 Jan 2006 15:04:05 -0700")} + header["Precedence"] = []string{"list"} + header["List-Help"] = []string{"<mailto:" + gConfig.CommandAddress + "?subject=help>"} + response.Header = header + response.Bcc = e.From response.TextBody = t.String() - response.Header["Date"] = []string{time.Now().Format("Mon, 2 Jan 2006 15:04:05 -0700")} - response.Header["Precedence"] = []string{"list"} - response.Header["List-Help"] = []string{"<mailto:" + gConfig.CommandAddress + "?subject=help>"} + log.Printf("%q", response) return &response }

@@ -431,11 +435,11 @@ return listIds

} // Check if a user is subscribed to a mailing list -func isSubscribed(sender *mail.Address, list string) bool { +func isSubscribed(address *mail.Address, list string) bool { db := requireDB() exists := false - err := db.QueryRow("SELECT 1 FROM subscriptions WHERE user=? AND list=?", sender.Address, list).Scan(&exists) + err := db.QueryRow("SELECT 1 FROM subscriptions WHERE user=? AND list=?", address.Address, list).Scan(&exists) if err == sql.ErrNoRows { return false

@@ -448,25 +452,25 @@ return true

} // Add a subscription to the subscription database -func addSubscription(sender *mail.Address, list string) { +func addSubscription(address *mail.Address, list string) { db := requireDB() - _, err := db.Exec("INSERT INTO subscriptions (user,list) VALUES(?,?)", sender.Address, list) + _, err := db.Exec("INSERT INTO subscriptions (user,list) VALUES(?,?)", address.Address, list) if err != nil { log.Printf("DATABASE_ERROR Error=%q\n", err.Error()) os.Exit(0) } - log.Printf("SUBSCRIPTION_ADDED Sender=%q List=%q\n", sender, list) + log.Printf("SUBSCRIPTION_ADDED Sender=%q List=%q\n", address, list) } // Remove a subscription from the subscription database -func removeSubscription(sender *mail.Address, list string) { +func removeSubscription(address *mail.Address, list string) { db := requireDB() - _, err := db.Exec("DELETE FROM subscriptions WHERE user=? AND list=?", sender.Address, list) + _, err := db.Exec("DELETE FROM subscriptions WHERE user=? AND list=?", address.Address, list) if err != nil { log.Printf("DATABASE_ERROR Error=%q\n", err.Error()) os.Exit(0) } - log.Printf("SUBSCRIPTION_REMOVED Sender=%q List=%q\n", sender, list) + log.Printf("SUBSCRIPTION_REMOVED Sender=%q List=%q\n", address, list) } // HELPER FUNCTIONS ///////////////////////////////////////////////////////////