/*
-SGBlog -- Git-backed CGI/inetd blogging/phlogging engine
-Copyright (C) 2020 Sergey Matveev <stargrave@stargrave.org>
+SGBlog -- Git-backed CGI/UCSPI blogging/phlogging/gemlogging engine
+Copyright (C) 2020-2023 Sergey Matveev <stargrave@stargrave.org>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-// Git-backed CGI/inetd blogging/phlogging engine
+// Git-backed CGI/UCSPI blogging/phlogging/gemlogging engine
package main
import (
"encoding/hex"
"flag"
"fmt"
- "io/ioutil"
+ "io"
"log"
"mime"
"net/mail"
"syscall"
"time"
- "go.cypherpunks.ru/netstring/v2"
+ "go.cypherpunks.ru/recfile"
"go.stargrave.org/sgblog"
)
// Remove various whitespaces and excess lines, because git-notes-add
// will remove and we have to know exact bytes count
-func cleanupBody(body string) string {
+func cleanupBody(body string) []string {
lines := strings.Split(string(body), "\n")
for i, line := range lines {
line = strings.ReplaceAll(line, " ", " ")
}
withoutDups = append(withoutDups, line)
}
- return strings.Join(withoutDups, "\n")
+ return withoutDups
}
func main() {
}
syscall.Umask(int(umaskInt))
- msg, err := mail.ReadMessage(os.Stdin)
+ data, err := io.ReadAll(os.Stdin)
+ if err != nil {
+ log.Fatal(err)
+ }
+ if bytes.HasPrefix(data, []byte("From ")) {
+ data = data[bytes.Index(data, []byte("\n"))+1:]
+ }
+ msg, err := mail.ReadMessage(bytes.NewReader(data))
if err != nil {
log.Fatal(err)
}
if err != nil {
log.Fatal(err)
}
- body, err := ioutil.ReadAll(r)
+ body, err := io.ReadAll(r)
if err != nil {
log.Fatal(err)
}
note = bytes.TrimRight(note, "\r\n")
buf := bytes.NewBuffer(note)
- w := netstring.NewWriter(buf)
- w.WriteChunk([]byte(fmt.Sprintf(
- "From: %s\nDate: %s\n\n%s",
- from,
- time.Now().UTC().Format(sgblog.WhenFmt),
- cleanupBody(string(body)),
- )))
+ recfileW := recfile.NewWriter(buf)
+ if _, err = recfileW.RecordStart(); err != nil {
+ log.Fatal(err)
+ }
+ // We trimmed newline, so have to start record twice
+ if _, err = recfileW.RecordStart(); err != nil {
+ log.Fatal(err)
+ }
+ if _, err = recfileW.WriteFields(
+ recfile.Field{Name: "From", Value: from},
+ recfile.Field{Name: "Date", Value: time.Now().UTC().Format(sgblog.WhenFmt)},
+ ); err != nil {
+ log.Fatal(err)
+ }
+ if _, err = recfileW.WriteFieldMultiline(
+ "Body", append([]string{""}, cleanupBody(string(body))...),
+ ); err != nil {
+ log.Fatal(err)
+ }
if *dryRun {
fmt.Print(buf.String())