From 8a3ddad58e9829bfe61d5c6990c1d1c0ec34c0d8 Mon Sep 17 00:00:00 2001 From: sloumdrone Date: Tue, 17 Sep 2019 21:57:21 -0700 Subject: [PATCH] Added ability to view a link's url with the check command --- .gitignore | 1 + client.go | 30 +++++++++++++---- gemini/gemini.go | 83 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+), 7 deletions(-) create mode 100644 gemini/gemini.go diff --git a/.gitignore b/.gitignore index 9fe1ace..cb9380e 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ bombadillo +*.asciinema diff --git a/client.go b/client.go index 372a00d..766bd96 100644 --- a/client.go +++ b/client.go @@ -386,16 +386,19 @@ func (c *client) doLinkCommandAs(action, target string, values []string) { return } + num -= 1 + + links := c.PageState.History[c.PageState.Position].Links + if num >= len(links) || num < 0 { + c.SetMessage(fmt.Sprintf("Invalid link id: %s", target), true) + c.DrawMessage() + return + } + switch action { case "ADD", "A": - links := c.PageState.History[c.PageState.Position].Links - if num >= len(links) { - c.SetMessage(fmt.Sprintf("Invalid link id: %s", target), true) - c.DrawMessage() - return - } bm := make([]string, 0, 5) - bm = append(bm, links[num-1]) + bm = append(bm, links[num]) bm = append(bm, values...) msg, err := c.BookMarks.Add(bm) if err != nil { @@ -455,6 +458,7 @@ func (c *client) doLinkCommand(action, target string) { c.DrawMessage() } + switch action { case "DELETE", "D": msg, err := c.BookMarks.Delete(num) @@ -482,6 +486,18 @@ func (c *client) doLinkCommand(action, target string) { return } c.Visit(c.BookMarks.Links[num]) + case "CHECK", "C": + num -= 1 + + links := c.PageState.History[c.PageState.Position].Links + if num >= len(links) || num < 0 { + c.SetMessage(fmt.Sprintf("Invalid link id: %s", target), true) + c.DrawMessage() + return + } + link := links[num] + c.SetMessage(fmt.Sprintf("[%d] %s", num + 1, link), false) + c.DrawMessage() default: c.SetMessage(fmt.Sprintf("Action %q does not exist for target %q", action, target), true) c.DrawMessage() diff --git a/gemini/gemini.go b/gemini/gemini.go new file mode 100644 index 0000000..d0f3ce9 --- /dev/null +++ b/gemini/gemini.go @@ -0,0 +1,83 @@ +package gemini + +import ( + "crypto/tls" + "fmt" + "net" + "io/ioutil" + // "strings" + "time" + + // "tildegit.org/sloum/mailcap" +) + + +//------------------------------------------------\\ +// + + + F U N C T I O N S + + + \\ +//--------------------------------------------------\\ + +func Retrieve(host, port, resource string) ([]byte, error) { + nullRes := make([]byte, 0) + timeOut := time.Duration(5) * time.Second + + if host == "" || port == "" { + return nullRes, fmt.Errorf("Incomplete request url") + } + + addr := host + ":" + port + + conf := &tls.Config{ + InsecureSkipVerify: true, + } + + conn, err := net.DialTimeout("tcp", addr, timeOut) + if err != nil { + return nullRes, err + } + + secureConn := tls.Client(conn, conf) + + send := resource + "\n" + + _, err = secureConn.Write([]byte(send)) + if err != nil { + return nullRes, err + } + + result, err := ioutil.ReadAll(conn) + if err != nil { + return nullRes, err + } + + return result, nil +} + +func Visit(host, port, resource string) (string, []string, error) { + resp, err := Retrieve(host, port, resource) + if err != nil { + return "", []string{}, err + } + + // TODO break out the header + // header := "" + mime := "" + mimeMaj := mime + mimeMin := mime + // status := "" + content := string(resp) + + if mimeMaj == "text" && mimeMin == "gemini" { + // text := string(resp) + // links := []string{} + + // TODO parse geminimap from 'content' + } else if mimeMaj == "text" { + // TODO just return the text + } else { + // TODO use mailcap to try and open the file + } + + + return content, []string{}, nil +} +