Adds basic finger protocol support to Bombadillo

This commit is contained in:
sloumdrone 2019-10-06 19:59:46 -07:00
parent 66acf6102f
commit b2a8e7dba5
3 changed files with 82 additions and 0 deletions

View File

@ -12,6 +12,7 @@ import (
"tildegit.org/sloum/bombadillo/cmdparse"
"tildegit.org/sloum/bombadillo/cui"
"tildegit.org/sloum/bombadillo/finger"
"tildegit.org/sloum/bombadillo/gemini"
"tildegit.org/sloum/bombadillo/gopher"
"tildegit.org/sloum/bombadillo/http"
@ -966,6 +967,20 @@ func (c *client) Visit(url string) {
c.SetMessage("'openhttp' is not set to true, cannot open web link", false)
c.DrawMessage()
}
case "finger":
content, err := finger.Finger(u.Host, u.Port, u.Resource)
if err != nil {
c.SetMessage(err.Error(), true)
c.DrawMessage()
return
}
pg := MakePage(u, content, []string{})
pg.WrapContent(c.Width - 1)
c.PageState.Add(pg)
c.SetPercentRead()
c.ClearMessage()
c.SetHeaderUrl()
c.Draw()
default:
c.SetMessage(fmt.Sprintf("%q is not a supported protocol", u.Scheme), true)
c.DrawMessage()

31
finger/finger.go Normal file
View File

@ -0,0 +1,31 @@
package finger
import (
"fmt"
"net"
"io/ioutil"
"time"
)
func Finger(host, port, resource string) (string, error) {
addr := fmt.Sprintf("%s:%s", host, port)
timeOut := time.Duration(3) * time.Second
conn, err := net.DialTimeout("tcp", addr, timeOut)
if err != nil {
return "", err
}
defer conn.Close()
_, err = conn.Write([]byte(resource + "\r\n"))
if err != nil {
return "", err
}
result, err := ioutil.ReadAll(conn)
if err != nil {
return "", err
}
return string(result), nil
}

36
url.go
View File

@ -36,6 +36,13 @@ type Url struct {
// representation of a url and returns a Url struct and
// an error (or nil).
func MakeUrl(u string) (Url, error) {
if len(u) < 1 {
return Url{}, fmt.Errorf("Invalid url, unable to parse")
}
if strings.HasPrefix(u, "finger://") {
return parseFinger(u)
}
var out Url
re := regexp.MustCompile(`^((?P<scheme>[a-zA-Z]+):\/\/)?(?P<host>[\w\-\.\d]+)(?::(?P<port>\d+)?)?(?:/(?P<type>[01345679gIhisp])?)?(?P<resource>.*)?$`)
match := re.FindStringSubmatch(u)
@ -106,3 +113,32 @@ func MakeUrl(u string) (Url, error) {
return out, nil
}
func parseFinger(u string) (Url, error) {
var out Url
out.Scheme = "finger"
if len(u) < 10 {
return out, fmt.Errorf("Invalid finger address")
}
u = u[9:]
userPlusAddress := strings.Split(u, "@")
if len(userPlusAddress) > 1 {
out.Resource = userPlusAddress[0]
u = userPlusAddress[1]
}
hostPort := strings.Split(u, ":")
if len(hostPort) < 2 {
out.Port = "79"
} else {
out.Port = hostPort[1]
}
out.Host = hostPort[0]
resource := ""
if out.Resource != "" {
resource = out.Resource + "@"
}
out.Full = fmt.Sprintf("%s://%s%s:%s", out.Scheme, resource, out.Host, out.Port)
return out, nil
}