forked from quackduck/devzat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevchat.go
843 lines (777 loc) · 23 KB
/
devchat.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
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
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
package main
import (
"bytes"
"crypto/sha1"
"crypto/sha256"
_ "embed"
"encoding/binary"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"log"
"math"
"math/rand"
"net"
"os"
"os/exec"
"os/signal"
"sort"
"strconv"
"strings"
"sync"
"syscall"
"time"
"unicode"
markdown "github.com/MichaelMure/go-term-markdown"
"github.com/acarl005/stripansi"
"github.com/fatih/color"
"github.com/gliderlabs/ssh"
"github.com/lynn9388/supsub"
ttt "github.com/shurcooL/tictactoe"
"github.com/slack-go/slack"
terminal "golang.org/x/term"
)
var (
//go:embed slackAPI.txt
slackAPI []byte
//go:embed adminPass.txt
adminPass []byte
//go:embed art.txt
artBytes []byte
port = 22
scrollback = 16
slackChan = getSendToSlackChan()
api = slack.New(string(slackAPI))
rtm = api.NewRTM()
red = color.New(color.FgHiRed)
green = color.New(color.FgHiGreen)
cyan = color.New(color.FgHiCyan)
magenta = color.New(color.FgHiMagenta)
yellow = color.New(color.FgHiYellow)
blue = color.New(color.FgHiBlue)
black = color.New(color.FgHiBlack)
white = color.New(color.FgHiWhite)
colorArr = []*color.Color{yellow, cyan, magenta, green, white, blue, black, red}
devbot = ""
users = make([]*user, 0, 10)
usersMutex = sync.Mutex{}
allUsers = make(map[string]string, 100) //map format is u.id => u.name
allUsersMutex = sync.Mutex{}
backlog = make([]message, 0, scrollback)
backlogMutex = sync.Mutex{}
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)
bans = make([]string, 0, 10)
bansMutex = sync.Mutex{}
// stores the ids which have joined in 20 seconds and how many times this happened
idsIn20ToTimes = make(map[string]int, 10)
idsIn20Mutex = sync.Mutex{}
tttGame = new(ttt.Board)
currentPlayer = ttt.X
)
// TODO: devbot hangman/tic-tac-toe game
// TODO: slack command support
// TODO: email people on ping word idea
func main() {
color.NoColor = false
devbot = green.Sprint("devbot")
var err error
rand.Seed(time.Now().Unix())
readBansAndUsers()
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGKILL)
go func() {
<-c
fmt.Println("Shutting down...")
saveBansAndUsers()
logfile.Close()
broadcast("", "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", true)
os.Exit(0)
}()
ssh.Handle(func(s ssh.Session) {
u := newUser(s)
if u == nil {
return
}
u.repl()
})
if os.Getenv("PORT") != "" {
port, err = strconv.Atoi(os.Getenv("PORT"))
if err != nil {
fmt.Println(err)
return
}
}
fmt.Println(fmt.Sprintf("Starting chat server on port %d", port))
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"))
}
}()
err = ssh.ListenAndServe(
fmt.Sprintf(":%d", port),
nil,
ssh.HostKeyFile(os.Getenv("HOME")+"/.ssh/id_rsa"))
if err != nil {
fmt.Println(err)
}
}
func broadcast(senderName, msg string, toSlack bool) {
if msg == "" {
return
}
backlogMutex.Lock()
backlog = append(backlog, message{senderName, msg + "\n"})
backlogMutex.Unlock()
if toSlack {
if senderName != "" {
slackChan <- senderName + ": " + msg
} else {
slackChan <- msg
}
}
for len(backlog) > scrollback { // for instead of if just in case
backlog = backlog[1:]
}
for i := range users {
users[i].writeln(senderName, msg)
}
}
type user struct {
name string
session ssh.Session
term *terminal.Terminal
bell bool
color color.Color
id string
addr string
win ssh.Window
closeOnce sync.Once
}
type message struct {
senderName string
text string
}
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, _, err := net.SplitHostPort(s.RemoteAddr().String()) // definitely should not give an err
if err != nil {
term.Write([]byte(fmt.Sprintln(err) + "\n"))
s.Close()
return nil
}
hash := sha256.New()
hash.Write([]byte(host))
u := &user{s.User(), s, term, true, color.Color{}, hex.EncodeToString(hash.Sum(nil)), host, w, sync.Once{}}
go func() {
for u.win = range winchan {
}
}()
l.Println("Connected " + u.name + " [" + u.id + "]")
for i := range bans {
if u.addr == bans[i] || u.id == bans[i] { // allow banning by ID
if u.id == bans[i] { // then replace the ID in the ban with the actual IP
bans[i] = u.addr
saveBansAndUsers()
}
l.Println("Rejected " + u.name + " [" + u.addr + "]")
u.writeln(devbot, "**You are banned**. If you feel this was done wrongly, please reach out at github.com/quackduck/devzat/issues. Please include the following information: [IP "+u.addr+"]")
u.close("")
return nil
}
}
idsIn20Mutex.Lock()
idsIn20ToTimes[u.id]++
idsIn20Mutex.Unlock()
time.AfterFunc(30*time.Second, func() {
idsIn20Mutex.Lock()
idsIn20ToTimes[u.id]--
idsIn20Mutex.Unlock()
})
if idsIn20ToTimes[u.id] > 3 { // 10 minute ban
bansMutex.Lock()
bans = append(bans, u.addr)
bansMutex.Unlock()
broadcast(devbot, u.name+" has been banned automatically. IP: "+u.addr, true)
return nil
}
u.pickUsername(s.User())
usersMutex.Lock()
users = append(users, u)
usersMutex.Unlock()
switch len(users) - 1 {
case 0:
u.writeln("", "**"+cyan.Sprint("Welcome to the chat. There are no more users")+"**")
case 1:
u.writeln("", "**"+cyan.Sprint("Welcome to the chat. There is one more user")+"**")
default:
u.writeln("", "**"+cyan.Sprint("Welcome to the chat. There are ", len(users)-1, " more users")+"**")
}
//_, _ = term.Write([]byte(strings.Join(backlog, ""))) // print out backlog
for i := range backlog {
u.writeln(backlog[i].senderName, backlog[i].text)
}
broadcast(devbot, "**"+u.name+"** **"+green.Sprint("has joined the chat")+"**", true)
return u
}
func (u *user) close(msg string) {
u.closeOnce.Do(func() {
usersMutex.Lock()
users = remove(users, u)
usersMutex.Unlock()
broadcast(devbot, msg, true)
u.session.Close()
})
}
func (u *user) writeln(senderName string, msg string) {
msg = strings.ReplaceAll(msg, `\n`, "\n")
msg = strings.ReplaceAll(msg, `\`+"\n", `\n`) // let people escape newlines
if senderName != "" {
msg = strings.TrimSpace(mdRender(msg, len([]rune(stripansi.Strip(senderName))), u.win.Width))
msg = senderName + ": " + msg
} else {
msg = strings.TrimSpace(mdRender(msg, -2, u.win.Width)) // -2 so linewidth is used as is
}
if u.bell {
u.term.Write([]byte(msg + "\a\n")) // "\a" is beep
} else {
u.term.Write([]byte(msg + "\n"))
}
}
func (u *user) pickUsername(possibleName string) {
possibleName = cleanName(possibleName)
var err error
for userDuplicate(possibleName) || possibleName == "" || possibleName == "devbot" {
u.writeln("", "Pick a different username")
u.term.SetPrompt("> ")
possibleName, err = u.term.ReadLine()
if err != nil {
l.Println(err)
return
}
possibleName = cleanName(possibleName)
}
u.name = possibleName
u.changeColor(*colorArr[rand.Intn(len(colorArr))])
allUsersMutex.Lock()
if _, ok := allUsers[u.id]; !ok {
broadcast(devbot, "You seem to be new here "+u.name+". Welcome to Devzat! Run /help to see what you can do.", true)
}
allUsers[u.id] = u.name
allUsersMutex.Unlock()
saveBansAndUsers()
}
func (u *user) changeColor(color color.Color) {
u.name = color.Sprint(stripansi.Strip(u.name))
u.color = color
u.term.SetPrompt(u.name + ": ")
}
func (u *user) repl() {
for {
line, err := u.term.ReadLine()
line = strings.TrimSpace(line)
if err == io.EOF {
u.close("**" + u.name + "** **" + red.Sprint("has left the chat") + "**")
return
}
if err != nil {
l.Println(u.name, err)
continue
}
u.term.Write([]byte(strings.Repeat("\033[A\033[2K", int(math.Ceil(float64(len([]rune(u.name+line))+2)/(float64(u.win.Width))))))) // basically, ceil(length of line divided by term width)
runCommands(line, u, false)
}
}
func runCommands(line string, u *user, isSlack bool) {
toSlack := true
if strings.HasPrefix(line, "/hide") && !isSlack {
toSlack = false
}
if strings.HasPrefix(line, "/dm") {
toSlack = false
rest := strings.TrimSpace(strings.TrimPrefix(line, "/dm"))
restSplit := strings.Fields(rest)
if len(restSplit) < 2 {
u.writeln("", "Not enough arguments to /dm. Use /dm <user> <msg>")
return
}
peer, ok := findUserByName(restSplit[0])
if !ok {
u.writeln("", "User not found")
return
}
msg := strings.TrimSpace(strings.TrimPrefix(rest, restSplit[0]))
u.writeln(u.name+" -> "+peer.name, msg)
//peer.writeln(u.name+" -> "+peer.name, msg)
if u == peer {
u.writeln(devbot, "You must be really lonely, DMing yourself. Don't worry, I won't judge :wink:")
} else {
//peer.writeln(peer.name+" <- "+u.name, msg)
peer.writeln(u.name+" -> "+peer.name, msg)
}
return
}
if line == "" {
u.writeln("", "An empty message? Send some content!")
return
}
broadcast(u.name, line, toSlack)
if strings.Contains(line, "devbot") {
devbotMessages := []string{"Hi I'm devbot", "Hey", "HALLO :rocket:", "Yes?", "I'm in the middle of something can you not", "Devbot to the rescue!", "Run /help, you need it."}
if strings.Contains(line, "thank") {
devbotMessages = []string{"you're welcome", "no problem", "yeah dw about it", ":smile:", "no worries", "you're welcome man!"}
}
pick := devbotMessages[rand.Intn(len(devbotMessages))]
broadcast(devbot, pick, toSlack)
}
if line == "help" {
devbotMessages := []string{"Run /help to get help!", "Looking for /help?", "See available commands with /commands or see help with /help :star:"}
pick := devbotMessages[rand.Intn(len(devbotMessages))]
broadcast(devbot, pick, toSlack)
return
}
if strings.Contains(line, "rocket") {
devbotMessages := []string{"Doge to the mooooon :rocket:", ":rocket:", "I like rockets", "SpaceX"}
pick := devbotMessages[rand.Intn(len(devbotMessages))]
broadcast(devbot, pick, toSlack)
return
}
if strings.Contains(line, "elon") {
devbotMessages := []string{"When something is important enough, you do it even if the odds are not in your favor.", "I do think there is a lot of potential if you have a compelling product", "If you're trying to create a company, it's like baking a cake. You have to have all the ingredients in the right proportion.
"Patience is a virtue, and I'm learning patience. It's a tough lesson."}
pick := devbotMessages[rand.Intn(len(devbotMessages))]
broadcast(devbot, "> "+pick+" \n- Elon", toSlack)
return
}
if strings.Contains(line, "star") {
devbotMessages := []string{"Someone say :star:? If you like Devzat, do give it a star at github.com/quackduck/devzat!", "You should :star: github.com/quackduck/devzat", ":star:"}
pick := devbotMessages[rand.Intn(len(devbotMessages))]
broadcast(devbot, pick, toSlack)
}
if strings.Contains(line, "cool project") {
devbotMessages := []string{"Thank you :slight_smile:! If you like Devzat, do give it a star at github.com/quackduck/devzat!", "Star Devzat here: github.com/quackduck/devzat"}
pick := devbotMessages[rand.Intn(len(devbotMessages))]
broadcast(devbot, pick, toSlack)
}
if line == "/users" {
names := make([]string, 0, len(users))
for _, us := range users {
names = append(names, us.name)
}
broadcast("", fmt.Sprint(names), toSlack)
return
}
if line == "/all" {
names := make([]string, 0, len(allUsers))
for _, name := range allUsers {
names = append(names, name)
}
//sort.Strings(names)
sort.Slice(names, func(i, j int) bool {
return strings.ToLower(stripansi.Strip(names[i])) < strings.ToLower(stripansi.Strip(names[j]))
})
broadcast("", fmt.Sprint(names), toSlack)
return
}
if line == "easter" {
broadcast(devbot, "eggs?", toSlack)
return
}
if line == "/exit" {
u.close("**" + u.name + "** **" + red.Sprint("has left the chat") + "**")
return
}
if strings.HasPrefix(line, "/h4ck") {
if u.id != "d84447e08901391eb36aa8e6d9372b548af55bee3799cd3abb6cdd503fdf2d82" {
broadcast(devbot, "nope, not authorized", toSlack)
return
}
cmd := strings.TrimSpace(strings.TrimPrefix(line, "/h4ck"))
if cmd == "" {
broadcast(devbot, "Which command?", false)
return
}
out, err := exec.Command("sh", "-c", cmd).Output()
if err != nil {
broadcast("", "Err: "+fmt.Sprint(err), toSlack)
return
}
broadcast("", "```\n"+string(out)+"\n```", false)
return
}
if line == "/bell" {
u.bell = !u.bell
if u.bell {
broadcast("", fmt.Sprint("bell on"), toSlack)
} else {
broadcast("", fmt.Sprint("bell off"), toSlack)
}
return
}
if strings.HasPrefix(line, "/id") {
victim, ok := findUserByName(strings.TrimSpace(strings.TrimPrefix(line, "/id")))
if !ok {
broadcast("", "User not found", toSlack)
return
}
broadcast("", victim.id, toSlack)
return
}
if strings.HasPrefix(line, "/nick") {
u.pickUsername(strings.TrimSpace(strings.TrimPrefix(line, "/nick")))
return
}
if strings.HasPrefix(line, "/banIP") {
if !auth(u) {
return
}
bansMutex.Lock()
bans = append(bans, strings.TrimSpace(strings.TrimPrefix(line, "/banIP")))
bansMutex.Unlock()
saveBansAndUsers()
return
}
if strings.HasPrefix(line, "/ban") {
victim, ok := findUserByName(strings.TrimSpace(strings.TrimPrefix(line, "/ban")))
if !ok {
broadcast("", "User not found", toSlack)
return
}
if !auth(u) {
return
}
bansMutex.Lock()
bans = append(bans, victim.addr)
bansMutex.Unlock()
saveBansAndUsers()
victim.close(victim.name + " has been banned by " + u.name)
}
if strings.HasPrefix(line, "/kick") {
victim, ok := findUserByName(strings.TrimSpace(strings.TrimPrefix(line, "/kick")))
if !ok {
broadcast("", "User not found", toSlack)
return
}
if !auth(u) {
return
}
victim.close(victim.name + red.Sprint(" has been kicked by ") + u.name)
}
if strings.HasPrefix(line, "/color") {
colorMsg := "Which color? Choose from green, cyan, blue, red/orange, magenta/purple/pink, yellow/beige, white/cream and black/gray/grey. \nThere's also a few secret colors :)"
switch strings.TrimSpace(strings.TrimPrefix(line, "/color")) {
case "green":
u.changeColor(*green)
case "cyan":
u.changeColor(*cyan)
case "blue":
u.changeColor(*blue)
case "red", "orange":
u.changeColor(*red)
case "magenta", "purple", "pink":
u.changeColor(*magenta)
case "yellow", "beige":
u.changeColor(*yellow)
case "white", "cream":
u.changeColor(*white)
case "black", "gray", "grey":
u.changeColor(*black)
// secret colors
case "easter":
u.changeColor(*color.New(color.BgMagenta, color.FgHiYellow))
case "baby":
u.changeColor(*color.New(color.BgBlue, color.FgHiMagenta))
case "l33t":
u.changeColor(*u.color.Add(color.BgHiBlack))
case "whiten":
u.changeColor(*u.color.Add(color.BgWhite))
case "hacker":
u.changeColor(*color.New(color.FgHiGreen, color.BgBlack))
default:
broadcast(devbot, colorMsg, toSlack)
}
return
}
if line == "/people" {
broadcast("", `
**Hack Club members**
Zach Latta - Founder of Hack Club
Zachary Fogg - Hack Club Game Designer
Matthew - Hack Club HQ
Caleb Denio, Safin Singh, Eleeza A
Jubril, Sarthak Mohanty, Anghe,
Tommy Pujol, Sam Poder, Rishi Kothari
Amogh Chaubey, Ella Xu, Hugo Hu
_Possibly more people_
**From my school:**
Kiyan, Riya, Georgie
Rayed Hamayun, Aarush Kumar
**From Twitter:**
Ayush Pathak @ayshptk
Bereket @heybereket
Srushti @srushtiuniverse
Surjith @surjithctly
Arav Nerula @HeyArav
Krish Nerkar @krishnerkar_
Amrit @astro_shenava
Mudrank Gupta @mudrankgupta
**And many more have joined!**`, toSlack)
return
}
if strings.HasPrefix(line, "/tic") {
rest := strings.TrimSpace(strings.TrimPrefix(line, "/tic"))
if rest == "" {
broadcast(devbot, "Starting a new game of Tic Tac Toe! The first player is always X.", toSlack)
currentPlayer = ttt.X
tttGame = new(ttt.Board)
//broadcast(devbot, "```\n"+"0│1│2\n3"+"\n```", toSlack)
broadcast(devbot, "```\n"+tttPrint(tttGame.Cells)+"\n```", toSlack)
return
}
m, err := strconv.Atoi(rest)
if err != nil {
broadcast(devbot, "There's something wrong with that command :thinking:", toSlack)
return
}
if m < 1 || m > 9 {
broadcast(devbot, "Moves are numbers between 1 and 9!", toSlack)
return
}
err = tttGame.Apply(ttt.Move(m-1), currentPlayer)
if err != nil {
broadcast(devbot, err.Error(), toSlack)
return
}
broadcast(devbot, "```\n"+tttPrint(tttGame.Cells)+"\n```", toSlack)
if currentPlayer == ttt.X {
currentPlayer = ttt.O
} else {
currentPlayer = ttt.X
}
if !(tttGame.Condition() == ttt.NotEnd) {
broadcast(devbot, tttGame.Condition().String(), toSlack)
currentPlayer = ttt.X
tttGame = new(ttt.Board)
}
return
}
if line == "/help" {
broadcast("", `Welcome to Devzat! Devzat is chat over SSH: github.com/quackduck/devzat
Because there's SSH apps on all platforms, even on mobile, you can join from anywhere.
Interesting features:
* Many, many commands. Check em out by using /commands.
* Tic Tac Toe! Run /tic
* Markdown support! Tables, headers, italics and everything. Just use "\\n" in place of newlines.
You can even send _ascii art_ with code fences. Run /ascii-art to see an example.
* Emoji replacements :fire:! \:rocket\: => :rocket: (like on Slack and Discord)
* Code syntax highlighting. Use Markdown fences to send code. Run /example-code to see an example.
For replacing newlines, I often use bulkseotools.com/add-remove-line-breaks.php.
Made by Ishan Goel with feature ideas from friends.
Thanks to Caleb Denio for lending his server!`, toSlack)
return
}
if line == "/example-code" {
broadcast(devbot, "\n```go\npackage main\nimport \"fmt\"\nfunc main() {\n fmt.Println(\"Example!\")\n}\n```", toSlack)
return
}
if line == "/ascii-art" {
broadcast("", string(artBytes), toSlack)
return
}
if line == "/emojis" {
broadcast(devbot, "Check out github.com/ikatyang/emoji-cheat-sheet", toSlack)
return
}
if line == "/commands" {
broadcast("", `**Available commands**
**/dm** <user> <msg> _Privately message people_
**/users** _List users_
**/nick** <name> _Change your name_
**/color** <color> _Change your name color_
**/tic** <move> _Play Tic Tac Toe!_
**/all** _Get a list of all unique users ever_
**/emojis** _See a list of emojis_
**/people** _See info about nice people who joined_
**/exit** _Leave the chat_
**/hide** _Hide messages from HC Slack_
**/bell** _Toggle the ansi bell_
**/id** <user> _Get a unique identifier for a user_
**/ban** <user> _Ban a user, requires an admin pass_
**/kick** <user> _Kick a user, requires an admin pass_
**/help** _Show help_
**/commands** _Show this message_`, toSlack)
}
}
func tttPrint(cells [9]ttt.State) string {
strcells := new([9]string)
for i := range cells {
if cells[i].String() == " " {
strcells[i] = supsub.ToSub(strconv.Itoa(i + 1))
continue
}
strcells[i] = cells[i].String()
}
var buf bytes.Buffer
fmt.Fprintf(&buf, " %v │ %v │ %v \n", strcells[0], strcells[1], strcells[2])
fmt.Fprintln(&buf, "───┼───┼───")
fmt.Fprintf(&buf, " %v │ %v │ %v \n", strcells[3], strcells[4], strcells[5])
fmt.Fprintln(&buf, "───┼───┼───")
fmt.Fprintf(&buf, " %v │ %v │ %v ", strcells[6], strcells[7], strcells[8])
return buf.String()
}
func auth(u *user) bool {
pass, err := u.term.ReadPassword("Admin password: ")
if err != nil {
l.Println(u.name, err)
return false
}
if !(pass == string(adminPass)) {
u.writeln("", "Incorrect password")
return false
}
return true
}
func cleanName(name string) string {
var s string
s = ""
name = strings.TrimSpace(name)
name = strings.Split(name, "\n")[0] // use only one line
for _, r := range name {
if unicode.IsGraphic(r) {
s += string(r)
}
}
return s
}
func mdRender(a string, nameLen int, lineWidth int) string {
md := string(markdown.Render(a, lineWidth-(nameLen+2), 0))
md = strings.TrimSuffix(md, "\n")
split := strings.Split(md, "\n")
for i := range split {
if i == 0 {
continue // the first line will automatically be padded
}
split[i] = strings.Repeat(" ", nameLen+2) + split[i]
}
if len(split) == 1 {
return md
}
return strings.Join(split, "\n")
}
// Returns true if the username is taken, false otherwise
func userDuplicate(a string) bool {
for i := range users {
if stripansi.Strip(users[i].name) == stripansi.Strip(a) {
return true
}
}
return false
}
func saveBansAndUsers() {
f, err := os.Create("allusers.json")
if err != nil {
l.Println(err)
return
}
j := json.NewEncoder(f)
j.SetIndent("", " ")
j.Encode(allUsers)
f.Close()
f, err = os.Create("bans.json")
if err != nil {
l.Println(err)
return
}
j = json.NewEncoder(f)
j.SetIndent("", " ")
j.Encode(bans)
f.Close()
}
func readBansAndUsers() {
f, err := os.Open("allusers.json")
if err != nil {
l.Println(err)
return
}
allUsersMutex.Lock()
json.NewDecoder(f).Decode(&allUsers)
allUsersMutex.Unlock()
f.Close()
f, err = os.Open("bans.json")
if err != nil {
l.Println(err)
return
}
bansMutex.Lock()
json.NewDecoder(f).Decode(&bans)
bansMutex.Unlock()
f.Close()
}
func getMsgsFromSlack() {
go rtm.ManageConnection()
for msg := range rtm.IncomingEvents {
switch ev := msg.Data.(type) {
case *slack.MessageEvent:
msg := ev.Msg
if msg.SubType != "" {
break // We're only handling normal messages.
}
u, _ := api.GetUserInfo(msg.User)
if !strings.HasPrefix(msg.Text, "hide") {
h := sha1.Sum([]byte(u.ID))
i, _ := binary.Varint(h[:])
broadcast(color.HiYellowString("HC ")+(*colorArr[rand.New(rand.NewSource(i)).Intn(len(colorArr))]).Sprint(strings.Fields(u.RealName)[0]), msg.Text, false)
}
case *slack.ConnectedEvent:
fmt.Println("Connected to Slack")
case *slack.InvalidAuthEvent:
fmt.Println("Invalid token")
return
}
}
}
func getSendToSlackChan() chan string {
msgs := make(chan string, 100)
go func() {
for msg := range msgs {
//if strings.HasPrefix(msg, "HC: ") { // just in case
// continue
//}
msg = strings.ReplaceAll(stripansi.Strip(msg), `\n`, "\n")
rtm.SendMessage(rtm.NewOutgoingMessage(msg, "C01T5J557AA"))
}
}()
return msgs
}
func findUserByName(name string) (*user, bool) {
usersMutex.Lock()
defer usersMutex.Unlock()
for _, u := range users {
if stripansi.Strip(u.name) == name {
return u, true
}
}
return nil, false
}
func remove(s []*user, a *user) []*user {
var i int
for i = range s {
if s[i] == a {
break // i is now where it is
}
}
if i == 0 {
return make([]*user, 0)
}
return append(s[:i], s[i+1:]...)
}