]> Sergey Matveev's repositories - sgblog.git/blob - cmd/sgblog-comment-add/main.go
Comments and refactoring
[sgblog.git] / cmd / sgblog-comment-add / main.go
1 /*
2 SGBlog -- Git-based CGI blogging engine
3 Copyright (C) 2020 Sergey Matveev <stargrave@stargrave.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Affero General Public License as
7 published by the Free Software Foundation, version 3 of the License.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU Affero General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 // Git-based CGI blogging engine email to comments adder
19 package main
20
21 import (
22         "bytes"
23         "crypto/sha1"
24         "encoding/hex"
25         "flag"
26         "fmt"
27         "io/ioutil"
28         "log"
29         "net/mail"
30         "os"
31         "os/exec"
32         "strconv"
33         "strings"
34         "syscall"
35         "time"
36
37         "go.cypherpunks.ru/netstring/v2"
38 )
39
40 const WhenFmt = "2006-01-02 15:04:05Z07:00"
41
42 func main() {
43         gitCmd := flag.String("git-cmd", "/usr/local/bin/git", "Path to git executable")
44         gitDir := flag.String("git-dir", "", "Path to .git repository")
45         notesRef := flag.String("ref", "comments", "notes reference name")
46         umask := flag.String("umask", "027", "umask value")
47         flag.Parse()
48         uid := syscall.Geteuid()
49         if err := syscall.Setuid(uid); err != nil {
50                 log.Fatal(err)
51         }
52         umaskInt, err := strconv.ParseUint(*umask, 8, 16)
53         if err != nil {
54                 panic(err)
55         }
56         syscall.Umask(int(umaskInt))
57
58         msg, err := mail.ReadMessage(os.Stdin)
59         if err != nil {
60                 log.Fatal(err)
61         }
62         subj, r, err := parseEmail(msg)
63         if err != nil {
64                 log.Fatal(err)
65         }
66         body, err := ioutil.ReadAll(r)
67         if err != nil {
68                 log.Fatal(err)
69         }
70         from := msg.Header.Get("From")
71         if from == "" {
72                 log.Fatal("From is missing")
73         }
74         if len(body) == 0 {
75                 log.Fatal("no body")
76         }
77
78         if h, err := hex.DecodeString(subj); err != nil || len(h) != sha1.Size {
79                 os.Exit(0)
80         }
81         fromCols := strings.Fields(from)
82         from = strings.Join(fromCols[:len(fromCols)-1], " ")
83
84         cmd := exec.Command(
85                 *gitCmd, "--git-dir", *gitDir,
86                 "notes", "--ref", *notesRef, "show", subj,
87         )
88         note, _ := cmd.Output()
89         note = bytes.TrimSuffix(note, []byte{'\n'})
90
91         // Remove trailing whitespaces, because git-notes-add will remove
92         // them anyway, and we have to know exact bytes count
93         lines := strings.Split(string(body), "\n")
94         for i, line := range lines {
95                 lines[i] = strings.TrimRight(line, " ")
96         }
97         for lines[len(lines)-1] == "" {
98                 lines = lines[:len(lines)-1]
99         }
100
101         buf := bytes.NewBuffer(note)
102         w := netstring.NewWriter(buf)
103         w.WriteChunk([]byte(fmt.Sprintf(
104                 "From: %s\nDate: %s\n\n%s",
105                 from,
106                 time.Now().Format(WhenFmt),
107                 strings.Join(lines, "\n"),
108         )))
109
110         cmd = exec.Command(
111                 *gitCmd, "--git-dir", *gitDir,
112                 "notes", "--ref", *notesRef, "add",
113                 "-F", "-", "-f", subj,
114         )
115         cmd.Stdin = buf
116         if err = cmd.Run(); err != nil {
117                 log.Fatal(err)
118         }
119 }