about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorDrew DeVault <sir@cmpwn.com>2018-01-09 20:39:00 -0500
committerDrew DeVault <sir@cmpwn.com>2018-01-09 20:39:00 -0500
commit6394e386c2a88c3b376cd422a7b7ce5ae7534984 (patch)
tree2d78f622096e439cf5d91a090999f5c522796c42
parent7d0edcc9e7ecdedb8cabafac1167581d7205596e (diff)
downloadaerc-6394e386c2a88c3b376cd422a7b7ce5ae7534984.tar.gz
Initial pass on worker/UI message passing
-rw-r--r--cmd/aerc/main.go10
-rw-r--r--config/config.go9
-rw-r--r--worker/imap/worker.go58
-rw-r--r--worker/types/messages.go55
-rw-r--r--worker/worker.go18
5 files changed, 145 insertions, 5 deletions
diff --git a/cmd/aerc/main.go b/cmd/aerc/main.go
index 96a8e0c..2627a71 100644
--- a/cmd/aerc/main.go
+++ b/cmd/aerc/main.go
@@ -4,6 +4,8 @@ import (
 	"fmt"
 
 	"git.sr.ht/~sircmpwn/aerc2/config"
+	"git.sr.ht/~sircmpwn/aerc2/worker"
+	"git.sr.ht/~sircmpwn/aerc2/worker/types"
 )
 
 func main() {
@@ -15,4 +17,12 @@ func main() {
 		panic(err)
 	}
 	fmt.Printf("%+v\n", *c)
+	w := worker.NewWorker("")
+	go w.Run()
+	w.PostAction(types.Ping{})
+	for {
+		if msg := w.GetMessage(); msg != nil {
+			fmt.Printf("<- %T: %v\n", msg, msg)
+		}
+	}
 }
diff --git a/config/config.go b/config/config.go
index b620258..6a420fa 100644
--- a/config/config.go
+++ b/config/config.go
@@ -20,11 +20,10 @@ type UIConfig struct {
 }
 
 type AccountConfig struct {
-	ConfigPath string
-	Name       string
-	Source     string
-	Folders    []string
-	Params     map[string]string
+	Name    string
+	Source  string
+	Folders []string
+	Params  map[string]string
 }
 
 type AercConfig struct {
diff --git a/worker/imap/worker.go b/worker/imap/worker.go
new file mode 100644
index 0000000..a095e60
--- /dev/null
+++ b/worker/imap/worker.go
@@ -0,0 +1,58 @@
+package imap
+
+import (
+	"git.sr.ht/~sircmpwn/aerc2/worker/types"
+
+	"fmt"
+)
+
+type IMAPWorker struct {
+	messages chan types.WorkerMessage
+	actions  chan types.WorkerMessage
+}
+
+func NewIMAPWorker() *IMAPWorker {
+	return &IMAPWorker{
+		messages: make(chan types.WorkerMessage, 50),
+		actions:  make(chan types.WorkerMessage, 50),
+	}
+}
+
+func (w *IMAPWorker) GetMessage() types.WorkerMessage {
+	select {
+	case msg := <-w.messages:
+		return msg
+	default:
+		return nil
+	}
+}
+
+func (w *IMAPWorker) PostAction(msg types.WorkerMessage) {
+	w.actions <- msg
+}
+
+func (w *IMAPWorker) handleMessage(_msg types.WorkerMessage) {
+	switch msg := _msg.(type) {
+	case types.Ping:
+		w.messages <- &types.Ack{
+			Message: types.RespondTo(msg),
+		}
+	default:
+		w.messages <- &types.Unsupported{
+			Message: types.RespondTo(_msg),
+		}
+	}
+}
+
+func (w *IMAPWorker) Run() {
+	// TODO: IMAP shit
+	for {
+		select {
+		case msg := <-w.actions:
+			fmt.Printf("<= %T: %+v\n", msg, msg)
+			w.handleMessage(msg)
+		default:
+			// no-op
+		}
+	}
+}
diff --git a/worker/types/messages.go b/worker/types/messages.go
new file mode 100644
index 0000000..845bb86
--- /dev/null
+++ b/worker/types/messages.go
@@ -0,0 +1,55 @@
+package types
+
+import (
+	"git.sr.ht/~sircmpwn/aerc2/config"
+)
+
+type WorkerMessage interface {
+	InResponseTo() WorkerMessage
+}
+
+type Message struct {
+	inResponseTo WorkerMessage
+}
+
+// Meta-messages
+type Ack struct {
+	Message
+}
+
+type Error struct {
+	Message
+	Error error
+}
+
+type Unsupported struct {
+	Message
+}
+
+// Commands
+type Ping struct {
+	Message
+}
+
+type Configure struct {
+	Message
+	Config config.AccountConfig
+}
+
+type Connect struct {
+	Message
+}
+
+type Disconnect struct {
+	Message
+}
+
+func RespondTo(msg WorkerMessage) Message {
+	return Message{
+		inResponseTo: msg,
+	}
+}
+
+func (m Message) InResponseTo() WorkerMessage {
+	return m.inResponseTo
+}
diff --git a/worker/worker.go b/worker/worker.go
new file mode 100644
index 0000000..1f9f04c
--- /dev/null
+++ b/worker/worker.go
@@ -0,0 +1,18 @@
+package worker
+
+import (
+	"git.sr.ht/~sircmpwn/aerc2/worker/imap"
+	"git.sr.ht/~sircmpwn/aerc2/worker/types"
+)
+
+type Worker interface {
+	GetMessage() types.WorkerMessage
+	PostAction(types.WorkerMessage)
+	Run()
+}
+
+// Guesses the appropriate worker type based on the given source string
+func NewWorker(source string) Worker {
+	// TODO: Do this properly
+	return imap.NewIMAPWorker()
+}