forked from shingetsu-gou/shingetsu-gou
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mch_datd.go
431 lines (394 loc) · 12.2 KB
/
mch_datd.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
/*
* 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 (
"bytes"
"errors"
"fmt"
"html"
"log"
"net/http"
"regexp"
"strconv"
"strings"
"time"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/shingetsu-gou/shingetsu-gou/cfg"
"github.com/shingetsu-gou/shingetsu-gou/mch"
"github.com/shingetsu-gou/shingetsu-gou/mch/keylib"
"github.com/shingetsu-gou/shingetsu-gou/record"
"github.com/shingetsu-gou/shingetsu-gou/tag/user"
"github.com/shingetsu-gou/shingetsu-gou/thread"
"github.com/shingetsu-gou/shingetsu-gou/thread/download"
"github.com/shingetsu-gou/shingetsu-gou/updateque"
"github.com/shingetsu-gou/shingetsu-gou/util"
)
//MchSetup setups handlers for 2ch interface.
func MchSetup(s *LoggingServeMux) {
log.Println("start 2ch interface")
rtr := mux.NewRouter()
registToRouter(rtr, "/2ch/", boardApp)
registToRouter(rtr, "/2ch/dat/{datkey:[^\\.]+}.dat", threadApp)
registToRouter(rtr, "/2ch/{board:[^/]+}/subject.txt", subjectApp)
registToRouter(rtr, "/2ch/subject.txt", subjectApp)
registToRouter(rtr, "/2ch/{board:[^/]+}/head.txt", headApp)
registToRouter(rtr, "/2ch/head.txt", headApp)
s.Handle("/2ch/", handlers.CompressHandler(rtr))
s.RegistCompressHandler("/test/bbs.cgi", postCommentApp)
}
//boardApp just calls boardApp(), only print title.
func boardApp(w http.ResponseWriter, r *http.Request) {
a, err := newMchCGI(w, r)
if err != nil {
log.Println(err)
return
}
a.boardApp()
}
//threadApp renders dat files(record data) in the thread.
func threadApp(w http.ResponseWriter, r *http.Request) {
a, err := newMchCGI(w, r)
if err != nil {
log.Println(err)
return
}
m := mux.Vars(r)
board := m["board"]
if board == "" {
board = "2ch"
}
a.threadApp(board, m["datkey"])
}
//subjectApp renders time-subject lines of the thread.
func subjectApp(w http.ResponseWriter, r *http.Request) {
a, err := newMchCGI(w, r)
if err != nil {
log.Println(err)
return
}
m := mux.Vars(r)
a.subjectApp(m["board"])
}
//postCommentApp posts one record to the thread.
func postCommentApp(w http.ResponseWriter, r *http.Request) {
a, err := newMchCGI(w, r)
if err != nil {
log.Println(err)
return
}
a.postCommentApp()
}
//headApp just renders motd.
func headApp(w http.ResponseWriter, r *http.Request) {
a, err := newMchCGI(w, r)
if err != nil {
log.Println(err)
return
}
a.headApp()
}
//mchCGI is a class for renderring pages of 2ch interface .
type mchCGI struct {
*cgi
}
//newMchCGI returns mchCGI obj if visitor is allowed.
//if not allowed print 403.
func newMchCGI(w http.ResponseWriter, r *http.Request) (mchCGI, error) {
c := mchCGI{
cgi: newCGI(w, r),
}
if c.cgi == nil || !c.checkVisitor() {
w.WriteHeader(403)
fmt.Fprintf(w, "403 Forbidden")
return c, errors.New("403 forbidden")
}
return c, nil
}
//serveContent serves str as content with name=name(only used suffix to determine
//data type),time=t after converted cp932. ServeContent is used to make clients possible
//to use range request.
func (m *mchCGI) serveContent(name string, t time.Time, str string) {
br := bytes.NewReader([]byte(util.ToSJIS(str)))
http.ServeContent(m.wr, m.req, name, t, br)
}
//boardApp just renders title stripped from url.
func (m *mchCGI) boardApp() {
l := m.req.FormValue("Accept-Language")
if l == "" {
l = "ja"
}
msg := searchMessage(l, cfg.FileDir)
m.wr.Header().Set("Content-Type", "text/html; charset=Shift_JIS")
board := util.Escape(util.GetBoard(m.path()))
text := ""
if board != "" {
text = fmt.Sprintf("%s - %s - %s", msg["logo"], msg["description"], board)
} else {
text = fmt.Sprintf("%s - %s", msg["logo"], msg["description"])
}
htmlStr := fmt.Sprintf(
`<!DOCTYPE html>
<html><head>
<meta http-equiv="content-type" content="text/html; charset=Shift_JIS">
<title>%s</title>
<meta name="description" content="%s">
</head><body>
<h1>%s</h1>
</body></html>`,
text, text, text)
m.serveContent("a.html", time.Time{}, htmlStr)
}
//threadApp load thread.Cache specified in the url and returns dat file
//listing records. if thread.Cache len=0 or for each refering the thread.Cache 4 times
//reloads thread.Cache fron network.
func (m *mchCGI) threadApp(board, datkey string) {
m.wr.Header().Set("Content-Type", "text/plain; charset=Shift_JIS")
n, err := strconv.ParseInt(datkey, 10, 64)
if err != nil {
log.Println(err)
return
}
key := keylib.GetFilekey(n)
if err != nil {
m.wr.WriteHeader(404)
fmt.Fprintf(m.wr, "404 Not Found")
return
}
data := thread.NewCache(key)
if !data.Exists() {
m.wr.WriteHeader(404)
fmt.Fprintf(m.wr, "404 Not Found")
return
}
if m.checkGetCache() {
download.GetCache(true, data)
}
thread := keylib.MakeDat(data, board, m.req.Host)
str := strings.Join(thread, "\n") + "\n"
m.serveContent("a.txt", time.Unix(data.Stamp(), 0), str)
}
//makeSubjectCachelist returns thread.Caches in all thread.Cache and in recentlist sorted by recent stamp.
//if board is specified, returns thread.Caches whose tagstr=board.
func (m *mchCGI) makeSubjectCachelist(board string) []*thread.Cache {
result := thread.MakeRecentCachelist()
if board == "" {
return result
}
var result2 []*thread.Cache
for _, c := range result {
if user.Has(c.Datfile, board) {
result2 = append(result2, c)
}
}
return result2
}
//subjectApp makes list of records title from thread.Caches whose tag is same as one stripped from url.
func (m *mchCGI) subjectApp(board string) {
var boardEncoded, boardName string
if board != "" {
boardEncoded = util.StrDecode(board)
}
if boardEncoded != "" {
boardName = util.FileDecode("dummy_" + boardEncoded)
}
subject, lastStamp := m.makeSubject(boardName)
m.wr.Header().Set("Content-Type", "text/plain; charset=Shift_JIS")
m.serveContent("a.txt", time.Unix(lastStamp, 0), strings.Join(subject, "\n")+"\n")
}
//makeSubject makes subject.txt(list of records title) from thread.Caches with tag=board.
func (m *mchCGI) makeSubject(board string) ([]string, int64) {
loadFromNet := m.checkGetCache()
var subjects []string
cl := m.makeSubjectCachelist(board)
var lastStamp int64
for _, c := range cl {
if !loadFromNet && c.Len() == 0 {
continue
}
if lastStamp < c.Stamp() {
lastStamp = c.Stamp()
}
key, err := keylib.GetDatkey(c.Datfile)
if err != nil {
log.Println(err)
continue
}
titleStr := util.FileDecode(c.Datfile)
if titleStr == "" {
continue
}
titleStr = strings.Trim(titleStr, "\r\n")
subjects = append(subjects, fmt.Sprintf("%d.dat<>%s (%d)",
key, titleStr, c.Len()))
}
return subjects, lastStamp
}
//headApp renders motd(terms of service).
func (m *mchCGI) headApp() {
m.wr.Header().Set("Content-Type", "text/plain; charset=Shift_JIS")
var body string
err := util.EachLine(cfg.Motd(), func(line string, i int) error {
line = strings.TrimSpace(line)
body += line + "<br>\n"
return nil
})
if err != nil {
log.Println(err)
}
m.serveContent("a.txt", time.Time{}, body)
}
var (
errSpamM = errors.New("this is spam")
)
//postComment creates a record from args and adds it to thread.Cache.
//also adds tag if not tag!=""
func (m *mchCGI) postComment(threadKey, name, mail, body, passwd, tag string) error {
stamp := time.Now().Unix()
recbody := make(map[string]string)
recbody["body"] = html.EscapeString(body)
recbody["name"] = html.EscapeString(name)
recbody["mail"] = html.EscapeString(mail)
c := thread.NewCache(threadKey)
rec := record.New(c.Datfile, "", 0)
rec.Build(stamp, recbody, passwd)
if rec.IsSpam() {
return errSpamM
}
rec.Sync()
if tag != "" {
user.Set(c.Datfile, []string{tag})
}
go updateque.UpdateNodes(rec, nil)
return nil
}
//errorResp render erro page with cp932 code.
func (m *mchCGI) errorResp(msg string, info map[string]string) {
m.wr.Header().Set("Content-Type", "text/html; charset=Shift_JIS")
info["message"] = msg
tmpH.RenderTemplate("2ch_error", info, m.wr)
}
//getCP932 returns form value of key with cp932 code.
func (m *mchCGI) getCP932(key string) string {
return util.FromSJIS(m.req.FormValue(key))
}
//getcommentData returns comment data with map in cp932 code.
func (m *mchCGI) getCommentData() map[string]string {
mail := m.getCP932("mail")
if strings.ToLower(mail) == "sage" {
mail = ""
}
return map[string]string{
"subject": m.getCP932("subject"),
"name": m.getCP932("FROM"),
"mail": mail,
"body": m.getCP932("MESSAGE"),
"key": m.getCP932("key"),
}
}
//checkInfo checks posted info and returs thread name.
//if ok.
func (m *mchCGI) checkInfo(info map[string]string) string {
key := ""
if info["subject"] != "" {
key = util.FileEncode("thread", info["subject"])
} else {
n, err := strconv.ParseInt(info["key"], 10, 64)
if err != nil {
m.errorResp(err.Error(), info)
return ""
}
key = keylib.GetFilekey(n)
}
switch {
case info["body"] == "":
m.errorResp("本文がありません.", info)
return ""
case thread.NewCache(key).Exists(), m.hasAuth():
case info["subject"] != "":
m.errorResp("掲示版を作る権限がありません", info)
return ""
default:
m.errorResp("掲示版がありません", info)
return ""
}
if info["subject"] == "" && key == "" {
m.errorResp("フォームが変です.", info)
return ""
}
return key
}
//postCommentApp checks posted data and replaces >> links to html links,
//and saves it as record.
func (m *mchCGI) postCommentApp() {
if m.req.Method != http.MethodPost {
m.wr.Header().Set("Content-Type", "text/plain")
m.wr.WriteHeader(404)
fmt.Fprintf(m.wr, "404 Not Found")
return
}
info := m.getCommentData()
info["host"] = m.req.Host
key := m.checkInfo(info)
if key == "" {
return
}
referer := m.getCP932("Referer")
reg := regexp.MustCompile("/2ch_([^/]+)/")
var tag string
if ma := reg.FindStringSubmatch(referer); ma != nil && m.hasAuth() {
tag = util.FileDecode("dummy_" + ma[1])
}
table := mch.NewResTable(thread.NewCache(key))
reg = regexp.MustCompile(">>([1-9][0-9]*)")
body := reg.ReplaceAllStringFunc(info["body"], func(str string) string {
noStr := reg.FindStringSubmatch(str)[1]
no, err := strconv.Atoi(noStr)
if err != nil {
log.Fatal(err)
}
return ">>" + table.Num2id[no]
})
name := info["name"]
var passwd string
if strings.ContainsRune(name, '#') {
ary := strings.Split(name, "#")
name = ary[0]
passwd = ary[1]
}
if passwd != "" && !m.isAdmin() {
m.errorResp("自ノード以外で署名機能は使えません", info)
}
err := m.postComment(key, name, info["mail"], body, passwd, tag)
if err == errSpamM {
m.errorResp("スパムとみなされました", info)
}
m.wr.Header().Set("Content-Type", "text/html; charset=Shift_JIS")
fmt.Fprintln(m.wr,
util.ToSJIS(`<html lang="ja"><head><meta http-equiv="Content-Type" content="text/html"><title>書きこみました。</title></head><body>書きこみが終わりました。<br><br></body></html>`))
}