~fkfd/sophon

fe413a692f9cc510401d6a979b0955a5b12d299b — Frederick Yin 3 years ago ceed985
Misc history-related fixes
4 files changed, 13 insertions(+), 6 deletions(-)

M history.go
M routes.go
M utils.go
M wiki.go
M history.go => history.go +2 -2
@@ 27,7 27,7 @@ func (entry historyEntry) Marshal() string {
		entry.Diff,
		"-----END DIFF-----",
		"-----END ENTRY-----",
	}, "\n") + "\n"
	}, "\n") + "\n\n"
}

type historyChronicle struct {


@@ 58,7 58,7 @@ func parseHistoryFile(page, hist string) (historyChronicle, error) {
			case strings.HasPrefix(ln, "ACTION: "):
				ent.Action = ln[8:]
			case strings.HasPrefix(ln, "SECTION: "):
				ent.Section= ln[9:]
				ent.Section = ln[9:]
			case strings.HasPrefix(ln, "TIME: "):
				t, err := time.Parse(time.RFC3339, ln[6:])
				if err != nil {

M routes.go => routes.go +2 -2
@@ 22,7 22,7 @@ func routeRequest(path string, query string, clientIP net.Addr) (string, string)

	switch segs[0] {
	case "", "index":
		return statusSuccess, "Sophon Index\nYour IP address is " + clientIP.String()
		return statusSuccess, "Sophon Index\nYour IP address is " + usefulIP(clientIP)
	case "pages":
		if len(segs) == 1 || segs[1] == "" {
			page, err := genPageList()


@@ 65,7 65,7 @@ func routeWikiRequest(segs []string, query string, clientIP net.Addr) (string, s
				// /wiki/<page>/edit?<section>
				// where <section> must be "meta" or a valid body section number
				// as defined in docs/wiki_pages.gmi
				sessionID, err := initEditSession(pageName, query, clientIP.String())
				sessionID, err := initEditSession(pageName, query, usefulIP(clientIP))
				if err != nil {
					return "40 " + err.Error(), ""
				}

M utils.go => utils.go +7 -0
@@ 3,7 3,9 @@ package main
import (
	"fmt"
	"io"
	"net"
	"os"
	"strings"
)

func copyFile(src, dst string) error {


@@ 30,3 32,8 @@ func copyFile(src, dst string) error {
	_, err = io.Copy(destination, source)
	return err
}

func usefulIP(ip net.Addr) string {
	segs := strings.Split(ip.String(), ":")
	return strings.Join(segs[:len(segs)-1], ":")
}

M wiki.go => wiki.go +2 -2
@@ 52,7 52,7 @@ func readWikiPage(pageName string) (string, error) {
type editSessionManifest struct {
	Version          string
	PageTitle        string
	Section             string
	Section          string
	ClientIP         string
	LastAccessedTime string
}


@@ 248,7 248,7 @@ func commitSession(id, msg string) error {
	}

	// keep history log
	entry := historyEntry{title, section, time.Now(), manifest.ClientIP, msg, string(diff)}
	entry := historyEntry{"EDITED", section, time.Now(), manifest.ClientIP, msg, string(diff)}
	histFile, err := os.OpenFile("history/"+title+".hist", os.O_APPEND|os.O_WRONLY|os.O_CREATE, filePerm)
	defer histFile.Close()
	_, err = histFile.WriteString(entry.Marshal())