]> Sergey Matveev's repositories - sgblog.git/blob - cmd/sgblog/gopher.go
URL support in Gopher
[sgblog.git] / cmd / sgblog / gopher.go
1 /*
2 SGBlog -- Git-based 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 package main
19
20 import (
21         "bufio"
22         "bytes"
23         "encoding/json"
24         "errors"
25         "fmt"
26         "io"
27         "io/ioutil"
28         "log"
29         "os"
30         "strconv"
31         "strings"
32         "time"
33
34         "github.com/hjson/hjson-go"
35         "go.stargrave.org/sgblog"
36         "gopkg.in/src-d/go-git.v4"
37         "gopkg.in/src-d/go-git.v4/plumbing"
38 )
39
40 const CRLF = "\r\n"
41
42 var DashLine = strings.Repeat("-", 72)
43
44 func serveGopher() {
45         cfgPath := os.Args[2]
46         cfgRaw, err := ioutil.ReadFile(cfgPath)
47         if err != nil {
48                 log.Fatalln(err)
49         }
50         var cfgGeneral map[string]interface{}
51         if err = hjson.Unmarshal(cfgRaw, &cfgGeneral); err != nil {
52                 log.Fatalln(err)
53         }
54         cfgRaw, err = json.Marshal(cfgGeneral)
55         if err != nil {
56                 log.Fatalln(err)
57         }
58         var cfg *Cfg
59         if err = json.Unmarshal(cfgRaw, &cfg); err != nil {
60                 log.Fatalln(err)
61         }
62         if cfg.GopherDomain == "" {
63                 log.Fatalln("GopherDomain is not configured")
64         }
65
66         headHash, err := initRepo(cfg)
67         if err != nil {
68                 log.Fatalln(err)
69         }
70
71         scanner := bufio.NewScanner(io.LimitReader(os.Stdin, 1<<8))
72         if !scanner.Scan() {
73                 log.Fatalln(errors.New("no CRLF found"))
74         }
75         selector := scanner.Text()
76         if selector == "" {
77                 selector = "offset/0"
78         }
79         if strings.HasPrefix(selector, "offset/") {
80                 offset, err := strconv.Atoi(selector[len("offset/"):])
81                 if err != nil {
82                         log.Fatalln(err)
83                 }
84                 repoLog, err := repo.Log(&git.LogOptions{From: *headHash})
85                 if err != nil {
86                         log.Fatalln(err)
87                 }
88                 commitN := 0
89                 for i := 0; i < offset; i++ {
90                         if _, err = repoLog.Next(); err != nil {
91                                 break
92                         }
93                         commitN++
94                 }
95
96                 logEnded := false
97                 var menu bytes.Buffer
98                 var yearPrev int
99                 var monthPrev time.Month
100                 var dayPrev int
101                 for i := 0; i < PageEntries; i++ {
102                         commit, err := repoLog.Next()
103                         if err != nil {
104                                 logEnded = true
105                                 break
106                         }
107                         yearCur, monthCur, dayCur := commit.Author.When.Date()
108                         if dayCur != dayPrev || monthCur != monthPrev || yearCur != yearPrev {
109                                 menu.WriteString(fmt.Sprintf(
110                                         "i%04d-%02d-%02d\tnil\t%s\t%d%s",
111                                         yearCur, monthCur, dayCur,
112                                         cfg.GopherDomain, 70, CRLF,
113                                 ))
114                                 yearPrev, monthPrev, dayPrev = yearCur, monthCur, dayCur
115                         }
116                         commitN++
117                         lines := msgSplit(commit.Message)
118                         var commentsValue string
119                         if l := len(parseComments(getNote(commentsTree, commit.Hash))); l > 0 {
120                                 commentsValue = fmt.Sprintf(" (%dC)", l)
121                         }
122                         menu.WriteString(fmt.Sprintf(
123                                 "0[%02d:%02d] %s (%dL)%s\t/%s\t%s\t%d%s",
124                                 commit.Author.When.Hour(),
125                                 commit.Author.When.Minute(),
126                                 lines[0],
127                                 len(lines)-2,
128                                 commentsValue,
129                                 commit.Hash.String(),
130                                 cfg.GopherDomain, 70, CRLF,
131                         ))
132                 }
133
134                 fmt.Printf(
135                         "i%s (%d-%d)\tnil\t%s\t%d%s",
136                         cfg.Title,
137                         offset,
138                         offset+PageEntries,
139                         cfg.GopherDomain, 70, CRLF,
140                 )
141                 if cfg.AboutURL != "" {
142                         fmt.Printf(
143                                 "hAbout\tURL:%s\t%s\t%d%s",
144                                 cfg.AboutURL,
145                                 cfg.GopherDomain, 70, CRLF,
146                         )
147                 }
148                 if offset > 0 {
149                         offsetPrev := offset - PageEntries
150                         if offsetPrev < 0 {
151                                 offsetPrev = 0
152                         }
153                         fmt.Printf(
154                                 "1Prev\toffset/%d\t%s\t%d%s",
155                                 offsetPrev,
156                                 cfg.GopherDomain, 70, CRLF,
157                         )
158                 }
159                 if !logEnded {
160                         fmt.Printf(
161                                 "1Next\toffset/%d\t%s\t%d%s",
162                                 offset+PageEntries,
163                                 cfg.GopherDomain, 70, CRLF,
164                         )
165                 }
166                 fmt.Print(menu.String())
167                 fmt.Printf(
168                         "iGenerated by: SGBlog %s\terr\t%s\t%d%s",
169                         sgblog.Version,
170                         cfg.GopherDomain, 70, CRLF,
171                 )
172                 fmt.Print("." + CRLF)
173         } else if strings.HasPrefix(selector, "URL:") {
174                 selector = selector[len("URL:"):]
175                 fmt.Printf(`<html>
176 <head>
177         <meta http-equiv="Refresh" content="1; url=%s" />
178         <title>Redirect to non-gopher URL</title>
179 </head>
180 <body>
181 Redirecting to <a href="%s">%s</a>...
182 </body>
183 </html>
184 `, selector, selector, selector)
185         } else if sha1DigestRe.MatchString(selector) {
186                 commit, err := repo.CommitObject(plumbing.NewHash(selector[1:]))
187                 if err != nil {
188                         log.Fatalln(err)
189                 }
190                 fmt.Printf(
191                         "What: %s\nWhen: %s\n%s\n%s",
192                         commit.Hash.String(),
193                         commit.Author.When.Format(sgblog.WhenFmt),
194                         DashLine,
195                         commit.Message,
196                 )
197                 notesRaw := getNote(notesTree, commit.Hash)
198                 if len(notesRaw) > 0 {
199                         fmt.Printf("%s\nNote:\n%s\n", DashLine, string(notesRaw))
200                 }
201                 for i, comment := range parseComments(getNote(commentsTree, commit.Hash)) {
202                         fmt.Printf("%s\ncomment %d:\n%s\n", DashLine, i, comment)
203                 }
204                 fmt.Printf("%s\nGenerated by: SGBlog %s\n", DashLine, sgblog.Version)
205         } else {
206                 log.Fatalln(errors.New("unknown selector"))
207         }
208 }