github.com/google/syzkaller@v0.0.0-20240517125934-c0f1611a36d6/pkg/email/reply.go (about)

     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 *Email, reply string) string {
    13  	s := bufio.NewScanner(strings.NewReader(email.Body))
    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 len(email.Commands) > 1 {
    25  			// If there are several commands, we cannot precisely attribute replies.
    26  			// TODO: that's possible, but such a refactoring does not seem to be worth it
    27  			// at the time.
    28  			continue
    29  		} else if !replied && bytes.HasPrefix(ln, []byte(commandPrefix)) {
    30  			replied = true
    31  			writeReply(out, reply)
    32  		}
    33  	}
    34  	if !replied {
    35  		writeReply(out, reply)
    36  	}
    37  	return out.String()
    38  }
    39  
    40  func writeReply(out *bytes.Buffer, reply string) {
    41  	out.WriteByte('\n')
    42  	out.WriteString(reply)
    43  	if reply != "" && reply[len(reply)-1] != '\n' {
    44  		out.WriteByte('\n')
    45  	}
    46  	out.WriteByte('\n')
    47  }