1 // SGBlog -- Git-backed CGI/UCSPI blogging/phlogging/gemlogging engine
2 // Copyright (C) 2020-2024 Sergey Matveev <stargrave@stargrave.org>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, version 3 of the License.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
13 // You should have received a copy of the GNU Affero General Public License
14 // along with this program. If not, see <http://www.gnu.org/licenses/>.
31 "github.com/go-git/go-git/v5"
32 "github.com/go-git/go-git/v5/plumbing"
33 "github.com/go-git/go-git/v5/plumbing/object"
34 "github.com/vorlif/spreak"
35 "go.stargrave.org/sgblog"
39 //go:embed gopher-menu.tmpl
40 TmplGopherMenuRaw string
41 //go:embed gopher-entry.tmpl
42 TmplGopherEntryRaw string
44 TmplGopherMenu = template.Must(template.New("gopher-menu").Parse(TmplGopherMenuRaw))
45 TmplGopherEntry = template.Must(template.New("gopher-entry").Parse(TmplGopherEntryRaw))
48 type TableMenuEntry struct {
57 func serveGopher(cfgPath string) {
58 cfg, err := readCfg(cfgPath)
62 if cfg.GopherDomain == "" {
63 log.Fatalln("GopherDomain is not configured")
65 initLocalizer(cfg.Lang)
67 headHash, err := initRepo(cfg)
72 scanner := bufio.NewScanner(io.LimitReader(os.Stdin, 1<<8))
74 log.Fatalln(errors.New("no CRLF found"))
76 selector := scanner.Text()
80 selectorParts := strings.Split(selector, "/")
81 if strings.HasPrefix(selector, "URL:") {
82 selector = selector[len("URL:"):]
83 fmt.Printf(`<!DOCTYPE html>
86 <meta http-equiv="Refresh" content="1; url=%s" />
87 <title>Redirect to non-gopher URL</title>
90 Redirecting to <a href="%s">%s</a>...
93 `, selector, selector, selector)
94 } else if sha1DigestRe.MatchString(selector) {
95 commit, err := repo.CommitObject(plumbing.NewHash(selector[1:]))
99 err = TmplGopherEntry.Execute(os.Stdout, struct {
101 Commit *object.Commit
113 When: commit.Author.When.Format(sgblog.WhenFmt),
115 Note: string(sgblog.GetNote(repo, notesTree, commit.Hash)),
116 Images: listImgs(cfg, commit.Hash),
117 Comments: sgblog.ParseComments(sgblog.GetNote(repo, commentsTree, commit.Hash)),
118 Topics: sgblog.ParseTopics(sgblog.GetNote(repo, topicsTree, commit.Hash)),
119 Version: sgblog.Version,
120 TitleEscaped: url.PathEscape(fmt.Sprintf(
121 "Re: %s (%s)", msgSplit(commit.Message)[0], commit.Hash,
127 } else if len(selectorParts) > 1 &&
128 selectorParts[len(selectorParts)-2] == "offset" {
129 offset, err := strconv.Atoi(selectorParts[len(selectorParts)-1])
133 repoLog, err := repo.Log(&git.LogOptions{From: *headHash})
137 topicsCache, err := getTopicsCache(cfg, repoLog)
141 repoLog, err = repo.Log(&git.LogOptions{From: *headHash})
147 if len(selectorParts) == 3 {
148 topic = selectorParts[0]
150 var commits CommitIterNext
152 for i := 0; i < offset; i++ {
153 if _, err = repoLog.Next(); err != nil {
159 hashes := topicsCache[topic]
161 log.Fatalln(errors.New("no posts with that topic"))
163 if len(hashes) > offset {
164 hashes = hashes[offset:]
166 commits = &HashesIter{hashes}
170 entries := make([]TableMenuEntry, 0, PageEntries)
171 for i := 0; i < PageEntries; i++ {
172 commit, err := commits.Next()
177 lines := msgSplit(commit.Message)
178 entries = append(entries, TableMenuEntry{
181 LinesNum: len(lines) - 2,
182 ImagesNum: len(listImgs(cfg, commit.Hash)),
183 CommentsNum: len(sgblog.ParseComments(sgblog.GetNote(
184 repo, commentsTree, commit.Hash,
186 Topics: sgblog.ParseTopics(sgblog.GetNote(
187 repo, topicsTree, commit.Hash,
191 offsetPrev := offset - PageEntries
195 err = TmplGopherMenu.Execute(os.Stdout, struct {
203 Entries []TableMenuEntry
211 OffsetPrev: offsetPrev,
212 OffsetNext: offset + PageEntries,
215 Topics: topicsCache.Topics(),
216 Version: sgblog.Version,
222 log.Fatalln(errors.New("unknown selector"))