forked from bakape/shamichan
/
post_updates.go
343 lines (302 loc) · 7.55 KB
/
post_updates.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
package websockets
import (
"encoding/json"
"errors"
"meguca/auth"
"meguca/common"
"meguca/config"
"meguca/db"
"meguca/parser"
"meguca/util"
"time"
"unicode/utf8"
)
var (
errNoPostOpen = errors.New("no post open")
errEmptyPost = errors.New("post body empty")
errTooManyLines = errors.New("too many lines in post body")
errInvalidSpliceCoords = errors.New("invalid splice coordinates")
errSpliceTooLong = errors.New("splice text too long")
errSpliceNOOP = errors.New("splice NOOP")
errTextOnly = errors.New("text only board")
errHasImage = errors.New("post already has image")
)
// Like spliceRequest, but with a string Text field. Used for internal
// conversions between []rune and string.
type spliceRequestString struct {
spliceCoords
Text string `json:"text"`
}
// Common part of a splice request and a splice response
type spliceCoords struct {
Start uint `json:"start"`
Len uint `json:"len"`
}
// Response to a spliceRequest. Sent to all listening clients.
type spliceMessage struct {
ID uint64 `json:"id"`
spliceRequestString
}
// Request or to replace the current line's text starting at an exact position
// in the current line
type spliceRequest struct {
spliceCoords
Text []rune
}
// Custom unmarshaling of string -> []rune
func (s *spliceRequest) UnmarshalJSON(buf []byte) error {
var tmp spliceRequestString
if err := json.Unmarshal(buf, &tmp); err != nil {
return err
}
*s = spliceRequest{
spliceCoords: tmp.spliceCoords,
Text: []rune(tmp.Text),
}
return nil
}
// Custom marshaling of []rune -> string
func (s spliceRequest) MarshalJSON() ([]byte, error) {
return json.Marshal(spliceRequestString{
spliceCoords: s.spliceCoords,
Text: string(s.Text),
})
}
// Append a rune to the body of the open post
func (c *Client) appendRune(data []byte) (err error) {
has, err := c.hasPost()
switch {
case err != nil:
return
case !has:
return
case c.post.len+1 > common.MaxLenBody:
return common.ErrBodyTooLong
}
var char rune
err = decodeMessage(data, &char)
switch {
case err != nil:
return
case char == 0:
return common.ErrContainsNull
case char == '\n':
c.post.lines++
if c.post.lines > common.MaxLinesBody {
return errTooManyLines
}
}
msg, err := common.EncodeMessage(
common.MessageAppend,
[2]uint64{c.post.id, uint64(char)},
)
if err != nil {
return
}
c.post.body = append(c.post.body, string(char)...)
c.post.len++
return c.updateBody(msg, 1)
}
// Send message to thread update feed and writes the open post's buffer to the
// embedded database. Requires locking of c.openPost.
// n specifies the number of characters updated.
func (c *Client) updateBody(msg []byte, n int) error {
c.feed.SetOpenBody(c.post.id, c.post.body, msg)
err := c.incrementSpamScore(time.Duration(n) * auth.CharScore)
if err != nil {
return err
}
return db.SetOpenBody(c.post.id, c.post.body)
}
// Increment the spam score for this IP by score. If the client requires a new
// solved captcha, send a notification.
func (c *Client) incrementSpamScore(score time.Duration) error {
exceeds, err := auth.IncrementSpamScore(c.ip, score)
if err != nil {
return err
}
if exceeds {
return c.sendMessage(common.MessageCaptcha, 0)
}
return nil
}
// Remove one character from the end of the line in the open post
func (c *Client) backspace() error {
has, err := c.hasPost()
switch {
case err != nil:
return err
case !has:
return nil
case c.post.len == 0:
return errEmptyPost
}
msg, err := common.EncodeMessage(common.MessageBackspace, c.post.id)
if err != nil {
return err
}
r, lastRuneLen := utf8.DecodeLastRune(c.post.body)
c.post.body = c.post.body[:len(c.post.body)-lastRuneLen]
if r == '\n' {
c.post.lines--
}
c.post.len--
return c.updateBody(msg, 1)
}
// Close an open post and parse the last line, if needed.
func (c *Client) closePost() error {
if c.post.id == 0 {
return errNoPostOpen
}
var (
links [][2]uint64
com []common.Command
err error
)
if c.post.len != 0 {
links, com, err = parser.ParseBody(c.post.body, c.post.board)
if err != nil {
return err
}
}
err = db.ClosePost(c.post.id, c.post.op, string(c.post.body), links, com)
if err != nil {
return err
}
c.post = openPost{}
return nil
}
// Splice the text in the open post. This call is also used for text pastes.
func (c *Client) spliceText(data []byte) error {
if has, err := c.hasPost(); err != nil {
return err
} else if !has {
return nil
}
// Decode and validate
var (
req spliceRequest
err = decodeMessage(data, &req)
)
switch {
case err != nil:
return err
case req.Start > common.MaxLenBody,
req.Len > common.MaxLenBody,
int(req.Start+req.Len) > c.post.len:
return errInvalidSpliceCoords
case req.Len == 0 && len(req.Text) == 0:
return errSpliceNOOP // This does nothing. Client-side error.
case len(req.Text) > common.MaxLenBody:
return errSpliceTooLong // Nice try, kid
}
for _, r := range req.Text {
if r == 0 {
return common.ErrContainsNull
}
}
var (
old = []rune(string(c.post.body))
end = append(req.Text, old[req.Start+req.Len:]...)
)
c.post.len += -int(req.Len) + len(req.Text)
res := spliceMessage{
ID: c.post.id,
spliceRequestString: spliceRequestString{
spliceCoords: req.spliceCoords,
Text: string(req.Text),
},
}
// If it goes over the max post length, trim the end
exceeding := c.post.len - common.MaxLenBody
if exceeding > 0 {
end = end[:len(end)-exceeding]
res.Len = uint(len(old[int(req.Start):]))
res.Text = string(end)
c.post.len = common.MaxLenBody
}
msg, err := common.EncodeMessage(common.MessageSplice, res)
if err != nil {
return err
}
// Need to prevent modifications to the original slice, as there might be
// concurrent reads in the update feed.
c.post.body = util.CloneBytes(c.post.body)
byteStartPos := 0
for _, r := range old[:req.Start] {
byteStartPos += utf8.RuneLen(r)
}
c.post.body = append(c.post.body[:byteStartPos], string(end)...)
c.post.countLines()
if c.post.lines > common.MaxLinesBody {
return errTooManyLines
}
// +1, so you can't spam zero insert splices to infinity
return c.updateBody(msg, len(res.Text)+1)
}
// Insert and image into an existing open post
func (c *Client) insertImage(data []byte) (err error) {
has, err := c.hasPost()
switch {
case err != nil:
return
case !has:
return errNoPostOpen
case c.post.hasImage:
return errHasImage
}
var req ImageRequest
err = decodeMessage(data, &req)
if err != nil {
return
}
if config.GetBoardConfigs(c.post.board).TextOnly {
return errTextOnly
}
img, err := getImage(req.Token, req.Name, req.Spoiler)
if err != nil {
return
}
c.post.hasImage = true
c.post.isSpoilered = req.Spoiler
err = db.InsertImage(c.post.id, *img)
if err != nil {
return
}
msg, err := common.EncodeMessage(common.MessageInsertImage, struct {
ID uint64 `json:"id"`
common.Image
}{
ID: c.post.id,
Image: *img,
})
if err != nil {
return
}
c.feed.InsertImage(c.post.id, msg)
return c.incrementSpamScore(auth.ImageScore)
}
// Spoiler an already inserted image in an unclosed post
func (c *Client) spoilerImage() (err error) {
has, err := c.hasPost()
switch {
case err != nil:
return err
case !has:
return errNoPostOpen
case !c.post.hasImage:
return errors.New("post does not have an image")
case c.post.isSpoilered:
return errors.New("already spoilered")
}
err = db.SpoilerImage(c.post.id)
if err != nil {
return
}
msg, err := common.EncodeMessage(common.MessageSpoiler, c.post.id)
if err != nil {
return
}
c.feed.SpoilerImage(c.post.id, msg)
return
}