moved imap folder prefix to imap config
This commit is contained in:
parent
4f49c854c8
commit
f500150974
|
@ -20,6 +20,7 @@ type Configuration struct {
|
|||
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":
|
||||
|
@ -83,6 +86,7 @@ func (c *Configuration) IMAP() *mstore.IMAPConfig {
|
|||
IMAPURL: c.IMAPURL,
|
||||
IMAPUsername: c.IMAPUsername,
|
||||
IMAPPassword: c.IMAPPassword,
|
||||
IMAPFolderPrefix: c.IMAPFolderPrefix,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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",
|
||||
IMAPFolderPrefix: "prefix",
|
||||
},
|
||||
},
|
||||
{
|
||||
|
@ -87,6 +88,7 @@ func TestConfigs(t *testing.T) {
|
|||
IMAPURL: "imap_url",
|
||||
IMAPUsername: "imap_username",
|
||||
IMAPPassword: "imap_password",
|
||||
IMAPFolderPrefix: "imap_folder_prefix",
|
||||
SMTPURL: "smtp_url",
|
||||
SMTPUsername: "smtp_username",
|
||||
SMTPPassword: "smtp_password",
|
||||
|
@ -102,6 +104,7 @@ func TestConfigs(t *testing.T) {
|
|||
IMAPURL: "imap_url",
|
||||
IMAPUsername: "imap_username",
|
||||
IMAPPassword: "imap_password",
|
||||
IMAPFolderPrefix: "imap_folder_prefix",
|
||||
}
|
||||
|
||||
test.Equals(t, exp, conf.IMAP())
|
||||
|
|
|
@ -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:"
|
||||
|
|
|
@ -44,6 +44,7 @@ type IMAPConfig struct {
|
|||
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)
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue