-
Notifications
You must be signed in to change notification settings - Fork 4
/
process_nagome_message.go
395 lines (339 loc) · 12 KB
/
process_nagome_message.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
package viewer
import (
"context"
"encoding/json"
"net/http"
"path/filepath"
"regexp"
"time"
"github.com/diginatu/nagome/nicolive"
)
// Constant values for processing plugin messages.
const (
NumFetchInformaionRetry = 3
)
var (
broadIDRegex = regexp.MustCompile(`(lv|co)\d+`)
)
func processNagomeMessage(cv *CommentViewer, m *Message) error {
switch m.Domain {
case DomainQuery:
switch m.Command {
case CommQueryBroadConnect:
if err := processQueryBroadConnect(cv, m); err != nil {
return err
}
cv.cli.log.Println("connected")
case CommQueryBroadDisconnect:
cv.Disconnect()
case CommQueryBroadSendComment:
if cv.Cmm == nil {
return nicolive.MakeError(nicolive.ErrSendComment, "not connected to live")
}
if cv.Lw == nil {
return nicolive.MakeError(nicolive.ErrSendComment, "cv.Lw is nil")
}
var ct CtQueryBroadSendComment
if err := json.Unmarshal(m.Content, &ct); err != nil {
return nicolive.MakeError(nicolive.ErrOther, "JSON error in the content : "+err.Error())
}
isowner := false
if cv.Lw.IsUserOwner() {
isowner = cv.Settings.OwnerComment
if ct.Type != "" {
isowner = ct.Type == CtQueryBroadSendCommentTypeOwner
}
}
if isowner {
rq := nicolive.CommentOwnerRequest{
Text: ct.Text,
Color: "",
IsPermanent: false,
UserName: "",
}
err := nicolive.CommentOwner(cv.Lw.BroadID, http.MethodPut, &rq, cv.Ac)
if err != nil {
return err
}
} else {
cv.Cmm.SendComment(ct.Text, ct.Iyayo)
}
case CommQueryAccountSet:
if cv.Ac == nil {
return nicolive.MakeError(nicolive.ErrOther, "Account data (cv.Ac) is nil.")
}
var ct CtQueryAccountSet
if err := json.Unmarshal(m.Content, &ct); err != nil {
return nicolive.MakeError(nicolive.ErrOther, "JSON error in the content : "+err.Error())
}
if ct.Mail != "" {
cv.Ac.Mail = ct.Mail
}
if ct.Pass != "" {
cv.Ac.Pass = ct.Pass
}
if ct.Usersession != "" {
cv.Ac.Usersession = ct.Usersession
}
cv.AntennaConnect()
case CommQueryAccountLogin:
err := cv.Ac.Login()
if err != nil {
if nerr, ok := err.(nicolive.Error); ok {
cv.EmitEvNewNotification(CtUINotificationTypeWarn, "login error", nerr.Description())
} else {
cv.EmitEvNewNotification(CtUINotificationTypeWarn, "login error", err.Error())
}
return err
}
cv.cli.log.Println("logged in")
cv.EmitEvNewNotification(CtUINotificationTypeInfo, "login succeeded", "login succeeded")
case CommQueryAccountLoad:
var err error
cv.Ac, err = nicolive.AccountLoad(filepath.Join(cv.cli.SavePath, accountFileName))
return err
case CommQueryAccountSave:
return cv.Ac.Save(filepath.Join(cv.cli.SavePath, accountFileName))
case CommQueryLogPrint:
var ct CtQueryLogPrint
if err := json.Unmarshal(m.Content, &ct); err != nil {
return nicolive.MakeError(nicolive.ErrOther, "JSON error in the content : "+err.Error())
}
cv.cli.log.Printf("plug[%s] %s\n", cv.PluginName(m.plgno), ct.Text)
case CommQuerySettingsSetCurrent:
var ct CtQuerySettingsSetCurrent
if err := json.Unmarshal(m.Content, &ct); err != nil {
return nicolive.MakeError(nicolive.ErrOther, "JSON error in the content : "+err.Error())
}
cv.Settings = SettingsSlot(ct)
for _, p := range cv.Pgns {
p.SetState(!cv.Settings.PluginDisable[p.Name])
}
case CommQuerySettingsSetAll:
var ct CtQuerySettingsSetAll
if err := json.Unmarshal(m.Content, &ct); err != nil {
return nicolive.MakeError(nicolive.ErrOther, "JSON error in the content : "+err.Error())
}
cv.cli.SettingsSlots = SettingsSlots(ct)
case CommQueryPlugEnable:
var ct CtQueryPlugEnable
if err := json.Unmarshal(m.Content, &ct); err != nil {
return nicolive.MakeError(nicolive.ErrOther, "JSON error in the content : "+err.Error())
}
pl, err := cv.Plugin(ct.No)
if err != nil {
return err
}
pl.SetState(ct.Enable)
if cv.Settings.PluginDisable == nil {
cv.Settings.PluginDisable = make(map[string]bool)
}
cv.Settings.PluginDisable[pl.Name] = !ct.Enable
case CommQueryUserSet:
var ct nicolive.User // CtQueryUserSet
if err := json.Unmarshal(m.Content, &ct); err != nil {
cv.EmitEvNewNotification(CtUINotificationTypeWarn, "Storing the user info failed", "JSON parse error")
return nicolive.MakeError(nicolive.ErrOther, "JSON error in the content : "+err.Error())
}
err := cv.prcdnle.userDB.Store(&ct)
if err != nil {
cv.EmitEvNewNotification(CtUINotificationTypeWarn, "Storing the user info failed", "DB error : "+err.Error())
return err
}
cv.Evch <- NewMessageMust(DomainNagome, CommNagomeUserUpdate, CtNagomeUserUpdate(ct))
case CommQueryUserSetName:
var ct CtQueryUserSetName
if err := json.Unmarshal(m.Content, &ct); err != nil {
cv.EmitEvNewNotification(CtUINotificationTypeWarn, "Storing the user name failed", "JSON parse error")
return nicolive.MakeError(nicolive.ErrOther, "JSON error in the content : "+err.Error())
}
if ct.Name == "" {
cv.EmitEvNewNotification(CtUINotificationTypeWarn, "Blank name", "You can't set blank name")
return nicolive.MakeError(nicolive.ErrOther, "format error : Name is empty")
}
user, err := cv.prcdnle.userDB.Fetch(ct.ID)
if err != nil {
nerr, ok := err.(nicolive.Error)
if ok && nerr.Type() == nicolive.ErrDBUserNotFound {
user, err = cv.prcdnle.CheckIntervalAndCreateUser(ct.ID)
if err != nil {
cv.EmitEvNewNotification(CtUINotificationTypeWarn, "Storing the user name failed", "DB error")
return nicolive.MakeError(nicolive.ErrOther, "Storing the user name failed: DB error : "+err.Error())
}
} else {
cv.EmitEvNewNotification(CtUINotificationTypeWarn, "Storing the user name failed", "DB error")
return nicolive.MakeError(nicolive.ErrOther, "Storing the user name failed: DB error : "+err.Error())
}
}
user.Name = ct.Name
err = cv.prcdnle.userDB.Store(user)
if err != nil {
cv.EmitEvNewNotification(CtUINotificationTypeWarn, "Storing the user name failed", "DB error : "+err.Error())
return err
}
cv.Evch <- NewMessageMust(DomainNagome, CommNagomeUserUpdate, CtNagomeUserUpdate(*user))
case CommQueryUserDelete:
var ct CtQueryUserDelete
if err := json.Unmarshal(m.Content, &ct); err != nil {
cv.EmitEvNewNotification(CtUINotificationTypeWarn, "Deleting the user info failed", "JSON parse error")
return nicolive.MakeError(nicolive.ErrOther, "JSON error in the content : "+err.Error())
}
err := cv.prcdnle.userDB.Remove(ct.ID)
if err != nil {
cv.EmitEvNewNotification(CtUINotificationTypeWarn, "Removing the user info failed", "DB error : "+err.Error())
return err
}
usr := CtNagomeUserUpdate{
ID: ct.ID,
Name: "",
CreateTime: time.Unix(0, 0),
Is184: nicolive.Is184UserID(ct.ID),
ThumbnailURL: "",
}
cv.Evch <- NewMessageMust(DomainNagome, CommNagomeUserUpdate, usr)
case CommQueryUserFetch:
var ct CtQueryUserFetch
if err := json.Unmarshal(m.Content, &ct); err != nil {
cv.EmitEvNewNotification(CtUINotificationTypeWarn, "Fetching the user info failed", "JSON parse error")
return nicolive.MakeError(nicolive.ErrOther, "JSON error in the content : "+err.Error())
}
user, err := cv.prcdnle.CheckIntervalAndCreateUser(ct.ID)
if err != nil {
cv.EmitEvNewNotification(CtUINotificationTypeWarn, "Removing the user info failed", "DB error : "+err.Error())
return err
}
userCurrent, err := cv.prcdnle.userDB.Fetch(ct.ID)
if err != nil {
nerr, ok := err.(nicolive.Error)
if ok && nerr.Type() == nicolive.ErrDBUserNotFound {
// If the user was not in the DB
userCurrent = user
} else {
cv.EmitEvNewNotification(CtUINotificationTypeWarn, "Removing the user info failed", "DB error : "+err.Error())
return err
}
} else {
userCurrent.Name = user.Name
userCurrent.ThumbnailURL = user.ThumbnailURL
}
err = cv.prcdnle.userDB.Store(userCurrent)
if err != nil {
cv.EmitEvNewNotification(CtUINotificationTypeWarn, "Removing the user info failed", "DB error : "+err.Error())
return err
}
cv.Evch <- NewMessageMust(DomainNagome, CommNagomeUserUpdate, CtNagomeUserUpdate(*userCurrent))
case CommDirectUserGet:
var ct CtDirectUserGet
if err := json.Unmarshal(m.Content, &ct); err != nil {
return nicolive.MakeError(nicolive.ErrOther, "JSON error in the content : "+err.Error())
}
user, err := cv.prcdnle.userDB.Fetch(ct.ID)
if err != nil {
cv.EmitEvNewNotification(CtUINotificationTypeWarn, "Removing the user info failed", "DB error : "+err.Error())
cv.cli.log.Printf("Removing the user info failed.\n DB error : %s", err.Error())
return err
}
cv.Evch <- NewMessageMust(DomainDirectngm, CommDirectngmUserGet, CtDirectngmUserGet(*user))
default:
return nicolive.MakeError(nicolive.ErrOther, "Message : invalid query command : "+m.Command)
}
case DomainAntenna:
switch m.Command {
case CommAntennaGot:
if cv.Settings.AutoFollowNextWaku {
var ct CtAntennaGot
if err := json.Unmarshal(m.Content, &ct); err != nil {
return nicolive.MakeError(nicolive.ErrOther, "JSON error in the content : "+err.Error())
}
if cv.Lw != nil && cv.Lw.Stream.CommunityID == ct.CommunityID {
ct := CtQueryBroadConnect{ct.BroadID, 0}
cv.cli.log.Println("following to " + ct.BroadID)
cv.Evch <- NewMessageMust(DomainQuery, CommQueryBroadConnect, ct)
}
}
}
}
return nil
}
func processQueryBroadConnect(cv *CommentViewer, m *Message) error {
var err error
var ct CtQueryBroadConnect
if err = json.Unmarshal(m.Content, &ct); err != nil {
return nicolive.MakeError(nicolive.ErrOther, "JSON error in the content : "+err.Error())
}
broadMch := broadIDRegex.FindString(ct.BroadID)
if broadMch == "" {
cv.EmitEvNewNotification(CtUINotificationTypeWarn, "invalid BroadID", "no valid BroadID found in the ID text")
return nicolive.MakeError(nicolive.ErrOther, "no valid BroadID found in the ID text")
}
lw := &nicolive.LiveWaku{Account: cv.Ac, BroadID: broadMch}
err = lw.FetchInformation()
if err != nil {
nerr, ok := err.(nicolive.Error)
if ok {
if nerr.Type() == nicolive.ErrNetwork {
ct.RetryN++
cv.cli.log.Printf("Failed to connect to %s.\n", ct.BroadID)
cv.cli.log.Printf("FetchInformation : %s\n", nerr.Error())
if ct.RetryN >= NumFetchInformaionRetry {
cv.cli.log.Println("Reached the limit of retrying.")
return nerr
}
cv.cli.log.Println("Retrying...")
go func() {
<-time.After(time.Second)
cv.Evch <- NewMessageMust(DomainQuery, CommQueryBroadConnect, ct)
}()
}
}
return err
}
cv.Disconnect()
cv.Lw = lw
cv.Cmm, err = nicolive.CommentConnect(context.TODO(), *cv.Lw, cv.prcdnle)
if err != nil {
return err
}
if lw.IsUserOwner() {
ps, err := nicolive.PublishStatus(lw.BroadID, cv.Ac)
if err != nil {
return err
}
cv.Lw.OwnerCommentToken = ps.Token
}
return nil
}
func processDirectMessage(cv *CommentViewer, m *Message) error {
var t *Message
var err error
switch m.Command {
case CommDirectngmAppVersion:
t, err = NewMessage(DomainDirectngm, CommDirectngmAppVersion, CtDirectngmAppVersion{
Name: cv.cli.AppName,
Version: cv.cli.Version,
})
if err != nil {
return nicolive.ErrFromStdErr(err)
}
case CommDirectPlugList:
c := CtDirectngmPlugList{&cv.Pgns}
t, err = NewMessage(DomainDirectngm, CommDirectngmPlugList, c)
if err != nil {
return nicolive.ErrFromStdErr(err)
}
case CommDirectSettingsCurrent:
t, err = NewMessage(DomainDirectngm, CommDirectngmSettingsCurrent, CtDirectngmSettingsCurrent(cv.Settings))
if err != nil {
return nicolive.ErrFromStdErr(err)
}
case CommDirectSettingsAll:
t, err = NewMessage(DomainDirectngm, CommDirectngmSettingsAll, CtDirectngmSettingsAll(cv.cli.SettingsSlots))
if err != nil {
return nicolive.ErrFromStdErr(err)
}
default:
return nicolive.MakeError(nicolive.ErrOther, "Message : invalid query command : "+m.Command)
}
cv.Pgns[m.plgno].WriteMess(t)
return nil
}