From 301edec262f009ca5707b61c1ed2b58dd2e4b69b Mon Sep 17 00:00:00 2001 From: blank X Date: Wed, 11 Aug 2021 17:49:59 +0700 Subject: [PATCH] Split konbata into different files --- main.go | 221 ----------------------------------------------------- parser.go | 76 ++++++++++++++++++ perrors.go | 93 ++++++++++++++++++++++ predirs.go | 75 ++++++++++++++++++ utils.go | 13 ++++ 5 files changed, 257 insertions(+), 221 deletions(-) create mode 100644 parser.go create mode 100644 perrors.go create mode 100644 predirs.go create mode 100644 utils.go diff --git a/main.go b/main.go index 528a269..3044944 100644 --- a/main.go +++ b/main.go @@ -6,10 +6,8 @@ import ( "log" "sort" "time" - "bufio" "errors" "context" - "strconv" "strings" "net/url" "crypto/x509" @@ -23,17 +21,8 @@ import ( var ( hosts tofu.KnownHosts hostsfile *tofu.HostWriter - predirs map[string]*url.URL - perrors map[string]PError ) -func xdgDataHome() string { - if s, ok := os.LookupEnv("XDG_DATA_HOME"); ok { - return s - } - return filepath.Join(os.Getenv("HOME"), ".local", "share") -} - func init() { path := filepath.Join(xdgDataHome(), "konbata", "known_hosts") err := hosts.Load(path) @@ -57,144 +46,6 @@ func init() { } } -func populatePRedirs() error { - file, err := os.OpenFile(filepath.Join(xdgDataHome(), "konbata", "predirs"), os.O_RDONLY|os.O_CREATE, 0600) - if err != nil { - return err - } - defer file.Close() - scanner := bufio.NewScanner(file) - predirs = make(map[string]*url.URL) - for scanner.Scan() { - urls := strings.Split(scanner.Text(), " ") - if len(urls) != 2 { - continue - } - url0, err := url.Parse(urls[0]) - if err != nil { - continue - } - if url0.Scheme != "gemini" { - continue - } - url1, err := url.Parse(urls[1]) - if err != nil { - continue - } - if url1.Scheme != "gemini" { - continue - } - predirs[url0.String()] = url1 - } - return nil -} - -func savePRedirs() error { - file, err := os.OpenFile(filepath.Join(xdgDataHome(), "konbata", "predirs"), os.O_WRONLY|os.O_CREATE, 0600) - if err != nil { - return err - } - defer file.Close() - err = file.Truncate(0) - if err != nil { - return err - } - writer := bufio.NewWriter(file) - for url0, url1 := range predirs { - _, err = writer.WriteString(url0) - if err != nil { - return err - } - _, err = writer.WriteRune(' ') - if err != nil { - return err - } - _, err = writer.WriteString(url1.String()) - if err != nil { - return err - } - _, err = writer.WriteRune('\n') - if err != nil { - return err - } - } - return writer.Flush() -} - -func populatePErrors() error { - file, err := os.OpenFile(filepath.Join(xdgDataHome(), "konbata", "perrors"), os.O_RDONLY|os.O_CREATE, 0600) - if err != nil { - return err - } - defer file.Close() - scanner := bufio.NewScanner(file) - perrors = make(map[string]PError) - unescaper := strings.NewReplacer("\\n", "\n", "\\\\", "\\") - for scanner.Scan() { - values := strings.SplitN(scanner.Text(), " ", 3) - if len(values) != 3 { - continue - } - url, err := url.Parse(values[0]) - if err != nil { - continue - } - if url.Scheme != "gemini" { - continue - } - code, err := strconv.Atoi(values[1]) - if err != nil { - continue - } - perrors[url.String()] = PError{ - Code: gemini.Status(code), - Message: unescaper.Replace(values[2]), - } - } - return nil -} - -func savePErrors() error { - file, err := os.OpenFile(filepath.Join(xdgDataHome(), "konbata", "perrors"), os.O_WRONLY|os.O_CREATE, 0600) - if err != nil { - return err - } - defer file.Close() - err = file.Truncate(0) - if err != nil { - return err - } - writer := bufio.NewWriter(file) - escaper := strings.NewReplacer("\\", "\\\\", "\n", "\\n") - for url, perror := range perrors { - _, err = writer.WriteString(url) - if err != nil { - return err - } - _, err = writer.WriteRune(' ') - if err != nil { - return err - } - _, err = writer.WriteString(strconv.Itoa(int(perror.Code))) - if err != nil { - return err - } - _, err = writer.WriteRune(' ') - if err != nil { - return err - } - _, err = escaper.WriteString(writer, perror.Message) - if err != nil { - return err - } - _, err = writer.WriteRune('\n') - if err != nil { - return err - } - } - return writer.Flush() -} - func trustCertificate(hostname string, cert *x509.Certificate) error { host := tofu.NewHost(hostname, cert.Raw) knownHost, ok := hosts.Lookup(hostname) @@ -354,75 +205,3 @@ func main() { } fmt.Println(string(out)) } - -type Feed struct { - XMLName xml.Name `xml:"feed"` - XMLNS string `xml:"xmlns,attr"` - Title string `xml:"title"` - Link FeedLink `xml:"link"` - Updated string `xml:"updated"` - Id string `xml:"id"` - Entries []FeedEntry `xml:"entry"` -} -type FeedLink struct { - XMLName xml.Name `xml:"link"` - Href string `xml:"href,attr"` -} -type FeedEntry struct { - XMLName xml.Name `xml:"entry"` - Title string `xml:"title"` - Link EntryLink `xml:"link"` - Id string `xml:"id"` - Updated string `xml:"updated"` -} -type EntryLink struct { - XMLName xml.Name `xml:"link"` - Href string `xml:"href,attr"` - Rel string `xml:"rel,attr"` -} - -type FeedItem struct { - Date time.Time - Title string - Link string -} - -type ByTime []FeedItem -func (a ByTime) Len() int { - return len(a) -} -func (a ByTime) Less(i, j int) bool { - return a[i].Date.Before(a[j].Date) -} -func (a ByTime) Swap(i, j int) { - a[i], a[j] = a[j], a[i] -} - -type PError struct { - Code gemini.Status - Message string -} - -type AtomWriter struct { - Title string - Items []FeedItem -} - -func (a *AtomWriter) Handle(line gemini.Line) { - switch line := line.(type) { - case gemini.LineLink: - runes := []rune(line.Name) - t, err := time.Parse("2006-01-02", string(runes[:10])) - if err == nil { - a.Items = append(a.Items, FeedItem{ - Date: t, - Title: strings.TrimSpace(strings.TrimLeft(strings.TrimSpace(string(runes[10:])), ":-")), - Link: string(line.URL), - }) - } - case gemini.LineHeading1: - if a.Title == "" { - a.Title = string(line) - } - } -} diff --git a/parser.go b/parser.go new file mode 100644 index 0000000..aa8e800 --- /dev/null +++ b/parser.go @@ -0,0 +1,76 @@ +package main + +import ( + "time" + "strings" + "encoding/xml" + + "git.sr.ht/~adnano/go-gemini" +) + +type Feed struct { + XMLName xml.Name `xml:"feed"` + XMLNS string `xml:"xmlns,attr"` + Title string `xml:"title"` + Link FeedLink `xml:"link"` + Updated string `xml:"updated"` + Id string `xml:"id"` + Entries []FeedEntry `xml:"entry"` +} +type FeedLink struct { + XMLName xml.Name `xml:"link"` + Href string `xml:"href,attr"` +} +type FeedEntry struct { + XMLName xml.Name `xml:"entry"` + Title string `xml:"title"` + Link EntryLink `xml:"link"` + Id string `xml:"id"` + Updated string `xml:"updated"` +} +type EntryLink struct { + XMLName xml.Name `xml:"link"` + Href string `xml:"href,attr"` + Rel string `xml:"rel,attr"` +} + +type FeedItem struct { + Date time.Time + Title string + Link string +} + +type ByTime []FeedItem +func (a ByTime) Len() int { + return len(a) +} +func (a ByTime) Less(i, j int) bool { + return a[i].Date.Before(a[j].Date) +} +func (a ByTime) Swap(i, j int) { + a[i], a[j] = a[j], a[i] +} + +type AtomWriter struct { + Title string + Items []FeedItem +} + +func (a *AtomWriter) Handle(line gemini.Line) { + switch line := line.(type) { + case gemini.LineLink: + runes := []rune(line.Name) + t, err := time.Parse("2006-01-02", string(runes[:10])) + if err == nil { + a.Items = append(a.Items, FeedItem{ + Date: t, + Title: strings.TrimSpace(strings.TrimLeft(strings.TrimSpace(string(runes[10:])), ":-")), + Link: string(line.URL), + }) + } + case gemini.LineHeading1: + if a.Title == "" { + a.Title = string(line) + } + } +} diff --git a/perrors.go b/perrors.go new file mode 100644 index 0000000..e78493e --- /dev/null +++ b/perrors.go @@ -0,0 +1,93 @@ +package main + +import ( + "os" + "bufio" + "strconv" + "strings" + "net/url" + "path/filepath" + + "git.sr.ht/~adnano/go-gemini" +) + +var perrors map[string]PError + +type PError struct { + Code gemini.Status + Message string +} + +func populatePErrors() error { + file, err := os.OpenFile(filepath.Join(xdgDataHome(), "konbata", "perrors"), os.O_RDONLY|os.O_CREATE, 0600) + if err != nil { + return err + } + defer file.Close() + scanner := bufio.NewScanner(file) + perrors = make(map[string]PError) + unescaper := strings.NewReplacer("\\n", "\n", "\\\\", "\\") + for scanner.Scan() { + values := strings.SplitN(scanner.Text(), " ", 3) + if len(values) != 3 { + continue + } + url, err := url.Parse(values[0]) + if err != nil { + continue + } + if url.Scheme != "gemini" { + continue + } + code, err := strconv.Atoi(values[1]) + if err != nil { + continue + } + perrors[url.String()] = PError{ + Code: gemini.Status(code), + Message: unescaper.Replace(values[2]), + } + } + return nil +} + +func savePErrors() error { + file, err := os.OpenFile(filepath.Join(xdgDataHome(), "konbata", "perrors"), os.O_WRONLY|os.O_CREATE, 0600) + if err != nil { + return err + } + defer file.Close() + err = file.Truncate(0) + if err != nil { + return err + } + writer := bufio.NewWriter(file) + escaper := strings.NewReplacer("\\", "\\\\", "\n", "\\n") + for url, perror := range perrors { + _, err = writer.WriteString(url) + if err != nil { + return err + } + _, err = writer.WriteRune(' ') + if err != nil { + return err + } + _, err = writer.WriteString(strconv.Itoa(int(perror.Code))) + if err != nil { + return err + } + _, err = writer.WriteRune(' ') + if err != nil { + return err + } + _, err = escaper.WriteString(writer, perror.Message) + if err != nil { + return err + } + _, err = writer.WriteRune('\n') + if err != nil { + return err + } + } + return writer.Flush() +} diff --git a/predirs.go b/predirs.go new file mode 100644 index 0000000..7fedfd8 --- /dev/null +++ b/predirs.go @@ -0,0 +1,75 @@ +package main + +import ( + "os" + "bufio" + "strings" + "net/url" + "path/filepath" +) + +var predirs map[string]*url.URL + +func populatePRedirs() error { + file, err := os.OpenFile(filepath.Join(xdgDataHome(), "konbata", "predirs"), os.O_RDONLY|os.O_CREATE, 0600) + if err != nil { + return err + } + defer file.Close() + scanner := bufio.NewScanner(file) + predirs = make(map[string]*url.URL) + for scanner.Scan() { + urls := strings.Split(scanner.Text(), " ") + if len(urls) != 2 { + continue + } + url0, err := url.Parse(urls[0]) + if err != nil { + continue + } + if url0.Scheme != "gemini" { + continue + } + url1, err := url.Parse(urls[1]) + if err != nil { + continue + } + if url1.Scheme != "gemini" { + continue + } + predirs[url0.String()] = url1 + } + return nil +} + +func savePRedirs() error { + file, err := os.OpenFile(filepath.Join(xdgDataHome(), "konbata", "predirs"), os.O_WRONLY|os.O_CREATE, 0600) + if err != nil { + return err + } + defer file.Close() + err = file.Truncate(0) + if err != nil { + return err + } + writer := bufio.NewWriter(file) + for url0, url1 := range predirs { + _, err = writer.WriteString(url0) + if err != nil { + return err + } + _, err = writer.WriteRune(' ') + if err != nil { + return err + } + _, err = writer.WriteString(url1.String()) + if err != nil { + return err + } + _, err = writer.WriteRune('\n') + if err != nil { + return err + } + } + return writer.Flush() +} diff --git a/utils.go b/utils.go new file mode 100644 index 0000000..4198d99 --- /dev/null +++ b/utils.go @@ -0,0 +1,13 @@ +package main + +import ( + "os" + "path/filepath" +) + +func xdgDataHome() string { + if s, ok := os.LookupEnv("XDG_DATA_HOME"); ok { + return s + } + return filepath.Join(os.Getenv("HOME"), ".local", "share") +}