Use original header bytes instead of printing parsed forms.

This commit is contained in:
Bill Thiede 2014-03-30 10:49:14 -07:00
parent b79c894575
commit 646ec2864f

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"bytes"
"flag" "flag"
"fmt" "fmt"
"io" "io"
@ -16,7 +17,8 @@ func main() {
flag.Parse() flag.Parse()
defer glog.Flush() defer glog.Flush()
msg, err := mail.ReadMessage(os.Stdin) hdrBuf := new(bytes.Buffer)
msg, err := mail.ReadMessage(io.TeeReader(os.Stdin, hdrBuf))
if err != nil { if err != nil {
glog.Fatal(err) glog.Fatal(err)
} }
@ -25,12 +27,20 @@ func main() {
if err != nil { if err != nil {
glog.Fatal(err) glog.Fatal(err)
} }
for k, vs := range msg.Header { // Find end of header in original byte stream.
for _, v := range vs { n := bytes.Index(hdrBuf.Bytes(), []byte("\n\n"))
fmt.Printf("%s: %s\n", k, v) if n == -1 {
} glog.Error("Couldn't find end of headers in hdrBuf")
// Copy the original message untouched.
io.Copy(os.Stdout, hdrBuf)
io.Copy(os.Stdout, os.Stdin)
return
} }
fmt.Println("X-Xinu-Hash:", h)
fmt.Println() // Copy the headers out untouched.
io.CopyN(os.Stdout, hdrBuf, int64(n))
// Insert our new header.
fmt.Printf("\nX-Xinu-Hash: %s\n\n", h)
// Copy the message body out untouched.
io.Copy(os.Stdout, msg.Body) io.Copy(os.Stdout, msg.Body)
} }