dispatch/server/websocket_handler.go

224 lines
4.4 KiB
Go
Raw Normal View History

package server
2015-01-17 01:37:21 +00:00
import (
"encoding/json"
"log"
"strings"
2015-01-17 01:37:21 +00:00
2015-05-01 22:20:22 +00:00
"github.com/khlieng/name_pending/Godeps/_workspace/src/github.com/gorilla/websocket"
2015-06-05 22:34:13 +00:00
"github.com/khlieng/name_pending/irc"
"github.com/khlieng/name_pending/storage"
2015-01-17 01:37:21 +00:00
)
2015-06-05 22:34:13 +00:00
func handleWS(conn *websocket.Conn) {
defer conn.Close()
2015-01-17 01:37:21 +00:00
var session *Session
var UUID string
2015-06-05 22:34:13 +00:00
addr := conn.RemoteAddr().String()
ws := newConn(conn)
defer ws.close()
go ws.send()
go ws.recv()
2015-01-17 01:37:21 +00:00
log.Println(addr, "connected")
for {
2015-06-05 22:34:13 +00:00
req, ok := <-ws.in
if !ok {
2015-01-17 01:37:21 +00:00
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
session.setWS(addr, ws)
2015-01-17 01:37:21 +00:00
log.Println(addr, "attached to", session.numIRC(), "existing IRC connections")
2015-01-17 01:37:21 +00:00
channels := session.user.GetChannels()
for i, channel := range channels {
channels[i].Topic = channelStore.GetTopic(channel.Server, channel.Name)
2015-01-17 01:37:21 +00:00
}
session.sendJSON("channels", channels)
session.sendJSON("servers", session.user.GetServers())
for _, channel := range channels {
session.sendJSON("users", Userlist{
Server: channel.Server,
Channel: channel.Name,
Users: channelStore.GetUsers(channel.Server, channel.Name),
})
}
2015-01-17 01:37:21 +00:00
} else {
session = NewSession()
session.user = storage.NewUser(UUID)
sessions[UUID] = session
sessionLock.Unlock()
session.setWS(addr, ws)
2015-05-04 21:52:34 +00:00
session.sendJSON("servers", nil)
2015-01-17 01:37:21 +00:00
go session.write()
}
case "connect":
var data Connect
json.Unmarshal(req.Request, &data)
if _, ok := session.getIRC(data.Server); !ok {
log.Println(addr, "connecting to", data.Server)
2015-06-05 22:34:13 +00:00
i := irc.NewClient(data.Nick, data.Username)
i.TLS = data.TLS
i.Password = data.Password
i.Realname = data.Realname
2015-01-17 01:37:21 +00:00
if idx := strings.Index(data.Server, ":"); idx < 0 {
2015-06-05 22:34:13 +00:00
session.setIRC(data.Server, i)
} else {
2015-06-05 22:34:13 +00:00
session.setIRC(data.Server[:idx], i)
}
i.Connect(data.Server)
2015-06-16 21:20:53 +00:00
go newIRCHandler(i, session).run()
session.user.AddServer(storage.Server{
Name: data.Name,
Address: i.Host,
TLS: data.TLS,
Password: data.Password,
Nick: data.Nick,
Username: data.Username,
Realname: data.Realname,
})
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)
2015-06-05 22:34:13 +00:00
if i, ok := session.getIRC(data.Server); ok {
i.Join(data.Channels...)
2015-01-17 01:37:21 +00:00
}
case "part":
var data Part
2015-01-17 01:37:21 +00:00
json.Unmarshal(req.Request, &data)
2015-06-05 22:34:13 +00:00
if i, ok := session.getIRC(data.Server); ok {
i.Part(data.Channels...)
2015-01-17 01:37:21 +00:00
}
case "quit":
var data Quit
json.Unmarshal(req.Request, &data)
2015-06-05 22:34:13 +00:00
if i, ok := session.getIRC(data.Server); ok {
i.Quit()
session.deleteIRC(data.Server)
2015-06-05 22:34:13 +00:00
channelStore.RemoveUserAll(i.GetNick(), data.Server)
session.user.RemoveServer(data.Server)
}
2015-01-17 01:37:21 +00:00
case "chat":
var data Chat
json.Unmarshal(req.Request, &data)
2015-06-05 22:34:13 +00:00
if i, ok := session.getIRC(data.Server); ok {
i.Privmsg(data.To, data.Message)
2015-01-17 01:37:21 +00:00
}
2015-02-04 01:43:49 +00:00
case "nick":
var data Nick
json.Unmarshal(req.Request, &data)
2015-06-05 22:34:13 +00:00
if i, ok := session.getIRC(data.Server); ok {
i.Nick(data.New)
2015-02-04 01:43:49 +00:00
session.user.SetNick(data.New, data.Server)
}
case "invite":
var data Invite
json.Unmarshal(req.Request, &data)
2015-06-05 22:34:13 +00:00
if i, ok := session.getIRC(data.Server); ok {
i.Invite(data.User, data.Channel)
}
case "kick":
var data Invite
json.Unmarshal(req.Request, &data)
2015-06-05 22:34:13 +00:00
if i, ok := session.getIRC(data.Server); ok {
i.Kick(data.Channel, data.User)
}
case "whois":
var data Whois
json.Unmarshal(req.Request, &data)
2015-06-05 22:34:13 +00:00
if i, ok := session.getIRC(data.Server); ok {
i.Whois(data.User)
}
2015-02-21 12:06:05 +00:00
case "away":
var data Away
json.Unmarshal(req.Request, &data)
2015-06-05 22:34:13 +00:00
if i, ok := session.getIRC(data.Server); ok {
i.Away(data.Message)
2015-02-21 12:06:05 +00:00
}
case "search":
go func() {
var data SearchRequest
json.Unmarshal(req.Request, &data)
results, err := session.user.SearchMessages(data.Server, data.Channel, data.Phrase)
if err != nil {
log.Println(err)
return
}
session.sendJSON("search", SearchResult{
Server: data.Server,
Channel: data.Channel,
Results: results,
})
}()
2015-01-17 01:37:21 +00:00
}
}
}