2015-05-01 20:59:46 +00:00
|
|
|
package server
|
2015-01-17 01:37:21 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"log"
|
2015-04-29 21:54:44 +00:00
|
|
|
"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-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
|
|
|
|
|
2015-05-01 22:20:22 +00:00
|
|
|
addr := ws.RemoteAddr().String()
|
2015-01-17 01:37:21 +00:00
|
|
|
|
|
|
|
log.Println(addr, "connected")
|
|
|
|
|
|
|
|
for {
|
2015-05-01 22:20:22 +00:00
|
|
|
err := ws.ReadJSON(&req)
|
2015-01-17 01:37:21 +00:00
|
|
|
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
|
|
|
|
|
2015-02-09 00:00:30 +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 {
|
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-02-12 01:44:15 +00:00
|
|
|
|
|
|
|
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()
|
|
|
|
|
2015-05-04 21:52:34 +00:00
|
|
|
session.sendJSON("servers", nil)
|
|
|
|
|
2015-01-17 01:37:21 +00:00
|
|
|
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-02-07 02:10:58 +00:00
|
|
|
irc.Password = data.Password
|
|
|
|
irc.Realname = data.Realname
|
2015-01-17 01:37:21 +00:00
|
|
|
|
2015-04-29 21:54:44 +00:00
|
|
|
if idx := strings.Index(data.Server, ":"); idx < 0 {
|
|
|
|
session.setIRC(data.Server, irc)
|
2015-01-29 23:38:51 +00:00
|
|
|
} else {
|
2015-04-29 21:54:44 +00:00
|
|
|
session.setIRC(data.Server[:idx], irc)
|
2015-01-29 23:38:51 +00:00
|
|
|
}
|
2015-04-29 21:54:44 +00:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
err := irc.Connect(data.Server)
|
|
|
|
if err != nil {
|
|
|
|
session.deleteIRC(irc.Host)
|
|
|
|
session.sendError(err, irc.Host)
|
|
|
|
log.Println(err)
|
|
|
|
} else {
|
|
|
|
go handleMessages(irc, session)
|
|
|
|
|
|
|
|
session.user.AddServer(storage.Server{
|
|
|
|
Name: data.Name,
|
|
|
|
Address: irc.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)
|
|
|
|
|
|
|
|
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 {
|
2015-02-09 00:00:30 +00:00
|
|
|
irc.Quit()
|
|
|
|
session.deleteIRC(data.Server)
|
2015-02-06 23:16:51 +00:00
|
|
|
channelStore.RemoveUserAll(irc.GetNick(), data.Server)
|
2015-01-21 02:06:34 +00:00
|
|
|
session.user.RemoveServer(data.Server)
|
|
|
|
}
|
|
|
|
|
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-02-10 01:25:28 +00:00
|
|
|
|
|
|
|
case "invite":
|
|
|
|
var data Invite
|
|
|
|
|
|
|
|
json.Unmarshal(req.Request, &data)
|
|
|
|
|
|
|
|
if irc, ok := session.getIRC(data.Server); ok {
|
|
|
|
irc.Invite(data.User, data.Channel)
|
|
|
|
}
|
|
|
|
|
|
|
|
case "kick":
|
|
|
|
var data Invite
|
|
|
|
|
|
|
|
json.Unmarshal(req.Request, &data)
|
|
|
|
|
|
|
|
if irc, ok := session.getIRC(data.Server); ok {
|
|
|
|
irc.Kick(data.Channel, data.User)
|
|
|
|
}
|
|
|
|
|
|
|
|
case "whois":
|
|
|
|
var data Whois
|
|
|
|
|
|
|
|
json.Unmarshal(req.Request, &data)
|
|
|
|
|
|
|
|
if irc, ok := session.getIRC(data.Server); ok {
|
|
|
|
irc.Whois(data.User)
|
|
|
|
}
|
2015-02-21 12:06:05 +00:00
|
|
|
|
|
|
|
case "away":
|
|
|
|
var data Away
|
|
|
|
|
|
|
|
json.Unmarshal(req.Request, &data)
|
|
|
|
|
|
|
|
if irc, ok := session.getIRC(data.Server); ok {
|
|
|
|
irc.Away(data.Message)
|
|
|
|
}
|
2015-01-17 01:37:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|