Finished working through file saving for gopher and gemini, and added mailcap functionality to gemini
This commit is contained in:
parent
db1cf75d2e
commit
1050b858dd
107
client.go
107
client.go
|
@ -5,7 +5,6 @@ import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
// "os/user"
|
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -17,6 +16,7 @@ import (
|
||||||
"tildegit.org/sloum/bombadillo/gopher"
|
"tildegit.org/sloum/bombadillo/gopher"
|
||||||
"tildegit.org/sloum/bombadillo/http"
|
"tildegit.org/sloum/bombadillo/http"
|
||||||
"tildegit.org/sloum/bombadillo/telnet"
|
"tildegit.org/sloum/bombadillo/telnet"
|
||||||
|
// "tildegit.org/sloum/mailcap"
|
||||||
)
|
)
|
||||||
|
|
||||||
//------------------------------------------------\\
|
//------------------------------------------------\\
|
||||||
|
@ -460,6 +460,8 @@ func (c *client) getCurrentPageRawData() (string, error) {
|
||||||
func (c *client) saveFile(u Url, name string) {
|
func (c *client) saveFile(u Url, name string) {
|
||||||
var file []byte
|
var file []byte
|
||||||
var err error
|
var err error
|
||||||
|
c.SetMessage(fmt.Sprintf("Saving %s ...", name), false)
|
||||||
|
c.DrawMessage()
|
||||||
switch u.Scheme {
|
switch u.Scheme {
|
||||||
case "gopher":
|
case "gopher":
|
||||||
file, err = gopher.Retrieve(u.Host, u.Port, u.Resource)
|
file, err = gopher.Retrieve(u.Host, u.Port, u.Resource)
|
||||||
|
@ -488,6 +490,22 @@ func (c *client) saveFile(u Url, name string) {
|
||||||
c.DrawMessage()
|
c.DrawMessage()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *client) saveFileFromData(d, name string) {
|
||||||
|
data := []byte(d)
|
||||||
|
c.SetMessage(fmt.Sprintf("Saving %s ...", name), false)
|
||||||
|
c.DrawMessage()
|
||||||
|
savePath := c.Options["savelocation"] + name
|
||||||
|
err := ioutil.WriteFile(savePath, data, 0644)
|
||||||
|
if err != nil {
|
||||||
|
c.SetMessage("Error writing file to disk", true)
|
||||||
|
c.DrawMessage()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.SetMessage(fmt.Sprintf("File saved to: %s", savePath), false)
|
||||||
|
c.DrawMessage()
|
||||||
|
}
|
||||||
|
|
||||||
func (c *client) doLinkCommand(action, target string) {
|
func (c *client) doLinkCommand(action, target string) {
|
||||||
num, err := strconv.Atoi(target)
|
num, err := strconv.Atoi(target)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -779,19 +797,29 @@ func (c *client) Visit(url string) {
|
||||||
|
|
||||||
switch u.Scheme {
|
switch u.Scheme {
|
||||||
case "gopher":
|
case "gopher":
|
||||||
content, links, err := gopher.Visit(u.Mime, u.Host, u.Port, u.Resource)
|
if u.DownloadOnly {
|
||||||
if err != nil {
|
nameSplit := strings.Split(u.Resource, "/")
|
||||||
c.SetMessage(err.Error(), true)
|
filename := nameSplit[len(nameSplit) - 1]
|
||||||
c.DrawMessage()
|
filename = strings.Trim(filename, " \t\r\n\v\f\a")
|
||||||
return
|
if filename == "" {
|
||||||
|
filename = "gopherfile"
|
||||||
|
}
|
||||||
|
c.saveFile(u, filename)
|
||||||
|
} else {
|
||||||
|
content, links, err := gopher.Visit(u.Mime, u.Host, u.Port, u.Resource)
|
||||||
|
if err != nil {
|
||||||
|
c.SetMessage(err.Error(), true)
|
||||||
|
c.DrawMessage()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
pg := MakePage(u, content, links)
|
||||||
|
pg.WrapContent(c.Width - 1)
|
||||||
|
c.PageState.Add(pg)
|
||||||
|
c.SetPercentRead()
|
||||||
|
c.ClearMessage()
|
||||||
|
c.SetHeaderUrl()
|
||||||
|
c.Draw()
|
||||||
}
|
}
|
||||||
pg := MakePage(u, content, links)
|
|
||||||
pg.WrapContent(c.Width - 1)
|
|
||||||
c.PageState.Add(pg)
|
|
||||||
c.SetPercentRead()
|
|
||||||
c.ClearMessage()
|
|
||||||
c.SetHeaderUrl()
|
|
||||||
c.Draw()
|
|
||||||
case "gemini":
|
case "gemini":
|
||||||
capsule, err := gemini.Visit(u.Host, u.Port, u.Resource)
|
capsule, err := gemini.Visit(u.Host, u.Port, u.Resource)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -810,8 +838,59 @@ func (c *client) Visit(url string) {
|
||||||
c.SetHeaderUrl()
|
c.SetHeaderUrl()
|
||||||
c.Draw()
|
c.Draw()
|
||||||
} else {
|
} else {
|
||||||
c.SetMessage("Still mulling how to handle binary files... come back soon", false)
|
c.SetMessage("The file is non-text: (o)pen or (w)rite to disk", false)
|
||||||
c.DrawMessage()
|
c.DrawMessage()
|
||||||
|
var ch rune
|
||||||
|
for {
|
||||||
|
ch = cui.Getch()
|
||||||
|
if ch == 'o' || ch == 'w' {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
switch ch {
|
||||||
|
case 'o':
|
||||||
|
mime := fmt.Sprintf("%s/%s", capsule.MimeMaj, capsule.MimeMin)
|
||||||
|
var term bool
|
||||||
|
if c.Options["terminalonly"] == "true" {
|
||||||
|
term = true
|
||||||
|
} else {
|
||||||
|
term = false
|
||||||
|
}
|
||||||
|
mcEntry, err := mc.FindMatch(mime, "view", term)
|
||||||
|
if err != nil {
|
||||||
|
c.SetMessage(err.Error(), true)
|
||||||
|
c.DrawMessage()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
file, err := ioutil.TempFile("/tmp/", "bombadillo-*.tmp")
|
||||||
|
if err != nil {
|
||||||
|
c.SetMessage("Unable to create temporary file for opening, aborting file open", true)
|
||||||
|
c.DrawMessage()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// defer os.Remove(file.Name())
|
||||||
|
file.Write([]byte(capsule.Content))
|
||||||
|
com, e := mcEntry.Command(file.Name())
|
||||||
|
if e != nil {
|
||||||
|
c.SetMessage(e.Error(), true)
|
||||||
|
c.DrawMessage()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
com.Stdin = os.Stdin
|
||||||
|
com.Stdout = os.Stdout
|
||||||
|
com.Stderr = os.Stderr
|
||||||
|
if c.Options["terminalonly"] == "true" {
|
||||||
|
cui.Clear("screen")
|
||||||
|
}
|
||||||
|
com.Run()
|
||||||
|
c.SetMessage("File opened by an appropriate program", true)
|
||||||
|
c.DrawMessage()
|
||||||
|
c.Draw()
|
||||||
|
case 'w':
|
||||||
|
nameSplit := strings.Split(u.Resource, "/")
|
||||||
|
filename := nameSplit[len(nameSplit) - 1]
|
||||||
|
c.saveFileFromData(capsule.Content, filename)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
case 3:
|
case 3:
|
||||||
c.SetMessage("[3] Redirect. Follow redirect? y or any other key for no", false)
|
c.SetMessage("[3] Redirect. Follow redirect? y or any other key for no", false)
|
||||||
|
|
|
@ -17,5 +17,6 @@ var defaultOptions = map[string]string{
|
||||||
"telnetcommand": "telnet",
|
"telnetcommand": "telnet",
|
||||||
"configlocation": userinfo.HomeDir,
|
"configlocation": userinfo.HomeDir,
|
||||||
"theme": "normal", // "normal", "inverted"
|
"theme": "normal", // "normal", "inverted"
|
||||||
|
"terminalonly": "true",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
23
main.go
23
main.go
|
@ -15,6 +15,7 @@ import (
|
||||||
|
|
||||||
"tildegit.org/sloum/bombadillo/config"
|
"tildegit.org/sloum/bombadillo/config"
|
||||||
"tildegit.org/sloum/bombadillo/cui"
|
"tildegit.org/sloum/bombadillo/cui"
|
||||||
|
"tildegit.org/sloum/mailcap"
|
||||||
)
|
)
|
||||||
|
|
||||||
const version = "2.0.0"
|
const version = "2.0.0"
|
||||||
|
@ -22,23 +23,7 @@ const version = "2.0.0"
|
||||||
var bombadillo *client
|
var bombadillo *client
|
||||||
var helplocation string = "gopher://colorfield.space:70/1/bombadillo-info"
|
var helplocation string = "gopher://colorfield.space:70/1/bombadillo-info"
|
||||||
var settings config.Config
|
var settings config.Config
|
||||||
|
var mc *mailcap.Mailcap
|
||||||
|
|
||||||
// func saveFileFromData(v gopher.View) error {
|
|
||||||
// quickMessage("Saving file...", false)
|
|
||||||
// urlsplit := strings.Split(v.Address.Full, "/")
|
|
||||||
// filename := urlsplit[len(urlsplit)-1]
|
|
||||||
// saveMsg := fmt.Sprintf("Saved file as %q", options["savelocation"]+filename)
|
|
||||||
// err := ioutil.WriteFile(options["savelocation"]+filename, []byte(strings.Join(v.Content, "")), 0644)
|
|
||||||
// if err != nil {
|
|
||||||
// quickMessage("Saving file...", true)
|
|
||||||
// return err
|
|
||||||
// }
|
|
||||||
|
|
||||||
// quickMessage(saveMsg, false)
|
|
||||||
// return nil
|
|
||||||
// }
|
|
||||||
|
|
||||||
|
|
||||||
func saveConfig() error {
|
func saveConfig() error {
|
||||||
var opts strings.Builder
|
var opts strings.Builder
|
||||||
|
@ -106,6 +91,10 @@ func main() {
|
||||||
}
|
}
|
||||||
args := flag.Args()
|
args := flag.Args()
|
||||||
|
|
||||||
|
// Build the mailcap db
|
||||||
|
// So that we can open files from gemini
|
||||||
|
mc = mailcap.NewMailcap()
|
||||||
|
|
||||||
cui.Tput("rmam") // turn off line wrapping
|
cui.Tput("rmam") // turn off line wrapping
|
||||||
cui.Tput("smcup") // use alternate screen
|
cui.Tput("smcup") // use alternate screen
|
||||||
defer cui.Exit()
|
defer cui.Exit()
|
||||||
|
|
Loading…
Reference in New Issue