2 SGBlog -- Git-backed CGI/inetd blogging/phlogging engine
3 Copyright (C) 2020 Sergey Matveev <stargrave@stargrave.org>
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.
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.
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/>.
18 // Git-backed CGI/inetd blogging/phlogging engine
38 "go.cypherpunks.ru/netstring/v2"
39 "go.stargrave.org/sgblog"
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 dryRun := flag.Bool("dryrun", false, "Show comment, do not add")
48 committerEmail := flag.String(
50 "comment@blog.example.com",
51 "Git committer's email",
54 uid := syscall.Geteuid()
55 if err := syscall.Setuid(uid); err != nil {
58 umaskInt, err := strconv.ParseUint(*umask, 8, 16)
62 syscall.Umask(int(umaskInt))
64 msg, err := mail.ReadMessage(os.Stdin)
68 subj, r, err := parseEmail(msg)
72 body, err := ioutil.ReadAll(r)
76 from := msg.Header.Get("From")
78 log.Fatal("From is missing")
83 from, err = new(mime.WordDecoder).DecodeHeader(from)
88 subj = strings.TrimPrefix(subj, "Re: ")
89 if h, err := hex.DecodeString(subj); err != nil || len(h) != sha1.Size {
92 fromCols := strings.Fields(from)
93 if len(fromCols) == 1 {
94 if idx := strings.Index(from, "@"); idx != -1 {
95 from = strings.Trim(from[:idx], "<>")
98 from = strings.Join(fromCols[:len(fromCols)-1], " ")
102 *gitCmd, "--git-dir", *gitDir,
103 "notes", "--ref", *notesRef, "show", subj,
105 note, _ := cmd.Output()
106 note = bytes.TrimRight(note, "\r\n")
108 // Remove trailing whitespaces, because git-notes-add will remove
109 // them anyway, and we have to know exact bytes count. Also convert
110 // all tabs into spaces
111 lines := strings.Split(string(body), "\n")
112 for i, line := range lines {
113 line = strings.ReplaceAll(line, " ", " ")
114 line = strings.TrimRight(line, " \r")
120 for lines[len(lines)-1] == "" {
121 lines = lines[:len(lines)-1]
124 buf := bytes.NewBuffer(note)
125 w := netstring.NewWriter(buf)
126 w.WriteChunk([]byte(fmt.Sprintf(
127 "From: %s\nDate: %s\n\n%s",
129 time.Now().UTC().Format(sgblog.WhenFmt),
130 strings.Join(lines, "\n"),
134 fmt.Print(buf.String())
139 *gitCmd, "--git-dir", *gitDir,
140 "notes", "--ref", *notesRef, "add",
141 "-F", "-", "-f", subj,
145 "GIT_AUTHOR_NAME=SGBlog "+sgblog.Version,
146 "GIT_AUTHOR_EMAIL="+*committerEmail,
147 "GIT_COMMITTER_NAME=SGBlog "+sgblog.Version,
148 "GIT_COMMITTER_EMAIL="+*committerEmail,
151 if err = cmd.Run(); err != nil {