aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/email/reply.go
blob: 28229ba62449cd09a42aaead5bbf826486262fc8 (plain)
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
// Copyright 2017 syzkaller project authors. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.

package email

import (
	"bufio"
	"bytes"
	"strings"
)

func FormReply(email *Email, reply string) string {
	s := bufio.NewScanner(strings.NewReader(email.Body))
	out := new(bytes.Buffer)
	replied := false
	for s.Scan() {
		ln := s.Bytes()
		out.WriteByte('>')
		if len(ln) != 0 && ln[0] != '>' {
			out.WriteByte(' ')
		}
		out.Write(ln)
		out.WriteByte('\n')
		if len(email.Commands) > 1 {
			// If there are several commands, we cannot precisely attribute replies.
			// TODO: that's possible, but such a refactoring does not seem to be worth it
			// at the time.
			continue
		} else if !replied && bytes.HasPrefix(ln, []byte(commandPrefix)) {
			replied = true
			writeReply(out, reply)
		}
	}
	if !replied {
		writeReply(out, reply)
	}
	return out.String()
}

func writeReply(out *bytes.Buffer, reply string) {
	out.WriteByte('\n')
	out.WriteString(reply)
	if reply != "" && reply[len(reply)-1] != '\n' {
		out.WriteByte('\n')
	}
	out.WriteByte('\n')
}