-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.go
130 lines (106 loc) · 2.98 KB
/
command.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
package chat_application
import "fmt"
type CommandFunc func(u *User, c []string) error
type Command struct {
name string
help string
commandFunc CommandFunc
}
var ServerCommands map[string]Command
func displayCommandHelp(u *User, c []string) error {
commandHelp := "\n"
for _, command := range ServerCommands {
commandHelp += command.help + "\n"
}
commandHelp += "\n"
u.msgToUser <- []byte(commandHelp)
return nil
}
func displayRooms(u *User, c []string) error {
roomMtx.Lock()
defer roomMtx.Unlock()
roomList := "\nROOMS available:\n"
for room, _ := range roomMap {
roomList = fmt.Sprintf("%s\n%s\n", roomList, room)
}
roomList += "\n"
u.msgToUser <- []byte(roomList)
return nil
}
func joinRoom(u *User, c []string) error {
if len(c) < 2 {
u.msgToUser <- []byte("Room not specified")
return nil
}
var err error
roomMtx.Lock()
room, ok := roomMap[c[1]]
roomMtx.Unlock()
if !ok {
// Room does not exist, create it before joining
room, err = NewRoom(c[1])
if err != nil {
return err
}
go room.Loop()
}
room.Join(u)
return nil
}
func leaveRoom(u *User, c []string) error {
if u.room != nil {
u.room.Leave(u)
}
return nil
}
func newNick(u *User, c []string) error {
if len(c) < 2 {
u.WriteTimestampedMessage([]byte("New nickname not specified"))
return nil
}
oldName := u.name
newName := c[1]
userMapMtx.Lock()
if _, ok := userMap[newName]; ok {
userMapMtx.Unlock()
msg := []byte(fmt.Sprintf("Cannot choose %s as new nick. It is already being used", newName))
u.WriteTimestampedMessage(msg)
return nil
}
delete(userMap, oldName)
u.name = newName
userMap[newName] = u
userMapMtx.Unlock()
msg := []byte(fmt.Sprintf("%s has been renamed to %s", oldName, newName))
if u.room != nil {
u.room.WriteTimestampedMessage(msg)
return nil
}
// User is not in a room, so send message directly to user
u.WriteTimestampedMessage(msg)
return nil
}
func usersInRoom(u *User, c []string) error {
if u.room == nil {
return nil
}
u.room.usersMtx.Lock()
defer u.room.usersMtx.Unlock()
msg := fmt.Sprintf("\nUsers in room: %s\n\n", u.room.name)
for user, _ := range u.room.users {
msg = fmt.Sprintf("%s\n%s", msg, user.name)
}
msg += "\n\n"
u.msgToUser <- []byte(msg)
return nil
}
func RegisterCommands() {
ServerCommands = map[string]Command{
"/help": Command{name: "/help", help: "/help , display help output of all commands", commandFunc: displayCommandHelp},
"/join": Command{name: "/join", help: "/join #room1 , enter #room1 , create it if it does not exist", commandFunc: joinRoom},
"/nick": Command{name: "/nick", help: "/nick newName , change nickname to newName", commandFunc: newNick},
"/leave": Command{name: "/leave", help: "/leave , leave the current room", commandFunc: leaveRoom},
"/rooms": Command{name: "/rooms", help: "/rooms , display all available rooms", commandFunc: displayRooms},
"/who": Command{name: "/who", help: "/who , display the users in the current room", commandFunc: usersInRoom},
}
}