summary refs log tree commit diff stats
path: root/worker
diff options
context:
space:
mode:
authorDrew DeVault <sir@cmpwn.com>2019-03-29 22:35:53 -0400
committerDrew DeVault <sir@cmpwn.com>2019-03-29 22:36:15 -0400
commit77ede6eb5a22a5407541ac587736189fcca0037f (patch)
tree45627c15f7087f91ed9881e776a566d440bd878e /worker
parent84e9853c1613f43f76bd46f0c1eb143d1db16ac2 (diff)
downloadaerc-77ede6eb5a22a5407541ac587736189fcca0037f.tar.gz
Add body fetching support code
Diffstat (limited to 'worker')
-rw-r--r--worker/imap/fetch.go61
-rw-r--r--worker/imap/worker.go2
-rw-r--r--worker/types/messages.go9
3 files changed, 55 insertions, 17 deletions
diff --git a/worker/imap/fetch.go b/worker/imap/fetch.go
index 489dbe4..884ab73 100644
--- a/worker/imap/fetch.go
+++ b/worker/imap/fetch.go
@@ -2,6 +2,7 @@ package imap
 
 import (
 	"github.com/emersion/go-imap"
+	"github.com/mohamedattahri/mail"
 
 	"git.sr.ht/~sircmpwn/aerc2/worker/types"
 )
@@ -10,28 +11,58 @@ func (imapw *IMAPWorker) handleFetchMessageHeaders(
 	msg *types.FetchMessageHeaders) {
 
 	imapw.worker.Logger.Printf("Fetching message headers")
+	items := []imap.FetchItem{
+		imap.FetchEnvelope,
+		imap.FetchInternalDate,
+		imap.FetchFlags,
+		imap.FetchUid,
+	}
+
+	imapw.handleFetchMessages(msg, &msg.Uids, items)
+}
+
+func (imapw *IMAPWorker) handleFetchMessageBodies(
+	msg *types.FetchMessageBodies) {
+
+	imapw.worker.Logger.Printf("Fetching message bodies")
+	section := &imap.BodySectionName{}
+	items := []imap.FetchItem{section.FetchItem()}
+	imapw.handleFetchMessages(msg, &msg.Uids, items)
+}
+
+func (imapw *IMAPWorker) handleFetchMessages(
+	msg types.WorkerMessage, uids *imap.SeqSet, items []imap.FetchItem) {
 
 	go func() {
 		messages := make(chan *imap.Message)
 		done := make(chan error, 1)
-		items := []imap.FetchItem{
-			imap.FetchEnvelope,
-			imap.FetchInternalDate,
-			imap.FetchFlags,
-			imap.FetchUid,
-		}
 		go func() {
-			done <- imapw.client.UidFetch(&msg.Uids, items, messages)
+			done <- imapw.client.UidFetch(uids, items, messages)
 		}()
 		go func() {
-			for msg := range messages {
-				imapw.seqMap[msg.SeqNum-1] = msg.Uid
-				imapw.worker.PostMessage(&types.MessageInfo{
-					Envelope:     msg.Envelope,
-					Flags:        msg.Flags,
-					InternalDate: msg.InternalDate,
-					Uid:          msg.Uid,
-				}, nil)
+			section := &imap.BodySectionName{}
+			for _msg := range messages {
+				imapw.seqMap[_msg.SeqNum-1] = _msg.Uid
+				if reader := _msg.GetBody(section); reader != nil {
+					email, err := mail.ReadMessage(reader)
+					if err != nil {
+						imapw.worker.PostMessage(&types.Error{
+							Message: types.RespondTo(msg),
+							Error:   err,
+						}, nil)
+					}
+					imapw.worker.PostMessage(&types.MessageBody{
+						Mail: email,
+						Uid:  _msg.Uid,
+					}, nil)
+				} else {
+					imapw.worker.PostMessage(&types.MessageInfo{
+						Envelope:     _msg.Envelope,
+						Flags:        _msg.Flags,
+						InternalDate: _msg.InternalDate,
+						Uid:          _msg.Uid,
+					}, nil)
+				}
 			}
 			if err := <-done; err != nil {
 				imapw.worker.PostMessage(&types.Error{
diff --git a/worker/imap/worker.go b/worker/imap/worker.go
index ea7f317..2f98595 100644
--- a/worker/imap/worker.go
+++ b/worker/imap/worker.go
@@ -158,6 +158,8 @@ func (w *IMAPWorker) handleMessage(msg types.WorkerMessage) error {
 		w.handleFetchDirectoryContents(msg)
 	case *types.FetchMessageHeaders:
 		w.handleFetchMessageHeaders(msg)
+	case *types.FetchMessageBodies:
+		w.handleFetchMessageBodies(msg)
 	case *types.DeleteMessages:
 		w.handleDeleteMessages(msg)
 	default:
diff --git a/worker/types/messages.go b/worker/types/messages.go
index ff2c36b..f38bb22 100644
--- a/worker/types/messages.go
+++ b/worker/types/messages.go
@@ -2,10 +2,10 @@ package types
 
 import (
 	"crypto/x509"
-	"net/mail"
 	"time"
 
 	"github.com/emersion/go-imap"
+	"github.com/mohamedattahri/mail"
 
 	"git.sr.ht/~sircmpwn/aerc2/config"
 )
@@ -123,11 +123,16 @@ type MessageInfo struct {
 	Envelope     *imap.Envelope
 	Flags        []string
 	InternalDate time.Time
-	Mail         *mail.Message
 	Size         uint32
 	Uid          uint32
 }
 
+type MessageBody struct {
+	Message
+	Mail *mail.Message
+	Uid  uint32
+}
+
 type MessagesDeleted struct {
 	Message
 	Uids []uint32
itle='Blame the previous revision' href='/akspecs/aerc/blame/widgets/account.go?h=0.1.2&id=b76deea9635d56bfa740127683e43bf817f5e3be'>^
589db74 ^



c286d3d ^
b76deea ^
0911cd5 ^
24dfc47 ^
589db74 ^
2349b7d ^
fcdcd32 ^
0f8b7a1 ^

24dfc47 ^
589db74 ^








0911cd5 ^






24dfc47 ^
0911cd5 ^


24dfc47 ^

589db74 ^
0911cd5 ^
24dfc47 ^
0911cd5 ^

17bd2dc ^



475b697 ^







62946ff ^
24dfc47 ^
62946ff ^

607ece8 ^
1228448 ^


648ca98 ^
b76deea ^


648ca98 ^








c286d3d ^
589db74 ^

c286d3d ^
2349b7d ^



589db74 ^
cf66462 ^



77c76ba ^
cf66462 ^








589db74 ^
cf66462 ^
2349b7d ^

2349b7d ^



2349b7d ^

b60999c ^




24daef8 ^



b60999c ^
b389647 ^



4ec7f5d ^








b389647 ^




026e8a1 ^





b389647 ^



0abafa6 ^
77ede6e ^

b389647 ^


312a53e ^


b389647 ^

589db74 ^
2958579 ^
b389647 ^
b60999c ^
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201