diff options
author | René 'Necoro' Neumann <necoro@necoro.eu> | 2020-04-20 02:02:15 +0200 |
---|---|---|
committer | René 'Necoro' Neumann <necoro@necoro.eu> | 2020-04-20 02:02:15 +0200 |
commit | 9e45db27daa3b682c4d8e1e1aa9e66594cf887a4 (patch) | |
tree | 251177708d11f7118541fc49ac1549df18803ad7 | |
parent | f1231aa444bd13aa031fe6e3dac32138436f956b (diff) | |
download | feed2imap-go-9e45db27daa3b682c4d8e1e1aa9e66594cf887a4.tar.gz feed2imap-go-9e45db27daa3b682c4d8e1e1aa9e66594cf887a4.tar.bz2 feed2imap-go-9e45db27daa3b682c4d8e1e1aa9e66594cf887a4.zip |
Started with mail creation
-rw-r--r-- | go.mod | 4 | ||||
-rw-r--r-- | go.sum | 12 | ||||
-rw-r--r-- | internal/config/config.go | 28 | ||||
-rw-r--r-- | internal/feed/mail.go | 85 |
4 files changed, 126 insertions, 3 deletions
@@ -5,7 +5,11 @@ go 1.14 require ( github.com/PuerkitoBio/goquery v1.5.1 // indirect github.com/emersion/go-imap v1.0.4 + github.com/emersion/go-message v0.11.2 // indirect + github.com/jaytaylor/html2text v0.0.0-20200412013138-3577fbdbcff7 // indirect github.com/mmcdole/gofeed v1.0.0-beta2.0.20200331235650-4298e4366be3 + github.com/olekukonko/tablewriter v0.0.4 // indirect + github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf // indirect golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e // indirect golang.org/x/text v0.3.2 // indirect gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c @@ -11,16 +11,28 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/emersion/go-imap v1.0.4 h1:uiCAIHM6Z5Jwkma1zdNDWWXxSCqb+/xHBkHflD7XBro= github.com/emersion/go-imap v1.0.4/go.mod h1:yKASt+C3ZiDAiCSssxg9caIckWF/JG7ZQTO7GAmvicU= github.com/emersion/go-message v0.11.1/go.mod h1:C4jnca5HOTo4bGN9YdqNQM9sITuT3Y0K6bSUw9RklvY= +github.com/emersion/go-message v0.11.2 h1:oxO9SQ+3wgBAQRdk07eqfkCJ26Tl8ZHF7CcpGVoE00o= +github.com/emersion/go-message v0.11.2/go.mod h1:C4jnca5HOTo4bGN9YdqNQM9sITuT3Y0K6bSUw9RklvY= github.com/emersion/go-sasl v0.0.0-20191210011802-430746ea8b9b h1:uhWtEWBHgop1rqEk2klKaxPAkVDCXexai6hSuRQ7Nvs= github.com/emersion/go-sasl v0.0.0-20191210011802-430746ea8b9b/go.mod h1:G/dpzLu16WtQpBfQ/z3LYiYJn3ZhKSGWn83fyoyQe/k= +github.com/emersion/go-textwrapper v0.0.0-20160606182133-d0e65e56babe h1:40SWqY0zE3qCi6ZrtTf5OUdNm5lDnGnjRSq9GgmeTrg= github.com/emersion/go-textwrapper v0.0.0-20160606182133-d0e65e56babe/go.mod h1:aqO8z8wPrjkscevZJFVE1wXJrLpC5LtJG7fqLOsPb2U= +github.com/jaytaylor/html2text v0.0.0-20200412013138-3577fbdbcff7 h1:g0fAGBisHaEQ0TRq1iBvemFRf+8AEWEmBESSiWB3Vsc= +github.com/jaytaylor/html2text v0.0.0-20200412013138-3577fbdbcff7/go.mod h1:CVKlgaMiht+LXvHG173ujK6JUhZXKb2u/BQtjPDIvyk= +github.com/martinlindhe/base36 v1.0.0 h1:eYsumTah144C0A8P1T/AVSUk5ZoLnhfYFM3OGQxB52A= github.com/martinlindhe/base36 v1.0.0/go.mod h1:+AtEs8xrBpCeYgSLoY/aJ6Wf37jtBuR0s35750M27+8= +github.com/mattn/go-runewidth v0.0.7 h1:Ei8KR0497xHyKJPAv59M1dkC+rOZCMBJ+t3fZ+twI54= +github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= github.com/mmcdole/gofeed v1.0.0-beta2.0.20200331235650-4298e4366be3 h1:Wy+ed15cpwtLcJYNiO4Z0wmjZHpNj4q0RsGbsoxWSMA= github.com/mmcdole/gofeed v1.0.0-beta2.0.20200331235650-4298e4366be3/go.mod h1:tkVcyzS3qVMlQrQxJoEH1hkTiuo9a8emDzkMi7TZBu0= github.com/mmcdole/goxpp v0.0.0-20181012175147-0068e33feabf h1:sWGE2v+hO0Nd4yFU/S/mDBM5plIU8v/Qhfz41hkDIAI= github.com/mmcdole/goxpp v0.0.0-20181012175147-0068e33feabf/go.mod h1:pasqhqstspkosTneA62Nc+2p9SOBBYAPbnmRRWPQ0V8= +github.com/olekukonko/tablewriter v0.0.4 h1:vHD/YYe1Wolo78koG299f7V/VAS08c6IpCLn+Ejf/w8= +github.com/olekukonko/tablewriter v0.0.4/go.mod h1:zq6QwlOf5SlnkVbMSr5EoBv3636FWnp+qbPhuoO21uA= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf h1:pvbZ0lM0XWPBqUKqFU8cmavspvIl9nulOYwdy6IFRRo= +github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf/go.mod h1:RJID2RhlZKId02nZ62WenDCkgHFerpIOmW0iT7GKmXM= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= diff --git a/internal/config/config.go b/internal/config/config.go index 6497ce1..a37cef1 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -10,15 +10,17 @@ import ( type Map map[string]interface{} type GlobalOptions struct { - Timeout int `yaml:"timeout"` - DefaultEmail string `yaml:"default-email"` - Target string `yaml:"target"` + Timeout int `yaml:"timeout"` + DefaultEmail string `yaml:"default-email"` + Target string `yaml:"target"` + Parts []string `yaml:"parts"` } var DefaultGlobalOptions = GlobalOptions{ Timeout: 30, DefaultEmail: username() + "@" + hostname(), Target: "", + Parts: []string{"text", "html"}, } type Config struct { @@ -31,6 +33,26 @@ type Options struct { InclImages *bool `yaml:"include-images"` } +func (c *Config) WithPartText() bool { + for _, part := range c.Parts { + if part == "text" { + return true + } + } + + return false +} + +func (c *Config) WithPartHtml() bool { + for _, part := range c.Parts { + if part == "html" { + return true + } + } + + return false +} + func hostname() (hostname string) { hostname, err := os.Hostname() if err != nil { diff --git a/internal/feed/mail.go b/internal/feed/mail.go new file mode 100644 index 0000000..d166e09 --- /dev/null +++ b/internal/feed/mail.go @@ -0,0 +1,85 @@ +package feed + +import ( + "bytes" + "io" + "time" + + "github.com/emersion/go-message/mail" + + "github.com/Necoro/feed2imap-go/internal/config" +) + +func address(name, address string) []*mail.Address { + return []*mail.Address{{name, address}} +} + +func fromAdress(feed Feed, item feeditem, cfg config.Config) []*mail.Address { + switch { + case item.Item.Author != nil && item.Item.Author.Email != "": + return address(item.Item.Author.Name, item.Item.Author.Email) + case item.Item.Author != nil && item.Item.Author.Name != "": + return address(item.Item.Author.Name, cfg.DefaultEmail) + case item.Feed.Author != nil && item.Feed.Author.Email != "": + return address(item.Feed.Author.Name, item.Feed.Author.Email) + case item.Feed.Author != nil && item.Feed.Author.Name != "": + return address(item.Feed.Author.Name, cfg.DefaultEmail) + default: + return address(feed.Name, cfg.DefaultEmail) + } +} + +func asMail(feed Feed, item feeditem, cfg config.Config) (string, error) { + var b bytes.Buffer + + var h mail.Header + h.SetAddressList("From", fromAdress(feed, item, cfg)) + h.SetAddressList("To", address(feed.Name, cfg.DefaultEmail)) + + { // date + date := item.Item.PublishedParsed + if date == nil { + now := time.Now() + date = &now + } + h.SetDate(*date) + } + { // subject + subject := item.Item.Title + if subject == "" { + subject = item.Item.Published + } + if subject == "" { + subject = item.Item.Link + } + h.SetSubject(subject) + } + + mw, err := mail.CreateWriter(&b, h) + if err != nil { + return "", err + } + + if cfg.WithPartText() { + tw, err := mw.CreateInline() + if err != nil { + return "", err + } + + var th mail.InlineHeader + th.SetContentType("text/plain", map[string]string{"charset": "utf-8", "format": "flowed"}) + + w, err := tw.CreatePart(th) + if err != nil { + return "", err + } + _, _ = io.WriteString(w, "Who are you?") + + _ = w.Close() + _ = tw.Close() + } + + _ = mw.Close() + + return b.String(), nil +} |