2015-01-17 01:37:21 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"golang.org/x/net/websocket"
|
|
|
|
|
2015-01-21 21:59:08 +00:00
|
|
|
"github.com/khlieng/name_pending/storage"
|
2015-01-17 01:37:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func handleWS(ws *websocket.Conn) {
|
|
|
|
defer ws.Close()
|
|
|
|
|
|
|
|
var session *Session
|
|
|
|
var UUID string
|
|
|
|
var req WSRequest
|
|
|
|
|
|
|
|
addr := ws.Request().RemoteAddr
|
|
|
|
|
|
|
|
log.Println(addr, "connected")
|
|
|
|
|
|
|
|
for {
|
|
|
|
err := websocket.JSON.Receive(ws, &req)
|
|
|
|
if err != nil {
|
|
|
|
if session != nil {
|
|
|
|
session.deleteWS(addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println(addr, "disconnected")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch req.Type {
|
|
|
|
case "uuid":
|
|
|
|
json.Unmarshal(req.Request, &UUID)
|
|
|
|
|
|
|
|
log.Println(addr, "set UUID", UUID)
|
|
|
|
|
|
|
|
sessionLock.Lock()
|
|
|
|
|
|
|
|
if storedSession, exists := sessions[UUID]; exists {
|
|
|
|
sessionLock.Unlock()
|
|
|
|
session = storedSession
|
|
|
|
|
|
|
|
log.Println(addr, "attached to existing IRC connections")
|
|
|
|
|
|
|
|
channels := session.user.GetChannels()
|
|
|
|
for i, channel := range channels {
|
|
|
|
channels[i].Users = channelStore.GetUsers(channel.Server, channel.Name)
|
2015-01-21 02:06:34 +00:00
|
|
|
channels[i].Topic = channelStore.GetTopic(channel.Server, channel.Name)
|
2015-01-17 01:37:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
session.sendJSON("channels", channels)
|
2015-01-22 00:03:25 +00:00
|
|
|
session.sendJSON("servers", session.user.GetServers())
|
2015-01-17 01:37:21 +00:00
|
|
|
} else {
|
|
|
|
session = NewSession()
|
|
|
|
session.user = storage.NewUser(UUID)
|
|
|
|
|
|
|
|
sessions[UUID] = session
|
|
|
|
sessionLock.Unlock()
|
|
|
|
|
|
|
|
go session.write()
|
|
|
|
}
|
|
|
|
|
|
|
|
session.setWS(addr, ws)
|
|
|
|
|
|
|
|
case "connect":
|
|
|
|
var data Connect
|
|
|
|
|
|
|
|
json.Unmarshal(req.Request, &data)
|
|
|
|
|
|
|
|
if _, ok := session.getIRC(data.Server); !ok {
|
|
|
|
log.Println(addr, "connecting to", data.Server)
|
|
|
|
|
|
|
|
irc := NewIRC(data.Nick, data.Username)
|
2015-01-29 23:38:51 +00:00
|
|
|
irc.TLS = data.TLS
|
2015-01-17 01:37:21 +00:00
|
|
|
|
2015-01-29 23:38:51 +00:00
|
|
|
if err := irc.Connect(data.Server); err != nil {
|
|
|
|
session.sendError(err, irc.Host)
|
|
|
|
log.Println(err)
|
|
|
|
} else {
|
|
|
|
session.setIRC(irc.Host, irc)
|
2015-01-17 01:37:21 +00:00
|
|
|
|
2015-01-29 23:38:51 +00:00
|
|
|
go handleMessages(irc, session)
|
2015-01-17 01:37:21 +00:00
|
|
|
|
2015-01-29 23:38:51 +00:00
|
|
|
session.user.AddServer(storage.Server{
|
|
|
|
Address: irc.Host,
|
2015-02-02 00:54:26 +00:00
|
|
|
TLS: data.TLS,
|
|
|
|
Name: data.Name,
|
2015-01-29 23:38:51 +00:00
|
|
|
Nick: data.Nick,
|
|
|
|
Username: data.Username,
|
|
|
|
})
|
|
|
|
}
|
2015-01-17 01:37:21 +00:00
|
|
|
} else {
|
|
|
|
log.Println(addr, "already connected to", data.Server)
|
|
|
|
}
|
|
|
|
|
|
|
|
case "join":
|
|
|
|
var data Join
|
|
|
|
|
|
|
|
json.Unmarshal(req.Request, &data)
|
|
|
|
|
|
|
|
if irc, ok := session.getIRC(data.Server); ok {
|
|
|
|
irc.Join(data.Channels...)
|
|
|
|
}
|
|
|
|
|
|
|
|
case "part":
|
2015-02-01 00:56:56 +00:00
|
|
|
var data Part
|
2015-01-17 01:37:21 +00:00
|
|
|
|
|
|
|
json.Unmarshal(req.Request, &data)
|
|
|
|
|
|
|
|
if irc, ok := session.getIRC(data.Server); ok {
|
|
|
|
irc.Part(data.Channels...)
|
|
|
|
}
|
|
|
|
|
2015-01-21 02:06:34 +00:00
|
|
|
case "quit":
|
|
|
|
var data Quit
|
|
|
|
|
|
|
|
json.Unmarshal(req.Request, &data)
|
|
|
|
|
|
|
|
if irc, ok := session.getIRC(data.Server); ok {
|
|
|
|
channelStore.RemoveUserAll(irc.nick, data.Server)
|
|
|
|
session.user.RemoveServer(data.Server)
|
|
|
|
irc.Quit()
|
|
|
|
}
|
|
|
|
|
2015-01-17 01:37:21 +00:00
|
|
|
case "chat":
|
|
|
|
var data Chat
|
|
|
|
|
|
|
|
json.Unmarshal(req.Request, &data)
|
|
|
|
|
|
|
|
if irc, ok := session.getIRC(data.Server); ok {
|
|
|
|
irc.Privmsg(data.To, data.Message)
|
|
|
|
}
|
2015-02-04 01:43:49 +00:00
|
|
|
|
|
|
|
case "nick":
|
|
|
|
var data Nick
|
|
|
|
|
|
|
|
json.Unmarshal(req.Request, &data)
|
|
|
|
|
|
|
|
if irc, ok := session.getIRC(data.Server); ok {
|
|
|
|
irc.Nick(data.New)
|
|
|
|
session.user.SetNick(data.New, data.Server)
|
|
|
|
}
|
2015-01-17 01:37:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|