summary refs log tree commit diff stats
path: root/worker
diff options
context:
space:
mode:
Diffstat (limited to 'worker')
-rw-r--r--worker/handlers/register.go27
-rw-r--r--worker/imap/worker.go10
-rw-r--r--worker/maildir/worker.go7
-rw-r--r--worker/worker.go24
-rw-r--r--worker/worker_enabled.go5
5 files changed, 53 insertions, 20 deletions
diff --git a/worker/handlers/register.go b/worker/handlers/register.go
new file mode 100644
index 0000000..5e8463e
--- /dev/null
+++ b/worker/handlers/register.go
@@ -0,0 +1,27 @@
+package handlers
+
+import (
+	"fmt"
+
+	"git.sr.ht/~sircmpwn/aerc/worker/types"
+)
+
+type FactoryFunc func(*types.Worker) (types.Backend, error)
+
+var workerFactories map[string]FactoryFunc = make(map[string]FactoryFunc)
+
+func RegisterWorkerFactory(scheme string, factory FactoryFunc) {
+	workerFactories[scheme] = factory
+}
+
+func GetHandlerForScheme(scheme string, worker *types.Worker) (types.Backend, error) {
+	factory, ok := workerFactories[scheme]
+	if !ok {
+		return nil, fmt.Errorf("Unknown backend %s", scheme)
+	}
+	backend, err := factory(worker)
+	if err != nil {
+		return nil, err
+	}
+	return backend, nil
+}
diff --git a/worker/imap/worker.go b/worker/imap/worker.go
index de86994..b1ef9c9 100644
--- a/worker/imap/worker.go
+++ b/worker/imap/worker.go
@@ -13,9 +13,15 @@ import (
 
 	"git.sr.ht/~sircmpwn/aerc/lib"
 	"git.sr.ht/~sircmpwn/aerc/models"
+	"git.sr.ht/~sircmpwn/aerc/worker/handlers"
 	"git.sr.ht/~sircmpwn/aerc/worker/types"
 )
 
+func init() {
+	handlers.RegisterWorkerFactory("imap", NewIMAPWorker)
+	handlers.RegisterWorkerFactory("imaps", NewIMAPWorker)
+}
+
 var errUnsupported = fmt.Errorf("unsupported command")
 
 type imapClient struct {
@@ -43,12 +49,12 @@ type IMAPWorker struct {
 	seqMap []uint32
 }
 
-func NewIMAPWorker(worker *types.Worker) *IMAPWorker {
+func NewIMAPWorker(worker *types.Worker) (types.Backend, error) {
 	return &IMAPWorker{
 		idleDone: make(chan error),
 		updates:  make(chan client.Update, 50),
 		worker:   worker,
-	}
+	}, nil
 }
 
 func (w *IMAPWorker) handleMessage(msg types.WorkerMessage) error {
diff --git a/worker/maildir/worker.go b/worker/maildir/worker.go
index 1e68a2e..84e0162 100644
--- a/worker/maildir/worker.go
+++ b/worker/maildir/worker.go
@@ -11,9 +11,14 @@ import (
 	"github.com/fsnotify/fsnotify"
 
 	"git.sr.ht/~sircmpwn/aerc/models"
+	"git.sr.ht/~sircmpwn/aerc/worker/handlers"
 	"git.sr.ht/~sircmpwn/aerc/worker/types"
 )
 
+func init() {
+	handlers.RegisterWorkerFactory("maildir", NewWorker)
+}
+
 var errUnsupported = fmt.Errorf("unsupported command")
 
 // A Worker handles interfacing between aerc's UI and a group of maildirs.
@@ -25,7 +30,7 @@ type Worker struct {
 }
 
 // NewWorker creates a new maildir worker with the provided worker.
-func NewWorker(worker *types.Worker) (*Worker, error) {
+func NewWorker(worker *types.Worker) (types.Backend, error) {
 	watch, err := fsnotify.NewWatcher()
 	if err != nil {
 		return nil, fmt.Errorf("could not create file system watcher: %v", err)
diff --git a/worker/worker.go b/worker/worker.go
index a37927e..f1f47c3 100644
--- a/worker/worker.go
+++ b/worker/worker.go
@@ -1,14 +1,13 @@
 package worker
 
 import (
-	"git.sr.ht/~sircmpwn/aerc/worker/imap"
-	"git.sr.ht/~sircmpwn/aerc/worker/maildir"
-	"git.sr.ht/~sircmpwn/aerc/worker/types"
-
 	"fmt"
 	"log"
 	"net/url"
 	"strings"
+
+	"git.sr.ht/~sircmpwn/aerc/worker/handlers"
+	"git.sr.ht/~sircmpwn/aerc/worker/types"
 )
 
 // Guesses the appropriate worker type based on the given source string
@@ -23,19 +22,10 @@ func NewWorker(source string, logger *log.Logger) (*types.Worker, error) {
 		scheme = scheme[:strings.IndexRune(scheme, '+')]
 		fmt.Println(scheme)
 	}
-	switch scheme {
-	case "imap":
-		fallthrough
-	case "imaps":
-		worker.Backend = imap.NewIMAPWorker(worker)
-	case "maildir":
-		if w, err := maildir.NewWorker(worker); err != nil {
-			return nil, fmt.Errorf("could not create maildir worker: %v", err)
-		} else {
-			worker.Backend = w
-		}
-	default:
-		return nil, fmt.Errorf("Unknown backend %s", u.Scheme)
+	backend, err := handlers.GetHandlerForScheme(scheme, worker)
+	if err != nil {
+		return nil, err
 	}
+	worker.Backend = backend
 	return worker, nil
 }
diff --git a/worker/worker_enabled.go b/worker/worker_enabled.go
new file mode 100644
index 0000000..4fba729
--- /dev/null
+++ b/worker/worker_enabled.go
@@ -0,0 +1,5 @@
+package worker
+
+// the following workers are always enabled
+import _ "git.sr.ht/~sircmpwn/aerc/worker/imap"
+import _ "git.sr.ht/~sircmpwn/aerc/worker/maildir"