]> Sergey Matveev's repositories - sgblog.git/blobdiff - cmd/sgblog/gopher.go
URL support in Gopher
[sgblog.git] / cmd / sgblog / gopher.go
index 808c40fb0d343e080a82d1cbb73e5e2a62791691..2fbbeda1479b110463b9cdac4d4b954eb95ccc85 100644 (file)
@@ -1,5 +1,5 @@
 /*
-SGBlog -- Git-based CGI blogging engine
+SGBlog -- Git-based CGI/inetd blogging/phlogging engine
 Copyright (C) 2020 Sergey Matveev <stargrave@stargrave.org>
 
 This program is free software: you can redistribute it and/or modify
@@ -15,7 +15,6 @@ You should have received a copy of the GNU Affero General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
-// Git-based CGI blogging engine
 package main
 
 import (
@@ -30,6 +29,7 @@ import (
        "os"
        "strconv"
        "strings"
+       "time"
 
        "github.com/hjson/hjson-go"
        "go.stargrave.org/sgblog"
@@ -95,22 +95,34 @@ func serveGopher() {
 
                logEnded := false
                var menu bytes.Buffer
+               var yearPrev int
+               var monthPrev time.Month
+               var dayPrev int
                for i := 0; i < PageEntries; i++ {
                        commit, err := repoLog.Next()
                        if err != nil {
                                logEnded = true
                                break
                        }
+                       yearCur, monthCur, dayCur := commit.Author.When.Date()
+                       if dayCur != dayPrev || monthCur != monthPrev || yearCur != yearPrev {
+                               menu.WriteString(fmt.Sprintf(
+                                       "i%04d-%02d-%02d\tnil\t%s\t%d%s",
+                                       yearCur, monthCur, dayCur,
+                                       cfg.GopherDomain, 70, CRLF,
+                               ))
+                               yearPrev, monthPrev, dayPrev = yearCur, monthCur, dayCur
+                       }
                        commitN++
                        lines := msgSplit(commit.Message)
-
                        var commentsValue string
                        if l := len(parseComments(getNote(commentsTree, commit.Hash))); l > 0 {
                                commentsValue = fmt.Sprintf(" (%dC)", l)
                        }
                        menu.WriteString(fmt.Sprintf(
-                               "0[%s] %s (%dL)%s\t/%s\t%s\t%d%s",
-                               commit.Author.When.Format(sgblog.WhenFmt),
+                               "0[%02d:%02d] %s (%dL)%s\t/%s\t%s\t%d%s",
+                               commit.Author.When.Hour(),
+                               commit.Author.When.Minute(),
                                lines[0],
                                len(lines)-2,
                                commentsValue,
@@ -119,39 +131,57 @@ func serveGopher() {
                        ))
                }
 
-               var links bytes.Buffer
+               fmt.Printf(
+                       "i%s (%d-%d)\tnil\t%s\t%d%s",
+                       cfg.Title,
+                       offset,
+                       offset+PageEntries,
+                       cfg.GopherDomain, 70, CRLF,
+               )
+               if cfg.AboutURL != "" {
+                       fmt.Printf(
+                               "hAbout\tURL:%s\t%s\t%d%s",
+                               cfg.AboutURL,
+                               cfg.GopherDomain, 70, CRLF,
+                       )
+               }
                if offset > 0 {
                        offsetPrev := offset - PageEntries
                        if offsetPrev < 0 {
                                offsetPrev = 0
                        }
-                       links.WriteString(fmt.Sprintf(
+                       fmt.Printf(
                                "1Prev\toffset/%d\t%s\t%d%s",
                                offsetPrev,
                                cfg.GopherDomain, 70, CRLF,
-                       ))
+                       )
                }
                if !logEnded {
-                       links.WriteString(fmt.Sprintf(
+                       fmt.Printf(
                                "1Next\toffset/%d\t%s\t%d%s",
                                offset+PageEntries,
                                cfg.GopherDomain, 70, CRLF,
-                       ))
+                       )
                }
-
+               fmt.Print(menu.String())
                fmt.Printf(
-                       "i%s (%d-%d)\t\tnull.host\t1%s",
-                       cfg.Title,
-                       offset,
-                       offset+PageEntries,
-                       CRLF,
+                       "iGenerated by: SGBlog %s\terr\t%s\t%d%s",
+                       sgblog.Version,
+                       cfg.GopherDomain, 70, CRLF,
                )
-               if cfg.AboutURL != "" {
-                       fmt.Printf("iAbout: %s\t\tnull.host\t1%s", cfg.AboutURL, CRLF)
-               }
-               fmt.Print(links.String())
-               fmt.Print(menu.String())
                fmt.Print("." + CRLF)
+       } else if strings.HasPrefix(selector, "URL:") {
+               selector = selector[len("URL:"):]
+               fmt.Printf(`<html>
+<head>
+       <meta http-equiv="Refresh" content="1; url=%s" />
+       <title>Redirect to non-gopher URL</title>
+</head>
+<body>
+Redirecting to <a href="%s">%s</a>...
+</body>
+</html>
+`, selector, selector, selector)
        } else if sha1DigestRe.MatchString(selector) {
                commit, err := repo.CommitObject(plumbing.NewHash(selector[1:]))
                if err != nil {
@@ -171,6 +201,7 @@ func serveGopher() {
                for i, comment := range parseComments(getNote(commentsTree, commit.Hash)) {
                        fmt.Printf("%s\ncomment %d:\n%s\n", DashLine, i, comment)
                }
+               fmt.Printf("%s\nGenerated by: SGBlog %s\n", DashLine, sgblog.Version)
        } else {
                log.Fatalln(errors.New("unknown selector"))
        }