main.go (view raw)
1package main
2
3import (
4 "encoding/json"
5 "log"
6 "net/http"
7 "net/url"
8
9 "git.icyphox.sh/forlater/navani/mail"
10 "git.icyphox.sh/forlater/navani/reader"
11)
12
13func main() {
14 http.HandleFunc("/webhook", func(w http.ResponseWriter, r *http.Request) {
15 m := mail.Mail{}
16 json.NewDecoder(r.Body).Decode(&m)
17 body, err := mail.MailBody(m.Parts)
18 log.Printf("recieved webhook: %v\n", m)
19 if err != nil {
20 log.Println(err)
21 }
22
23 for _, u := range distinct(mail.ExtractURLs(body)) {
24 parsedURL, err := url.Parse(u)
25 if err != nil {
26 log.Printf("url parse: %s\n", err)
27 }
28
29 f, err := reader.Fetch(parsedURL.String())
30 if err != nil {
31 log.Printf("reader fetch: %s\n", err)
32 }
33
34 article, err := reader.Readable(f, parsedURL)
35 if err == nil {
36 err = mail.SendArticle(&article, m.From, true)
37 if err != nil {
38 log.Printf("error sending mail to: %s: %v\n", m.From, err)
39 } else {
40 log.Printf("sent mail to %s: %s\n", m.From, article.Title)
41 }
42 } else {
43 log.Printf("not readable: %s\n", err)
44 err := mail.SendArticle(&article, m.From, false)
45 if err != nil {
46 log.Printf("error sending mail to: %s: %v\n", m.From, err)
47 }
48 }
49 }
50 w.WriteHeader(204)
51 })
52
53 http.ListenAndServe(":8001", nil)
54}