-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
179 lines (142 loc) · 3.12 KB
/
main.go
File metadata and controls
179 lines (142 loc) · 3.12 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
package main
import (
"bufio"
"flag"
"fmt"
"math/rand"
"net"
"strconv"
"strings"
"time"
)
type User struct {
Username string
Color string
Message string
}
const (
twitchServer = "irc.chat.twitch.tv:6667"
password = "SCHMOOPIIE"
defaultColor = "#FFFFFF"
)
var writer *bufio.Writer
func main() {
channelName := flag.String("channel", "", "Auto connect to channel")
flag.Parse()
setTitle("Twitch Chat")
r := rand.New(rand.NewSource(time.Now().UnixNano()))
nickname := "justinfan" + strconv.Itoa(r.Intn(90000)+10000)
channel := *channelName
if channel == "" {
fmt.Print("Enter channel: ")
_, err := fmt.Scanln(&channel)
if err != nil {
fmt.Println("Error reading channel")
return
}
}
fmt.Println("Connecting to Twitch IRC")
dial, err := net.Dial("tcp", twitchServer)
if err != nil {
panic(err)
}
defer dial.Close()
fmt.Println("Connected to Twitch IRC")
writer = bufio.NewWriter(dial)
reader := bufio.NewReader(dial)
fmt.Println("Trying to login")
writeToChat("CAP REQ :twitch.tv/tags")
writeToChat("PASS " + password)
writeToChat("NICK " + nickname)
_ = writer.Flush()
for {
line, err := reader.ReadString('\n')
if err != nil {
panic(err)
}
splits := strings.SplitN(line, " ", 3)
if isInt(splits[1]) {
if splits[1] == "001" {
fmt.Println("Logged in")
writeToChat("JOIN #" + channel)
fmt.Println("Join channel")
_ = writer.Flush()
}
continue
}
if splits[1] == "JOIN" {
setTitle("Twitch Chat - " + channel)
clearScreen()
continue
}
if strings.HasPrefix(line, "@") {
user, err := getUser(line)
if err != nil {
fmt.Println(err)
}
fmt.Print(colorize(user.Username, user.Color) + ": " + user.Message)
continue
}
}
}
func getUser(s string) (User, error) {
var user User
for _, part := range strings.Split(s, ";") {
splitN := strings.SplitN(part, "=", 2)
if len(splitN) != 2 {
continue
}
key := splitN[0]
value := splitN[1]
switch key {
case "color":
if value == "" {
user.Color = defaultColor
} else {
user.Color = value
}
case "user-type", "vip":
if value == "" {
continue
}
message, err := parseUserMessage(value)
if err != nil {
return User{}, fmt.Errorf("fehler beim Parsen der Nachricht: %w", err)
}
user.Message = message
case "display-name":
user.Username = value
}
}
return user, nil
}
func parseUserMessage(s string) (string, error) {
splits := strings.SplitN(s, ":", 2)
if len(splits) != 2 {
return "", fmt.Errorf("failed to parse user message: %s", s)
}
message := strings.SplitN(splits[1], " ", 4)[3][1:]
return message, nil
}
func colorize(text string, hexColor string) string {
var r, g, b int
_, err := fmt.Sscanf(hexColor, "#%02x%02x%02x", &r, &g, &b)
if err != nil {
return ""
}
return fmt.Sprintf("\033[38;2;%d;%d;%dm%s\033[0m", r, g, b, text)
}
func clearScreen() {
fmt.Print("\033[H\033[2J")
}
func setTitle(s string) {
fmt.Printf("\033]0;%s\007", s)
}
func isInt(s string) bool {
_, err := strconv.Atoi(s)
return err == nil
}
func writeToChat(s string) {
_, _ = writer.WriteString(s)
_, _ = writer.WriteString("\r\n")
}