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
424
|
import tables
import options
import os
import streams
import buffer/cell
import config/toml
import io/request
import io/urlfilter
import js/javascript
import js/regex
import types/color
import types/cookie
import types/referer
import types/url
import utils/twtstr
type
ColorMode* = enum
MONOCHROME, ANSI, EIGHT_BIT, TRUE_COLOR
FormatMode* = set[FormatFlags]
ActionMap = Table[string, string]
StaticSiteConfig = object
url: Option[string]
host: Option[string]
subst: Option[string]
cookie: Option[bool]
thirdpartycookie: seq[string]
sharecookiejar: Option[string]
refererfrom*: Option[bool]
scripting: Option[bool]
StaticOmniRule = object
match: string
subst: string
SiteConfig* = object
url*: Option[Regex]
host*: Option[Regex]
subst*: (proc(s: URL): Option[URL])
cookie*: Option[bool]
thirdpartycookie*: seq[Regex]
sharecookiejar*: Option[string]
refererfrom*: Option[bool]
scripting*: Option[bool]
OmniRule* = object
match*: Regex
subst*: (proc(s: string): Option[string])
Config* = ref ConfigObj
ConfigObj* = object
searchwrap* {.jsget, jsset.}: bool
maxredirect*: int
prependhttps* {.jsget, jsset.}: bool
termreload*: bool
nmap*: ActionMap
lemap*: ActionMap
stylesheet* {.jsget, jsset.}: string
startup*: string
ambiguous_double*: bool
hlcolor*: RGBAColor
headless*: bool
colormode*: Option[ColorMode]
formatmode*: Option[FormatMode]
noformatmode*: FormatMode
altscreen*: Option[bool]
mincontrast*: int
editor*: string
tmpdir*: string
siteconf: seq[StaticSiteConfig]
omnirules: seq[StaticOmniRule]
forceclear*: bool
emulateoverline*: bool
visualhome*: string
BufferConfig* = object
userstyle*: string
filter*: URLFilter
cookiejar*: CookieJar
headers*: HeaderList
refererfrom*: bool
referrerpolicy*: ReferrerPolicy
scripting*: bool
ForkServerConfig* = object
tmpdir*: string
ambiguous_double*: bool
const DefaultHeaders* = {
"User-Agent": "chawan",
"Accept": "text/html,text/*;q=0.5",
"Accept-Language": "en;q=1.0",
"Pragma": "no-cache",
"Cache-Control": "no-cache",
}.toTable().newHeaderList()[]
func getForkServerConfig*(config: Config): ForkServerConfig =
return ForkServerConfig(
tmpdir: config.tmpdir,
ambiguous_double: config.ambiguous_double
)
proc getBufferConfig*(config: Config, location: URL, cookiejar: CookieJar = nil,
headers: HeaderList = nil, refererfrom = false, scripting = false): BufferConfig =
result = BufferConfig(
userstyle: config.stylesheet,
filter: newURLFilter(scheme = some(location.scheme), default = true),
cookiejar: cookiejar,
headers: headers,
refererfrom: refererfrom,
scripting: scripting
)
new(result.headers)
result.headers[] = DefaultHeaders
proc getSiteConfig*(config: Config, jsctx: JSContext): seq[SiteConfig] =
for sc in config.siteconf:
var conf = SiteConfig(
cookie: sc.cookie,
scripting: sc.scripting,
sharecookiejar: sc.sharecookiejar,
refererfrom: sc.refererfrom
)
if sc.url.isSome:
conf.url = compileRegex(sc.url.get, 0)
elif sc.host.isSome:
conf.host = compileRegex(sc.host.get, 0)
for rule in sc.thirdpartycookie:
conf.thirdpartycookie.add(compileRegex(rule, 0).get)
if sc.subst.isSome:
let fun = jsctx.eval(sc.subst.get, "<siteconf>", JS_EVAL_TYPE_GLOBAL)
let f = getJSFunction[URL, URL](jsctx, fun)
conf.subst = f.get
result.add(conf)
proc getOmniRules*(config: Config, jsctx: JSContext): seq[OmniRule] =
for rule in config.omnirules:
let re = compileRegex(rule.match, 0)
var conf = OmniRule(
match: re.get
)
let fun = jsctx.eval(rule.subst, "<siteconf>", JS_EVAL_TYPE_GLOBAL)
let f = getJSFunction[string, string](jsctx, fun)
conf.subst = f.get
result.add(conf)
func getRealKey(key: string): string =
var realk: string
var control = 0
var meta = 0
var skip = false
for c in key:
if c == '\\':
skip = true
elif skip:
realk &= c
skip = false
elif c == 'M' and meta == 0:
inc meta
elif c == 'C' and control == 0:
inc control
elif c == '-' and control == 1:
inc control
elif c == '-' and meta == 1:
inc meta
elif meta == 1:
realk &= 'M' & c
meta = 0
elif control == 1:
realk &= 'C' & c
control = 0
else:
if meta == 2:
realk &= '\e'
meta = 0
if control == 2:
realk &= getControlChar(c)
control = 0
else:
realk &= c
if control == 1:
realk &= 'C'
if meta == 1:
realk &= 'M'
return realk
func constructActionTable*(origTable: Table[string, string]): Table[string, string] =
var strs: seq[string]
for k in origTable.keys:
let realk = getRealKey(k)
var teststr = ""
for c in realk:
teststr &= c
strs.add(teststr)
for k, v in origTable:
let realk = getRealKey(k)
var teststr = ""
for c in realk:
teststr &= c
if strs.contains(teststr):
result[teststr] = "client.feedNext()"
result[realk] = v
proc readUserStylesheet(dir, file: string): string =
if file.len == 0:
return ""
if file[0] == '~' or file[0] == '/':
var f: File
if f.open(expandPath(file)):
result = f.readAll()
f.close()
else:
var f: File
if f.open(dir / file):
result = f.readAll()
f.close()
proc parseConfig(config: Config, dir: string, stream: Stream)
proc parseConfig*(config: Config, dir: string, s: string)
proc loadConfig*(config: Config, s: string) {.jsfunc.} =
let s = if s.len > 0 and s[0] == '/':
s
else:
getCurrentDir() / s
if not fileExists(s): return
config.parseConfig(parentDir(s), newFileStream(s))
proc bindPagerKey*(config: Config, key, action: string) {.jsfunc.} =
let k = getRealKey(key)
config.nmap[k] = action
var teststr = ""
for c in k:
teststr &= c
if teststr notin config.nmap:
config.nmap[teststr] = "client.feedNext()"
proc bindLineKey*(config: Config, key, action: string) {.jsfunc.} =
let k = getRealKey(key)
config.lemap[k] = action
var teststr = ""
for c in k:
teststr &= c
if teststr notin config.nmap:
config.lemap[teststr] = "client.feedNext()"
proc parseConfig(config: Config, dir: string, t: TomlValue) =
for k, v in t:
case k
of "include":
if v.vt == VALUE_STRING:
when nimvm:
config.parseConfig(dir, staticRead(dir / v.s))
else:
config.parseConfig(dir, newFileStream(dir / v.s))
elif t.vt == VALUE_ARRAY:
for v in t.a:
when nimvm:
config.parseConfig(dir, staticRead(dir / v.s))
else:
config.parseConfig(dir, newFileStream(dir / v.s))
of "search":
for k, v in v:
case k
of "wrap":
config.searchwrap = v.b
of "start":
for k, v in v:
case k
of "visual-home":
config.visualhome = v.s
of "run-script":
config.startup = v.s
of "headless":
config.headless = v.b
of "network":
for k, v in v:
case k
of "max-redirect":
config.maxredirect = int(v.i)
of "prepend-https":
config.prependhttps = v.b
of "page":
for k, v in v:
config.nmap[getRealKey(k)] = v.s
of "line":
for k, v in v:
config.lemap[getRealKey(k)] = v.s
of "css":
for k, v in v:
case k
of "include":
case v.vt
of VALUE_STRING:
config.stylesheet &= readUserStylesheet(dir, v.s)
of VALUE_ARRAY:
for child in v.a:
config.stylesheet &= readUserStylesheet(dir, v.s)
else: discard
of "inline":
config.stylesheet &= v.s
of "display":
template get_format_mode(v: TomlValue): FormatMode =
var mode: FormatMode
for vv in v.a:
case vv.s
of "bold": mode.incl(FLAG_BOLD)
of "italic": mode.incl(FLAG_ITALIC)
of "underline": mode.incl(FLAG_UNDERLINE)
of "reverse": mode.incl(FLAG_REVERSE)
of "strike": mode.incl(FLAG_STRIKE)
of "overline": mode.incl(FLAG_OVERLINE)
of "blink": mode.incl(FLAG_BLINK)
mode
for k, v in v:
case k
of "alt-screen":
if v.vt == VALUE_BOOLEAN:
config.altscreen = some(v.b)
elif v.vt == VALUE_STRING and v.s == "auto":
config.altscreen = none(bool)
of "color-mode":
case v.s
of "auto": config.colormode = none(ColorMode)
of "monochrome": config.colormode = some(MONOCHROME)
of "ansi": config.colormode = some(ANSI)
of "8bit": config.colormode = some(EIGHT_BIT)
of "24bit": config.colormode = some(TRUE_COLOR)
of "format-mode":
if v.vt == VALUE_STRING and v.s == "auto":
config.formatmode = none(FormatMode)
elif v.vt == VALUE_ARRAY:
config.formatmode = some(get_format_mode v)
of "no-format-mode":
config.noformatmode = get_format_mode v
of "highlight-color":
config.hlcolor = parseRGBAColor(v.s).get
of "double-width-ambiguous":
config.ambiguous_double = v.b
of "minimum-contrast":
config.mincontrast = int(v.i)
of "force-clear": config.forceclear = v.b
of "emulate-overline": config.emulateoverline = v.b
of "external":
for k, v in v:
case k
of "editor": config.editor = v.s
of "tmpdir": config.tmpdir = v.s
of "siteconf":
for v in v:
var conf = StaticSiteConfig()
for k, v in v:
case k
of "url": conf.url = some(v.s)
of "host": conf.host = some(v.s)
of "rewrite-url": conf.subst = some(v.s)
of "referer-from": conf.refererfrom = some(v.b)
of "cookie": conf.cookie = some(v.b)
of "third-party-cookie":
if v.vt == VALUE_STRING:
conf.thirdpartycookie = @[v.s]
else:
for v in v.a:
conf.thirdpartycookie.add(v.s)
of "share-cookie-jar": conf.sharecookiejar = some(v.s)
of "scripting": conf.scripting = some(v.b)
assert conf.url.isSome != conf.host.isSome
config.siteconf.add(conf)
of "omnirule":
if v.vt == VALUE_ARRAY and v.a.len == 0:
config.omnirules.setLen(0)
else:
for v in v:
var rule = StaticOmniRule()
for k, v in v:
case k
of "match": rule.match = v.s
of "substitute-url": rule.subst = v.s
if rule.match != "":
assert rule.subst != "", "Unspecified substitution for rule " & rule.match
config.omnirules.add(rule)
proc parseConfig(config: Config, dir: string, stream: Stream) =
config.parseConfig(dir, parseToml(stream))
proc parseConfig*(config: Config, dir: string, s: string) =
config.parseConfig(dir, newStringStream(s))
proc staticReadConfig(): ConfigObj =
var config = new(Config)
config.parseConfig("res", staticRead"res/config.toml")
return config[]
const defaultConfig = staticReadConfig()
proc readConfig(config: Config, dir: string) =
let fs = newFileStream(dir / "config.toml")
if fs != nil:
config.parseConfig(dir, fs)
proc getNormalAction*(config: Config, s: string): string =
if config.nmap.hasKey(s):
return config.nmap[s]
return ""
proc getLinedAction*(config: Config, s: string): string =
if config.lemap.hasKey(s):
return config.lemap[s]
return ""
proc readConfig*(): Config =
new(result)
result[] = defaultConfig
when defined(debug):
result.readConfig(getCurrentDir() / "res")
result.readConfig(getConfigDir() / "chawan")
proc addConfigModule*(ctx: JSContext) =
ctx.registerType(Config)
|