]> Sergey Matveev's repositories - sgblog.git/blobdiff - cmd/sgblog-comment-add/main.go
Strip "From " from mail message
[sgblog.git] / cmd / sgblog-comment-add / main.go
index 2b977ae7feb14c0c886851f8871ca6e1d982b32c..5d6130aafc74ae4b1f92d0e3115ebecd0002ce63 100644 (file)
@@ -1,6 +1,6 @@
 /*
-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
@@ -15,7 +15,7 @@ You should have received a copy of the GNU Affero General Public License
 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 (
@@ -24,7 +24,7 @@ import (
        "encoding/hex"
        "flag"
        "fmt"
-       "io/ioutil"
+       "io"
        "log"
        "mime"
        "net/mail"
@@ -36,6 +36,7 @@ import (
        "syscall"
        "time"
 
+       "go.cypherpunks.ru/recfile"
        "go.stargrave.org/sgblog"
 )
 
@@ -94,7 +95,14 @@ 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)
        }
@@ -102,7 +110,7 @@ func main() {
        if err != nil {
                log.Fatal(err)
        }
-       body, err := ioutil.ReadAll(r)
+       body, err := io.ReadAll(r)
        if err != nil {
                log.Fatal(err)
        }
@@ -142,15 +150,24 @@ func main() {
        note = bytes.TrimRight(note, "\r\n")
 
        buf := bytes.NewBuffer(note)
-       buf.WriteString(fmt.Sprintf(
-               "\n\nFrom: %s\nDate: %s\nBody:\n",
-               from,
-               time.Now().UTC().Format(sgblog.WhenFmt),
-       ))
-       for _, s := range cleanupBody(string(body)) {
-               buf.WriteString("+ ")
-               buf.WriteString(s)
-               buf.WriteString("\n")
+       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 {