/* SGBlog -- Git-based CGI blogging engine Copyright (C) 2020 Sergey Matveev This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, version 3 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . */ // Git-based CGI blogging engine email to comments adder package main import ( "bytes" "crypto/sha1" "encoding/hex" "flag" "fmt" "io/ioutil" "log" "net/mail" "os" "os/exec" "strconv" "strings" "syscall" "time" "go.cypherpunks.ru/netstring/v2" ) const WhenFmt = "2006-01-02 15:04:05Z07:00" func main() { gitCmd := flag.String("git-cmd", "/usr/local/bin/git", "Path to git executable") gitDir := flag.String("git-dir", "", "Path to .git repository") notesRef := flag.String("ref", "comments", "notes reference name") umask := flag.String("umask", "027", "umask value") dryRun := flag.Bool("dryrun", false, "Show comment, do not add") flag.Parse() uid := syscall.Geteuid() if err := syscall.Setuid(uid); err != nil { log.Fatal(err) } umaskInt, err := strconv.ParseUint(*umask, 8, 16) if err != nil { panic(err) } syscall.Umask(int(umaskInt)) msg, err := mail.ReadMessage(os.Stdin) if err != nil { log.Fatal(err) } subj, r, err := parseEmail(msg) if err != nil { log.Fatal(err) } body, err := ioutil.ReadAll(r) if err != nil { log.Fatal(err) } from := msg.Header.Get("From") if from == "" { log.Fatal("From is missing") } if len(body) == 0 { log.Fatal("no body") } subj = strings.TrimPrefix(subj, "Re: ") if h, err := hex.DecodeString(subj); err != nil || len(h) != sha1.Size { os.Exit(0) } fromCols := strings.Fields(from) if len(fromCols) == 1 { if idx := strings.Index(from, "@"); idx != -1 { from = strings.Trim(from[:idx], "<>") } } else { from = strings.Join(fromCols[:len(fromCols)-1], " ") } cmd := exec.Command( *gitCmd, "--git-dir", *gitDir, "notes", "--ref", *notesRef, "show", subj, ) note, _ := cmd.Output() note = bytes.TrimSuffix(note, []byte{'\n'}) // Remove trailing whitespaces, because git-notes-add will remove // them anyway, and we have to know exact bytes count lines := strings.Split(string(body), "\n") for i, line := range lines { lines[i] = strings.TrimRight(line, " ") } for lines[len(lines)-1] == "" { lines = lines[:len(lines)-1] } buf := bytes.NewBuffer(note) w := netstring.NewWriter(buf) w.WriteChunk([]byte(fmt.Sprintf( "From: %s\nDate: %s\n\n%s", from, time.Now().Format(WhenFmt), strings.Join(lines, "\n"), ))) if *dryRun { fmt.Print(buf.String()) os.Exit(0) } cmd = exec.Command( *gitCmd, "--git-dir", *gitDir, "notes", "--ref", *notesRef, "add", "-F", "-", "-f", subj, ) cmd.Stdin = buf if err = cmd.Run(); err != nil { log.Fatal(err) } }