forked from shingetsu-gou/shingetsu-gou
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gateway.go
682 lines (632 loc) · 16.8 KB
/
gateway.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
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
/*
* Copyright (c) 2015, Shinya Yagyu
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package cgi
import (
"fmt"
"html"
"html/template"
"log"
"net/http"
"os"
"path"
"regexp"
"sort"
"strings"
"time"
"github.com/shingetsu-gou/shingetsu-gou/thread"
"github.com/shingetsu-gou/shingetsu-gou/util"
"golang.org/x/text/language"
)
//message hold string map.
type message map[string]string
//newMessage reads from the file excpet #comment and stores them with url unescaping value.
func newMessage(file string) message {
m := make(map[string]string)
re := regexp.MustCompile(`^\s*#`)
err := util.EachLine(file, func(line string, i int) error {
var err error
if re.MatchString(line) {
return nil
}
buf := strings.Split(line, "<>")
if len(buf) == 2 {
buf[1] = html.UnescapeString(buf[1])
m[buf[0]] = buf[1]
}
return err
})
if err != nil {
log.Println(file, err)
return nil
}
return m
}
//searchMessage parse Accept-Language header ,selects most-weighted(biggest q)
//language ,reads the associated message file, and creates and returns message obj.
func searchMessage(acceptLanguage, filedir string) message {
const defaultLanguage = "en" // Language code (see RFC3066)
var lang []string
if acceptLanguage != "" {
tags, _, err := language.ParseAcceptLanguage(acceptLanguage)
if err != nil {
log.Println(err)
} else {
for _, tag := range tags {
lang = append(lang, tag.String())
}
}
}
lang = append(lang, defaultLanguage)
for _, l := range lang {
slang := strings.Split(l, "-")[0]
for _, j := range []string{l, slang} {
file := path.Join(filedir, "message-"+j+".txt")
if util.IsFile(file) {
return newMessage(file)
}
}
}
return nil
}
//GatewayLink is a struct for gateway_link.txt
type GatewayLink struct {
Message message
CGIname string
Command string
Description string
htemplate *util.Htemplate
}
//Render renders "gateway_link.txt" and returns its resutl string which is not escaped in template.
//GatewayLink.Message must be setted up previously.
func (c GatewayLink) Render(cginame, command string) template.HTML {
c.CGIname = cginame
c.Command = command
c.Description = c.Message["desc_"+command]
return template.HTML(c.htemplate.ExecuteTemplate("gateway_link", c))
}
//ListItem is for list_item.txt
type ListItem struct {
Cache *thread.Cache
CacheSize int64
Title string
Tags thread.Tagslice
Sugtags []*thread.Tag
Target string
Remove bool
IsAdmin bool
StrOpts string
GatewayCGI string
ThreadURL string
Message message
CacheInfo *thread.CacheInfo
filter string
tag string
suggestedTagTable *thread.SuggestedTagTable
htemplate *util.Htemplate
}
//checkCache checks cache ca has specified tag and datfile doesn't contains filterd string.
func (l *ListItem) checkCache(ca *thread.Cache, target string) (string, bool) {
x := util.FileDecode(ca.Datfile)
if x == "" {
return "", false
}
if l.filter != "" && !strings.Contains(l.filter, strings.ToLower(x)) {
return "", false
}
if l.tag != "" {
switch {
case ca.HasTagstr(strings.ToLower(l.tag)):
case target == "recent" && l.suggestedTagTable.HasTagstr(ca.Datfile, strings.ToLower(l.tag)):
default:
return "", false
}
}
return x, true
}
//Render renders "list_items.txt" and returns its result string which is not escaped in template.
//ListItem.IsAdmin,filter,tag,Message must be setted up previously.
func (l ListItem) Render(ca *thread.Cache, remove bool, target string, search bool) template.HTML {
x, ok := l.checkCache(ca, target)
if !ok {
return template.HTML("")
}
x = util.EscapeSpace(x)
var strOpts string
if search {
strOpts = "?search_new_file=yes"
}
var sugtags []*thread.Tag
if target == "recent" {
strTags := make([]string, ca.LenTags())
for i, v := range ca.GetTags() {
strTags[i] = strings.ToLower(v.Tagstr)
}
for _, st := range l.suggestedTagTable.Get(ca.Datfile, nil) {
if !util.HasString(strTags, strings.ToLower(st.Tagstr)) {
sugtags = append(sugtags, st)
}
}
}
l.CacheInfo = ca.ReadInfo()
l.Cache = ca
l.Title = x
l.Tags = ca.GetTags()
l.Sugtags = sugtags
l.Target = target
l.Remove = remove
l.StrOpts = strOpts
l.GatewayCGI = GatewayURL
l.ThreadURL = ThreadURL
return template.HTML(l.htemplate.ExecuteTemplate("list_item", l))
}
//CGIConfig is config for CGI struct.
type CGIConfig struct {
FileDir string
Docroot string
MaxConnection int
ServerName string
ReAdminStr string
ReFriendStr string
ReVisitorStr string
Htemplate *util.Htemplate
UserTag *thread.UserTag
SuggestedTagTable *thread.SuggestedTagTable
Version string
EnableEmbed bool
}
//cgi is a base class for all http handlers.
type cgi struct {
*CGIConfig
m message
jc *jsCache
req *http.Request
wr http.ResponseWriter
filter string
tag string
}
//CGICfg is cfg for CGI struct.
//it must be set before using.
var CGICfg *CGIConfig
//newCGI reads messages file, and set params , returns cgi obj.
//cgi obj is cached.
func newCGI(w http.ResponseWriter, r *http.Request) *cgi {
if CGICfg == nil {
log.Fatal("must set CGICfg")
}
c := &cgi{
CGIConfig: CGICfg,
jc: newJsCache(CGICfg.Docroot),
wr: w,
m: searchMessage(r.Header.Get("Accept-Language"), CGICfg.FileDir),
req: r,
}
err := r.ParseForm()
if err != nil {
log.Println(err)
return nil
}
return c
}
//host returns servername or host in http header.
func (c *cgi) host() string {
host := c.ServerName
if host == "" {
host = c.req.Host
}
return host
}
//isAdmin returns tur if matches admin regexp setted in config file.
func (c *cgi) isAdmin() bool {
m, err := regexp.MatchString(c.ReAdminStr, c.req.RemoteAddr)
if err != nil {
log.Fatal(err)
}
return m
}
//isFriend returns tur if matches friend regexp setted in config file.
func (c *cgi) isFriend() bool {
m, err := regexp.MatchString(c.ReFriendStr, c.req.RemoteAddr)
if err != nil {
log.Fatal(err)
}
return m
}
//isVisitor returns tur if matches visitor regexp setted in config file.
func (c *cgi) isVisitor() bool {
m, err := regexp.MatchString(c.ReVisitorStr, c.req.RemoteAddr)
if err != nil {
log.Fatal(err)
}
return m
}
//path returns path part of url.
//e.g. /thread.cgi/hoe/moe -> hoe/moe
func (c *cgi) path() string {
p := strings.Split(c.req.URL.Path, "/")
// /thread.cgi/hoe
// 0/ 1/ 2
var path string
if len(p) > 1 {
path = strings.Join(p[2:], "/")
}
return path
}
//extentions reads files with suffix in root dir and return them.
//if __merged file exists return it when useMerged=true.
func (c *cgi) extension(suffix string, useMerged bool) []string {
var filename []string
var merged string
d, err := util.AssetDir("www")
if err != nil {
log.Fatal(err)
}
for _, fname := range d {
if util.HasExt(fname, suffix) {
filename = append(filename, fname)
}
}
if util.IsDir(c.Docroot) {
err = util.EachFiles(c.Docroot, func(f os.FileInfo) error {
i := f.Name()
if util.HasExt(i, suffix) {
if !util.HasString(filename, i) {
filename = append(filename, i)
}
} else {
if useMerged && i == "__merged."+suffix {
merged = i
}
}
return nil
})
if err != nil {
log.Println(err)
}
if merged != "" {
return []string{merged}
}
}
sort.Strings(filename)
return filename
}
//footer render footer.
func (c *cgi) footer(menubar *Menubar) {
g := struct {
Menubar *Menubar
Version string
}{
menubar,
c.Version,
}
c.Htemplate.RenderTemplate("footer", g, c.wr)
}
//rfc822Time convers stamp to "2006-01-02 15:04:05"
func (c *cgi) rfc822Time(stamp int64) string {
return time.Unix(stamp, 0).Format("2006-01-02 15:04:05")
}
//printParagraph render paragraph.txt,just print constents.
//contentsKey must be a key of Message map.
func (c *cgi) printParagraph(contentsKey string) {
g := struct {
Contents template.HTML
}{
Contents: template.HTML(c.m[contentsKey]),
}
c.Htemplate.RenderTemplate("paragraph", g, c.wr)
}
//Menubar is var set for menubar.txt
type Menubar struct {
GatewayLink
GatewayCGI string
Message message
ID string
RSS string
IsAdmin bool
IsFriend bool
}
//mekaMenubar makes and returns *Menubar obj.
func (c *cgi) makeMenubar(id, rss string) *Menubar {
g := &Menubar{
GatewayLink{
Message: c.m,
htemplate: c.Htemplate,
},
GatewayURL,
c.m,
id,
rss,
c.isAdmin(),
c.isFriend(),
}
return g
}
//header renders header.txt with cookie.
func (c *cgi) header(title, rss string, cookie []*http.Cookie, denyRobot bool) {
if rss == "" {
rss = GatewayURL + "/rss"
}
var js []string
if c.req.FormValue("__debug_js") != "" {
js = c.extension("js", false)
} else {
c.jc.update()
}
h := struct {
Message message
RootPath string
Title string
RSS string
Mergedjs *jsCache
JS []string
CSS []string
Menubar *Menubar
DenyRobot bool
Dummyquery int64
ThreadCGI string
AppliType string
}{
c.m,
"/",
title,
rss,
c.jc,
js,
c.extension("css", false),
c.makeMenubar("top", rss),
denyRobot,
time.Now().Unix(),
ThreadURL,
"thread",
}
if cookie != nil {
for _, co := range cookie {
http.SetCookie(c.wr, co)
}
}
c.Htemplate.RenderTemplate("header", h, c.wr)
}
//resAnchor returns a href string with url.
func (c *cgi) resAnchor(id, appli string, title string, absuri bool) string {
title = util.StrEncode(title)
var prefix, innerlink string
if absuri {
prefix = "http://" + c.host()
} else {
innerlink = " class=\"innerlink\""
}
return fmt.Sprintf("<a href=\"%s%s%s%s/%s\"%s>", prefix, appli, "/", title, id, innerlink)
}
//htmlFormat converts plain text to html , including converting link string to <a href="link">.
func (c *cgi) htmlFormat(plain, appli string, title string, absuri bool) string {
buf := strings.Replace(plain, "<br>", "\n", -1)
buf = strings.Replace(buf, "\t", " ", -1)
buf = util.Escape(buf)
regLink := regexp.MustCompile(`https?://[^\x00-\x20"'\(\)<>\[\]\x7F-\xFF]{2,}`)
if c.EnableEmbed {
var strs []string
for _, str := range strings.Split(buf, "<br>") {
s := regLink.ReplaceAllString(str, `<a href="$0">$0</a>`)
strs = append(strs, s)
for _, link := range regLink.FindAllString(str, -1) {
e := util.EmbedURL(link)
if e != "" {
strs = append(strs, e)
strs = append(strs, "")
}
}
}
buf = strings.Join(strs, "<br>")
}
reg := regexp.MustCompile(">>[0-9a-f]{8}")
buf = reg.ReplaceAllStringFunc(buf, func(str string) string {
regg := regexp.MustCompile("(>>)([0-9a-f]{8})")
id := regg.ReplaceAllString(str, "$2")
return regg.ReplaceAllString(str, c.resAnchor(id, appli, title, absuri)+"$1$2</a>")
})
reg = regexp.MustCompile(`\[\[([^<>]+?)\]\]`)
tmp := reg.ReplaceAllStringFunc(buf, func(str string) string {
bl := c.bracketLink(str[2:len(str)-2], appli, absuri)
return bl
})
return util.EscapeSpace(tmp)
}
//bracketLink convert ling string to [[link]] string with href tag.
// if not thread and rec link, simply return [[link]]
func (c *cgi) bracketLink(link, appli string, absuri bool) string {
var prefix string
if absuri {
prefix = "http://" + c.host()
}
reg := regexp.MustCompile("^/(thread)/([^/]+)/([0-9a-f]{8})$")
if m := reg.FindStringSubmatch(link); m != nil {
url := prefix + ThreadURL + "/" + util.StrEncode(m[2]) + "/" + m[3]
return "<a href=\"" + url + "\" class=\"reclink\">[[" + link + "]]</a>"
}
reg = regexp.MustCompile("^/(thread)/([^/]+)$")
if m := reg.FindStringSubmatch(link); m != nil {
uri := prefix + ThreadURL + "/" + util.StrEncode(m[2])
return "<a href=\"" + uri + "\">[[" + link + "]]</a>"
}
reg = regexp.MustCompile("^([^/]+)/([0-9a-f]{8})$")
if m := reg.FindStringSubmatch(link); m != nil {
uri := prefix + appli + "/" + util.StrEncode(m[1]) + "/" + m[2]
return "<a href=\"" + uri + "\" class=\"reclink\">[[" + link + "]]</a>"
}
reg = regexp.MustCompile("^([^/]+)$")
if m := reg.FindStringSubmatch(link); m != nil {
uri := prefix + appli + "/" + util.StrEncode(m[1])
return "<a href=\"" + uri + "\">[[" + link + "]]</a>"
}
return "[[" + link + "]]"
}
//removeFileForm render remove_form_form page.
func (c *cgi) removeFileForm(ca *thread.Cache, title string) {
s := struct {
Cache *thread.Cache
CacheSize int64
Title string
IsAdmin bool
AdminCGI string
Message message
}{
ca,
ca.ReadInfo().Size,
title,
c.isAdmin(),
AdminURL,
c.m,
}
c.Htemplate.RenderTemplate("remove_file_form", s, c.wr)
}
//printJump render jump (redirect)page.
func (c *cgi) printJump(next string) {
s := struct {
Next template.HTML
}{
template.HTML(next),
}
c.Htemplate.RenderTemplate("jump", s, c.wr)
}
//print302 renders jump page(meaning found and redirect)
func (c *cgi) print302(next string) {
c.header("Loading...", "", nil, false)
c.printJump(next)
c.footer(nil)
}
//print403 renders 403 forbidden page with jump page.
func (c *cgi) print403() {
c.header(c.m["403"], "", nil, true)
c.printParagraph("403_body")
c.footer(nil)
}
//print404 render the 404 page.
//if ca!=nil also renders info page of removing cache.
func (c *cgi) print404(ca *thread.Cache, id string) {
c.header(c.m["404"], "", nil, true)
c.printParagraph("404_body")
if ca != nil {
c.removeFileForm(ca, "")
}
c.footer(nil)
}
//checkVisitor returns true if visitor is permitted.
func (c *cgi) checkVisitor() bool {
return c.isAdmin() || c.isFriend() || c.isVisitor()
}
//hasAuth auth returns true if visitor is admin or friend.
func (c *cgi) hasAuth() bool {
return c.isAdmin() || c.isFriend()
}
//printIndexList renders index_list.txt which renders threads in cachelist.
func (c *cgi) printIndexList(cl []*thread.Cache, target string, footer bool, searchNewFile bool) {
s := struct {
Target string
Filter string
Tag string
Taglist thread.Tagslice
Cachelist []*thread.Cache
GatewayCGI string
AdminCGI string
Message message
SearchNewFile bool
IsAdmin bool
IsFriend bool
GatewayLink
ListItem
}{
target,
c.filter,
c.tag,
c.UserTag.Get(),
cl,
GatewayURL,
AdminURL,
c.m,
searchNewFile,
c.isAdmin(),
c.isFriend(),
GatewayLink{
htemplate: c.Htemplate,
Message: c.m,
},
ListItem{
htemplate: c.Htemplate,
suggestedTagTable: c.SuggestedTagTable,
IsAdmin: c.isAdmin(),
filter: c.filter,
tag: c.tag,
Message: c.m,
},
}
c.Htemplate.RenderTemplate("index_list", s, c.wr)
if footer {
c.printNewElementForm()
c.footer(nil)
}
}
//printNewElementForm renders new_element_form.txt for posting new thread.
func (c *cgi) printNewElementForm() {
const titleLimit = 30 //Charactors
if !c.isAdmin() && !c.isFriend() {
return
}
s := struct {
Datfile string
CGIname string
Message message
TitleLimit int
IsAdmin bool
}{
"",
GatewayURL,
c.m,
titleLimit,
c.isAdmin(),
}
c.Htemplate.RenderTemplate("new_element_form", s, c.wr)
}
//checkGetCache return true
//if visitor is firend or admin and user-agent is not robot.
func (c *cgi) checkGetCache() bool {
const (
robot = "Google|bot|Yahoo|archiver|Wget|Crawler|Yeti|Baidu"
)
if !c.hasAuth() {
return false
}
agent := c.req.Header.Get("User-Agent")
reg, err := regexp.Compile(robot)
if err != nil {
log.Println(err)
return true
}
if reg.MatchString(agent) {
return false
}
return true
}