-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
383 lines (294 loc) · 8.91 KB
/
main.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
package main
import (
"bufio"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"strconv"
"strings"
"time"
)
type ChatMessage struct {
msg string
id int
}
type User struct {
id int
name string
muteList map[int]string //users to mute by id
}
type Config struct {
Host string `json:"host"`
Port string `json:"port"`
ConnType string `json:"connType"`
LogPath string `json:"logFilePath"`
}
// used for HTTP api
type HttpAPI struct {
b *Broker
cfg Config
}
// Global vars
var state = []User{}
var userNameList = map[string]int{} // map userNames to user ID
var msgHistory []string // keep last 30 messages in mem
func main() {
cfg := readConfigFromFile()
// add system user for system notifications
addNewUser("system")
// create and start our primary chatroom
b := NewBroker()
go b.Start()
// setup http server for http API access
api := &HttpAPI{cfg: cfg, b: b}
go func() {
http.HandleFunc("/messages", httpHistoryHandler)
http.HandleFunc("/post", api.httpPosthandler)
log.Fatal(http.ListenAndServe(":3000", nil))
}()
// Listen for incoming tcp connections ie: telnet
ln, err := net.Listen(cfg.ConnType, cfg.Host+":"+cfg.Port)
if err != nil {
fmt.Println("Error listening:", err.Error())
os.Exit(1)
}
defer ln.Close()
// handle new incoming connections
for {
conn, err := ln.Accept()
if err != nil {
fmt.Println("Error handling connection:", err.Error())
}
id := addNewUser("")
go handleConnection(conn, b, id, cfg)
}
}
/**
* HTTP handlers for http requests
**/
func httpHistoryHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Here are the most recent messages: \n%s", getMsgHistory())
}
func (api *HttpAPI) httpPosthandler(w http.ResponseWriter, r *http.Request) {
var userId int
r.ParseForm()
// TODO: check for params, and respond with error if some are missing
username := r.Form["username"][0]
msg := r.Form["msg"][0]
// TODO: add proper authentication
usernameExists := hasUserNameBeenTaken(username)
if usernameExists == true {
userId = userNameList[username]
} else {
userId = addNewUser(username)
}
sendMessage(msg, userId, api.b, api.cfg)
fmt.Fprintf(w, "Message successfully posted: \n%s", msgHistory[len(msgHistory)-1])
}
// handles each message we (the client) get from the server
func setupClientMsgHandler(id int, b *Broker, conn net.Conn) {
msgCh := b.Subscribe()
for {
msgData := <-msgCh
msgDetails := msgData.(ChatMessage)
// don't write message from self, or if author was muted
if id != msgDetails.id && isMessageAuthorMuted(id, msgDetails.id) == false {
conn.Write([]byte(msgDetails.msg))
}
}
}
func handleConnection(conn net.Conn, b *Broker, id int, cfg Config) {
remoteAddr := conn.RemoteAddr().String()
fmt.Printf("Client id: %d with username [%s] connected from %s\n", id, getUserName(id), remoteAddr)
msg := fmt.Sprintf("%s has joined", getUserName(id))
sendMessage(msg, 0, b, cfg)
printWelcomeMessage(conn, id)
// subscribe to all messages
go setupClientMsgHandler(id, b, conn)
scanner := bufio.NewScanner(conn)
// handles every message from the client
for {
ok := scanner.Scan()
msg := fmt.Sprintf(scanner.Text())
// if msg starts with / process as command
if len(msg) > 0 && msg[0] == '/' {
handleCommand(msg, conn, id, b, cfg)
} else {
sendMessage(msg, id, b, cfg)
}
if !ok {
break
}
}
fmt.Println("Client at " + remoteAddr + " disconnected.")
}
func handleCommand(message string, conn net.Conn, id int, b *Broker, cfg Config) {
if len(message) > 0 && message[0] == '/' {
switch {
case message == "/help":
msg := `> Commands you can run:
/help - See commands available to you
/history - See the last 30 messages
/name - Change your username
/mute [name] - mute another user
/unmute [name] - unmute a user you have muted previously
/quit - disconnect your client` + "\n"
// msg = formatMessage(msg, 0)
conn.Write([]byte(msg))
case message == "/history":
resp := strings.Join(msgHistory, "")
resp = "> Recent messages: \n" + resp
conn.Write([]byte(resp))
// starts with /name
case strings.HasPrefix(message, "/name "):
oldName := getUserName(id)
newName := strings.Replace(message, "/name ", "", -1)
nameTaken := hasUserNameBeenTaken(newName)
if nameTaken == true {
msg := "> I'm sorry. The userName [" + newName + "] has already been taken. Try another name\n"
conn.Write([]byte(msg))
return
}
changeUserName(id, newName)
// send as system message
msg := fmt.Sprintf("%s has changed their name to %s", oldName, newName)
sendMessage(msg, 0, b, cfg) // send system notification
case strings.HasPrefix(message, "/mute "):
userToMute := strings.Replace(message, "/mute ", "", -1)
muteUser(id, userToMute)
// success msg
msg := fmt.Sprintf("%s has muted %s", getUserName(id), userToMute)
sendMessage(msg, 0, b, cfg) // send system notification
case strings.HasPrefix(message, "/unmute "):
userToUnMute := strings.Replace(message, "/unmute ", "", -1)
unMuteUser(id, userToUnMute)
// success msg
msg := fmt.Sprintf("%s has UN-muted %s", getUserName(id), userToUnMute)
sendMessage(msg, 0, b, cfg) // send system notification
case message == "/quit":
msg := fmt.Sprintf("%s has left", getUserName(id))
sendMessage(msg, 0, b, cfg)
conn.Write([]byte("> Goodbye\n"))
conn.Close()
default:
conn.Write([]byte("Unrecognized command.\n"))
}
}
}
/******************************************
/* UTILS TODO: move to separate file?
/******************************************/
func readConfigFromFile() Config {
byteVal, _ := ioutil.ReadFile("./config.json") // read the entire file into memory
var cfg Config
// decode json and store in data map. handle err
if err := json.Unmarshal(byteVal, &cfg); err != nil {
panic(err)
}
return cfg
}
func getTimeStamp() (timestamp string) {
now := time.Now()
timestamp = fmt.Sprintf("%d:%d", now.Hour(),
now.Minute())
return timestamp
}
func printWelcomeMessage(conn net.Conn, id int) {
conn.Write([]byte("> Welcome to the chat room. Type /help for a list of available commands.\n> Your username is [" + getUserName(id) + "]\n"))
}
func changeUserName(id int, newName string) {
state[id].name = newName
userNameList[newName] = id
}
func hasUserNameBeenTaken(name string) bool {
if _, ok := userNameList[name]; ok {
return true
}
return false
}
// searches through mutelist of a user. Returns true if msg should be muted
func isMessageAuthorMuted(userIdRequestingMute int, messageAuthorId int) bool {
muteList := getMuteList(userIdRequestingMute)
if _, ok := muteList[messageAuthorId]; ok {
return true
}
return false
}
// returns id of newest user added
func addNewUser(userName string) int {
// newUser := User{name: userName, id: len(state), muteList: map[int]string{}}
newUser := User{name: userName, id: len(state), muteList: map[int]string{}}
state = append(state, newUser)
userNameList[newUser.name] = newUser.id
return len(state) - 1
}
// look up username
func getUserName(id int) string {
userName := state[id].name
if userName != "" {
return userName
}
userName = "Anon" + strconv.Itoa(id)
return userName
}
// retrieve saved messages in history
func getMsgHistory() string {
return strings.Join(msgHistory, "")
}
// add messages to history. limits history to 30 messages
func updateMsgHistory(msg string) {
// remove first item, if message history is 30
if len(msgHistory) > 29 {
msgHistory = append(msgHistory[1:], msg)
} else {
msgHistory = append(msgHistory, msg)
}
}
func getIdFromUserName(userName string) int {
return userNameList[userName]
}
func getMuteList(id int) map[int]string {
return state[id].muteList
}
// adds list of users to mute
func muteUser(requestorId int, userToMute string) {
idToMute := getIdFromUserName(userToMute)
state[requestorId].muteList[idToMute] = "" // hash map for quick and easy access. Don't care about the value
}
func unMuteUser(requestorId int, userToUnMute string) {
idToUnMute := getIdFromUserName(userToUnMute)
delete(state[requestorId].muteList, idToUnMute)
}
// adds the metadata to the message
func formatMessage(msg string, id int) string {
formattedMsg := fmt.Sprintf("%s [%s] %s\n", getTimeStamp(), getUserName(id), msg)
return formattedMsg
}
// format and send message to all connected clients
func sendMessage(msg string, id int, b *Broker, cfg Config) {
// avoid phantom message when user disconnects. TODO: probably need to clean up channel
if msg == "" {
return
}
msg = formatMessage(msg, id)
msgData := ChatMessage{msg: msg, id: id}
b.Publish(msgData) // send to all users
logMessage(msg, cfg) // log message
updateMsgHistory(msg)
}
func logMessage(msg string, cfg Config) {
fmt.Print(msg)
saveLogToFile(msg, cfg.LogPath)
}
func saveLogToFile(msg string, logFilePath string) {
f, err := os.OpenFile(logFilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
panic(err)
}
defer f.Close()
fmt.Fprintf(f, msg)
}