-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuser_tcp.go
284 lines (250 loc) · 7.31 KB
/
user_tcp.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
package chat
import (
"bufio"
"net"
"strings"
)
const chatHelp = `Hello, welcome to the chat server!
Commands:
/help see this help message again (example: /help)
/listusers see all users connected (example: /listusers)
/listrooms see all channels (example: /listrooms)
/newroom create a new room and join it (example: /newroom random)
/join join a room (example: /join random)
/leave leave a room (example: /leave random)
/mute mute a user (example: /mute rob)
/unmute unmute a user (example: /unmute rob)
/mutes see who you've muted (example: /mutes)
/dm send a message to a user (example: /dm rob: hello!)
`
type command func(tc *tcpUser, arg string)
// commands are each action a client is able to perform besides just sending
// plaintext.
var commands = map[string]command{
"/help": helpCmd,
"/listusers": listUsersCmd,
"/listrooms": listRoomsCmd,
"/newroom": newRoomCmd,
"/join": joinRoomCmd,
"/leave": leaveRoomCmd,
"/mute": muteCmd,
"/unmute": unmuteCmd,
"/mutes": mutesCmd,
"/dm": dmCmd,
}
// a tcpUser represents a telnet user, relying on text-only commands to
// communicate.
type tcpUser struct {
currentRoomName string
muted map[string]bool
username string
r *bufio.Reader
conn net.Conn
send chan<- *message
}
func newTCPUser(conn net.Conn, h *hub) *tcpUser {
var name string
r := bufio.NewReader(conn)
conn.Write([]byte("Please enter your username: "))
for {
n, err := r.ReadString('\n')
if err != nil {
conn.Close()
}
n = strings.TrimSpace(n)
if n == "" {
conn.Write([]byte("Your name cannot be blank. Try again: "))
continue
}
if _, ok := h.users[n]; !ok {
name = n
break
}
conn.Write([]byte("Sorry, the name " + n + " is already taken. Please choose another one: "))
continue
}
return &tcpUser{
currentRoomName: defaultChannelName,
muted: make(map[string]bool),
username: name,
r: bufio.NewReader(conn),
conn: conn,
send: h.messageCh,
}
}
func (tc *tcpUser) read() error {
for {
messageText, err := tc.r.ReadString('\n')
if err != nil {
tc.send <- newMessage("everyone", tc.username, tc.username+" has left that chat\n", quit)
return err
}
if ok := tc.handleCommand(messageText); ok {
continue
}
tc.send <- newMessage(tc.currentRoomName, tc.username, messageText, text)
}
}
func (tc *tcpUser) write(message *message) error {
if _, ok := tc.muted[message.Username]; ok {
return nil
}
switch message.MessageType {
case text:
return tc.writeText("(" + message.Username + " to " + message.Channel + "): " + message.Text)
case listUsers, listChannels:
return tc.writeText(message.Text + "\n")
case join, create:
tc.currentRoomName = message.Channel
return tc.writeText(message.Text)
case leave:
tc.currentRoomName = defaultChannelName
return tc.writeText(message.Text)
case mute:
if _, ok := tc.muted[message.Channel]; !ok {
tc.muted[message.Channel] = true
return tc.writeText(message.Text)
}
return tc.writeText("User " + message.Channel + " is already muted.\n")
case unmute:
if _, ok := tc.muted[message.Channel]; ok {
delete(tc.muted, message.Channel)
return tc.writeText(message.Text)
}
return tc.writeText("User " + message.Channel + " isn't muted.\n")
case dm:
tc.writeText("(" + message.Username + " to " + message.Channel + "): " + message.Text + "\n")
}
return nil
}
func (tc *tcpUser) writeText(text string) error {
_, err := tc.conn.Write([]byte(text))
if err != nil {
return err
}
return nil
}
func (tc *tcpUser) close() {
tc.conn.Close()
}
func (tc *tcpUser) name() string {
return tc.username
}
func (tc *tcpUser) handleCommand(s string) bool {
if !strings.HasPrefix(s, "/") {
return false
}
cmd := strings.TrimSpace(strings.Split(s, " ")[0])
cmdFunc, ok := commands[cmd]
if !ok {
tc.write(newMessage(tc.currentRoomName, tc.username, "Command "+cmd+" doesn't exist\n", text))
return true
}
cmdArg := strings.TrimSpace(strings.TrimPrefix(s, cmd))
cmdFunc(tc, cmdArg)
return true
}
func helpCmd(tc *tcpUser, _ string) {
tc.write(newMessage("you", "server", chatHelp, text))
}
func newRoomCmd(tc *tcpUser, arg string) {
arg = strings.TrimSpace(arg)
if arg == "" {
tc.write(newMessage("you", "server", "Room name cannot be blank\n", text))
return
}
if arg == tc.currentRoomName {
tc.write(newMessage("you", "server", "You're already in that room\n", text))
return
}
tc.send <- newMessage(arg, tc.username, tc.username+" created new channel "+arg, create)
}
func joinRoomCmd(tc *tcpUser, arg string) {
arg = strings.TrimSpace(arg)
if arg == "" {
tc.write(newMessage("you", "server", "Room name cannot be blank\n", text))
return
}
if arg == tc.currentRoomName {
tc.write(newMessage("you", "server", "You're already in that room\n", text))
return
}
tc.send <- newMessage(arg, tc.username, tc.username+" joined channel "+arg, join)
}
func leaveRoomCmd(tc *tcpUser, arg string) {
arg = strings.TrimSpace(arg)
if arg == "" {
tc.write(newMessage("you", "server", "Room name cannot be blank\n", text))
}
tc.send <- newMessage(arg, tc.username, tc.username+" joined channel "+arg, leave)
}
func muteCmd(tc *tcpUser, arg string) {
arg = strings.TrimSpace(arg)
if arg == "" {
tc.writeText("Username cannot be blank\n")
return
}
if arg == tc.username {
tc.writeText("You can't mute yourself\n")
return
}
tc.send <- newMessage(arg, tc.username, "Muted user "+arg+".\n", mute)
}
func unmuteCmd(tc *tcpUser, arg string) {
arg = strings.TrimSpace(arg)
if arg == "" {
tc.writeText("Username cannot be blank\n")
return
}
tc.send <- newMessage(arg, tc.username, "Unmuted user "+arg+".\n", unmute)
}
func mutesCmd(tc *tcpUser, _ string) {
if len(tc.muted) < 1 {
tc.writeText("You haven't muted anyone.\n")
return
}
var mutes []string
for mute := range tc.muted {
mutes = append(mutes, mute)
}
muteList := strings.Join(mutes, "\n - ")
tc.writeText("You've muted:\n - " + muteList + "\n")
}
func dmCmd(tc *tcpUser, arg string) {
i := strings.Index(arg, ":")
if i == -1 {
tc.writeText("/dm command not understood, you're missing a ':' from your command.\n")
return
}
msgs := strings.SplitAfterN(arg, ":", 2)
// sanity check, SplitAfterN should always return a length 2 slice
if len(msgs) < 2 {
tc.writeText("/dm command not understood, commands appears to be malformed. Type '/help' to see how to use each command.\n")
return
}
user := strings.TrimSpace(strings.TrimRight(msgs[0], ":"))
if user == "" {
tc.writeText("/dm command not understood, you're missing a username.\n")
return
}
if user == tc.username {
tc.writeText("You can't send a dm to yourself.\n")
return
}
msg := strings.TrimSpace(msgs[1])
if msg == "" {
tc.writeText("/dm command not understood, it looks like your message is blank.\n")
return
}
tc.send <- (newMessage(user, tc.username, msg, dm))
}
func listUsersCmd(tc *tcpUser, arg string) {
if arg != "" {
tc.send <- newMessage(arg, tc.username, "", listUsers)
return
}
tc.send <- newMessage("", tc.username, "", listUsers)
}
func listRoomsCmd(tc *tcpUser, _ string) {
tc.send <- newMessage("", tc.username, "", listChannels)
}