all repos — navani @ 237e72d1efd0e5c3c940f03595c6b891f6ae2455

forlater's primary mail processing service

Add check for unreadable link

We just email it back to them.
Anirudh Oppiliappan x@icyphox.sh
Wed, 15 Sep 2021 18:12:16 +0530
commit

237e72d1efd0e5c3c940f03595c6b891f6ae2455

parent

c0119bb48c0e5fba30d1a838d623d8aaed2b1eca

3 files changed, 28 insertions(+), 12 deletions(-)

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

@@ -17,7 +17,7 @@ log.Fatal("error loading .env file")

} } -func SendArticle(article *reader.Article, to string) error { +func SendArticle(article *reader.Article, to string, readable bool) error { var ( EMAIL_USER_SECRET = os.Getenv("EMAIL_USER_SECRET") EMAIL_PASSWORD = os.Getenv("EMAIL_PASSWORD")

@@ -37,9 +37,17 @@ email := mail.New()

email.Encryption = mail.EncryptionTLS email.SetFrom(fmt.Sprintf("saved forlater <%s>", EMAIL_FROM)) email.AddTo(to) - email.SetSubject(article.Title) - email.SetBody("text/plain", string(plainContent)) - email.AddAlternative("text/html", string(htmlContent)) + if readable { + email.SetSubject(article.Title) + email.SetBody("text/plain", string(plainContent)) + email.AddAlternative("text/html", string(htmlContent)) + } else { + email.SetSubject(article.URL.String()) + email.SetBody("text/plain", fmt.Sprintf( + "We were unable to parse your link: %s", + article.URL.String(), + )) + } email.Username = EMAIL_USER_SECRET email.Password = EMAIL_PASSWORD
M main.gomain.go

@@ -32,15 +32,20 @@ log.Printf("reader fetch: %s\n", err)

} article, err := reader.Readable(f, parsedURL) - if err != nil { + if err == nil { + err = mail.SendArticle(&article, m.From, true) + if err != nil { + log.Printf("error sending mail to: %s: %v\n", m.From, err) + } else { + log.Printf("sent mail to %s: %s\n", m.From, article.Title) + } + } else { log.Printf("not readable: %s\n", err) + err := mail.SendArticle(&article, m.From, false) + if err != nil { + log.Printf("error sending mail to: %s: %v\n", m.From, err) + } } - - err = mail.SendArticle(&article, m.From) - if err != nil { - log.Println(err) - } - log.Printf("sent mail to %s: %s\n", m.From, article.Title) } w.WriteHeader(204) })
M reader/fetch.goreader/fetch.go

@@ -56,9 +56,12 @@

// Makes a given html body readable. Returns an error if it // can't. func Readable(r io.Reader, u *url.URL) (Article, error) { + if !readability.Check(r) { + return Article{readability.Article{}, u}, fmt.Errorf("failed to parse %s", u) + } article, err := readability.FromReader(r, u) if err != nil { - return Article{}, fmt.Errorf("failed to parse %s: %v\n", u, err) + return Article{readability.Article{}, u}, fmt.Errorf("failed to parse %s: %v\n", u, err) } return Article{article, u}, nil