aboutsummaryrefslogtreecommitdiffstats
path: root/worker/imap/worker.go
blob: 7525da542b5d935aaa2f603b805f9275379e403a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package imap

import (
	"git.sr.ht/~sircmpwn/aerc2/worker/types"

	"fmt"
	"time"
)

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\n", msg)
			w.handleMessage(msg)
		default:
			time.Sleep(100 * time.Millisecond)
		}
	}
}