email/cmd/addhashheader/addhashheader.go

47 lines
867 B
Go

package main
import (
"bytes"
"flag"
"fmt"
"io"
"net/mail"
"os"
"github.com/golang/glog"
"xinu.tv/email"
)
func main() {
flag.Parse()
defer glog.Flush()
hdrBuf := new(bytes.Buffer)
msg, err := mail.ReadMessage(io.TeeReader(os.Stdin, hdrBuf))
if err != nil {
glog.Fatal(err)
}
h, err := email.HashMessage(msg)
if err != nil {
glog.Fatal(err)
}
// Find end of header in original byte stream.
n := bytes.Index(hdrBuf.Bytes(), []byte("\n\n"))
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
}
// 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)
}