]> Sergey Matveev's repositories - sgblog.git/blobdiff - cmd/sgblog/gopher.go
URL support in Gopher
[sgblog.git] / cmd / sgblog / gopher.go
index e98d65b9f08d25c50639b39af32aec232495f08b..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 (
@@ -108,8 +107,9 @@ func serveGopher() {
                        yearCur, monthCur, dayCur := commit.Author.When.Date()
                        if dayCur != dayPrev || monthCur != monthPrev || yearCur != yearPrev {
                                menu.WriteString(fmt.Sprintf(
-                                       "i%04d-%02d-%02d\t\tnull.host\t1%s",
-                                       yearCur, monthCur, dayCur, CRLF,
+                                       "i%04d-%02d-%02d\tnil\t%s\t%d%s",
+                                       yearCur, monthCur, dayCur,
+                                       cfg.GopherDomain, 70, CRLF,
                                ))
                                yearPrev, monthPrev, dayPrev = yearCur, monthCur, dayCur
                        }
@@ -131,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 {
@@ -183,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"))
        }