2 SGBlog -- Git-based CGI blogging 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-based CGI blogging engine email to comments adder
37 "go.cypherpunks.ru/netstring/v2"
38 "go.stargrave.org/sgblog"
42 gitCmd := flag.String("git-cmd", "/usr/local/bin/git", "Path to git executable")
43 gitDir := flag.String("git-dir", "", "Path to .git repository")
44 notesRef := flag.String("ref", "comments", "notes reference name")
45 umask := flag.String("umask", "027", "umask value")
46 dryRun := flag.Bool("dryrun", false, "Show comment, do not add")
47 committerEmail := flag.String(
49 "comment@blog.example.com",
50 "Git committer's email",
53 uid := syscall.Geteuid()
54 if err := syscall.Setuid(uid); err != nil {
57 umaskInt, err := strconv.ParseUint(*umask, 8, 16)
61 syscall.Umask(int(umaskInt))
63 msg, err := mail.ReadMessage(os.Stdin)
67 subj, r, err := parseEmail(msg)
71 body, err := ioutil.ReadAll(r)
75 from := msg.Header.Get("From")
77 log.Fatal("From is missing")
83 subj = strings.TrimPrefix(subj, "Re: ")
84 if h, err := hex.DecodeString(subj); err != nil || len(h) != sha1.Size {
87 fromCols := strings.Fields(from)
88 if len(fromCols) == 1 {
89 if idx := strings.Index(from, "@"); idx != -1 {
90 from = strings.Trim(from[:idx], "<>")
93 from = strings.Join(fromCols[:len(fromCols)-1], " ")
97 *gitCmd, "--git-dir", *gitDir,
98 "notes", "--ref", *notesRef, "show", subj,
100 note, _ := cmd.Output()
101 note = bytes.TrimSuffix(note, []byte{'\n'})
103 // Remove trailing whitespaces, because git-notes-add will remove
104 // them anyway, and we have to know exact bytes count
105 lines := strings.Split(string(body), "\n")
106 for i, line := range lines {
107 lines[i] = strings.TrimRight(line, " ")
109 for lines[len(lines)-1] == "" {
110 lines = lines[:len(lines)-1]
113 buf := bytes.NewBuffer(note)
114 w := netstring.NewWriter(buf)
115 w.WriteChunk([]byte(fmt.Sprintf(
116 "From: %s\nDate: %s\n\n%s",
118 time.Now().UTC().Format(sgblog.WhenFmt),
119 strings.Join(lines, "\n"),
123 fmt.Print(buf.String())
128 *gitCmd, "--git-dir", *gitDir,
129 "notes", "--ref", *notesRef, "add",
130 "-F", "-", "-f", subj,
134 "GIT_AUTHOR_NAME=SGBlog "+sgblog.Version,
135 "GIT_AUTHOR_EMAIL="+*committerEmail,
136 "GIT_COMMITTER_NAME=SGBlog "+sgblog.Version,
137 "GIT_COMMITTER_EMAIL="+*committerEmail,
140 if err = cmd.Run(); err != nil {