forked from shingetsu-gou/shingetsu-gou
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keylib.go
236 lines (221 loc) · 6.7 KB
/
keylib.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
/*
* 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 keylib
import (
"fmt"
"log"
"regexp"
"time"
"github.com/boltdb/bolt"
"github.com/shingetsu-gou/shingetsu-gou/db"
"github.com/shingetsu-gou/shingetsu-gou/mch"
"github.com/shingetsu-gou/shingetsu-gou/recentlist"
"github.com/shingetsu-gou/shingetsu-gou/record"
"github.com/shingetsu-gou/shingetsu-gou/thread"
"github.com/shingetsu-gou/shingetsu-gou/util"
)
func getThread(tx *bolt.Tx, stamp int64) (string, error) {
var thread string
k := db.MustTob(stamp)
_, err := db.Get(tx, "keylibST", k, &thread)
return thread, err
}
func getTime(tx *bolt.Tx, thread string) (int64, error) {
var stamp int64
k := db.MustTob(thread)
_, err := db.Get(tx, "keylibTS", k, &stamp)
return stamp, err
}
//Load loads from the file, adds stamps/datfile pairs from cachelist and recentlist.
//and saves to file.
func Load() {
allCaches := thread.AllCaches()
allRecs := recentlist.GetRecords()
err := db.DB.Update(func(tx *bolt.Tx) error {
for _, c := range allCaches {
setFromCache(tx, c)
}
for _, rec := range allRecs {
c := thread.NewCache(rec.Datfile)
setFromCache(tx, c)
}
return nil
})
if err != nil {
log.Println(err)
}
}
//setEntry stores stamp/value.
func setEntry(tx *bolt.Tx, stamp int64, filekey string) {
sb := db.MustTob(stamp)
fb := db.MustTob(filekey)
err := db.Put(tx, "keylibST", sb, fb)
if err != nil {
panic(err)
}
err = db.Put(tx, "keylibTS", fb, sb)
if err != nil {
panic(err)
}
}
//setFromCache adds cache.datfile/timestamp pair if not exists.
func setFromCache(tx *bolt.Tx, ca *thread.Cache) {
_, err := getTime(tx, ca.Datfile)
if err == nil {
return
}
var firstStamp int64
rec := ca.LoadRecords(record.Alive)
switch {
case !ca.HasRecord():
firstStamp = ca.RecentStamp()
case len(rec) > 0:
firstStamp = rec[rec.Keys()[0]].Stamp
default:
firstStamp = time.Now().Add(-24 * time.Hour).Unix()
}
for {
_, err = getThread(tx, firstStamp)
if err != nil {
break
}
firstStamp++
}
setEntry(tx, firstStamp, ca.Datfile)
}
//GetDatkey returns stamp from filekey.
//if not found, tries to read from cache.
func GetDatkey(filekey string) (int64, error) {
var v int64
err := db.DB.Update(func(tx *bolt.Tx) error {
var errr error
v, errr = getTime(tx, filekey)
if errr == nil {
return nil
}
c := thread.NewCache(filekey)
setFromCache(tx, c)
v, errr = getTime(tx, filekey)
return errr
})
return v, err
}
//GetFilekey returns value from datkey(stamp).
func GetFilekey(nDatkey int64) string {
var v string
err := db.DB.View(func(tx *bolt.Tx) error {
var errr error
v, errr = getThread(tx, nDatkey)
return errr
})
if err == nil {
return v
}
return ""
}
//MakeBracketLink changes str in brackets to the html links format.
func MakeBracketLink(body, datHost, board string, table *mch.ResTable) string {
regs := []*regexp.Regexp{
regexp.MustCompile("^(?P<title>[^/]+)$"),
regexp.MustCompile("^/(?P<type>[a-z]+)/(?P<title>[^/]+)$"),
regexp.MustCompile("^(?P<title>[^/]+)/(?P<id>[0-9a-f]{8})$"),
regexp.MustCompile("^/(?P<type>[a-z]+)/(?P<title>[^/]+)/(?P<id>[0-9a-f]{8})$"),
}
reg := regexp.MustCompile(`\[\[([^<>]+?)\]\]`)
return reg.ReplaceAllStringFunc(body, func(str string) string {
link := reg.FindStringSubmatch(str)[1]
result := make(map[string]string)
for _, r := range regs {
if match := r.FindStringSubmatch(link); match != nil {
for i, name := range r.SubexpNames() {
result[name] = match[i]
}
break
}
}
if result["title"] == "" {
return result["body"]
}
if result["type"] == "" {
result["type"] = "thread"
}
file := util.FileEncode(result["type"], result["title"])
datkey, err := GetDatkey(file)
if err != nil {
log.Println(err)
return body
}
if result["id"] == "" {
url := fmt.Sprintf("http://%s/test/read.cgi/%s/%d/", datHost, board, datkey)
return fmt.Sprintf("[[%s(%s)]]", result["title"], url)
}
ca := thread.NewCache(file)
table = mch.NewResTable(ca)
no := table.ID2num[result["id"]]
url := fmt.Sprintf("http://%s/test/read.cgi/%s/%d/%d", datHost, board, datkey, no)
return fmt.Sprintf("[[%s(>>%d %s)]]", result["title"], no, url)
})
}
//MakeBody makes a dat body(message) line after stamp.
func MakeBody(rec *record.Record, host, board string, table *mch.ResTable) string {
body := rec.GetBodyValue("body", "")
body += rec.MakeAttachLink(host)
body = table.MakeRSSAnchor(body)
body = MakeBracketLink(body, host, board, table)
return body
}
//MakeDat makes dat lines of 2ch from cache.
func MakeDat(ca *thread.Cache, board, host string) []string {
recs := ca.LoadRecords(record.Alive)
dat := make([]string, len(recs))
table := mch.NewResTable(ca)
i := 0
for _, datfile := range recs.Keys() {
rec := recs[datfile]
err := rec.Load()
if err != nil {
log.Println(err)
continue
}
name := rec.GetBodyValue("name", "")
if name == "" {
name = "名無しさん"
}
if rec.GetBodyValue("pubkey", "") != "" {
name += "◆" + rec.GetBodyValue("pubkey", "")[:10]
}
comment := fmt.Sprintf("%s<>%s<>%s<>%s<>",
name, rec.GetBodyValue("main", ""), util.Datestr2ch(rec.Stamp), MakeBody(rec, host, board, table))
if i == 0 {
comment += util.FileDecode(ca.Datfile)
}
dat[i] = comment
i++
}
return dat
}