summary refs log tree commit diff stats
path: root/worker
diff options
context:
space:
mode:
authorDrew DeVault <sir@cmpwn.com>2019-03-20 23:23:38 -0400
committerDrew DeVault <sir@cmpwn.com>2019-03-20 23:23:38 -0400
commit312a53e5ff721e0a29e34aaeceb0eece1203002d (patch)
treefb0c5f30d89f904d5fad4a900c910a1dd1d891be /worker
parentf3d3e0ed4f5dbf36b7a7c9762e6297083843f127 (diff)
downloadaerc-312a53e5ff721e0a29e34aaeceb0eece1203002d.tar.gz
Implement :delete-message
Diffstat (limited to 'worker')
-rw-r--r--worker/imap/fetch.go1
-rw-r--r--worker/imap/flags.go43
-rw-r--r--worker/imap/open.go1
-rw-r--r--worker/imap/worker.go6
-rw-r--r--worker/types/messages.go10
5 files changed, 60 insertions, 1 deletions
diff --git a/worker/imap/fetch.go b/worker/imap/fetch.go
index 383a8a8..489dbe4 100644
--- a/worker/imap/fetch.go
+++ b/worker/imap/fetch.go
@@ -25,6 +25,7 @@ func (imapw *IMAPWorker) handleFetchMessageHeaders(
 		}()
 		go func() {
 			for msg := range messages {
+				imapw.seqMap[msg.SeqNum-1] = msg.Uid
 				imapw.worker.PostMessage(&types.MessageInfo{
 					Envelope:     msg.Envelope,
 					Flags:        msg.Flags,
diff --git a/worker/imap/flags.go b/worker/imap/flags.go
new file mode 100644
index 0000000..cb9b3b1
--- /dev/null
+++ b/worker/imap/flags.go
@@ -0,0 +1,43 @@
+package imap
+
+import (
+	"github.com/emersion/go-imap"
+
+	"git.sr.ht/~sircmpwn/aerc2/worker/types"
+)
+
+func (imapw *IMAPWorker) handleDeleteMessages(msg *types.DeleteMessages) {
+	item := imap.FormatFlagsOp(imap.AddFlags, true)
+	flags := []interface{}{imap.DeletedFlag}
+	if err := imapw.client.UidStore(&msg.Uids, item, flags, nil); err != nil {
+		imapw.worker.PostMessage(&types.Error{
+			Message: types.RespondTo(msg),
+			Error:   err,
+		}, nil)
+		return
+	}
+	var deleted []uint32
+	ch := make(chan uint32)
+	done := make(chan interface{})
+	go func() {
+		for seqNum := range ch {
+			i := seqNum - 1
+			deleted = append(deleted, imapw.seqMap[i])
+			imapw.seqMap = append(imapw.seqMap[:i], imapw.seqMap[i+1:]...)
+		}
+		done <- nil
+	}()
+	if err := imapw.client.Expunge(ch); err != nil {
+		imapw.worker.PostMessage(&types.Error{
+			Message: types.RespondTo(msg),
+			Error:   err,
+		}, nil)
+	} else {
+		<-done
+		imapw.worker.PostMessage(&types.MessagesDeleted{
+			Message: types.RespondTo(msg),
+			Uids:    deleted,
+		}, nil)
+		imapw.worker.PostMessage(&types.Done{types.RespondTo(msg)}, nil)
+	}
+}
diff --git a/worker/imap/open.go b/worker/imap/open.go
index 87c4fb3..3705bc0 100644
--- a/worker/imap/open.go
+++ b/worker/imap/open.go
@@ -39,6 +39,7 @@ func (imapw *IMAPWorker) handleFetchDirectoryContents(
 			}, nil)
 		} else {
 			imapw.worker.Logger.Printf("Found %d UIDs", len(uids))
+			imapw.seqMap = make([]uint32, len(uids))
 			imapw.worker.PostMessage(&types.DirectoryContents{
 				Message: types.RespondTo(msg),
 				Uids:    uids,
diff --git a/worker/imap/worker.go b/worker/imap/worker.go
index 1646165..ea7f317 100644
--- a/worker/imap/worker.go
+++ b/worker/imap/worker.go
@@ -33,12 +33,14 @@ type IMAPWorker struct {
 	selected imap.MailboxStatus
 	updates  chan client.Update
 	worker   *types.Worker
+	// Map of sequence numbers to UIDs, index 0 is seq number 1
+	seqMap []uint32
 }
 
 func NewIMAPWorker(worker *types.Worker) *IMAPWorker {
 	return &IMAPWorker{
-		worker:  worker,
 		updates: make(chan client.Update, 50),
+		worker:  worker,
 	}
 }
 
@@ -156,6 +158,8 @@ func (w *IMAPWorker) handleMessage(msg types.WorkerMessage) error {
 		w.handleFetchDirectoryContents(msg)
 	case *types.FetchMessageHeaders:
 		w.handleFetchMessageHeaders(msg)
+	case *types.DeleteMessages:
+		w.handleDeleteMessages(msg)
 	default:
 		return errUnsupported
 	}
diff --git a/worker/types/messages.go b/worker/types/messages.go
index 3f1a39f..ff2c36b 100644
--- a/worker/types/messages.go
+++ b/worker/types/messages.go
@@ -86,6 +86,11 @@ type FetchMessageBodies struct {
 	Uids imap.SeqSet
 }
 
+type DeleteMessages struct {
+	Message
+	Uids imap.SeqSet
+}
+
 // Messages
 
 type CertificateApprovalRequest struct {
@@ -122,3 +127,8 @@ type MessageInfo struct {
 	Size         uint32
 	Uid          uint32
 }
+
+type MessagesDeleted struct {
+	Message
+	Uids []uint32
+}
0fdf7dbca'>d3d9df2 ^
b9d16fb ^
d4c05d2 ^

b9d16fb ^
d4c05d2 ^


d6fbc25 ^
d4c05d2 ^
b9d16fb ^
d3d9df2 ^

b9d16fb ^
d4c05d2 ^






d6fbc25 ^
d4c05d2 ^
b9d16fb ^

d3d9df2 ^
d2680fb ^








d6fbc25 ^
d2680fb ^



d3d9df2 ^
b9d16fb ^
d4c05d2 ^





d6fbc25 ^
d4c05d2 ^
b9d16fb ^

d3d9df2 ^

b9d16fb ^
d4c05d2 ^




126c00e ^
d4c05d2 ^
d6fbc25 ^
d4c05d2 ^
b9d16fb ^

d6fbc25 ^
































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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159