Adds telnet and http modules, updates visit method on client

This commit is contained in:
sloumdrone 2019-09-10 20:13:30 -07:00
parent da45f627e0
commit 84631a38da
13 changed files with 160 additions and 78 deletions

View File

@ -2,6 +2,7 @@ package main
import (
"fmt"
"strings"
)
//------------------------------------------------\\
@ -21,14 +22,24 @@ type Bookmarks struct {
// + + + R E C E I V E R S + + + \\
//--------------------------------------------------\\
func (b *Bookmarks) Add([]string) error {
// TODO add a bookmark
return fmt.Errorf("")
func (b *Bookmarks) Add(v []string) (string, error) {
if len(v) < 2 {
return "", fmt.Errorf("Received %d arguments, expected 2+", len(v))
}
b.Titles = append(b.Titles, strings.Join(v[1:], " "))
b.Links = append(b.Links, v[0])
b.Length = len(b.Titles)
return "Bookmark added successfully", nil
}
func (b *Bookmarks) Delete(int) error {
// TODO delete a bookmark
return fmt.Errorf("")
func (b *Bookmarks) Delete(i int) (string, error) {
if i < len(b.Titles) && len(b.Titles) == len(b.Links) {
b.Titles = append(b.Titles[:i], b.Titles[i+1:]...)
b.Links = append(b.Links[:i], b.Links[i+1:]...)
b.Length = len(b.Titles)
return "Bookmark deleted successfully", nil
}
return "", fmt.Errorf("Bookmark %d does not exist", i)
}
func (b *Bookmarks) ToggleOpen() {
@ -46,17 +57,42 @@ func (b *Bookmarks) ToggleFocused() {
}
}
func (b *Bookmarks) IniDump() string {
// TODO create dump of values for INI file
return ""
func (b Bookmarks) IniDump() string {
if len(b.Titles) < 0 {
return ""
}
out := "[BOOKMARKS]\n"
for i := 0; i < len(b.Titles); i++ {
out += b.Titles[i]
out += "="
out += b.Links[i]
out += "\n"
}
return out
}
func (b *Bookmarks) Render() ([]string, error) {
// TODO grab all of the bookmarks as a fixed
// width string including border and spacing
return []string{}, fmt.Errorf("")
// Get a list, including link nums, of bookmarks
// as a string slice
func (b Bookmarks) List() []string {
var out []string
for i, t := range b.Titles {
out = append(out, fmt.Sprintf("[%d] %s", i, t))
}
return out
}
func (b Bookmarks) Render() ([]string, error) {
// TODO Use b.List() to get the necessary
// text and add on the correct border for
// rendering the focus. Use sprintf, left
// aligned: "| %-36.36s |" of the like.
return []string{}, nil
}
// TODO handle scrolling of the bookmarks list
// either here widh a scroll up/down or in the client
// code for scroll
//------------------------------------------------\\
// + + + F U N C T I O N S + + + \\

View File

@ -14,7 +14,10 @@ import (
"tildegit.org/sloum/bombadillo/cmdparse"
"tildegit.org/sloum/bombadillo/cui"
"tildegit.org/sloum/bombadillo/gopher"
// "tildegit.org/sloum/bombadillo/gemini"
// "tildegit.org/sloum/bombadillo/gopher"
"tildegit.org/sloum/bombadillo/http"
"tildegit.org/sloum/bombadillo/telnet"
)
//------------------------------------------------\\
@ -231,11 +234,14 @@ func (c *client) doCommandAs(action string, values []string) {
switch action {
case "ADD", "A":
err := c.BookMarks.Add(values)
msg, err := c.BookMarks.Add(values)
if err != nil {
c.SetMessage(err.Error(), true)
c.DrawMessage()
return
} else {
c.SetMessage(msg, false)
c.DrawMessage()
}
err = saveConfig()
@ -342,15 +348,7 @@ func (c *client) search() {
escapedEntry := entry
go c.Visit(fmt.Sprintf("%s?%s",u.Full,escapedEntry))
case "http", "https":
c.SetMessage("Attempting to open in web browser", false)
c.DrawMessage()
err := gopher.OpenBrowser(u.Full)
if err != nil {
c.SetMessage(err.Error(), true)
} else {
c.SetMessage("Opened in web browser", false)
}
c.DrawMessage()
c.Visit(u.Full)
default:
c.SetMessage(fmt.Sprintf("%q is not a supported protocol", u.Scheme), true)
c.DrawMessage()
@ -463,19 +461,31 @@ func (c *client) Visit(url string) {
// TODO send over to gopher request
case "gemini":
// TODO send over to gemini request
case "telnet":
c.SetMessage("Attempting to start telnet session", false)
c.DrawMessage()
msg, err := telnet.StartSession(u.Host, u.Port)
if err != nil {
c.SetMessage(err.Error(), true)
c.DrawMessage()
} else {
c.SetMessage(msg, true)
c.DrawMessage()
}
c.Draw()
case "http", "https":
c.SetMessage("Attempting to open in web browser", false)
c.DrawMessage()
if strings.ToUpper(c.Options["openhttp"]) == "TRUE" {
err := gopher.OpenBrowser(u.Full)
msg, err := http.OpenInBrowser(u.Full)
if err != nil {
c.SetMessage(err.Error(), true)
} else {
c.SetMessage("Opened in web browser", false)
c.SetMessage(msg, false)
}
c.DrawMessage()
} else {
c.SetMessage("'openhttp' is not set to true, aborting opening web link", false)
c.SetMessage("'openhttp' is not set to true, cannot open web link", false)
c.DrawMessage()
}
default:

View File

@ -5,7 +5,7 @@ package gopher
import (
"errors"
"fmt"
// "fmt"
"io/ioutil"
"net"
"strings"
@ -86,16 +86,16 @@ func Visit(addr, openhttp string) (View, error) {
return View{}, err
}
if u.Gophertype == "h" {
if res, tf := isWebLink(u.Resource); tf && strings.ToUpper(openhttp) == "TRUE" {
err := OpenBrowser(res)
if err != nil {
return View{}, err
}
return View{}, fmt.Errorf("")
}
}
// if u.Gophertype == "h" {
// if res, tf := isWebLink(u.Resource); tf && strings.ToUpper(openhttp) == "TRUE" {
// err := OpenBrowser(res)
// if err != nil {
// return View{}, err
// }
//
// return View{}, fmt.Errorf("")
// }
// }
text, err := Retrieve(u)
if err != nil {

View File

@ -1,9 +0,0 @@
// +build darwin
package gopher
import "os/exec"
func OpenBrowser(url string) error {
return exec.Command("open", url).Start()
}

View File

@ -1,9 +0,0 @@
// +build linux
package gopher
import "os/exec"
func OpenBrowser(url string) error {
return exec.Command("xdg-open", url).Start()
}

View File

@ -1,11 +0,0 @@
// +build !linux
// +build !darwin
// +build !windows
package gopher
import "fmt"
func OpenBrowser(url string) error {
return fmt.Errorf("Unsupported os for browser detection")
}

View File

@ -1,9 +0,0 @@
// +build windows
package gopher
import "os/exec"
func OpenBrowser(url string) error {
return exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
}

View File

@ -0,0 +1,13 @@
// +build darwin
package http
import "os/exec"
func OpenInBrowser(url string) (string, error) {
err := exec.Command("open", url).Start()
if err != nil {
return "", err
}
return "Opened in system default web browser", nil
}

View File

@ -0,0 +1,13 @@
// +build linux
package http
import "os/exec"
func OpenInBrowser(url string) (string, error) {
err := exec.Command("xdg-open", url).Start()
if err != nil {
return "", err
}
return "Opened in system default web browser", nil
}

View File

@ -0,0 +1,11 @@
// +build !linux
// +build !darwin
// +build !windows
package http
import "fmt"
func OpenInBrowser(url string) (string, error) {
return "", fmt.Errorf("Unsupported os for browser detection")
}

View File

@ -0,0 +1,13 @@
// +build windows
package http
import "os/exec"
func OpenInBrowser(url string) (string, error) {
err := exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
if err != nil {
return "", err
}
return "Opened in system default web browser", nil
}

View File

@ -6,7 +6,7 @@ package main
//--------------------------------------------------\\
type Page struct {
WrappedContent []string
WrappedContent string
RawContent string
Links []string
Location Url
@ -24,7 +24,7 @@ type Page struct {
//--------------------------------------------------\\
func MakePage(url Url, content string) Page {
p := Page{make([]string, 0), content, make([]string, 0), url, 0}
p := Page{"", content, make([]string, 0), url, 0}
return p
}

24
telnet/telnet.go Normal file
View File

@ -0,0 +1,24 @@
package telnet
import (
"fmt"
"os"
"os/exec"
)
func StartSession(host string, port string) (string, error) {
// Case for telnet links
c := exec.Command("telnet", host, port)
c.Stdin = os.Stdin
c.Stdout = os.Stdout
c.Stderr = os.Stderr
// Clear the screen and position the cursor at the top left
fmt.Print("\033[2J\033[0;0H")
err := c.Run()
if err != nil {
return "", fmt.Errorf("Telnet error response: %s", err.Error())
}
return "Telnet session terminated", nil
}