forked from quackduck/devzat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevchat.go
More file actions
676 lines (613 loc) · 18.6 KB
/
devchat.go
File metadata and controls
676 lines (613 loc) · 18.6 KB
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
package main
import (
_ "embed"
"errors"
"fmt"
"io"
"log"
"math"
"math/rand"
"net"
"net/http"
_ "net/http/pprof"
"os"
"os/signal"
"runtime/debug"
"strconv"
"strings"
"sync"
"syscall"
"time"
"github.com/acarl005/stripansi"
"github.com/gliderlabs/ssh"
terminal "github.com/quackduck/term"
)
var (
port = 22
scrollback = 16
profilePort = 5555
// should this instance run offline? (should it not connect to slack or twitter?)
offlineSlack = os.Getenv("DEVZAT_OFFLINE_SLACK") != ""
offlineTwitter = os.Getenv("DEVZAT_OFFLINE_TWITTER") != ""
mainRoom = &room{"#main", make([]*user, 0, 10), sync.Mutex{}}
rooms = map[string]*room{mainRoom.name: mainRoom}
backlog = make([]backlogMessage, 0, scrollback)
bans = make([]ban, 0, 10)
idsInMinToTimes = make(map[string]int, 10) // TODO: maybe add some IP-based factor to disallow rapid key-gen attempts
antispamMessages = make(map[string]int)
logfile, _ = os.OpenFile("log.txt", os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0666)
l = log.New(io.MultiWriter(logfile, os.Stdout), "", log.Ldate|log.Ltime|log.Lshortfile)
devbot = "" // initialized in main
startupTime = time.Now()
)
type ban struct {
Addr string
ID string
}
type room struct {
name string
users []*user
usersMutex sync.Mutex
}
type user struct {
name string
pronouns []string
session ssh.Session
term *terminal.Terminal
room *room
messaging *user // currently messaging this user in a DM
bell bool
pingEverytime bool
isSlack bool
formatTime24 bool
color string
colorBG string
id string
addr string
win ssh.Window
closeOnce sync.Once
lastTimestamp time.Time
joinTime time.Time
timezone *time.Location
}
type backlogMessage struct {
timestamp time.Time
senderName string
text string
}
// TODO: have a web dashboard that shows logs
func main() {
go func() {
err := http.ListenAndServe(fmt.Sprintf(":%d", profilePort), nil)
if err != nil {
l.Println(err)
}
}()
devbot = green.Paint("devbot")
rand.Seed(time.Now().Unix())
readBans()
c := make(chan os.Signal, 2)
signal.Notify(c, os.Interrupt, syscall.SIGTERM, syscall.SIGHUP)
go func() {
<-c
fmt.Println("Shutting down...")
saveBans()
logfile.Close()
time.AfterFunc(time.Second, func() {
l.Println("Broadcast taking too long, exiting server early.")
os.Exit(4)
})
universeBroadcast(devbot, "Server going down! This is probably because it is being updated. Try joining back immediately. \n"+
"If you still can't join, try joining back in 2 minutes. If you _still_ can't join, make an issue at github.com/quackduck/devzat/issues")
os.Exit(0)
}()
ssh.Handle(func(s ssh.Session) {
u := newUser(s)
if u == nil {
s.Close()
return
}
defer func() { // crash protection
if i := recover(); i != nil {
mainRoom.broadcast(devbot, "Slap the developers in the face for me, the server almost crashed, also tell them this: "+fmt.Sprint(i)+", stack: "+string(debug.Stack()))
}
}()
u.repl()
})
var err error
if os.Getenv("PORT") != "" {
port, err = strconv.Atoi(os.Getenv("PORT"))
if err != nil {
fmt.Println(err)
return
}
}
// Check for global offline for backwards compatibility
if os.Getenv("DEVZAT_OFFLINE") != "" {
offlineSlack = true
offlineTwitter = true
}
fmt.Printf("Starting chat server on port %d and profiling on port %d\n", port, profilePort)
go getMsgsFromSlack()
go func() {
if port == 22 {
fmt.Println("Also starting chat server on port 443")
err = ssh.ListenAndServe(":443", nil, ssh.HostKeyFile(os.Getenv("HOME")+"/.ssh/id_rsa"))
if err != nil {
fmt.Println(err)
}
}
}()
err = ssh.ListenAndServe(fmt.Sprintf(":%d", port), nil, ssh.HostKeyFile(os.Getenv("HOME")+"/.ssh/id_rsa"), ssh.PublicKeyAuth(func(ctx ssh.Context, key ssh.PublicKey) bool {
return true // allow all keys, this lets us hash pubkeys later
}))
if err != nil {
fmt.Println(err)
}
}
func universeBroadcast(senderName, msg string) {
for _, r := range rooms {
r.broadcast(senderName, msg)
}
}
func (r *room) broadcast(senderName, msg string) {
if msg == "" {
return
}
if senderName != "" {
slackChan <- "[" + r.name + "] " + senderName + ": " + msg
} else {
slackChan <- "[" + r.name + "] " + msg
}
r.broadcastNoSlack(senderName, msg)
}
func (r *room) broadcastNoSlack(senderName, msg string) {
if msg == "" {
return
}
msg = strings.ReplaceAll(msg, "@everyone", green.Paint("everyone\a"))
r.usersMutex.Lock()
for i := range r.users {
msg = strings.ReplaceAll(msg, "@"+stripansi.Strip(r.users[i].name), r.users[i].name)
msg = strings.ReplaceAll(msg, `\`+r.users[i].name, "@"+stripansi.Strip(r.users[i].name)) // allow escaping
}
for i := range r.users {
r.users[i].writeln(senderName, msg)
}
r.usersMutex.Unlock()
if r == mainRoom {
backlog = append(backlog, backlogMessage{time.Now(), senderName, msg + "\n"})
if len(backlog) > scrollback {
backlog = backlog[len(backlog)-scrollback:]
}
}
}
func autocompleteCallback(u *user, line string, pos int, key rune) (string, int, bool) {
if key == '\t' {
// Autocomplete a username
// Split the input string to look for @<name>
words := strings.Fields(line)
toAdd := userMentionAutocomplete(u, words)
if toAdd != "" {
return line + toAdd, pos + len(toAdd), true
}
toAdd = roomAutocomplete(u, words)
if toAdd != "" {
return line + toAdd, pos + len(toAdd), true
}
//return line + toAdd + " ", pos + len(toAdd) + 1, true
}
return "", pos, false
}
func userMentionAutocomplete(u *user, words []string) string {
if len(words) < 1 {
return ""
}
// Check the last word and see if it's trying to refer to a user
if words[len(words)-1][0] == '@' || (len(words)-1 == 0 && words[0][0] == '=') { // mentioning someone or dm-ing someone
inputWord := words[len(words)-1][1:] // slice the @ or = off
for i := range u.room.users {
strippedName := stripansi.Strip(u.room.users[i].name)
toAdd := strings.TrimPrefix(strippedName, inputWord)
if toAdd != strippedName { // there was a match, and some text got trimmed!
return toAdd + " "
}
}
}
return ""
}
func roomAutocomplete(u *user, words []string) string {
// trying to refer to a room?
if len(words) > 0 && words[len(words)-1][0] == '#' {
// don't slice the # off, since the room name includes it
for name := range rooms {
toAdd := strings.TrimPrefix(name, words[len(words)-1])
if toAdd != name { // there was a match, and some text got trimmed!
return toAdd + " "
}
}
}
return ""
}
func newUser(s ssh.Session) *user {
term := terminal.NewTerminal(s, "> ")
_ = term.SetSize(10000, 10000) // disable any formatting done by term
pty, winChan, _ := s.Pty()
w := pty.Window
host, _, _ := net.SplitHostPort(s.RemoteAddr().String()) // definitely should not give an err
toHash := ""
pubkey := s.PublicKey()
if pubkey != nil {
toHash = string(pubkey.Marshal())
} else { // If we can't get the public key fall back to the IP.
toHash = host
}
u := &user{
name: "",
pronouns: []string{"unset"},
session: s,
term: term,
bell: true,
colorBG: "bg-off", // the FG will be set randomly
id: shasum(toHash),
addr: host,
win: w,
lastTimestamp: time.Now(),
joinTime: time.Now(),
room: mainRoom}
go func() {
for u.win = range winChan {
}
}()
l.Println("Connected " + u.name + " [" + u.id + "]")
if bansContains(bans, u.addr, u.id) {
l.Println("Rejected " + u.name + " [" + host + "]")
u.writeln(devbot, "**You are banned**. If you feel this was a mistake, please reach out at github.com/quackduck/devzat/issues or email igoel.mail@gmail.com. Please include the following information: [ID "+u.id+"]")
u.closeQuietly()
return nil
}
idsInMinToTimes[u.id]++
time.AfterFunc(60*time.Second, func() {
idsInMinToTimes[u.id]--
})
if idsInMinToTimes[u.id] > 6 {
bans = append(bans, ban{u.addr, u.id})
mainRoom.broadcast(devbot, "`"+s.User()+"` has been banned automatically. ID: "+u.id)
return nil
}
clearCMD("", u) // always clear the screen on connect
valentines(u)
if len(backlog) > 0 {
lastStamp := backlog[0].timestamp
u.rWriteln(printPrettyDuration(u.joinTime.Sub(lastStamp)) + " earlier")
for i := range backlog {
if backlog[i].timestamp.Sub(lastStamp) > time.Minute {
lastStamp = backlog[i].timestamp
u.rWriteln(printPrettyDuration(u.joinTime.Sub(lastStamp)) + " earlier")
}
u.writeln(backlog[i].senderName, backlog[i].text)
}
}
if err := u.pickUsernameQuietly(s.User()); err != nil { // user exited or had some error
l.Println(err)
s.Close()
return nil
}
mainRoom.usersMutex.Lock()
mainRoom.users = append(mainRoom.users, u)
go sendCurrentUsersTwitterMessage()
mainRoom.usersMutex.Unlock()
u.term.SetBracketedPasteMode(true) // experimental paste bracketing support
term.AutoCompleteCallback = func(line string, pos int, key rune) (string, int, bool) {
return autocompleteCallback(u, line, pos, key)
}
switch len(mainRoom.users) - 1 {
case 0:
u.writeln("", blue.Paint("Welcome to the chat. There are no more users"))
case 1:
u.writeln("", yellow.Paint("Welcome to the chat. There is one more user"))
default:
u.writeln("", green.Paint("Welcome to the chat. There are", strconv.Itoa(len(mainRoom.users)-1), "more users"))
}
mainRoom.broadcast(devbot, u.name+" has joined the chat")
return u
}
func valentines(u *user) {
if time.Now().Month() == time.February && (time.Now().Day() == 14 || time.Now().Day() == 15 || time.Now().Day() == 13) {
// TODO: add a few more random images
u.writeln("", "")
//u.term.Write([]byte("\u001B[A\u001B[2K\u001B[A\u001B[2K")) // delete last line of rendered markdown
time.Sleep(time.Second)
// clear screen
clearCMD("", u)
}
}
// cleanupRoom deletes a room if it's empty and isn't the main room
func cleanupRoom(r *room) {
if r != mainRoom && len(r.users) == 0 {
delete(rooms, r.name)
}
}
// Removes a user and prints Twitter and chat message
func (u *user) close(msg string) {
u.closeOnce.Do(func() {
u.closeQuietly()
go sendCurrentUsersTwitterMessage()
if time.Since(u.joinTime) > time.Minute/2 {
msg += ". They were online for " + printPrettyDuration(time.Since(u.joinTime))
}
u.room.broadcast(devbot, msg)
u.room.users = remove(u.room.users, u)
cleanupRoom(u.room)
})
}
// Removes a user silently, used to close banned users
func (u *user) closeQuietly() {
u.room.usersMutex.Lock()
u.room.users = remove(u.room.users, u)
u.room.usersMutex.Unlock()
u.session.Close()
}
func (u *user) writeln(senderName string, msg string) {
if strings.Contains(msg, u.name) { // is a ping
msg += "\a"
}
msg = strings.ReplaceAll(msg, `\n`, "\n")
msg = strings.ReplaceAll(msg, `\`+"\n", `\n`) // let people escape newlines
if senderName != "" {
if strings.HasSuffix(senderName, " <- ") || strings.HasSuffix(senderName, " -> ") { // TODO: kinda hacky DM detection
msg = strings.TrimSpace(mdRender(msg, lenString(senderName), u.win.Width))
msg = senderName + msg + "\a"
} else {
msg = strings.TrimSpace(mdRender(msg, lenString(senderName)+2, u.win.Width))
msg = senderName + ": " + msg
}
} else {
msg = strings.TrimSpace(mdRender(msg, 0, u.win.Width)) // No sender
}
if time.Since(u.lastTimestamp) > time.Minute {
if u.timezone == nil {
u.rWriteln(printPrettyDuration(time.Since(u.joinTime)) + " in")
} else {
if u.formatTime24 {
u.rWriteln(time.Now().In(u.timezone).Format("15:04"))
} else {
u.rWriteln(time.Now().In(u.timezone).Format("3:04 pm"))
}
}
u.lastTimestamp = time.Now()
}
if u.pingEverytime && senderName != u.name {
msg += "\a"
}
if !u.bell {
msg = strings.ReplaceAll(msg, "\a", "")
}
_, err := u.term.Write([]byte(msg + "\n"))
if err != nil {
u.close(u.name + "has left the chat because of an error writing to their terminal: " + err.Error())
}
}
// Write to the right of the user's window
func (u *user) rWriteln(msg string) {
if u.win.Width-lenString(msg) > 0 {
u.term.Write([]byte(strings.Repeat(" ", u.win.Width-lenString(msg)) + msg + "\n"))
} else {
u.term.Write([]byte(msg + "\n"))
}
}
// pickUsernameQuietly changes the user's username, broadcasting a name change notification if needed.
// An error is returned if the username entered had a bad word or reading input failed.
func (u *user) pickUsername(possibleName string) error {
oldName := u.name
err := u.pickUsernameQuietly(possibleName)
if err != nil {
return err
}
if stripansi.Strip(u.name) != stripansi.Strip(oldName) && stripansi.Strip(u.name) != possibleName { // did the name change, and is it not what the user entered?
u.room.broadcast(devbot, oldName+" is now called "+u.name)
}
return nil
}
// pickUsernameQuietly is like pickUsername but does not
func (u *user) pickUsernameQuietly(possibleName string) error {
possibleName = cleanName(possibleName)
var err error
for {
if possibleName == "" {
} else if strings.HasPrefix(possibleName, "#") || possibleName == "devbot" {
u.writeln("", "Your username is invalid. Pick a different one:")
} else if otherUser, dup := userDuplicate(u.room, possibleName); dup {
if otherUser == u {
break // allow selecting the same name as before
}
u.writeln("", "Your username is already in use. Pick a different one:")
} else {
possibleName = cleanName(possibleName)
break
}
u.term.SetPrompt("> ")
possibleName, err = u.term.ReadLine()
if err != nil {
return err
}
possibleName = cleanName(possibleName)
}
if detectBadWords(possibleName) { // sadly this is necessary
banUser("devbot [grow up]", u)
return errors.New(u.name + "'s username contained a bad word")
}
u.name = possibleName
//u.initColor()
if rand.Float64() <= 0.4 { // 40% chance of being a random color
// changeColor also sets prompt
u.changeColor("random") //nolint:errcheck // we know "random" is a valid color
return nil
}
u.changeColor(styles[rand.Intn(len(styles))].name) //nolint:errcheck // we know this is a valid color
return nil
}
func (u *user) displayPronouns() string {
result := ""
for i := 0; i < len(u.pronouns); i++ {
str, _ := applyColorToData(u.pronouns[i], u.color, u.colorBG)
result += "/" + str
}
if result == "" {
return result
}
return result[1:]
}
func (u *user) changeRoom(r *room) {
if u.room == r {
return
}
u.room.users = remove(u.room.users, u)
u.room.broadcast("", u.name+" is joining "+blue.Paint(r.name)) // tell the old room
cleanupRoom(u.room)
u.room = r
if _, dup := userDuplicate(u.room, u.name); dup {
u.pickUsername("") //nolint:errcheck // if reading input failed the next repl will err out
}
u.room.users = append(u.room.users, u)
u.room.broadcast(devbot, u.name+" has joined "+blue.Paint(u.room.name))
}
func (u *user) repl() {
for {
line, err := u.term.ReadLine()
if err == io.EOF {
u.close(u.name + " has left the chat")
return
}
line += "\n"
hasNewlines := false
//oldPrompt := u.name + ": "
for err == terminal.ErrPasteIndicator {
hasNewlines = true
//u.term.SetPrompt(strings.Repeat(" ", lenString(u.name)+2))
u.term.SetPrompt("")
additionalLine := ""
additionalLine, err = u.term.ReadLine()
additionalLine = strings.ReplaceAll(additionalLine, `\n`, `\\n`)
//additionalLine = strings.ReplaceAll(additionalLine, "\t", strings.Repeat(" ", 8))
line += additionalLine + "\n"
}
u.term.SetPrompt(u.name + ": ")
line = strings.TrimSpace(line)
if err != nil {
l.Println(u.name, err)
u.close(u.name + " has left the chat due to an error: " + err.Error())
return
}
//fmt.Println("window", u.win)
if hasNewlines {
calculateLinesTaken(u, u.name+": "+line, u.win.Width)
} else {
u.term.Write([]byte(strings.Repeat("\033[A\033[2K", int(math.Ceil(float64(lenString(u.name+line)+2)/(float64(u.win.Width))))))) // basically, ceil(length of line divided by term width)
}
//u.term.Write([]byte(strings.Repeat("\033[A\033[2K", calculateLinesTaken(u.name+": "+line, u.win.Width))))
if line == "" {
continue
}
antispamMessages[u.id]++
time.AfterFunc(5*time.Second, func() {
antispamMessages[u.id]--
})
if antispamMessages[u.id] >= 30 {
u.room.broadcast(devbot, u.name+", stop spamming or you could get banned.")
}
if antispamMessages[u.id] >= 50 {
if !bansContains(bans, u.addr, u.id) {
bans = append(bans, ban{u.addr, u.id})
saveBans()
}
u.writeln(devbot, "anti-spam triggered")
u.close(red.Paint(u.name + " has been banned for spamming"))
return
}
line = replaceSlackEmoji(line)
runCommands(line, u)
}
}
func replaceSlackEmoji(input string) string {
if len(input) < 4 {
return input
}
emojiName := ""
result := make([]byte, 0, len(input))
inEmojiName := false
for i := 0; i < len(input)-1; i++ {
if inEmojiName {
emojiName += string(input[i]) // end result: if input contains "::lol::", emojiName will contain ":lol:". "::lol:: ::cat::" => ":lol::cat:"
}
if input[i] == ':' && input[i+1] == ':' {
inEmojiName = !inEmojiName
}
//if !inEmojiName {
result = append(result, input[i])
//}
}
result = append(result, input[len(input)-1])
if emojiName != "" {
toAdd := fetchEmoji(strings.Split(strings.ReplaceAll(emojiName[1:len(emojiName)-1], "::", ":"), ":")) // cut the ':' at the start and end
result = append(result, toAdd...)
}
return string(result)
}
// accepts a ':' separated list of emoji
func fetchEmoji(names []string) string {
if offlineSlack {
return ""
}
result := ""
for _, name := range names {
result += fetchEmojiSingle(name)
}
return result
}
func fetchEmojiSingle(name string) string {
if offlineSlack {
return ""
}
r, err := http.Get("https://e.benjaminsmith.dev/" + name)
if err != nil {
return ""
}
defer r.Body.Close()
if r.StatusCode != 200 {
return ""
}
return ""
}
// may contain a bug ("may" because it could be the terminal's fault)
func calculateLinesTaken(u *user, s string, width int) {
s = stripansi.Strip(s)
//fmt.Println("`"+s+"`", "width", width)
pos := 0
//lines := 1
u.term.Write([]byte("\033[A\033[2K"))
currLine := ""
for _, c := range s {
pos++
currLine += string(c)
if c == '\t' {
pos += 8
}
if c == '\n' || pos > width {
pos = 1
//lines++
u.term.Write([]byte("\033[A\033[2K"))
}
//fmt.Println(string(c), "`"+currLine+"`", "pos", pos, "lines", lines)
}
//return lines
}
// bansContains reports if the addr or id is found in the bans list
func bansContains(b []ban, addr string, id string) bool {
for i := 0; i < len(b); i++ {
if b[i].Addr == addr || b[i].ID == id {
return true
}
}
return false
}