aboutsummaryrefslogtreecommitdiff
path: root/internal/imap/imap.go
diff options
context:
space:
mode:
authorRené 'Necoro' Neumann <necoro@necoro.eu>2020-04-19 19:10:41 +0200
committerRené 'Necoro' Neumann <necoro@necoro.eu>2020-04-19 19:10:41 +0200
commit9f5a52b14d142fe3aae6c3b8be67cb622f0c93f6 (patch)
tree74f7c1a079832be3809775b867551a89d5a86e16 /internal/imap/imap.go
parent40f5d435e5f92abc8b64dae1c029f67d620db1f2 (diff)
downloadfeed2imap-go-9f5a52b14d142fe3aae6c3b8be67cb622f0c93f6.tar.gz
feed2imap-go-9f5a52b14d142fe3aae6c3b8be67cb622f0c93f6.tar.bz2
feed2imap-go-9f5a52b14d142fe3aae6c3b8be67cb622f0c93f6.zip
Improved IMAP
Diffstat (limited to '')
-rw-r--r--internal/imap/imap.go91
1 files changed, 88 insertions, 3 deletions
diff --git a/internal/imap/imap.go b/internal/imap/imap.go
index 18039d3..94e92ed 100644
--- a/internal/imap/imap.go
+++ b/internal/imap/imap.go
@@ -15,9 +15,23 @@ const (
imapPort = "143"
)
+type folders map[string]*imap.MailboxInfo
+
+func (f folders) contains(elem string) bool {
+ _, ok := f[elem]
+ return ok
+}
+
+func (f folders) add(elem *imap.MailboxInfo) {
+ name := elem.Name
+ f[name] = elem
+}
+
type Client struct {
- c *imapClient.Client
- host string
+ c *imapClient.Client
+ host string
+ folders folders
+ delimiter string
}
func forceTLS(url *url.URL) bool {
@@ -70,6 +84,69 @@ func (client *Client) Disconnect() {
}
}
+func (client *Client) createFolder(folder string) error {
+ return nil
+}
+
+func (client *Client) list(folder string) (*imap.MailboxInfo, int, error) {
+ mailboxes := make(chan *imap.MailboxInfo, 10)
+ done := make(chan error, 1)
+ go func() {
+ done <- client.c.List("", folder, mailboxes)
+ }()
+
+ found := 0
+ var mbox *imap.MailboxInfo
+ for m := range mailboxes {
+ if found == 0 {
+ mbox = m
+ }
+ found++
+ }
+
+ if err := <-done; err != nil {
+ return nil, 0, fmt.Errorf("while listing '%s': %w", folder, err)
+ }
+
+ return mbox, found, nil
+}
+
+func (client *Client) fetchDelimiter() error {
+ mbox, _, err := client.list("")
+ if err != nil {
+ return err
+ }
+
+ client.delimiter = mbox.Delimiter
+ return nil
+}
+
+func (client *Client) EnsureFolder(folder string) error {
+ if folder[0] == '/' {
+ folder = folder[1:]
+ }
+
+ if client.folders.contains(folder) {
+ return nil
+ }
+
+ log.Printf("Checking for folder '%s'", folder)
+
+ mbox, found, err := client.list(folder)
+
+ switch {
+ case err != nil:
+ return err
+ case found == 0:
+ return client.createFolder(folder)
+ case found == 1:
+ client.folders.add(mbox)
+ return nil
+ default:
+ return fmt.Errorf("Found multiple folders matching '%s'.", folder)
+ }
+}
+
func Connect(url *url.URL) (*Client, error) {
var c *imapClient.Client
var err error
@@ -91,7 +168,7 @@ func Connect(url *url.URL) (*Client, error) {
}
}
- var client = Client{c, url.Host}
+ var client = Client{c: c, host: url.Host, folders: folders{}}
defer func() {
if err != nil {
@@ -122,5 +199,13 @@ func Connect(url *url.URL) (*Client, error) {
return nil, fmt.Errorf("login to %s: %w", url.Host, err)
}
+ if err = client.fetchDelimiter(); err != nil {
+ return nil, fmt.Errorf("fetching delimiter: %w", err)
+ }
+
+ if err = client.EnsureFolder(url.Path); err != nil {
+ return nil, err
+ }
+
return &client, nil
}
n class='insertions'>+2 2007-04-24add application icon and desktop filenecoro1-2/+2 2007-04-24add application icon and desktop filenecoro7-4/+25 2007-04-24moved *.glade and *.ui to one directory to omit the change of the config.py ↵necoro8-5/+2538 each time 2007-04-24moved *.glade and *.ui to one directory to omit the change of the config.py ↵necoro5-1191/+0 each time 2007-04-24moved *.glade and *.ui to one directory to omit the change of the config.py ↵necoro1-1340/+0 each time 2007-04-24added dialogs to warn you when the queue is running on quitting portatonecoro8-214/+267 2007-04-20Made qt plugin-ready; lots of documentationnecoro15-81/+351 2007-04-18nomsgnecoro1-1/+0 2007-04-18finished Qt-Frontendnecoro5-15/+117 2007-04-17marking installed packages in pkglistnecoro5-14/+50 2007-04-16added ebuild window for Qt-Frontendnecoro4-2/+229 2007-04-16added preferences for Qt-Frontendnecoro9-279/+710 2007-04-13Improved masking display and made most of the Qt-Frontend worknecoro7-35/+334 2007-04-10Some more functionality for the Qt-Frontend (complete emerge)necoro8-217/+347 2007-04-07Some more functionality for the Qt-Frontendnecoro11-88/+463 2007-04-07Added Qt-Terminalnecoro4-4/+213 2007-04-06First qt draftnecoro6-1/+796 2007-04-04showed masked packages unmasked by the user similar to stable marked testing ↵necoro5-13/+40 packages 2007-03-31changed changelognecoro1-1/+2 2007-03-31Some small changes for etcproposals 1.1necoro1-2/+2 2007-03-31Some small changes for etcproposals 1.1necoro2-3/+3 2007-03-31Some small changes for etcproposals 1.1necoro1-6/+13 2007-03-31Allowed Plugins to have a menunecoro7-201/+315 2007-03-15Added etc-proposals pluginnecoro7-28/+121 2007-03-10Added USE_EXPAND-supportnecoro5-11/+63 2007-03-10Added plugin-data to about-dialognecoro3-197/+264