-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
306 lines (267 loc) · 8.05 KB
/
main.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
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
"sync"
"time"
"github.com/BurntSushi/toml"
"github.com/MemeLabs/dggchat"
"github.com/awesome-gocui/gocui"
)
type config struct {
AuthToken string `toml:"auth_token"`
CustomURL string `toml:"custom_url"`
Username string `toml:"username"`
Timeformat string `toml:"timeformat"`
Maxlines int `toml:"maxlines"`
ScrollingSpeed int `toml:"scrolling_speed"`
PageUpDownSpeed int `toml:"page_up_down_Speed"`
Highlighted []string `toml:"highlighted"`
Tags map[string]string `toml:"tags"`
Ignores []string `toml:"ignores"`
Stalks []string `toml:"stalks"`
ShowJoinLeave bool `toml:"showjoinleave"`
HighlightColor string `toml:"highlight_color"`
TagColor string `toml:"tag_color"`
HighlightBg string `toml:"highlight_bg_color"`
HighlightFg string `toml:"highlight_fg_color"`
LoadHistory bool `toml:"load_history"`
HistoryURL string `toml:"history_url"`
sync.RWMutex
}
var configFile string
func init() {
flag.StringVar(&configFile, "config", "config.toml", "location of config file to be used")
flag.Parse()
}
func main() {
// defaults that won't be set corretly if omitted in config file
config := config{
Timeformat: time.Kitchen,
Maxlines: 1000,
ScrollingSpeed: 1,
PageUpDownSpeed: 10,
}
_, err := toml.DecodeFile(configFile, &config)
if err != nil {
log.Fatalf("malformed configuration file: %v\n", err)
}
g, err := gocui.NewGui(gocui.OutputNormal, false)
if err != nil {
log.Panicln(err)
}
defer g.Close()
g.SetManagerFunc(layout)
g.Mouse = true
chat, err := newChat(&config, g)
if err != nil {
log.Panicln(err)
}
if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil {
log.Panicln(err)
}
if err := g.SetKeybinding("", gocui.KeyF1, gocui.ModNone, chat.showHelp); err != nil {
log.Panicln(err)
}
if err := g.SetKeybinding("", gocui.KeyF12, gocui.ModNone, chat.showDebug); err != nil {
log.Panicln(err)
}
if err := g.SetKeybinding("", gocui.KeyF2, gocui.ModNone, chat.showUserList); err != nil {
log.Panicln(err)
}
if err := g.SetKeybinding("input", gocui.KeyArrowUp, gocui.ModNone, func(g *gocui.Gui, v *gocui.View) error {
err = chat.historyUp(g, v)
return err
}); err != nil {
log.Panicln(err)
}
if err := g.SetKeybinding("input", gocui.KeyArrowDown, gocui.ModNone, func(g *gocui.Gui, v *gocui.View) error {
err = chat.historyDown(g, v)
return err
}); err != nil {
log.Panicln(err)
}
if err := g.SetKeybinding("", gocui.KeyPgup, gocui.ModNone, func(g *gocui.Gui, v *gocui.View) error {
err = scroll(-chat.config.PageUpDownSpeed, chat, "messages")
return err
}); err != nil {
log.Panicln(err)
}
if err := g.SetKeybinding("", gocui.KeyPgdn, gocui.ModNone, func(g *gocui.Gui, v *gocui.View) error {
err = scroll(chat.config.PageUpDownSpeed, chat, "messages")
return err
}); err != nil {
log.Panicln(err)
}
chat.mustAddScroll("messages", chat.config.ScrollingSpeed, gocui.MouseWheelUp, gocui.MouseWheelDown)
chat.mustAddScroll("users", chat.config.ScrollingSpeed, gocui.MouseWheelUp, gocui.MouseWheelDown)
chat.mustAddScroll("help", 1, gocui.MouseWheelUp, gocui.MouseWheelDown)
chat.mustAddScroll("debug", 1, gocui.MouseWheelUp, gocui.MouseWheelDown)
err = g.SetKeybinding("input", gocui.KeyEnter, gocui.ModNone, func(g *gocui.Gui, v *gocui.View) error {
if v.Buffer() == "" {
return nil
}
chat.handleInput(strings.TrimSpace(v.Buffer()))
g.Update(func(g *gocui.Gui) error {
v.Clear()
v.SetCursor(0, 0)
v.SetOrigin(0, 0)
return nil
})
return nil
})
if err != nil {
log.Panicln(err)
}
if err := g.SetKeybinding("input", gocui.KeyTab, gocui.ModNone, func(g *gocui.Gui, v *gocui.View) error {
chat.tabComplete(v)
return nil
}); err != nil {
log.Panicln(err)
}
chat.Session.AddNamesHandler(func(n dggchat.Names, s *dggchat.Session) {
chat.renderCommand("Connected!")
chat.renderUsers(n.Users)
})
chat.Session.AddSocketErrorHandler(func(err error, s *dggchat.Session) {
chat.renderError(err.Error() + " - Trying to reconnect...")
})
chat.Session.AddMessageHandler(func(m dggchat.Message, s *dggchat.Session) {
chat.renderMessage(m)
})
chat.Session.AddMessageHandler(func(m dggchat.Message, s *dggchat.Session) {
chat.renderMessage(m)
})
chat.Session.AddErrorHandler(func(e string, s *dggchat.Session) {
chat.renderError(e)
})
chat.Session.AddMuteHandler(func(m dggchat.Mute, s *dggchat.Session) {
chat.renderMute(m)
})
chat.Session.AddUnmuteHandler(func(m dggchat.Mute, s *dggchat.Session) {
chat.renderUnmute(m)
})
chat.Session.AddBanHandler(func(b dggchat.Ban, s *dggchat.Session) {
chat.renderBan(b)
})
chat.Session.AddUnbanHandler(func(b dggchat.Ban, s *dggchat.Session) {
chat.renderUnban(b)
})
chat.Session.AddJoinHandler(func(r dggchat.RoomAction, s *dggchat.Session) {
chat.renderJoin(r)
chat.renderUsers(chat.Session.GetUsers())
})
chat.Session.AddQuitHandler(func(r dggchat.RoomAction, s *dggchat.Session) {
chat.renderQuit(r)
chat.renderUsers(chat.Session.GetUsers())
})
chat.Session.AddSubOnlyHandler(func(so dggchat.SubOnly, s *dggchat.Session) {
chat.renderSubOnly(so)
})
chat.Session.AddBroadcastHandler(func(b dggchat.Broadcast, s *dggchat.Session) {
chat.renderBroadcast(b)
})
chat.Session.AddPMHandler(func(pm dggchat.PrivateMessage, s *dggchat.Session) {
chat.renderPrivateMessage(pm)
})
chat.Session.AddPingHandler(func(p dggchat.Ping, s *dggchat.Session) {
_ = p.Timestamp // TODO
})
if config.LoadHistory {
// writing custom load for now, really this should be in
// the library itself.
// fetch history
type message struct {
Nick string `json:"nick"`
Features []string `json:"features"`
Timestamp int64 `json:"timestamp"`
Data string `json:"data"`
}
var received []string
historyClient := http.Client{
Timeout: time.Second * 2,
}
req, err := http.NewRequest(http.MethodGet, config.HistoryURL, nil)
if err != nil {
log.Fatal(err)
}
req.Header.Set("User-Agent", "tsgg")
res, err := historyClient.Do(req)
if err != nil {
log.Fatal(err)
}
if res.StatusCode == 200 {
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatal(err)
}
err = json.Unmarshal(body, &received)
if err != nil {
log.Fatal(err)
}
for _, x := range received {
mslice := strings.SplitN(x, " ", 2)
var m message
err := json.Unmarshal([]byte(mslice[1]), &m)
if err != nil {
log.Fatal(err)
}
user := dggchat.User{
Nick: m.Nick,
Features: m.Features,
}
M := dggchat.Message{
Sender: user,
Timestamp: time.Unix(m.Timestamp/1000, 0),
Message: m.Data,
}
chat.renderMessage(M)
}
}
}
err = chat.Session.Open()
if err != nil {
// Most common problem is that the connection couldn't be established.
log.Panicln(err)
}
defer chat.Session.Close()
if err := g.MainLoop(); err != nil && err != gocui.ErrQuit {
log.Panicln(err)
}
}
func quit(g *gocui.Gui, v *gocui.View) error {
return gocui.ErrQuit
}
func (cfg *config) save() error {
buf := bytes.NewBuffer([]byte{})
err := toml.NewEncoder(buf).Encode(&cfg)
if err != nil {
return fmt.Errorf("error marshaling config: %v", err)
}
err = ioutil.WriteFile(configFile, buf.Bytes(), 0600)
if err != nil {
return fmt.Errorf("error saving config: %v", err)
}
return nil
}
func (c *chat) mustAddScroll(view string, speed int, up gocui.Key, down gocui.Key) {
var err error
err = c.guiwrapper.gui.SetKeybinding(view, down, gocui.ModNone, func(g *gocui.Gui, v *gocui.View) error {
return scroll(speed, c, view)
})
if err != nil {
log.Panicln(err)
}
err = c.guiwrapper.gui.SetKeybinding(view, up, gocui.ModNone, func(g *gocui.Gui, v *gocui.View) error {
return scroll(-speed, c, view)
})
if err != nil {
log.Panicln(err)
}
}