aboutsummaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
authorRené 'Necoro' Neumann <necoro@necoro.eu>2020-04-20 02:02:15 +0200
committerRené 'Necoro' Neumann <necoro@necoro.eu>2020-04-20 02:02:15 +0200
commit9e45db27daa3b682c4d8e1e1aa9e66594cf887a4 (patch)
tree251177708d11f7118541fc49ac1549df18803ad7 /internal
parentf1231aa444bd13aa031fe6e3dac32138436f956b (diff)
downloadfeed2imap-go-9e45db27daa3b682c4d8e1e1aa9e66594cf887a4.tar.gz
feed2imap-go-9e45db27daa3b682c4d8e1e1aa9e66594cf887a4.tar.bz2
feed2imap-go-9e45db27daa3b682c4d8e1e1aa9e66594cf887a4.zip
Started with mail creation
Diffstat (limited to '')
-rw-r--r--internal/config/config.go28
-rw-r--r--internal/feed/mail.go85
2 files changed, 110 insertions, 3 deletions
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
+}
to/__init__.py?id=147ed67257f351737707c7189b072973a7e9fffc&follow=1'>Also show revision during startup and with -vRené 'Necoro' Neumann1-1/+5 2009-10-24Adding revison info to version output.René 'Necoro' Neumann3-2/+44 2009-10-23Turn new_version plugin from a mess into sth useful.René 'Necoro' Neumann2-10/+10 2009-10-23Honor branches different from masterRené 'Necoro' Neumann1-2/+4 2009-10-23Change the new_version plugin to use git.René 'Necoro' Neumann2-32/+33 2009-10-15Objectified all the functional stuff in backend.__init__.René 'Necoro' Neumann2-32/+34 2009-10-08Enhance the splash window handling.René 'Necoro' Neumann2-2/+9 2009-10-08Enhance the splash window handling.René 'Necoro' Neumann2-2/+9 2009-10-05Some more stuff to ignoreRené 'Necoro' Neumann1-0/+3 2009-10-05Renamed the ignore fileRené 'Necoro' Neumann1-0/+0 2009-10-05Update NEWSRené 'Necoro' Neumann1-0/+1 2009-10-05Also allow 'unselect all' in the PkgListRené 'Necoro' Neumann1-1/+10 2009-10-05Now have it the sorted way in PkgListsRené 'Necoro' Neumann2-3/+9 2009-10-05Enhanced system.sort_package_list to also sort CPVsRené 'Necoro' Neumann5-27/+38 2009-10-05Added an PkgList window and rewrote UpdateWindow and WorldListWindow to use itRené 'Necoro' Neumann3-39/+63 2009-10-05Add uninstall button and rename to PkgListWindowRené 'Necoro' Neumann1-2/+17 2009-10-05First quick hack to have a world listRené 'Necoro' Neumann3-2/+24