-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathirc_server.go
More file actions
258 lines (215 loc) · 6.55 KB
/
irc_server.go
File metadata and controls
258 lines (215 loc) · 6.55 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
package main
import (
"bufio"
"io"
"log"
"net"
"strings"
)
type user struct {
username string
nickname string
password string
cur_channel string
connection net.Conn
message string
}
type irc struct {
channelsMap map[string]map[*user]net.Conn
usersMap map[string]*user
nicknameMap map[string]*user
}
func handleConnection(conn net.Conn, instance irc) {
defer conn.Close()
s := bufio.NewScanner(conn)
// A. User log in
inst, user := login(instance, conn, s)
userPtr := inst.usersMap[user]
// B. Server waits for and executes instructions and messages
func() {
io.WriteString(conn, "["+userPtr.cur_channel+"] "+userPtr.nickname+": ")
for s.Scan() {
text := s.Text()
chMap := inst.channelsMap
uMap := inst.usersMap
nickMap := inst.nicknameMap
if len(text) > 0 {
switch arg := strings.SplitN(text, " ", 3); arg[0] {
case "/nick": // Change nickname
if len(arg) == 2 {
if verifyInput(arg[1]) == false || len(arg[1]) == 0 {
io.WriteString(conn, "Invalid nickname. Try again.\n")
} else {
nickOps(arg[1], nickMap, userPtr, true)
}
} else {
io.WriteString(conn, "Command usage: /nick newNickName\n")
}
case "/join": // Makes the user join a channel
if len(arg) == 2 {
if verifyInput(arg[1]) == false || len(arg[1]) == 0 {
io.WriteString(conn, "Invalid channel. Try again.\n")
} else {
leaveChannel(userPtr.cur_channel, chMap, uMap, user, userPtr, conn, s)
joinChannel(arg[1], chMap, uMap, user, userPtr, conn, s)
}
} else {
io.WriteString(conn, "Command usage: /join channelName\n")
}
case "/part": // Makes the user leave a channel
if len(arg) == 1 {
leaveChannel(userPtr.cur_channel, chMap, uMap, user, userPtr, conn, s)
joinChannel("waiting", chMap, uMap, user, userPtr, conn, s)
} else {
io.WriteString(conn, "Command usage: /part\n")
}
case "/names": // Lists all users connected to the server
if len(arg) == 1 {
io.WriteString(userPtr.connection, "Users connected to the server: ")
for _, u := range uMap {
if u.connection != nil {
io.WriteString(conn, u.nickname+" ")
}
}
io.WriteString(conn, "\n")
} else {
io.WriteString(conn, "Command usage: /names")
}
case "/list": // Lists all channels in the server
if len(arg) == 1 {
io.WriteString(conn, "List of channels: ")
for ch, _ := range chMap {
io.WriteString(conn, ch+" ")
}
io.WriteString(conn, "\n")
}
case "/privmsg": // Send a message to another user or a channel
if len(arg) == 3 {
if verifyInput(arg[1]) == false || len(arg[1]) == 0 {
io.WriteString(conn, "Invalid user/channel. Try again.\n")
} else {
if _, ok := nickMap[arg[1]]; ok {
dst := nickMap[arg[1]].username
msg := "\nPrivate message from (" + userPtr.nickname + "): " + arg[2] + "\n"
privateMsg(dst, uMap, msg, user)
} else if authChannel(inst.channelsMap, arg[1]) {
msg := "\nChannel message from (" + userPtr.nickname + ") in [" + userPtr.cur_channel + "] channel: " + arg[2] + "\n"
channelMsg(arg[1], chMap, uMap, msg, userPtr.connection)
} else {
io.WriteString(conn, "Invalid user/channel. Try again.\n")
}
}
} else {
io.WriteString(conn, "Command usage: /privmsg nickName|channelName [msg]\n")
}
case "/exit":
if len(arg) == 1 {
defer conn.Close()
delete(inst.channelsMap[userPtr.cur_channel], userPtr)
userPtr.cur_channel = ""
userPtr.connection = nil
userPtr.message = ""
return
}
default:
if verifyInput(arg[0]) {
msg := "[" + userPtr.cur_channel + "] (" + userPtr.nickname + "): " + text + "\n"
channelMsg(uMap[user].cur_channel, chMap, uMap, msg, conn)
}
}
}
io.WriteString(conn, "["+userPtr.cur_channel+"] "+userPtr.nickname+": ")
}
}()
}
// A) Log-in sequence
func login(inst irc, conn net.Conn, s *bufio.Scanner) (irc, string) {
chMap := inst.channelsMap
uMap := inst.usersMap
nickMap := inst.nicknameMap
io.WriteString(conn, "Enter username: ")
s.Scan()
uName := s.Text()
for !verifyInput(uName) {
io.WriteString(conn, "Invalid characters used. Try again. \nEnter a username: ")
s.Scan()
uName = s.Text()
}
if authUser(uMap, uName) { // User exists, check password
io.WriteString(conn, "Enter password: ")
s.Scan()
passwd := s.Text()
for passwd != uMap[uName].password {
io.WriteString(conn, "Password incorrect. Try again.\n")
io.WriteString(conn, "Enter password: ")
s.Scan()
passwd = s.Text()
}
} else { // User doesn't exist, create new user
userPtr := createUser(uName, uMap)
io.WriteString(conn, "Enter a nickname: ")
s.Scan()
newNick := s.Text()
vi := verifyInput(newNick)
au := authUser(nickMap, newNick)
// Check if nickname contains invalid characters & if nickname already exists
for vi == false || au == true {
if !vi {
io.WriteString(conn, "Invalid characters used. Try again.\nEnter a nickname: ")
}
if au {
io.WriteString(conn, "Nickname already in use. Try again.\nEnter a nickname: ")
}
s.Scan()
newNick = s.Text()
vi = verifyInput(newNick)
au = authUser(nickMap, newNick)
}
// Create new nickname
nickOps(newNick, nickMap, userPtr, false)
// New user creates a password
io.WriteString(conn, "Enter a password: ")
s.Scan()
newPasswd := s.Text()
for !verifyInput(newPasswd) {
io.WriteString(conn, "Invalid characters used. Try again. \nEnter a password: ")
s.Scan()
newPasswd = s.Text()
}
uMap[uName].password = newPasswd
}
uMap[uName].connection = conn
// Prompt user to enter a channel
io.WriteString(conn, "Enter a channel name: ")
s.Scan()
ch := s.Text()
userPtr := uMap[uName]
joinChannel(ch, chMap, uMap, uName, userPtr, conn, s)
return inst, uName
}
func main() {
// Listen on TCP port 6667 on all available unicast and
// anycast IP addresses of the local system
ln, err := net.Listen("tcp", ":6667")
if err != nil {
log.Fatal(err)
}
defer ln.Close()
instance := irc{
channelsMap: make(map[string]map[*user]net.Conn),
usersMap: make(map[string]*user),
nicknameMap: make(map[string]*user),
}
for {
//Wait for a connection
conn, err := ln.Accept()
if err != nil {
log.Fatal(err)
}
// Handle the connection in a netw goroutine
// The loop then returns to accepting, so that
// multiple connections may be served concurrently
go handleConnection(conn, instance)
// Echo all incoming data
}
}