summary refs log tree commit diff stats
path: root/widgets/msgviewer.go
diff options
context:
space:
mode:
authorYash Srivastav <yash111998@gmail.com>2019-06-07 13:56:14 +0530
committerDrew DeVault <sir@cmpwn.com>2019-06-07 09:20:24 -0400
commitb83e7c9fa6a0d187a0f20d98d522cff792053cdd (patch)
tree4c3765b41d216e26406d21d3b045f340edabbadc /widgets/msgviewer.go
parent2279ac3ab3d19cd233600907bc8e3e8b4e57b350 (diff)
downloadaerc-b83e7c9fa6a0d187a0f20d98d522cff792053cdd.tar.gz
implements ability to view headers in message view
Diffstat (limited to 'widgets/msgviewer.go')
-rw-r--r--widgets/msgviewer.go144
1 files changed, 91 insertions, 53 deletions
diff --git a/widgets/msgviewer.go b/widgets/msgviewer.go
index 45a5ed0..b82fa8d 100644
--- a/widgets/msgviewer.go
+++ b/widgets/msgviewer.go
@@ -35,8 +35,9 @@ type MessageViewer struct {
 
 type PartSwitcher struct {
 	ui.Invalidatable
-	parts    []*PartViewer
-	selected int
+	parts       []*PartViewer
+	selected    int
+	showHeaders bool
 }
 
 func formatAddresses(addrs []*imap.Address) string {
@@ -98,33 +99,10 @@ func NewMessageViewer(acct *AccountView, conf *config.AercConfig,
 		}).At(2, 0).Span(1, 2)
 	headers.AddChild(ui.NewFill(' ')).At(3, 0).Span(1, 2)
 
-	var err error
 	switcher := &PartSwitcher{}
-	if len(msg.BodyStructure.Parts) == 0 {
-		pv, err := NewPartViewer(conf, store, msg, msg.BodyStructure, []int{1})
-		if err != nil {
-			goto handle_error
-		}
-		switcher.parts = []*PartViewer{pv}
-		pv.OnInvalidate(func(_ ui.Drawable) {
-			switcher.Invalidate()
-		})
-	} else {
-		switcher.parts, err = enumerateParts(conf, store,
-			msg, msg.BodyStructure, []int{})
-		if err != nil {
-			goto handle_error
-		}
-		switcher.selected = -1
-		for i, pv := range switcher.parts {
-			pv.OnInvalidate(func(_ ui.Drawable) {
-				switcher.Invalidate()
-			})
-			// TODO: switch to user's preferred mimetype, if configured
-			if switcher.selected == -1 && pv.part.MIMEType != "multipart" {
-				switcher.selected = i
-			}
-		}
+	err := createSwitcher(switcher, conf, store, msg, conf.Viewer.ShowHeaders)
+	if err != nil {
+		goto handle_error
 	}
 
 	grid.AddChild(headers).At(0, 0)
@@ -132,6 +110,7 @@ func NewMessageViewer(acct *AccountView, conf *config.AercConfig,
 
 	return &MessageViewer{
 		acct:     acct,
+		conf:     conf,
 		grid:     grid,
 		msg:      msg,
 		store:    store,
@@ -148,7 +127,7 @@ handle_error:
 
 func enumerateParts(conf *config.AercConfig, store *lib.MessageStore,
 	msg *types.MessageInfo, body *imap.BodyStructure,
-	index []int) ([]*PartViewer, error) {
+	showHeaders bool, index []int) ([]*PartViewer, error) {
 
 	var parts []*PartViewer
 	for i, part := range body.Parts {
@@ -158,14 +137,14 @@ func enumerateParts(conf *config.AercConfig, store *lib.MessageStore,
 			pv := &PartViewer{part: part}
 			parts = append(parts, pv)
 			subParts, err := enumerateParts(
-				conf, store, msg, part, curindex)
+				conf, store, msg, part, showHeaders, curindex)
 			if err != nil {
 				return nil, err
 			}
 			parts = append(parts, subParts...)
 			continue
 		}
-		pv, err := NewPartViewer(conf, store, msg, part, curindex)
+		pv, err := NewPartViewer(conf, store, msg, part, showHeaders, curindex)
 		if err != nil {
 			return nil, err
 		}
@@ -174,6 +153,44 @@ func enumerateParts(conf *config.AercConfig, store *lib.MessageStore,
 	return parts, nil
 }
 
+func createSwitcher(switcher *PartSwitcher, conf *config.AercConfig,
+	store *lib.MessageStore, msg *types.MessageInfo, showHeaders bool) error {
+	var err error
+	switcher.showHeaders = showHeaders
+
+	if showHeaders {
+	}
+
+	if len(msg.BodyStructure.Parts) == 0 {
+		pv, err := NewPartViewer(conf, store, msg, msg.BodyStructure,
+			showHeaders, []int{1})
+		if err != nil {
+			return err
+		}
+		switcher.parts = []*PartViewer{pv}
+		pv.OnInvalidate(func(_ ui.Drawable) {
+			switcher.Invalidate()
+		})
+	} else {
+		switcher.parts, err = enumerateParts(conf, store,
+			msg, msg.BodyStructure, showHeaders, []int{})
+		if err != nil {
+			return err
+		}
+		switcher.selected = -1
+		for i, pv := range switcher.parts {
+			pv.OnInvalidate(func(_ ui.Drawable) {
+				switcher.Invalidate()
+			})
+			// TODO: switch to user's preferred mimetype, if configured
+			if switcher.selected == -1 && pv.part.MIMEType != "multipart" {
+				switcher.selected = i
+			}
+		}
+	}
+	return nil
+}
+
 func (mv *MessageViewer) Draw(ctx *ui.Context) {
 	if mv.err != nil {
 		ctx.Fill(0, 0, ctx.Width(), ctx.Height(), ' ', tcell.StyleDefault)
@@ -205,6 +222,15 @@ func (mv *MessageViewer) SelectedMessage() *types.MessageInfo {
 	return mv.msg
 }
 
+func (mv *MessageViewer) ToggleHeaders() {
+	switcher := mv.switcher
+	err := createSwitcher(switcher, mv.conf, mv.store, mv.msg, !switcher.showHeaders)
+	if err != nil {
+		mv.acct.Logger().Printf("warning: error during create switcher - %v", err)
+	}
+	switcher.Invalidate()
+}
+
 func (mv *MessageViewer) CurrentPart() *PartInfo {
 	switcher := mv.switcher
 	part := switcher.parts[switcher.selected]
@@ -295,18 +321,19 @@ func (mv *MessageViewer) Focus(focus bool) {
 
 type PartViewer struct {
 	ui.Invalidatable
-	err     error
-	fetched bool
-	filter  *exec.Cmd
-	index   []int
-	msg     *types.MessageInfo
-	pager   *exec.Cmd
-	pagerin io.WriteCloser
-	part    *imap.BodyStructure
-	sink    io.WriteCloser
-	source  io.Reader
-	store   *lib.MessageStore
-	term    *Terminal
+	err         error
+	fetched     bool
+	filter      *exec.Cmd
+	index       []int
+	msg         *types.MessageInfo
+	pager       *exec.Cmd
+	pagerin     io.WriteCloser
+	part        *imap.BodyStructure
+	showHeaders bool
+	sink        io.WriteCloser
+	source      io.Reader
+	store       *lib.MessageStore
+	term        *Terminal
 }
 
 type PartInfo struct {
@@ -318,7 +345,8 @@ type PartInfo struct {
 
 func NewPartViewer(conf *config.AercConfig,
 	store *lib.MessageStore, msg *types.MessageInfo,
-	part *imap.BodyStructure, index []int) (*PartViewer, error) {
+	part *imap.BodyStructure, showHeaders bool,
+	index []int) (*PartViewer, error) {
 
 	var (
 		filter  *exec.Cmd
@@ -375,15 +403,16 @@ func NewPartViewer(conf *config.AercConfig,
 	}
 
 	pv := &PartViewer{
-		filter:  filter,
-		index:   index,
-		msg:     msg,
-		pager:   pager,
-		pagerin: pagerin,
-		part:    part,
-		sink:    pipe,
-		store:   store,
-		term:    term,
+		filter:      filter,
+		index:       index,
+		msg:         msg,
+		pager:       pager,
+		pagerin:     pagerin,
+		part:        part,
+		showHeaders: showHeaders,
+		sink:        pipe,
+		store:       store,
+		term:        term,
 	}
 
 	if term != nil {
@@ -439,6 +468,15 @@ func (pv *PartViewer) attemptCopy() {
 			}()
 		}
 		go func() {
+			if pv.showHeaders && pv.msg.RFC822Headers != nil {
+				fields := pv.msg.RFC822Headers.Fields()
+				for fields.Next() {
+					field := fmt.Sprintf("%s: %s\n", fields.Key(), fields.Value())
+					pv.sink.Write([]byte(field))
+				}
+				pv.sink.Write([]byte{'\n'})
+			}
+
 			entity, err := message.New(header, pv.source)
 			if err != nil {
 				pv.err = err
2019-05-12 00:38:48 -0400 Expand compose focus handling a bit' href='/akspecs/aerc/commit/widgets/compose.go?h=0.2.1&id=2a4dd5cb87179d8ffc00bad2552880f9bd50b75f'>2a4dd5c ^
17bd2dc ^

b0bf09b ^
2a4dd5c ^
17bd2dc ^

577248f ^
6c36e04 ^


bb46b2b ^



577248f ^

2b3e123 ^

















8be59ca ^




5701b6e ^
8be59ca ^



475b697 ^






2c486cb ^





577248f ^













29de329 ^












f77d7c2 ^









577248f ^
2a4dd5c ^
577248f ^


2a4dd5c ^
577248f ^

6c36e04 ^



b0bf09b ^



2b3e123 ^
6c36e04 ^




6c36e04 ^



6c36e04 ^


6c36e04 ^

6c36e04 ^

475b697 ^
6c36e04 ^













29de329 ^
6c36e04 ^


29de329 ^
6c36e04 ^








475b697 ^
2b3e123 ^


475b697 ^
2b3e123 ^


29de329 ^



















6c36e04 ^
29de329 ^
6c36e04 ^
29de329 ^
6c36e04 ^
29de329 ^


6c36e04 ^

bb46b2b ^




29de329 ^
bb46b2b ^

f37508a ^














bb46b2b ^




577248f ^
577248f ^














2a4dd5c ^














577248f ^
bb46b2b ^
2c486cb ^





bb46b2b ^






































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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392