~ewintr/gte

f5001509744b354d6360c22e128faca62587ceaf — Erik Winter 2 months ago 4f49c85
moved imap folder prefix to imap config
M internal/configuration/configuration.go => internal/configuration/configuration.go +10 -6
@@ 17,9 17,10 @@ var (
)

type Configuration struct {
	IMAPURL      string
	IMAPUsername string
	IMAPPassword string
	IMAPURL          string
	IMAPUsername     string
	IMAPPassword     string
	IMAPFolderPrefix string

	SMTPURL      string
	SMTPUsername string


@@ 56,6 57,8 @@ func New(src io.Reader) *Configuration {
			conf.IMAPUsername = value
		case "imap_password":
			conf.IMAPPassword = value
		case "imap_folder_prefix":
			conf.IMAPFolderPrefix = value
		case "smtp_url":
			conf.SMTPURL = value
		case "smtp_username":


@@ 80,9 83,10 @@ func New(src io.Reader) *Configuration {

func (c *Configuration) IMAP() *mstore.IMAPConfig {
	return &mstore.IMAPConfig{
		IMAPURL:      c.IMAPURL,
		IMAPUsername: c.IMAPUsername,
		IMAPPassword: c.IMAPPassword,
		IMAPURL:          c.IMAPURL,
		IMAPUsername:     c.IMAPUsername,
		IMAPPassword:     c.IMAPPassword,
		IMAPFolderPrefix: c.IMAPFolderPrefix,
	}
}


M internal/configuration/configuration_test.go => internal/configuration/configuration_test.go +21 -18
@@ 42,11 42,12 @@ func TestNew(t *testing.T) {
		},
		{
			name:   "imap",
			source: "imap_url=url\nimap_username=username\nimap_password=password",
			source: "imap_url=url\nimap_username=username\nimap_password=password\nimap_folder_prefix=prefix",
			exp: &configuration.Configuration{
				IMAPURL:      "url",
				IMAPUsername: "username",
				IMAPPassword: "password",
				IMAPURL:          "url",
				IMAPUsername:     "username",
				IMAPPassword:     "password",
				IMAPFolderPrefix: "prefix",
			},
		},
		{


@@ 84,24 85,26 @@ func TestNew(t *testing.T) {

func TestConfigs(t *testing.T) {
	conf := &configuration.Configuration{
		IMAPURL:      "imap_url",
		IMAPUsername: "imap_username",
		IMAPPassword: "imap_password",
		SMTPURL:      "smtp_url",
		SMTPUsername: "smtp_username",
		SMTPPassword: "smtp_password",
		ToName:       "to_name",
		ToAddress:    "to_address",
		FromName:     "from_name",
		FromAddress:  "from_address",
		LocalDBPath:  "db_path",
		IMAPURL:          "imap_url",
		IMAPUsername:     "imap_username",
		IMAPPassword:     "imap_password",
		IMAPFolderPrefix: "imap_folder_prefix",
		SMTPURL:          "smtp_url",
		SMTPUsername:     "smtp_username",
		SMTPPassword:     "smtp_password",
		ToName:           "to_name",
		ToAddress:        "to_address",
		FromName:         "from_name",
		FromAddress:      "from_address",
		LocalDBPath:      "db_path",
	}

	t.Run("imap", func(t *testing.T) {
		exp := &mstore.IMAPConfig{
			IMAPURL:      "imap_url",
			IMAPUsername: "imap_username",
			IMAPPassword: "imap_password",
			IMAPURL:          "imap_url",
			IMAPUsername:     "imap_username",
			IMAPPassword:     "imap_password",
			IMAPFolderPrefix: "imap_folder_prefix",
		}

		test.Equals(t, exp, conf.IMAP())

M internal/task/task.go => internal/task/task.go +5 -5
@@ 16,11 16,11 @@ var (
)

const (
	FOLDER_INBOX     = "GTE/Inbox"
	FOLDER_NEW       = "GTE/New"
	FOLDER_RECURRING = "GTE/Recurring"
	FOLDER_PLANNED   = "GTE/Planned"
	FOLDER_UNPLANNED = "GTE/Unplanned"
	FOLDER_INBOX     = "Inbox"
	FOLDER_NEW       = "New"
	FOLDER_RECURRING = "Recurring"
	FOLDER_PLANNED   = "Planned"
	FOLDER_UNPLANNED = "Unplanned"

	QUOTE_PREFIX       = ">"
	PREVIOUS_SEPARATOR = "Previous version:"

M pkg/mstore/imap.go => pkg/mstore/imap.go +6 -26
@@ 41,9 41,10 @@ func (b *IMAPBody) Len() int {
}

type IMAPConfig struct {
	IMAPURL      string
	IMAPUsername string
	IMAPPassword string
	IMAPURL          string
	IMAPUsername     string
	IMAPPassword     string
	IMAPFolderPrefix string
}

func (esc *IMAPConfig) Valid() bool {


@@ 97,34 98,12 @@ func (im *IMAP) Close() {
	im.connected = false
}

func (im *IMAP) Folders() ([]string, error) {
	if err := im.Connect(); err != nil {
		return []string{}, err
	}
	defer im.Close()

	boxes, done := make(chan *imap.MailboxInfo), make(chan error)
	go func() {
		done <- im.client.List("", "*", boxes)
	}()

	folders := []string{}
	for b := range boxes {
		folders = append(folders, b.Name)
	}

	if err := <-done; err != nil {
		return []string{}, err
	}

	return folders, nil
}

func (im *IMAP) selectFolder(folder string) error {
	if !im.connected {
		return ErrIMAPNotConnected
	}

	folder = fmt.Sprintf("%s%s", im.config.IMAPFolderPrefix, folder)
	status, err := im.client.Select(folder, false)
	if err != nil {
		return fmt.Errorf("%w, %v", ErrIMAPServerProblem, err)


@@ 243,6 222,7 @@ func (im *IMAP) Add(folder, subject, body string) error {
	)
	msg := NewIMAPBody(msgStr)

	folder = fmt.Sprintf("%s%s", im.config.IMAPFolderPrefix, folder)
	if err := im.client.Append(folder, nil, time.Time{}, imap.Literal(msg)); err != nil {
		return fmt.Errorf("%w: %v", ErrIMAPServerProblem, err)
	}

M pkg/mstore/memory.go => pkg/mstore/memory.go +0 -4
@@ 37,10 37,6 @@ func NewMemory(folders []string) (*Memory, error) {
	}, nil
}

func (mem *Memory) Folders() ([]string, error) {
	return mem.folders, nil
}

func (mem *Memory) Add(folder, subject, body string) error {
	if subject == "" {
		return ErrInvalidMessage

M pkg/mstore/memory_test.go => pkg/mstore/memory_test.go +0 -27
@@ 2,7 2,6 @@ package mstore_test

import (
	"fmt"
	"sort"
	"testing"

	"ewintr.nl/go-kit/test"


@@ 41,32 40,6 @@ func TestNewMemory(t *testing.T) {
	}
}

func TestMemoryFolders(t *testing.T) {
	for _, tc := range []struct {
		name    string
		folders []string
	}{
		{
			name:    "one",
			folders: []string{"one"},
		},
		{
			name:    "many",
			folders: []string{"one", "two", "three"},
		},
	} {
		t.Run(tc.name, func(t *testing.T) {
			mem, err := mstore.NewMemory(tc.folders)
			test.OK(t, err)
			actFolders, err := mem.Folders()
			test.OK(t, err)
			expFolders := tc.folders
			sort.Strings(expFolders)
			test.Equals(t, expFolders, actFolders)
		})
	}
}

func TestMemoryAdd(t *testing.T) {
	folder := "folder"
	subject := "subject"

M pkg/mstore/mstore.go => pkg/mstore/mstore.go +0 -1
@@ 38,7 38,6 @@ func (m *Message) Equal(n *Message) bool {
}

type MStorer interface {
	Folders() ([]string, error)
	Messages(folder string) ([]*Message, error)
	Add(folder, subject, body string) error
	Remove(msg *Message) error