moved imap folder prefix to imap config
This commit is contained in:
parent
4f49c854c8
commit
f500150974
|
@ -17,9 +17,10 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Configuration struct {
|
type Configuration struct {
|
||||||
IMAPURL string
|
IMAPURL string
|
||||||
IMAPUsername string
|
IMAPUsername string
|
||||||
IMAPPassword string
|
IMAPPassword string
|
||||||
|
IMAPFolderPrefix string
|
||||||
|
|
||||||
SMTPURL string
|
SMTPURL string
|
||||||
SMTPUsername string
|
SMTPUsername string
|
||||||
|
@ -56,6 +57,8 @@ func New(src io.Reader) *Configuration {
|
||||||
conf.IMAPUsername = value
|
conf.IMAPUsername = value
|
||||||
case "imap_password":
|
case "imap_password":
|
||||||
conf.IMAPPassword = value
|
conf.IMAPPassword = value
|
||||||
|
case "imap_folder_prefix":
|
||||||
|
conf.IMAPFolderPrefix = value
|
||||||
case "smtp_url":
|
case "smtp_url":
|
||||||
conf.SMTPURL = value
|
conf.SMTPURL = value
|
||||||
case "smtp_username":
|
case "smtp_username":
|
||||||
|
@ -80,9 +83,10 @@ func New(src io.Reader) *Configuration {
|
||||||
|
|
||||||
func (c *Configuration) IMAP() *mstore.IMAPConfig {
|
func (c *Configuration) IMAP() *mstore.IMAPConfig {
|
||||||
return &mstore.IMAPConfig{
|
return &mstore.IMAPConfig{
|
||||||
IMAPURL: c.IMAPURL,
|
IMAPURL: c.IMAPURL,
|
||||||
IMAPUsername: c.IMAPUsername,
|
IMAPUsername: c.IMAPUsername,
|
||||||
IMAPPassword: c.IMAPPassword,
|
IMAPPassword: c.IMAPPassword,
|
||||||
|
IMAPFolderPrefix: c.IMAPFolderPrefix,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -42,11 +42,12 @@ func TestNew(t *testing.T) {
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "imap",
|
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{
|
exp: &configuration.Configuration{
|
||||||
IMAPURL: "url",
|
IMAPURL: "url",
|
||||||
IMAPUsername: "username",
|
IMAPUsername: "username",
|
||||||
IMAPPassword: "password",
|
IMAPPassword: "password",
|
||||||
|
IMAPFolderPrefix: "prefix",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -84,24 +85,26 @@ func TestNew(t *testing.T) {
|
||||||
|
|
||||||
func TestConfigs(t *testing.T) {
|
func TestConfigs(t *testing.T) {
|
||||||
conf := &configuration.Configuration{
|
conf := &configuration.Configuration{
|
||||||
IMAPURL: "imap_url",
|
IMAPURL: "imap_url",
|
||||||
IMAPUsername: "imap_username",
|
IMAPUsername: "imap_username",
|
||||||
IMAPPassword: "imap_password",
|
IMAPPassword: "imap_password",
|
||||||
SMTPURL: "smtp_url",
|
IMAPFolderPrefix: "imap_folder_prefix",
|
||||||
SMTPUsername: "smtp_username",
|
SMTPURL: "smtp_url",
|
||||||
SMTPPassword: "smtp_password",
|
SMTPUsername: "smtp_username",
|
||||||
ToName: "to_name",
|
SMTPPassword: "smtp_password",
|
||||||
ToAddress: "to_address",
|
ToName: "to_name",
|
||||||
FromName: "from_name",
|
ToAddress: "to_address",
|
||||||
FromAddress: "from_address",
|
FromName: "from_name",
|
||||||
LocalDBPath: "db_path",
|
FromAddress: "from_address",
|
||||||
|
LocalDBPath: "db_path",
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Run("imap", func(t *testing.T) {
|
t.Run("imap", func(t *testing.T) {
|
||||||
exp := &mstore.IMAPConfig{
|
exp := &mstore.IMAPConfig{
|
||||||
IMAPURL: "imap_url",
|
IMAPURL: "imap_url",
|
||||||
IMAPUsername: "imap_username",
|
IMAPUsername: "imap_username",
|
||||||
IMAPPassword: "imap_password",
|
IMAPPassword: "imap_password",
|
||||||
|
IMAPFolderPrefix: "imap_folder_prefix",
|
||||||
}
|
}
|
||||||
|
|
||||||
test.Equals(t, exp, conf.IMAP())
|
test.Equals(t, exp, conf.IMAP())
|
||||||
|
|
|
@ -16,11 +16,11 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
FOLDER_INBOX = "GTE/Inbox"
|
FOLDER_INBOX = "Inbox"
|
||||||
FOLDER_NEW = "GTE/New"
|
FOLDER_NEW = "New"
|
||||||
FOLDER_RECURRING = "GTE/Recurring"
|
FOLDER_RECURRING = "Recurring"
|
||||||
FOLDER_PLANNED = "GTE/Planned"
|
FOLDER_PLANNED = "Planned"
|
||||||
FOLDER_UNPLANNED = "GTE/Unplanned"
|
FOLDER_UNPLANNED = "Unplanned"
|
||||||
|
|
||||||
QUOTE_PREFIX = ">"
|
QUOTE_PREFIX = ">"
|
||||||
PREVIOUS_SEPARATOR = "Previous version:"
|
PREVIOUS_SEPARATOR = "Previous version:"
|
||||||
|
|
|
@ -41,9 +41,10 @@ func (b *IMAPBody) Len() int {
|
||||||
}
|
}
|
||||||
|
|
||||||
type IMAPConfig struct {
|
type IMAPConfig struct {
|
||||||
IMAPURL string
|
IMAPURL string
|
||||||
IMAPUsername string
|
IMAPUsername string
|
||||||
IMAPPassword string
|
IMAPPassword string
|
||||||
|
IMAPFolderPrefix string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (esc *IMAPConfig) Valid() bool {
|
func (esc *IMAPConfig) Valid() bool {
|
||||||
|
@ -97,34 +98,12 @@ func (im *IMAP) Close() {
|
||||||
im.connected = false
|
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 {
|
func (im *IMAP) selectFolder(folder string) error {
|
||||||
if !im.connected {
|
if !im.connected {
|
||||||
return ErrIMAPNotConnected
|
return ErrIMAPNotConnected
|
||||||
}
|
}
|
||||||
|
|
||||||
|
folder = fmt.Sprintf("%s%s", im.config.IMAPFolderPrefix, folder)
|
||||||
status, err := im.client.Select(folder, false)
|
status, err := im.client.Select(folder, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("%w, %v", ErrIMAPServerProblem, err)
|
return fmt.Errorf("%w, %v", ErrIMAPServerProblem, err)
|
||||||
|
@ -243,6 +222,7 @@ func (im *IMAP) Add(folder, subject, body string) error {
|
||||||
)
|
)
|
||||||
msg := NewIMAPBody(msgStr)
|
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 {
|
if err := im.client.Append(folder, nil, time.Time{}, imap.Literal(msg)); err != nil {
|
||||||
return fmt.Errorf("%w: %v", ErrIMAPServerProblem, err)
|
return fmt.Errorf("%w: %v", ErrIMAPServerProblem, err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,10 +37,6 @@ func NewMemory(folders []string) (*Memory, error) {
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mem *Memory) Folders() ([]string, error) {
|
|
||||||
return mem.folders, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (mem *Memory) Add(folder, subject, body string) error {
|
func (mem *Memory) Add(folder, subject, body string) error {
|
||||||
if subject == "" {
|
if subject == "" {
|
||||||
return ErrInvalidMessage
|
return ErrInvalidMessage
|
||||||
|
|
|
@ -2,7 +2,6 @@ package mstore_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"sort"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"ewintr.nl/go-kit/test"
|
"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) {
|
func TestMemoryAdd(t *testing.T) {
|
||||||
folder := "folder"
|
folder := "folder"
|
||||||
subject := "subject"
|
subject := "subject"
|
||||||
|
|
|
@ -38,7 +38,6 @@ func (m *Message) Equal(n *Message) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
type MStorer interface {
|
type MStorer interface {
|
||||||
Folders() ([]string, error)
|
|
||||||
Messages(folder string) ([]*Message, error)
|
Messages(folder string) ([]*Message, error)
|
||||||
Add(folder, subject, body string) error
|
Add(folder, subject, body string) error
|
||||||
Remove(msg *Message) error
|
Remove(msg *Message) error
|
||||||
|
|
Loading…
Reference in New Issue