-
Notifications
You must be signed in to change notification settings - Fork 0
/
find.go
138 lines (120 loc) · 3.14 KB
/
find.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
//go:build !windows
// +build !windows
// unix only
package lynx
import (
"bufio"
"os"
"path/filepath"
"strings"
"github.com/dvgamerr-app/go-kooky"
"github.com/dvgamerr-app/go-kooky/internal/cookies"
"github.com/dvgamerr-app/go-kooky/internal/netscape"
)
type lynxFinder struct{}
var _ kooky.CookieStoreFinder = (*lynxFinder)(nil)
func init() {
kooky.RegisterFinder(`lynx`, &lynxFinder{})
}
func (f *lynxFinder) FindCookieStores() ([]kooky.CookieStore, error) {
home, err := os.UserHomeDir()
if err != nil {
return nil, err
}
var ret []kooky.CookieStore
// the default value is ~/.lynx_cookies for most systems, but ~/cookies for MS-DOS
ret = append(
ret,
&cookies.CookieJar{
CookieStore: &netscape.CookieStore{
DefaultCookieStore: cookies.DefaultCookieStore{
BrowserStr: `lynx`,
IsDefaultProfileBool: true,
FileNameStr: filepath.Join(home, `.lynx_cookies`),
},
},
},
)
// parse config files so that we don't have to execute lynx -show_cfg
configFiles := []string{
filepath.Join(`/etc`, `lynx.cfg`),
filepath.Join(`/etc`, `lynx`, `lynx.cfg`), // Debian
}
// INCLUDE:/etc/lynx/local.cfg
// `/etc/lynx/lynx.cfg` includes `/etc/lynx/local.cfg` on Debian
// https://lynx.invisible-island.net/current/README.cookies
// COOKIE_FILE:/path/to/directory/.lynx_cookies // read file (?)
// COOKIE_SAVE_FILE:/path/to/directory/.lynx_cookies // save file
var includes, cookieFiles, cookieSaveFiles []string
parse := func(configFile string) error {
file, err := os.Open(configFile)
if err != nil {
return err
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, `INCLUDE:`) {
sp := strings.Split(line, `:`)
if len(sp) == 2 {
includes = append(includes, sp[1])
}
}
if strings.HasPrefix(line, `COOKIE_FILE:`) {
sp := strings.Split(line, `:`)
if len(sp) == 2 {
cookieFiles = append(cookieFiles, sp[1])
}
}
if strings.HasPrefix(line, `COOKIE_SAVE_FILE:`) {
sp := strings.Split(line, `:`)
if len(sp) == 2 {
cookieSaveFiles = append(cookieSaveFiles, sp[1])
}
}
}
return nil
}
configFileLoop:
for _, configFile := range configFiles {
_ = parse(configFile)
}
if len(includes) > 0 {
configFiles = includes
includes = nil
goto configFileLoop
}
var primCookieFile string
if len(cookieFiles) > 0 {
primCookieFile = cookieFiles[len(cookieFiles)-1]
}
cookieMap := make(map[string]struct{})
for _, cookieFile := range append(cookieSaveFiles, cookieFiles...) {
if _, exists := cookieMap[cookieFile]; exists {
continue
}
cookieMap[cookieFile] = struct{}{}
}
{
last := len(cookieMap) - 1
i := 0
for cookieFile := range cookieMap {
ret = append(
ret,
&cookies.CookieJar{
CookieStore: &netscape.CookieStore{
DefaultCookieStore: cookies.DefaultCookieStore{
BrowserStr: `lynx`,
// last one probably overwrites earlier configuration
IsDefaultProfileBool: cookieFile == primCookieFile || i == last,
FileNameStr: cookieFile,
},
},
},
)
i++
}
}
return ret, nil
}