]> Sergey Matveev's repositories - sgblog.git/blob - cmd/sgblog-comment-add/main.go
RFC 2047 decode From
[sgblog.git] / cmd / sgblog-comment-add / main.go
1 /*
2 SGBlog -- Git-backed CGI/inetd blogging/phlogging 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-backed CGI/inetd blogging/phlogging engine
19 package main
20
21 import (
22         "bytes"
23         "crypto/sha1"
24         "encoding/hex"
25         "flag"
26         "fmt"
27         "io/ioutil"
28         "log"
29         "mime"
30         "net/mail"
31         "os"
32         "os/exec"
33         "strconv"
34         "strings"
35         "syscall"
36         "time"
37
38         "go.cypherpunks.ru/netstring/v2"
39         "go.stargrave.org/sgblog"
40 )
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         dryRun := flag.Bool("dryrun", false, "Show comment, do not add")
48         committerEmail := flag.String(
49                 "committer-email",
50                 "comment@blog.example.com",
51                 "Git committer's email",
52         )
53         flag.Parse()
54         uid := syscall.Geteuid()
55         if err := syscall.Setuid(uid); err != nil {
56                 log.Fatal(err)
57         }
58         umaskInt, err := strconv.ParseUint(*umask, 8, 16)
59         if err != nil {
60                 panic(err)
61         }
62         syscall.Umask(int(umaskInt))
63
64         msg, err := mail.ReadMessage(os.Stdin)
65         if err != nil {
66                 log.Fatal(err)
67         }
68         subj, r, err := parseEmail(msg)
69         if err != nil {
70                 log.Fatal(err)
71         }
72         body, err := ioutil.ReadAll(r)
73         if err != nil {
74                 log.Fatal(err)
75         }
76         from := msg.Header.Get("From")
77         if from == "" {
78                 log.Fatal("From is missing")
79         }
80         if len(body) == 0 {
81                 log.Fatal("no body")
82         }
83         from, err = new(mime.WordDecoder).DecodeHeader(from)
84         if err != nil {
85                 log.Fatal(err)
86         }
87
88         subj = strings.TrimPrefix(subj, "Re: ")
89         if h, err := hex.DecodeString(subj); err != nil || len(h) != sha1.Size {
90                 os.Exit(0)
91         }
92         fromCols := strings.Fields(from)
93         if len(fromCols) == 1 {
94                 if idx := strings.Index(from, "@"); idx != -1 {
95                         from = strings.Trim(from[:idx], "<>")
96                 }
97         } else {
98                 from = strings.Join(fromCols[:len(fromCols)-1], " ")
99         }
100
101         cmd := exec.Command(
102                 *gitCmd, "--git-dir", *gitDir,
103                 "notes", "--ref", *notesRef, "show", subj,
104         )
105         note, _ := cmd.Output()
106         note = bytes.TrimRight(note, "\r\n")
107
108         // Remove trailing whitespaces, because git-notes-add will remove
109         // them anyway, and we have to know exact bytes count
110         lines := strings.Split(string(body), "\n")
111         for i, line := range lines {
112                 lines[i] = strings.TrimRight(line, " \r")
113         }
114         for lines[len(lines)-1] == "" {
115                 lines = lines[:len(lines)-1]
116         }
117
118         buf := bytes.NewBuffer(note)
119         w := netstring.NewWriter(buf)
120         w.WriteChunk([]byte(fmt.Sprintf(
121                 "From: %s\nDate: %s\n\n%s",
122                 from,
123                 time.Now().UTC().Format(sgblog.WhenFmt),
124                 strings.Join(lines, "\n"),
125         )))
126
127         if *dryRun {
128                 fmt.Print(buf.String())
129                 os.Exit(0)
130         }
131
132         cmd = exec.Command(
133                 *gitCmd, "--git-dir", *gitDir,
134                 "notes", "--ref", *notesRef, "add",
135                 "-F", "-", "-f", subj,
136         )
137         cmd.Env = append(
138                 cmd.Env,
139                 "GIT_AUTHOR_NAME=SGBlog "+sgblog.Version,
140                 "GIT_AUTHOR_EMAIL="+*committerEmail,
141                 "GIT_COMMITTER_NAME=SGBlog "+sgblog.Version,
142                 "GIT_COMMITTER_EMAIL="+*committerEmail,
143         )
144         cmd.Stdin = buf
145         if err = cmd.Run(); err != nil {
146                 log.Fatal(err)
147         }
148 }