]> 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 49c8d136476b31cceb7cb45784c690e3c8857e23..5d6130aafc74ae4b1f92d0e3115ebecd0002ce63 100644 (file)
@@ -1,6 +1,6 @@
 /*
-SGBlog -- Git-backed CGI/inetd blogging/phlogging engine
-Copyright (C) 2020-2021 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"
@@ -95,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)
        }
@@ -103,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)
        }
@@ -152,8 +159,8 @@ func main() {
                log.Fatal(err)
        }
        if _, err = recfileW.WriteFields(
-               recfile.Field{"From", from},
-               recfile.Field{"Date", time.Now().UTC().Format(sgblog.WhenFmt)},
+               recfile.Field{Name: "From", Value: from},
+               recfile.Field{Name: "Date", Value: time.Now().UTC().Format(sgblog.WhenFmt)},
        ); err != nil {
                log.Fatal(err)
        }