-
Notifications
You must be signed in to change notification settings - Fork 0
/
gopher.go
145 lines (132 loc) · 3.06 KB
/
gopher.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"bufio"
"fmt"
"net"
"net/url"
"strings"
)
type GopherResponse struct {
bodyReader *bufio.Reader
conn *net.Conn
connClosed bool
gophertype string
}
var gophertypes = map[string]string{
"0": "TXT",
"1": "DIR",
"3": "ERR",
"4": "BIN",
"5": "DOS",
"6": "UUE",
"7": "SEARCH",
"8": "TEL",
"9": "BIN",
"g": "GIF",
"G": "GMI",
"h": "HTML",
"I": "IMG",
"p": "PNG",
"s": "SND",
"S": "SSH",
"T": "TEL",
}
// GopherParsedURL fetches u and returns a GopherResponse
func GopherParsedURL(u *url.URL) (res *GopherResponse, err error) {
host := u.Host
if u.Port() == "" {
host += ":70"
}
// Connect to server, no TLS
conn, err := net.Dial("tcp", host)
if err != nil {
return
}
fullpath := strings.TrimPrefix(u.Path, "/")
if fullpath == "" || fullpath == "1" {
fullpath = "1/"
}
pathParts := strings.SplitN(fullpath, "/", 2)
gophertype := pathParts[0]
fmt.Fprintf(conn, "%s\n", "/"+pathParts[1])
reader := bufio.NewReader(conn)
res = &GopherResponse{
bodyReader: reader,
conn: &conn,
connClosed: false,
gophertype: gophertype,
}
return
}
func (c *Client) ParseGophermap(page *Page) string {
var linkStyle = c.style.gmiLink.Sprint
body := string(page.bodyBytes)
rendered := []string{}
dedents := []int{}
for _, line := range strings.Split(body, "\n") {
line = strings.Trim(line, "\r\n")
if line == "." {
rendered = append(rendered, "")
dedents = append(dedents, 0)
continue
}
columns := strings.Split(line, "\t")
var title string
if len(columns[0]) > 1 {
title = columns[0][1:]
} else if len(columns[0]) == 1 {
title = ""
} else {
title = ""
columns[0] = "i"
}
if len(columns) < 4 || strings.HasPrefix(columns[0], "i") {
dedents = append(dedents, 0)
rendered = append(rendered, title)
} else {
host := columns[2]
port := columns[3]
gtype := string(columns[0][0])
path := columns[1]
link := fmt.Sprintf("gopher://%s:%s/%s%s", host, port, gtype, path)
switch gtype {
case "8", "T":
link = fmt.Sprintf("telnet://%s:%s", host, port)
case "G":
link = fmt.Sprintf("gemini://%s:%s%s", host, port, path)
case "h":
u, tf := isWebLink(path)
if tf {
if strings.Index(u, "://") > 0 {
link = u
} else {
link = fmt.Sprintf("http://%s", u)
}
} else {
link = fmt.Sprintf("gopher://%s:%s/h%s", host, port, path)
}
case "7":
c.inputLinks = append(c.inputLinks, len(c.links))
}
c.links = append(c.links, link)
gophertype := "(" + getGophertype(string(columns[0][0])) + ")"
linkLine := fmt.Sprintf("%s [%d] %s", gophertype, len(c.links), linkStyle(title))
dedents = append(dedents, len(gophertype)+2)
rendered = append(rendered, linkLine)
}
}
return c.Centered(rendered, 0, dedents)
}
func isWebLink(resource string) (string, bool) {
split := strings.SplitN(resource, ":", 2)
if first := strings.ToUpper(split[0]); first == "URL" && len(split) > 1 {
return split[1], true
}
return "", false
}
func getGophertype(t string) string {
if val, ok := gophertypes[t]; ok {
return val
}
return "???"
}