summaryrefslogtreecommitdiff
path: root/actions.go
blob: afb8289a42d53f38438b67f192515d8c65b4519b (plain)
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
package main

import (
	"bytes"
	"errors"
	"fmt"
	"io"
	"net/url"
	"os"
	"os/exec"
	"path/filepath"
	"strconv"
	"strings"
	"syscall"

	"tildegit.org/tjp/sliderule"
)

var client sliderule.Client

func init() {
	client = sliderule.NewClient(nil)
}

var (
	ErrMustBeOnAPage      = errors.New("you must be on a page to do that, use the \"go\" command first")
	ErrNoPreviousHistory  = errors.New("there is no previous page in the history")
	ErrNoNextHistory      = errors.New("there is no page to go forward to")
	ErrOnLastLink         = errors.New("already on the last link")
	ErrOnFirstLink        = errors.New("already on the first link")
	ErrCantMoveRelative   = errors.New("next/previous only work after navigating to a link on a page")
	ErrAlreadyAtTop       = errors.New("already at the site root")
	ErrInvalidNumericLink = errors.New("no link with that number")
	ErrInvalidLink        = errors.New("that doesn't look like a valid URL")
	ErrSaveNeedsFilename  = errors.New("save requires a filename argument")
	ErrInvalidMarkArgs    = errors.New("mark what?")
	ErrInvalidTourArgs    = errors.New("tour what?")
)

func Navigate(state *BrowserState, target *url.URL, navIndex int, conf *Config) error {
	hist := state.History

	if hist.Url == nil || target.String() != hist.Url.String() {
		state.History = &History{
			Url:      target,
			Depth:    hist.Depth + 1,
			Back:     hist,
			NavIndex: navIndex,
		}
		hist.Forward = state.History
	}
	state.Modal = nil

	return Reload(state, conf)
}

func gopherURL(u *url.URL) (string, sliderule.Status) {
	if u.Scheme != "gopher" || len(u.Path) < 2 || !strings.HasPrefix(u.Path, "/") {
		return u.String(), 0
	}
	itemType := u.Path[1]
	clone := *u
	clone.Path = u.Path[2:]
	return clone.String(), sliderule.Status(itemType)
}

func Reload(state *BrowserState, conf *Config) error {
	if state.Url == nil {
		return ErrMustBeOnAPage
	}

	urlStr, _ := gopherURL(state.Url)
	response, err := client.Fetch(urlStr)
	if err != nil {
		return err
	}

	state.DocType = docType(state.Url, response)
	state.Body, err = io.ReadAll(response.Body)
	if err != nil {
		return err
	}

	state.Formatted, state.Links, err = parseDoc(state.DocType, state.Body, conf)
	if err != nil {
		return err
	}

	return print(state)
}

func back(state *BrowserState) error {
	if state.Back == nil {
		return ErrNoPreviousHistory
	}
	state.History = state.Back
	state.Modal = nil
	return nil
}

func Back(state *BrowserState) error {
	if err := back(state); err != nil {
		return err
	}

	return print(state)
}

func Forward(state *BrowserState) error {
	if state.Forward == nil {
		return ErrNoNextHistory
	}
	state.History = state.Forward
	state.Modal = nil

	return print(state)
}

func Next(state *BrowserState, conf *Config) error {
	switch state.NavIndex {
	case -1:
		return ErrCantMoveRelative
	case len(state.Back.Links) - 1:
		return ErrOnLastLink
	}

	index := state.NavIndex + 1

	if err := back(state); err != nil {
		return err
	}

	u := state.Url.ResolveReference(state.Links[index].Target)

	return Navigate(state, u, index, conf)
}

func Previous(state *BrowserState, conf *Config) error {
	switch state.NavIndex {
	case -1:
		return ErrCantMoveRelative
	case 0:
		return ErrOnFirstLink
	}

	index := state.NavIndex - 1

	if err := back(state); err != nil {
		return err
	}

	u := state.Url.ResolveReference(state.Links[index].Target)

	return Navigate(state, u, index, conf)
}

func Root(state *BrowserState, tilde bool, conf *Config) error {
	if state.Url == nil {
		return ErrMustBeOnAPage
	}

	u := *state.Url
	u.RawQuery = ""
	u.Fragment = ""

	base := "/"
	if u.Scheme == "gopher" {
		base = "/1/"
	}

	if tilde && strings.HasPrefix(u.Path, "/~") {
		u.Path = base + strings.SplitN(u.Path, "/", 3)[1] + "/"
	} else {
		u.Path = base
	}

	return Navigate(state, &u, -1, conf)
}

func Up(state *BrowserState, conf *Config) error {
	if state.Url == nil {
		return ErrMustBeOnAPage
	}

	u := *state.Url
	u.Path = strings.TrimSuffix(u.Path, "/")
	if u.Path == "" {
		return ErrAlreadyAtTop
	}

	u.Path = u.Path[:strings.LastIndex(u.Path, "/")+1]

	return Navigate(state, &u, -1, conf)
}

func Go(state *BrowserState, dest string, conf *Config) error {
	u, idx, err := parseURL(dest, state, conf.DefaultScheme)
	if err != nil {
		return err
	}

	return Navigate(state, u, idx, conf)
}

func parseURL(str string, state *BrowserState, defaultScheme string) (*url.URL, int, error) {
	if str == "." {
		if state.Url == nil {
			return nil, -1, ErrMustBeOnAPage
		}
		return state.Url, -1, nil
	}

	if strings.HasPrefix(str, "m:") {
		target, err := findMark(state, str[2:])
		if err != nil {
			return nil, -1, err
		}
		str = target
	} else if strings.HasPrefix(str, "t:") {
		i, err := strconv.Atoi(str[2:])
		if err != nil {
			return nil, -1, ErrInvalidLink
		}

		if i < 0 || i >= len(state.CurrentTour.Links) {
			return nil, -1, ErrInvalidTourPos
		}
		return state.CurrentTour.Links[i], -1, nil
	} else if strings.HasPrefix(str, "t[") {
		idx := strings.IndexByte(str, ']')
		if idx < 0 || idx >= len(str)-2 || str[idx+1] != ':' {
			return nil, -1, ErrInvalidLink
		}
		if i, err := strconv.Atoi(str[idx+2:]); err != nil {
			return nil, -1, ErrInvalidLink
		} else {
			tour, err := findTour(state, str[2:idx])
			if err != nil {
				return nil, -1, err
			}

			if i < 0 || i >= len(tour.Links) {
				return nil, -1, ErrInvalidTourPos
			}
			return tour.Links[i], -1, nil
		}
	}

	var u *url.URL
	i, err := strconv.Atoi(str)
	if err == nil {
		if len(state.Links) <= i {
			return nil, -1, ErrInvalidNumericLink
		}
		u = state.Links[i].Target
	} else {
		i = -1
		u, err = url.Parse(str)
		if err != nil {
			return nil, -1, ErrInvalidLink
		}
		if u.Scheme == "" {
			u.Scheme = defaultScheme
		}
	}
	if state.Url != nil {
		u = state.Url.ResolveReference(u)
	}

	if u.Hostname() == "" {
		return nil, -1, ErrInvalidLink
	}

	return u, i, nil
}

func print(state *BrowserState) error {
	if state.Quiet {
		return nil
	}

	if state.Body == nil && state.Modal == nil {
		return ErrMustBeOnAPage
	}
	out := []byte(state.Formatted)
	if state.Modal != nil {
		out = state.Modal
	}
	_, err := os.Stdout.Write(out)
	return err
}

func Print(state *BrowserState) error {
	q := state.Quiet
	defer func() { state.Quiet = q }()
	state.Quiet = false
	return print(state)
}

func Links(state *BrowserState, conf *Config) error {
	if state.Links == nil {
		return ErrMustBeOnAPage
	}

	buf := &bytes.Buffer{}
	for _, link := range state.Links {
		fmt.Fprintf(buf, "=> %s %s\n", link.Target.String(), link.Text)
	}
	formatted, _, err := parseDoc("text/gemini", buf.Bytes(), conf)
	if err != nil {
		return err
	}
	state.Modal = []byte(formatted)

	return Print(state)
}

func HistoryCmd(state *BrowserState) error {
	if state.History == nil {
		return ErrMustBeOnAPage
	}

	h := state.History
	i := 0
	for h.Forward != nil {
		h = h.Forward
		i += 1
	}

	buf := &bytes.Buffer{}
	j := 0
	for h.Url != nil {
		mark := ""
		if j == i {
			mark = "* "
		}
		fmt.Fprintf(buf, "%s%s\n", mark, h.Url.String())
		j += 1
		h = h.Back
	}
	state.Modal = buf.Bytes()

	return Print(state)
}

func Save(state *BrowserState, filename string, conf *Config) error {
	if state.Body == nil {
		return ErrMustBeOnAPage
	}
	if filename == "" {
		return ErrSaveNeedsFilename
	}

	p := filepath.Join(conf.DownloadFolder, filename)
	_, err := os.Stat(p)
	pbase := p
	i := 1
	for !errors.Is(err, syscall.ENOENT) {
		p = pbase + "-" + strconv.Itoa(i)
		_, err = os.Stat(p)
		i += 1
	}

	return os.WriteFile(p, state.Body, 0o644)
}

func Mark(state *BrowserState, args []string, conf *Config) error {
	switch args[0] {
	case "add":
		return MarkAdd(state, conf, args[1], args[2])
	case "go":
		return MarkGo(state, conf, args[1])
	case "list":
		return MarkList(state)
	}

	return ErrInvalidMarkArgs
}

func TourCmd(state *BrowserState, args []string, conf *Config) error {
	switch args[0] {
	case "add":
		if args[1] == "next" {
			return TourAddNext(state, conf, args[2:])
		}
		return TourAdd(state, conf, args[1:])
	case "show":
		return TourShow(state)
	case "select":
		return TourSelect(state, args[1])
	case "next":
		return TourNext(state, conf)
	case "previous":
		return TourPrevious(state, conf)
	case "clear":
		return TourClear(state)
	case "list":
		return TourList(state)
	case "go":
		return TourGo(state, conf, args[1])
	}

	return ErrInvalidTourArgs
}

func Pipe(state *BrowserState, cmdStr string) error {
	if state.Body == nil {
		return ErrMustBeOnAPage
	}

	sh, err := exec.LookPath("sh")
	if err != nil {
		return err
	}

	cmd := exec.Command(sh, "-c", cmdStr)
	cmd.Stdin = bytes.NewBuffer(state.Body)

	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr

	return cmd.Run()
}