Home | History | Annotate | Download | only in email
      1 // Copyright 2017 syzkaller project authors. All rights reserved.
      2 // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
      3 
      4 package email
      5 
      6 import (
      7 	"bufio"
      8 	"bytes"
      9 	"strings"
     10 )
     11 
     12 func FormReply(email, reply string) string {
     13 	s := bufio.NewScanner(strings.NewReader(email))
     14 	out := new(bytes.Buffer)
     15 	replied := false
     16 	for s.Scan() {
     17 		ln := s.Bytes()
     18 		out.WriteByte('>')
     19 		if len(ln) != 0 && ln[0] != '>' {
     20 			out.WriteByte(' ')
     21 		}
     22 		out.Write(ln)
     23 		out.WriteByte('\n')
     24 		if !replied && bytes.HasPrefix(ln, []byte(commandPrefix)) {
     25 			replied = true
     26 			writeReply(out, reply)
     27 		}
     28 	}
     29 	if !replied {
     30 		writeReply(out, reply)
     31 	}
     32 	return out.String()
     33 }
     34 
     35 func writeReply(out *bytes.Buffer, reply string) {
     36 	out.WriteByte('\n')
     37 	out.WriteString(reply)
     38 	if reply != "" && reply[len(reply)-1] != '\n' {
     39 		out.WriteByte('\n')
     40 	}
     41 	out.WriteByte('\n')
     42 }
     43